1/* Intel Ethernet Switch Host Interface Driver
2 * Copyright(c) 2013 - 2015 Intel Corporation.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11 * more details.
12 *
13 * The full GNU General Public License is included in this distribution in
14 * the file called "COPYING".
15 *
16 * Contact Information:
17 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
18 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
19 */
20
21#include "fm10k_vf.h"
22
23/**
24 *  fm10k_stop_hw_vf - Stop Tx/Rx units
25 *  @hw: pointer to hardware structure
26 *
27 **/
28static s32 fm10k_stop_hw_vf(struct fm10k_hw *hw)
29{
30	u8 *perm_addr = hw->mac.perm_addr;
31	u32 bal = 0, bah = 0;
32	s32 err;
33	u16 i;
34
35	/* we need to disable the queues before taking further steps */
36	err = fm10k_stop_hw_generic(hw);
37	if (err)
38		return err;
39
40	/* If permanent address is set then we need to restore it */
41	if (is_valid_ether_addr(perm_addr)) {
42		bal = (((u32)perm_addr[3]) << 24) |
43		      (((u32)perm_addr[4]) << 16) |
44		      (((u32)perm_addr[5]) << 8);
45		bah = (((u32)0xFF)	   << 24) |
46		      (((u32)perm_addr[0]) << 16) |
47		      (((u32)perm_addr[1]) << 8) |
48		       ((u32)perm_addr[2]);
49	}
50
51	/* The queues have already been disabled so we just need to
52	 * update their base address registers
53	 */
54	for (i = 0; i < hw->mac.max_queues; i++) {
55		fm10k_write_reg(hw, FM10K_TDBAL(i), bal);
56		fm10k_write_reg(hw, FM10K_TDBAH(i), bah);
57		fm10k_write_reg(hw, FM10K_RDBAL(i), bal);
58		fm10k_write_reg(hw, FM10K_RDBAH(i), bah);
59	}
60
61	return 0;
62}
63
64/**
65 *  fm10k_reset_hw_vf - VF hardware reset
66 *  @hw: pointer to hardware structure
67 *
68 *  This function should return the hardware to a state similar to the
69 *  one it is in after just being initialized.
70 **/
71static s32 fm10k_reset_hw_vf(struct fm10k_hw *hw)
72{
73	s32 err;
74
75	/* shut down queues we own and reset DMA configuration */
76	err = fm10k_stop_hw_vf(hw);
77	if (err)
78		return err;
79
80	/* Inititate VF reset */
81	fm10k_write_reg(hw, FM10K_VFCTRL, FM10K_VFCTRL_RST);
82
83	/* Flush write and allow 100us for reset to complete */
84	fm10k_write_flush(hw);
85	udelay(FM10K_RESET_TIMEOUT);
86
87	/* Clear reset bit and verify it was cleared */
88	fm10k_write_reg(hw, FM10K_VFCTRL, 0);
89	if (fm10k_read_reg(hw, FM10K_VFCTRL) & FM10K_VFCTRL_RST)
90		err = FM10K_ERR_RESET_FAILED;
91
92	return err;
93}
94
95/**
96 *  fm10k_init_hw_vf - VF hardware initialization
97 *  @hw: pointer to hardware structure
98 *
99 **/
100static s32 fm10k_init_hw_vf(struct fm10k_hw *hw)
101{
102	u32 tqdloc, tqdloc0 = ~fm10k_read_reg(hw, FM10K_TQDLOC(0));
103	s32 err;
104	u16 i;
105
106	/* assume we always have at least 1 queue */
107	for (i = 1; tqdloc0 && (i < FM10K_MAX_QUEUES_POOL); i++) {
108		/* verify the Descriptor cache offsets are increasing */
109		tqdloc = ~fm10k_read_reg(hw, FM10K_TQDLOC(i));
110		if (!tqdloc || (tqdloc == tqdloc0))
111			break;
112
113		/* check to verify the PF doesn't own any of our queues */
114		if (!~fm10k_read_reg(hw, FM10K_TXQCTL(i)) ||
115		    !~fm10k_read_reg(hw, FM10K_RXQCTL(i)))
116			break;
117	}
118
119	/* shut down queues we own and reset DMA configuration */
120	err = fm10k_disable_queues_generic(hw, i);
121	if (err)
122		return err;
123
124	/* record maximum queue count */
125	hw->mac.max_queues = i;
126
127	/* fetch default VLAN */
128	hw->mac.default_vid = (fm10k_read_reg(hw, FM10K_TXQCTL(0)) &
129			       FM10K_TXQCTL_VID_MASK) >> FM10K_TXQCTL_VID_SHIFT;
130
131	return 0;
132}
133
134/**
135 *  fm10k_is_slot_appropriate_vf - Indicate appropriate slot for this SKU
136 *  @hw: pointer to hardware structure
137 *
138 *  Looks at the PCIe bus info to confirm whether or not this slot can support
139 *  the necessary bandwidth for this device. Since the VF has no control over
140 *  the "slot" it is in, always indicate that the slot is appropriate.
141 **/
142static bool fm10k_is_slot_appropriate_vf(struct fm10k_hw *hw)
143{
144	return true;
145}
146
147/* This structure defines the attibutes to be parsed below */
148const struct fm10k_tlv_attr fm10k_mac_vlan_msg_attr[] = {
149	FM10K_TLV_ATTR_U32(FM10K_MAC_VLAN_MSG_VLAN),
150	FM10K_TLV_ATTR_BOOL(FM10K_MAC_VLAN_MSG_SET),
151	FM10K_TLV_ATTR_MAC_ADDR(FM10K_MAC_VLAN_MSG_MAC),
152	FM10K_TLV_ATTR_MAC_ADDR(FM10K_MAC_VLAN_MSG_DEFAULT_MAC),
153	FM10K_TLV_ATTR_MAC_ADDR(FM10K_MAC_VLAN_MSG_MULTICAST),
154	FM10K_TLV_ATTR_LAST
155};
156
157/**
158 *  fm10k_update_vlan_vf - Update status of VLAN ID in VLAN filter table
159 *  @hw: pointer to hardware structure
160 *  @vid: VLAN ID to add to table
161 *  @vsi: Reserved, should always be 0
162 *  @set: Indicates if this is a set or clear operation
163 *
164 *  This function adds or removes the corresponding VLAN ID from the VLAN
165 *  filter table for this VF.
166 **/
167static s32 fm10k_update_vlan_vf(struct fm10k_hw *hw, u32 vid, u8 vsi, bool set)
168{
169	struct fm10k_mbx_info *mbx = &hw->mbx;
170	u32 msg[4];
171
172	/* verify the index is not set */
173	if (vsi)
174		return FM10K_ERR_PARAM;
175
176	/* verify upper 4 bits of vid and length are 0 */
177	if ((vid << 16 | vid) >> 28)
178		return FM10K_ERR_PARAM;
179
180	/* encode set bit into the VLAN ID */
181	if (!set)
182		vid |= FM10K_VLAN_CLEAR;
183
184	/* generate VLAN request */
185	fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_MAC_VLAN);
186	fm10k_tlv_attr_put_u32(msg, FM10K_MAC_VLAN_MSG_VLAN, vid);
187
188	/* load onto outgoing mailbox */
189	return mbx->ops.enqueue_tx(hw, mbx, msg);
190}
191
192/**
193 *  fm10k_msg_mac_vlan_vf - Read device MAC address from mailbox message
194 *  @hw: pointer to the HW structure
195 *  @results: Attributes for message
196 *  @mbx: unused mailbox data
197 *
198 *  This function should determine the MAC address for the VF
199 **/
200s32 fm10k_msg_mac_vlan_vf(struct fm10k_hw *hw, u32 **results,
201			  struct fm10k_mbx_info *mbx)
202{
203	u8 perm_addr[ETH_ALEN];
204	u16 vid;
205	s32 err;
206
207	/* record MAC address requested */
208	err = fm10k_tlv_attr_get_mac_vlan(
209					results[FM10K_MAC_VLAN_MSG_DEFAULT_MAC],
210					perm_addr, &vid);
211	if (err)
212		return err;
213
214	ether_addr_copy(hw->mac.perm_addr, perm_addr);
215	hw->mac.default_vid = vid & (FM10K_VLAN_TABLE_VID_MAX - 1);
216	hw->mac.vlan_override = !!(vid & FM10K_VLAN_CLEAR);
217
218	return 0;
219}
220
221/**
222 *  fm10k_read_mac_addr_vf - Read device MAC address
223 *  @hw: pointer to the HW structure
224 *
225 *  This function should determine the MAC address for the VF
226 **/
227static s32 fm10k_read_mac_addr_vf(struct fm10k_hw *hw)
228{
229	u8 perm_addr[ETH_ALEN];
230	u32 base_addr;
231
232	base_addr = fm10k_read_reg(hw, FM10K_TDBAL(0));
233
234	/* last byte should be 0 */
235	if (base_addr << 24)
236		return  FM10K_ERR_INVALID_MAC_ADDR;
237
238	perm_addr[3] = (u8)(base_addr >> 24);
239	perm_addr[4] = (u8)(base_addr >> 16);
240	perm_addr[5] = (u8)(base_addr >> 8);
241
242	base_addr = fm10k_read_reg(hw, FM10K_TDBAH(0));
243
244	/* first byte should be all 1's */
245	if ((~base_addr) >> 24)
246		return  FM10K_ERR_INVALID_MAC_ADDR;
247
248	perm_addr[0] = (u8)(base_addr >> 16);
249	perm_addr[1] = (u8)(base_addr >> 8);
250	perm_addr[2] = (u8)(base_addr);
251
252	ether_addr_copy(hw->mac.perm_addr, perm_addr);
253	ether_addr_copy(hw->mac.addr, perm_addr);
254
255	return 0;
256}
257
258/**
259 *  fm10k_update_uc_addr_vf - Update device unicast addresses
260 *  @hw: pointer to the HW structure
261 *  @glort: unused
262 *  @mac: MAC address to add/remove from table
263 *  @vid: VLAN ID to add/remove from table
264 *  @add: Indicates if this is an add or remove operation
265 *  @flags: flags field to indicate add and secure - unused
266 *
267 *  This function is used to add or remove unicast MAC addresses for
268 *  the VF.
269 **/
270static s32 fm10k_update_uc_addr_vf(struct fm10k_hw *hw, u16 glort,
271				   const u8 *mac, u16 vid, bool add, u8 flags)
272{
273	struct fm10k_mbx_info *mbx = &hw->mbx;
274	u32 msg[7];
275
276	/* verify VLAN ID is valid */
277	if (vid >= FM10K_VLAN_TABLE_VID_MAX)
278		return FM10K_ERR_PARAM;
279
280	/* verify MAC address is valid */
281	if (!is_valid_ether_addr(mac))
282		return FM10K_ERR_PARAM;
283
284	/* verify we are not locked down on the MAC address */
285	if (is_valid_ether_addr(hw->mac.perm_addr) &&
286	    memcmp(hw->mac.perm_addr, mac, ETH_ALEN))
287		return FM10K_ERR_PARAM;
288
289	/* add bit to notify us if this is a set or clear operation */
290	if (!add)
291		vid |= FM10K_VLAN_CLEAR;
292
293	/* generate VLAN request */
294	fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_MAC_VLAN);
295	fm10k_tlv_attr_put_mac_vlan(msg, FM10K_MAC_VLAN_MSG_MAC, mac, vid);
296
297	/* load onto outgoing mailbox */
298	return mbx->ops.enqueue_tx(hw, mbx, msg);
299}
300
301/**
302 *  fm10k_update_mc_addr_vf - Update device multicast addresses
303 *  @hw: pointer to the HW structure
304 *  @glort: unused
305 *  @mac: MAC address to add/remove from table
306 *  @vid: VLAN ID to add/remove from table
307 *  @add: Indicates if this is an add or remove operation
308 *
309 *  This function is used to add or remove multicast MAC addresses for
310 *  the VF.
311 **/
312static s32 fm10k_update_mc_addr_vf(struct fm10k_hw *hw, u16 glort,
313				   const u8 *mac, u16 vid, bool add)
314{
315	struct fm10k_mbx_info *mbx = &hw->mbx;
316	u32 msg[7];
317
318	/* verify VLAN ID is valid */
319	if (vid >= FM10K_VLAN_TABLE_VID_MAX)
320		return FM10K_ERR_PARAM;
321
322	/* verify multicast address is valid */
323	if (!is_multicast_ether_addr(mac))
324		return FM10K_ERR_PARAM;
325
326	/* add bit to notify us if this is a set or clear operation */
327	if (!add)
328		vid |= FM10K_VLAN_CLEAR;
329
330	/* generate VLAN request */
331	fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_MAC_VLAN);
332	fm10k_tlv_attr_put_mac_vlan(msg, FM10K_MAC_VLAN_MSG_MULTICAST,
333				    mac, vid);
334
335	/* load onto outgoing mailbox */
336	return mbx->ops.enqueue_tx(hw, mbx, msg);
337}
338
339/**
340 *  fm10k_update_int_moderator_vf - Request update of interrupt moderator list
341 *  @hw: pointer to hardware structure
342 *
343 *  This function will issue a request to the PF to rescan our MSI-X table
344 *  and to update the interrupt moderator linked list.
345 **/
346static void fm10k_update_int_moderator_vf(struct fm10k_hw *hw)
347{
348	struct fm10k_mbx_info *mbx = &hw->mbx;
349	u32 msg[1];
350
351	/* generate MSI-X request */
352	fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_MSIX);
353
354	/* load onto outgoing mailbox */
355	mbx->ops.enqueue_tx(hw, mbx, msg);
356}
357
358/* This structure defines the attibutes to be parsed below */
359const struct fm10k_tlv_attr fm10k_lport_state_msg_attr[] = {
360	FM10K_TLV_ATTR_BOOL(FM10K_LPORT_STATE_MSG_DISABLE),
361	FM10K_TLV_ATTR_U8(FM10K_LPORT_STATE_MSG_XCAST_MODE),
362	FM10K_TLV_ATTR_BOOL(FM10K_LPORT_STATE_MSG_READY),
363	FM10K_TLV_ATTR_LAST
364};
365
366/**
367 *  fm10k_msg_lport_state_vf - Message handler for lport_state message from PF
368 *  @hw: Pointer to hardware structure
369 *  @results: pointer array containing parsed data
370 *  @mbx: Pointer to mailbox information structure
371 *
372 *  This handler is meant to capture the indication from the PF that we
373 *  are ready to bring up the interface.
374 **/
375s32 fm10k_msg_lport_state_vf(struct fm10k_hw *hw, u32 **results,
376			     struct fm10k_mbx_info *mbx)
377{
378	hw->mac.dglort_map = !results[FM10K_LPORT_STATE_MSG_READY] ?
379			     FM10K_DGLORTMAP_NONE : FM10K_DGLORTMAP_ZERO;
380
381	return 0;
382}
383
384/**
385 *  fm10k_update_lport_state_vf - Update device state in lower device
386 *  @hw: pointer to the HW structure
387 *  @glort: unused
388 *  @count: number of logical ports to enable - unused (always 1)
389 *  @enable: boolean value indicating if this is an enable or disable request
390 *
391 *  Notify the lower device of a state change.  If the lower device is
392 *  enabled we can add filters, if it is disabled all filters for this
393 *  logical port are flushed.
394 **/
395static s32 fm10k_update_lport_state_vf(struct fm10k_hw *hw, u16 glort,
396				       u16 count, bool enable)
397{
398	struct fm10k_mbx_info *mbx = &hw->mbx;
399	u32 msg[2];
400
401	/* reset glort mask 0 as we have to wait to be enabled */
402	hw->mac.dglort_map = FM10K_DGLORTMAP_NONE;
403
404	/* generate port state request */
405	fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_LPORT_STATE);
406	if (!enable)
407		fm10k_tlv_attr_put_bool(msg, FM10K_LPORT_STATE_MSG_DISABLE);
408
409	/* load onto outgoing mailbox */
410	return mbx->ops.enqueue_tx(hw, mbx, msg);
411}
412
413/**
414 *  fm10k_update_xcast_mode_vf - Request update of multicast mode
415 *  @hw: pointer to hardware structure
416 *  @glort: unused
417 *  @mode: integer value indicating mode being requested
418 *
419 *  This function will attempt to request a higher mode for the port
420 *  so that it can enable either multicast, multicast promiscuous, or
421 *  promiscuous mode of operation.
422 **/
423static s32 fm10k_update_xcast_mode_vf(struct fm10k_hw *hw, u16 glort, u8 mode)
424{
425	struct fm10k_mbx_info *mbx = &hw->mbx;
426	u32 msg[3];
427
428	if (mode > FM10K_XCAST_MODE_NONE)
429		return FM10K_ERR_PARAM;
430	/* generate message requesting to change xcast mode */
431	fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_LPORT_STATE);
432	fm10k_tlv_attr_put_u8(msg, FM10K_LPORT_STATE_MSG_XCAST_MODE, mode);
433
434	/* load onto outgoing mailbox */
435	return mbx->ops.enqueue_tx(hw, mbx, msg);
436}
437
438const struct fm10k_tlv_attr fm10k_1588_msg_attr[] = {
439	FM10K_TLV_ATTR_U64(FM10K_1588_MSG_TIMESTAMP),
440	FM10K_TLV_ATTR_LAST
441};
442
443/* currently there is no shared 1588 timestamp handler */
444
445/**
446 *  fm10k_update_hw_stats_vf - Updates hardware related statistics of VF
447 *  @hw: pointer to hardware structure
448 *  @stats: pointer to statistics structure
449 *
450 *  This function collects and aggregates per queue hardware statistics.
451 **/
452static void fm10k_update_hw_stats_vf(struct fm10k_hw *hw,
453				     struct fm10k_hw_stats *stats)
454{
455	fm10k_update_hw_stats_q(hw, stats->q, 0, hw->mac.max_queues);
456}
457
458/**
459 *  fm10k_rebind_hw_stats_vf - Resets base for hardware statistics of VF
460 *  @hw: pointer to hardware structure
461 *  @stats: pointer to the stats structure to update
462 *
463 *  This function resets the base for queue hardware statistics.
464 **/
465static void fm10k_rebind_hw_stats_vf(struct fm10k_hw *hw,
466				     struct fm10k_hw_stats *stats)
467{
468	/* Unbind Queue Statistics */
469	fm10k_unbind_hw_stats_q(stats->q, 0, hw->mac.max_queues);
470
471	/* Reinitialize bases for all stats */
472	fm10k_update_hw_stats_vf(hw, stats);
473}
474
475/**
476 *  fm10k_configure_dglort_map_vf - Configures GLORT entry and queues
477 *  @hw: pointer to hardware structure
478 *  @dglort: pointer to dglort configuration structure
479 *
480 *  Reads the configuration structure contained in dglort_cfg and uses
481 *  that information to then populate a DGLORTMAP/DEC entry and the queues
482 *  to which it has been assigned.
483 **/
484static s32 fm10k_configure_dglort_map_vf(struct fm10k_hw *hw,
485					 struct fm10k_dglort_cfg *dglort)
486{
487	/* verify the dglort pointer */
488	if (!dglort)
489		return FM10K_ERR_PARAM;
490
491	/* stub for now until we determine correct message for this */
492
493	return 0;
494}
495
496/**
497 *  fm10k_adjust_systime_vf - Adjust systime frequency
498 *  @hw: pointer to hardware structure
499 *  @ppb: adjustment rate in parts per billion
500 *
501 *  This function takes an adjustment rate in parts per billion and will
502 *  verify that this value is 0 as the VF cannot support adjusting the
503 *  systime clock.
504 *
505 *  If the ppb value is non-zero the return is ERR_PARAM else success
506 **/
507static s32 fm10k_adjust_systime_vf(struct fm10k_hw *hw, s32 ppb)
508{
509	/* The VF cannot adjust the clock frequency, however it should
510	 * already have a syntonic clock with whichever host interface is
511	 * running as the master for the host interface clock domain so
512	 * there should be not frequency adjustment necessary.
513	 */
514	return ppb ? FM10K_ERR_PARAM : 0;
515}
516
517/**
518 *  fm10k_read_systime_vf - Reads value of systime registers
519 *  @hw: pointer to the hardware structure
520 *
521 *  Function reads the content of 2 registers, combined to represent a 64 bit
522 *  value measured in nanoseconds.  In order to guarantee the value is accurate
523 *  we check the 32 most significant bits both before and after reading the
524 *  32 least significant bits to verify they didn't change as we were reading
525 *  the registers.
526 **/
527static u64 fm10k_read_systime_vf(struct fm10k_hw *hw)
528{
529	u32 systime_l, systime_h, systime_tmp;
530
531	systime_h = fm10k_read_reg(hw, FM10K_VFSYSTIME + 1);
532
533	do {
534		systime_tmp = systime_h;
535		systime_l = fm10k_read_reg(hw, FM10K_VFSYSTIME);
536		systime_h = fm10k_read_reg(hw, FM10K_VFSYSTIME + 1);
537	} while (systime_tmp != systime_h);
538
539	return ((u64)systime_h << 32) | systime_l;
540}
541
542static const struct fm10k_msg_data fm10k_msg_data_vf[] = {
543	FM10K_TLV_MSG_TEST_HANDLER(fm10k_tlv_msg_test),
544	FM10K_VF_MSG_MAC_VLAN_HANDLER(fm10k_msg_mac_vlan_vf),
545	FM10K_VF_MSG_LPORT_STATE_HANDLER(fm10k_msg_lport_state_vf),
546	FM10K_TLV_MSG_ERROR_HANDLER(fm10k_tlv_msg_error),
547};
548
549static struct fm10k_mac_ops mac_ops_vf = {
550	.get_bus_info		= &fm10k_get_bus_info_generic,
551	.reset_hw		= &fm10k_reset_hw_vf,
552	.init_hw		= &fm10k_init_hw_vf,
553	.start_hw		= &fm10k_start_hw_generic,
554	.stop_hw		= &fm10k_stop_hw_vf,
555	.is_slot_appropriate	= &fm10k_is_slot_appropriate_vf,
556	.update_vlan		= &fm10k_update_vlan_vf,
557	.read_mac_addr		= &fm10k_read_mac_addr_vf,
558	.update_uc_addr		= &fm10k_update_uc_addr_vf,
559	.update_mc_addr		= &fm10k_update_mc_addr_vf,
560	.update_xcast_mode	= &fm10k_update_xcast_mode_vf,
561	.update_int_moderator	= &fm10k_update_int_moderator_vf,
562	.update_lport_state	= &fm10k_update_lport_state_vf,
563	.update_hw_stats	= &fm10k_update_hw_stats_vf,
564	.rebind_hw_stats	= &fm10k_rebind_hw_stats_vf,
565	.configure_dglort_map	= &fm10k_configure_dglort_map_vf,
566	.get_host_state		= &fm10k_get_host_state_generic,
567	.adjust_systime		= &fm10k_adjust_systime_vf,
568	.read_systime		= &fm10k_read_systime_vf,
569};
570
571static s32 fm10k_get_invariants_vf(struct fm10k_hw *hw)
572{
573	fm10k_get_invariants_generic(hw);
574
575	return fm10k_pfvf_mbx_init(hw, &hw->mbx, fm10k_msg_data_vf, 0);
576}
577
578struct fm10k_info fm10k_vf_info = {
579	.mac		= fm10k_mac_vf,
580	.get_invariants	= &fm10k_get_invariants_vf,
581	.mac_ops	= &mac_ops_vf,
582};
583