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