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  **/
28 static 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  **/
71 static 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  **/
100 static 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 /* This structure defines the attibutes to be parsed below */
135 const struct fm10k_tlv_attr fm10k_mac_vlan_msg_attr[] = {
136 	FM10K_TLV_ATTR_U32(FM10K_MAC_VLAN_MSG_VLAN),
137 	FM10K_TLV_ATTR_BOOL(FM10K_MAC_VLAN_MSG_SET),
138 	FM10K_TLV_ATTR_MAC_ADDR(FM10K_MAC_VLAN_MSG_MAC),
139 	FM10K_TLV_ATTR_MAC_ADDR(FM10K_MAC_VLAN_MSG_DEFAULT_MAC),
140 	FM10K_TLV_ATTR_MAC_ADDR(FM10K_MAC_VLAN_MSG_MULTICAST),
141 	FM10K_TLV_ATTR_LAST
142 };
143 
144 /**
145  *  fm10k_update_vlan_vf - Update status of VLAN ID in VLAN filter table
146  *  @hw: pointer to hardware structure
147  *  @vid: VLAN ID to add to table
148  *  @vsi: Reserved, should always be 0
149  *  @set: Indicates if this is a set or clear operation
150  *
151  *  This function adds or removes the corresponding VLAN ID from the VLAN
152  *  filter table for this VF.
153  **/
154 static s32 fm10k_update_vlan_vf(struct fm10k_hw *hw, u32 vid, u8 vsi, bool set)
155 {
156 	struct fm10k_mbx_info *mbx = &hw->mbx;
157 	u32 msg[4];
158 
159 	/* verify the index is not set */
160 	if (vsi)
161 		return FM10K_ERR_PARAM;
162 
163 	/* verify upper 4 bits of vid and length are 0 */
164 	if ((vid << 16 | vid) >> 28)
165 		return FM10K_ERR_PARAM;
166 
167 	/* encode set bit into the VLAN ID */
168 	if (!set)
169 		vid |= FM10K_VLAN_CLEAR;
170 
171 	/* generate VLAN request */
172 	fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_MAC_VLAN);
173 	fm10k_tlv_attr_put_u32(msg, FM10K_MAC_VLAN_MSG_VLAN, vid);
174 
175 	/* load onto outgoing mailbox */
176 	return mbx->ops.enqueue_tx(hw, mbx, msg);
177 }
178 
179 /**
180  *  fm10k_msg_mac_vlan_vf - Read device MAC address from mailbox message
181  *  @hw: pointer to the HW structure
182  *  @results: Attributes for message
183  *  @mbx: unused mailbox data
184  *
185  *  This function should determine the MAC address for the VF
186  **/
187 s32 fm10k_msg_mac_vlan_vf(struct fm10k_hw *hw, u32 **results,
188 			  struct fm10k_mbx_info *mbx)
189 {
190 	u8 perm_addr[ETH_ALEN];
191 	u16 vid;
192 	s32 err;
193 
194 	/* record MAC address requested */
195 	err = fm10k_tlv_attr_get_mac_vlan(
196 					results[FM10K_MAC_VLAN_MSG_DEFAULT_MAC],
197 					perm_addr, &vid);
198 	if (err)
199 		return err;
200 
201 	ether_addr_copy(hw->mac.perm_addr, perm_addr);
202 	hw->mac.default_vid = vid & (FM10K_VLAN_TABLE_VID_MAX - 1);
203 	hw->mac.vlan_override = !!(vid & FM10K_VLAN_CLEAR);
204 
205 	return 0;
206 }
207 
208 /**
209  *  fm10k_read_mac_addr_vf - Read device MAC address
210  *  @hw: pointer to the HW structure
211  *
212  *  This function should determine the MAC address for the VF
213  **/
214 static s32 fm10k_read_mac_addr_vf(struct fm10k_hw *hw)
215 {
216 	u8 perm_addr[ETH_ALEN];
217 	u32 base_addr;
218 
219 	base_addr = fm10k_read_reg(hw, FM10K_TDBAL(0));
220 
221 	/* last byte should be 0 */
222 	if (base_addr << 24)
223 		return  FM10K_ERR_INVALID_MAC_ADDR;
224 
225 	perm_addr[3] = (u8)(base_addr >> 24);
226 	perm_addr[4] = (u8)(base_addr >> 16);
227 	perm_addr[5] = (u8)(base_addr >> 8);
228 
229 	base_addr = fm10k_read_reg(hw, FM10K_TDBAH(0));
230 
231 	/* first byte should be all 1's */
232 	if ((~base_addr) >> 24)
233 		return  FM10K_ERR_INVALID_MAC_ADDR;
234 
235 	perm_addr[0] = (u8)(base_addr >> 16);
236 	perm_addr[1] = (u8)(base_addr >> 8);
237 	perm_addr[2] = (u8)(base_addr);
238 
239 	ether_addr_copy(hw->mac.perm_addr, perm_addr);
240 	ether_addr_copy(hw->mac.addr, perm_addr);
241 
242 	return 0;
243 }
244 
245 /**
246  *  fm10k_update_uc_addr_vf - Update device unicast addresses
247  *  @hw: pointer to the HW structure
248  *  @glort: unused
249  *  @mac: MAC address to add/remove from table
250  *  @vid: VLAN ID to add/remove from table
251  *  @add: Indicates if this is an add or remove operation
252  *  @flags: flags field to indicate add and secure - unused
253  *
254  *  This function is used to add or remove unicast MAC addresses for
255  *  the VF.
256  **/
257 static s32 fm10k_update_uc_addr_vf(struct fm10k_hw *hw, u16 glort,
258 				   const u8 *mac, u16 vid, bool add, u8 flags)
259 {
260 	struct fm10k_mbx_info *mbx = &hw->mbx;
261 	u32 msg[7];
262 
263 	/* verify VLAN ID is valid */
264 	if (vid >= FM10K_VLAN_TABLE_VID_MAX)
265 		return FM10K_ERR_PARAM;
266 
267 	/* verify MAC address is valid */
268 	if (!is_valid_ether_addr(mac))
269 		return FM10K_ERR_PARAM;
270 
271 	/* verify we are not locked down on the MAC address */
272 	if (is_valid_ether_addr(hw->mac.perm_addr) &&
273 	    memcmp(hw->mac.perm_addr, mac, ETH_ALEN))
274 		return FM10K_ERR_PARAM;
275 
276 	/* add bit to notify us if this is a set or clear operation */
277 	if (!add)
278 		vid |= FM10K_VLAN_CLEAR;
279 
280 	/* generate VLAN request */
281 	fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_MAC_VLAN);
282 	fm10k_tlv_attr_put_mac_vlan(msg, FM10K_MAC_VLAN_MSG_MAC, mac, vid);
283 
284 	/* load onto outgoing mailbox */
285 	return mbx->ops.enqueue_tx(hw, mbx, msg);
286 }
287 
288 /**
289  *  fm10k_update_mc_addr_vf - Update device multicast addresses
290  *  @hw: pointer to the HW structure
291  *  @glort: unused
292  *  @mac: MAC address to add/remove from table
293  *  @vid: VLAN ID to add/remove from table
294  *  @add: Indicates if this is an add or remove operation
295  *
296  *  This function is used to add or remove multicast MAC addresses for
297  *  the VF.
298  **/
299 static s32 fm10k_update_mc_addr_vf(struct fm10k_hw *hw, u16 glort,
300 				   const u8 *mac, u16 vid, bool add)
301 {
302 	struct fm10k_mbx_info *mbx = &hw->mbx;
303 	u32 msg[7];
304 
305 	/* verify VLAN ID is valid */
306 	if (vid >= FM10K_VLAN_TABLE_VID_MAX)
307 		return FM10K_ERR_PARAM;
308 
309 	/* verify multicast address is valid */
310 	if (!is_multicast_ether_addr(mac))
311 		return FM10K_ERR_PARAM;
312 
313 	/* add bit to notify us if this is a set or clear operation */
314 	if (!add)
315 		vid |= FM10K_VLAN_CLEAR;
316 
317 	/* generate VLAN request */
318 	fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_MAC_VLAN);
319 	fm10k_tlv_attr_put_mac_vlan(msg, FM10K_MAC_VLAN_MSG_MULTICAST,
320 				    mac, vid);
321 
322 	/* load onto outgoing mailbox */
323 	return mbx->ops.enqueue_tx(hw, mbx, msg);
324 }
325 
326 /**
327  *  fm10k_update_int_moderator_vf - Request update of interrupt moderator list
328  *  @hw: pointer to hardware structure
329  *
330  *  This function will issue a request to the PF to rescan our MSI-X table
331  *  and to update the interrupt moderator linked list.
332  **/
333 static void fm10k_update_int_moderator_vf(struct fm10k_hw *hw)
334 {
335 	struct fm10k_mbx_info *mbx = &hw->mbx;
336 	u32 msg[1];
337 
338 	/* generate MSI-X request */
339 	fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_MSIX);
340 
341 	/* load onto outgoing mailbox */
342 	mbx->ops.enqueue_tx(hw, mbx, msg);
343 }
344 
345 /* This structure defines the attibutes to be parsed below */
346 const struct fm10k_tlv_attr fm10k_lport_state_msg_attr[] = {
347 	FM10K_TLV_ATTR_BOOL(FM10K_LPORT_STATE_MSG_DISABLE),
348 	FM10K_TLV_ATTR_U8(FM10K_LPORT_STATE_MSG_XCAST_MODE),
349 	FM10K_TLV_ATTR_BOOL(FM10K_LPORT_STATE_MSG_READY),
350 	FM10K_TLV_ATTR_LAST
351 };
352 
353 /**
354  *  fm10k_msg_lport_state_vf - Message handler for lport_state message from PF
355  *  @hw: Pointer to hardware structure
356  *  @results: pointer array containing parsed data
357  *  @mbx: Pointer to mailbox information structure
358  *
359  *  This handler is meant to capture the indication from the PF that we
360  *  are ready to bring up the interface.
361  **/
362 s32 fm10k_msg_lport_state_vf(struct fm10k_hw *hw, u32 **results,
363 			     struct fm10k_mbx_info *mbx)
364 {
365 	hw->mac.dglort_map = !results[FM10K_LPORT_STATE_MSG_READY] ?
366 			     FM10K_DGLORTMAP_NONE : FM10K_DGLORTMAP_ZERO;
367 
368 	return 0;
369 }
370 
371 /**
372  *  fm10k_update_lport_state_vf - Update device state in lower device
373  *  @hw: pointer to the HW structure
374  *  @glort: unused
375  *  @count: number of logical ports to enable - unused (always 1)
376  *  @enable: boolean value indicating if this is an enable or disable request
377  *
378  *  Notify the lower device of a state change.  If the lower device is
379  *  enabled we can add filters, if it is disabled all filters for this
380  *  logical port are flushed.
381  **/
382 static s32 fm10k_update_lport_state_vf(struct fm10k_hw *hw, u16 glort,
383 				       u16 count, bool enable)
384 {
385 	struct fm10k_mbx_info *mbx = &hw->mbx;
386 	u32 msg[2];
387 
388 	/* reset glort mask 0 as we have to wait to be enabled */
389 	hw->mac.dglort_map = FM10K_DGLORTMAP_NONE;
390 
391 	/* generate port state request */
392 	fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_LPORT_STATE);
393 	if (!enable)
394 		fm10k_tlv_attr_put_bool(msg, FM10K_LPORT_STATE_MSG_DISABLE);
395 
396 	/* load onto outgoing mailbox */
397 	return mbx->ops.enqueue_tx(hw, mbx, msg);
398 }
399 
400 /**
401  *  fm10k_update_xcast_mode_vf - Request update of multicast mode
402  *  @hw: pointer to hardware structure
403  *  @glort: unused
404  *  @mode: integer value indicating mode being requested
405  *
406  *  This function will attempt to request a higher mode for the port
407  *  so that it can enable either multicast, multicast promiscuous, or
408  *  promiscuous mode of operation.
409  **/
410 static s32 fm10k_update_xcast_mode_vf(struct fm10k_hw *hw, u16 glort, u8 mode)
411 {
412 	struct fm10k_mbx_info *mbx = &hw->mbx;
413 	u32 msg[3];
414 
415 	if (mode > FM10K_XCAST_MODE_NONE)
416 		return FM10K_ERR_PARAM;
417 	/* generate message requesting to change xcast mode */
418 	fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_LPORT_STATE);
419 	fm10k_tlv_attr_put_u8(msg, FM10K_LPORT_STATE_MSG_XCAST_MODE, mode);
420 
421 	/* load onto outgoing mailbox */
422 	return mbx->ops.enqueue_tx(hw, mbx, msg);
423 }
424 
425 const struct fm10k_tlv_attr fm10k_1588_msg_attr[] = {
426 	FM10K_TLV_ATTR_U64(FM10K_1588_MSG_TIMESTAMP),
427 	FM10K_TLV_ATTR_LAST
428 };
429 
430 /* currently there is no shared 1588 timestamp handler */
431 
432 /**
433  *  fm10k_update_hw_stats_vf - Updates hardware related statistics of VF
434  *  @hw: pointer to hardware structure
435  *  @stats: pointer to statistics structure
436  *
437  *  This function collects and aggregates per queue hardware statistics.
438  **/
439 static void fm10k_update_hw_stats_vf(struct fm10k_hw *hw,
440 				     struct fm10k_hw_stats *stats)
441 {
442 	fm10k_update_hw_stats_q(hw, stats->q, 0, hw->mac.max_queues);
443 }
444 
445 /**
446  *  fm10k_rebind_hw_stats_vf - Resets base for hardware statistics of VF
447  *  @hw: pointer to hardware structure
448  *  @stats: pointer to the stats structure to update
449  *
450  *  This function resets the base for queue hardware statistics.
451  **/
452 static void fm10k_rebind_hw_stats_vf(struct fm10k_hw *hw,
453 				     struct fm10k_hw_stats *stats)
454 {
455 	/* Unbind Queue Statistics */
456 	fm10k_unbind_hw_stats_q(stats->q, 0, hw->mac.max_queues);
457 
458 	/* Reinitialize bases for all stats */
459 	fm10k_update_hw_stats_vf(hw, stats);
460 }
461 
462 /**
463  *  fm10k_configure_dglort_map_vf - Configures GLORT entry and queues
464  *  @hw: pointer to hardware structure
465  *  @dglort: pointer to dglort configuration structure
466  *
467  *  Reads the configuration structure contained in dglort_cfg and uses
468  *  that information to then populate a DGLORTMAP/DEC entry and the queues
469  *  to which it has been assigned.
470  **/
471 static s32 fm10k_configure_dglort_map_vf(struct fm10k_hw *hw,
472 					 struct fm10k_dglort_cfg *dglort)
473 {
474 	/* verify the dglort pointer */
475 	if (!dglort)
476 		return FM10K_ERR_PARAM;
477 
478 	/* stub for now until we determine correct message for this */
479 
480 	return 0;
481 }
482 
483 /**
484  *  fm10k_adjust_systime_vf - Adjust systime frequency
485  *  @hw: pointer to hardware structure
486  *  @ppb: adjustment rate in parts per billion
487  *
488  *  This function takes an adjustment rate in parts per billion and will
489  *  verify that this value is 0 as the VF cannot support adjusting the
490  *  systime clock.
491  *
492  *  If the ppb value is non-zero the return is ERR_PARAM else success
493  **/
494 static s32 fm10k_adjust_systime_vf(struct fm10k_hw *hw, s32 ppb)
495 {
496 	/* The VF cannot adjust the clock frequency, however it should
497 	 * already have a syntonic clock with whichever host interface is
498 	 * running as the master for the host interface clock domain so
499 	 * there should be not frequency adjustment necessary.
500 	 */
501 	return ppb ? FM10K_ERR_PARAM : 0;
502 }
503 
504 /**
505  *  fm10k_read_systime_vf - Reads value of systime registers
506  *  @hw: pointer to the hardware structure
507  *
508  *  Function reads the content of 2 registers, combined to represent a 64 bit
509  *  value measured in nanoseconds.  In order to guarantee the value is accurate
510  *  we check the 32 most significant bits both before and after reading the
511  *  32 least significant bits to verify they didn't change as we were reading
512  *  the registers.
513  **/
514 static u64 fm10k_read_systime_vf(struct fm10k_hw *hw)
515 {
516 	u32 systime_l, systime_h, systime_tmp;
517 
518 	systime_h = fm10k_read_reg(hw, FM10K_VFSYSTIME + 1);
519 
520 	do {
521 		systime_tmp = systime_h;
522 		systime_l = fm10k_read_reg(hw, FM10K_VFSYSTIME);
523 		systime_h = fm10k_read_reg(hw, FM10K_VFSYSTIME + 1);
524 	} while (systime_tmp != systime_h);
525 
526 	return ((u64)systime_h << 32) | systime_l;
527 }
528 
529 static const struct fm10k_msg_data fm10k_msg_data_vf[] = {
530 	FM10K_TLV_MSG_TEST_HANDLER(fm10k_tlv_msg_test),
531 	FM10K_VF_MSG_MAC_VLAN_HANDLER(fm10k_msg_mac_vlan_vf),
532 	FM10K_VF_MSG_LPORT_STATE_HANDLER(fm10k_msg_lport_state_vf),
533 	FM10K_TLV_MSG_ERROR_HANDLER(fm10k_tlv_msg_error),
534 };
535 
536 static struct fm10k_mac_ops mac_ops_vf = {
537 	.get_bus_info		= &fm10k_get_bus_info_generic,
538 	.reset_hw		= &fm10k_reset_hw_vf,
539 	.init_hw		= &fm10k_init_hw_vf,
540 	.start_hw		= &fm10k_start_hw_generic,
541 	.stop_hw		= &fm10k_stop_hw_vf,
542 	.update_vlan		= &fm10k_update_vlan_vf,
543 	.read_mac_addr		= &fm10k_read_mac_addr_vf,
544 	.update_uc_addr		= &fm10k_update_uc_addr_vf,
545 	.update_mc_addr		= &fm10k_update_mc_addr_vf,
546 	.update_xcast_mode	= &fm10k_update_xcast_mode_vf,
547 	.update_int_moderator	= &fm10k_update_int_moderator_vf,
548 	.update_lport_state	= &fm10k_update_lport_state_vf,
549 	.update_hw_stats	= &fm10k_update_hw_stats_vf,
550 	.rebind_hw_stats	= &fm10k_rebind_hw_stats_vf,
551 	.configure_dglort_map	= &fm10k_configure_dglort_map_vf,
552 	.get_host_state		= &fm10k_get_host_state_generic,
553 	.adjust_systime		= &fm10k_adjust_systime_vf,
554 	.read_systime		= &fm10k_read_systime_vf,
555 };
556 
557 static s32 fm10k_get_invariants_vf(struct fm10k_hw *hw)
558 {
559 	fm10k_get_invariants_generic(hw);
560 
561 	return fm10k_pfvf_mbx_init(hw, &hw->mbx, fm10k_msg_data_vf, 0);
562 }
563 
564 struct fm10k_info fm10k_vf_info = {
565 	.mac		= fm10k_mac_vf,
566 	.get_invariants	= &fm10k_get_invariants_vf,
567 	.mac_ops	= &mac_ops_vf,
568 };
569