1 /* Intel Ethernet Switch Host Interface Driver
2  * Copyright(c) 2013 - 2014 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_pf.h"
22 #include "fm10k_vf.h"
23 
24 /**
25  *  fm10k_reset_hw_pf - PF hardware reset
26  *  @hw: pointer to hardware structure
27  *
28  *  This function should return the hardware to a state similar to the
29  *  one it is in after being powered on.
30  **/
31 static s32 fm10k_reset_hw_pf(struct fm10k_hw *hw)
32 {
33 	s32 err;
34 	u32 reg;
35 	u16 i;
36 
37 	/* Disable interrupts */
38 	fm10k_write_reg(hw, FM10K_EIMR, FM10K_EIMR_DISABLE(ALL));
39 
40 	/* Lock ITR2 reg 0 into itself and disable interrupt moderation */
41 	fm10k_write_reg(hw, FM10K_ITR2(0), 0);
42 	fm10k_write_reg(hw, FM10K_INT_CTRL, 0);
43 
44 	/* We assume here Tx and Rx queue 0 are owned by the PF */
45 
46 	/* Shut off VF access to their queues forcing them to queue 0 */
47 	for (i = 0; i < FM10K_TQMAP_TABLE_SIZE; i++) {
48 		fm10k_write_reg(hw, FM10K_TQMAP(i), 0);
49 		fm10k_write_reg(hw, FM10K_RQMAP(i), 0);
50 	}
51 
52 	/* shut down all rings */
53 	err = fm10k_disable_queues_generic(hw, FM10K_MAX_QUEUES);
54 	if (err)
55 		return err;
56 
57 	/* Verify that DMA is no longer active */
58 	reg = fm10k_read_reg(hw, FM10K_DMA_CTRL);
59 	if (reg & (FM10K_DMA_CTRL_TX_ACTIVE | FM10K_DMA_CTRL_RX_ACTIVE))
60 		return FM10K_ERR_DMA_PENDING;
61 
62 	/* verify the switch is ready for reset */
63 	reg = fm10k_read_reg(hw, FM10K_DMA_CTRL2);
64 	if (!(reg & FM10K_DMA_CTRL2_SWITCH_READY))
65 		goto out;
66 
67 	/* Inititate data path reset */
68 	reg |= FM10K_DMA_CTRL_DATAPATH_RESET;
69 	fm10k_write_reg(hw, FM10K_DMA_CTRL, reg);
70 
71 	/* Flush write and allow 100us for reset to complete */
72 	fm10k_write_flush(hw);
73 	udelay(FM10K_RESET_TIMEOUT);
74 
75 	/* Verify we made it out of reset */
76 	reg = fm10k_read_reg(hw, FM10K_IP);
77 	if (!(reg & FM10K_IP_NOTINRESET))
78 		err = FM10K_ERR_RESET_FAILED;
79 
80 out:
81 	return err;
82 }
83 
84 /**
85  *  fm10k_is_ari_hierarchy_pf - Indicate ARI hierarchy support
86  *  @hw: pointer to hardware structure
87  *
88  *  Looks at the ARI hierarchy bit to determine whether ARI is supported or not.
89  **/
90 static bool fm10k_is_ari_hierarchy_pf(struct fm10k_hw *hw)
91 {
92 	u16 sriov_ctrl = fm10k_read_pci_cfg_word(hw, FM10K_PCIE_SRIOV_CTRL);
93 
94 	return !!(sriov_ctrl & FM10K_PCIE_SRIOV_CTRL_VFARI);
95 }
96 
97 /**
98  *  fm10k_init_hw_pf - PF hardware initialization
99  *  @hw: pointer to hardware structure
100  *
101  **/
102 static s32 fm10k_init_hw_pf(struct fm10k_hw *hw)
103 {
104 	u32 dma_ctrl, txqctl;
105 	u16 i;
106 
107 	/* Establish default VSI as valid */
108 	fm10k_write_reg(hw, FM10K_DGLORTDEC(fm10k_dglort_default), 0);
109 	fm10k_write_reg(hw, FM10K_DGLORTMAP(fm10k_dglort_default),
110 			FM10K_DGLORTMAP_ANY);
111 
112 	/* Invalidate all other GLORT entries */
113 	for (i = 1; i < FM10K_DGLORT_COUNT; i++)
114 		fm10k_write_reg(hw, FM10K_DGLORTMAP(i), FM10K_DGLORTMAP_NONE);
115 
116 	/* reset ITR2(0) to point to itself */
117 	fm10k_write_reg(hw, FM10K_ITR2(0), 0);
118 
119 	/* reset VF ITR2(0) to point to 0 avoid PF registers */
120 	fm10k_write_reg(hw, FM10K_ITR2(FM10K_ITR_REG_COUNT_PF), 0);
121 
122 	/* loop through all PF ITR2 registers pointing them to the previous */
123 	for (i = 1; i < FM10K_ITR_REG_COUNT_PF; i++)
124 		fm10k_write_reg(hw, FM10K_ITR2(i), i - 1);
125 
126 	/* Enable interrupt moderator if not already enabled */
127 	fm10k_write_reg(hw, FM10K_INT_CTRL, FM10K_INT_CTRL_ENABLEMODERATOR);
128 
129 	/* compute the default txqctl configuration */
130 	txqctl = FM10K_TXQCTL_PF | FM10K_TXQCTL_UNLIMITED_BW |
131 		 (hw->mac.default_vid << FM10K_TXQCTL_VID_SHIFT);
132 
133 	for (i = 0; i < FM10K_MAX_QUEUES; i++) {
134 		/* configure rings for 256 Queue / 32 Descriptor cache mode */
135 		fm10k_write_reg(hw, FM10K_TQDLOC(i),
136 				(i * FM10K_TQDLOC_BASE_32_DESC) |
137 				FM10K_TQDLOC_SIZE_32_DESC);
138 		fm10k_write_reg(hw, FM10K_TXQCTL(i), txqctl);
139 
140 		/* configure rings to provide TPH processing hints */
141 		fm10k_write_reg(hw, FM10K_TPH_TXCTRL(i),
142 				FM10K_TPH_TXCTRL_DESC_TPHEN |
143 				FM10K_TPH_TXCTRL_DESC_RROEN |
144 				FM10K_TPH_TXCTRL_DESC_WROEN |
145 				FM10K_TPH_TXCTRL_DATA_RROEN);
146 		fm10k_write_reg(hw, FM10K_TPH_RXCTRL(i),
147 				FM10K_TPH_RXCTRL_DESC_TPHEN |
148 				FM10K_TPH_RXCTRL_DESC_RROEN |
149 				FM10K_TPH_RXCTRL_DATA_WROEN |
150 				FM10K_TPH_RXCTRL_HDR_WROEN);
151 	}
152 
153 	/* set max hold interval to align with 1.024 usec in all modes */
154 	switch (hw->bus.speed) {
155 	case fm10k_bus_speed_2500:
156 		dma_ctrl = FM10K_DMA_CTRL_MAX_HOLD_1US_GEN1;
157 		break;
158 	case fm10k_bus_speed_5000:
159 		dma_ctrl = FM10K_DMA_CTRL_MAX_HOLD_1US_GEN2;
160 		break;
161 	case fm10k_bus_speed_8000:
162 		dma_ctrl = FM10K_DMA_CTRL_MAX_HOLD_1US_GEN3;
163 		break;
164 	default:
165 		dma_ctrl = 0;
166 		break;
167 	}
168 
169 	/* Configure TSO flags */
170 	fm10k_write_reg(hw, FM10K_DTXTCPFLGL, FM10K_TSO_FLAGS_LOW);
171 	fm10k_write_reg(hw, FM10K_DTXTCPFLGH, FM10K_TSO_FLAGS_HI);
172 
173 	/* Enable DMA engine
174 	 * Set Rx Descriptor size to 32
175 	 * Set Minimum MSS to 64
176 	 * Set Maximum number of Rx queues to 256 / 32 Descriptor
177 	 */
178 	dma_ctrl |= FM10K_DMA_CTRL_TX_ENABLE | FM10K_DMA_CTRL_RX_ENABLE |
179 		    FM10K_DMA_CTRL_RX_DESC_SIZE | FM10K_DMA_CTRL_MINMSS_64 |
180 		    FM10K_DMA_CTRL_32_DESC;
181 
182 	fm10k_write_reg(hw, FM10K_DMA_CTRL, dma_ctrl);
183 
184 	/* record maximum queue count, we limit ourselves to 128 */
185 	hw->mac.max_queues = FM10K_MAX_QUEUES_PF;
186 
187 	/* We support either 64 VFs or 7 VFs depending on if we have ARI */
188 	hw->iov.total_vfs = fm10k_is_ari_hierarchy_pf(hw) ? 64 : 7;
189 
190 	return 0;
191 }
192 
193 /**
194  *  fm10k_update_vlan_pf - Update status of VLAN ID in VLAN filter table
195  *  @hw: pointer to hardware structure
196  *  @vid: VLAN ID to add to table
197  *  @vsi: Index indicating VF ID or PF ID in table
198  *  @set: Indicates if this is a set or clear operation
199  *
200  *  This function adds or removes the corresponding VLAN ID from the VLAN
201  *  filter table for the corresponding function.  In addition to the
202  *  standard set/clear that supports one bit a multi-bit write is
203  *  supported to set 64 bits at a time.
204  **/
205 static s32 fm10k_update_vlan_pf(struct fm10k_hw *hw, u32 vid, u8 vsi, bool set)
206 {
207 	u32 vlan_table, reg, mask, bit, len;
208 
209 	/* verify the VSI index is valid */
210 	if (vsi > FM10K_VLAN_TABLE_VSI_MAX)
211 		return FM10K_ERR_PARAM;
212 
213 	/* VLAN multi-bit write:
214 	 * The multi-bit write has several parts to it.
215 	 *    3			  2		      1			  0
216 	 *  1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
217 	 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
218 	 * | RSVD0 |         Length        |C|RSVD0|        VLAN ID        |
219 	 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
220 	 *
221 	 * VLAN ID: Vlan Starting value
222 	 * RSVD0: Reserved section, must be 0
223 	 * C: Flag field, 0 is set, 1 is clear (Used in VF VLAN message)
224 	 * Length: Number of times to repeat the bit being set
225 	 */
226 	len = vid >> 16;
227 	vid = (vid << 17) >> 17;
228 
229 	/* verify the reserved 0 fields are 0 */
230 	if (len >= FM10K_VLAN_TABLE_VID_MAX || vid >= FM10K_VLAN_TABLE_VID_MAX)
231 		return FM10K_ERR_PARAM;
232 
233 	/* Loop through the table updating all required VLANs */
234 	for (reg = FM10K_VLAN_TABLE(vsi, vid / 32), bit = vid % 32;
235 	     len < FM10K_VLAN_TABLE_VID_MAX;
236 	     len -= 32 - bit, reg++, bit = 0) {
237 		/* record the initial state of the register */
238 		vlan_table = fm10k_read_reg(hw, reg);
239 
240 		/* truncate mask if we are at the start or end of the run */
241 		mask = (~(u32)0 >> ((len < 31) ? 31 - len : 0)) << bit;
242 
243 		/* make necessary modifications to the register */
244 		mask &= set ? ~vlan_table : vlan_table;
245 		if (mask)
246 			fm10k_write_reg(hw, reg, vlan_table ^ mask);
247 	}
248 
249 	return 0;
250 }
251 
252 /**
253  *  fm10k_read_mac_addr_pf - Read device MAC address
254  *  @hw: pointer to the HW structure
255  *
256  *  Reads the device MAC address from the SM_AREA and stores the value.
257  **/
258 static s32 fm10k_read_mac_addr_pf(struct fm10k_hw *hw)
259 {
260 	u8 perm_addr[ETH_ALEN];
261 	u32 serial_num;
262 	int i;
263 
264 	serial_num = fm10k_read_reg(hw, FM10K_SM_AREA(1));
265 
266 	/* last byte should be all 1's */
267 	if ((~serial_num) << 24)
268 		return  FM10K_ERR_INVALID_MAC_ADDR;
269 
270 	perm_addr[0] = (u8)(serial_num >> 24);
271 	perm_addr[1] = (u8)(serial_num >> 16);
272 	perm_addr[2] = (u8)(serial_num >> 8);
273 
274 	serial_num = fm10k_read_reg(hw, FM10K_SM_AREA(0));
275 
276 	/* first byte should be all 1's */
277 	if ((~serial_num) >> 24)
278 		return  FM10K_ERR_INVALID_MAC_ADDR;
279 
280 	perm_addr[3] = (u8)(serial_num >> 16);
281 	perm_addr[4] = (u8)(serial_num >> 8);
282 	perm_addr[5] = (u8)(serial_num);
283 
284 	for (i = 0; i < ETH_ALEN; i++) {
285 		hw->mac.perm_addr[i] = perm_addr[i];
286 		hw->mac.addr[i] = perm_addr[i];
287 	}
288 
289 	return 0;
290 }
291 
292 /**
293  *  fm10k_glort_valid_pf - Validate that the provided glort is valid
294  *  @hw: pointer to the HW structure
295  *  @glort: base glort to be validated
296  *
297  *  This function will return an error if the provided glort is invalid
298  **/
299 bool fm10k_glort_valid_pf(struct fm10k_hw *hw, u16 glort)
300 {
301 	glort &= hw->mac.dglort_map >> FM10K_DGLORTMAP_MASK_SHIFT;
302 
303 	return glort == (hw->mac.dglort_map & FM10K_DGLORTMAP_NONE);
304 }
305 
306 /**
307  *  fm10k_update_xc_addr_pf - Update device addresses
308  *  @hw: pointer to the HW structure
309  *  @glort: base resource tag for this request
310  *  @mac: MAC address to add/remove from table
311  *  @vid: VLAN ID to add/remove from table
312  *  @add: Indicates if this is an add or remove operation
313  *  @flags: flags field to indicate add and secure
314  *
315  *  This function generates a message to the Switch API requesting
316  *  that the given logical port add/remove the given L2 MAC/VLAN address.
317  **/
318 static s32 fm10k_update_xc_addr_pf(struct fm10k_hw *hw, u16 glort,
319 				   const u8 *mac, u16 vid, bool add, u8 flags)
320 {
321 	struct fm10k_mbx_info *mbx = &hw->mbx;
322 	struct fm10k_mac_update mac_update;
323 	u32 msg[5];
324 
325 	/* clear set bit from VLAN ID */
326 	vid &= ~FM10K_VLAN_CLEAR;
327 
328 	/* if glort or vlan are not valid return error */
329 	if (!fm10k_glort_valid_pf(hw, glort) || vid >= FM10K_VLAN_TABLE_VID_MAX)
330 		return FM10K_ERR_PARAM;
331 
332 	/* record fields */
333 	mac_update.mac_lower = cpu_to_le32(((u32)mac[2] << 24) |
334 						 ((u32)mac[3] << 16) |
335 						 ((u32)mac[4] << 8) |
336 						 ((u32)mac[5]));
337 	mac_update.mac_upper = cpu_to_le16(((u32)mac[0] << 8) |
338 						 ((u32)mac[1]));
339 	mac_update.vlan = cpu_to_le16(vid);
340 	mac_update.glort = cpu_to_le16(glort);
341 	mac_update.action = add ? 0 : 1;
342 	mac_update.flags = flags;
343 
344 	/* populate mac_update fields */
345 	fm10k_tlv_msg_init(msg, FM10K_PF_MSG_ID_UPDATE_MAC_FWD_RULE);
346 	fm10k_tlv_attr_put_le_struct(msg, FM10K_PF_ATTR_ID_MAC_UPDATE,
347 				     &mac_update, sizeof(mac_update));
348 
349 	/* load onto outgoing mailbox */
350 	return mbx->ops.enqueue_tx(hw, mbx, msg);
351 }
352 
353 /**
354  *  fm10k_update_uc_addr_pf - Update device unicast addresses
355  *  @hw: pointer to the HW structure
356  *  @glort: base resource tag for this request
357  *  @mac: MAC address to add/remove from table
358  *  @vid: VLAN ID to add/remove from table
359  *  @add: Indicates if this is an add or remove operation
360  *  @flags: flags field to indicate add and secure
361  *
362  *  This function is used to add or remove unicast addresses for
363  *  the PF.
364  **/
365 static s32 fm10k_update_uc_addr_pf(struct fm10k_hw *hw, u16 glort,
366 				   const u8 *mac, u16 vid, bool add, u8 flags)
367 {
368 	/* verify MAC address is valid */
369 	if (!is_valid_ether_addr(mac))
370 		return FM10K_ERR_PARAM;
371 
372 	return fm10k_update_xc_addr_pf(hw, glort, mac, vid, add, flags);
373 }
374 
375 /**
376  *  fm10k_update_mc_addr_pf - Update device multicast addresses
377  *  @hw: pointer to the HW structure
378  *  @glort: base resource tag for this request
379  *  @mac: MAC address to add/remove from table
380  *  @vid: VLAN ID to add/remove from table
381  *  @add: Indicates if this is an add or remove operation
382  *
383  *  This function is used to add or remove multicast MAC addresses for
384  *  the PF.
385  **/
386 static s32 fm10k_update_mc_addr_pf(struct fm10k_hw *hw, u16 glort,
387 				   const u8 *mac, u16 vid, bool add)
388 {
389 	/* verify multicast address is valid */
390 	if (!is_multicast_ether_addr(mac))
391 		return FM10K_ERR_PARAM;
392 
393 	return fm10k_update_xc_addr_pf(hw, glort, mac, vid, add, 0);
394 }
395 
396 /**
397  *  fm10k_update_xcast_mode_pf - Request update of multicast mode
398  *  @hw: pointer to hardware structure
399  *  @glort: base resource tag for this request
400  *  @mode: integer value indicating mode being requested
401  *
402  *  This function will attempt to request a higher mode for the port
403  *  so that it can enable either multicast, multicast promiscuous, or
404  *  promiscuous mode of operation.
405  **/
406 static s32 fm10k_update_xcast_mode_pf(struct fm10k_hw *hw, u16 glort, u8 mode)
407 {
408 	struct fm10k_mbx_info *mbx = &hw->mbx;
409 	u32 msg[3], xcast_mode;
410 
411 	if (mode > FM10K_XCAST_MODE_NONE)
412 		return FM10K_ERR_PARAM;
413 	/* if glort is not valid return error */
414 	if (!fm10k_glort_valid_pf(hw, glort))
415 		return FM10K_ERR_PARAM;
416 
417 	/* write xcast mode as a single u32 value,
418 	 * lower 16 bits: glort
419 	 * upper 16 bits: mode
420 	 */
421 	xcast_mode = ((u32)mode << 16) | glort;
422 
423 	/* generate message requesting to change xcast mode */
424 	fm10k_tlv_msg_init(msg, FM10K_PF_MSG_ID_XCAST_MODES);
425 	fm10k_tlv_attr_put_u32(msg, FM10K_PF_ATTR_ID_XCAST_MODE, xcast_mode);
426 
427 	/* load onto outgoing mailbox */
428 	return mbx->ops.enqueue_tx(hw, mbx, msg);
429 }
430 
431 /**
432  *  fm10k_update_int_moderator_pf - Update interrupt moderator linked list
433  *  @hw: pointer to hardware structure
434  *
435  *  This function walks through the MSI-X vector table to determine the
436  *  number of active interrupts and based on that information updates the
437  *  interrupt moderator linked list.
438  **/
439 static void fm10k_update_int_moderator_pf(struct fm10k_hw *hw)
440 {
441 	u32 i;
442 
443 	/* Disable interrupt moderator */
444 	fm10k_write_reg(hw, FM10K_INT_CTRL, 0);
445 
446 	/* loop through PF from last to first looking enabled vectors */
447 	for (i = FM10K_ITR_REG_COUNT_PF - 1; i; i--) {
448 		if (!fm10k_read_reg(hw, FM10K_MSIX_VECTOR_MASK(i)))
449 			break;
450 	}
451 
452 	/* always reset VFITR2[0] to point to last enabled PF vector */
453 	fm10k_write_reg(hw, FM10K_ITR2(FM10K_ITR_REG_COUNT_PF), i);
454 
455 	/* reset ITR2[0] to point to last enabled PF vector */
456 	if (!hw->iov.num_vfs)
457 		fm10k_write_reg(hw, FM10K_ITR2(0), i);
458 
459 	/* Enable interrupt moderator */
460 	fm10k_write_reg(hw, FM10K_INT_CTRL, FM10K_INT_CTRL_ENABLEMODERATOR);
461 }
462 
463 /**
464  *  fm10k_update_lport_state_pf - Notify the switch of a change in port state
465  *  @hw: pointer to the HW structure
466  *  @glort: base resource tag for this request
467  *  @count: number of logical ports being updated
468  *  @enable: boolean value indicating enable or disable
469  *
470  *  This function is used to add/remove a logical port from the switch.
471  **/
472 static s32 fm10k_update_lport_state_pf(struct fm10k_hw *hw, u16 glort,
473 				       u16 count, bool enable)
474 {
475 	struct fm10k_mbx_info *mbx = &hw->mbx;
476 	u32 msg[3], lport_msg;
477 
478 	/* do nothing if we are being asked to create or destroy 0 ports */
479 	if (!count)
480 		return 0;
481 
482 	/* if glort is not valid return error */
483 	if (!fm10k_glort_valid_pf(hw, glort))
484 		return FM10K_ERR_PARAM;
485 
486 	/* construct the lport message from the 2 pieces of data we have */
487 	lport_msg = ((u32)count << 16) | glort;
488 
489 	/* generate lport create/delete message */
490 	fm10k_tlv_msg_init(msg, enable ? FM10K_PF_MSG_ID_LPORT_CREATE :
491 					 FM10K_PF_MSG_ID_LPORT_DELETE);
492 	fm10k_tlv_attr_put_u32(msg, FM10K_PF_ATTR_ID_PORT, lport_msg);
493 
494 	/* load onto outgoing mailbox */
495 	return mbx->ops.enqueue_tx(hw, mbx, msg);
496 }
497 
498 /**
499  *  fm10k_configure_dglort_map_pf - Configures GLORT entry and queues
500  *  @hw: pointer to hardware structure
501  *  @dglort: pointer to dglort configuration structure
502  *
503  *  Reads the configuration structure contained in dglort_cfg and uses
504  *  that information to then populate a DGLORTMAP/DEC entry and the queues
505  *  to which it has been assigned.
506  **/
507 static s32 fm10k_configure_dglort_map_pf(struct fm10k_hw *hw,
508 					 struct fm10k_dglort_cfg *dglort)
509 {
510 	u16 glort, queue_count, vsi_count, pc_count;
511 	u16 vsi, queue, pc, q_idx;
512 	u32 txqctl, dglortdec, dglortmap;
513 
514 	/* verify the dglort pointer */
515 	if (!dglort)
516 		return FM10K_ERR_PARAM;
517 
518 	/* verify the dglort values */
519 	if ((dglort->idx > 7) || (dglort->rss_l > 7) || (dglort->pc_l > 3) ||
520 	    (dglort->vsi_l > 6) || (dglort->vsi_b > 64) ||
521 	    (dglort->queue_l > 8) || (dglort->queue_b >= 256))
522 		return FM10K_ERR_PARAM;
523 
524 	/* determine count of VSIs and queues */
525 	queue_count = 1 << (dglort->rss_l + dglort->pc_l);
526 	vsi_count = 1 << (dglort->vsi_l + dglort->queue_l);
527 	glort = dglort->glort;
528 	q_idx = dglort->queue_b;
529 
530 	/* configure SGLORT for queues */
531 	for (vsi = 0; vsi < vsi_count; vsi++, glort++) {
532 		for (queue = 0; queue < queue_count; queue++, q_idx++) {
533 			if (q_idx >= FM10K_MAX_QUEUES)
534 				break;
535 
536 			fm10k_write_reg(hw, FM10K_TX_SGLORT(q_idx), glort);
537 			fm10k_write_reg(hw, FM10K_RX_SGLORT(q_idx), glort);
538 		}
539 	}
540 
541 	/* determine count of PCs and queues */
542 	queue_count = 1 << (dglort->queue_l + dglort->rss_l + dglort->vsi_l);
543 	pc_count = 1 << dglort->pc_l;
544 
545 	/* configure PC for Tx queues */
546 	for (pc = 0; pc < pc_count; pc++) {
547 		q_idx = pc + dglort->queue_b;
548 		for (queue = 0; queue < queue_count; queue++) {
549 			if (q_idx >= FM10K_MAX_QUEUES)
550 				break;
551 
552 			txqctl = fm10k_read_reg(hw, FM10K_TXQCTL(q_idx));
553 			txqctl &= ~FM10K_TXQCTL_PC_MASK;
554 			txqctl |= pc << FM10K_TXQCTL_PC_SHIFT;
555 			fm10k_write_reg(hw, FM10K_TXQCTL(q_idx), txqctl);
556 
557 			q_idx += pc_count;
558 		}
559 	}
560 
561 	/* configure DGLORTDEC */
562 	dglortdec = ((u32)(dglort->rss_l) << FM10K_DGLORTDEC_RSSLENGTH_SHIFT) |
563 		    ((u32)(dglort->queue_b) << FM10K_DGLORTDEC_QBASE_SHIFT) |
564 		    ((u32)(dglort->pc_l) << FM10K_DGLORTDEC_PCLENGTH_SHIFT) |
565 		    ((u32)(dglort->vsi_b) << FM10K_DGLORTDEC_VSIBASE_SHIFT) |
566 		    ((u32)(dglort->vsi_l) << FM10K_DGLORTDEC_VSILENGTH_SHIFT) |
567 		    ((u32)(dglort->queue_l));
568 	if (dglort->inner_rss)
569 		dglortdec |=  FM10K_DGLORTDEC_INNERRSS_ENABLE;
570 
571 	/* configure DGLORTMAP */
572 	dglortmap = (dglort->idx == fm10k_dglort_default) ?
573 			FM10K_DGLORTMAP_ANY : FM10K_DGLORTMAP_ZERO;
574 	dglortmap <<= dglort->vsi_l + dglort->queue_l + dglort->shared_l;
575 	dglortmap |= dglort->glort;
576 
577 	/* write values to hardware */
578 	fm10k_write_reg(hw, FM10K_DGLORTDEC(dglort->idx), dglortdec);
579 	fm10k_write_reg(hw, FM10K_DGLORTMAP(dglort->idx), dglortmap);
580 
581 	return 0;
582 }
583 
584 u16 fm10k_queues_per_pool(struct fm10k_hw *hw)
585 {
586 	u16 num_pools = hw->iov.num_pools;
587 
588 	return (num_pools > 32) ? 2 : (num_pools > 16) ? 4 : (num_pools > 8) ?
589 	       8 : FM10K_MAX_QUEUES_POOL;
590 }
591 
592 u16 fm10k_vf_queue_index(struct fm10k_hw *hw, u16 vf_idx)
593 {
594 	u16 num_vfs = hw->iov.num_vfs;
595 	u16 vf_q_idx = FM10K_MAX_QUEUES;
596 
597 	vf_q_idx -= fm10k_queues_per_pool(hw) * (num_vfs - vf_idx);
598 
599 	return vf_q_idx;
600 }
601 
602 static u16 fm10k_vectors_per_pool(struct fm10k_hw *hw)
603 {
604 	u16 num_pools = hw->iov.num_pools;
605 
606 	return (num_pools > 32) ? 8 : (num_pools > 16) ? 16 :
607 	       FM10K_MAX_VECTORS_POOL;
608 }
609 
610 static u16 fm10k_vf_vector_index(struct fm10k_hw *hw, u16 vf_idx)
611 {
612 	u16 vf_v_idx = FM10K_MAX_VECTORS_PF;
613 
614 	vf_v_idx += fm10k_vectors_per_pool(hw) * vf_idx;
615 
616 	return vf_v_idx;
617 }
618 
619 /**
620  *  fm10k_iov_assign_resources_pf - Assign pool resources for virtualization
621  *  @hw: pointer to the HW structure
622  *  @num_vfs: number of VFs to be allocated
623  *  @num_pools: number of virtualization pools to be allocated
624  *
625  *  Allocates queues and traffic classes to virtualization entities to prepare
626  *  the PF for SR-IOV and VMDq
627  **/
628 static s32 fm10k_iov_assign_resources_pf(struct fm10k_hw *hw, u16 num_vfs,
629 					 u16 num_pools)
630 {
631 	u16 qmap_stride, qpp, vpp, vf_q_idx, vf_q_idx0, qmap_idx;
632 	u32 vid = hw->mac.default_vid << FM10K_TXQCTL_VID_SHIFT;
633 	int i, j;
634 
635 	/* hardware only supports up to 64 pools */
636 	if (num_pools > 64)
637 		return FM10K_ERR_PARAM;
638 
639 	/* the number of VFs cannot exceed the number of pools */
640 	if ((num_vfs > num_pools) || (num_vfs > hw->iov.total_vfs))
641 		return FM10K_ERR_PARAM;
642 
643 	/* record number of virtualization entities */
644 	hw->iov.num_vfs = num_vfs;
645 	hw->iov.num_pools = num_pools;
646 
647 	/* determine qmap offsets and counts */
648 	qmap_stride = (num_vfs > 8) ? 32 : 256;
649 	qpp = fm10k_queues_per_pool(hw);
650 	vpp = fm10k_vectors_per_pool(hw);
651 
652 	/* calculate starting index for queues */
653 	vf_q_idx = fm10k_vf_queue_index(hw, 0);
654 	qmap_idx = 0;
655 
656 	/* establish TCs with -1 credits and no quanta to prevent transmit */
657 	for (i = 0; i < num_vfs; i++) {
658 		fm10k_write_reg(hw, FM10K_TC_MAXCREDIT(i), 0);
659 		fm10k_write_reg(hw, FM10K_TC_RATE(i), 0);
660 		fm10k_write_reg(hw, FM10K_TC_CREDIT(i),
661 				FM10K_TC_CREDIT_CREDIT_MASK);
662 	}
663 
664 	/* zero out all mbmem registers */
665 	for (i = FM10K_VFMBMEM_LEN * num_vfs; i--;)
666 		fm10k_write_reg(hw, FM10K_MBMEM(i), 0);
667 
668 	/* clear event notification of VF FLR */
669 	fm10k_write_reg(hw, FM10K_PFVFLREC(0), ~0);
670 	fm10k_write_reg(hw, FM10K_PFVFLREC(1), ~0);
671 
672 	/* loop through unallocated rings assigning them back to PF */
673 	for (i = FM10K_MAX_QUEUES_PF; i < vf_q_idx; i++) {
674 		fm10k_write_reg(hw, FM10K_TXDCTL(i), 0);
675 		fm10k_write_reg(hw, FM10K_TXQCTL(i), FM10K_TXQCTL_PF |
676 				FM10K_TXQCTL_UNLIMITED_BW | vid);
677 		fm10k_write_reg(hw, FM10K_RXQCTL(i), FM10K_RXQCTL_PF);
678 	}
679 
680 	/* PF should have already updated VFITR2[0] */
681 
682 	/* update all ITR registers to flow to VFITR2[0] */
683 	for (i = FM10K_ITR_REG_COUNT_PF + 1; i < FM10K_ITR_REG_COUNT; i++) {
684 		if (!(i & (vpp - 1)))
685 			fm10k_write_reg(hw, FM10K_ITR2(i), i - vpp);
686 		else
687 			fm10k_write_reg(hw, FM10K_ITR2(i), i - 1);
688 	}
689 
690 	/* update PF ITR2[0] to reference the last vector */
691 	fm10k_write_reg(hw, FM10K_ITR2(0),
692 			fm10k_vf_vector_index(hw, num_vfs - 1));
693 
694 	/* loop through rings populating rings and TCs */
695 	for (i = 0; i < num_vfs; i++) {
696 		/* record index for VF queue 0 for use in end of loop */
697 		vf_q_idx0 = vf_q_idx;
698 
699 		for (j = 0; j < qpp; j++, qmap_idx++, vf_q_idx++) {
700 			/* assign VF and locked TC to queues */
701 			fm10k_write_reg(hw, FM10K_TXDCTL(vf_q_idx), 0);
702 			fm10k_write_reg(hw, FM10K_TXQCTL(vf_q_idx),
703 					(i << FM10K_TXQCTL_TC_SHIFT) | i |
704 					FM10K_TXQCTL_VF | vid);
705 			fm10k_write_reg(hw, FM10K_RXDCTL(vf_q_idx),
706 					FM10K_RXDCTL_WRITE_BACK_MIN_DELAY |
707 					FM10K_RXDCTL_DROP_ON_EMPTY);
708 			fm10k_write_reg(hw, FM10K_RXQCTL(vf_q_idx),
709 					FM10K_RXQCTL_VF |
710 					(i << FM10K_RXQCTL_VF_SHIFT));
711 
712 			/* map queue pair to VF */
713 			fm10k_write_reg(hw, FM10K_TQMAP(qmap_idx), vf_q_idx);
714 			fm10k_write_reg(hw, FM10K_RQMAP(qmap_idx), vf_q_idx);
715 		}
716 
717 		/* repeat the first ring for all of the remaining VF rings */
718 		for (; j < qmap_stride; j++, qmap_idx++) {
719 			fm10k_write_reg(hw, FM10K_TQMAP(qmap_idx), vf_q_idx0);
720 			fm10k_write_reg(hw, FM10K_RQMAP(qmap_idx), vf_q_idx0);
721 		}
722 	}
723 
724 	/* loop through remaining indexes assigning all to queue 0 */
725 	while (qmap_idx < FM10K_TQMAP_TABLE_SIZE) {
726 		fm10k_write_reg(hw, FM10K_TQMAP(qmap_idx), 0);
727 		fm10k_write_reg(hw, FM10K_RQMAP(qmap_idx), 0);
728 		qmap_idx++;
729 	}
730 
731 	return 0;
732 }
733 
734 /**
735  *  fm10k_iov_configure_tc_pf - Configure the shaping group for VF
736  *  @hw: pointer to the HW structure
737  *  @vf_idx: index of VF receiving GLORT
738  *  @rate: Rate indicated in Mb/s
739  *
740  *  Configured the TC for a given VF to allow only up to a given number
741  *  of Mb/s of outgoing Tx throughput.
742  **/
743 static s32 fm10k_iov_configure_tc_pf(struct fm10k_hw *hw, u16 vf_idx, int rate)
744 {
745 	/* configure defaults */
746 	u32 interval = FM10K_TC_RATE_INTERVAL_4US_GEN3;
747 	u32 tc_rate = FM10K_TC_RATE_QUANTA_MASK;
748 
749 	/* verify vf is in range */
750 	if (vf_idx >= hw->iov.num_vfs)
751 		return FM10K_ERR_PARAM;
752 
753 	/* set interval to align with 4.096 usec in all modes */
754 	switch (hw->bus.speed) {
755 	case fm10k_bus_speed_2500:
756 		interval = FM10K_TC_RATE_INTERVAL_4US_GEN1;
757 		break;
758 	case fm10k_bus_speed_5000:
759 		interval = FM10K_TC_RATE_INTERVAL_4US_GEN2;
760 		break;
761 	default:
762 		break;
763 	}
764 
765 	if (rate) {
766 		if (rate > FM10K_VF_TC_MAX || rate < FM10K_VF_TC_MIN)
767 			return FM10K_ERR_PARAM;
768 
769 		/* The quanta is measured in Bytes per 4.096 or 8.192 usec
770 		 * The rate is provided in Mbits per second
771 		 * To tralslate from rate to quanta we need to multiply the
772 		 * rate by 8.192 usec and divide by 8 bits/byte.  To avoid
773 		 * dealing with floating point we can round the values up
774 		 * to the nearest whole number ratio which gives us 128 / 125.
775 		 */
776 		tc_rate = (rate * 128) / 125;
777 
778 		/* try to keep the rate limiting accurate by increasing
779 		 * the number of credits and interval for rates less than 4Gb/s
780 		 */
781 		if (rate < 4000)
782 			interval <<= 1;
783 		else
784 			tc_rate >>= 1;
785 	}
786 
787 	/* update rate limiter with new values */
788 	fm10k_write_reg(hw, FM10K_TC_RATE(vf_idx), tc_rate | interval);
789 	fm10k_write_reg(hw, FM10K_TC_MAXCREDIT(vf_idx), FM10K_TC_MAXCREDIT_64K);
790 	fm10k_write_reg(hw, FM10K_TC_CREDIT(vf_idx), FM10K_TC_MAXCREDIT_64K);
791 
792 	return 0;
793 }
794 
795 /**
796  *  fm10k_iov_assign_int_moderator_pf - Add VF interrupts to moderator list
797  *  @hw: pointer to the HW structure
798  *  @vf_idx: index of VF receiving GLORT
799  *
800  *  Update the interrupt moderator linked list to include any MSI-X
801  *  interrupts which the VF has enabled in the MSI-X vector table.
802  **/
803 static s32 fm10k_iov_assign_int_moderator_pf(struct fm10k_hw *hw, u16 vf_idx)
804 {
805 	u16 vf_v_idx, vf_v_limit, i;
806 
807 	/* verify vf is in range */
808 	if (vf_idx >= hw->iov.num_vfs)
809 		return FM10K_ERR_PARAM;
810 
811 	/* determine vector offset and count */
812 	vf_v_idx = fm10k_vf_vector_index(hw, vf_idx);
813 	vf_v_limit = vf_v_idx + fm10k_vectors_per_pool(hw);
814 
815 	/* search for first vector that is not masked */
816 	for (i = vf_v_limit - 1; i > vf_v_idx; i--) {
817 		if (!fm10k_read_reg(hw, FM10K_MSIX_VECTOR_MASK(i)))
818 			break;
819 	}
820 
821 	/* reset linked list so it now includes our active vectors */
822 	if (vf_idx == (hw->iov.num_vfs - 1))
823 		fm10k_write_reg(hw, FM10K_ITR2(0), i);
824 	else
825 		fm10k_write_reg(hw, FM10K_ITR2(vf_v_limit), i);
826 
827 	return 0;
828 }
829 
830 /**
831  *  fm10k_iov_assign_default_mac_vlan_pf - Assign a MAC and VLAN to VF
832  *  @hw: pointer to the HW structure
833  *  @vf_info: pointer to VF information structure
834  *
835  *  Assign a MAC address and default VLAN to a VF and notify it of the update
836  **/
837 static s32 fm10k_iov_assign_default_mac_vlan_pf(struct fm10k_hw *hw,
838 						struct fm10k_vf_info *vf_info)
839 {
840 	u16 qmap_stride, queues_per_pool, vf_q_idx, timeout, qmap_idx, i;
841 	u32 msg[4], txdctl, txqctl, tdbal = 0, tdbah = 0;
842 	s32 err = 0;
843 	u16 vf_idx, vf_vid;
844 
845 	/* verify vf is in range */
846 	if (!vf_info || vf_info->vf_idx >= hw->iov.num_vfs)
847 		return FM10K_ERR_PARAM;
848 
849 	/* determine qmap offsets and counts */
850 	qmap_stride = (hw->iov.num_vfs > 8) ? 32 : 256;
851 	queues_per_pool = fm10k_queues_per_pool(hw);
852 
853 	/* calculate starting index for queues */
854 	vf_idx = vf_info->vf_idx;
855 	vf_q_idx = fm10k_vf_queue_index(hw, vf_idx);
856 	qmap_idx = qmap_stride * vf_idx;
857 
858 	/* MAP Tx queue back to 0 temporarily, and disable it */
859 	fm10k_write_reg(hw, FM10K_TQMAP(qmap_idx), 0);
860 	fm10k_write_reg(hw, FM10K_TXDCTL(vf_q_idx), 0);
861 
862 	/* determine correct default VLAN ID */
863 	if (vf_info->pf_vid)
864 		vf_vid = vf_info->pf_vid | FM10K_VLAN_CLEAR;
865 	else
866 		vf_vid = vf_info->sw_vid;
867 
868 	/* generate MAC_ADDR request */
869 	fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_MAC_VLAN);
870 	fm10k_tlv_attr_put_mac_vlan(msg, FM10K_MAC_VLAN_MSG_DEFAULT_MAC,
871 				    vf_info->mac, vf_vid);
872 
873 	/* load onto outgoing mailbox, ignore any errors on enqueue */
874 	if (vf_info->mbx.ops.enqueue_tx)
875 		vf_info->mbx.ops.enqueue_tx(hw, &vf_info->mbx, msg);
876 
877 	/* verify ring has disabled before modifying base address registers */
878 	txdctl = fm10k_read_reg(hw, FM10K_TXDCTL(vf_q_idx));
879 	for (timeout = 0; txdctl & FM10K_TXDCTL_ENABLE; timeout++) {
880 		/* limit ourselves to a 1ms timeout */
881 		if (timeout == 10) {
882 			err = FM10K_ERR_DMA_PENDING;
883 			goto err_out;
884 		}
885 
886 		usleep_range(100, 200);
887 		txdctl = fm10k_read_reg(hw, FM10K_TXDCTL(vf_q_idx));
888 	}
889 
890 	/* Update base address registers to contain MAC address */
891 	if (is_valid_ether_addr(vf_info->mac)) {
892 		tdbal = (((u32)vf_info->mac[3]) << 24) |
893 			(((u32)vf_info->mac[4]) << 16) |
894 			(((u32)vf_info->mac[5]) << 8);
895 
896 		tdbah = (((u32)0xFF)	        << 24) |
897 			(((u32)vf_info->mac[0]) << 16) |
898 			(((u32)vf_info->mac[1]) << 8) |
899 			((u32)vf_info->mac[2]);
900 	}
901 
902 	/* Record the base address into queue 0 */
903 	fm10k_write_reg(hw, FM10K_TDBAL(vf_q_idx), tdbal);
904 	fm10k_write_reg(hw, FM10K_TDBAH(vf_q_idx), tdbah);
905 
906 err_out:
907 	/* configure Queue control register */
908 	txqctl = ((u32)vf_vid << FM10K_TXQCTL_VID_SHIFT) &
909 		 FM10K_TXQCTL_VID_MASK;
910 	txqctl |= (vf_idx << FM10K_TXQCTL_TC_SHIFT) |
911 		  FM10K_TXQCTL_VF | vf_idx;
912 
913 	/* assign VID */
914 	for (i = 0; i < queues_per_pool; i++)
915 		fm10k_write_reg(hw, FM10K_TXQCTL(vf_q_idx + i), txqctl);
916 
917 	/* restore the queue back to VF ownership */
918 	fm10k_write_reg(hw, FM10K_TQMAP(qmap_idx), vf_q_idx);
919 	return err;
920 }
921 
922 /**
923  *  fm10k_iov_reset_resources_pf - Reassign queues and interrupts to a VF
924  *  @hw: pointer to the HW structure
925  *  @vf_info: pointer to VF information structure
926  *
927  *  Reassign the interrupts and queues to a VF following an FLR
928  **/
929 static s32 fm10k_iov_reset_resources_pf(struct fm10k_hw *hw,
930 					struct fm10k_vf_info *vf_info)
931 {
932 	u16 qmap_stride, queues_per_pool, vf_q_idx, qmap_idx;
933 	u32 tdbal = 0, tdbah = 0, txqctl, rxqctl;
934 	u16 vf_v_idx, vf_v_limit, vf_vid;
935 	u8 vf_idx = vf_info->vf_idx;
936 	int i;
937 
938 	/* verify vf is in range */
939 	if (vf_idx >= hw->iov.num_vfs)
940 		return FM10K_ERR_PARAM;
941 
942 	/* clear event notification of VF FLR */
943 	fm10k_write_reg(hw, FM10K_PFVFLREC(vf_idx / 32), 1 << (vf_idx % 32));
944 
945 	/* force timeout and then disconnect the mailbox */
946 	vf_info->mbx.timeout = 0;
947 	if (vf_info->mbx.ops.disconnect)
948 		vf_info->mbx.ops.disconnect(hw, &vf_info->mbx);
949 
950 	/* determine vector offset and count */
951 	vf_v_idx = fm10k_vf_vector_index(hw, vf_idx);
952 	vf_v_limit = vf_v_idx + fm10k_vectors_per_pool(hw);
953 
954 	/* determine qmap offsets and counts */
955 	qmap_stride = (hw->iov.num_vfs > 8) ? 32 : 256;
956 	queues_per_pool = fm10k_queues_per_pool(hw);
957 	qmap_idx = qmap_stride * vf_idx;
958 
959 	/* make all the queues inaccessible to the VF */
960 	for (i = qmap_idx; i < (qmap_idx + qmap_stride); i++) {
961 		fm10k_write_reg(hw, FM10K_TQMAP(i), 0);
962 		fm10k_write_reg(hw, FM10K_RQMAP(i), 0);
963 	}
964 
965 	/* calculate starting index for queues */
966 	vf_q_idx = fm10k_vf_queue_index(hw, vf_idx);
967 
968 	/* determine correct default VLAN ID */
969 	if (vf_info->pf_vid)
970 		vf_vid = vf_info->pf_vid;
971 	else
972 		vf_vid = vf_info->sw_vid;
973 
974 	/* configure Queue control register */
975 	txqctl = ((u32)vf_vid << FM10K_TXQCTL_VID_SHIFT) |
976 		 (vf_idx << FM10K_TXQCTL_TC_SHIFT) |
977 		 FM10K_TXQCTL_VF | vf_idx;
978 	rxqctl = FM10K_RXQCTL_VF | (vf_idx << FM10K_RXQCTL_VF_SHIFT);
979 
980 	/* stop further DMA and reset queue ownership back to VF */
981 	for (i = vf_q_idx; i < (queues_per_pool + vf_q_idx); i++) {
982 		fm10k_write_reg(hw, FM10K_TXDCTL(i), 0);
983 		fm10k_write_reg(hw, FM10K_TXQCTL(i), txqctl);
984 		fm10k_write_reg(hw, FM10K_RXDCTL(i),
985 				FM10K_RXDCTL_WRITE_BACK_MIN_DELAY |
986 				FM10K_RXDCTL_DROP_ON_EMPTY);
987 		fm10k_write_reg(hw, FM10K_RXQCTL(i), rxqctl);
988 	}
989 
990 	/* reset TC with -1 credits and no quanta to prevent transmit */
991 	fm10k_write_reg(hw, FM10K_TC_MAXCREDIT(vf_idx), 0);
992 	fm10k_write_reg(hw, FM10K_TC_RATE(vf_idx), 0);
993 	fm10k_write_reg(hw, FM10K_TC_CREDIT(vf_idx),
994 			FM10K_TC_CREDIT_CREDIT_MASK);
995 
996 	/* update our first entry in the table based on previous VF */
997 	if (!vf_idx)
998 		hw->mac.ops.update_int_moderator(hw);
999 	else
1000 		hw->iov.ops.assign_int_moderator(hw, vf_idx - 1);
1001 
1002 	/* reset linked list so it now includes our active vectors */
1003 	if (vf_idx == (hw->iov.num_vfs - 1))
1004 		fm10k_write_reg(hw, FM10K_ITR2(0), vf_v_idx);
1005 	else
1006 		fm10k_write_reg(hw, FM10K_ITR2(vf_v_limit), vf_v_idx);
1007 
1008 	/* link remaining vectors so that next points to previous */
1009 	for (vf_v_idx++; vf_v_idx < vf_v_limit; vf_v_idx++)
1010 		fm10k_write_reg(hw, FM10K_ITR2(vf_v_idx), vf_v_idx - 1);
1011 
1012 	/* zero out MBMEM, VLAN_TABLE, RETA, RSSRK, and MRQC registers */
1013 	for (i = FM10K_VFMBMEM_LEN; i--;)
1014 		fm10k_write_reg(hw, FM10K_MBMEM_VF(vf_idx, i), 0);
1015 	for (i = FM10K_VLAN_TABLE_SIZE; i--;)
1016 		fm10k_write_reg(hw, FM10K_VLAN_TABLE(vf_info->vsi, i), 0);
1017 	for (i = FM10K_RETA_SIZE; i--;)
1018 		fm10k_write_reg(hw, FM10K_RETA(vf_info->vsi, i), 0);
1019 	for (i = FM10K_RSSRK_SIZE; i--;)
1020 		fm10k_write_reg(hw, FM10K_RSSRK(vf_info->vsi, i), 0);
1021 	fm10k_write_reg(hw, FM10K_MRQC(vf_info->vsi), 0);
1022 
1023 	/* Update base address registers to contain MAC address */
1024 	if (is_valid_ether_addr(vf_info->mac)) {
1025 		tdbal = (((u32)vf_info->mac[3]) << 24) |
1026 			(((u32)vf_info->mac[4]) << 16) |
1027 			(((u32)vf_info->mac[5]) << 8);
1028 		tdbah = (((u32)0xFF)	   << 24) |
1029 			(((u32)vf_info->mac[0]) << 16) |
1030 			(((u32)vf_info->mac[1]) << 8) |
1031 			((u32)vf_info->mac[2]);
1032 	}
1033 
1034 	/* map queue pairs back to VF from last to first */
1035 	for (i = queues_per_pool; i--;) {
1036 		fm10k_write_reg(hw, FM10K_TDBAL(vf_q_idx + i), tdbal);
1037 		fm10k_write_reg(hw, FM10K_TDBAH(vf_q_idx + i), tdbah);
1038 		fm10k_write_reg(hw, FM10K_TQMAP(qmap_idx + i), vf_q_idx + i);
1039 		fm10k_write_reg(hw, FM10K_RQMAP(qmap_idx + i), vf_q_idx + i);
1040 	}
1041 
1042 	/* repeat the first ring for all the remaining VF rings */
1043 	for (i = queues_per_pool; i < qmap_stride; i++) {
1044 		fm10k_write_reg(hw, FM10K_TQMAP(qmap_idx + i), vf_q_idx);
1045 		fm10k_write_reg(hw, FM10K_RQMAP(qmap_idx + i), vf_q_idx);
1046 	}
1047 
1048 	return 0;
1049 }
1050 
1051 /**
1052  *  fm10k_iov_set_lport_pf - Assign and enable a logical port for a given VF
1053  *  @hw: pointer to hardware structure
1054  *  @vf_info: pointer to VF information structure
1055  *  @lport_idx: Logical port offset from the hardware glort
1056  *  @flags: Set of capability flags to extend port beyond basic functionality
1057  *
1058  *  This function allows enabling a VF port by assigning it a GLORT and
1059  *  setting the flags so that it can enable an Rx mode.
1060  **/
1061 static s32 fm10k_iov_set_lport_pf(struct fm10k_hw *hw,
1062 				  struct fm10k_vf_info *vf_info,
1063 				  u16 lport_idx, u8 flags)
1064 {
1065 	u16 glort = (hw->mac.dglort_map + lport_idx) & FM10K_DGLORTMAP_NONE;
1066 
1067 	/* if glort is not valid return error */
1068 	if (!fm10k_glort_valid_pf(hw, glort))
1069 		return FM10K_ERR_PARAM;
1070 
1071 	vf_info->vf_flags = flags | FM10K_VF_FLAG_NONE_CAPABLE;
1072 	vf_info->glort = glort;
1073 
1074 	return 0;
1075 }
1076 
1077 /**
1078  *  fm10k_iov_reset_lport_pf - Disable a logical port for a given VF
1079  *  @hw: pointer to hardware structure
1080  *  @vf_info: pointer to VF information structure
1081  *
1082  *  This function disables a VF port by stripping it of a GLORT and
1083  *  setting the flags so that it cannot enable any Rx mode.
1084  **/
1085 static void fm10k_iov_reset_lport_pf(struct fm10k_hw *hw,
1086 				     struct fm10k_vf_info *vf_info)
1087 {
1088 	u32 msg[1];
1089 
1090 	/* need to disable the port if it is already enabled */
1091 	if (FM10K_VF_FLAG_ENABLED(vf_info)) {
1092 		/* notify switch that this port has been disabled */
1093 		fm10k_update_lport_state_pf(hw, vf_info->glort, 1, false);
1094 
1095 		/* generate port state response to notify VF it is not ready */
1096 		fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_LPORT_STATE);
1097 		vf_info->mbx.ops.enqueue_tx(hw, &vf_info->mbx, msg);
1098 	}
1099 
1100 	/* clear flags and glort if it exists */
1101 	vf_info->vf_flags = 0;
1102 	vf_info->glort = 0;
1103 }
1104 
1105 /**
1106  *  fm10k_iov_update_stats_pf - Updates hardware related statistics for VFs
1107  *  @hw: pointer to hardware structure
1108  *  @q: stats for all queues of a VF
1109  *  @vf_idx: index of VF
1110  *
1111  *  This function collects queue stats for VFs.
1112  **/
1113 static void fm10k_iov_update_stats_pf(struct fm10k_hw *hw,
1114 				      struct fm10k_hw_stats_q *q,
1115 				      u16 vf_idx)
1116 {
1117 	u32 idx, qpp;
1118 
1119 	/* get stats for all of the queues */
1120 	qpp = fm10k_queues_per_pool(hw);
1121 	idx = fm10k_vf_queue_index(hw, vf_idx);
1122 	fm10k_update_hw_stats_q(hw, q, idx, qpp);
1123 }
1124 
1125 static s32 fm10k_iov_report_timestamp_pf(struct fm10k_hw *hw,
1126 					 struct fm10k_vf_info *vf_info,
1127 					 u64 timestamp)
1128 {
1129 	u32 msg[4];
1130 
1131 	/* generate port state response to notify VF it is not ready */
1132 	fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_1588);
1133 	fm10k_tlv_attr_put_u64(msg, FM10K_1588_MSG_TIMESTAMP, timestamp);
1134 
1135 	return vf_info->mbx.ops.enqueue_tx(hw, &vf_info->mbx, msg);
1136 }
1137 
1138 /**
1139  *  fm10k_iov_msg_msix_pf - Message handler for MSI-X request from VF
1140  *  @hw: Pointer to hardware structure
1141  *  @results: Pointer array to message, results[0] is pointer to message
1142  *  @mbx: Pointer to mailbox information structure
1143  *
1144  *  This function is a default handler for MSI-X requests from the VF.  The
1145  *  assumption is that in this case it is acceptable to just directly
1146  *  hand off the message from the VF to the underlying shared code.
1147  **/
1148 s32 fm10k_iov_msg_msix_pf(struct fm10k_hw *hw, u32 **results,
1149 			  struct fm10k_mbx_info *mbx)
1150 {
1151 	struct fm10k_vf_info *vf_info = (struct fm10k_vf_info *)mbx;
1152 	u8 vf_idx = vf_info->vf_idx;
1153 
1154 	return hw->iov.ops.assign_int_moderator(hw, vf_idx);
1155 }
1156 
1157 /**
1158  *  fm10k_iov_msg_mac_vlan_pf - Message handler for MAC/VLAN request from VF
1159  *  @hw: Pointer to hardware structure
1160  *  @results: Pointer array to message, results[0] is pointer to message
1161  *  @mbx: Pointer to mailbox information structure
1162  *
1163  *  This function is a default handler for MAC/VLAN requests from the VF.
1164  *  The assumption is that in this case it is acceptable to just directly
1165  *  hand off the message from the VF to the underlying shared code.
1166  **/
1167 s32 fm10k_iov_msg_mac_vlan_pf(struct fm10k_hw *hw, u32 **results,
1168 			      struct fm10k_mbx_info *mbx)
1169 {
1170 	struct fm10k_vf_info *vf_info = (struct fm10k_vf_info *)mbx;
1171 	int err = 0;
1172 	u8 mac[ETH_ALEN];
1173 	u32 *result;
1174 	u16 vlan;
1175 	u32 vid;
1176 
1177 	/* we shouldn't be updating rules on a disabled interface */
1178 	if (!FM10K_VF_FLAG_ENABLED(vf_info))
1179 		err = FM10K_ERR_PARAM;
1180 
1181 	if (!err && !!results[FM10K_MAC_VLAN_MSG_VLAN]) {
1182 		result = results[FM10K_MAC_VLAN_MSG_VLAN];
1183 
1184 		/* record VLAN id requested */
1185 		err = fm10k_tlv_attr_get_u32(result, &vid);
1186 		if (err)
1187 			return err;
1188 
1189 		/* if VLAN ID is 0, set the default VLAN ID instead of 0 */
1190 		if (!vid || (vid == FM10K_VLAN_CLEAR)) {
1191 			if (vf_info->pf_vid)
1192 				vid |= vf_info->pf_vid;
1193 			else
1194 				vid |= vf_info->sw_vid;
1195 		} else if (vid != vf_info->pf_vid) {
1196 			return FM10K_ERR_PARAM;
1197 		}
1198 
1199 		/* update VSI info for VF in regards to VLAN table */
1200 		err = hw->mac.ops.update_vlan(hw, vid, vf_info->vsi,
1201 					      !(vid & FM10K_VLAN_CLEAR));
1202 	}
1203 
1204 	if (!err && !!results[FM10K_MAC_VLAN_MSG_MAC]) {
1205 		result = results[FM10K_MAC_VLAN_MSG_MAC];
1206 
1207 		/* record unicast MAC address requested */
1208 		err = fm10k_tlv_attr_get_mac_vlan(result, mac, &vlan);
1209 		if (err)
1210 			return err;
1211 
1212 		/* block attempts to set MAC for a locked device */
1213 		if (is_valid_ether_addr(vf_info->mac) &&
1214 		    memcmp(mac, vf_info->mac, ETH_ALEN))
1215 			return FM10K_ERR_PARAM;
1216 
1217 		/* if VLAN ID is 0, set the default VLAN ID instead of 0 */
1218 		if (!vlan || (vlan == FM10K_VLAN_CLEAR)) {
1219 			if (vf_info->pf_vid)
1220 				vlan |= vf_info->pf_vid;
1221 			else
1222 				vlan |= vf_info->sw_vid;
1223 		} else if (vf_info->pf_vid) {
1224 			return FM10K_ERR_PARAM;
1225 		}
1226 
1227 		/* notify switch of request for new unicast address */
1228 		err = hw->mac.ops.update_uc_addr(hw, vf_info->glort, mac, vlan,
1229 						 !(vlan & FM10K_VLAN_CLEAR), 0);
1230 	}
1231 
1232 	if (!err && !!results[FM10K_MAC_VLAN_MSG_MULTICAST]) {
1233 		result = results[FM10K_MAC_VLAN_MSG_MULTICAST];
1234 
1235 		/* record multicast MAC address requested */
1236 		err = fm10k_tlv_attr_get_mac_vlan(result, mac, &vlan);
1237 		if (err)
1238 			return err;
1239 
1240 		/* verify that the VF is allowed to request multicast */
1241 		if (!(vf_info->vf_flags & FM10K_VF_FLAG_MULTI_ENABLED))
1242 			return FM10K_ERR_PARAM;
1243 
1244 		/* if VLAN ID is 0, set the default VLAN ID instead of 0 */
1245 		if (!vlan || (vlan == FM10K_VLAN_CLEAR)) {
1246 			if (vf_info->pf_vid)
1247 				vlan |= vf_info->pf_vid;
1248 			else
1249 				vlan |= vf_info->sw_vid;
1250 		} else if (vf_info->pf_vid) {
1251 			return FM10K_ERR_PARAM;
1252 		}
1253 
1254 		/* notify switch of request for new multicast address */
1255 		err = hw->mac.ops.update_mc_addr(hw, vf_info->glort, mac, vlan,
1256 						 !(vlan & FM10K_VLAN_CLEAR));
1257 	}
1258 
1259 	return err;
1260 }
1261 
1262 /**
1263  *  fm10k_iov_supported_xcast_mode_pf - Determine best match for xcast mode
1264  *  @vf_info: VF info structure containing capability flags
1265  *  @mode: Requested xcast mode
1266  *
1267  *  This function outputs the mode that most closely matches the requested
1268  *  mode.  If not modes match it will request we disable the port
1269  **/
1270 static u8 fm10k_iov_supported_xcast_mode_pf(struct fm10k_vf_info *vf_info,
1271 					    u8 mode)
1272 {
1273 	u8 vf_flags = vf_info->vf_flags;
1274 
1275 	/* match up mode to capabilities as best as possible */
1276 	switch (mode) {
1277 	case FM10K_XCAST_MODE_PROMISC:
1278 		if (vf_flags & FM10K_VF_FLAG_PROMISC_CAPABLE)
1279 			return FM10K_XCAST_MODE_PROMISC;
1280 		/* fallthough */
1281 	case FM10K_XCAST_MODE_ALLMULTI:
1282 		if (vf_flags & FM10K_VF_FLAG_ALLMULTI_CAPABLE)
1283 			return FM10K_XCAST_MODE_ALLMULTI;
1284 		/* fallthough */
1285 	case FM10K_XCAST_MODE_MULTI:
1286 		if (vf_flags & FM10K_VF_FLAG_MULTI_CAPABLE)
1287 			return FM10K_XCAST_MODE_MULTI;
1288 		/* fallthough */
1289 	case FM10K_XCAST_MODE_NONE:
1290 		if (vf_flags & FM10K_VF_FLAG_NONE_CAPABLE)
1291 			return FM10K_XCAST_MODE_NONE;
1292 		/* fallthough */
1293 	default:
1294 		break;
1295 	}
1296 
1297 	/* disable interface as it should not be able to request any */
1298 	return FM10K_XCAST_MODE_DISABLE;
1299 }
1300 
1301 /**
1302  *  fm10k_iov_msg_lport_state_pf - Message handler for port state requests
1303  *  @hw: Pointer to hardware structure
1304  *  @results: Pointer array to message, results[0] is pointer to message
1305  *  @mbx: Pointer to mailbox information structure
1306  *
1307  *  This function is a default handler for port state requests.  The port
1308  *  state requests for now are basic and consist of enabling or disabling
1309  *  the port.
1310  **/
1311 s32 fm10k_iov_msg_lport_state_pf(struct fm10k_hw *hw, u32 **results,
1312 				 struct fm10k_mbx_info *mbx)
1313 {
1314 	struct fm10k_vf_info *vf_info = (struct fm10k_vf_info *)mbx;
1315 	u32 *result;
1316 	s32 err = 0;
1317 	u32 msg[2];
1318 	u8 mode = 0;
1319 
1320 	/* verify VF is allowed to enable even minimal mode */
1321 	if (!(vf_info->vf_flags & FM10K_VF_FLAG_NONE_CAPABLE))
1322 		return FM10K_ERR_PARAM;
1323 
1324 	if (!!results[FM10K_LPORT_STATE_MSG_XCAST_MODE]) {
1325 		result = results[FM10K_LPORT_STATE_MSG_XCAST_MODE];
1326 
1327 		/* XCAST mode update requested */
1328 		err = fm10k_tlv_attr_get_u8(result, &mode);
1329 		if (err)
1330 			return FM10K_ERR_PARAM;
1331 
1332 		/* prep for possible demotion depending on capabilities */
1333 		mode = fm10k_iov_supported_xcast_mode_pf(vf_info, mode);
1334 
1335 		/* if mode is not currently enabled, enable it */
1336 		if (!(FM10K_VF_FLAG_ENABLED(vf_info) & (1 << mode)))
1337 			fm10k_update_xcast_mode_pf(hw, vf_info->glort, mode);
1338 
1339 		/* swap mode back to a bit flag */
1340 		mode = FM10K_VF_FLAG_SET_MODE(mode);
1341 	} else if (!results[FM10K_LPORT_STATE_MSG_DISABLE]) {
1342 		/* need to disable the port if it is already enabled */
1343 		if (FM10K_VF_FLAG_ENABLED(vf_info))
1344 			err = fm10k_update_lport_state_pf(hw, vf_info->glort,
1345 							  1, false);
1346 
1347 		/* we need to clear VF_FLAG_ENABLED flags in order to ensure
1348 		 * that we actually re-enable the LPORT state below. Note that
1349 		 * this has no impact if the VF is already disabled, as the
1350 		 * flags are already cleared.
1351 		 */
1352 		if (!err)
1353 			vf_info->vf_flags = FM10K_VF_FLAG_CAPABLE(vf_info);
1354 
1355 		/* when enabling the port we should reset the rate limiters */
1356 		hw->iov.ops.configure_tc(hw, vf_info->vf_idx, vf_info->rate);
1357 
1358 		/* set mode for minimal functionality */
1359 		mode = FM10K_VF_FLAG_SET_MODE_NONE;
1360 
1361 		/* generate port state response to notify VF it is ready */
1362 		fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_LPORT_STATE);
1363 		fm10k_tlv_attr_put_bool(msg, FM10K_LPORT_STATE_MSG_READY);
1364 		mbx->ops.enqueue_tx(hw, mbx, msg);
1365 	}
1366 
1367 	/* if enable state toggled note the update */
1368 	if (!err && (!FM10K_VF_FLAG_ENABLED(vf_info) != !mode))
1369 		err = fm10k_update_lport_state_pf(hw, vf_info->glort, 1,
1370 						  !!mode);
1371 
1372 	/* if state change succeeded, then update our stored state */
1373 	mode |= FM10K_VF_FLAG_CAPABLE(vf_info);
1374 	if (!err)
1375 		vf_info->vf_flags = mode;
1376 
1377 	return err;
1378 }
1379 
1380 const struct fm10k_msg_data fm10k_iov_msg_data_pf[] = {
1381 	FM10K_TLV_MSG_TEST_HANDLER(fm10k_tlv_msg_test),
1382 	FM10K_VF_MSG_MSIX_HANDLER(fm10k_iov_msg_msix_pf),
1383 	FM10K_VF_MSG_MAC_VLAN_HANDLER(fm10k_iov_msg_mac_vlan_pf),
1384 	FM10K_VF_MSG_LPORT_STATE_HANDLER(fm10k_iov_msg_lport_state_pf),
1385 	FM10K_TLV_MSG_ERROR_HANDLER(fm10k_tlv_msg_error),
1386 };
1387 
1388 /**
1389  *  fm10k_update_stats_hw_pf - Updates hardware related statistics of PF
1390  *  @hw: pointer to hardware structure
1391  *  @stats: pointer to the stats structure to update
1392  *
1393  *  This function collects and aggregates global and per queue hardware
1394  *  statistics.
1395  **/
1396 static void fm10k_update_hw_stats_pf(struct fm10k_hw *hw,
1397 				     struct fm10k_hw_stats *stats)
1398 {
1399 	u32 timeout, ur, ca, um, xec, vlan_drop, loopback_drop, nodesc_drop;
1400 	u32 id, id_prev;
1401 
1402 	/* Use Tx queue 0 as a canary to detect a reset */
1403 	id = fm10k_read_reg(hw, FM10K_TXQCTL(0));
1404 
1405 	/* Read Global Statistics */
1406 	do {
1407 		timeout = fm10k_read_hw_stats_32b(hw, FM10K_STATS_TIMEOUT,
1408 						  &stats->timeout);
1409 		ur = fm10k_read_hw_stats_32b(hw, FM10K_STATS_UR, &stats->ur);
1410 		ca = fm10k_read_hw_stats_32b(hw, FM10K_STATS_CA, &stats->ca);
1411 		um = fm10k_read_hw_stats_32b(hw, FM10K_STATS_UM, &stats->um);
1412 		xec = fm10k_read_hw_stats_32b(hw, FM10K_STATS_XEC, &stats->xec);
1413 		vlan_drop = fm10k_read_hw_stats_32b(hw, FM10K_STATS_VLAN_DROP,
1414 						    &stats->vlan_drop);
1415 		loopback_drop = fm10k_read_hw_stats_32b(hw,
1416 							FM10K_STATS_LOOPBACK_DROP,
1417 							&stats->loopback_drop);
1418 		nodesc_drop = fm10k_read_hw_stats_32b(hw,
1419 						      FM10K_STATS_NODESC_DROP,
1420 						      &stats->nodesc_drop);
1421 
1422 		/* if value has not changed then we have consistent data */
1423 		id_prev = id;
1424 		id = fm10k_read_reg(hw, FM10K_TXQCTL(0));
1425 	} while ((id ^ id_prev) & FM10K_TXQCTL_ID_MASK);
1426 
1427 	/* drop non-ID bits and set VALID ID bit */
1428 	id &= FM10K_TXQCTL_ID_MASK;
1429 	id |= FM10K_STAT_VALID;
1430 
1431 	/* Update Global Statistics */
1432 	if (stats->stats_idx == id) {
1433 		stats->timeout.count += timeout;
1434 		stats->ur.count += ur;
1435 		stats->ca.count += ca;
1436 		stats->um.count += um;
1437 		stats->xec.count += xec;
1438 		stats->vlan_drop.count += vlan_drop;
1439 		stats->loopback_drop.count += loopback_drop;
1440 		stats->nodesc_drop.count += nodesc_drop;
1441 	}
1442 
1443 	/* Update bases and record current PF id */
1444 	fm10k_update_hw_base_32b(&stats->timeout, timeout);
1445 	fm10k_update_hw_base_32b(&stats->ur, ur);
1446 	fm10k_update_hw_base_32b(&stats->ca, ca);
1447 	fm10k_update_hw_base_32b(&stats->um, um);
1448 	fm10k_update_hw_base_32b(&stats->xec, xec);
1449 	fm10k_update_hw_base_32b(&stats->vlan_drop, vlan_drop);
1450 	fm10k_update_hw_base_32b(&stats->loopback_drop, loopback_drop);
1451 	fm10k_update_hw_base_32b(&stats->nodesc_drop, nodesc_drop);
1452 	stats->stats_idx = id;
1453 
1454 	/* Update Queue Statistics */
1455 	fm10k_update_hw_stats_q(hw, stats->q, 0, hw->mac.max_queues);
1456 }
1457 
1458 /**
1459  *  fm10k_rebind_hw_stats_pf - Resets base for hardware statistics of PF
1460  *  @hw: pointer to hardware structure
1461  *  @stats: pointer to the stats structure to update
1462  *
1463  *  This function resets the base for global and per queue hardware
1464  *  statistics.
1465  **/
1466 static void fm10k_rebind_hw_stats_pf(struct fm10k_hw *hw,
1467 				     struct fm10k_hw_stats *stats)
1468 {
1469 	/* Unbind Global Statistics */
1470 	fm10k_unbind_hw_stats_32b(&stats->timeout);
1471 	fm10k_unbind_hw_stats_32b(&stats->ur);
1472 	fm10k_unbind_hw_stats_32b(&stats->ca);
1473 	fm10k_unbind_hw_stats_32b(&stats->um);
1474 	fm10k_unbind_hw_stats_32b(&stats->xec);
1475 	fm10k_unbind_hw_stats_32b(&stats->vlan_drop);
1476 	fm10k_unbind_hw_stats_32b(&stats->loopback_drop);
1477 	fm10k_unbind_hw_stats_32b(&stats->nodesc_drop);
1478 
1479 	/* Unbind Queue Statistics */
1480 	fm10k_unbind_hw_stats_q(stats->q, 0, hw->mac.max_queues);
1481 
1482 	/* Reinitialize bases for all stats */
1483 	fm10k_update_hw_stats_pf(hw, stats);
1484 }
1485 
1486 /**
1487  *  fm10k_set_dma_mask_pf - Configures PhyAddrSpace to limit DMA to system
1488  *  @hw: pointer to hardware structure
1489  *  @dma_mask: 64 bit DMA mask required for platform
1490  *
1491  *  This function sets the PHYADDR.PhyAddrSpace bits for the endpoint in order
1492  *  to limit the access to memory beyond what is physically in the system.
1493  **/
1494 static void fm10k_set_dma_mask_pf(struct fm10k_hw *hw, u64 dma_mask)
1495 {
1496 	/* we need to write the upper 32 bits of DMA mask to PhyAddrSpace */
1497 	u32 phyaddr = (u32)(dma_mask >> 32);
1498 
1499 	fm10k_write_reg(hw, FM10K_PHYADDR, phyaddr);
1500 }
1501 
1502 /**
1503  *  fm10k_get_fault_pf - Record a fault in one of the interface units
1504  *  @hw: pointer to hardware structure
1505  *  @type: pointer to fault type register offset
1506  *  @fault: pointer to memory location to record the fault
1507  *
1508  *  Record the fault register contents to the fault data structure and
1509  *  clear the entry from the register.
1510  *
1511  *  Returns ERR_PARAM if invalid register is specified or no error is present.
1512  **/
1513 static s32 fm10k_get_fault_pf(struct fm10k_hw *hw, int type,
1514 			      struct fm10k_fault *fault)
1515 {
1516 	u32 func;
1517 
1518 	/* verify the fault register is in range and is aligned */
1519 	switch (type) {
1520 	case FM10K_PCA_FAULT:
1521 	case FM10K_THI_FAULT:
1522 	case FM10K_FUM_FAULT:
1523 		break;
1524 	default:
1525 		return FM10K_ERR_PARAM;
1526 	}
1527 
1528 	/* only service faults that are valid */
1529 	func = fm10k_read_reg(hw, type + FM10K_FAULT_FUNC);
1530 	if (!(func & FM10K_FAULT_FUNC_VALID))
1531 		return FM10K_ERR_PARAM;
1532 
1533 	/* read remaining fields */
1534 	fault->address = fm10k_read_reg(hw, type + FM10K_FAULT_ADDR_HI);
1535 	fault->address <<= 32;
1536 	fault->address = fm10k_read_reg(hw, type + FM10K_FAULT_ADDR_LO);
1537 	fault->specinfo = fm10k_read_reg(hw, type + FM10K_FAULT_SPECINFO);
1538 
1539 	/* clear valid bit to allow for next error */
1540 	fm10k_write_reg(hw, type + FM10K_FAULT_FUNC, FM10K_FAULT_FUNC_VALID);
1541 
1542 	/* Record which function triggered the error */
1543 	if (func & FM10K_FAULT_FUNC_PF)
1544 		fault->func = 0;
1545 	else
1546 		fault->func = 1 + ((func & FM10K_FAULT_FUNC_VF_MASK) >>
1547 				   FM10K_FAULT_FUNC_VF_SHIFT);
1548 
1549 	/* record fault type */
1550 	fault->type = func & FM10K_FAULT_FUNC_TYPE_MASK;
1551 
1552 	return 0;
1553 }
1554 
1555 /**
1556  *  fm10k_request_lport_map_pf - Request LPORT map from the switch API
1557  *  @hw: pointer to hardware structure
1558  *
1559  **/
1560 static s32 fm10k_request_lport_map_pf(struct fm10k_hw *hw)
1561 {
1562 	struct fm10k_mbx_info *mbx = &hw->mbx;
1563 	u32 msg[1];
1564 
1565 	/* issue request asking for LPORT map */
1566 	fm10k_tlv_msg_init(msg, FM10K_PF_MSG_ID_LPORT_MAP);
1567 
1568 	/* load onto outgoing mailbox */
1569 	return mbx->ops.enqueue_tx(hw, mbx, msg);
1570 }
1571 
1572 /**
1573  *  fm10k_get_host_state_pf - Returns the state of the switch and mailbox
1574  *  @hw: pointer to hardware structure
1575  *  @switch_ready: pointer to boolean value that will record switch state
1576  *
1577  *  This funciton will check the DMA_CTRL2 register and mailbox in order
1578  *  to determine if the switch is ready for the PF to begin requesting
1579  *  addresses and mapping traffic to the local interface.
1580  **/
1581 static s32 fm10k_get_host_state_pf(struct fm10k_hw *hw, bool *switch_ready)
1582 {
1583 	s32 ret_val = 0;
1584 	u32 dma_ctrl2;
1585 
1586 	/* verify the switch is ready for interaction */
1587 	dma_ctrl2 = fm10k_read_reg(hw, FM10K_DMA_CTRL2);
1588 	if (!(dma_ctrl2 & FM10K_DMA_CTRL2_SWITCH_READY))
1589 		goto out;
1590 
1591 	/* retrieve generic host state info */
1592 	ret_val = fm10k_get_host_state_generic(hw, switch_ready);
1593 	if (ret_val)
1594 		goto out;
1595 
1596 	/* interface cannot receive traffic without logical ports */
1597 	if (hw->mac.dglort_map == FM10K_DGLORTMAP_NONE)
1598 		ret_val = fm10k_request_lport_map_pf(hw);
1599 
1600 out:
1601 	return ret_val;
1602 }
1603 
1604 /* This structure defines the attibutes to be parsed below */
1605 const struct fm10k_tlv_attr fm10k_lport_map_msg_attr[] = {
1606 	FM10K_TLV_ATTR_U32(FM10K_PF_ATTR_ID_LPORT_MAP),
1607 	FM10K_TLV_ATTR_LAST
1608 };
1609 
1610 /**
1611  *  fm10k_msg_lport_map_pf - Message handler for lport_map message from SM
1612  *  @hw: Pointer to hardware structure
1613  *  @results: pointer array containing parsed data
1614  *  @mbx: Pointer to mailbox information structure
1615  *
1616  *  This handler configures the lport mapping based on the reply from the
1617  *  switch API.
1618  **/
1619 s32 fm10k_msg_lport_map_pf(struct fm10k_hw *hw, u32 **results,
1620 			   struct fm10k_mbx_info *mbx)
1621 {
1622 	u16 glort, mask;
1623 	u32 dglort_map;
1624 	s32 err;
1625 
1626 	err = fm10k_tlv_attr_get_u32(results[FM10K_PF_ATTR_ID_LPORT_MAP],
1627 				     &dglort_map);
1628 	if (err)
1629 		return err;
1630 
1631 	/* extract values out of the header */
1632 	glort = FM10K_MSG_HDR_FIELD_GET(dglort_map, LPORT_MAP_GLORT);
1633 	mask = FM10K_MSG_HDR_FIELD_GET(dglort_map, LPORT_MAP_MASK);
1634 
1635 	/* verify mask is set and none of the masked bits in glort are set */
1636 	if (!mask || (glort & ~mask))
1637 		return FM10K_ERR_PARAM;
1638 
1639 	/* verify the mask is contiguous, and that it is 1's followed by 0's */
1640 	if (((~(mask - 1) & mask) + mask) & FM10K_DGLORTMAP_NONE)
1641 		return FM10K_ERR_PARAM;
1642 
1643 	/* record the glort, mask, and port count */
1644 	hw->mac.dglort_map = dglort_map;
1645 
1646 	return 0;
1647 }
1648 
1649 const struct fm10k_tlv_attr fm10k_update_pvid_msg_attr[] = {
1650 	FM10K_TLV_ATTR_U32(FM10K_PF_ATTR_ID_UPDATE_PVID),
1651 	FM10K_TLV_ATTR_LAST
1652 };
1653 
1654 /**
1655  *  fm10k_msg_update_pvid_pf - Message handler for port VLAN message from SM
1656  *  @hw: Pointer to hardware structure
1657  *  @results: pointer array containing parsed data
1658  *  @mbx: Pointer to mailbox information structure
1659  *
1660  *  This handler configures the default VLAN for the PF
1661  **/
1662 s32 fm10k_msg_update_pvid_pf(struct fm10k_hw *hw, u32 **results,
1663 			     struct fm10k_mbx_info *mbx)
1664 {
1665 	u16 glort, pvid;
1666 	u32 pvid_update;
1667 	s32 err;
1668 
1669 	err = fm10k_tlv_attr_get_u32(results[FM10K_PF_ATTR_ID_UPDATE_PVID],
1670 				     &pvid_update);
1671 	if (err)
1672 		return err;
1673 
1674 	/* extract values from the pvid update */
1675 	glort = FM10K_MSG_HDR_FIELD_GET(pvid_update, UPDATE_PVID_GLORT);
1676 	pvid = FM10K_MSG_HDR_FIELD_GET(pvid_update, UPDATE_PVID_PVID);
1677 
1678 	/* if glort is not valid return error */
1679 	if (!fm10k_glort_valid_pf(hw, glort))
1680 		return FM10K_ERR_PARAM;
1681 
1682 	/* verify VID is valid */
1683 	if (pvid >= FM10K_VLAN_TABLE_VID_MAX)
1684 		return FM10K_ERR_PARAM;
1685 
1686 	/* record the port VLAN ID value */
1687 	hw->mac.default_vid = pvid;
1688 
1689 	return 0;
1690 }
1691 
1692 /**
1693  *  fm10k_record_global_table_data - Move global table data to swapi table info
1694  *  @from: pointer to source table data structure
1695  *  @to: pointer to destination table info structure
1696  *
1697  *  This function is will copy table_data to the table_info contained in
1698  *  the hw struct.
1699  **/
1700 static void fm10k_record_global_table_data(struct fm10k_global_table_data *from,
1701 					   struct fm10k_swapi_table_info *to)
1702 {
1703 	/* convert from le32 struct to CPU byte ordered values */
1704 	to->used = le32_to_cpu(from->used);
1705 	to->avail = le32_to_cpu(from->avail);
1706 }
1707 
1708 const struct fm10k_tlv_attr fm10k_err_msg_attr[] = {
1709 	FM10K_TLV_ATTR_LE_STRUCT(FM10K_PF_ATTR_ID_ERR,
1710 				 sizeof(struct fm10k_swapi_error)),
1711 	FM10K_TLV_ATTR_LAST
1712 };
1713 
1714 /**
1715  *  fm10k_msg_err_pf - Message handler for error reply
1716  *  @hw: Pointer to hardware structure
1717  *  @results: pointer array containing parsed data
1718  *  @mbx: Pointer to mailbox information structure
1719  *
1720  *  This handler will capture the data for any error replies to previous
1721  *  messages that the PF has sent.
1722  **/
1723 s32 fm10k_msg_err_pf(struct fm10k_hw *hw, u32 **results,
1724 		     struct fm10k_mbx_info *mbx)
1725 {
1726 	struct fm10k_swapi_error err_msg;
1727 	s32 err;
1728 
1729 	/* extract structure from message */
1730 	err = fm10k_tlv_attr_get_le_struct(results[FM10K_PF_ATTR_ID_ERR],
1731 					   &err_msg, sizeof(err_msg));
1732 	if (err)
1733 		return err;
1734 
1735 	/* record table status */
1736 	fm10k_record_global_table_data(&err_msg.mac, &hw->swapi.mac);
1737 	fm10k_record_global_table_data(&err_msg.nexthop, &hw->swapi.nexthop);
1738 	fm10k_record_global_table_data(&err_msg.ffu, &hw->swapi.ffu);
1739 
1740 	/* record SW API status value */
1741 	hw->swapi.status = le32_to_cpu(err_msg.status);
1742 
1743 	return 0;
1744 }
1745 
1746 const struct fm10k_tlv_attr fm10k_1588_timestamp_msg_attr[] = {
1747 	FM10K_TLV_ATTR_LE_STRUCT(FM10K_PF_ATTR_ID_1588_TIMESTAMP,
1748 				 sizeof(struct fm10k_swapi_1588_timestamp)),
1749 	FM10K_TLV_ATTR_LAST
1750 };
1751 
1752 /* currently there is no shared 1588 timestamp handler */
1753 
1754 /**
1755  *  fm10k_adjust_systime_pf - Adjust systime frequency
1756  *  @hw: pointer to hardware structure
1757  *  @ppb: adjustment rate in parts per billion
1758  *
1759  *  This function will adjust the SYSTIME_CFG register contained in BAR 4
1760  *  if this function is supported for BAR 4 access.  The adjustment amount
1761  *  is based on the parts per billion value provided and adjusted to a
1762  *  value based on parts per 2^48 clock cycles.
1763  *
1764  *  If adjustment is not supported or the requested value is too large
1765  *  we will return an error.
1766  **/
1767 static s32 fm10k_adjust_systime_pf(struct fm10k_hw *hw, s32 ppb)
1768 {
1769 	u64 systime_adjust;
1770 
1771 	/* if sw_addr is not set we don't have switch register access */
1772 	if (!hw->sw_addr)
1773 		return ppb ? FM10K_ERR_PARAM : 0;
1774 
1775 	/* we must convert the value from parts per billion to parts per
1776 	 * 2^48 cycles.  In addition I have opted to only use the 30 most
1777 	 * significant bits of the adjustment value as the 8 least
1778 	 * significant bits are located in another register and represent
1779 	 * a value significantly less than a part per billion, the result
1780 	 * of dropping the 8 least significant bits is that the adjustment
1781 	 * value is effectively multiplied by 2^8 when we write it.
1782 	 *
1783 	 * As a result of all this the math for this breaks down as follows:
1784 	 *	ppb / 10^9 == adjust * 2^8 / 2^48
1785 	 * If we solve this for adjust, and simplify it comes out as:
1786 	 *	ppb * 2^31 / 5^9 == adjust
1787 	 */
1788 	systime_adjust = (ppb < 0) ? -ppb : ppb;
1789 	systime_adjust <<= 31;
1790 	do_div(systime_adjust, 1953125);
1791 
1792 	/* verify the requested adjustment value is in range */
1793 	if (systime_adjust > FM10K_SW_SYSTIME_ADJUST_MASK)
1794 		return FM10K_ERR_PARAM;
1795 
1796 	if (ppb > 0)
1797 		systime_adjust |= FM10K_SW_SYSTIME_ADJUST_DIR_POSITIVE;
1798 
1799 	fm10k_write_sw_reg(hw, FM10K_SW_SYSTIME_ADJUST, (u32)systime_adjust);
1800 
1801 	return 0;
1802 }
1803 
1804 /**
1805  *  fm10k_read_systime_pf - Reads value of systime registers
1806  *  @hw: pointer to the hardware structure
1807  *
1808  *  Function reads the content of 2 registers, combined to represent a 64 bit
1809  *  value measured in nanosecods.  In order to guarantee the value is accurate
1810  *  we check the 32 most significant bits both before and after reading the
1811  *  32 least significant bits to verify they didn't change as we were reading
1812  *  the registers.
1813  **/
1814 static u64 fm10k_read_systime_pf(struct fm10k_hw *hw)
1815 {
1816 	u32 systime_l, systime_h, systime_tmp;
1817 
1818 	systime_h = fm10k_read_reg(hw, FM10K_SYSTIME + 1);
1819 
1820 	do {
1821 		systime_tmp = systime_h;
1822 		systime_l = fm10k_read_reg(hw, FM10K_SYSTIME);
1823 		systime_h = fm10k_read_reg(hw, FM10K_SYSTIME + 1);
1824 	} while (systime_tmp != systime_h);
1825 
1826 	return ((u64)systime_h << 32) | systime_l;
1827 }
1828 
1829 static const struct fm10k_msg_data fm10k_msg_data_pf[] = {
1830 	FM10K_PF_MSG_ERR_HANDLER(XCAST_MODES, fm10k_msg_err_pf),
1831 	FM10K_PF_MSG_ERR_HANDLER(UPDATE_MAC_FWD_RULE, fm10k_msg_err_pf),
1832 	FM10K_PF_MSG_LPORT_MAP_HANDLER(fm10k_msg_lport_map_pf),
1833 	FM10K_PF_MSG_ERR_HANDLER(LPORT_CREATE, fm10k_msg_err_pf),
1834 	FM10K_PF_MSG_ERR_HANDLER(LPORT_DELETE, fm10k_msg_err_pf),
1835 	FM10K_PF_MSG_UPDATE_PVID_HANDLER(fm10k_msg_update_pvid_pf),
1836 	FM10K_TLV_MSG_ERROR_HANDLER(fm10k_tlv_msg_error),
1837 };
1838 
1839 static struct fm10k_mac_ops mac_ops_pf = {
1840 	.get_bus_info		= &fm10k_get_bus_info_generic,
1841 	.reset_hw		= &fm10k_reset_hw_pf,
1842 	.init_hw		= &fm10k_init_hw_pf,
1843 	.start_hw		= &fm10k_start_hw_generic,
1844 	.stop_hw		= &fm10k_stop_hw_generic,
1845 	.update_vlan		= &fm10k_update_vlan_pf,
1846 	.read_mac_addr		= &fm10k_read_mac_addr_pf,
1847 	.update_uc_addr		= &fm10k_update_uc_addr_pf,
1848 	.update_mc_addr		= &fm10k_update_mc_addr_pf,
1849 	.update_xcast_mode	= &fm10k_update_xcast_mode_pf,
1850 	.update_int_moderator	= &fm10k_update_int_moderator_pf,
1851 	.update_lport_state	= &fm10k_update_lport_state_pf,
1852 	.update_hw_stats	= &fm10k_update_hw_stats_pf,
1853 	.rebind_hw_stats	= &fm10k_rebind_hw_stats_pf,
1854 	.configure_dglort_map	= &fm10k_configure_dglort_map_pf,
1855 	.set_dma_mask		= &fm10k_set_dma_mask_pf,
1856 	.get_fault		= &fm10k_get_fault_pf,
1857 	.get_host_state		= &fm10k_get_host_state_pf,
1858 	.adjust_systime		= &fm10k_adjust_systime_pf,
1859 	.read_systime		= &fm10k_read_systime_pf,
1860 };
1861 
1862 static struct fm10k_iov_ops iov_ops_pf = {
1863 	.assign_resources		= &fm10k_iov_assign_resources_pf,
1864 	.configure_tc			= &fm10k_iov_configure_tc_pf,
1865 	.assign_int_moderator		= &fm10k_iov_assign_int_moderator_pf,
1866 	.assign_default_mac_vlan	= fm10k_iov_assign_default_mac_vlan_pf,
1867 	.reset_resources		= &fm10k_iov_reset_resources_pf,
1868 	.set_lport			= &fm10k_iov_set_lport_pf,
1869 	.reset_lport			= &fm10k_iov_reset_lport_pf,
1870 	.update_stats			= &fm10k_iov_update_stats_pf,
1871 	.report_timestamp		= &fm10k_iov_report_timestamp_pf,
1872 };
1873 
1874 static s32 fm10k_get_invariants_pf(struct fm10k_hw *hw)
1875 {
1876 	fm10k_get_invariants_generic(hw);
1877 
1878 	return fm10k_sm_mbx_init(hw, &hw->mbx, fm10k_msg_data_pf);
1879 }
1880 
1881 struct fm10k_info fm10k_pf_info = {
1882 	.mac		= fm10k_mac_pf,
1883 	.get_invariants	= &fm10k_get_invariants_pf,
1884 	.mac_ops	= &mac_ops_pf,
1885 	.iov_ops	= &iov_ops_pf,
1886 };
1887