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