]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/net/ethernet/intel/i40e/i40e_main.c
i40e: lock service task correctly
[mirror_ubuntu-artful-kernel.git] / drivers / net / ethernet / intel / i40e / i40e_main.c
CommitLineData
41c445ff
JB
1/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
2818ccd9 4 * Copyright(c) 2013 - 2016 Intel Corporation.
41c445ff
JB
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
dc641b73
GR
15 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
41c445ff
JB
17 *
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 *
21 * Contact Information:
22 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 ******************************************************************************/
26
b499ffb0
SV
27#include <linux/etherdevice.h>
28#include <linux/of_net.h>
29#include <linux/pci.h>
30
41c445ff
JB
31/* Local includes */
32#include "i40e.h"
4eb3f768 33#include "i40e_diag.h"
06a5f7f1 34#include <net/udp_tunnel.h>
41c445ff
JB
35
36const char i40e_driver_name[] = "i40e";
37static const char i40e_driver_string[] =
38 "Intel(R) Ethernet Connection XL710 Network Driver";
39
40#define DRV_KERN "-k"
41
e8e724db 42#define DRV_VERSION_MAJOR 1
07061958 43#define DRV_VERSION_MINOR 6
36023869 44#define DRV_VERSION_BUILD 25
41c445ff
JB
45#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
46 __stringify(DRV_VERSION_MINOR) "." \
47 __stringify(DRV_VERSION_BUILD) DRV_KERN
48const char i40e_driver_version_str[] = DRV_VERSION;
8fb905b3 49static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
41c445ff
JB
50
51/* a bit of forward declarations */
52static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
53static void i40e_handle_reset_warning(struct i40e_pf *pf);
54static int i40e_add_vsi(struct i40e_vsi *vsi);
55static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
bc7d338f 56static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
41c445ff
JB
57static int i40e_setup_misc_vector(struct i40e_pf *pf);
58static void i40e_determine_queue_usage(struct i40e_pf *pf);
59static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
cbf61325 60static void i40e_fdir_sb_setup(struct i40e_pf *pf);
4e3b35b0 61static int i40e_veb_get_bw_info(struct i40e_veb *veb);
41c445ff
JB
62
63/* i40e_pci_tbl - PCI Device ID Table
64 *
65 * Last entry must be all 0s
66 *
67 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
68 * Class, Class Mask, private data (not used) }
69 */
9baa3c34 70static const struct pci_device_id i40e_pci_tbl[] = {
ab60085e 71 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
ab60085e 72 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
ab60085e
SN
73 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
74 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
ab60085e
SN
75 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
76 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
77 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
5960d33f 78 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
bc5166b9 79 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
ae24b409 80 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
35dae51d
ASJ
81 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
82 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
87e6c1d7
ASJ
83 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
84 {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
85 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
d6bf58c2 86 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
48a3b512
SN
87 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
88 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
3123237a
CW
89 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
90 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
41c445ff
JB
91 /* required last entry */
92 {0, }
93};
94MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
95
96#define I40E_MAX_VF_COUNT 128
97static int debug = -1;
5d4ca23e
AD
98module_param(debug, uint, 0);
99MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
41c445ff
JB
100
101MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
102MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
103MODULE_LICENSE("GPL");
104MODULE_VERSION(DRV_VERSION);
105
2803b16c
JB
106static struct workqueue_struct *i40e_wq;
107
41c445ff
JB
108/**
109 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
110 * @hw: pointer to the HW structure
111 * @mem: ptr to mem struct to fill out
112 * @size: size of memory requested
113 * @alignment: what to align the allocation to
114 **/
115int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
116 u64 size, u32 alignment)
117{
118 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
119
120 mem->size = ALIGN(size, alignment);
121 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
122 &mem->pa, GFP_KERNEL);
93bc73b8
JB
123 if (!mem->va)
124 return -ENOMEM;
41c445ff 125
93bc73b8 126 return 0;
41c445ff
JB
127}
128
129/**
130 * i40e_free_dma_mem_d - OS specific memory free for shared code
131 * @hw: pointer to the HW structure
132 * @mem: ptr to mem struct to free
133 **/
134int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
135{
136 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
137
138 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
139 mem->va = NULL;
140 mem->pa = 0;
141 mem->size = 0;
142
143 return 0;
144}
145
146/**
147 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
148 * @hw: pointer to the HW structure
149 * @mem: ptr to mem struct to fill out
150 * @size: size of memory requested
151 **/
152int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
153 u32 size)
154{
155 mem->size = size;
156 mem->va = kzalloc(size, GFP_KERNEL);
157
93bc73b8
JB
158 if (!mem->va)
159 return -ENOMEM;
41c445ff 160
93bc73b8 161 return 0;
41c445ff
JB
162}
163
164/**
165 * i40e_free_virt_mem_d - OS specific memory free for shared code
166 * @hw: pointer to the HW structure
167 * @mem: ptr to mem struct to free
168 **/
169int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
170{
171 /* it's ok to kfree a NULL pointer */
172 kfree(mem->va);
173 mem->va = NULL;
174 mem->size = 0;
175
176 return 0;
177}
178
179/**
180 * i40e_get_lump - find a lump of free generic resource
181 * @pf: board private structure
182 * @pile: the pile of resource to search
183 * @needed: the number of items needed
184 * @id: an owner id to stick on the items assigned
185 *
186 * Returns the base item index of the lump, or negative for error
187 *
188 * The search_hint trick and lack of advanced fit-finding only work
189 * because we're highly likely to have all the same size lump requests.
190 * Linear search time and any fragmentation should be minimal.
191 **/
192static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
193 u16 needed, u16 id)
194{
195 int ret = -ENOMEM;
ddf434ac 196 int i, j;
41c445ff
JB
197
198 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
199 dev_info(&pf->pdev->dev,
200 "param err: pile=%p needed=%d id=0x%04x\n",
201 pile, needed, id);
202 return -EINVAL;
203 }
204
205 /* start the linear search with an imperfect hint */
206 i = pile->search_hint;
ddf434ac 207 while (i < pile->num_entries) {
41c445ff
JB
208 /* skip already allocated entries */
209 if (pile->list[i] & I40E_PILE_VALID_BIT) {
210 i++;
211 continue;
212 }
213
214 /* do we have enough in this lump? */
215 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
216 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
217 break;
218 }
219
220 if (j == needed) {
221 /* there was enough, so assign it to the requestor */
222 for (j = 0; j < needed; j++)
223 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
224 ret = i;
225 pile->search_hint = i + j;
ddf434ac 226 break;
41c445ff 227 }
6995b36c
JB
228
229 /* not enough, so skip over it and continue looking */
230 i += j;
41c445ff
JB
231 }
232
233 return ret;
234}
235
236/**
237 * i40e_put_lump - return a lump of generic resource
238 * @pile: the pile of resource to search
239 * @index: the base item index
240 * @id: the owner id of the items assigned
241 *
242 * Returns the count of items in the lump
243 **/
244static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
245{
246 int valid_id = (id | I40E_PILE_VALID_BIT);
247 int count = 0;
248 int i;
249
250 if (!pile || index >= pile->num_entries)
251 return -EINVAL;
252
253 for (i = index;
254 i < pile->num_entries && pile->list[i] == valid_id;
255 i++) {
256 pile->list[i] = 0;
257 count++;
258 }
259
260 if (count && index < pile->search_hint)
261 pile->search_hint = index;
262
263 return count;
264}
265
fdf0e0bf
ASJ
266/**
267 * i40e_find_vsi_from_id - searches for the vsi with the given id
268 * @pf - the pf structure to search for the vsi
269 * @id - id of the vsi it is searching for
270 **/
271struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
272{
273 int i;
274
275 for (i = 0; i < pf->num_alloc_vsi; i++)
276 if (pf->vsi[i] && (pf->vsi[i]->id == id))
277 return pf->vsi[i];
278
279 return NULL;
280}
281
41c445ff
JB
282/**
283 * i40e_service_event_schedule - Schedule the service task to wake up
284 * @pf: board private structure
285 *
286 * If not already scheduled, this puts the task into the work queue
287 **/
e3219ce6 288void i40e_service_event_schedule(struct i40e_pf *pf)
41c445ff
JB
289{
290 if (!test_bit(__I40E_DOWN, &pf->state) &&
91089033 291 !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
2803b16c 292 queue_work(i40e_wq, &pf->service_task);
41c445ff
JB
293}
294
295/**
296 * i40e_tx_timeout - Respond to a Tx Hang
297 * @netdev: network interface device structure
298 *
299 * If any port has noticed a Tx timeout, it is likely that the whole
300 * device is munged, not just the one netdev port, so go for the full
301 * reset.
302 **/
38e00438
VD
303#ifdef I40E_FCOE
304void i40e_tx_timeout(struct net_device *netdev)
305#else
41c445ff 306static void i40e_tx_timeout(struct net_device *netdev)
38e00438 307#endif
41c445ff
JB
308{
309 struct i40e_netdev_priv *np = netdev_priv(netdev);
310 struct i40e_vsi *vsi = np->vsi;
311 struct i40e_pf *pf = vsi->back;
b03a8c1f
KP
312 struct i40e_ring *tx_ring = NULL;
313 unsigned int i, hung_queue = 0;
314 u32 head, val;
41c445ff
JB
315
316 pf->tx_timeout_count++;
317
b03a8c1f
KP
318 /* find the stopped queue the same way the stack does */
319 for (i = 0; i < netdev->num_tx_queues; i++) {
320 struct netdev_queue *q;
321 unsigned long trans_start;
322
323 q = netdev_get_tx_queue(netdev, i);
9b36627a 324 trans_start = q->trans_start;
b03a8c1f
KP
325 if (netif_xmit_stopped(q) &&
326 time_after(jiffies,
327 (trans_start + netdev->watchdog_timeo))) {
328 hung_queue = i;
329 break;
330 }
331 }
332
333 if (i == netdev->num_tx_queues) {
334 netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
335 } else {
336 /* now that we have an index, find the tx_ring struct */
337 for (i = 0; i < vsi->num_queue_pairs; i++) {
338 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
339 if (hung_queue ==
340 vsi->tx_rings[i]->queue_index) {
341 tx_ring = vsi->tx_rings[i];
342 break;
343 }
344 }
345 }
346 }
347
41c445ff 348 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
b03a8c1f
KP
349 pf->tx_timeout_recovery_level = 1; /* reset after some time */
350 else if (time_before(jiffies,
351 (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
352 return; /* don't do any new action before the next timeout */
353
354 if (tx_ring) {
355 head = i40e_get_head(tx_ring);
356 /* Read interrupt register */
357 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
358 val = rd32(&pf->hw,
359 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
360 tx_ring->vsi->base_vector - 1));
361 else
362 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
363
364 netdev_info(netdev, "tx_timeout: VSI_seid: %d, Q %d, NTC: 0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x, INT: 0x%x\n",
365 vsi->seid, hung_queue, tx_ring->next_to_clean,
366 head, tx_ring->next_to_use,
367 readl(tx_ring->tail), val);
368 }
369
41c445ff 370 pf->tx_timeout_last_recovery = jiffies;
b03a8c1f
KP
371 netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
372 pf->tx_timeout_recovery_level, hung_queue);
41c445ff
JB
373
374 switch (pf->tx_timeout_recovery_level) {
41c445ff
JB
375 case 1:
376 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
377 break;
378 case 2:
379 set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
380 break;
381 case 3:
382 set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
383 break;
384 default:
385 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
41c445ff
JB
386 break;
387 }
b03a8c1f 388
41c445ff
JB
389 i40e_service_event_schedule(pf);
390 pf->tx_timeout_recovery_level++;
391}
392
41c445ff
JB
393/**
394 * i40e_get_vsi_stats_struct - Get System Network Statistics
395 * @vsi: the VSI we care about
396 *
397 * Returns the address of the device statistics structure.
398 * The statistics are actually updated from the service task.
399 **/
400struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
401{
402 return &vsi->net_stats;
403}
404
405/**
406 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
407 * @netdev: network interface device structure
408 *
409 * Returns the address of the device statistics structure.
410 * The statistics are actually updated from the service task.
411 **/
38e00438
VD
412#ifdef I40E_FCOE
413struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
414 struct net_device *netdev,
415 struct rtnl_link_stats64 *stats)
416#else
41c445ff
JB
417static struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
418 struct net_device *netdev,
980e9b11 419 struct rtnl_link_stats64 *stats)
38e00438 420#endif
41c445ff
JB
421{
422 struct i40e_netdev_priv *np = netdev_priv(netdev);
e7046ee1 423 struct i40e_ring *tx_ring, *rx_ring;
41c445ff 424 struct i40e_vsi *vsi = np->vsi;
980e9b11
AD
425 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
426 int i;
427
bc7d338f
ASJ
428 if (test_bit(__I40E_DOWN, &vsi->state))
429 return stats;
430
3c325ced
JB
431 if (!vsi->tx_rings)
432 return stats;
433
980e9b11
AD
434 rcu_read_lock();
435 for (i = 0; i < vsi->num_queue_pairs; i++) {
980e9b11
AD
436 u64 bytes, packets;
437 unsigned int start;
438
439 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
440 if (!tx_ring)
441 continue;
442
443 do {
57a7744e 444 start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
980e9b11
AD
445 packets = tx_ring->stats.packets;
446 bytes = tx_ring->stats.bytes;
57a7744e 447 } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
980e9b11
AD
448
449 stats->tx_packets += packets;
450 stats->tx_bytes += bytes;
451 rx_ring = &tx_ring[1];
452
453 do {
57a7744e 454 start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
980e9b11
AD
455 packets = rx_ring->stats.packets;
456 bytes = rx_ring->stats.bytes;
57a7744e 457 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
41c445ff 458
980e9b11
AD
459 stats->rx_packets += packets;
460 stats->rx_bytes += bytes;
461 }
462 rcu_read_unlock();
463
a5282f44 464 /* following stats updated by i40e_watchdog_subtask() */
980e9b11
AD
465 stats->multicast = vsi_stats->multicast;
466 stats->tx_errors = vsi_stats->tx_errors;
467 stats->tx_dropped = vsi_stats->tx_dropped;
468 stats->rx_errors = vsi_stats->rx_errors;
d8201e20 469 stats->rx_dropped = vsi_stats->rx_dropped;
980e9b11
AD
470 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
471 stats->rx_length_errors = vsi_stats->rx_length_errors;
41c445ff 472
980e9b11 473 return stats;
41c445ff
JB
474}
475
476/**
477 * i40e_vsi_reset_stats - Resets all stats of the given vsi
478 * @vsi: the VSI to have its stats reset
479 **/
480void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
481{
482 struct rtnl_link_stats64 *ns;
483 int i;
484
485 if (!vsi)
486 return;
487
488 ns = i40e_get_vsi_stats_struct(vsi);
489 memset(ns, 0, sizeof(*ns));
490 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
491 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
492 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
8e9dca53 493 if (vsi->rx_rings && vsi->rx_rings[0]) {
41c445ff 494 for (i = 0; i < vsi->num_queue_pairs; i++) {
6995b36c 495 memset(&vsi->rx_rings[i]->stats, 0,
9f65e15b 496 sizeof(vsi->rx_rings[i]->stats));
6995b36c 497 memset(&vsi->rx_rings[i]->rx_stats, 0,
9f65e15b 498 sizeof(vsi->rx_rings[i]->rx_stats));
6995b36c 499 memset(&vsi->tx_rings[i]->stats, 0,
9f65e15b
AD
500 sizeof(vsi->tx_rings[i]->stats));
501 memset(&vsi->tx_rings[i]->tx_stats, 0,
502 sizeof(vsi->tx_rings[i]->tx_stats));
41c445ff 503 }
8e9dca53 504 }
41c445ff
JB
505 vsi->stat_offsets_loaded = false;
506}
507
508/**
b40c82e6 509 * i40e_pf_reset_stats - Reset all of the stats for the given PF
41c445ff
JB
510 * @pf: the PF to be reset
511 **/
512void i40e_pf_reset_stats(struct i40e_pf *pf)
513{
e91fdf76
SN
514 int i;
515
41c445ff
JB
516 memset(&pf->stats, 0, sizeof(pf->stats));
517 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
518 pf->stat_offsets_loaded = false;
e91fdf76
SN
519
520 for (i = 0; i < I40E_MAX_VEB; i++) {
521 if (pf->veb[i]) {
522 memset(&pf->veb[i]->stats, 0,
523 sizeof(pf->veb[i]->stats));
524 memset(&pf->veb[i]->stats_offsets, 0,
525 sizeof(pf->veb[i]->stats_offsets));
526 pf->veb[i]->stat_offsets_loaded = false;
527 }
528 }
42bce04e 529 pf->hw_csum_rx_error = 0;
41c445ff
JB
530}
531
532/**
533 * i40e_stat_update48 - read and update a 48 bit stat from the chip
534 * @hw: ptr to the hardware info
535 * @hireg: the high 32 bit reg to read
536 * @loreg: the low 32 bit reg to read
537 * @offset_loaded: has the initial offset been loaded yet
538 * @offset: ptr to current offset value
539 * @stat: ptr to the stat
540 *
541 * Since the device stats are not reset at PFReset, they likely will not
542 * be zeroed when the driver starts. We'll save the first values read
543 * and use them as offsets to be subtracted from the raw values in order
544 * to report stats that count from zero. In the process, we also manage
545 * the potential roll-over.
546 **/
547static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
548 bool offset_loaded, u64 *offset, u64 *stat)
549{
550 u64 new_data;
551
ab60085e 552 if (hw->device_id == I40E_DEV_ID_QEMU) {
41c445ff
JB
553 new_data = rd32(hw, loreg);
554 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
555 } else {
556 new_data = rd64(hw, loreg);
557 }
558 if (!offset_loaded)
559 *offset = new_data;
560 if (likely(new_data >= *offset))
561 *stat = new_data - *offset;
562 else
41a1d04b 563 *stat = (new_data + BIT_ULL(48)) - *offset;
41c445ff
JB
564 *stat &= 0xFFFFFFFFFFFFULL;
565}
566
567/**
568 * i40e_stat_update32 - read and update a 32 bit stat from the chip
569 * @hw: ptr to the hardware info
570 * @reg: the hw reg to read
571 * @offset_loaded: has the initial offset been loaded yet
572 * @offset: ptr to current offset value
573 * @stat: ptr to the stat
574 **/
575static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
576 bool offset_loaded, u64 *offset, u64 *stat)
577{
578 u32 new_data;
579
580 new_data = rd32(hw, reg);
581 if (!offset_loaded)
582 *offset = new_data;
583 if (likely(new_data >= *offset))
584 *stat = (u32)(new_data - *offset);
585 else
41a1d04b 586 *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
41c445ff
JB
587}
588
589/**
590 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
591 * @vsi: the VSI to be updated
592 **/
593void i40e_update_eth_stats(struct i40e_vsi *vsi)
594{
595 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
596 struct i40e_pf *pf = vsi->back;
597 struct i40e_hw *hw = &pf->hw;
598 struct i40e_eth_stats *oes;
599 struct i40e_eth_stats *es; /* device's eth stats */
600
601 es = &vsi->eth_stats;
602 oes = &vsi->eth_stats_offsets;
603
604 /* Gather up the stats that the hw collects */
605 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
606 vsi->stat_offsets_loaded,
607 &oes->tx_errors, &es->tx_errors);
608 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
609 vsi->stat_offsets_loaded,
610 &oes->rx_discards, &es->rx_discards);
41a9e55c
SN
611 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
612 vsi->stat_offsets_loaded,
613 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
614 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
615 vsi->stat_offsets_loaded,
616 &oes->tx_errors, &es->tx_errors);
41c445ff
JB
617
618 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
619 I40E_GLV_GORCL(stat_idx),
620 vsi->stat_offsets_loaded,
621 &oes->rx_bytes, &es->rx_bytes);
622 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
623 I40E_GLV_UPRCL(stat_idx),
624 vsi->stat_offsets_loaded,
625 &oes->rx_unicast, &es->rx_unicast);
626 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
627 I40E_GLV_MPRCL(stat_idx),
628 vsi->stat_offsets_loaded,
629 &oes->rx_multicast, &es->rx_multicast);
630 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
631 I40E_GLV_BPRCL(stat_idx),
632 vsi->stat_offsets_loaded,
633 &oes->rx_broadcast, &es->rx_broadcast);
634
635 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
636 I40E_GLV_GOTCL(stat_idx),
637 vsi->stat_offsets_loaded,
638 &oes->tx_bytes, &es->tx_bytes);
639 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
640 I40E_GLV_UPTCL(stat_idx),
641 vsi->stat_offsets_loaded,
642 &oes->tx_unicast, &es->tx_unicast);
643 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
644 I40E_GLV_MPTCL(stat_idx),
645 vsi->stat_offsets_loaded,
646 &oes->tx_multicast, &es->tx_multicast);
647 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
648 I40E_GLV_BPTCL(stat_idx),
649 vsi->stat_offsets_loaded,
650 &oes->tx_broadcast, &es->tx_broadcast);
651 vsi->stat_offsets_loaded = true;
652}
653
654/**
655 * i40e_update_veb_stats - Update Switch component statistics
656 * @veb: the VEB being updated
657 **/
658static void i40e_update_veb_stats(struct i40e_veb *veb)
659{
660 struct i40e_pf *pf = veb->pf;
661 struct i40e_hw *hw = &pf->hw;
662 struct i40e_eth_stats *oes;
663 struct i40e_eth_stats *es; /* device's eth stats */
fe860afb
NP
664 struct i40e_veb_tc_stats *veb_oes;
665 struct i40e_veb_tc_stats *veb_es;
666 int i, idx = 0;
41c445ff
JB
667
668 idx = veb->stats_idx;
669 es = &veb->stats;
670 oes = &veb->stats_offsets;
fe860afb
NP
671 veb_es = &veb->tc_stats;
672 veb_oes = &veb->tc_stats_offsets;
41c445ff
JB
673
674 /* Gather up the stats that the hw collects */
675 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
676 veb->stat_offsets_loaded,
677 &oes->tx_discards, &es->tx_discards);
7134f9ce
JB
678 if (hw->revision_id > 0)
679 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
680 veb->stat_offsets_loaded,
681 &oes->rx_unknown_protocol,
682 &es->rx_unknown_protocol);
41c445ff
JB
683 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
684 veb->stat_offsets_loaded,
685 &oes->rx_bytes, &es->rx_bytes);
686 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
687 veb->stat_offsets_loaded,
688 &oes->rx_unicast, &es->rx_unicast);
689 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
690 veb->stat_offsets_loaded,
691 &oes->rx_multicast, &es->rx_multicast);
692 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
693 veb->stat_offsets_loaded,
694 &oes->rx_broadcast, &es->rx_broadcast);
695
696 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
697 veb->stat_offsets_loaded,
698 &oes->tx_bytes, &es->tx_bytes);
699 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
700 veb->stat_offsets_loaded,
701 &oes->tx_unicast, &es->tx_unicast);
702 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
703 veb->stat_offsets_loaded,
704 &oes->tx_multicast, &es->tx_multicast);
705 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
706 veb->stat_offsets_loaded,
707 &oes->tx_broadcast, &es->tx_broadcast);
fe860afb
NP
708 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
709 i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
710 I40E_GLVEBTC_RPCL(i, idx),
711 veb->stat_offsets_loaded,
712 &veb_oes->tc_rx_packets[i],
713 &veb_es->tc_rx_packets[i]);
714 i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
715 I40E_GLVEBTC_RBCL(i, idx),
716 veb->stat_offsets_loaded,
717 &veb_oes->tc_rx_bytes[i],
718 &veb_es->tc_rx_bytes[i]);
719 i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
720 I40E_GLVEBTC_TPCL(i, idx),
721 veb->stat_offsets_loaded,
722 &veb_oes->tc_tx_packets[i],
723 &veb_es->tc_tx_packets[i]);
724 i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
725 I40E_GLVEBTC_TBCL(i, idx),
726 veb->stat_offsets_loaded,
727 &veb_oes->tc_tx_bytes[i],
728 &veb_es->tc_tx_bytes[i]);
729 }
41c445ff
JB
730 veb->stat_offsets_loaded = true;
731}
732
38e00438
VD
733#ifdef I40E_FCOE
734/**
735 * i40e_update_fcoe_stats - Update FCoE-specific ethernet statistics counters.
736 * @vsi: the VSI that is capable of doing FCoE
737 **/
738static void i40e_update_fcoe_stats(struct i40e_vsi *vsi)
739{
740 struct i40e_pf *pf = vsi->back;
741 struct i40e_hw *hw = &pf->hw;
742 struct i40e_fcoe_stats *ofs;
743 struct i40e_fcoe_stats *fs; /* device's eth stats */
744 int idx;
745
746 if (vsi->type != I40E_VSI_FCOE)
747 return;
748
4147e2c5 749 idx = hw->pf_id + I40E_FCOE_PF_STAT_OFFSET;
38e00438
VD
750 fs = &vsi->fcoe_stats;
751 ofs = &vsi->fcoe_stats_offsets;
752
753 i40e_stat_update32(hw, I40E_GL_FCOEPRC(idx),
754 vsi->fcoe_stat_offsets_loaded,
755 &ofs->rx_fcoe_packets, &fs->rx_fcoe_packets);
756 i40e_stat_update48(hw, I40E_GL_FCOEDWRCH(idx), I40E_GL_FCOEDWRCL(idx),
757 vsi->fcoe_stat_offsets_loaded,
758 &ofs->rx_fcoe_dwords, &fs->rx_fcoe_dwords);
759 i40e_stat_update32(hw, I40E_GL_FCOERPDC(idx),
760 vsi->fcoe_stat_offsets_loaded,
761 &ofs->rx_fcoe_dropped, &fs->rx_fcoe_dropped);
762 i40e_stat_update32(hw, I40E_GL_FCOEPTC(idx),
763 vsi->fcoe_stat_offsets_loaded,
764 &ofs->tx_fcoe_packets, &fs->tx_fcoe_packets);
765 i40e_stat_update48(hw, I40E_GL_FCOEDWTCH(idx), I40E_GL_FCOEDWTCL(idx),
766 vsi->fcoe_stat_offsets_loaded,
767 &ofs->tx_fcoe_dwords, &fs->tx_fcoe_dwords);
768 i40e_stat_update32(hw, I40E_GL_FCOECRC(idx),
769 vsi->fcoe_stat_offsets_loaded,
770 &ofs->fcoe_bad_fccrc, &fs->fcoe_bad_fccrc);
771 i40e_stat_update32(hw, I40E_GL_FCOELAST(idx),
772 vsi->fcoe_stat_offsets_loaded,
773 &ofs->fcoe_last_error, &fs->fcoe_last_error);
774 i40e_stat_update32(hw, I40E_GL_FCOEDDPC(idx),
775 vsi->fcoe_stat_offsets_loaded,
776 &ofs->fcoe_ddp_count, &fs->fcoe_ddp_count);
777
778 vsi->fcoe_stat_offsets_loaded = true;
779}
780
781#endif
41c445ff 782/**
7812fddc 783 * i40e_update_vsi_stats - Update the vsi statistics counters.
41c445ff
JB
784 * @vsi: the VSI to be updated
785 *
786 * There are a few instances where we store the same stat in a
787 * couple of different structs. This is partly because we have
788 * the netdev stats that need to be filled out, which is slightly
789 * different from the "eth_stats" defined by the chip and used in
7812fddc 790 * VF communications. We sort it out here.
41c445ff 791 **/
7812fddc 792static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
41c445ff
JB
793{
794 struct i40e_pf *pf = vsi->back;
41c445ff
JB
795 struct rtnl_link_stats64 *ons;
796 struct rtnl_link_stats64 *ns; /* netdev stats */
797 struct i40e_eth_stats *oes;
798 struct i40e_eth_stats *es; /* device's eth stats */
799 u32 tx_restart, tx_busy;
dd353109 800 u64 tx_lost_interrupt;
bf00b376 801 struct i40e_ring *p;
41c445ff 802 u32 rx_page, rx_buf;
bf00b376
AA
803 u64 bytes, packets;
804 unsigned int start;
2fc3d715 805 u64 tx_linearize;
164c9f54 806 u64 tx_force_wb;
41c445ff
JB
807 u64 rx_p, rx_b;
808 u64 tx_p, tx_b;
41c445ff
JB
809 u16 q;
810
811 if (test_bit(__I40E_DOWN, &vsi->state) ||
812 test_bit(__I40E_CONFIG_BUSY, &pf->state))
813 return;
814
815 ns = i40e_get_vsi_stats_struct(vsi);
816 ons = &vsi->net_stats_offsets;
817 es = &vsi->eth_stats;
818 oes = &vsi->eth_stats_offsets;
819
820 /* Gather up the netdev and vsi stats that the driver collects
821 * on the fly during packet processing
822 */
823 rx_b = rx_p = 0;
824 tx_b = tx_p = 0;
164c9f54 825 tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
dd353109 826 tx_lost_interrupt = 0;
41c445ff
JB
827 rx_page = 0;
828 rx_buf = 0;
980e9b11 829 rcu_read_lock();
41c445ff 830 for (q = 0; q < vsi->num_queue_pairs; q++) {
980e9b11
AD
831 /* locate Tx ring */
832 p = ACCESS_ONCE(vsi->tx_rings[q]);
833
834 do {
57a7744e 835 start = u64_stats_fetch_begin_irq(&p->syncp);
980e9b11
AD
836 packets = p->stats.packets;
837 bytes = p->stats.bytes;
57a7744e 838 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
980e9b11
AD
839 tx_b += bytes;
840 tx_p += packets;
841 tx_restart += p->tx_stats.restart_queue;
842 tx_busy += p->tx_stats.tx_busy;
2fc3d715 843 tx_linearize += p->tx_stats.tx_linearize;
164c9f54 844 tx_force_wb += p->tx_stats.tx_force_wb;
dd353109 845 tx_lost_interrupt += p->tx_stats.tx_lost_interrupt;
41c445ff 846
980e9b11
AD
847 /* Rx queue is part of the same block as Tx queue */
848 p = &p[1];
849 do {
57a7744e 850 start = u64_stats_fetch_begin_irq(&p->syncp);
980e9b11
AD
851 packets = p->stats.packets;
852 bytes = p->stats.bytes;
57a7744e 853 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
980e9b11
AD
854 rx_b += bytes;
855 rx_p += packets;
420136cc
MW
856 rx_buf += p->rx_stats.alloc_buff_failed;
857 rx_page += p->rx_stats.alloc_page_failed;
41c445ff 858 }
980e9b11 859 rcu_read_unlock();
41c445ff
JB
860 vsi->tx_restart = tx_restart;
861 vsi->tx_busy = tx_busy;
2fc3d715 862 vsi->tx_linearize = tx_linearize;
164c9f54 863 vsi->tx_force_wb = tx_force_wb;
dd353109 864 vsi->tx_lost_interrupt = tx_lost_interrupt;
41c445ff
JB
865 vsi->rx_page_failed = rx_page;
866 vsi->rx_buf_failed = rx_buf;
867
868 ns->rx_packets = rx_p;
869 ns->rx_bytes = rx_b;
870 ns->tx_packets = tx_p;
871 ns->tx_bytes = tx_b;
872
41c445ff 873 /* update netdev stats from eth stats */
7812fddc 874 i40e_update_eth_stats(vsi);
41c445ff
JB
875 ons->tx_errors = oes->tx_errors;
876 ns->tx_errors = es->tx_errors;
877 ons->multicast = oes->rx_multicast;
878 ns->multicast = es->rx_multicast;
41a9e55c
SN
879 ons->rx_dropped = oes->rx_discards;
880 ns->rx_dropped = es->rx_discards;
41c445ff
JB
881 ons->tx_dropped = oes->tx_discards;
882 ns->tx_dropped = es->tx_discards;
883
7812fddc 884 /* pull in a couple PF stats if this is the main vsi */
41c445ff 885 if (vsi == pf->vsi[pf->lan_vsi]) {
7812fddc
SN
886 ns->rx_crc_errors = pf->stats.crc_errors;
887 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
888 ns->rx_length_errors = pf->stats.rx_length_errors;
889 }
890}
41c445ff 891
7812fddc 892/**
b40c82e6 893 * i40e_update_pf_stats - Update the PF statistics counters.
7812fddc
SN
894 * @pf: the PF to be updated
895 **/
896static void i40e_update_pf_stats(struct i40e_pf *pf)
897{
898 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
899 struct i40e_hw_port_stats *nsd = &pf->stats;
900 struct i40e_hw *hw = &pf->hw;
901 u32 val;
902 int i;
41c445ff 903
7812fddc
SN
904 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
905 I40E_GLPRT_GORCL(hw->port),
906 pf->stat_offsets_loaded,
907 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
908 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
909 I40E_GLPRT_GOTCL(hw->port),
910 pf->stat_offsets_loaded,
911 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
912 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
913 pf->stat_offsets_loaded,
914 &osd->eth.rx_discards,
915 &nsd->eth.rx_discards);
532d283d
SN
916 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
917 I40E_GLPRT_UPRCL(hw->port),
918 pf->stat_offsets_loaded,
919 &osd->eth.rx_unicast,
920 &nsd->eth.rx_unicast);
7812fddc
SN
921 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
922 I40E_GLPRT_MPRCL(hw->port),
923 pf->stat_offsets_loaded,
924 &osd->eth.rx_multicast,
925 &nsd->eth.rx_multicast);
532d283d
SN
926 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
927 I40E_GLPRT_BPRCL(hw->port),
928 pf->stat_offsets_loaded,
929 &osd->eth.rx_broadcast,
930 &nsd->eth.rx_broadcast);
931 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
932 I40E_GLPRT_UPTCL(hw->port),
933 pf->stat_offsets_loaded,
934 &osd->eth.tx_unicast,
935 &nsd->eth.tx_unicast);
936 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
937 I40E_GLPRT_MPTCL(hw->port),
938 pf->stat_offsets_loaded,
939 &osd->eth.tx_multicast,
940 &nsd->eth.tx_multicast);
941 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
942 I40E_GLPRT_BPTCL(hw->port),
943 pf->stat_offsets_loaded,
944 &osd->eth.tx_broadcast,
945 &nsd->eth.tx_broadcast);
41c445ff 946
7812fddc
SN
947 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
948 pf->stat_offsets_loaded,
949 &osd->tx_dropped_link_down,
950 &nsd->tx_dropped_link_down);
41c445ff 951
7812fddc
SN
952 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
953 pf->stat_offsets_loaded,
954 &osd->crc_errors, &nsd->crc_errors);
41c445ff 955
7812fddc
SN
956 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
957 pf->stat_offsets_loaded,
958 &osd->illegal_bytes, &nsd->illegal_bytes);
41c445ff 959
7812fddc
SN
960 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
961 pf->stat_offsets_loaded,
962 &osd->mac_local_faults,
963 &nsd->mac_local_faults);
964 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
965 pf->stat_offsets_loaded,
966 &osd->mac_remote_faults,
967 &nsd->mac_remote_faults);
41c445ff 968
7812fddc
SN
969 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
970 pf->stat_offsets_loaded,
971 &osd->rx_length_errors,
972 &nsd->rx_length_errors);
41c445ff 973
7812fddc
SN
974 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
975 pf->stat_offsets_loaded,
976 &osd->link_xon_rx, &nsd->link_xon_rx);
977 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
978 pf->stat_offsets_loaded,
979 &osd->link_xon_tx, &nsd->link_xon_tx);
95db239f
NP
980 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
981 pf->stat_offsets_loaded,
982 &osd->link_xoff_rx, &nsd->link_xoff_rx);
7812fddc
SN
983 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
984 pf->stat_offsets_loaded,
985 &osd->link_xoff_tx, &nsd->link_xoff_tx);
41c445ff 986
7812fddc 987 for (i = 0; i < 8; i++) {
95db239f
NP
988 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
989 pf->stat_offsets_loaded,
990 &osd->priority_xoff_rx[i],
991 &nsd->priority_xoff_rx[i]);
7812fddc 992 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
41c445ff 993 pf->stat_offsets_loaded,
7812fddc
SN
994 &osd->priority_xon_rx[i],
995 &nsd->priority_xon_rx[i]);
996 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
41c445ff 997 pf->stat_offsets_loaded,
7812fddc
SN
998 &osd->priority_xon_tx[i],
999 &nsd->priority_xon_tx[i]);
1000 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
41c445ff 1001 pf->stat_offsets_loaded,
7812fddc
SN
1002 &osd->priority_xoff_tx[i],
1003 &nsd->priority_xoff_tx[i]);
1004 i40e_stat_update32(hw,
1005 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
bee5af7e 1006 pf->stat_offsets_loaded,
7812fddc
SN
1007 &osd->priority_xon_2_xoff[i],
1008 &nsd->priority_xon_2_xoff[i]);
41c445ff
JB
1009 }
1010
7812fddc
SN
1011 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1012 I40E_GLPRT_PRC64L(hw->port),
1013 pf->stat_offsets_loaded,
1014 &osd->rx_size_64, &nsd->rx_size_64);
1015 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1016 I40E_GLPRT_PRC127L(hw->port),
1017 pf->stat_offsets_loaded,
1018 &osd->rx_size_127, &nsd->rx_size_127);
1019 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1020 I40E_GLPRT_PRC255L(hw->port),
1021 pf->stat_offsets_loaded,
1022 &osd->rx_size_255, &nsd->rx_size_255);
1023 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1024 I40E_GLPRT_PRC511L(hw->port),
1025 pf->stat_offsets_loaded,
1026 &osd->rx_size_511, &nsd->rx_size_511);
1027 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1028 I40E_GLPRT_PRC1023L(hw->port),
1029 pf->stat_offsets_loaded,
1030 &osd->rx_size_1023, &nsd->rx_size_1023);
1031 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1032 I40E_GLPRT_PRC1522L(hw->port),
1033 pf->stat_offsets_loaded,
1034 &osd->rx_size_1522, &nsd->rx_size_1522);
1035 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1036 I40E_GLPRT_PRC9522L(hw->port),
1037 pf->stat_offsets_loaded,
1038 &osd->rx_size_big, &nsd->rx_size_big);
1039
1040 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1041 I40E_GLPRT_PTC64L(hw->port),
1042 pf->stat_offsets_loaded,
1043 &osd->tx_size_64, &nsd->tx_size_64);
1044 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1045 I40E_GLPRT_PTC127L(hw->port),
1046 pf->stat_offsets_loaded,
1047 &osd->tx_size_127, &nsd->tx_size_127);
1048 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1049 I40E_GLPRT_PTC255L(hw->port),
1050 pf->stat_offsets_loaded,
1051 &osd->tx_size_255, &nsd->tx_size_255);
1052 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1053 I40E_GLPRT_PTC511L(hw->port),
1054 pf->stat_offsets_loaded,
1055 &osd->tx_size_511, &nsd->tx_size_511);
1056 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1057 I40E_GLPRT_PTC1023L(hw->port),
1058 pf->stat_offsets_loaded,
1059 &osd->tx_size_1023, &nsd->tx_size_1023);
1060 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1061 I40E_GLPRT_PTC1522L(hw->port),
1062 pf->stat_offsets_loaded,
1063 &osd->tx_size_1522, &nsd->tx_size_1522);
1064 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1065 I40E_GLPRT_PTC9522L(hw->port),
1066 pf->stat_offsets_loaded,
1067 &osd->tx_size_big, &nsd->tx_size_big);
1068
1069 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1070 pf->stat_offsets_loaded,
1071 &osd->rx_undersize, &nsd->rx_undersize);
1072 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1073 pf->stat_offsets_loaded,
1074 &osd->rx_fragments, &nsd->rx_fragments);
1075 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1076 pf->stat_offsets_loaded,
1077 &osd->rx_oversize, &nsd->rx_oversize);
1078 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1079 pf->stat_offsets_loaded,
1080 &osd->rx_jabber, &nsd->rx_jabber);
1081
433c47de 1082 /* FDIR stats */
0bf4b1b0
ASJ
1083 i40e_stat_update32(hw,
1084 I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
433c47de
ASJ
1085 pf->stat_offsets_loaded,
1086 &osd->fd_atr_match, &nsd->fd_atr_match);
0bf4b1b0
ASJ
1087 i40e_stat_update32(hw,
1088 I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
433c47de
ASJ
1089 pf->stat_offsets_loaded,
1090 &osd->fd_sb_match, &nsd->fd_sb_match);
60ccd45c
ASJ
1091 i40e_stat_update32(hw,
1092 I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
1093 pf->stat_offsets_loaded,
1094 &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
433c47de 1095
7812fddc
SN
1096 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1097 nsd->tx_lpi_status =
1098 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1099 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1100 nsd->rx_lpi_status =
1101 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1102 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1103 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1104 pf->stat_offsets_loaded,
1105 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1106 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1107 pf->stat_offsets_loaded,
1108 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1109
d0389e51
ASJ
1110 if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1111 !(pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED))
1112 nsd->fd_sb_status = true;
1113 else
1114 nsd->fd_sb_status = false;
1115
1116 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1117 !(pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
1118 nsd->fd_atr_status = true;
1119 else
1120 nsd->fd_atr_status = false;
1121
41c445ff
JB
1122 pf->stat_offsets_loaded = true;
1123}
1124
7812fddc
SN
1125/**
1126 * i40e_update_stats - Update the various statistics counters.
1127 * @vsi: the VSI to be updated
1128 *
1129 * Update the various stats for this VSI and its related entities.
1130 **/
1131void i40e_update_stats(struct i40e_vsi *vsi)
1132{
1133 struct i40e_pf *pf = vsi->back;
1134
1135 if (vsi == pf->vsi[pf->lan_vsi])
1136 i40e_update_pf_stats(pf);
1137
1138 i40e_update_vsi_stats(vsi);
38e00438
VD
1139#ifdef I40E_FCOE
1140 i40e_update_fcoe_stats(vsi);
1141#endif
7812fddc
SN
1142}
1143
41c445ff
JB
1144/**
1145 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1146 * @vsi: the VSI to be searched
1147 * @macaddr: the MAC address
1148 * @vlan: the vlan
41c445ff
JB
1149 *
1150 * Returns ptr to the filter object or NULL
1151 **/
1152static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
6622f5cd 1153 const u8 *macaddr, s16 vlan)
41c445ff
JB
1154{
1155 struct i40e_mac_filter *f;
278e7d0b 1156 u64 key;
41c445ff
JB
1157
1158 if (!vsi || !macaddr)
1159 return NULL;
1160
278e7d0b
JK
1161 key = i40e_addr_to_hkey(macaddr);
1162 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
41c445ff 1163 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1bc87e80 1164 (vlan == f->vlan))
41c445ff
JB
1165 return f;
1166 }
1167 return NULL;
1168}
1169
1170/**
1171 * i40e_find_mac - Find a mac addr in the macvlan filters list
1172 * @vsi: the VSI to be searched
1173 * @macaddr: the MAC address we are searching for
41c445ff
JB
1174 *
1175 * Returns the first filter with the provided MAC address or NULL if
1176 * MAC address was not found
1177 **/
6622f5cd 1178struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
41c445ff
JB
1179{
1180 struct i40e_mac_filter *f;
278e7d0b 1181 u64 key;
41c445ff
JB
1182
1183 if (!vsi || !macaddr)
1184 return NULL;
1185
278e7d0b
JK
1186 key = i40e_addr_to_hkey(macaddr);
1187 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1bc87e80 1188 if ((ether_addr_equal(macaddr, f->macaddr)))
41c445ff
JB
1189 return f;
1190 }
1191 return NULL;
1192}
1193
1194/**
1195 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1196 * @vsi: the VSI to be searched
1197 *
1198 * Returns true if VSI is in vlan mode or false otherwise
1199 **/
1200bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1201{
cbebb85f
JK
1202 /* If we have a PVID, always operate in VLAN mode */
1203 if (vsi->info.pvid)
1204 return true;
1205
1206 /* We need to operate in VLAN mode whenever we have any filters with
1207 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1208 * time, incurring search cost repeatedly. However, we can notice two
1209 * things:
1210 *
1211 * 1) the only place where we can gain a VLAN filter is in
1212 * i40e_add_filter.
1213 *
1214 * 2) the only place where filters are actually removed is in
0b7c8b5d 1215 * i40e_sync_filters_subtask.
cbebb85f
JK
1216 *
1217 * Thus, we can simply use a boolean value, has_vlan_filters which we
1218 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1219 * we have to perform the full search after deleting filters in
0b7c8b5d 1220 * i40e_sync_filters_subtask, but we already have to search
cbebb85f
JK
1221 * filters here and can perform the check at the same time. This
1222 * results in avoiding embedding a loop for VLAN mode inside another
1223 * loop over all the filters, and should maintain correctness as noted
1224 * above.
41c445ff 1225 */
cbebb85f 1226 return vsi->has_vlan_filter;
41c445ff
JB
1227}
1228
1596b5dd
JK
1229/**
1230 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1231 * @vsi: the PF Main VSI - inappropriate for any other VSI
1232 * @macaddr: the MAC address
1233 *
1234 * Remove whatever filter the firmware set up so the driver can manage
1235 * its own filtering intelligently.
1236 **/
1237static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1238{
1239 struct i40e_aqc_remove_macvlan_element_data element;
1240 struct i40e_pf *pf = vsi->back;
1241
1242 /* Only appropriate for the PF main VSI */
1243 if (vsi->type != I40E_VSI_MAIN)
1244 return;
1245
1246 memset(&element, 0, sizeof(element));
1247 ether_addr_copy(element.mac_addr, macaddr);
1248 element.vlan_tag = 0;
1249 /* Ignore error returns, some firmware does it this way... */
1250 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1251 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1252
1253 memset(&element, 0, sizeof(element));
1254 ether_addr_copy(element.mac_addr, macaddr);
1255 element.vlan_tag = 0;
1256 /* ...and some firmware does it this way. */
1257 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1258 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1259 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1260}
1261
41c445ff
JB
1262/**
1263 * i40e_add_filter - Add a mac/vlan filter to the VSI
1264 * @vsi: the VSI to be searched
1265 * @macaddr: the MAC address
1266 * @vlan: the vlan
41c445ff
JB
1267 *
1268 * Returns ptr to the filter object or NULL when no memory available.
21659035 1269 *
278e7d0b 1270 * NOTE: This function is expected to be called with mac_filter_hash_lock
21659035 1271 * being held.
41c445ff
JB
1272 **/
1273struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
6622f5cd 1274 const u8 *macaddr, s16 vlan)
41c445ff
JB
1275{
1276 struct i40e_mac_filter *f;
278e7d0b 1277 u64 key;
41c445ff
JB
1278
1279 if (!vsi || !macaddr)
1280 return NULL;
1281
1bc87e80 1282 f = i40e_find_filter(vsi, macaddr, vlan);
41c445ff
JB
1283 if (!f) {
1284 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1285 if (!f)
1bc87e80 1286 return NULL;
41c445ff 1287
cbebb85f
JK
1288 /* Update the boolean indicating if we need to function in
1289 * VLAN mode.
1290 */
1291 if (vlan >= 0)
1292 vsi->has_vlan_filter = true;
1293
9a173901 1294 ether_addr_copy(f->macaddr, macaddr);
41c445ff 1295 f->vlan = vlan;
c3c7ea27
MW
1296 /* If we're in overflow promisc mode, set the state directly
1297 * to failed, so we don't bother to try sending the filter
1298 * to the hardware.
1299 */
1300 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))
1301 f->state = I40E_FILTER_FAILED;
1302 else
1303 f->state = I40E_FILTER_NEW;
278e7d0b
JK
1304 INIT_HLIST_NODE(&f->hlist);
1305
1306 key = i40e_addr_to_hkey(macaddr);
1307 hash_add(vsi->mac_filter_hash, &f->hlist, key);
41c445ff 1308
41c445ff
JB
1309 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1310 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1311 }
1312
1bc87e80
JK
1313 /* If we're asked to add a filter that has been marked for removal, it
1314 * is safe to simply restore it to active state. __i40e_del_filter
1315 * will have simply deleted any filters which were previously marked
1316 * NEW or FAILED, so if it is currently marked REMOVE it must have
1317 * previously been ACTIVE. Since we haven't yet run the sync filters
1318 * task, just restore this filter to the ACTIVE state so that the
1319 * sync task leaves it in place
1320 */
1321 if (f->state == I40E_FILTER_REMOVE)
1322 f->state = I40E_FILTER_ACTIVE;
1323
41c445ff
JB
1324 return f;
1325}
1326
1327/**
290d2557
JK
1328 * __i40e_del_filter - Remove a specific filter from the VSI
1329 * @vsi: VSI to remove from
1330 * @f: the filter to remove from the list
1331 *
1332 * This function should be called instead of i40e_del_filter only if you know
1333 * the exact filter you will remove already, such as via i40e_find_filter or
1334 * i40e_find_mac.
21659035 1335 *
278e7d0b 1336 * NOTE: This function is expected to be called with mac_filter_hash_lock
21659035 1337 * being held.
c3c7ea27
MW
1338 * ANOTHER NOTE: This function MUST be called from within the context of
1339 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1340 * instead of list_for_each_entry().
41c445ff 1341 **/
290d2557 1342static void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
41c445ff 1343{
1bc87e80 1344 if (!f)
41c445ff
JB
1345 return;
1346
1bc87e80
JK
1347 if ((f->state == I40E_FILTER_FAILED) ||
1348 (f->state == I40E_FILTER_NEW)) {
1349 /* this one never got added by the FW. Just remove it,
1350 * no need to sync anything.
1351 */
278e7d0b 1352 hash_del(&f->hlist);
1bc87e80 1353 kfree(f);
41c445ff 1354 } else {
1bc87e80
JK
1355 f->state = I40E_FILTER_REMOVE;
1356 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1357 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
41c445ff
JB
1358 }
1359}
1360
290d2557
JK
1361/**
1362 * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1363 * @vsi: the VSI to be searched
1364 * @macaddr: the MAC address
1365 * @vlan: the VLAN
1366 *
278e7d0b 1367 * NOTE: This function is expected to be called with mac_filter_hash_lock
290d2557
JK
1368 * being held.
1369 * ANOTHER NOTE: This function MUST be called from within the context of
1370 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1371 * instead of list_for_each_entry().
1372 **/
1373void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1374{
1375 struct i40e_mac_filter *f;
1376
1377 if (!vsi || !macaddr)
1378 return;
1379
1380 f = i40e_find_filter(vsi, macaddr, vlan);
1381 __i40e_del_filter(vsi, f);
1382}
1383
35ec2ff3
JK
1384/**
1385 * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
1386 * @vsi: the VSI to be searched
1387 * @macaddr: the mac address to be filtered
1388 *
5feb3d7b
JK
1389 * Goes through all the macvlan filters and adds a macvlan filter for each
1390 * unique vlan that already exists. If a PVID has been assigned, instead only
1391 * add the macaddr to that VLAN.
35ec2ff3 1392 *
5feb3d7b 1393 * Returns last filter added on success, else NULL
35ec2ff3
JK
1394 **/
1395struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi,
1396 const u8 *macaddr)
1397{
5feb3d7b 1398 struct i40e_mac_filter *f, *add = NULL;
278e7d0b
JK
1399 struct hlist_node *h;
1400 int bkt;
5feb3d7b
JK
1401
1402 if (vsi->info.pvid)
1403 return i40e_add_filter(vsi, macaddr,
1404 le16_to_cpu(vsi->info.pvid));
35ec2ff3 1405
278e7d0b 1406 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
57b341d6
JK
1407 if (f->state == I40E_FILTER_REMOVE)
1408 continue;
5feb3d7b
JK
1409 add = i40e_add_filter(vsi, macaddr, f->vlan);
1410 if (!add)
1411 return NULL;
35ec2ff3
JK
1412 }
1413
5feb3d7b 1414 return add;
35ec2ff3
JK
1415}
1416
1417/**
1418 * i40e_del_mac_all_vlan - Remove a MAC filter from all VLANS
1419 * @vsi: the VSI to be searched
1420 * @macaddr: the mac address to be removed
1421 *
1422 * Removes a given MAC address from a VSI, regardless of VLAN
1423 *
1424 * Returns 0 for success, or error
1425 **/
1426int i40e_del_mac_all_vlan(struct i40e_vsi *vsi, const u8 *macaddr)
1427{
278e7d0b
JK
1428 struct i40e_mac_filter *f;
1429 struct hlist_node *h;
290d2557 1430 bool found = false;
278e7d0b 1431 int bkt;
35ec2ff3 1432
278e7d0b
JK
1433 WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
1434 "Missing mac_filter_hash_lock\n");
1435 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
290d2557
JK
1436 if (ether_addr_equal(macaddr, f->macaddr)) {
1437 __i40e_del_filter(vsi, f);
1438 found = true;
1439 }
35ec2ff3 1440 }
290d2557
JK
1441
1442 if (found)
35ec2ff3 1443 return 0;
290d2557
JK
1444 else
1445 return -ENOENT;
35ec2ff3
JK
1446}
1447
41c445ff
JB
1448/**
1449 * i40e_set_mac - NDO callback to set mac address
1450 * @netdev: network interface device structure
1451 * @p: pointer to an address structure
1452 *
1453 * Returns 0 on success, negative on failure
1454 **/
38e00438
VD
1455#ifdef I40E_FCOE
1456int i40e_set_mac(struct net_device *netdev, void *p)
1457#else
41c445ff 1458static int i40e_set_mac(struct net_device *netdev, void *p)
38e00438 1459#endif
41c445ff
JB
1460{
1461 struct i40e_netdev_priv *np = netdev_priv(netdev);
1462 struct i40e_vsi *vsi = np->vsi;
30650cc5
SN
1463 struct i40e_pf *pf = vsi->back;
1464 struct i40e_hw *hw = &pf->hw;
41c445ff 1465 struct sockaddr *addr = p;
41c445ff
JB
1466
1467 if (!is_valid_ether_addr(addr->sa_data))
1468 return -EADDRNOTAVAIL;
1469
30650cc5
SN
1470 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1471 netdev_info(netdev, "already using mac address %pM\n",
1472 addr->sa_data);
1473 return 0;
1474 }
41c445ff 1475
80f6428f
ASJ
1476 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1477 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1478 return -EADDRNOTAVAIL;
1479
30650cc5
SN
1480 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1481 netdev_info(netdev, "returning to hw mac address %pM\n",
1482 hw->mac.addr);
1483 else
1484 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1485
278e7d0b 1486 spin_lock_bh(&vsi->mac_filter_hash_lock);
1bc87e80
JK
1487 i40e_del_mac_all_vlan(vsi, netdev->dev_addr);
1488 i40e_put_mac_in_vlan(vsi, addr->sa_data);
278e7d0b 1489 spin_unlock_bh(&vsi->mac_filter_hash_lock);
c3c7ea27 1490 ether_addr_copy(netdev->dev_addr, addr->sa_data);
41c445ff
JB
1491 if (vsi->type == I40E_VSI_MAIN) {
1492 i40e_status ret;
6995b36c 1493
41c445ff 1494 ret = i40e_aq_mac_address_write(&vsi->back->hw,
cc41222c 1495 I40E_AQC_WRITE_TYPE_LAA_WOL,
41c445ff 1496 addr->sa_data, NULL);
c3c7ea27
MW
1497 if (ret)
1498 netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1499 i40e_stat_str(hw, ret),
1500 i40e_aq_str(hw, hw->aq.asq_last_status));
30650cc5
SN
1501 }
1502
c53934c6
JB
1503 /* schedule our worker thread which will take care of
1504 * applying the new filter changes
1505 */
1506 i40e_service_event_schedule(vsi->back);
1507 return 0;
41c445ff
JB
1508}
1509
1510/**
1511 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1512 * @vsi: the VSI being setup
1513 * @ctxt: VSI context structure
1514 * @enabled_tc: Enabled TCs bitmap
1515 * @is_add: True if called before Add VSI
1516 *
1517 * Setup VSI queue mapping for enabled traffic classes.
1518 **/
38e00438
VD
1519#ifdef I40E_FCOE
1520void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1521 struct i40e_vsi_context *ctxt,
1522 u8 enabled_tc,
1523 bool is_add)
1524#else
41c445ff
JB
1525static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1526 struct i40e_vsi_context *ctxt,
1527 u8 enabled_tc,
1528 bool is_add)
38e00438 1529#endif
41c445ff
JB
1530{
1531 struct i40e_pf *pf = vsi->back;
1532 u16 sections = 0;
1533 u8 netdev_tc = 0;
1534 u16 numtc = 0;
1535 u16 qcount;
1536 u8 offset;
1537 u16 qmap;
1538 int i;
4e3b35b0 1539 u16 num_tc_qps = 0;
41c445ff
JB
1540
1541 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1542 offset = 0;
1543
1544 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1545 /* Find numtc from enabled TC bitmap */
1546 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
75f5cea9 1547 if (enabled_tc & BIT(i)) /* TC is enabled */
41c445ff
JB
1548 numtc++;
1549 }
1550 if (!numtc) {
1551 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1552 numtc = 1;
1553 }
1554 } else {
1555 /* At least TC0 is enabled in case of non-DCB case */
1556 numtc = 1;
1557 }
1558
1559 vsi->tc_config.numtc = numtc;
1560 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
4e3b35b0 1561 /* Number of queues per enabled TC */
7d64402f
CS
1562 qcount = vsi->alloc_queue_pairs;
1563
7f9ff476 1564 num_tc_qps = qcount / numtc;
e25d00b8 1565 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
41c445ff
JB
1566
1567 /* Setup queue offset/count for all TCs for given VSI */
1568 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1569 /* See if the given TC is enabled for the given VSI */
75f5cea9 1570 if (vsi->tc_config.enabled_tc & BIT(i)) {
41a1d04b 1571 /* TC is enabled */
41c445ff
JB
1572 int pow, num_qps;
1573
41c445ff
JB
1574 switch (vsi->type) {
1575 case I40E_VSI_MAIN:
acd65448
HZ
1576 qcount = min_t(int, pf->alloc_rss_size,
1577 num_tc_qps);
41c445ff 1578 break;
38e00438
VD
1579#ifdef I40E_FCOE
1580 case I40E_VSI_FCOE:
1581 qcount = num_tc_qps;
1582 break;
1583#endif
41c445ff
JB
1584 case I40E_VSI_FDIR:
1585 case I40E_VSI_SRIOV:
1586 case I40E_VSI_VMDQ2:
1587 default:
4e3b35b0 1588 qcount = num_tc_qps;
41c445ff
JB
1589 WARN_ON(i != 0);
1590 break;
1591 }
4e3b35b0
NP
1592 vsi->tc_config.tc_info[i].qoffset = offset;
1593 vsi->tc_config.tc_info[i].qcount = qcount;
41c445ff 1594
1e200e4a 1595 /* find the next higher power-of-2 of num queue pairs */
4e3b35b0 1596 num_qps = qcount;
41c445ff 1597 pow = 0;
41a1d04b 1598 while (num_qps && (BIT_ULL(pow) < qcount)) {
41c445ff
JB
1599 pow++;
1600 num_qps >>= 1;
1601 }
1602
1603 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1604 qmap =
1605 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1606 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1607
4e3b35b0 1608 offset += qcount;
41c445ff
JB
1609 } else {
1610 /* TC is not enabled so set the offset to
1611 * default queue and allocate one queue
1612 * for the given TC.
1613 */
1614 vsi->tc_config.tc_info[i].qoffset = 0;
1615 vsi->tc_config.tc_info[i].qcount = 1;
1616 vsi->tc_config.tc_info[i].netdev_tc = 0;
1617
1618 qmap = 0;
1619 }
1620 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1621 }
1622
1623 /* Set actual Tx/Rx queue pairs */
1624 vsi->num_queue_pairs = offset;
9a3bd2f1
ASJ
1625 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1626 if (vsi->req_queue_pairs > 0)
1627 vsi->num_queue_pairs = vsi->req_queue_pairs;
26cdc443 1628 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
9a3bd2f1
ASJ
1629 vsi->num_queue_pairs = pf->num_lan_msix;
1630 }
41c445ff
JB
1631
1632 /* Scheduler section valid can only be set for ADD VSI */
1633 if (is_add) {
1634 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1635
1636 ctxt->info.up_enable_bits = enabled_tc;
1637 }
1638 if (vsi->type == I40E_VSI_SRIOV) {
1639 ctxt->info.mapping_flags |=
1640 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1641 for (i = 0; i < vsi->num_queue_pairs; i++)
1642 ctxt->info.queue_mapping[i] =
1643 cpu_to_le16(vsi->base_queue + i);
1644 } else {
1645 ctxt->info.mapping_flags |=
1646 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1647 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1648 }
1649 ctxt->info.valid_sections |= cpu_to_le16(sections);
1650}
1651
6622f5cd
JK
1652/**
1653 * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1654 * @netdev: the netdevice
1655 * @addr: address to add
1656 *
1657 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1658 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1659 */
1660static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1661{
1662 struct i40e_netdev_priv *np = netdev_priv(netdev);
1663 struct i40e_vsi *vsi = np->vsi;
1664 struct i40e_mac_filter *f;
1665
1666 if (i40e_is_vsi_in_vlan(vsi))
1667 f = i40e_put_mac_in_vlan(vsi, addr);
1668 else
1669 f = i40e_add_filter(vsi, addr, I40E_VLAN_ANY);
1670
1671 if (f)
1672 return 0;
1673 else
1674 return -ENOMEM;
1675}
1676
1677/**
1678 * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1679 * @netdev: the netdevice
1680 * @addr: address to add
1681 *
1682 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1683 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1684 */
1685static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1686{
1687 struct i40e_netdev_priv *np = netdev_priv(netdev);
1688 struct i40e_vsi *vsi = np->vsi;
1689
1690 if (i40e_is_vsi_in_vlan(vsi))
1691 i40e_del_mac_all_vlan(vsi, addr);
1692 else
1693 i40e_del_filter(vsi, addr, I40E_VLAN_ANY);
1694
1695 return 0;
1696}
1697
41c445ff
JB
1698/**
1699 * i40e_set_rx_mode - NDO callback to set the netdev filters
1700 * @netdev: network interface device structure
1701 **/
38e00438
VD
1702#ifdef I40E_FCOE
1703void i40e_set_rx_mode(struct net_device *netdev)
1704#else
41c445ff 1705static void i40e_set_rx_mode(struct net_device *netdev)
38e00438 1706#endif
41c445ff
JB
1707{
1708 struct i40e_netdev_priv *np = netdev_priv(netdev);
41c445ff 1709 struct i40e_vsi *vsi = np->vsi;
41c445ff 1710
278e7d0b 1711 spin_lock_bh(&vsi->mac_filter_hash_lock);
21659035 1712
6622f5cd
JK
1713 __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1714 __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
41c445ff 1715
278e7d0b 1716 spin_unlock_bh(&vsi->mac_filter_hash_lock);
41c445ff
JB
1717
1718 /* check for other flag changes */
1719 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1720 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1721 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1722 }
c53934c6
JB
1723
1724 /* schedule our worker thread which will take care of
1725 * applying the new filter changes
1726 */
1727 i40e_service_event_schedule(vsi->back);
41c445ff
JB
1728}
1729
21659035 1730/**
4a2ce27b
JK
1731 * i40e_undo_filter_entries - Undo the changes made to MAC filter entries
1732 * @vsi: Pointer to VSI struct
21659035
KP
1733 * @from: Pointer to list which contains MAC filter entries - changes to
1734 * those entries needs to be undone.
1735 *
4a2ce27b
JK
1736 * MAC filter entries from list were slated to be sent to firmware, either for
1737 * addition or deletion.
21659035 1738 **/
4a2ce27b
JK
1739static void i40e_undo_filter_entries(struct i40e_vsi *vsi,
1740 struct hlist_head *from)
21659035 1741{
278e7d0b
JK
1742 struct i40e_mac_filter *f;
1743 struct hlist_node *h;
1744
1745 hlist_for_each_entry_safe(f, h, from, hlist) {
1746 u64 key = i40e_addr_to_hkey(f->macaddr);
21659035 1747
21659035 1748 /* Move the element back into MAC filter list*/
278e7d0b
JK
1749 hlist_del(&f->hlist);
1750 hash_add(vsi->mac_filter_hash, &f->hlist, key);
21659035
KP
1751 }
1752}
1753
1754/**
c3c7ea27
MW
1755 * i40e_update_filter_state - Update filter state based on return data
1756 * from firmware
1757 * @count: Number of filters added
1758 * @add_list: return data from fw
1759 * @head: pointer to first filter in current batch
1760 * @aq_err: status from fw
21659035 1761 *
c3c7ea27
MW
1762 * MAC filter entries from list were slated to be added to device. Returns
1763 * number of successful filters. Note that 0 does NOT mean success!
21659035 1764 **/
c3c7ea27
MW
1765static int
1766i40e_update_filter_state(int count,
1767 struct i40e_aqc_add_macvlan_element_data *add_list,
1768 struct i40e_mac_filter *add_head, int aq_err)
21659035 1769{
c3c7ea27
MW
1770 int retval = 0;
1771 int i;
21659035 1772
21659035 1773
c3c7ea27
MW
1774 if (!aq_err) {
1775 retval = count;
1776 /* Everything's good, mark all filters active. */
1777 for (i = 0; i < count ; i++) {
1778 add_head->state = I40E_FILTER_ACTIVE;
278e7d0b
JK
1779 add_head = hlist_entry(add_head->hlist.next,
1780 typeof(struct i40e_mac_filter),
1781 hlist);
c3c7ea27
MW
1782 }
1783 } else if (aq_err == I40E_AQ_RC_ENOSPC) {
1784 /* Device ran out of filter space. Check the return value
1785 * for each filter to see which ones are active.
1786 */
1787 for (i = 0; i < count ; i++) {
1788 if (add_list[i].match_method ==
1789 I40E_AQC_MM_ERR_NO_RES) {
1790 add_head->state = I40E_FILTER_FAILED;
1791 } else {
1792 add_head->state = I40E_FILTER_ACTIVE;
1793 retval++;
1794 }
278e7d0b
JK
1795 add_head = hlist_entry(add_head->hlist.next,
1796 typeof(struct i40e_mac_filter),
1797 hlist);
c3c7ea27
MW
1798 }
1799 } else {
1800 /* Some other horrible thing happened, fail all filters */
1801 retval = 0;
1802 for (i = 0; i < count ; i++) {
1803 add_head->state = I40E_FILTER_FAILED;
278e7d0b
JK
1804 add_head = hlist_entry(add_head->hlist.next,
1805 typeof(struct i40e_mac_filter),
1806 hlist);
c3c7ea27 1807 }
21659035 1808 }
c3c7ea27 1809 return retval;
21659035
KP
1810}
1811
00936319
JK
1812/**
1813 * i40e_aqc_del_filters - Request firmware to delete a set of filters
1814 * @vsi: ptr to the VSI
1815 * @vsi_name: name to display in messages
1816 * @list: the list of filters to send to firmware
1817 * @num_del: the number of filters to delete
1818 * @retval: Set to -EIO on failure to delete
1819 *
1820 * Send a request to firmware via AdminQ to delete a set of filters. Uses
1821 * *retval instead of a return value so that success does not force ret_val to
1822 * be set to 0. This ensures that a sequence of calls to this function
1823 * preserve the previous value of *retval on successful delete.
1824 */
1825static
1826void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
1827 struct i40e_aqc_remove_macvlan_element_data *list,
1828 int num_del, int *retval)
1829{
1830 struct i40e_hw *hw = &vsi->back->hw;
1831 i40e_status aq_ret;
1832 int aq_err;
1833
1834 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
1835 aq_err = hw->aq.asq_last_status;
1836
1837 /* Explicitly ignore and do not report when firmware returns ENOENT */
1838 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
1839 *retval = -EIO;
1840 dev_info(&vsi->back->pdev->dev,
1841 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
1842 vsi_name, i40e_stat_str(hw, aq_ret),
1843 i40e_aq_str(hw, aq_err));
1844 }
1845}
1846
1847/**
1848 * i40e_aqc_add_filters - Request firmware to add a set of filters
1849 * @vsi: ptr to the VSI
1850 * @vsi_name: name to display in messages
1851 * @list: the list of filters to send to firmware
1852 * @add_head: Position in the add hlist
1853 * @num_add: the number of filters to add
1854 * @promisc_change: set to true on exit if promiscuous mode was forced on
1855 *
1856 * Send a request to firmware via AdminQ to add a chunk of filters. Will set
1857 * promisc_changed to true if the firmware has run out of space for more
1858 * filters.
1859 */
1860static
1861void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
1862 struct i40e_aqc_add_macvlan_element_data *list,
1863 struct i40e_mac_filter *add_head,
1864 int num_add, bool *promisc_changed)
1865{
1866 struct i40e_hw *hw = &vsi->back->hw;
1867 i40e_status aq_ret;
1868 int aq_err, fcnt;
1869
1870 aq_ret = i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
1871 aq_err = hw->aq.asq_last_status;
1872 fcnt = i40e_update_filter_state(num_add, list, add_head, aq_ret);
1873 vsi->active_filters += fcnt;
1874
1875 if (fcnt != num_add) {
1876 *promisc_changed = true;
1877 set_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
1878 vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
1879 dev_warn(&vsi->back->pdev->dev,
1880 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
1881 i40e_aq_str(hw, aq_err),
1882 vsi_name);
1883 }
1884}
1885
435c084a
JK
1886/**
1887 * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
1888 * @vsi: pointer to the VSI
1889 * @f: filter data
1890 *
1891 * This function sets or clears the promiscuous broadcast flags for VLAN
1892 * filters in order to properly receive broadcast frames. Assumes that only
1893 * broadcast filters are passed.
1894 **/
1895static
1896void i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
1897 struct i40e_mac_filter *f)
1898{
1899 bool enable = f->state == I40E_FILTER_NEW;
1900 struct i40e_hw *hw = &vsi->back->hw;
1901 i40e_status aq_ret;
1902
1903 if (f->vlan == I40E_VLAN_ANY) {
1904 aq_ret = i40e_aq_set_vsi_broadcast(hw,
1905 vsi->seid,
1906 enable,
1907 NULL);
1908 } else {
1909 aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
1910 vsi->seid,
1911 enable,
1912 f->vlan,
1913 NULL);
1914 }
1915
1916 if (aq_ret) {
1917 dev_warn(&vsi->back->pdev->dev,
1918 "Error %s setting broadcast promiscuous mode on %s\n",
1919 i40e_aq_str(hw, hw->aq.asq_last_status),
1920 vsi_name);
1921 f->state = I40E_FILTER_FAILED;
1922 } else if (enable) {
1923 f->state = I40E_FILTER_ACTIVE;
1924 }
1925}
1926
41c445ff
JB
1927/**
1928 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1929 * @vsi: ptr to the VSI
1930 *
1931 * Push any outstanding VSI filter changes through the AdminQ.
1932 *
1933 * Returns 0 or error value
1934 **/
17652c63 1935int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
41c445ff 1936{
278e7d0b 1937 struct hlist_head tmp_add_list, tmp_del_list;
84f5ca6c 1938 struct i40e_mac_filter *f, *add_head = NULL;
3e25a8f3 1939 struct i40e_hw *hw = &vsi->back->hw;
84f5ca6c
AB
1940 unsigned int vlan_any_filters = 0;
1941 unsigned int non_vlan_filters = 0;
1942 unsigned int vlan_filters = 0;
c3c7ea27 1943 bool promisc_changed = false;
2d1de828 1944 char vsi_name[16] = "PF";
41c445ff 1945 int filter_list_len = 0;
ea02e90b 1946 i40e_status aq_ret = 0;
84f5ca6c 1947 u32 changed_flags = 0;
278e7d0b 1948 struct hlist_node *h;
41c445ff
JB
1949 struct i40e_pf *pf;
1950 int num_add = 0;
1951 int num_del = 0;
84f5ca6c 1952 int retval = 0;
41c445ff 1953 u16 cmd_flags;
c3c7ea27 1954 int list_size;
278e7d0b 1955 int bkt;
41c445ff
JB
1956
1957 /* empty array typed pointers, kcalloc later */
1958 struct i40e_aqc_add_macvlan_element_data *add_list;
1959 struct i40e_aqc_remove_macvlan_element_data *del_list;
1960
1961 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1962 usleep_range(1000, 2000);
1963 pf = vsi->back;
1964
1965 if (vsi->netdev) {
1966 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1967 vsi->current_netdev_flags = vsi->netdev->flags;
1968 }
1969
278e7d0b
JK
1970 INIT_HLIST_HEAD(&tmp_add_list);
1971 INIT_HLIST_HEAD(&tmp_del_list);
21659035 1972
2d1de828
SN
1973 if (vsi->type == I40E_VSI_SRIOV)
1974 snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
1975 else if (vsi->type != I40E_VSI_MAIN)
1976 snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
1977
41c445ff
JB
1978 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1979 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1980
278e7d0b 1981 spin_lock_bh(&vsi->mac_filter_hash_lock);
c3c7ea27 1982 /* Create a list of filters to delete. */
278e7d0b 1983 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
c3c7ea27 1984 if (f->state == I40E_FILTER_REMOVE) {
c3c7ea27 1985 /* Move the element into temporary del_list */
278e7d0b
JK
1986 hash_del(&f->hlist);
1987 hlist_add_head(&f->hlist, &tmp_del_list);
c3c7ea27 1988 vsi->active_filters--;
84f5ca6c
AB
1989
1990 /* Avoid counting removed filters */
1991 continue;
c3c7ea27
MW
1992 }
1993 if (f->state == I40E_FILTER_NEW) {
278e7d0b
JK
1994 hash_del(&f->hlist);
1995 hlist_add_head(&f->hlist, &tmp_add_list);
21659035 1996 }
84f5ca6c
AB
1997
1998 /* Count the number of each type of filter we have
1999 * remaining, ignoring any filters we're about to
2000 * delete.
2001 */
2002 if (f->vlan > 0)
2003 vlan_filters++;
2004 else if (!f->vlan)
2005 non_vlan_filters++;
2006 else
2007 vlan_any_filters++;
2008 }
2009
2010 /* We should never have VLAN=-1 filters at the same time as we
2011 * have either VLAN=0 or VLAN>0 filters, so warn about this
2012 * case here to help catch any issues.
2013 */
2014 WARN_ON(vlan_any_filters && (vlan_filters + non_vlan_filters));
2015
2016 /* If we only have VLAN=0 filters remaining, and don't have
2017 * any other VLAN filters, we need to convert these VLAN=0
2018 * filters into VLAN=-1 (I40E_VLAN_ANY) so that we operate
2019 * correctly in non-VLAN mode and receive all traffic tagged
2020 * or untagged.
2021 */
2022 if (non_vlan_filters && !vlan_filters) {
2023 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f,
2024 hlist) {
2025 /* Only replace VLAN=0 filters */
2026 if (f->vlan)
2027 continue;
2028
2029 /* Allocate a replacement element */
2030 add_head = kzalloc(sizeof(*add_head),
2031 GFP_KERNEL);
2032 if (!add_head)
2033 goto err_no_memory_locked;
2034
2035 /* Copy the filter, with new state and VLAN */
2036 *add_head = *f;
2037 add_head->state = I40E_FILTER_NEW;
2038 add_head->vlan = I40E_VLAN_ANY;
2039
2040 /* Move the replacement to the add list */
2041 INIT_HLIST_NODE(&add_head->hlist);
2042 hlist_add_head(&add_head->hlist,
2043 &tmp_add_list);
2044
2045 /* Move the original to the delete list */
2046 f->state = I40E_FILTER_REMOVE;
2047 hash_del(&f->hlist);
2048 hlist_add_head(&f->hlist, &tmp_del_list);
2049 vsi->active_filters--;
2050 }
2051
2052 /* Also update any filters on the tmp_add list */
2053 hlist_for_each_entry(f, &tmp_add_list, hlist) {
2054 if (!f->vlan)
2055 f->vlan = I40E_VLAN_ANY;
2056 }
2057 add_head = NULL;
21659035 2058 }
278e7d0b 2059 spin_unlock_bh(&vsi->mac_filter_hash_lock);
21659035
KP
2060 }
2061
2062 /* Now process 'del_list' outside the lock */
278e7d0b 2063 if (!hlist_empty(&tmp_del_list)) {
3e25a8f3 2064 filter_list_len = hw->aq.asq_buf_size /
21659035 2065 sizeof(struct i40e_aqc_remove_macvlan_element_data);
c3c7ea27 2066 list_size = filter_list_len *
f1199998 2067 sizeof(struct i40e_aqc_remove_macvlan_element_data);
c3c7ea27 2068 del_list = kzalloc(list_size, GFP_ATOMIC);
4a2ce27b
JK
2069 if (!del_list)
2070 goto err_no_memory;
21659035 2071
278e7d0b 2072 hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
41c445ff
JB
2073 cmd_flags = 0;
2074
435c084a
JK
2075 /* handle broadcast filters by updating the broadcast
2076 * promiscuous flag instead of deleting a MAC filter.
2077 */
2078 if (is_broadcast_ether_addr(f->macaddr)) {
2079 i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2080
2081 hlist_del(&f->hlist);
2082 kfree(f);
2083 continue;
2084 }
2085
41c445ff 2086 /* add to delete list */
9a173901 2087 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
c3c7ea27
MW
2088 if (f->vlan == I40E_VLAN_ANY) {
2089 del_list[num_del].vlan_tag = 0;
a6cb9146 2090 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
c3c7ea27
MW
2091 } else {
2092 del_list[num_del].vlan_tag =
2093 cpu_to_le16((u16)(f->vlan));
2094 }
41c445ff 2095
41c445ff
JB
2096 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2097 del_list[num_del].flags = cmd_flags;
2098 num_del++;
2099
41c445ff
JB
2100 /* flush a full buffer */
2101 if (num_del == filter_list_len) {
00936319
JK
2102 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2103 num_del, &retval);
c3c7ea27 2104 memset(del_list, 0, list_size);
00936319 2105 num_del = 0;
41c445ff 2106 }
21659035
KP
2107 /* Release memory for MAC filter entries which were
2108 * synced up with HW.
2109 */
278e7d0b 2110 hlist_del(&f->hlist);
21659035 2111 kfree(f);
41c445ff 2112 }
21659035 2113
41c445ff 2114 if (num_del) {
00936319
JK
2115 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2116 num_del, &retval);
41c445ff
JB
2117 }
2118
2119 kfree(del_list);
2120 del_list = NULL;
21659035
KP
2121 }
2122
cbebb85f
JK
2123 /* After finishing notifying firmware of the deleted filters, update
2124 * the cached value of vsi->has_vlan_filter. Note that we are safe to
2125 * use just !!vlan_filters here because if we only have VLAN=0 (that
2126 * is, non_vlan_filters) these will all be converted to VLAN=-1 in the
2127 * logic above already so this value would still be correct.
2128 */
2129 vsi->has_vlan_filter = !!vlan_filters;
2130
278e7d0b 2131 if (!hlist_empty(&tmp_add_list)) {
c3c7ea27 2132 /* Do all the adds now. */
3e25a8f3 2133 filter_list_len = hw->aq.asq_buf_size /
f1199998 2134 sizeof(struct i40e_aqc_add_macvlan_element_data);
c3c7ea27
MW
2135 list_size = filter_list_len *
2136 sizeof(struct i40e_aqc_add_macvlan_element_data);
2137 add_list = kzalloc(list_size, GFP_ATOMIC);
4a2ce27b
JK
2138 if (!add_list)
2139 goto err_no_memory;
2140
c3c7ea27 2141 num_add = 0;
435c084a 2142 hlist_for_each_entry_safe(f, h, &tmp_add_list, hlist) {
c3c7ea27
MW
2143 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2144 &vsi->state)) {
2145 f->state = I40E_FILTER_FAILED;
2146 continue;
2147 }
435c084a
JK
2148
2149 /* handle broadcast filters by updating the broadcast
2150 * promiscuous flag instead of adding a MAC filter.
2151 */
2152 if (is_broadcast_ether_addr(f->macaddr)) {
2153 u64 key = i40e_addr_to_hkey(f->macaddr);
2154 i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2155
2156 hlist_del(&f->hlist);
2157 hash_add(vsi->mac_filter_hash, &f->hlist, key);
2158 continue;
2159 }
2160
41c445ff 2161 /* add to add array */
c3c7ea27
MW
2162 if (num_add == 0)
2163 add_head = f;
2164 cmd_flags = 0;
9a173901 2165 ether_addr_copy(add_list[num_add].mac_addr, f->macaddr);
c3c7ea27
MW
2166 if (f->vlan == I40E_VLAN_ANY) {
2167 add_list[num_add].vlan_tag = 0;
2168 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2169 } else {
2170 add_list[num_add].vlan_tag =
2171 cpu_to_le16((u16)(f->vlan));
2172 }
41c445ff 2173 add_list[num_add].queue_number = 0;
41c445ff 2174 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
41c445ff
JB
2175 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2176 num_add++;
2177
2178 /* flush a full buffer */
2179 if (num_add == filter_list_len) {
00936319
JK
2180 i40e_aqc_add_filters(vsi, vsi_name, add_list,
2181 add_head, num_add,
2182 &promisc_changed);
c3c7ea27 2183 memset(add_list, 0, list_size);
41c445ff 2184 num_add = 0;
41c445ff
JB
2185 }
2186 }
2187 if (num_add) {
00936319
JK
2188 i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2189 num_add, &promisc_changed);
41c445ff 2190 }
c3c7ea27
MW
2191 /* Now move all of the filters from the temp add list back to
2192 * the VSI's list.
2193 */
278e7d0b
JK
2194 spin_lock_bh(&vsi->mac_filter_hash_lock);
2195 hlist_for_each_entry_safe(f, h, &tmp_add_list, hlist) {
2196 u64 key = i40e_addr_to_hkey(f->macaddr);
2197
2198 hlist_del(&f->hlist);
2199 hash_add(vsi->mac_filter_hash, &f->hlist, key);
c3c7ea27 2200 }
278e7d0b 2201 spin_unlock_bh(&vsi->mac_filter_hash_lock);
41c445ff
JB
2202 kfree(add_list);
2203 add_list = NULL;
c3c7ea27 2204 }
41c445ff 2205
c3c7ea27
MW
2206 /* Check to see if we can drop out of overflow promiscuous mode. */
2207 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state) &&
2208 (vsi->active_filters < vsi->promisc_threshold)) {
2209 int failed_count = 0;
2210 /* See if we have any failed filters. We can't drop out of
2211 * promiscuous until these have all been deleted.
2212 */
278e7d0b
JK
2213 spin_lock_bh(&vsi->mac_filter_hash_lock);
2214 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
c3c7ea27
MW
2215 if (f->state == I40E_FILTER_FAILED)
2216 failed_count++;
2217 }
278e7d0b 2218 spin_unlock_bh(&vsi->mac_filter_hash_lock);
c3c7ea27 2219 if (!failed_count) {
41c445ff 2220 dev_info(&pf->pdev->dev,
c3c7ea27
MW
2221 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2222 vsi_name);
2223 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2224 promisc_changed = true;
2225 vsi->promisc_threshold = 0;
41c445ff
JB
2226 }
2227 }
2228
a856b5cb
ASJ
2229 /* if the VF is not trusted do not do promisc */
2230 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2231 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2232 goto out;
2233 }
2234
41c445ff
JB
2235 /* check for changes in promiscuous modes */
2236 if (changed_flags & IFF_ALLMULTI) {
2237 bool cur_multipromisc;
6995b36c 2238
41c445ff 2239 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
ea02e90b
MW
2240 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2241 vsi->seid,
2242 cur_multipromisc,
2243 NULL);
2244 if (aq_ret) {
2245 retval = i40e_aq_rc_to_posix(aq_ret,
3e25a8f3 2246 hw->aq.asq_last_status);
41c445ff 2247 dev_info(&pf->pdev->dev,
2d1de828
SN
2248 "set multi promisc failed on %s, err %s aq_err %s\n",
2249 vsi_name,
3e25a8f3
MW
2250 i40e_stat_str(hw, aq_ret),
2251 i40e_aq_str(hw, hw->aq.asq_last_status));
ea02e90b 2252 }
41c445ff 2253 }
c3c7ea27
MW
2254 if ((changed_flags & IFF_PROMISC) ||
2255 (promisc_changed &&
2256 test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))) {
41c445ff 2257 bool cur_promisc;
6995b36c 2258
41c445ff
JB
2259 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2260 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2261 &vsi->state));
6784ed5a
ASJ
2262 if ((vsi->type == I40E_VSI_MAIN) &&
2263 (pf->lan_veb != I40E_NO_VEB) &&
2264 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
92faef85
ASJ
2265 /* set defport ON for Main VSI instead of true promisc
2266 * this way we will get all unicast/multicast and VLAN
2267 * promisc behavior but will not get VF or VMDq traffic
2268 * replicated on the Main VSI.
2269 */
2270 if (pf->cur_promisc != cur_promisc) {
2271 pf->cur_promisc = cur_promisc;
5bc16031
MW
2272 if (cur_promisc)
2273 aq_ret =
2274 i40e_aq_set_default_vsi(hw,
2275 vsi->seid,
2276 NULL);
2277 else
2278 aq_ret =
2279 i40e_aq_clear_default_vsi(hw,
2280 vsi->seid,
2281 NULL);
2282 if (aq_ret) {
2283 retval = i40e_aq_rc_to_posix(aq_ret,
2284 hw->aq.asq_last_status);
2285 dev_info(&pf->pdev->dev,
2d1de828
SN
2286 "Set default VSI failed on %s, err %s, aq_err %s\n",
2287 vsi_name,
5bc16031
MW
2288 i40e_stat_str(hw, aq_ret),
2289 i40e_aq_str(hw,
2290 hw->aq.asq_last_status));
2291 }
92faef85
ASJ
2292 }
2293 } else {
ea02e90b 2294 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
3e25a8f3 2295 hw,
f1c7e72e 2296 vsi->seid,
b5569892
ASJ
2297 cur_promisc, NULL,
2298 true);
ea02e90b
MW
2299 if (aq_ret) {
2300 retval =
2301 i40e_aq_rc_to_posix(aq_ret,
3e25a8f3 2302 hw->aq.asq_last_status);
92faef85 2303 dev_info(&pf->pdev->dev,
2d1de828
SN
2304 "set unicast promisc failed on %s, err %s, aq_err %s\n",
2305 vsi_name,
3e25a8f3
MW
2306 i40e_stat_str(hw, aq_ret),
2307 i40e_aq_str(hw,
2308 hw->aq.asq_last_status));
ea02e90b
MW
2309 }
2310 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
3e25a8f3 2311 hw,
92faef85
ASJ
2312 vsi->seid,
2313 cur_promisc, NULL);
ea02e90b
MW
2314 if (aq_ret) {
2315 retval =
2316 i40e_aq_rc_to_posix(aq_ret,
3e25a8f3 2317 hw->aq.asq_last_status);
92faef85 2318 dev_info(&pf->pdev->dev,
2d1de828
SN
2319 "set multicast promisc failed on %s, err %s, aq_err %s\n",
2320 vsi_name,
3e25a8f3
MW
2321 i40e_stat_str(hw, aq_ret),
2322 i40e_aq_str(hw,
2323 hw->aq.asq_last_status));
ea02e90b 2324 }
92faef85 2325 }
ea02e90b
MW
2326 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
2327 vsi->seid,
2328 cur_promisc, NULL);
2329 if (aq_ret) {
2330 retval = i40e_aq_rc_to_posix(aq_ret,
2331 pf->hw.aq.asq_last_status);
1a10370a 2332 dev_info(&pf->pdev->dev,
f1c7e72e 2333 "set brdcast promisc failed, err %s, aq_err %s\n",
3e25a8f3
MW
2334 i40e_stat_str(hw, aq_ret),
2335 i40e_aq_str(hw,
2336 hw->aq.asq_last_status));
ea02e90b 2337 }
41c445ff 2338 }
ea02e90b 2339out:
2818ccd9
JB
2340 /* if something went wrong then set the changed flag so we try again */
2341 if (retval)
2342 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2343
41c445ff 2344 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
ea02e90b 2345 return retval;
4a2ce27b
JK
2346
2347err_no_memory:
2348 /* Restore elements on the temporary add and delete lists */
2349 spin_lock_bh(&vsi->mac_filter_hash_lock);
84f5ca6c 2350err_no_memory_locked:
4a2ce27b
JK
2351 i40e_undo_filter_entries(vsi, &tmp_del_list);
2352 i40e_undo_filter_entries(vsi, &tmp_add_list);
2353 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2354
2355 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2356 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
2357 return -ENOMEM;
41c445ff
JB
2358}
2359
2360/**
2361 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2362 * @pf: board private structure
2363 **/
2364static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2365{
2366 int v;
2367
2368 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2369 return;
2370 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2371
505682cd 2372 for (v = 0; v < pf->num_alloc_vsi; v++) {
41c445ff 2373 if (pf->vsi[v] &&
17652c63
JB
2374 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2375 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2376
2377 if (ret) {
2378 /* come back and try again later */
2379 pf->flags |= I40E_FLAG_FILTER_SYNC;
2380 break;
2381 }
2382 }
41c445ff
JB
2383 }
2384}
2385
2386/**
2387 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2388 * @netdev: network interface device structure
2389 * @new_mtu: new value for maximum frame size
2390 *
2391 * Returns 0 on success, negative on failure
2392 **/
2393static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2394{
2395 struct i40e_netdev_priv *np = netdev_priv(netdev);
41c445ff
JB
2396 struct i40e_vsi *vsi = np->vsi;
2397
41c445ff
JB
2398 netdev_info(netdev, "changing MTU from %d to %d\n",
2399 netdev->mtu, new_mtu);
2400 netdev->mtu = new_mtu;
2401 if (netif_running(netdev))
2402 i40e_vsi_reinit_locked(vsi);
e3219ce6 2403 i40e_notify_client_of_l2_param_changes(vsi);
41c445ff
JB
2404 return 0;
2405}
2406
beb0dff1
JK
2407/**
2408 * i40e_ioctl - Access the hwtstamp interface
2409 * @netdev: network interface device structure
2410 * @ifr: interface request data
2411 * @cmd: ioctl command
2412 **/
2413int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2414{
2415 struct i40e_netdev_priv *np = netdev_priv(netdev);
2416 struct i40e_pf *pf = np->vsi->back;
2417
2418 switch (cmd) {
2419 case SIOCGHWTSTAMP:
2420 return i40e_ptp_get_ts_config(pf, ifr);
2421 case SIOCSHWTSTAMP:
2422 return i40e_ptp_set_ts_config(pf, ifr);
2423 default:
2424 return -EOPNOTSUPP;
2425 }
2426}
2427
41c445ff
JB
2428/**
2429 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2430 * @vsi: the vsi being adjusted
2431 **/
2432void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2433{
2434 struct i40e_vsi_context ctxt;
2435 i40e_status ret;
2436
2437 if ((vsi->info.valid_sections &
2438 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2439 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2440 return; /* already enabled */
2441
2442 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2443 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2444 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2445
2446 ctxt.seid = vsi->seid;
1a2f6248 2447 ctxt.info = vsi->info;
41c445ff
JB
2448 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2449 if (ret) {
2450 dev_info(&vsi->back->pdev->dev,
f1c7e72e
SN
2451 "update vlan stripping failed, err %s aq_err %s\n",
2452 i40e_stat_str(&vsi->back->hw, ret),
2453 i40e_aq_str(&vsi->back->hw,
2454 vsi->back->hw.aq.asq_last_status));
41c445ff
JB
2455 }
2456}
2457
2458/**
2459 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2460 * @vsi: the vsi being adjusted
2461 **/
2462void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2463{
2464 struct i40e_vsi_context ctxt;
2465 i40e_status ret;
2466
2467 if ((vsi->info.valid_sections &
2468 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2469 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2470 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2471 return; /* already disabled */
2472
2473 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2474 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2475 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2476
2477 ctxt.seid = vsi->seid;
1a2f6248 2478 ctxt.info = vsi->info;
41c445ff
JB
2479 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2480 if (ret) {
2481 dev_info(&vsi->back->pdev->dev,
f1c7e72e
SN
2482 "update vlan stripping failed, err %s aq_err %s\n",
2483 i40e_stat_str(&vsi->back->hw, ret),
2484 i40e_aq_str(&vsi->back->hw,
2485 vsi->back->hw.aq.asq_last_status));
41c445ff
JB
2486 }
2487}
2488
2489/**
2490 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2491 * @netdev: network interface to be adjusted
2492 * @features: netdev features to test if VLAN offload is enabled or not
2493 **/
2494static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2495{
2496 struct i40e_netdev_priv *np = netdev_priv(netdev);
2497 struct i40e_vsi *vsi = np->vsi;
2498
2499 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2500 i40e_vlan_stripping_enable(vsi);
2501 else
2502 i40e_vlan_stripping_disable(vsi);
2503}
2504
2505/**
2506 * i40e_vsi_add_vlan - Add vsi membership for given vlan
2507 * @vsi: the vsi being configured
2508 * @vid: vlan id to be added (0 = untagged only , -1 = any)
2509 **/
2510int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
2511{
278e7d0b
JK
2512 struct i40e_mac_filter *f, *add_f, *del_f;
2513 struct hlist_node *h;
2514 int bkt;
41c445ff 2515
21659035 2516 /* Locked once because all functions invoked below iterates list*/
278e7d0b 2517 spin_lock_bh(&vsi->mac_filter_hash_lock);
21659035 2518
278e7d0b 2519 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
57b341d6
JK
2520 if (f->state == I40E_FILTER_REMOVE)
2521 continue;
1bc87e80 2522 add_f = i40e_add_filter(vsi, f->macaddr, vid);
41c445ff
JB
2523 if (!add_f) {
2524 dev_info(&vsi->back->pdev->dev,
2525 "Could not add vlan filter %d for %pM\n",
2526 vid, f->macaddr);
278e7d0b 2527 spin_unlock_bh(&vsi->mac_filter_hash_lock);
41c445ff
JB
2528 return -ENOMEM;
2529 }
2530 }
2531
3c7cbd45
JK
2532 /* When we add a new VLAN filter, we need to make sure that all existing
2533 * filters which are marked as vid=-1 (I40E_VLAN_ANY) are converted to
2534 * vid=0. The simplest way is just search for all filters marked as
2535 * vid=-1 and replace them with vid=0. This converts all filters that
2536 * were marked to receive all traffic (tagged or untagged) into
2537 * filters to receive only untagged traffic, so that we don't receive
2538 * tagged traffic for VLANs which we have not configured.
41c445ff 2539 */
8d82a7c5 2540 if (vid > 0 && !vsi->info.pvid) {
278e7d0b 2541 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
57b341d6
JK
2542 if (f->state == I40E_FILTER_REMOVE)
2543 continue;
290d2557
JK
2544 del_f = i40e_find_filter(vsi, f->macaddr,
2545 I40E_VLAN_ANY);
2546 if (!del_f)
21659035 2547 continue;
290d2557 2548 __i40e_del_filter(vsi, del_f);
1bc87e80 2549 add_f = i40e_add_filter(vsi, f->macaddr, 0);
21659035
KP
2550 if (!add_f) {
2551 dev_info(&vsi->back->pdev->dev,
2552 "Could not add filter 0 for %pM\n",
2553 f->macaddr);
278e7d0b 2554 spin_unlock_bh(&vsi->mac_filter_hash_lock);
21659035 2555 return -ENOMEM;
41c445ff
JB
2556 }
2557 }
41c445ff
JB
2558 }
2559
278e7d0b 2560 spin_unlock_bh(&vsi->mac_filter_hash_lock);
21659035 2561
0e4425ed
JB
2562 /* schedule our worker thread which will take care of
2563 * applying the new filter changes
2564 */
2565 i40e_service_event_schedule(vsi->back);
2566 return 0;
41c445ff
JB
2567}
2568
2569/**
2570 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
2571 * @vsi: the vsi being configured
2572 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
2573 **/
3aa7b74d 2574void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
41c445ff 2575{
84f5ca6c 2576 struct i40e_mac_filter *f;
278e7d0b 2577 struct hlist_node *h;
278e7d0b 2578 int bkt;
41c445ff 2579
21659035 2580 /* Locked once because all functions invoked below iterates list */
278e7d0b 2581 spin_lock_bh(&vsi->mac_filter_hash_lock);
21659035 2582
278e7d0b 2583 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
290d2557
JK
2584 if (f->vlan == vid)
2585 __i40e_del_filter(vsi, f);
2586 }
41c445ff 2587
278e7d0b 2588 spin_unlock_bh(&vsi->mac_filter_hash_lock);
21659035 2589
0e4425ed
JB
2590 /* schedule our worker thread which will take care of
2591 * applying the new filter changes
2592 */
2593 i40e_service_event_schedule(vsi->back);
41c445ff
JB
2594}
2595
2596/**
2597 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2598 * @netdev: network interface to be adjusted
2599 * @vid: vlan id to be added
078b5876
JB
2600 *
2601 * net_device_ops implementation for adding vlan ids
41c445ff 2602 **/
38e00438
VD
2603#ifdef I40E_FCOE
2604int i40e_vlan_rx_add_vid(struct net_device *netdev,
2605 __always_unused __be16 proto, u16 vid)
2606#else
41c445ff
JB
2607static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2608 __always_unused __be16 proto, u16 vid)
38e00438 2609#endif
41c445ff
JB
2610{
2611 struct i40e_netdev_priv *np = netdev_priv(netdev);
2612 struct i40e_vsi *vsi = np->vsi;
078b5876 2613 int ret = 0;
41c445ff 2614
6a112785 2615 if (vid >= VLAN_N_VID)
078b5876
JB
2616 return -EINVAL;
2617
6982d429
ASJ
2618 /* If the network stack called us with vid = 0 then
2619 * it is asking to receive priority tagged packets with
2620 * vlan id 0. Our HW receives them by default when configured
2621 * to receive untagged packets so there is no need to add an
2622 * extra filter for vlan 0 tagged packets.
41c445ff 2623 */
6982d429
ASJ
2624 if (vid)
2625 ret = i40e_vsi_add_vlan(vsi, vid);
41c445ff 2626
6a112785 2627 if (!ret)
078b5876 2628 set_bit(vid, vsi->active_vlans);
41c445ff 2629
078b5876 2630 return ret;
41c445ff
JB
2631}
2632
2633/**
2634 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2635 * @netdev: network interface to be adjusted
2636 * @vid: vlan id to be removed
078b5876 2637 *
fdfd943e 2638 * net_device_ops implementation for removing vlan ids
41c445ff 2639 **/
38e00438
VD
2640#ifdef I40E_FCOE
2641int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2642 __always_unused __be16 proto, u16 vid)
2643#else
41c445ff
JB
2644static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2645 __always_unused __be16 proto, u16 vid)
38e00438 2646#endif
41c445ff
JB
2647{
2648 struct i40e_netdev_priv *np = netdev_priv(netdev);
2649 struct i40e_vsi *vsi = np->vsi;
2650
41c445ff
JB
2651 /* return code is ignored as there is nothing a user
2652 * can do about failure to remove and a log message was
078b5876 2653 * already printed from the other function
41c445ff
JB
2654 */
2655 i40e_vsi_kill_vlan(vsi, vid);
2656
2657 clear_bit(vid, vsi->active_vlans);
078b5876 2658
41c445ff
JB
2659 return 0;
2660}
2661
b1b15df5
TD
2662/**
2663 * i40e_macaddr_init - explicitly write the mac address filters
2664 *
2665 * @vsi: pointer to the vsi
2666 * @macaddr: the MAC address
2667 *
2668 * This is needed when the macaddr has been obtained by other
2669 * means than the default, e.g., from Open Firmware or IDPROM.
2670 * Returns 0 on success, negative on failure
2671 **/
2672static int i40e_macaddr_init(struct i40e_vsi *vsi, u8 *macaddr)
2673{
2674 int ret;
2675 struct i40e_aqc_add_macvlan_element_data element;
2676
2677 ret = i40e_aq_mac_address_write(&vsi->back->hw,
2678 I40E_AQC_WRITE_TYPE_LAA_WOL,
2679 macaddr, NULL);
2680 if (ret) {
2681 dev_info(&vsi->back->pdev->dev,
2682 "Addr change for VSI failed: %d\n", ret);
2683 return -EADDRNOTAVAIL;
2684 }
2685
2686 memset(&element, 0, sizeof(element));
2687 ether_addr_copy(element.mac_addr, macaddr);
2688 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
2689 ret = i40e_aq_add_macvlan(&vsi->back->hw, vsi->seid, &element, 1, NULL);
2690 if (ret) {
2691 dev_info(&vsi->back->pdev->dev,
2692 "add filter failed err %s aq_err %s\n",
2693 i40e_stat_str(&vsi->back->hw, ret),
2694 i40e_aq_str(&vsi->back->hw,
2695 vsi->back->hw.aq.asq_last_status));
2696 }
2697 return ret;
2698}
2699
41c445ff
JB
2700/**
2701 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2702 * @vsi: the vsi being brought back up
2703 **/
2704static void i40e_restore_vlan(struct i40e_vsi *vsi)
2705{
2706 u16 vid;
2707
2708 if (!vsi->netdev)
2709 return;
2710
2711 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2712
2713 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2714 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2715 vid);
2716}
2717
2718/**
2719 * i40e_vsi_add_pvid - Add pvid for the VSI
2720 * @vsi: the vsi being adjusted
2721 * @vid: the vlan id to set as a PVID
2722 **/
dcae29be 2723int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
41c445ff
JB
2724{
2725 struct i40e_vsi_context ctxt;
f1c7e72e 2726 i40e_status ret;
41c445ff
JB
2727
2728 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2729 vsi->info.pvid = cpu_to_le16(vid);
6c12fcbf
GR
2730 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2731 I40E_AQ_VSI_PVLAN_INSERT_PVID |
b774c7dd 2732 I40E_AQ_VSI_PVLAN_EMOD_STR;
41c445ff
JB
2733
2734 ctxt.seid = vsi->seid;
1a2f6248 2735 ctxt.info = vsi->info;
f1c7e72e
SN
2736 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2737 if (ret) {
41c445ff 2738 dev_info(&vsi->back->pdev->dev,
f1c7e72e
SN
2739 "add pvid failed, err %s aq_err %s\n",
2740 i40e_stat_str(&vsi->back->hw, ret),
2741 i40e_aq_str(&vsi->back->hw,
2742 vsi->back->hw.aq.asq_last_status));
dcae29be 2743 return -ENOENT;
41c445ff
JB
2744 }
2745
dcae29be 2746 return 0;
41c445ff
JB
2747}
2748
2749/**
2750 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2751 * @vsi: the vsi being adjusted
2752 *
2753 * Just use the vlan_rx_register() service to put it back to normal
2754 **/
2755void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2756{
6c12fcbf
GR
2757 i40e_vlan_stripping_disable(vsi);
2758
41c445ff 2759 vsi->info.pvid = 0;
41c445ff
JB
2760}
2761
2762/**
2763 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2764 * @vsi: ptr to the VSI
2765 *
2766 * If this function returns with an error, then it's possible one or
2767 * more of the rings is populated (while the rest are not). It is the
2768 * callers duty to clean those orphaned rings.
2769 *
2770 * Return 0 on success, negative on failure
2771 **/
2772static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2773{
2774 int i, err = 0;
2775
2776 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
9f65e15b 2777 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
41c445ff
JB
2778
2779 return err;
2780}
2781
2782/**
2783 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2784 * @vsi: ptr to the VSI
2785 *
2786 * Free VSI's transmit software resources
2787 **/
2788static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2789{
2790 int i;
2791
8e9dca53
GR
2792 if (!vsi->tx_rings)
2793 return;
2794
41c445ff 2795 for (i = 0; i < vsi->num_queue_pairs; i++)
8e9dca53 2796 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
9f65e15b 2797 i40e_free_tx_resources(vsi->tx_rings[i]);
41c445ff
JB
2798}
2799
2800/**
2801 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2802 * @vsi: ptr to the VSI
2803 *
2804 * If this function returns with an error, then it's possible one or
2805 * more of the rings is populated (while the rest are not). It is the
2806 * callers duty to clean those orphaned rings.
2807 *
2808 * Return 0 on success, negative on failure
2809 **/
2810static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2811{
2812 int i, err = 0;
2813
2814 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
9f65e15b 2815 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
38e00438
VD
2816#ifdef I40E_FCOE
2817 i40e_fcoe_setup_ddp_resources(vsi);
2818#endif
41c445ff
JB
2819 return err;
2820}
2821
2822/**
2823 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2824 * @vsi: ptr to the VSI
2825 *
2826 * Free all receive software resources
2827 **/
2828static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2829{
2830 int i;
2831
8e9dca53
GR
2832 if (!vsi->rx_rings)
2833 return;
2834
41c445ff 2835 for (i = 0; i < vsi->num_queue_pairs; i++)
8e9dca53 2836 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
9f65e15b 2837 i40e_free_rx_resources(vsi->rx_rings[i]);
38e00438
VD
2838#ifdef I40E_FCOE
2839 i40e_fcoe_free_ddp_resources(vsi);
2840#endif
41c445ff
JB
2841}
2842
3ffa037d
NP
2843/**
2844 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2845 * @ring: The Tx ring to configure
2846 *
2847 * This enables/disables XPS for a given Tx descriptor ring
2848 * based on the TCs enabled for the VSI that ring belongs to.
2849 **/
2850static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2851{
2852 struct i40e_vsi *vsi = ring->vsi;
2853 cpumask_var_t mask;
2854
9a660eea
JB
2855 if (!ring->q_vector || !ring->netdev)
2856 return;
2857
2858 /* Single TC mode enable XPS */
2859 if (vsi->tc_config.numtc <= 1) {
2860 if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
3ffa037d
NP
2861 netif_set_xps_queue(ring->netdev,
2862 &ring->q_vector->affinity_mask,
2863 ring->queue_index);
9a660eea
JB
2864 } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
2865 /* Disable XPS to allow selection based on TC */
2866 bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
2867 netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
2868 free_cpumask_var(mask);
3ffa037d 2869 }
0e4425ed
JB
2870
2871 /* schedule our worker thread which will take care of
2872 * applying the new filter changes
2873 */
2874 i40e_service_event_schedule(vsi->back);
3ffa037d
NP
2875}
2876
41c445ff
JB
2877/**
2878 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2879 * @ring: The Tx ring to configure
2880 *
2881 * Configure the Tx descriptor ring in the HMC context.
2882 **/
2883static int i40e_configure_tx_ring(struct i40e_ring *ring)
2884{
2885 struct i40e_vsi *vsi = ring->vsi;
2886 u16 pf_q = vsi->base_queue + ring->queue_index;
2887 struct i40e_hw *hw = &vsi->back->hw;
2888 struct i40e_hmc_obj_txq tx_ctx;
2889 i40e_status err = 0;
2890 u32 qtx_ctl = 0;
2891
2892 /* some ATR related tx ring init */
60ea5f83 2893 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
41c445ff
JB
2894 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2895 ring->atr_count = 0;
2896 } else {
2897 ring->atr_sample_rate = 0;
2898 }
2899
3ffa037d
NP
2900 /* configure XPS */
2901 i40e_config_xps_tx_ring(ring);
41c445ff
JB
2902
2903 /* clear the context structure first */
2904 memset(&tx_ctx, 0, sizeof(tx_ctx));
2905
2906 tx_ctx.new_context = 1;
2907 tx_ctx.base = (ring->dma / 128);
2908 tx_ctx.qlen = ring->count;
60ea5f83
JB
2909 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2910 I40E_FLAG_FD_ATR_ENABLED));
38e00438
VD
2911#ifdef I40E_FCOE
2912 tx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2913#endif
beb0dff1 2914 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
1943d8ba
JB
2915 /* FDIR VSI tx ring can still use RS bit and writebacks */
2916 if (vsi->type != I40E_VSI_FDIR)
2917 tx_ctx.head_wb_ena = 1;
2918 tx_ctx.head_wb_addr = ring->dma +
2919 (ring->count * sizeof(struct i40e_tx_desc));
41c445ff
JB
2920
2921 /* As part of VSI creation/update, FW allocates certain
2922 * Tx arbitration queue sets for each TC enabled for
2923 * the VSI. The FW returns the handles to these queue
2924 * sets as part of the response buffer to Add VSI,
2925 * Update VSI, etc. AQ commands. It is expected that
2926 * these queue set handles be associated with the Tx
2927 * queues by the driver as part of the TX queue context
2928 * initialization. This has to be done regardless of
2929 * DCB as by default everything is mapped to TC0.
2930 */
2931 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2932 tx_ctx.rdylist_act = 0;
2933
2934 /* clear the context in the HMC */
2935 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2936 if (err) {
2937 dev_info(&vsi->back->pdev->dev,
2938 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2939 ring->queue_index, pf_q, err);
2940 return -ENOMEM;
2941 }
2942
2943 /* set the context in the HMC */
2944 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2945 if (err) {
2946 dev_info(&vsi->back->pdev->dev,
2947 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2948 ring->queue_index, pf_q, err);
2949 return -ENOMEM;
2950 }
2951
2952 /* Now associate this queue with this PCI function */
7a28d885 2953 if (vsi->type == I40E_VSI_VMDQ2) {
9d8bf547 2954 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
7a28d885
MW
2955 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
2956 I40E_QTX_CTL_VFVM_INDX_MASK;
2957 } else {
9d8bf547 2958 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
7a28d885
MW
2959 }
2960
13fd9774
SN
2961 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2962 I40E_QTX_CTL_PF_INDX_MASK);
41c445ff
JB
2963 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2964 i40e_flush(hw);
2965
41c445ff
JB
2966 /* cache tail off for easier writes later */
2967 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2968
2969 return 0;
2970}
2971
2972/**
2973 * i40e_configure_rx_ring - Configure a receive ring context
2974 * @ring: The Rx ring to configure
2975 *
2976 * Configure the Rx descriptor ring in the HMC context.
2977 **/
2978static int i40e_configure_rx_ring(struct i40e_ring *ring)
2979{
2980 struct i40e_vsi *vsi = ring->vsi;
2981 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2982 u16 pf_q = vsi->base_queue + ring->queue_index;
2983 struct i40e_hw *hw = &vsi->back->hw;
2984 struct i40e_hmc_obj_rxq rx_ctx;
2985 i40e_status err = 0;
2986
2987 ring->state = 0;
2988
2989 /* clear the context structure first */
2990 memset(&rx_ctx, 0, sizeof(rx_ctx));
2991
2992 ring->rx_buf_len = vsi->rx_buf_len;
41c445ff
JB
2993
2994 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
41c445ff
JB
2995
2996 rx_ctx.base = (ring->dma / 128);
2997 rx_ctx.qlen = ring->count;
2998
bec60fc4
JB
2999 /* use 32 byte descriptors */
3000 rx_ctx.dsize = 1;
41c445ff 3001
bec60fc4
JB
3002 /* descriptor type is always zero
3003 * rx_ctx.dtype = 0;
3004 */
b32bfa17 3005 rx_ctx.hsplit_0 = 0;
41c445ff 3006
b32bfa17 3007 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
7134f9ce
JB
3008 if (hw->revision_id == 0)
3009 rx_ctx.lrxqthresh = 0;
3010 else
3011 rx_ctx.lrxqthresh = 2;
41c445ff
JB
3012 rx_ctx.crcstrip = 1;
3013 rx_ctx.l2tsel = 1;
c4bbac39
JB
3014 /* this controls whether VLAN is stripped from inner headers */
3015 rx_ctx.showiv = 0;
38e00438
VD
3016#ifdef I40E_FCOE
3017 rx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
3018#endif
acb3676b
CS
3019 /* set the prefena field to 1 because the manual says to */
3020 rx_ctx.prefena = 1;
41c445ff
JB
3021
3022 /* clear the context in the HMC */
3023 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3024 if (err) {
3025 dev_info(&vsi->back->pdev->dev,
3026 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3027 ring->queue_index, pf_q, err);
3028 return -ENOMEM;
3029 }
3030
3031 /* set the context in the HMC */
3032 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3033 if (err) {
3034 dev_info(&vsi->back->pdev->dev,
3035 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3036 ring->queue_index, pf_q, err);
3037 return -ENOMEM;
3038 }
3039
3040 /* cache tail for quicker writes, and clear the reg before use */
3041 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3042 writel(0, ring->tail);
3043
1a557afc 3044 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
41c445ff
JB
3045
3046 return 0;
3047}
3048
3049/**
3050 * i40e_vsi_configure_tx - Configure the VSI for Tx
3051 * @vsi: VSI structure describing this set of rings and resources
3052 *
3053 * Configure the Tx VSI for operation.
3054 **/
3055static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3056{
3057 int err = 0;
3058 u16 i;
3059
9f65e15b
AD
3060 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3061 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
41c445ff
JB
3062
3063 return err;
3064}
3065
3066/**
3067 * i40e_vsi_configure_rx - Configure the VSI for Rx
3068 * @vsi: the VSI being configured
3069 *
3070 * Configure the Rx VSI for operation.
3071 **/
3072static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3073{
3074 int err = 0;
3075 u16 i;
3076
3077 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
3078 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
3079 + ETH_FCS_LEN + VLAN_HLEN;
3080 else
3081 vsi->max_frame = I40E_RXBUFFER_2048;
3082
1a557afc 3083 vsi->rx_buf_len = I40E_RXBUFFER_2048;
41c445ff 3084
38e00438
VD
3085#ifdef I40E_FCOE
3086 /* setup rx buffer for FCoE */
3087 if ((vsi->type == I40E_VSI_FCOE) &&
3088 (vsi->back->flags & I40E_FLAG_FCOE_ENABLED)) {
38e00438
VD
3089 vsi->rx_buf_len = I40E_RXBUFFER_3072;
3090 vsi->max_frame = I40E_RXBUFFER_3072;
38e00438
VD
3091 }
3092
3093#endif /* I40E_FCOE */
41c445ff 3094 /* round up for the chip's needs */
41c445ff 3095 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
41a1d04b 3096 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
41c445ff
JB
3097
3098 /* set up individual rings */
3099 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
9f65e15b 3100 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
41c445ff
JB
3101
3102 return err;
3103}
3104
3105/**
3106 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3107 * @vsi: ptr to the VSI
3108 **/
3109static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3110{
e7046ee1 3111 struct i40e_ring *tx_ring, *rx_ring;
41c445ff
JB
3112 u16 qoffset, qcount;
3113 int i, n;
3114
cd238a3e
PN
3115 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3116 /* Reset the TC information */
3117 for (i = 0; i < vsi->num_queue_pairs; i++) {
3118 rx_ring = vsi->rx_rings[i];
3119 tx_ring = vsi->tx_rings[i];
3120 rx_ring->dcb_tc = 0;
3121 tx_ring->dcb_tc = 0;
3122 }
3123 }
41c445ff
JB
3124
3125 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
41a1d04b 3126 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
41c445ff
JB
3127 continue;
3128
3129 qoffset = vsi->tc_config.tc_info[n].qoffset;
3130 qcount = vsi->tc_config.tc_info[n].qcount;
3131 for (i = qoffset; i < (qoffset + qcount); i++) {
e7046ee1
AA
3132 rx_ring = vsi->rx_rings[i];
3133 tx_ring = vsi->tx_rings[i];
41c445ff
JB
3134 rx_ring->dcb_tc = n;
3135 tx_ring->dcb_tc = n;
3136 }
3137 }
3138}
3139
3140/**
3141 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3142 * @vsi: ptr to the VSI
3143 **/
3144static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3145{
b1b15df5
TD
3146 struct i40e_pf *pf = vsi->back;
3147 int err;
3148
41c445ff
JB
3149 if (vsi->netdev)
3150 i40e_set_rx_mode(vsi->netdev);
b1b15df5
TD
3151
3152 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
3153 err = i40e_macaddr_init(vsi, pf->hw.mac.addr);
3154 if (err) {
3155 dev_warn(&pf->pdev->dev,
3156 "could not set up macaddr; err %d\n", err);
3157 }
3158 }
41c445ff
JB
3159}
3160
17a73f6b
JG
3161/**
3162 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3163 * @vsi: Pointer to the targeted VSI
3164 *
3165 * This function replays the hlist on the hw where all the SB Flow Director
3166 * filters were saved.
3167 **/
3168static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3169{
3170 struct i40e_fdir_filter *filter;
3171 struct i40e_pf *pf = vsi->back;
3172 struct hlist_node *node;
3173
55a5e60b
ASJ
3174 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3175 return;
3176
17a73f6b
JG
3177 hlist_for_each_entry_safe(filter, node,
3178 &pf->fdir_filter_list, fdir_node) {
3179 i40e_add_del_fdir(vsi, filter, true);
3180 }
3181}
3182
41c445ff
JB
3183/**
3184 * i40e_vsi_configure - Set up the VSI for action
3185 * @vsi: the VSI being configured
3186 **/
3187static int i40e_vsi_configure(struct i40e_vsi *vsi)
3188{
3189 int err;
3190
3191 i40e_set_vsi_rx_mode(vsi);
3192 i40e_restore_vlan(vsi);
3193 i40e_vsi_config_dcb_rings(vsi);
3194 err = i40e_vsi_configure_tx(vsi);
3195 if (!err)
3196 err = i40e_vsi_configure_rx(vsi);
3197
3198 return err;
3199}
3200
3201/**
3202 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3203 * @vsi: the VSI being configured
3204 **/
3205static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3206{
3207 struct i40e_pf *pf = vsi->back;
41c445ff
JB
3208 struct i40e_hw *hw = &pf->hw;
3209 u16 vector;
3210 int i, q;
41c445ff
JB
3211 u32 qp;
3212
3213 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3214 * and PFINT_LNKLSTn registers, e.g.:
3215 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3216 */
3217 qp = vsi->base_queue;
3218 vector = vsi->base_vector;
493fb300 3219 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
ac26fc13
JB
3220 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3221
ee2319cf 3222 q_vector->itr_countdown = ITR_COUNTDOWN_START;
a75e8005 3223 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
41c445ff
JB
3224 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3225 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3226 q_vector->rx.itr);
a75e8005 3227 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
41c445ff
JB
3228 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3229 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3230 q_vector->tx.itr);
ac26fc13
JB
3231 wr32(hw, I40E_PFINT_RATEN(vector - 1),
3232 INTRL_USEC_TO_REG(vsi->int_rate_limit));
41c445ff
JB
3233
3234 /* Linked list for the queuepairs assigned to this vector */
3235 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3236 for (q = 0; q < q_vector->num_ringpairs; q++) {
ac26fc13
JB
3237 u32 val;
3238
41c445ff
JB
3239 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3240 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3241 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3242 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3243 (I40E_QUEUE_TYPE_TX
3244 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3245
3246 wr32(hw, I40E_QINT_RQCTL(qp), val);
3247
3248 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3249 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3250 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3251 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
3252 (I40E_QUEUE_TYPE_RX
3253 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3254
3255 /* Terminate the linked list */
3256 if (q == (q_vector->num_ringpairs - 1))
3257 val |= (I40E_QUEUE_END_OF_LIST
3258 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3259
3260 wr32(hw, I40E_QINT_TQCTL(qp), val);
3261 qp++;
3262 }
3263 }
3264
3265 i40e_flush(hw);
3266}
3267
3268/**
3269 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3270 * @hw: ptr to the hardware info
3271 **/
ab437b5a 3272static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
41c445ff 3273{
ab437b5a 3274 struct i40e_hw *hw = &pf->hw;
41c445ff
JB
3275 u32 val;
3276
3277 /* clear things first */
3278 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3279 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3280
3281 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3282 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3283 I40E_PFINT_ICR0_ENA_GRST_MASK |
3284 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3285 I40E_PFINT_ICR0_ENA_GPIO_MASK |
41c445ff
JB
3286 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3287 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3288 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3289
0d8e1439
ASJ
3290 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3291 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3292
ab437b5a
JK
3293 if (pf->flags & I40E_FLAG_PTP)
3294 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3295
41c445ff
JB
3296 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3297
3298 /* SW_ITR_IDX = 0, but don't change INTENA */
84ed40e7
ASJ
3299 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3300 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
41c445ff
JB
3301
3302 /* OTHER_ITR_IDX = 0 */
3303 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3304}
3305
3306/**
3307 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3308 * @vsi: the VSI being configured
3309 **/
3310static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3311{
493fb300 3312 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
41c445ff
JB
3313 struct i40e_pf *pf = vsi->back;
3314 struct i40e_hw *hw = &pf->hw;
3315 u32 val;
3316
3317 /* set the ITR configuration */
ee2319cf 3318 q_vector->itr_countdown = ITR_COUNTDOWN_START;
a75e8005 3319 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
41c445ff
JB
3320 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3321 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
a75e8005 3322 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
41c445ff
JB
3323 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3324 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3325
ab437b5a 3326 i40e_enable_misc_int_causes(pf);
41c445ff
JB
3327
3328 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3329 wr32(hw, I40E_PFINT_LNKLST0, 0);
3330
f29eaa3d 3331 /* Associate the queue pair to the vector and enable the queue int */
41c445ff
JB
3332 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3333 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3334 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3335
3336 wr32(hw, I40E_QINT_RQCTL(0), val);
3337
3338 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3339 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3340 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3341
3342 wr32(hw, I40E_QINT_TQCTL(0), val);
3343 i40e_flush(hw);
3344}
3345
2ef28cfb
MW
3346/**
3347 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3348 * @pf: board private structure
3349 **/
3350void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3351{
3352 struct i40e_hw *hw = &pf->hw;
3353
3354 wr32(hw, I40E_PFINT_DYN_CTL0,
3355 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3356 i40e_flush(hw);
3357}
3358
41c445ff
JB
3359/**
3360 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3361 * @pf: board private structure
40d72a50 3362 * @clearpba: true when all pending interrupt events should be cleared
41c445ff 3363 **/
40d72a50 3364void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
41c445ff
JB
3365{
3366 struct i40e_hw *hw = &pf->hw;
3367 u32 val;
3368
3369 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
40d72a50 3370 (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
41c445ff
JB
3371 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3372
3373 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3374 i40e_flush(hw);
3375}
3376
41c445ff
JB
3377/**
3378 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3379 * @irq: interrupt number
3380 * @data: pointer to a q_vector
3381 **/
3382static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3383{
3384 struct i40e_q_vector *q_vector = data;
3385
cd0b6fa6 3386 if (!q_vector->tx.ring && !q_vector->rx.ring)
41c445ff
JB
3387 return IRQ_HANDLED;
3388
5d3465a1 3389 napi_schedule_irqoff(&q_vector->napi);
41c445ff
JB
3390
3391 return IRQ_HANDLED;
3392}
3393
96db776a
AB
3394/**
3395 * i40e_irq_affinity_notify - Callback for affinity changes
3396 * @notify: context as to what irq was changed
3397 * @mask: the new affinity mask
3398 *
3399 * This is a callback function used by the irq_set_affinity_notifier function
3400 * so that we may register to receive changes to the irq affinity masks.
3401 **/
3402static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3403 const cpumask_t *mask)
3404{
3405 struct i40e_q_vector *q_vector =
3406 container_of(notify, struct i40e_q_vector, affinity_notify);
3407
3408 q_vector->affinity_mask = *mask;
3409}
3410
3411/**
3412 * i40e_irq_affinity_release - Callback for affinity notifier release
3413 * @ref: internal core kernel usage
3414 *
3415 * This is a callback function used by the irq_set_affinity_notifier function
3416 * to inform the current notification subscriber that they will no longer
3417 * receive notifications.
3418 **/
3419static void i40e_irq_affinity_release(struct kref *ref) {}
3420
41c445ff
JB
3421/**
3422 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3423 * @vsi: the VSI being configured
3424 * @basename: name for the vector
3425 *
3426 * Allocates MSI-X vectors and requests interrupts from the kernel.
3427 **/
3428static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3429{
3430 int q_vectors = vsi->num_q_vectors;
3431 struct i40e_pf *pf = vsi->back;
3432 int base = vsi->base_vector;
3433 int rx_int_idx = 0;
3434 int tx_int_idx = 0;
3435 int vector, err;
96db776a 3436 int irq_num;
41c445ff
JB
3437
3438 for (vector = 0; vector < q_vectors; vector++) {
493fb300 3439 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
41c445ff 3440
96db776a
AB
3441 irq_num = pf->msix_entries[base + vector].vector;
3442
cd0b6fa6 3443 if (q_vector->tx.ring && q_vector->rx.ring) {
41c445ff
JB
3444 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3445 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3446 tx_int_idx++;
cd0b6fa6 3447 } else if (q_vector->rx.ring) {
41c445ff
JB
3448 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3449 "%s-%s-%d", basename, "rx", rx_int_idx++);
cd0b6fa6 3450 } else if (q_vector->tx.ring) {
41c445ff
JB
3451 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3452 "%s-%s-%d", basename, "tx", tx_int_idx++);
3453 } else {
3454 /* skip this unused q_vector */
3455 continue;
3456 }
96db776a 3457 err = request_irq(irq_num,
41c445ff
JB
3458 vsi->irq_handler,
3459 0,
3460 q_vector->name,
3461 q_vector);
3462 if (err) {
3463 dev_info(&pf->pdev->dev,
fb43201f 3464 "MSIX request_irq failed, error: %d\n", err);
41c445ff
JB
3465 goto free_queue_irqs;
3466 }
96db776a
AB
3467
3468 /* register for affinity change notifications */
3469 q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3470 q_vector->affinity_notify.release = i40e_irq_affinity_release;
3471 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
41c445ff 3472 /* assign the mask for this irq */
96db776a 3473 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
41c445ff
JB
3474 }
3475
63741846 3476 vsi->irqs_ready = true;
41c445ff
JB
3477 return 0;
3478
3479free_queue_irqs:
3480 while (vector) {
3481 vector--;
96db776a
AB
3482 irq_num = pf->msix_entries[base + vector].vector;
3483 irq_set_affinity_notifier(irq_num, NULL);
3484 irq_set_affinity_hint(irq_num, NULL);
3485 free_irq(irq_num, &vsi->q_vectors[vector]);
41c445ff
JB
3486 }
3487 return err;
3488}
3489
3490/**
3491 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3492 * @vsi: the VSI being un-configured
3493 **/
3494static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3495{
3496 struct i40e_pf *pf = vsi->back;
3497 struct i40e_hw *hw = &pf->hw;
3498 int base = vsi->base_vector;
3499 int i;
3500
3501 for (i = 0; i < vsi->num_queue_pairs; i++) {
9f65e15b
AD
3502 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
3503 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
41c445ff
JB
3504 }
3505
3506 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3507 for (i = vsi->base_vector;
3508 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3509 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3510
3511 i40e_flush(hw);
3512 for (i = 0; i < vsi->num_q_vectors; i++)
3513 synchronize_irq(pf->msix_entries[i + base].vector);
3514 } else {
3515 /* Legacy and MSI mode - this stops all interrupt handling */
3516 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3517 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3518 i40e_flush(hw);
3519 synchronize_irq(pf->pdev->irq);
3520 }
3521}
3522
3523/**
3524 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3525 * @vsi: the VSI being configured
3526 **/
3527static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3528{
3529 struct i40e_pf *pf = vsi->back;
3530 int i;
3531
3532 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
7845548d 3533 for (i = 0; i < vsi->num_q_vectors; i++)
41c445ff
JB
3534 i40e_irq_dynamic_enable(vsi, i);
3535 } else {
40d72a50 3536 i40e_irq_dynamic_enable_icr0(pf, true);
41c445ff
JB
3537 }
3538
1022cb6c 3539 i40e_flush(&pf->hw);
41c445ff
JB
3540 return 0;
3541}
3542
3543/**
3544 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
3545 * @pf: board private structure
3546 **/
3547static void i40e_stop_misc_vector(struct i40e_pf *pf)
3548{
3549 /* Disable ICR 0 */
3550 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3551 i40e_flush(&pf->hw);
3552}
3553
3554/**
3555 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3556 * @irq: interrupt number
3557 * @data: pointer to a q_vector
3558 *
3559 * This is the handler used for all MSI/Legacy interrupts, and deals
3560 * with both queue and non-queue interrupts. This is also used in
3561 * MSIX mode to handle the non-queue interrupts.
3562 **/
3563static irqreturn_t i40e_intr(int irq, void *data)
3564{
3565 struct i40e_pf *pf = (struct i40e_pf *)data;
3566 struct i40e_hw *hw = &pf->hw;
5e823066 3567 irqreturn_t ret = IRQ_NONE;
41c445ff
JB
3568 u32 icr0, icr0_remaining;
3569 u32 val, ena_mask;
3570
3571 icr0 = rd32(hw, I40E_PFINT_ICR0);
5e823066 3572 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
41c445ff 3573
116a57d4
SN
3574 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3575 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
5e823066 3576 goto enable_intr;
41c445ff 3577
cd92e72f
SN
3578 /* if interrupt but no bits showing, must be SWINT */
3579 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3580 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3581 pf->sw_int_count++;
3582
0d8e1439
ASJ
3583 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3584 (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3585 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3586 icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
23bb6dc3 3587 dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
0d8e1439
ASJ
3588 }
3589
41c445ff
JB
3590 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3591 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
5d3465a1
AD
3592 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3593 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
41c445ff 3594
a16ae2d5
ASJ
3595 /* We do not have a way to disarm Queue causes while leaving
3596 * interrupt enabled for all other causes, ideally
3597 * interrupt should be disabled while we are in NAPI but
3598 * this is not a performance path and napi_schedule()
3599 * can deal with rescheduling.
3600 */
41c445ff 3601 if (!test_bit(__I40E_DOWN, &pf->state))
5d3465a1 3602 napi_schedule_irqoff(&q_vector->napi);
41c445ff
JB
3603 }
3604
3605 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3606 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3607 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
6e93d0c9 3608 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
41c445ff
JB
3609 }
3610
3611 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3612 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3613 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
3614 }
3615
3616 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3617 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3618 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
3619 }
3620
3621 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3622 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
3623 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
3624 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3625 val = rd32(hw, I40E_GLGEN_RSTAT);
3626 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3627 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4eb3f768 3628 if (val == I40E_RESET_CORER) {
41c445ff 3629 pf->corer_count++;
4eb3f768 3630 } else if (val == I40E_RESET_GLOBR) {
41c445ff 3631 pf->globr_count++;
4eb3f768 3632 } else if (val == I40E_RESET_EMPR) {
41c445ff 3633 pf->empr_count++;
9df42d1a 3634 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
4eb3f768 3635 }
41c445ff
JB
3636 }
3637
9c010ee0
ASJ
3638 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3639 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3640 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
25fc0e65
ASJ
3641 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3642 rd32(hw, I40E_PFHMC_ERRORINFO),
3643 rd32(hw, I40E_PFHMC_ERRORDATA));
9c010ee0
ASJ
3644 }
3645
beb0dff1
JK
3646 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3647 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3648
3649 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
cafa1fca 3650 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
beb0dff1 3651 i40e_ptp_tx_hwtstamp(pf);
beb0dff1 3652 }
beb0dff1
JK
3653 }
3654
41c445ff
JB
3655 /* If a critical error is pending we have no choice but to reset the
3656 * device.
3657 * Report and mask out any remaining unexpected interrupts.
3658 */
3659 icr0_remaining = icr0 & ena_mask;
3660 if (icr0_remaining) {
3661 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3662 icr0_remaining);
9c010ee0 3663 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
41c445ff 3664 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
c0c28975 3665 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
9c010ee0
ASJ
3666 dev_info(&pf->pdev->dev, "device will be reset\n");
3667 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
3668 i40e_service_event_schedule(pf);
41c445ff
JB
3669 }
3670 ena_mask &= ~icr0_remaining;
3671 }
5e823066 3672 ret = IRQ_HANDLED;
41c445ff 3673
5e823066 3674enable_intr:
41c445ff
JB
3675 /* re-enable interrupt causes */
3676 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
41c445ff
JB
3677 if (!test_bit(__I40E_DOWN, &pf->state)) {
3678 i40e_service_event_schedule(pf);
40d72a50 3679 i40e_irq_dynamic_enable_icr0(pf, false);
41c445ff
JB
3680 }
3681
5e823066 3682 return ret;
41c445ff
JB
3683}
3684
cbf61325
ASJ
3685/**
3686 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3687 * @tx_ring: tx ring to clean
3688 * @budget: how many cleans we're allowed
3689 *
3690 * Returns true if there's any budget left (e.g. the clean is finished)
3691 **/
3692static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3693{
3694 struct i40e_vsi *vsi = tx_ring->vsi;
3695 u16 i = tx_ring->next_to_clean;
3696 struct i40e_tx_buffer *tx_buf;
3697 struct i40e_tx_desc *tx_desc;
3698
3699 tx_buf = &tx_ring->tx_bi[i];
3700 tx_desc = I40E_TX_DESC(tx_ring, i);
3701 i -= tx_ring->count;
3702
3703 do {
3704 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3705
3706 /* if next_to_watch is not set then there is no work pending */
3707 if (!eop_desc)
3708 break;
3709
3710 /* prevent any other reads prior to eop_desc */
3711 read_barrier_depends();
3712
3713 /* if the descriptor isn't done, no work yet to do */
3714 if (!(eop_desc->cmd_type_offset_bsz &
3715 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3716 break;
3717
3718 /* clear next_to_watch to prevent false hangs */
3719 tx_buf->next_to_watch = NULL;
3720
49d7d933
ASJ
3721 tx_desc->buffer_addr = 0;
3722 tx_desc->cmd_type_offset_bsz = 0;
3723 /* move past filter desc */
3724 tx_buf++;
3725 tx_desc++;
3726 i++;
3727 if (unlikely(!i)) {
3728 i -= tx_ring->count;
3729 tx_buf = tx_ring->tx_bi;
3730 tx_desc = I40E_TX_DESC(tx_ring, 0);
3731 }
cbf61325
ASJ
3732 /* unmap skb header data */
3733 dma_unmap_single(tx_ring->dev,
3734 dma_unmap_addr(tx_buf, dma),
3735 dma_unmap_len(tx_buf, len),
3736 DMA_TO_DEVICE);
49d7d933
ASJ
3737 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3738 kfree(tx_buf->raw_buf);
cbf61325 3739
49d7d933
ASJ
3740 tx_buf->raw_buf = NULL;
3741 tx_buf->tx_flags = 0;
3742 tx_buf->next_to_watch = NULL;
cbf61325 3743 dma_unmap_len_set(tx_buf, len, 0);
49d7d933
ASJ
3744 tx_desc->buffer_addr = 0;
3745 tx_desc->cmd_type_offset_bsz = 0;
cbf61325 3746
49d7d933 3747 /* move us past the eop_desc for start of next FD desc */
cbf61325
ASJ
3748 tx_buf++;
3749 tx_desc++;
3750 i++;
3751 if (unlikely(!i)) {
3752 i -= tx_ring->count;
3753 tx_buf = tx_ring->tx_bi;
3754 tx_desc = I40E_TX_DESC(tx_ring, 0);
3755 }
3756
3757 /* update budget accounting */
3758 budget--;
3759 } while (likely(budget));
3760
3761 i += tx_ring->count;
3762 tx_ring->next_to_clean = i;
3763
6995b36c 3764 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
7845548d 3765 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
6995b36c 3766
cbf61325
ASJ
3767 return budget > 0;
3768}
3769
3770/**
3771 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3772 * @irq: interrupt number
3773 * @data: pointer to a q_vector
3774 **/
3775static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3776{
3777 struct i40e_q_vector *q_vector = data;
3778 struct i40e_vsi *vsi;
3779
3780 if (!q_vector->tx.ring)
3781 return IRQ_HANDLED;
3782
3783 vsi = q_vector->tx.ring->vsi;
3784 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3785
3786 return IRQ_HANDLED;
3787}
3788
41c445ff 3789/**
cd0b6fa6 3790 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
41c445ff
JB
3791 * @vsi: the VSI being configured
3792 * @v_idx: vector index
cd0b6fa6 3793 * @qp_idx: queue pair index
41c445ff 3794 **/
26cdc443 3795static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
41c445ff 3796{
493fb300 3797 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
9f65e15b
AD
3798 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3799 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
41c445ff
JB
3800
3801 tx_ring->q_vector = q_vector;
cd0b6fa6
AD
3802 tx_ring->next = q_vector->tx.ring;
3803 q_vector->tx.ring = tx_ring;
41c445ff 3804 q_vector->tx.count++;
cd0b6fa6
AD
3805
3806 rx_ring->q_vector = q_vector;
3807 rx_ring->next = q_vector->rx.ring;
3808 q_vector->rx.ring = rx_ring;
3809 q_vector->rx.count++;
41c445ff
JB
3810}
3811
3812/**
3813 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3814 * @vsi: the VSI being configured
3815 *
3816 * This function maps descriptor rings to the queue-specific vectors
3817 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3818 * one vector per queue pair, but on a constrained vector budget, we
3819 * group the queue pairs as "efficiently" as possible.
3820 **/
3821static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3822{
3823 int qp_remaining = vsi->num_queue_pairs;
3824 int q_vectors = vsi->num_q_vectors;
cd0b6fa6 3825 int num_ringpairs;
41c445ff
JB
3826 int v_start = 0;
3827 int qp_idx = 0;
3828
3829 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3830 * group them so there are multiple queues per vector.
70114ec4
ASJ
3831 * It is also important to go through all the vectors available to be
3832 * sure that if we don't use all the vectors, that the remaining vectors
3833 * are cleared. This is especially important when decreasing the
3834 * number of queues in use.
41c445ff 3835 */
70114ec4 3836 for (; v_start < q_vectors; v_start++) {
cd0b6fa6
AD
3837 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3838
3839 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3840
3841 q_vector->num_ringpairs = num_ringpairs;
3842
3843 q_vector->rx.count = 0;
3844 q_vector->tx.count = 0;
3845 q_vector->rx.ring = NULL;
3846 q_vector->tx.ring = NULL;
3847
3848 while (num_ringpairs--) {
26cdc443 3849 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
cd0b6fa6
AD
3850 qp_idx++;
3851 qp_remaining--;
41c445ff
JB
3852 }
3853 }
3854}
3855
3856/**
3857 * i40e_vsi_request_irq - Request IRQ from the OS
3858 * @vsi: the VSI being configured
3859 * @basename: name for the vector
3860 **/
3861static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3862{
3863 struct i40e_pf *pf = vsi->back;
3864 int err;
3865
3866 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3867 err = i40e_vsi_request_irq_msix(vsi, basename);
3868 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3869 err = request_irq(pf->pdev->irq, i40e_intr, 0,
b294ac70 3870 pf->int_name, pf);
41c445ff
JB
3871 else
3872 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
b294ac70 3873 pf->int_name, pf);
41c445ff
JB
3874
3875 if (err)
3876 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3877
3878 return err;
3879}
3880
3881#ifdef CONFIG_NET_POLL_CONTROLLER
3882/**
d89d967f 3883 * i40e_netpoll - A Polling 'interrupt' handler
41c445ff
JB
3884 * @netdev: network interface device structure
3885 *
3886 * This is used by netconsole to send skbs without having to re-enable
3887 * interrupts. It's not called while the normal interrupt routine is executing.
3888 **/
38e00438
VD
3889#ifdef I40E_FCOE
3890void i40e_netpoll(struct net_device *netdev)
3891#else
41c445ff 3892static void i40e_netpoll(struct net_device *netdev)
38e00438 3893#endif
41c445ff
JB
3894{
3895 struct i40e_netdev_priv *np = netdev_priv(netdev);
3896 struct i40e_vsi *vsi = np->vsi;
3897 struct i40e_pf *pf = vsi->back;
3898 int i;
3899
3900 /* if interface is down do nothing */
3901 if (test_bit(__I40E_DOWN, &vsi->state))
3902 return;
3903
41c445ff
JB
3904 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3905 for (i = 0; i < vsi->num_q_vectors; i++)
493fb300 3906 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
41c445ff
JB
3907 } else {
3908 i40e_intr(pf->pdev->irq, netdev);
3909 }
41c445ff
JB
3910}
3911#endif
3912
23527308
NP
3913/**
3914 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
3915 * @pf: the PF being configured
3916 * @pf_q: the PF queue
3917 * @enable: enable or disable state of the queue
3918 *
3919 * This routine will wait for the given Tx queue of the PF to reach the
3920 * enabled or disabled state.
3921 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3922 * multiple retries; else will return 0 in case of success.
3923 **/
3924static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3925{
3926 int i;
3927 u32 tx_reg;
3928
3929 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3930 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
3931 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3932 break;
3933
f98a2006 3934 usleep_range(10, 20);
23527308
NP
3935 }
3936 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3937 return -ETIMEDOUT;
3938
3939 return 0;
3940}
3941
41c445ff
JB
3942/**
3943 * i40e_vsi_control_tx - Start or stop a VSI's rings
3944 * @vsi: the VSI being configured
3945 * @enable: start or stop the rings
3946 **/
3947static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3948{
3949 struct i40e_pf *pf = vsi->back;
3950 struct i40e_hw *hw = &pf->hw;
23527308 3951 int i, j, pf_q, ret = 0;
41c445ff
JB
3952 u32 tx_reg;
3953
3954 pf_q = vsi->base_queue;
3955 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
351499ab
MJ
3956
3957 /* warn the TX unit of coming changes */
3958 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
3959 if (!enable)
f98a2006 3960 usleep_range(10, 20);
351499ab 3961
6c5ef620 3962 for (j = 0; j < 50; j++) {
41c445ff 3963 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
6c5ef620
MW
3964 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3965 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3966 break;
3967 usleep_range(1000, 2000);
3968 }
fda972f6 3969 /* Skip if the queue is already in the requested state */
7c122007 3970 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
fda972f6 3971 continue;
41c445ff
JB
3972
3973 /* turn on/off the queue */
c5c9eb9e
SN
3974 if (enable) {
3975 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
6c5ef620 3976 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
c5c9eb9e 3977 } else {
41c445ff 3978 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
c5c9eb9e 3979 }
41c445ff
JB
3980
3981 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
69129dc3
NP
3982 /* No waiting for the Tx queue to disable */
3983 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
3984 continue;
41c445ff
JB
3985
3986 /* wait for the change to finish */
23527308
NP
3987 ret = i40e_pf_txq_wait(pf, pf_q, enable);
3988 if (ret) {
3989 dev_info(&pf->pdev->dev,
fb43201f
SN
3990 "VSI seid %d Tx ring %d %sable timeout\n",
3991 vsi->seid, pf_q, (enable ? "en" : "dis"));
23527308 3992 break;
41c445ff
JB
3993 }
3994 }
3995
7134f9ce
JB
3996 if (hw->revision_id == 0)
3997 mdelay(50);
23527308
NP
3998 return ret;
3999}
4000
4001/**
4002 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4003 * @pf: the PF being configured
4004 * @pf_q: the PF queue
4005 * @enable: enable or disable state of the queue
4006 *
4007 * This routine will wait for the given Rx queue of the PF to reach the
4008 * enabled or disabled state.
4009 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4010 * multiple retries; else will return 0 in case of success.
4011 **/
4012static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4013{
4014 int i;
4015 u32 rx_reg;
4016
4017 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4018 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4019 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4020 break;
4021
f98a2006 4022 usleep_range(10, 20);
23527308
NP
4023 }
4024 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4025 return -ETIMEDOUT;
7134f9ce 4026
41c445ff
JB
4027 return 0;
4028}
4029
4030/**
4031 * i40e_vsi_control_rx - Start or stop a VSI's rings
4032 * @vsi: the VSI being configured
4033 * @enable: start or stop the rings
4034 **/
4035static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4036{
4037 struct i40e_pf *pf = vsi->back;
4038 struct i40e_hw *hw = &pf->hw;
23527308 4039 int i, j, pf_q, ret = 0;
41c445ff
JB
4040 u32 rx_reg;
4041
4042 pf_q = vsi->base_queue;
4043 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
6c5ef620 4044 for (j = 0; j < 50; j++) {
41c445ff 4045 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
6c5ef620
MW
4046 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4047 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4048 break;
4049 usleep_range(1000, 2000);
4050 }
41c445ff 4051
7c122007
CS
4052 /* Skip if the queue is already in the requested state */
4053 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4054 continue;
41c445ff
JB
4055
4056 /* turn on/off the queue */
4057 if (enable)
6c5ef620 4058 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
41c445ff 4059 else
6c5ef620 4060 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
41c445ff 4061 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
3fe06f41
NP
4062 /* No waiting for the Tx queue to disable */
4063 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
4064 continue;
41c445ff
JB
4065
4066 /* wait for the change to finish */
23527308
NP
4067 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4068 if (ret) {
4069 dev_info(&pf->pdev->dev,
fb43201f
SN
4070 "VSI seid %d Rx ring %d %sable timeout\n",
4071 vsi->seid, pf_q, (enable ? "en" : "dis"));
23527308 4072 break;
41c445ff
JB
4073 }
4074 }
4075
23527308 4076 return ret;
41c445ff
JB
4077}
4078
4079/**
3aa7b74d 4080 * i40e_vsi_start_rings - Start a VSI's rings
41c445ff 4081 * @vsi: the VSI being configured
41c445ff 4082 **/
3aa7b74d 4083int i40e_vsi_start_rings(struct i40e_vsi *vsi)
41c445ff 4084{
3b867b28 4085 int ret = 0;
41c445ff
JB
4086
4087 /* do rx first for enable and last for disable */
3aa7b74d
FS
4088 ret = i40e_vsi_control_rx(vsi, true);
4089 if (ret)
4090 return ret;
4091 ret = i40e_vsi_control_tx(vsi, true);
41c445ff
JB
4092
4093 return ret;
4094}
4095
3aa7b74d
FS
4096/**
4097 * i40e_vsi_stop_rings - Stop a VSI's rings
4098 * @vsi: the VSI being configured
4099 **/
4100void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4101{
4102 /* do rx first for enable and last for disable
4103 * Ignore return value, we need to shutdown whatever we can
4104 */
4105 i40e_vsi_control_tx(vsi, false);
4106 i40e_vsi_control_rx(vsi, false);
4107}
4108
41c445ff
JB
4109/**
4110 * i40e_vsi_free_irq - Free the irq association with the OS
4111 * @vsi: the VSI being configured
4112 **/
4113static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4114{
4115 struct i40e_pf *pf = vsi->back;
4116 struct i40e_hw *hw = &pf->hw;
4117 int base = vsi->base_vector;
4118 u32 val, qp;
4119 int i;
4120
4121 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4122 if (!vsi->q_vectors)
4123 return;
4124
63741846
SN
4125 if (!vsi->irqs_ready)
4126 return;
4127
4128 vsi->irqs_ready = false;
41c445ff 4129 for (i = 0; i < vsi->num_q_vectors; i++) {
96db776a
AB
4130 int irq_num;
4131 u16 vector;
4132
4133 vector = i + base;
4134 irq_num = pf->msix_entries[vector].vector;
41c445ff
JB
4135
4136 /* free only the irqs that were actually requested */
78681b1f
SN
4137 if (!vsi->q_vectors[i] ||
4138 !vsi->q_vectors[i]->num_ringpairs)
41c445ff
JB
4139 continue;
4140
96db776a
AB
4141 /* clear the affinity notifier in the IRQ descriptor */
4142 irq_set_affinity_notifier(irq_num, NULL);
41c445ff 4143 /* clear the affinity_mask in the IRQ descriptor */
96db776a
AB
4144 irq_set_affinity_hint(irq_num, NULL);
4145 synchronize_irq(irq_num);
4146 free_irq(irq_num, vsi->q_vectors[i]);
41c445ff
JB
4147
4148 /* Tear down the interrupt queue link list
4149 *
4150 * We know that they come in pairs and always
4151 * the Rx first, then the Tx. To clear the
4152 * link list, stick the EOL value into the
4153 * next_q field of the registers.
4154 */
4155 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4156 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4157 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4158 val |= I40E_QUEUE_END_OF_LIST
4159 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4160 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4161
4162 while (qp != I40E_QUEUE_END_OF_LIST) {
4163 u32 next;
4164
4165 val = rd32(hw, I40E_QINT_RQCTL(qp));
4166
4167 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4168 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4169 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4170 I40E_QINT_RQCTL_INTEVENT_MASK);
4171
4172 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4173 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4174
4175 wr32(hw, I40E_QINT_RQCTL(qp), val);
4176
4177 val = rd32(hw, I40E_QINT_TQCTL(qp));
4178
4179 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4180 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4181
4182 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4183 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4184 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4185 I40E_QINT_TQCTL_INTEVENT_MASK);
4186
4187 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4188 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4189
4190 wr32(hw, I40E_QINT_TQCTL(qp), val);
4191 qp = next;
4192 }
4193 }
4194 } else {
4195 free_irq(pf->pdev->irq, pf);
4196
4197 val = rd32(hw, I40E_PFINT_LNKLST0);
4198 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4199 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4200 val |= I40E_QUEUE_END_OF_LIST
4201 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4202 wr32(hw, I40E_PFINT_LNKLST0, val);
4203
4204 val = rd32(hw, I40E_QINT_RQCTL(qp));
4205 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4206 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4207 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4208 I40E_QINT_RQCTL_INTEVENT_MASK);
4209
4210 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4211 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4212
4213 wr32(hw, I40E_QINT_RQCTL(qp), val);
4214
4215 val = rd32(hw, I40E_QINT_TQCTL(qp));
4216
4217 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4218 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4219 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4220 I40E_QINT_TQCTL_INTEVENT_MASK);
4221
4222 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4223 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4224
4225 wr32(hw, I40E_QINT_TQCTL(qp), val);
4226 }
4227}
4228
493fb300
AD
4229/**
4230 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4231 * @vsi: the VSI being configured
4232 * @v_idx: Index of vector to be freed
4233 *
4234 * This function frees the memory allocated to the q_vector. In addition if
4235 * NAPI is enabled it will delete any references to the NAPI struct prior
4236 * to freeing the q_vector.
4237 **/
4238static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4239{
4240 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
cd0b6fa6 4241 struct i40e_ring *ring;
493fb300
AD
4242
4243 if (!q_vector)
4244 return;
4245
4246 /* disassociate q_vector from rings */
cd0b6fa6
AD
4247 i40e_for_each_ring(ring, q_vector->tx)
4248 ring->q_vector = NULL;
4249
4250 i40e_for_each_ring(ring, q_vector->rx)
4251 ring->q_vector = NULL;
493fb300
AD
4252
4253 /* only VSI w/ an associated netdev is set up w/ NAPI */
4254 if (vsi->netdev)
4255 netif_napi_del(&q_vector->napi);
4256
4257 vsi->q_vectors[v_idx] = NULL;
4258
4259 kfree_rcu(q_vector, rcu);
4260}
4261
41c445ff
JB
4262/**
4263 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4264 * @vsi: the VSI being un-configured
4265 *
4266 * This frees the memory allocated to the q_vectors and
4267 * deletes references to the NAPI struct.
4268 **/
4269static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4270{
4271 int v_idx;
4272
493fb300
AD
4273 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4274 i40e_free_q_vector(vsi, v_idx);
41c445ff
JB
4275}
4276
4277/**
4278 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4279 * @pf: board private structure
4280 **/
4281static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4282{
4283 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4284 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4285 pci_disable_msix(pf->pdev);
4286 kfree(pf->msix_entries);
4287 pf->msix_entries = NULL;
3b444399
SN
4288 kfree(pf->irq_pile);
4289 pf->irq_pile = NULL;
41c445ff
JB
4290 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4291 pci_disable_msi(pf->pdev);
4292 }
4293 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4294}
4295
4296/**
4297 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4298 * @pf: board private structure
4299 *
4300 * We go through and clear interrupt specific resources and reset the structure
4301 * to pre-load conditions
4302 **/
4303static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4304{
4305 int i;
4306
e147758d 4307 i40e_stop_misc_vector(pf);
69278398 4308 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
e147758d
SN
4309 synchronize_irq(pf->msix_entries[0].vector);
4310 free_irq(pf->msix_entries[0].vector, pf);
4311 }
4312
e3219ce6
ASJ
4313 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4314 I40E_IWARP_IRQ_PILE_ID);
4315
41c445ff 4316 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
505682cd 4317 for (i = 0; i < pf->num_alloc_vsi; i++)
41c445ff
JB
4318 if (pf->vsi[i])
4319 i40e_vsi_free_q_vectors(pf->vsi[i]);
4320 i40e_reset_interrupt_capability(pf);
4321}
4322
4323/**
4324 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4325 * @vsi: the VSI being configured
4326 **/
4327static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4328{
4329 int q_idx;
4330
4331 if (!vsi->netdev)
4332 return;
4333
4334 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
493fb300 4335 napi_enable(&vsi->q_vectors[q_idx]->napi);
41c445ff
JB
4336}
4337
4338/**
4339 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4340 * @vsi: the VSI being configured
4341 **/
4342static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4343{
4344 int q_idx;
4345
4346 if (!vsi->netdev)
4347 return;
4348
4349 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
493fb300 4350 napi_disable(&vsi->q_vectors[q_idx]->napi);
41c445ff
JB
4351}
4352
90ef8d47
SN
4353/**
4354 * i40e_vsi_close - Shut down a VSI
4355 * @vsi: the vsi to be quelled
4356 **/
4357static void i40e_vsi_close(struct i40e_vsi *vsi)
4358{
e3219ce6
ASJ
4359 bool reset = false;
4360
90ef8d47
SN
4361 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
4362 i40e_down(vsi);
4363 i40e_vsi_free_irq(vsi);
4364 i40e_vsi_free_tx_resources(vsi);
4365 i40e_vsi_free_rx_resources(vsi);
92faef85 4366 vsi->current_netdev_flags = 0;
e3219ce6
ASJ
4367 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4368 reset = true;
4369 i40e_notify_client_of_netdev_close(vsi, reset);
90ef8d47
SN
4370}
4371
41c445ff
JB
4372/**
4373 * i40e_quiesce_vsi - Pause a given VSI
4374 * @vsi: the VSI being paused
4375 **/
4376static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4377{
4378 if (test_bit(__I40E_DOWN, &vsi->state))
4379 return;
4380
d341b7a5
NP
4381 /* No need to disable FCoE VSI when Tx suspended */
4382 if ((test_bit(__I40E_PORT_TX_SUSPENDED, &vsi->back->state)) &&
4383 vsi->type == I40E_VSI_FCOE) {
4384 dev_dbg(&vsi->back->pdev->dev,
fb43201f 4385 "VSI seid %d skipping FCoE VSI disable\n", vsi->seid);
d341b7a5
NP
4386 return;
4387 }
4388
41c445ff 4389 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
6995b36c 4390 if (vsi->netdev && netif_running(vsi->netdev))
41c445ff 4391 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
6995b36c 4392 else
90ef8d47 4393 i40e_vsi_close(vsi);
41c445ff
JB
4394}
4395
4396/**
4397 * i40e_unquiesce_vsi - Resume a given VSI
4398 * @vsi: the VSI being resumed
4399 **/
4400static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4401{
4402 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
4403 return;
4404
4405 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
4406 if (vsi->netdev && netif_running(vsi->netdev))
4407 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4408 else
8276f757 4409 i40e_vsi_open(vsi); /* this clears the DOWN bit */
41c445ff
JB
4410}
4411
4412/**
4413 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4414 * @pf: the PF
4415 **/
4416static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4417{
4418 int v;
4419
505682cd 4420 for (v = 0; v < pf->num_alloc_vsi; v++) {
41c445ff
JB
4421 if (pf->vsi[v])
4422 i40e_quiesce_vsi(pf->vsi[v]);
4423 }
4424}
4425
4426/**
4427 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4428 * @pf: the PF
4429 **/
4430static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4431{
4432 int v;
4433
505682cd 4434 for (v = 0; v < pf->num_alloc_vsi; v++) {
41c445ff
JB
4435 if (pf->vsi[v])
4436 i40e_unquiesce_vsi(pf->vsi[v]);
4437 }
4438}
4439
69129dc3
NP
4440#ifdef CONFIG_I40E_DCB
4441/**
3fe06f41 4442 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
69129dc3
NP
4443 * @vsi: the VSI being configured
4444 *
3fe06f41 4445 * This function waits for the given VSI's queues to be disabled.
69129dc3 4446 **/
3fe06f41 4447static int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
69129dc3
NP
4448{
4449 struct i40e_pf *pf = vsi->back;
4450 int i, pf_q, ret;
4451
4452 pf_q = vsi->base_queue;
4453 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4454 /* Check and wait for the disable status of the queue */
4455 ret = i40e_pf_txq_wait(pf, pf_q, false);
4456 if (ret) {
4457 dev_info(&pf->pdev->dev,
fb43201f
SN
4458 "VSI seid %d Tx ring %d disable timeout\n",
4459 vsi->seid, pf_q);
69129dc3
NP
4460 return ret;
4461 }
4462 }
4463
3fe06f41
NP
4464 pf_q = vsi->base_queue;
4465 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4466 /* Check and wait for the disable status of the queue */
4467 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4468 if (ret) {
4469 dev_info(&pf->pdev->dev,
4470 "VSI seid %d Rx ring %d disable timeout\n",
4471 vsi->seid, pf_q);
4472 return ret;
4473 }
4474 }
4475
69129dc3
NP
4476 return 0;
4477}
4478
4479/**
3fe06f41 4480 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
69129dc3
NP
4481 * @pf: the PF
4482 *
3fe06f41 4483 * This function waits for the queues to be in disabled state for all the
69129dc3
NP
4484 * VSIs that are managed by this PF.
4485 **/
3fe06f41 4486static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
69129dc3
NP
4487{
4488 int v, ret = 0;
4489
4490 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
d341b7a5
NP
4491 /* No need to wait for FCoE VSI queues */
4492 if (pf->vsi[v] && pf->vsi[v]->type != I40E_VSI_FCOE) {
3fe06f41 4493 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
69129dc3
NP
4494 if (ret)
4495 break;
4496 }
4497 }
4498
4499 return ret;
4500}
4501
4502#endif
b03a8c1f
KP
4503
4504/**
4505 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4506 * @q_idx: TX queue number
4507 * @vsi: Pointer to VSI struct
4508 *
4509 * This function checks specified queue for given VSI. Detects hung condition.
4510 * Sets hung bit since it is two step process. Before next run of service task
4511 * if napi_poll runs, it reset 'hung' bit for respective q_vector. If not,
4512 * hung condition remain unchanged and during subsequent run, this function
4513 * issues SW interrupt to recover from hung condition.
4514 **/
4515static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4516{
4517 struct i40e_ring *tx_ring = NULL;
4518 struct i40e_pf *pf;
dd353109 4519 u32 head, val, tx_pending_hw;
b03a8c1f
KP
4520 int i;
4521
4522 pf = vsi->back;
4523
4524 /* now that we have an index, find the tx_ring struct */
4525 for (i = 0; i < vsi->num_queue_pairs; i++) {
4526 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4527 if (q_idx == vsi->tx_rings[i]->queue_index) {
4528 tx_ring = vsi->tx_rings[i];
4529 break;
4530 }
4531 }
4532 }
4533
4534 if (!tx_ring)
4535 return;
4536
4537 /* Read interrupt register */
4538 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4539 val = rd32(&pf->hw,
4540 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4541 tx_ring->vsi->base_vector - 1));
4542 else
4543 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4544
4545 head = i40e_get_head(tx_ring);
4546
dd353109 4547 tx_pending_hw = i40e_get_tx_pending(tx_ring, false);
b03a8c1f 4548
9c6c1259
KP
4549 /* HW is done executing descriptors, updated HEAD write back,
4550 * but SW hasn't processed those descriptors. If interrupt is
4551 * not generated from this point ON, it could result into
4552 * dev_watchdog detecting timeout on those netdev_queue,
4553 * hence proactively trigger SW interrupt.
b03a8c1f 4554 */
dd353109 4555 if (tx_pending_hw && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
9c6c1259
KP
4556 /* NAPI Poll didn't run and clear since it was set */
4557 if (test_and_clear_bit(I40E_Q_VECTOR_HUNG_DETECT,
4558 &tx_ring->q_vector->hung_detected)) {
dd353109
ASJ
4559 netdev_info(vsi->netdev, "VSI_seid %d, Hung TX queue %d, tx_pending_hw: %d, NTC:0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x\n",
4560 vsi->seid, q_idx, tx_pending_hw,
9c6c1259
KP
4561 tx_ring->next_to_clean, head,
4562 tx_ring->next_to_use,
4563 readl(tx_ring->tail));
4564 netdev_info(vsi->netdev, "VSI_seid %d, Issuing force_wb for TX queue %d, Interrupt Reg: 0x%x\n",
4565 vsi->seid, q_idx, val);
4566 i40e_force_wb(vsi, tx_ring->q_vector);
4567 } else {
4568 /* First Chance - detected possible hung */
4569 set_bit(I40E_Q_VECTOR_HUNG_DETECT,
4570 &tx_ring->q_vector->hung_detected);
4571 }
4572 }
dd353109
ASJ
4573
4574 /* This is the case where we have interrupts missing,
4575 * so the tx_pending in HW will most likely be 0, but we
4576 * will have tx_pending in SW since the WB happened but the
4577 * interrupt got lost.
4578 */
4579 if ((!tx_pending_hw) && i40e_get_tx_pending(tx_ring, true) &&
4580 (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
4581 if (napi_reschedule(&tx_ring->q_vector->napi))
4582 tx_ring->tx_stats.tx_lost_interrupt++;
4583 }
b03a8c1f
KP
4584}
4585
4586/**
4587 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4588 * @pf: pointer to PF struct
4589 *
4590 * LAN VSI has netdev and netdev has TX queues. This function is to check
4591 * each of those TX queues if they are hung, trigger recovery by issuing
4592 * SW interrupt.
4593 **/
4594static void i40e_detect_recover_hung(struct i40e_pf *pf)
4595{
4596 struct net_device *netdev;
4597 struct i40e_vsi *vsi;
4598 int i;
4599
4600 /* Only for LAN VSI */
4601 vsi = pf->vsi[pf->lan_vsi];
4602
4603 if (!vsi)
4604 return;
4605
4606 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
4607 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
4608 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4609 return;
4610
4611 /* Make sure type is MAIN VSI */
4612 if (vsi->type != I40E_VSI_MAIN)
4613 return;
4614
4615 netdev = vsi->netdev;
4616 if (!netdev)
4617 return;
4618
4619 /* Bail out if netif_carrier is not OK */
4620 if (!netif_carrier_ok(netdev))
4621 return;
4622
4623 /* Go thru' TX queues for netdev */
4624 for (i = 0; i < netdev->num_tx_queues; i++) {
4625 struct netdev_queue *q;
4626
4627 q = netdev_get_tx_queue(netdev, i);
4628 if (q)
4629 i40e_detect_recover_hung_queue(i, vsi);
4630 }
4631}
4632
63d7e5a4
NP
4633/**
4634 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
b40c82e6 4635 * @pf: pointer to PF
63d7e5a4
NP
4636 *
4637 * Get TC map for ISCSI PF type that will include iSCSI TC
4638 * and LAN TC.
4639 **/
4640static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4641{
4642 struct i40e_dcb_app_priority_table app;
4643 struct i40e_hw *hw = &pf->hw;
4644 u8 enabled_tc = 1; /* TC0 is always enabled */
4645 u8 tc, i;
4646 /* Get the iSCSI APP TLV */
4647 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4648
4649 for (i = 0; i < dcbcfg->numapps; i++) {
4650 app = dcbcfg->app[i];
4651 if (app.selector == I40E_APP_SEL_TCPIP &&
4652 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4653 tc = dcbcfg->etscfg.prioritytable[app.priority];
75f5cea9 4654 enabled_tc |= BIT(tc);
63d7e5a4
NP
4655 break;
4656 }
4657 }
4658
4659 return enabled_tc;
4660}
4661
41c445ff
JB
4662/**
4663 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4664 * @dcbcfg: the corresponding DCBx configuration structure
4665 *
4666 * Return the number of TCs from given DCBx configuration
4667 **/
4668static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4669{
fbfe12c6 4670 int i, tc_unused = 0;
078b5876 4671 u8 num_tc = 0;
fbfe12c6 4672 u8 ret = 0;
41c445ff
JB
4673
4674 /* Scan the ETS Config Priority Table to find
4675 * traffic class enabled for a given priority
fbfe12c6 4676 * and create a bitmask of enabled TCs
41c445ff 4677 */
fbfe12c6
DE
4678 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4679 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
41c445ff 4680
fbfe12c6
DE
4681 /* Now scan the bitmask to check for
4682 * contiguous TCs starting with TC0
41c445ff 4683 */
fbfe12c6
DE
4684 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4685 if (num_tc & BIT(i)) {
4686 if (!tc_unused) {
4687 ret++;
4688 } else {
4689 pr_err("Non-contiguous TC - Disabling DCB\n");
4690 return 1;
4691 }
4692 } else {
4693 tc_unused = 1;
4694 }
4695 }
4696
4697 /* There is always at least TC0 */
4698 if (!ret)
4699 ret = 1;
4700
4701 return ret;
41c445ff
JB
4702}
4703
4704/**
4705 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4706 * @dcbcfg: the corresponding DCBx configuration structure
4707 *
4708 * Query the current DCB configuration and return the number of
4709 * traffic classes enabled from the given DCBX config
4710 **/
4711static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4712{
4713 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4714 u8 enabled_tc = 1;
4715 u8 i;
4716
4717 for (i = 0; i < num_tc; i++)
41a1d04b 4718 enabled_tc |= BIT(i);
41c445ff
JB
4719
4720 return enabled_tc;
4721}
4722
4723/**
4724 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4725 * @pf: PF being queried
4726 *
4727 * Return number of traffic classes enabled for the given PF
4728 **/
4729static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4730{
4731 struct i40e_hw *hw = &pf->hw;
52a08caa 4732 u8 i, enabled_tc = 1;
41c445ff
JB
4733 u8 num_tc = 0;
4734 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4735
4736 /* If DCB is not enabled then always in single TC */
4737 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4738 return 1;
4739
63d7e5a4
NP
4740 /* SFP mode will be enabled for all TCs on port */
4741 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4742 return i40e_dcb_get_num_tc(dcbcfg);
4743
41c445ff 4744 /* MFP mode return count of enabled TCs for this PF */
63d7e5a4
NP
4745 if (pf->hw.func_caps.iscsi)
4746 enabled_tc = i40e_get_iscsi_tc_map(pf);
4747 else
fc51de96 4748 return 1; /* Only TC0 */
41c445ff 4749
63d7e5a4 4750 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
75f5cea9 4751 if (enabled_tc & BIT(i))
63d7e5a4
NP
4752 num_tc++;
4753 }
4754 return num_tc;
41c445ff
JB
4755}
4756
41c445ff
JB
4757/**
4758 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4759 * @pf: PF being queried
4760 *
4761 * Return a bitmap for enabled traffic classes for this PF.
4762 **/
4763static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4764{
4765 /* If DCB is not enabled for this PF then just return default TC */
4766 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
ea6acb7e 4767 return I40E_DEFAULT_TRAFFIC_CLASS;
41c445ff 4768
41c445ff 4769 /* SFP mode we want PF to be enabled for all TCs */
63d7e5a4
NP
4770 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4771 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4772
fc51de96 4773 /* MFP enabled and iSCSI PF type */
63d7e5a4
NP
4774 if (pf->hw.func_caps.iscsi)
4775 return i40e_get_iscsi_tc_map(pf);
4776 else
ea6acb7e 4777 return I40E_DEFAULT_TRAFFIC_CLASS;
41c445ff
JB
4778}
4779
4780/**
4781 * i40e_vsi_get_bw_info - Query VSI BW Information
4782 * @vsi: the VSI being queried
4783 *
4784 * Returns 0 on success, negative value on failure
4785 **/
4786static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4787{
4788 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4789 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4790 struct i40e_pf *pf = vsi->back;
4791 struct i40e_hw *hw = &pf->hw;
f1c7e72e 4792 i40e_status ret;
41c445ff 4793 u32 tc_bw_max;
41c445ff
JB
4794 int i;
4795
4796 /* Get the VSI level BW configuration */
f1c7e72e
SN
4797 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4798 if (ret) {
41c445ff 4799 dev_info(&pf->pdev->dev,
f1c7e72e
SN
4800 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4801 i40e_stat_str(&pf->hw, ret),
4802 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
dcae29be 4803 return -EINVAL;
41c445ff
JB
4804 }
4805
4806 /* Get the VSI level BW configuration per TC */
f1c7e72e
SN
4807 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4808 NULL);
4809 if (ret) {
41c445ff 4810 dev_info(&pf->pdev->dev,
f1c7e72e
SN
4811 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4812 i40e_stat_str(&pf->hw, ret),
4813 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
dcae29be 4814 return -EINVAL;
41c445ff
JB
4815 }
4816
4817 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4818 dev_info(&pf->pdev->dev,
4819 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4820 bw_config.tc_valid_bits,
4821 bw_ets_config.tc_valid_bits);
4822 /* Still continuing */
4823 }
4824
4825 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4826 vsi->bw_max_quanta = bw_config.max_bw;
4827 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4828 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4829 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4830 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4831 vsi->bw_ets_limit_credits[i] =
4832 le16_to_cpu(bw_ets_config.credits[i]);
4833 /* 3 bits out of 4 for each TC */
4834 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4835 }
078b5876 4836
dcae29be 4837 return 0;
41c445ff
JB
4838}
4839
4840/**
4841 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4842 * @vsi: the VSI being configured
4843 * @enabled_tc: TC bitmap
4844 * @bw_credits: BW shared credits per TC
4845 *
4846 * Returns 0 on success, negative value on failure
4847 **/
dcae29be 4848static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
41c445ff
JB
4849 u8 *bw_share)
4850{
4851 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
f1c7e72e 4852 i40e_status ret;
dcae29be 4853 int i;
41c445ff
JB
4854
4855 bw_data.tc_valid_bits = enabled_tc;
4856 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4857 bw_data.tc_bw_credits[i] = bw_share[i];
4858
f1c7e72e
SN
4859 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4860 NULL);
4861 if (ret) {
41c445ff 4862 dev_info(&vsi->back->pdev->dev,
69bfb110
JB
4863 "AQ command Config VSI BW allocation per TC failed = %d\n",
4864 vsi->back->hw.aq.asq_last_status);
dcae29be 4865 return -EINVAL;
41c445ff
JB
4866 }
4867
4868 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4869 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
4870
dcae29be 4871 return 0;
41c445ff
JB
4872}
4873
4874/**
4875 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
4876 * @vsi: the VSI being configured
4877 * @enabled_tc: TC map to be enabled
4878 *
4879 **/
4880static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4881{
4882 struct net_device *netdev = vsi->netdev;
4883 struct i40e_pf *pf = vsi->back;
4884 struct i40e_hw *hw = &pf->hw;
4885 u8 netdev_tc = 0;
4886 int i;
4887 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4888
4889 if (!netdev)
4890 return;
4891
4892 if (!enabled_tc) {
4893 netdev_reset_tc(netdev);
4894 return;
4895 }
4896
4897 /* Set up actual enabled TCs on the VSI */
4898 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
4899 return;
4900
4901 /* set per TC queues for the VSI */
4902 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4903 /* Only set TC queues for enabled tcs
4904 *
4905 * e.g. For a VSI that has TC0 and TC3 enabled the
4906 * enabled_tc bitmap would be 0x00001001; the driver
4907 * will set the numtc for netdev as 2 that will be
4908 * referenced by the netdev layer as TC 0 and 1.
4909 */
75f5cea9 4910 if (vsi->tc_config.enabled_tc & BIT(i))
41c445ff
JB
4911 netdev_set_tc_queue(netdev,
4912 vsi->tc_config.tc_info[i].netdev_tc,
4913 vsi->tc_config.tc_info[i].qcount,
4914 vsi->tc_config.tc_info[i].qoffset);
4915 }
4916
4917 /* Assign UP2TC map for the VSI */
4918 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4919 /* Get the actual TC# for the UP */
4920 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
4921 /* Get the mapped netdev TC# for the UP */
4922 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
4923 netdev_set_prio_tc_map(netdev, i, netdev_tc);
4924 }
4925}
4926
4927/**
4928 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
4929 * @vsi: the VSI being configured
4930 * @ctxt: the ctxt buffer returned from AQ VSI update param command
4931 **/
4932static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
4933 struct i40e_vsi_context *ctxt)
4934{
4935 /* copy just the sections touched not the entire info
4936 * since not all sections are valid as returned by
4937 * update vsi params
4938 */
4939 vsi->info.mapping_flags = ctxt->info.mapping_flags;
4940 memcpy(&vsi->info.queue_mapping,
4941 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
4942 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
4943 sizeof(vsi->info.tc_mapping));
4944}
4945
4946/**
4947 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
4948 * @vsi: VSI to be configured
4949 * @enabled_tc: TC bitmap
4950 *
4951 * This configures a particular VSI for TCs that are mapped to the
4952 * given TC bitmap. It uses default bandwidth share for TCs across
4953 * VSIs to configure TC for a particular VSI.
4954 *
4955 * NOTE:
4956 * It is expected that the VSI queues have been quisced before calling
4957 * this function.
4958 **/
4959static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4960{
4961 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
4962 struct i40e_vsi_context ctxt;
4963 int ret = 0;
4964 int i;
4965
4966 /* Check if enabled_tc is same as existing or new TCs */
4967 if (vsi->tc_config.enabled_tc == enabled_tc)
4968 return ret;
4969
4970 /* Enable ETS TCs with equal BW Share for now across all VSIs */
4971 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
75f5cea9 4972 if (enabled_tc & BIT(i))
41c445ff
JB
4973 bw_share[i] = 1;
4974 }
4975
4976 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
4977 if (ret) {
4978 dev_info(&vsi->back->pdev->dev,
4979 "Failed configuring TC map %d for VSI %d\n",
4980 enabled_tc, vsi->seid);
4981 goto out;
4982 }
4983
4984 /* Update Queue Pairs Mapping for currently enabled UPs */
4985 ctxt.seid = vsi->seid;
4986 ctxt.pf_num = vsi->back->hw.pf_id;
4987 ctxt.vf_num = 0;
4988 ctxt.uplink_seid = vsi->uplink_seid;
1a2f6248 4989 ctxt.info = vsi->info;
41c445ff
JB
4990 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
4991
e3219ce6
ASJ
4992 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
4993 ctxt.info.valid_sections |=
4994 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
4995 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
4996 }
4997
41c445ff
JB
4998 /* Update the VSI after updating the VSI queue-mapping information */
4999 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
5000 if (ret) {
5001 dev_info(&vsi->back->pdev->dev,
f1c7e72e
SN
5002 "Update vsi tc config failed, err %s aq_err %s\n",
5003 i40e_stat_str(&vsi->back->hw, ret),
5004 i40e_aq_str(&vsi->back->hw,
5005 vsi->back->hw.aq.asq_last_status));
41c445ff
JB
5006 goto out;
5007 }
5008 /* update the local VSI info with updated queue map */
5009 i40e_vsi_update_queue_map(vsi, &ctxt);
5010 vsi->info.valid_sections = 0;
5011
5012 /* Update current VSI BW information */
5013 ret = i40e_vsi_get_bw_info(vsi);
5014 if (ret) {
5015 dev_info(&vsi->back->pdev->dev,
f1c7e72e
SN
5016 "Failed updating vsi bw info, err %s aq_err %s\n",
5017 i40e_stat_str(&vsi->back->hw, ret),
5018 i40e_aq_str(&vsi->back->hw,
5019 vsi->back->hw.aq.asq_last_status));
41c445ff
JB
5020 goto out;
5021 }
5022
5023 /* Update the netdev TC setup */
5024 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5025out:
5026 return ret;
5027}
5028
4e3b35b0
NP
5029/**
5030 * i40e_veb_config_tc - Configure TCs for given VEB
5031 * @veb: given VEB
5032 * @enabled_tc: TC bitmap
5033 *
5034 * Configures given TC bitmap for VEB (switching) element
5035 **/
5036int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
5037{
5038 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
5039 struct i40e_pf *pf = veb->pf;
5040 int ret = 0;
5041 int i;
5042
5043 /* No TCs or already enabled TCs just return */
5044 if (!enabled_tc || veb->enabled_tc == enabled_tc)
5045 return ret;
5046
5047 bw_data.tc_valid_bits = enabled_tc;
5048 /* bw_data.absolute_credits is not set (relative) */
5049
5050 /* Enable ETS TCs with equal BW Share for now */
5051 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
75f5cea9 5052 if (enabled_tc & BIT(i))
4e3b35b0
NP
5053 bw_data.tc_bw_share_credits[i] = 1;
5054 }
5055
5056 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
5057 &bw_data, NULL);
5058 if (ret) {
5059 dev_info(&pf->pdev->dev,
f1c7e72e
SN
5060 "VEB bw config failed, err %s aq_err %s\n",
5061 i40e_stat_str(&pf->hw, ret),
5062 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
4e3b35b0
NP
5063 goto out;
5064 }
5065
5066 /* Update the BW information */
5067 ret = i40e_veb_get_bw_info(veb);
5068 if (ret) {
5069 dev_info(&pf->pdev->dev,
f1c7e72e
SN
5070 "Failed getting veb bw config, err %s aq_err %s\n",
5071 i40e_stat_str(&pf->hw, ret),
5072 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
4e3b35b0
NP
5073 }
5074
5075out:
5076 return ret;
5077}
5078
5079#ifdef CONFIG_I40E_DCB
5080/**
5081 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
5082 * @pf: PF struct
5083 *
5084 * Reconfigure VEB/VSIs on a given PF; it is assumed that
5085 * the caller would've quiesce all the VSIs before calling
5086 * this function
5087 **/
5088static void i40e_dcb_reconfigure(struct i40e_pf *pf)
5089{
5090 u8 tc_map = 0;
5091 int ret;
5092 u8 v;
5093
5094 /* Enable the TCs available on PF to all VEBs */
5095 tc_map = i40e_pf_get_tc_map(pf);
5096 for (v = 0; v < I40E_MAX_VEB; v++) {
5097 if (!pf->veb[v])
5098 continue;
5099 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
5100 if (ret) {
5101 dev_info(&pf->pdev->dev,
5102 "Failed configuring TC for VEB seid=%d\n",
5103 pf->veb[v]->seid);
5104 /* Will try to configure as many components */
5105 }
5106 }
5107
5108 /* Update each VSI */
505682cd 5109 for (v = 0; v < pf->num_alloc_vsi; v++) {
4e3b35b0
NP
5110 if (!pf->vsi[v])
5111 continue;
5112
5113 /* - Enable all TCs for the LAN VSI
38e00438
VD
5114#ifdef I40E_FCOE
5115 * - For FCoE VSI only enable the TC configured
5116 * as per the APP TLV
5117#endif
4e3b35b0
NP
5118 * - For all others keep them at TC0 for now
5119 */
5120 if (v == pf->lan_vsi)
5121 tc_map = i40e_pf_get_tc_map(pf);
5122 else
ea6acb7e 5123 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
38e00438
VD
5124#ifdef I40E_FCOE
5125 if (pf->vsi[v]->type == I40E_VSI_FCOE)
5126 tc_map = i40e_get_fcoe_tc_map(pf);
5127#endif /* #ifdef I40E_FCOE */
4e3b35b0
NP
5128
5129 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
5130 if (ret) {
5131 dev_info(&pf->pdev->dev,
5132 "Failed configuring TC for VSI seid=%d\n",
5133 pf->vsi[v]->seid);
5134 /* Will try to configure as many components */
5135 } else {
0672a091
NP
5136 /* Re-configure VSI vectors based on updated TC map */
5137 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
4e3b35b0
NP
5138 if (pf->vsi[v]->netdev)
5139 i40e_dcbnl_set_all(pf->vsi[v]);
5140 }
5141 }
5142}
5143
2fd75f31
NP
5144/**
5145 * i40e_resume_port_tx - Resume port Tx
5146 * @pf: PF struct
5147 *
5148 * Resume a port's Tx and issue a PF reset in case of failure to
5149 * resume.
5150 **/
5151static int i40e_resume_port_tx(struct i40e_pf *pf)
5152{
5153 struct i40e_hw *hw = &pf->hw;
5154 int ret;
5155
5156 ret = i40e_aq_resume_port_tx(hw, NULL);
5157 if (ret) {
5158 dev_info(&pf->pdev->dev,
f1c7e72e
SN
5159 "Resume Port Tx failed, err %s aq_err %s\n",
5160 i40e_stat_str(&pf->hw, ret),
5161 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
2fd75f31
NP
5162 /* Schedule PF reset to recover */
5163 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5164 i40e_service_event_schedule(pf);
5165 }
5166
5167 return ret;
5168}
5169
4e3b35b0
NP
5170/**
5171 * i40e_init_pf_dcb - Initialize DCB configuration
5172 * @pf: PF being configured
5173 *
5174 * Query the current DCB configuration and cache it
5175 * in the hardware structure
5176 **/
5177static int i40e_init_pf_dcb(struct i40e_pf *pf)
5178{
5179 struct i40e_hw *hw = &pf->hw;
5180 int err = 0;
5181
025b4a54 5182 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
f1bbad33 5183 if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
025b4a54
ASJ
5184 goto out;
5185
4e3b35b0
NP
5186 /* Get the initial DCB configuration */
5187 err = i40e_init_dcb(hw);
5188 if (!err) {
5189 /* Device/Function is not DCBX capable */
5190 if ((!hw->func_caps.dcb) ||
5191 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
5192 dev_info(&pf->pdev->dev,
5193 "DCBX offload is not supported or is disabled for this PF.\n");
5194
5195 if (pf->flags & I40E_FLAG_MFP_ENABLED)
5196 goto out;
5197
5198 } else {
5199 /* When status is not DISABLED then DCBX in FW */
5200 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5201 DCB_CAP_DCBX_VER_IEEE;
4d9b6043
NP
5202
5203 pf->flags |= I40E_FLAG_DCB_CAPABLE;
a036244c
DE
5204 /* Enable DCB tagging only when more than one TC
5205 * or explicitly disable if only one TC
5206 */
4d9b6043
NP
5207 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5208 pf->flags |= I40E_FLAG_DCB_ENABLED;
a036244c
DE
5209 else
5210 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9fa61dd2
NP
5211 dev_dbg(&pf->pdev->dev,
5212 "DCBX offload is supported for this PF.\n");
4e3b35b0 5213 }
014269ff 5214 } else {
aebfc816 5215 dev_info(&pf->pdev->dev,
f1c7e72e
SN
5216 "Query for DCB configuration failed, err %s aq_err %s\n",
5217 i40e_stat_str(&pf->hw, err),
5218 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
4e3b35b0
NP
5219 }
5220
5221out:
5222 return err;
5223}
5224#endif /* CONFIG_I40E_DCB */
cf05ed08
JB
5225#define SPEED_SIZE 14
5226#define FC_SIZE 8
5227/**
5228 * i40e_print_link_message - print link up or down
5229 * @vsi: the VSI for which link needs a message
5230 */
c156f856 5231void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
cf05ed08 5232{
7ec9ba11 5233 enum i40e_aq_link_speed new_speed;
a9165490
SN
5234 char *speed = "Unknown";
5235 char *fc = "Unknown";
cf05ed08 5236
7ec9ba11
FS
5237 new_speed = vsi->back->hw.phy.link_info.link_speed;
5238
5239 if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
c156f856
MJ
5240 return;
5241 vsi->current_isup = isup;
7ec9ba11 5242 vsi->current_speed = new_speed;
cf05ed08
JB
5243 if (!isup) {
5244 netdev_info(vsi->netdev, "NIC Link is Down\n");
5245 return;
5246 }
5247
148c2d80
GR
5248 /* Warn user if link speed on NPAR enabled partition is not at
5249 * least 10GB
5250 */
5251 if (vsi->back->hw.func_caps.npar_enable &&
5252 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
5253 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
5254 netdev_warn(vsi->netdev,
5255 "The partition detected link speed that is less than 10Gbps\n");
5256
cf05ed08
JB
5257 switch (vsi->back->hw.phy.link_info.link_speed) {
5258 case I40E_LINK_SPEED_40GB:
a9165490 5259 speed = "40 G";
cf05ed08 5260 break;
ae24b409 5261 case I40E_LINK_SPEED_20GB:
a9165490 5262 speed = "20 G";
ae24b409 5263 break;
3123237a
CW
5264 case I40E_LINK_SPEED_25GB:
5265 speed = "25 G";
5266 break;
cf05ed08 5267 case I40E_LINK_SPEED_10GB:
a9165490 5268 speed = "10 G";
cf05ed08
JB
5269 break;
5270 case I40E_LINK_SPEED_1GB:
a9165490 5271 speed = "1000 M";
cf05ed08 5272 break;
5960d33f 5273 case I40E_LINK_SPEED_100MB:
a9165490 5274 speed = "100 M";
5960d33f 5275 break;
cf05ed08
JB
5276 default:
5277 break;
5278 }
5279
5280 switch (vsi->back->hw.fc.current_mode) {
5281 case I40E_FC_FULL:
a9165490 5282 fc = "RX/TX";
cf05ed08
JB
5283 break;
5284 case I40E_FC_TX_PAUSE:
a9165490 5285 fc = "TX";
cf05ed08
JB
5286 break;
5287 case I40E_FC_RX_PAUSE:
a9165490 5288 fc = "RX";
cf05ed08
JB
5289 break;
5290 default:
a9165490 5291 fc = "None";
cf05ed08
JB
5292 break;
5293 }
5294
a9165490 5295 netdev_info(vsi->netdev, "NIC Link is Up %sbps Full Duplex, Flow Control: %s\n",
cf05ed08
JB
5296 speed, fc);
5297}
4e3b35b0 5298
41c445ff
JB
5299/**
5300 * i40e_up_complete - Finish the last steps of bringing up a connection
5301 * @vsi: the VSI being configured
5302 **/
5303static int i40e_up_complete(struct i40e_vsi *vsi)
5304{
5305 struct i40e_pf *pf = vsi->back;
5306 int err;
5307
5308 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5309 i40e_vsi_configure_msix(vsi);
5310 else
5311 i40e_configure_msi_and_legacy(vsi);
5312
5313 /* start rings */
3aa7b74d 5314 err = i40e_vsi_start_rings(vsi);
41c445ff
JB
5315 if (err)
5316 return err;
5317
5318 clear_bit(__I40E_DOWN, &vsi->state);
5319 i40e_napi_enable_all(vsi);
5320 i40e_vsi_enable_irq(vsi);
5321
5322 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
5323 (vsi->netdev)) {
cf05ed08 5324 i40e_print_link_message(vsi, true);
41c445ff
JB
5325 netif_tx_start_all_queues(vsi->netdev);
5326 netif_carrier_on(vsi->netdev);
6d779b41 5327 } else if (vsi->netdev) {
cf05ed08 5328 i40e_print_link_message(vsi, false);
7b592f61
CW
5329 /* need to check for qualified module here*/
5330 if ((pf->hw.phy.link_info.link_info &
5331 I40E_AQ_MEDIA_AVAILABLE) &&
5332 (!(pf->hw.phy.link_info.an_info &
5333 I40E_AQ_QUALIFIED_MODULE)))
5334 netdev_err(vsi->netdev,
5335 "the driver failed to link because an unqualified module was detected.");
41c445ff 5336 }
ca64fa4e
ASJ
5337
5338 /* replay FDIR SB filters */
1e1be8f6
ASJ
5339 if (vsi->type == I40E_VSI_FDIR) {
5340 /* reset fd counters */
5341 pf->fd_add_err = pf->fd_atr_cnt = 0;
5342 if (pf->fd_tcp_rule > 0) {
234dc4e6 5343 pf->auto_disable_flags |= I40E_FLAG_FD_ATR_ENABLED;
2e4875e3
ASJ
5344 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5345 dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 exist\n");
1e1be8f6
ASJ
5346 pf->fd_tcp_rule = 0;
5347 }
ca64fa4e 5348 i40e_fdir_filter_restore(vsi);
1e1be8f6 5349 }
e3219ce6
ASJ
5350
5351 /* On the next run of the service_task, notify any clients of the new
5352 * opened netdev
5353 */
5354 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
41c445ff
JB
5355 i40e_service_event_schedule(pf);
5356
5357 return 0;
5358}
5359
5360/**
5361 * i40e_vsi_reinit_locked - Reset the VSI
5362 * @vsi: the VSI being configured
5363 *
5364 * Rebuild the ring structs after some configuration
5365 * has changed, e.g. MTU size.
5366 **/
5367static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
5368{
5369 struct i40e_pf *pf = vsi->back;
5370
5371 WARN_ON(in_interrupt());
5372 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
5373 usleep_range(1000, 2000);
5374 i40e_down(vsi);
5375
41c445ff
JB
5376 i40e_up(vsi);
5377 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
5378}
5379
5380/**
5381 * i40e_up - Bring the connection back up after being down
5382 * @vsi: the VSI being configured
5383 **/
5384int i40e_up(struct i40e_vsi *vsi)
5385{
5386 int err;
5387
5388 err = i40e_vsi_configure(vsi);
5389 if (!err)
5390 err = i40e_up_complete(vsi);
5391
5392 return err;
5393}
5394
5395/**
5396 * i40e_down - Shutdown the connection processing
5397 * @vsi: the VSI being stopped
5398 **/
5399void i40e_down(struct i40e_vsi *vsi)
5400{
5401 int i;
5402
5403 /* It is assumed that the caller of this function
5404 * sets the vsi->state __I40E_DOWN bit.
5405 */
5406 if (vsi->netdev) {
5407 netif_carrier_off(vsi->netdev);
5408 netif_tx_disable(vsi->netdev);
5409 }
5410 i40e_vsi_disable_irq(vsi);
3aa7b74d 5411 i40e_vsi_stop_rings(vsi);
41c445ff
JB
5412 i40e_napi_disable_all(vsi);
5413
5414 for (i = 0; i < vsi->num_queue_pairs; i++) {
9f65e15b
AD
5415 i40e_clean_tx_ring(vsi->tx_rings[i]);
5416 i40e_clean_rx_ring(vsi->rx_rings[i]);
41c445ff 5417 }
f980d445
CS
5418
5419 i40e_notify_client_of_netdev_close(vsi, false);
5420
41c445ff
JB
5421}
5422
5423/**
5424 * i40e_setup_tc - configure multiple traffic classes
5425 * @netdev: net device to configure
5426 * @tc: number of traffic classes to enable
5427 **/
5428static int i40e_setup_tc(struct net_device *netdev, u8 tc)
5429{
5430 struct i40e_netdev_priv *np = netdev_priv(netdev);
5431 struct i40e_vsi *vsi = np->vsi;
5432 struct i40e_pf *pf = vsi->back;
5433 u8 enabled_tc = 0;
5434 int ret = -EINVAL;
5435 int i;
5436
5437 /* Check if DCB enabled to continue */
5438 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5439 netdev_info(netdev, "DCB is not enabled for adapter\n");
5440 goto exit;
5441 }
5442
5443 /* Check if MFP enabled */
5444 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5445 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5446 goto exit;
5447 }
5448
5449 /* Check whether tc count is within enabled limit */
5450 if (tc > i40e_pf_get_num_tc(pf)) {
5451 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5452 goto exit;
5453 }
5454
5455 /* Generate TC map for number of tc requested */
5456 for (i = 0; i < tc; i++)
75f5cea9 5457 enabled_tc |= BIT(i);
41c445ff
JB
5458
5459 /* Requesting same TC configuration as already enabled */
5460 if (enabled_tc == vsi->tc_config.enabled_tc)
5461 return 0;
5462
5463 /* Quiesce VSI queues */
5464 i40e_quiesce_vsi(vsi);
5465
5466 /* Configure VSI for enabled TCs */
5467 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5468 if (ret) {
5469 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5470 vsi->seid);
5471 goto exit;
5472 }
5473
5474 /* Unquiesce VSI */
5475 i40e_unquiesce_vsi(vsi);
5476
5477exit:
5478 return ret;
5479}
5480
e4c6734e 5481#ifdef I40E_FCOE
16e5cc64
JF
5482int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5483 struct tc_to_netdev *tc)
e4c6734e 5484#else
16e5cc64
JF
5485static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5486 struct tc_to_netdev *tc)
e4c6734e
JF
5487#endif
5488{
16e5cc64 5489 if (handle != TC_H_ROOT || tc->type != TC_SETUP_MQPRIO)
e4c6734e 5490 return -EINVAL;
16e5cc64 5491 return i40e_setup_tc(netdev, tc->tc);
e4c6734e
JF
5492}
5493
41c445ff
JB
5494/**
5495 * i40e_open - Called when a network interface is made active
5496 * @netdev: network interface device structure
5497 *
5498 * The open entry point is called when a network interface is made
5499 * active by the system (IFF_UP). At this point all resources needed
5500 * for transmit and receive operations are allocated, the interrupt
5501 * handler is registered with the OS, the netdev watchdog subtask is
5502 * enabled, and the stack is notified that the interface is ready.
5503 *
5504 * Returns 0 on success, negative value on failure
5505 **/
38e00438 5506int i40e_open(struct net_device *netdev)
41c445ff
JB
5507{
5508 struct i40e_netdev_priv *np = netdev_priv(netdev);
5509 struct i40e_vsi *vsi = np->vsi;
5510 struct i40e_pf *pf = vsi->back;
41c445ff
JB
5511 int err;
5512
4eb3f768
SN
5513 /* disallow open during test or if eeprom is broken */
5514 if (test_bit(__I40E_TESTING, &pf->state) ||
5515 test_bit(__I40E_BAD_EEPROM, &pf->state))
41c445ff
JB
5516 return -EBUSY;
5517
5518 netif_carrier_off(netdev);
5519
6c167f58
EK
5520 err = i40e_vsi_open(vsi);
5521 if (err)
5522 return err;
5523
059dab69
JB
5524 /* configure global TSO hardware offload settings */
5525 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5526 TCP_FLAG_FIN) >> 16);
5527 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5528 TCP_FLAG_FIN |
5529 TCP_FLAG_CWR) >> 16);
5530 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5531
06a5f7f1 5532 udp_tunnel_get_rx_info(netdev);
e3219ce6 5533
6c167f58
EK
5534 return 0;
5535}
5536
5537/**
5538 * i40e_vsi_open -
5539 * @vsi: the VSI to open
5540 *
5541 * Finish initialization of the VSI.
5542 *
5543 * Returns 0 on success, negative value on failure
5544 **/
5545int i40e_vsi_open(struct i40e_vsi *vsi)
5546{
5547 struct i40e_pf *pf = vsi->back;
b294ac70 5548 char int_name[I40E_INT_NAME_STR_LEN];
6c167f58
EK
5549 int err;
5550
41c445ff
JB
5551 /* allocate descriptors */
5552 err = i40e_vsi_setup_tx_resources(vsi);
5553 if (err)
5554 goto err_setup_tx;
5555 err = i40e_vsi_setup_rx_resources(vsi);
5556 if (err)
5557 goto err_setup_rx;
5558
5559 err = i40e_vsi_configure(vsi);
5560 if (err)
5561 goto err_setup_rx;
5562
c22e3c6c
SN
5563 if (vsi->netdev) {
5564 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5565 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5566 err = i40e_vsi_request_irq(vsi, int_name);
5567 if (err)
5568 goto err_setup_rx;
41c445ff 5569
c22e3c6c
SN
5570 /* Notify the stack of the actual queue counts. */
5571 err = netif_set_real_num_tx_queues(vsi->netdev,
5572 vsi->num_queue_pairs);
5573 if (err)
5574 goto err_set_queues;
25946ddb 5575
c22e3c6c
SN
5576 err = netif_set_real_num_rx_queues(vsi->netdev,
5577 vsi->num_queue_pairs);
5578 if (err)
5579 goto err_set_queues;
8a9eb7d3
SN
5580
5581 } else if (vsi->type == I40E_VSI_FDIR) {
e240f674 5582 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
b2008cbf
CW
5583 dev_driver_string(&pf->pdev->dev),
5584 dev_name(&pf->pdev->dev));
8a9eb7d3 5585 err = i40e_vsi_request_irq(vsi, int_name);
b2008cbf 5586
c22e3c6c 5587 } else {
ce9ccb17 5588 err = -EINVAL;
6c167f58
EK
5589 goto err_setup_rx;
5590 }
25946ddb 5591
41c445ff
JB
5592 err = i40e_up_complete(vsi);
5593 if (err)
5594 goto err_up_complete;
5595
41c445ff
JB
5596 return 0;
5597
5598err_up_complete:
5599 i40e_down(vsi);
25946ddb 5600err_set_queues:
41c445ff
JB
5601 i40e_vsi_free_irq(vsi);
5602err_setup_rx:
5603 i40e_vsi_free_rx_resources(vsi);
5604err_setup_tx:
5605 i40e_vsi_free_tx_resources(vsi);
5606 if (vsi == pf->vsi[pf->lan_vsi])
41a1d04b 5607 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
41c445ff
JB
5608
5609 return err;
5610}
5611
17a73f6b
JG
5612/**
5613 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
b40c82e6 5614 * @pf: Pointer to PF
17a73f6b
JG
5615 *
5616 * This function destroys the hlist where all the Flow Director
5617 * filters were saved.
5618 **/
5619static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5620{
5621 struct i40e_fdir_filter *filter;
5622 struct hlist_node *node2;
5623
5624 hlist_for_each_entry_safe(filter, node2,
5625 &pf->fdir_filter_list, fdir_node) {
5626 hlist_del(&filter->fdir_node);
5627 kfree(filter);
5628 }
5629 pf->fdir_pf_active_filters = 0;
5630}
5631
41c445ff
JB
5632/**
5633 * i40e_close - Disables a network interface
5634 * @netdev: network interface device structure
5635 *
5636 * The close entry point is called when an interface is de-activated
5637 * by the OS. The hardware is still under the driver's control, but
5638 * this netdev interface is disabled.
5639 *
5640 * Returns 0, this is not allowed to fail
5641 **/
38e00438 5642int i40e_close(struct net_device *netdev)
41c445ff
JB
5643{
5644 struct i40e_netdev_priv *np = netdev_priv(netdev);
5645 struct i40e_vsi *vsi = np->vsi;
5646
90ef8d47 5647 i40e_vsi_close(vsi);
41c445ff
JB
5648
5649 return 0;
5650}
5651
5652/**
5653 * i40e_do_reset - Start a PF or Core Reset sequence
5654 * @pf: board private structure
5655 * @reset_flags: which reset is requested
5656 *
5657 * The essential difference in resets is that the PF Reset
5658 * doesn't clear the packet buffers, doesn't reset the PE
5659 * firmware, and doesn't bother the other PFs on the chip.
5660 **/
5661void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
5662{
5663 u32 val;
5664
5665 WARN_ON(in_interrupt());
5666
263fc48f 5667
41c445ff 5668 /* do the biggest reset indicated */
41a1d04b 5669 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
41c445ff
JB
5670
5671 /* Request a Global Reset
5672 *
5673 * This will start the chip's countdown to the actual full
5674 * chip reset event, and a warning interrupt to be sent
5675 * to all PFs, including the requestor. Our handler
5676 * for the warning interrupt will deal with the shutdown
5677 * and recovery of the switch setup.
5678 */
69bfb110 5679 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
41c445ff
JB
5680 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5681 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5682 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5683
41a1d04b 5684 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
41c445ff
JB
5685
5686 /* Request a Core Reset
5687 *
5688 * Same as Global Reset, except does *not* include the MAC/PHY
5689 */
69bfb110 5690 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
41c445ff
JB
5691 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5692 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5693 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5694 i40e_flush(&pf->hw);
5695
41a1d04b 5696 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
41c445ff
JB
5697
5698 /* Request a PF Reset
5699 *
5700 * Resets only the PF-specific registers
5701 *
5702 * This goes directly to the tear-down and rebuild of
5703 * the switch, since we need to do all the recovery as
5704 * for the Core Reset.
5705 */
69bfb110 5706 dev_dbg(&pf->pdev->dev, "PFR requested\n");
41c445ff
JB
5707 i40e_handle_reset_warning(pf);
5708
41a1d04b 5709 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
41c445ff
JB
5710 int v;
5711
5712 /* Find the VSI(s) that requested a re-init */
5713 dev_info(&pf->pdev->dev,
5714 "VSI reinit requested\n");
505682cd 5715 for (v = 0; v < pf->num_alloc_vsi; v++) {
41c445ff 5716 struct i40e_vsi *vsi = pf->vsi[v];
6995b36c 5717
41c445ff
JB
5718 if (vsi != NULL &&
5719 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
5720 i40e_vsi_reinit_locked(pf->vsi[v]);
5721 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
5722 }
5723 }
41a1d04b 5724 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
b5d06f05
NP
5725 int v;
5726
5727 /* Find the VSI(s) that needs to be brought down */
5728 dev_info(&pf->pdev->dev, "VSI down requested\n");
5729 for (v = 0; v < pf->num_alloc_vsi; v++) {
5730 struct i40e_vsi *vsi = pf->vsi[v];
6995b36c 5731
b5d06f05
NP
5732 if (vsi != NULL &&
5733 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
5734 set_bit(__I40E_DOWN, &vsi->state);
5735 i40e_down(vsi);
5736 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
5737 }
5738 }
41c445ff
JB
5739 } else {
5740 dev_info(&pf->pdev->dev,
5741 "bad reset request 0x%08x\n", reset_flags);
41c445ff
JB
5742 }
5743}
5744
4e3b35b0
NP
5745#ifdef CONFIG_I40E_DCB
5746/**
5747 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5748 * @pf: board private structure
5749 * @old_cfg: current DCB config
5750 * @new_cfg: new DCB config
5751 **/
5752bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5753 struct i40e_dcbx_config *old_cfg,
5754 struct i40e_dcbx_config *new_cfg)
5755{
5756 bool need_reconfig = false;
5757
5758 /* Check if ETS configuration has changed */
5759 if (memcmp(&new_cfg->etscfg,
5760 &old_cfg->etscfg,
5761 sizeof(new_cfg->etscfg))) {
5762 /* If Priority Table has changed reconfig is needed */
5763 if (memcmp(&new_cfg->etscfg.prioritytable,
5764 &old_cfg->etscfg.prioritytable,
5765 sizeof(new_cfg->etscfg.prioritytable))) {
5766 need_reconfig = true;
69bfb110 5767 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
4e3b35b0
NP
5768 }
5769
5770 if (memcmp(&new_cfg->etscfg.tcbwtable,
5771 &old_cfg->etscfg.tcbwtable,
5772 sizeof(new_cfg->etscfg.tcbwtable)))
69bfb110 5773 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
4e3b35b0
NP
5774
5775 if (memcmp(&new_cfg->etscfg.tsatable,
5776 &old_cfg->etscfg.tsatable,
5777 sizeof(new_cfg->etscfg.tsatable)))
69bfb110 5778 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
4e3b35b0
NP
5779 }
5780
5781 /* Check if PFC configuration has changed */
5782 if (memcmp(&new_cfg->pfc,
5783 &old_cfg->pfc,
5784 sizeof(new_cfg->pfc))) {
5785 need_reconfig = true;
69bfb110 5786 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
4e3b35b0
NP
5787 }
5788
5789 /* Check if APP Table has changed */
5790 if (memcmp(&new_cfg->app,
5791 &old_cfg->app,
3d9667a9 5792 sizeof(new_cfg->app))) {
4e3b35b0 5793 need_reconfig = true;
69bfb110 5794 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
3d9667a9 5795 }
4e3b35b0 5796
fb43201f 5797 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
4e3b35b0
NP
5798 return need_reconfig;
5799}
5800
5801/**
5802 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5803 * @pf: board private structure
5804 * @e: event info posted on ARQ
5805 **/
5806static int i40e_handle_lldp_event(struct i40e_pf *pf,
5807 struct i40e_arq_event_info *e)
5808{
5809 struct i40e_aqc_lldp_get_mib *mib =
5810 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5811 struct i40e_hw *hw = &pf->hw;
4e3b35b0
NP
5812 struct i40e_dcbx_config tmp_dcbx_cfg;
5813 bool need_reconfig = false;
5814 int ret = 0;
5815 u8 type;
5816
4d9b6043 5817 /* Not DCB capable or capability disabled */
ea6acb7e 5818 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
4d9b6043
NP
5819 return ret;
5820
4e3b35b0
NP
5821 /* Ignore if event is not for Nearest Bridge */
5822 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5823 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
fb43201f 5824 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
4e3b35b0
NP
5825 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5826 return ret;
5827
5828 /* Check MIB Type and return if event for Remote MIB update */
5829 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9fa61dd2 5830 dev_dbg(&pf->pdev->dev,
fb43201f 5831 "LLDP event mib type %s\n", type ? "remote" : "local");
4e3b35b0
NP
5832 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
5833 /* Update the remote cached instance and return */
5834 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
5835 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
5836 &hw->remote_dcbx_config);
5837 goto exit;
5838 }
5839
9fa61dd2 5840 /* Store the old configuration */
1a2f6248 5841 tmp_dcbx_cfg = hw->local_dcbx_config;
9fa61dd2 5842
750fcbcf
NP
5843 /* Reset the old DCBx configuration data */
5844 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9fa61dd2
NP
5845 /* Get updated DCBX data from firmware */
5846 ret = i40e_get_dcb_config(&pf->hw);
4e3b35b0 5847 if (ret) {
f1c7e72e
SN
5848 dev_info(&pf->pdev->dev,
5849 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
5850 i40e_stat_str(&pf->hw, ret),
5851 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
4e3b35b0
NP
5852 goto exit;
5853 }
5854
5855 /* No change detected in DCBX configs */
750fcbcf
NP
5856 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
5857 sizeof(tmp_dcbx_cfg))) {
69bfb110 5858 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
4e3b35b0
NP
5859 goto exit;
5860 }
5861
750fcbcf
NP
5862 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
5863 &hw->local_dcbx_config);
4e3b35b0 5864
750fcbcf 5865 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
4e3b35b0
NP
5866
5867 if (!need_reconfig)
5868 goto exit;
5869
4d9b6043 5870 /* Enable DCB tagging only when more than one TC */
750fcbcf 5871 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
4d9b6043
NP
5872 pf->flags |= I40E_FLAG_DCB_ENABLED;
5873 else
5874 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
5875
69129dc3 5876 set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
4e3b35b0
NP
5877 /* Reconfiguration needed quiesce all VSIs */
5878 i40e_pf_quiesce_all_vsi(pf);
5879
5880 /* Changes in configuration update VEB/VSI */
5881 i40e_dcb_reconfigure(pf);
5882
2fd75f31
NP
5883 ret = i40e_resume_port_tx(pf);
5884
69129dc3 5885 clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
2fd75f31 5886 /* In case of error no point in resuming VSIs */
69129dc3
NP
5887 if (ret)
5888 goto exit;
5889
3fe06f41
NP
5890 /* Wait for the PF's queues to be disabled */
5891 ret = i40e_pf_wait_queues_disabled(pf);
11e47708
PN
5892 if (ret) {
5893 /* Schedule PF reset to recover */
5894 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5895 i40e_service_event_schedule(pf);
5896 } else {
2fd75f31 5897 i40e_pf_unquiesce_all_vsi(pf);
85a1aab7
NP
5898 /* Notify the client for the DCB changes */
5899 i40e_notify_client_of_l2_param_changes(pf->vsi[pf->lan_vsi]);
11e47708
PN
5900 }
5901
4e3b35b0
NP
5902exit:
5903 return ret;
5904}
5905#endif /* CONFIG_I40E_DCB */
5906
23326186
ASJ
5907/**
5908 * i40e_do_reset_safe - Protected reset path for userland calls.
5909 * @pf: board private structure
5910 * @reset_flags: which reset is requested
5911 *
5912 **/
5913void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
5914{
5915 rtnl_lock();
5916 i40e_do_reset(pf, reset_flags);
5917 rtnl_unlock();
5918}
5919
41c445ff
JB
5920/**
5921 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
5922 * @pf: board private structure
5923 * @e: event info posted on ARQ
5924 *
5925 * Handler for LAN Queue Overflow Event generated by the firmware for PF
5926 * and VF queues
5927 **/
5928static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
5929 struct i40e_arq_event_info *e)
5930{
5931 struct i40e_aqc_lan_overflow *data =
5932 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
5933 u32 queue = le32_to_cpu(data->prtdcb_rupto);
5934 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
5935 struct i40e_hw *hw = &pf->hw;
5936 struct i40e_vf *vf;
5937 u16 vf_id;
5938
69bfb110
JB
5939 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
5940 queue, qtx_ctl);
41c445ff
JB
5941
5942 /* Queue belongs to VF, find the VF and issue VF reset */
5943 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
5944 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
5945 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
5946 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
5947 vf_id -= hw->func_caps.vf_base_id;
5948 vf = &pf->vf[vf_id];
5949 i40e_vc_notify_vf_reset(vf);
5950 /* Allow VF to process pending reset notification */
5951 msleep(20);
5952 i40e_reset_vf(vf, false);
5953 }
5954}
5955
55a5e60b 5956/**
12957388
ASJ
5957 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
5958 * @pf: board private structure
5959 **/
04294e38 5960u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
12957388 5961{
04294e38 5962 u32 val, fcnt_prog;
12957388
ASJ
5963
5964 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5965 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
5966 return fcnt_prog;
5967}
5968
5969/**
04294e38 5970 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
55a5e60b
ASJ
5971 * @pf: board private structure
5972 **/
04294e38 5973u32 i40e_get_current_fd_count(struct i40e_pf *pf)
55a5e60b 5974{
04294e38
ASJ
5975 u32 val, fcnt_prog;
5976
55a5e60b
ASJ
5977 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5978 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
5979 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
5980 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
5981 return fcnt_prog;
5982}
1e1be8f6 5983
04294e38
ASJ
5984/**
5985 * i40e_get_global_fd_count - Get total FD filters programmed on device
5986 * @pf: board private structure
5987 **/
5988u32 i40e_get_global_fd_count(struct i40e_pf *pf)
5989{
5990 u32 val, fcnt_prog;
5991
5992 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
5993 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
5994 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
5995 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
5996 return fcnt_prog;
5997}
5998
55a5e60b
ASJ
5999/**
6000 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
6001 * @pf: board private structure
6002 **/
6003void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
6004{
3487b6c3 6005 struct i40e_fdir_filter *filter;
55a5e60b 6006 u32 fcnt_prog, fcnt_avail;
3487b6c3 6007 struct hlist_node *node;
55a5e60b 6008
1e1be8f6
ASJ
6009 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
6010 return;
6011
55a5e60b
ASJ
6012 /* Check if, FD SB or ATR was auto disabled and if there is enough room
6013 * to re-enable
6014 */
04294e38 6015 fcnt_prog = i40e_get_global_fd_count(pf);
12957388 6016 fcnt_avail = pf->fdir_pf_filter_count;
1e1be8f6
ASJ
6017 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
6018 (pf->fd_add_err == 0) ||
6019 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
55a5e60b
ASJ
6020 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
6021 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
6022 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
2e4875e3
ASJ
6023 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6024 dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
55a5e60b
ASJ
6025 }
6026 }
a3417d28
JK
6027
6028 /* Wait for some more space to be available to turn on ATR. We also
6029 * must check that no existing ntuple rules for TCP are in effect
6030 */
55a5e60b
ASJ
6031 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
6032 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
a3417d28
JK
6033 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED) &&
6034 (pf->fd_tcp_rule == 0)) {
55a5e60b 6035 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
2e4875e3 6036 if (I40E_DEBUG_FD & pf->hw.debug_mask)
a3417d28 6037 dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
55a5e60b
ASJ
6038 }
6039 }
3487b6c3
CW
6040
6041 /* if hw had a problem adding a filter, delete it */
6042 if (pf->fd_inv > 0) {
6043 hlist_for_each_entry_safe(filter, node,
6044 &pf->fdir_filter_list, fdir_node) {
6045 if (filter->fd_id == pf->fd_inv) {
6046 hlist_del(&filter->fdir_node);
6047 kfree(filter);
6048 pf->fdir_pf_active_filters--;
6049 }
6050 }
6051 }
55a5e60b
ASJ
6052}
6053
1e1be8f6 6054#define I40E_MIN_FD_FLUSH_INTERVAL 10
04294e38 6055#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
1e1be8f6
ASJ
6056/**
6057 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
6058 * @pf: board private structure
6059 **/
6060static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
6061{
04294e38 6062 unsigned long min_flush_time;
1e1be8f6 6063 int flush_wait_retry = 50;
04294e38
ASJ
6064 bool disable_atr = false;
6065 int fd_room;
1e1be8f6
ASJ
6066 int reg;
6067
a5fdaf34
JB
6068 if (!time_after(jiffies, pf->fd_flush_timestamp +
6069 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
6070 return;
04294e38 6071
a5fdaf34
JB
6072 /* If the flush is happening too quick and we have mostly SB rules we
6073 * should not re-enable ATR for some time.
6074 */
6075 min_flush_time = pf->fd_flush_timestamp +
6076 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
6077 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
6078
6079 if (!(time_after(jiffies, min_flush_time)) &&
6080 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
6081 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6082 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
6083 disable_atr = true;
6084 }
6085
6086 pf->fd_flush_timestamp = jiffies;
234dc4e6 6087 pf->auto_disable_flags |= I40E_FLAG_FD_ATR_ENABLED;
a5fdaf34
JB
6088 /* flush all filters */
6089 wr32(&pf->hw, I40E_PFQF_CTL_1,
6090 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
6091 i40e_flush(&pf->hw);
6092 pf->fd_flush_cnt++;
6093 pf->fd_add_err = 0;
6094 do {
6095 /* Check FD flush status every 5-6msec */
6096 usleep_range(5000, 6000);
6097 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
6098 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
6099 break;
6100 } while (flush_wait_retry--);
6101 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
6102 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
6103 } else {
6104 /* replay sideband filters */
6105 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
6106 if (!disable_atr)
234dc4e6 6107 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
a5fdaf34
JB
6108 clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
6109 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6110 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
1e1be8f6
ASJ
6111 }
6112}
6113
6114/**
6115 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
6116 * @pf: board private structure
6117 **/
04294e38 6118u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
1e1be8f6
ASJ
6119{
6120 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
6121}
6122
6123/* We can see up to 256 filter programming desc in transit if the filters are
6124 * being applied really fast; before we see the first
6125 * filter miss error on Rx queue 0. Accumulating enough error messages before
6126 * reacting will make sure we don't cause flush too often.
6127 */
6128#define I40E_MAX_FD_PROGRAM_ERROR 256
6129
41c445ff
JB
6130/**
6131 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
6132 * @pf: board private structure
6133 **/
6134static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
6135{
41c445ff 6136
41c445ff
JB
6137 /* if interface is down do nothing */
6138 if (test_bit(__I40E_DOWN, &pf->state))
6139 return;
1e1be8f6 6140
04294e38 6141 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
1e1be8f6
ASJ
6142 i40e_fdir_flush_and_replay(pf);
6143
55a5e60b
ASJ
6144 i40e_fdir_check_and_reenable(pf);
6145
41c445ff
JB
6146}
6147
6148/**
6149 * i40e_vsi_link_event - notify VSI of a link event
6150 * @vsi: vsi to be notified
6151 * @link_up: link up or down
6152 **/
6153static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
6154{
32b5b811 6155 if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
41c445ff
JB
6156 return;
6157
6158 switch (vsi->type) {
6159 case I40E_VSI_MAIN:
38e00438
VD
6160#ifdef I40E_FCOE
6161 case I40E_VSI_FCOE:
6162#endif
41c445ff
JB
6163 if (!vsi->netdev || !vsi->netdev_registered)
6164 break;
6165
6166 if (link_up) {
6167 netif_carrier_on(vsi->netdev);
6168 netif_tx_wake_all_queues(vsi->netdev);
6169 } else {
6170 netif_carrier_off(vsi->netdev);
6171 netif_tx_stop_all_queues(vsi->netdev);
6172 }
6173 break;
6174
6175 case I40E_VSI_SRIOV:
41c445ff
JB
6176 case I40E_VSI_VMDQ2:
6177 case I40E_VSI_CTRL:
e3219ce6 6178 case I40E_VSI_IWARP:
41c445ff
JB
6179 case I40E_VSI_MIRROR:
6180 default:
6181 /* there is no notification for other VSIs */
6182 break;
6183 }
6184}
6185
6186/**
6187 * i40e_veb_link_event - notify elements on the veb of a link event
6188 * @veb: veb to be notified
6189 * @link_up: link up or down
6190 **/
6191static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
6192{
6193 struct i40e_pf *pf;
6194 int i;
6195
6196 if (!veb || !veb->pf)
6197 return;
6198 pf = veb->pf;
6199
6200 /* depth first... */
6201 for (i = 0; i < I40E_MAX_VEB; i++)
6202 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6203 i40e_veb_link_event(pf->veb[i], link_up);
6204
6205 /* ... now the local VSIs */
505682cd 6206 for (i = 0; i < pf->num_alloc_vsi; i++)
41c445ff
JB
6207 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6208 i40e_vsi_link_event(pf->vsi[i], link_up);
6209}
6210
6211/**
6212 * i40e_link_event - Update netif_carrier status
6213 * @pf: board private structure
6214 **/
6215static void i40e_link_event(struct i40e_pf *pf)
6216{
320684cd 6217 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
fef59ddf 6218 u8 new_link_speed, old_link_speed;
a72a5abc
JB
6219 i40e_status status;
6220 bool new_link, old_link;
41c445ff 6221
1f9610e4
CS
6222 /* save off old link status information */
6223 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6224
1e701e09
JB
6225 /* set this to force the get_link_status call to refresh state */
6226 pf->hw.phy.get_link_info = true;
6227
41c445ff 6228 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
a72a5abc
JB
6229
6230 status = i40e_get_link_status(&pf->hw, &new_link);
6231 if (status) {
6232 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
6233 status);
6234 return;
6235 }
6236
fef59ddf
CS
6237 old_link_speed = pf->hw.phy.link_info_old.link_speed;
6238 new_link_speed = pf->hw.phy.link_info.link_speed;
41c445ff 6239
1e701e09 6240 if (new_link == old_link &&
fef59ddf 6241 new_link_speed == old_link_speed &&
320684cd
MW
6242 (test_bit(__I40E_DOWN, &vsi->state) ||
6243 new_link == netif_carrier_ok(vsi->netdev)))
41c445ff 6244 return;
320684cd
MW
6245
6246 if (!test_bit(__I40E_DOWN, &vsi->state))
6247 i40e_print_link_message(vsi, new_link);
41c445ff
JB
6248
6249 /* Notify the base of the switch tree connected to
6250 * the link. Floating VEBs are not notified.
6251 */
6252 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6253 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
6254 else
320684cd 6255 i40e_vsi_link_event(vsi, new_link);
41c445ff
JB
6256
6257 if (pf->vf)
6258 i40e_vc_notify_link_state(pf);
beb0dff1
JK
6259
6260 if (pf->flags & I40E_FLAG_PTP)
6261 i40e_ptp_set_increment(pf);
41c445ff
JB
6262}
6263
41c445ff 6264/**
21536717 6265 * i40e_watchdog_subtask - periodic checks not using event driven response
41c445ff
JB
6266 * @pf: board private structure
6267 **/
6268static void i40e_watchdog_subtask(struct i40e_pf *pf)
6269{
6270 int i;
6271
6272 /* if interface is down do nothing */
6273 if (test_bit(__I40E_DOWN, &pf->state) ||
6274 test_bit(__I40E_CONFIG_BUSY, &pf->state))
6275 return;
6276
21536717
SN
6277 /* make sure we don't do these things too often */
6278 if (time_before(jiffies, (pf->service_timer_previous +
6279 pf->service_timer_period)))
6280 return;
6281 pf->service_timer_previous = jiffies;
6282
9ac77266
SN
6283 if (pf->flags & I40E_FLAG_LINK_POLLING_ENABLED)
6284 i40e_link_event(pf);
21536717 6285
41c445ff
JB
6286 /* Update the stats for active netdevs so the network stack
6287 * can look at updated numbers whenever it cares to
6288 */
505682cd 6289 for (i = 0; i < pf->num_alloc_vsi; i++)
41c445ff
JB
6290 if (pf->vsi[i] && pf->vsi[i]->netdev)
6291 i40e_update_stats(pf->vsi[i]);
6292
d1a8d275
ASJ
6293 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6294 /* Update the stats for the active switching components */
6295 for (i = 0; i < I40E_MAX_VEB; i++)
6296 if (pf->veb[i])
6297 i40e_update_veb_stats(pf->veb[i]);
6298 }
beb0dff1
JK
6299
6300 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
41c445ff
JB
6301}
6302
6303/**
6304 * i40e_reset_subtask - Set up for resetting the device and driver
6305 * @pf: board private structure
6306 **/
6307static void i40e_reset_subtask(struct i40e_pf *pf)
6308{
6309 u32 reset_flags = 0;
6310
23326186 6311 rtnl_lock();
41c445ff 6312 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
75f5cea9 6313 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
41c445ff
JB
6314 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
6315 }
6316 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
75f5cea9 6317 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
41c445ff
JB
6318 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6319 }
6320 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
75f5cea9 6321 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
41c445ff
JB
6322 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
6323 }
6324 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
75f5cea9 6325 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
41c445ff
JB
6326 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
6327 }
b5d06f05 6328 if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
75f5cea9 6329 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
b5d06f05
NP
6330 clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
6331 }
41c445ff
JB
6332
6333 /* If there's a recovery already waiting, it takes
6334 * precedence before starting a new reset sequence.
6335 */
6336 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
6337 i40e_handle_reset_warning(pf);
23326186 6338 goto unlock;
41c445ff
JB
6339 }
6340
6341 /* If we're already down or resetting, just bail */
6342 if (reset_flags &&
6343 !test_bit(__I40E_DOWN, &pf->state) &&
6344 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
6345 i40e_do_reset(pf, reset_flags);
23326186
ASJ
6346
6347unlock:
6348 rtnl_unlock();
41c445ff
JB
6349}
6350
6351/**
6352 * i40e_handle_link_event - Handle link event
6353 * @pf: board private structure
6354 * @e: event info posted on ARQ
6355 **/
6356static void i40e_handle_link_event(struct i40e_pf *pf,
6357 struct i40e_arq_event_info *e)
6358{
41c445ff
JB
6359 struct i40e_aqc_get_link_status *status =
6360 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
41c445ff 6361
1e701e09
JB
6362 /* Do a new status request to re-enable LSE reporting
6363 * and load new status information into the hw struct
6364 * This completely ignores any state information
6365 * in the ARQ event info, instead choosing to always
6366 * issue the AQ update link status command.
6367 */
6368 i40e_link_event(pf);
6369
7b592f61
CW
6370 /* check for unqualified module, if link is down */
6371 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6372 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6373 (!(status->link_info & I40E_AQ_LINK_UP)))
6374 dev_err(&pf->pdev->dev,
6375 "The driver failed to link because an unqualified module was detected.\n");
41c445ff
JB
6376}
6377
6378/**
6379 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6380 * @pf: board private structure
6381 **/
6382static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6383{
6384 struct i40e_arq_event_info event;
6385 struct i40e_hw *hw = &pf->hw;
6386 u16 pending, i = 0;
6387 i40e_status ret;
6388 u16 opcode;
86df242b 6389 u32 oldval;
41c445ff
JB
6390 u32 val;
6391
a316f651
ASJ
6392 /* Do not run clean AQ when PF reset fails */
6393 if (test_bit(__I40E_RESET_FAILED, &pf->state))
6394 return;
6395
86df242b
SN
6396 /* check for error indications */
6397 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6398 oldval = val;
6399 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
75eb73c1
MW
6400 if (hw->debug_mask & I40E_DEBUG_AQ)
6401 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
86df242b
SN
6402 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6403 }
6404 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
75eb73c1
MW
6405 if (hw->debug_mask & I40E_DEBUG_AQ)
6406 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
86df242b 6407 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
1d0a4ada 6408 pf->arq_overflows++;
86df242b
SN
6409 }
6410 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
75eb73c1
MW
6411 if (hw->debug_mask & I40E_DEBUG_AQ)
6412 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
86df242b
SN
6413 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6414 }
6415 if (oldval != val)
6416 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6417
6418 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6419 oldval = val;
6420 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
75eb73c1
MW
6421 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6422 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
86df242b
SN
6423 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6424 }
6425 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
75eb73c1
MW
6426 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6427 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
86df242b
SN
6428 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6429 }
6430 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
75eb73c1
MW
6431 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6432 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
86df242b
SN
6433 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6434 }
6435 if (oldval != val)
6436 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6437
1001dc37
MW
6438 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6439 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
41c445ff
JB
6440 if (!event.msg_buf)
6441 return;
6442
6443 do {
6444 ret = i40e_clean_arq_element(hw, &event, &pending);
56497978 6445 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
41c445ff 6446 break;
56497978 6447 else if (ret) {
41c445ff
JB
6448 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6449 break;
6450 }
6451
6452 opcode = le16_to_cpu(event.desc.opcode);
6453 switch (opcode) {
6454
6455 case i40e_aqc_opc_get_link_status:
6456 i40e_handle_link_event(pf, &event);
6457 break;
6458 case i40e_aqc_opc_send_msg_to_pf:
6459 ret = i40e_vc_process_vf_msg(pf,
6460 le16_to_cpu(event.desc.retval),
6461 le32_to_cpu(event.desc.cookie_high),
6462 le32_to_cpu(event.desc.cookie_low),
6463 event.msg_buf,
1001dc37 6464 event.msg_len);
41c445ff
JB
6465 break;
6466 case i40e_aqc_opc_lldp_update_mib:
69bfb110 6467 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
4e3b35b0
NP
6468#ifdef CONFIG_I40E_DCB
6469 rtnl_lock();
6470 ret = i40e_handle_lldp_event(pf, &event);
6471 rtnl_unlock();
6472#endif /* CONFIG_I40E_DCB */
41c445ff
JB
6473 break;
6474 case i40e_aqc_opc_event_lan_overflow:
69bfb110 6475 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
41c445ff
JB
6476 i40e_handle_lan_overflow_event(pf, &event);
6477 break;
0467bc91
SN
6478 case i40e_aqc_opc_send_msg_to_peer:
6479 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6480 break;
91a0f930
SN
6481 case i40e_aqc_opc_nvm_erase:
6482 case i40e_aqc_opc_nvm_update:
00ada50d 6483 case i40e_aqc_opc_oem_post_update:
6e93d0c9
SN
6484 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
6485 "ARQ NVM operation 0x%04x completed\n",
6486 opcode);
91a0f930 6487 break;
41c445ff
JB
6488 default:
6489 dev_info(&pf->pdev->dev,
56e5ca68 6490 "ARQ: Unknown event 0x%04x ignored\n",
0467bc91 6491 opcode);
41c445ff
JB
6492 break;
6493 }
6494 } while (pending && (i++ < pf->adminq_work_limit));
6495
6496 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
6497 /* re-enable Admin queue interrupt cause */
6498 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6499 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6500 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6501 i40e_flush(hw);
6502
6503 kfree(event.msg_buf);
6504}
6505
4eb3f768
SN
6506/**
6507 * i40e_verify_eeprom - make sure eeprom is good to use
6508 * @pf: board private structure
6509 **/
6510static void i40e_verify_eeprom(struct i40e_pf *pf)
6511{
6512 int err;
6513
6514 err = i40e_diag_eeprom_test(&pf->hw);
6515 if (err) {
6516 /* retry in case of garbage read */
6517 err = i40e_diag_eeprom_test(&pf->hw);
6518 if (err) {
6519 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6520 err);
6521 set_bit(__I40E_BAD_EEPROM, &pf->state);
6522 }
6523 }
6524
6525 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
6526 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
6527 clear_bit(__I40E_BAD_EEPROM, &pf->state);
6528 }
6529}
6530
386a0afa
AA
6531/**
6532 * i40e_enable_pf_switch_lb
b40c82e6 6533 * @pf: pointer to the PF structure
386a0afa
AA
6534 *
6535 * enable switch loop back or die - no point in a return value
6536 **/
6537static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6538{
6539 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6540 struct i40e_vsi_context ctxt;
f1c7e72e 6541 int ret;
386a0afa
AA
6542
6543 ctxt.seid = pf->main_vsi_seid;
6544 ctxt.pf_num = pf->hw.pf_id;
6545 ctxt.vf_num = 0;
f1c7e72e
SN
6546 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6547 if (ret) {
386a0afa 6548 dev_info(&pf->pdev->dev,
f1c7e72e
SN
6549 "couldn't get PF vsi config, err %s aq_err %s\n",
6550 i40e_stat_str(&pf->hw, ret),
6551 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
386a0afa
AA
6552 return;
6553 }
6554 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6555 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6556 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6557
f1c7e72e
SN
6558 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6559 if (ret) {
386a0afa 6560 dev_info(&pf->pdev->dev,
f1c7e72e
SN
6561 "update vsi switch failed, err %s aq_err %s\n",
6562 i40e_stat_str(&pf->hw, ret),
6563 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
386a0afa
AA
6564 }
6565}
6566
6567/**
6568 * i40e_disable_pf_switch_lb
b40c82e6 6569 * @pf: pointer to the PF structure
386a0afa
AA
6570 *
6571 * disable switch loop back or die - no point in a return value
6572 **/
6573static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6574{
6575 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6576 struct i40e_vsi_context ctxt;
f1c7e72e 6577 int ret;
386a0afa
AA
6578
6579 ctxt.seid = pf->main_vsi_seid;
6580 ctxt.pf_num = pf->hw.pf_id;
6581 ctxt.vf_num = 0;
f1c7e72e
SN
6582 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6583 if (ret) {
386a0afa 6584 dev_info(&pf->pdev->dev,
f1c7e72e
SN
6585 "couldn't get PF vsi config, err %s aq_err %s\n",
6586 i40e_stat_str(&pf->hw, ret),
6587 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
386a0afa
AA
6588 return;
6589 }
6590 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6591 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6592 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6593
f1c7e72e
SN
6594 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6595 if (ret) {
386a0afa 6596 dev_info(&pf->pdev->dev,
f1c7e72e
SN
6597 "update vsi switch failed, err %s aq_err %s\n",
6598 i40e_stat_str(&pf->hw, ret),
6599 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
386a0afa
AA
6600 }
6601}
6602
51616018
NP
6603/**
6604 * i40e_config_bridge_mode - Configure the HW bridge mode
6605 * @veb: pointer to the bridge instance
6606 *
6607 * Configure the loop back mode for the LAN VSI that is downlink to the
6608 * specified HW bridge instance. It is expected this function is called
6609 * when a new HW bridge is instantiated.
6610 **/
6611static void i40e_config_bridge_mode(struct i40e_veb *veb)
6612{
6613 struct i40e_pf *pf = veb->pf;
6614
6dec1017
SN
6615 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6616 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6617 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
51616018
NP
6618 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6619 i40e_disable_pf_switch_lb(pf);
6620 else
6621 i40e_enable_pf_switch_lb(pf);
6622}
6623
41c445ff
JB
6624/**
6625 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6626 * @veb: pointer to the VEB instance
6627 *
6628 * This is a recursive function that first builds the attached VSIs then
6629 * recurses in to build the next layer of VEB. We track the connections
6630 * through our own index numbers because the seid's from the HW could
6631 * change across the reset.
6632 **/
6633static int i40e_reconstitute_veb(struct i40e_veb *veb)
6634{
6635 struct i40e_vsi *ctl_vsi = NULL;
6636 struct i40e_pf *pf = veb->pf;
6637 int v, veb_idx;
6638 int ret;
6639
6640 /* build VSI that owns this VEB, temporarily attached to base VEB */
505682cd 6641 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
41c445ff
JB
6642 if (pf->vsi[v] &&
6643 pf->vsi[v]->veb_idx == veb->idx &&
6644 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6645 ctl_vsi = pf->vsi[v];
6646 break;
6647 }
6648 }
6649 if (!ctl_vsi) {
6650 dev_info(&pf->pdev->dev,
6651 "missing owner VSI for veb_idx %d\n", veb->idx);
6652 ret = -ENOENT;
6653 goto end_reconstitute;
6654 }
6655 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6656 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6657 ret = i40e_add_vsi(ctl_vsi);
6658 if (ret) {
6659 dev_info(&pf->pdev->dev,
f1c7e72e
SN
6660 "rebuild of veb_idx %d owner VSI failed: %d\n",
6661 veb->idx, ret);
41c445ff
JB
6662 goto end_reconstitute;
6663 }
6664 i40e_vsi_reset_stats(ctl_vsi);
6665
6666 /* create the VEB in the switch and move the VSI onto the VEB */
6667 ret = i40e_add_veb(veb, ctl_vsi);
6668 if (ret)
6669 goto end_reconstitute;
6670
fc60861e
ASJ
6671 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6672 veb->bridge_mode = BRIDGE_MODE_VEB;
6673 else
6674 veb->bridge_mode = BRIDGE_MODE_VEPA;
51616018 6675 i40e_config_bridge_mode(veb);
b64ba084 6676
41c445ff 6677 /* create the remaining VSIs attached to this VEB */
505682cd 6678 for (v = 0; v < pf->num_alloc_vsi; v++) {
41c445ff
JB
6679 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6680 continue;
6681
6682 if (pf->vsi[v]->veb_idx == veb->idx) {
6683 struct i40e_vsi *vsi = pf->vsi[v];
6995b36c 6684
41c445ff
JB
6685 vsi->uplink_seid = veb->seid;
6686 ret = i40e_add_vsi(vsi);
6687 if (ret) {
6688 dev_info(&pf->pdev->dev,
6689 "rebuild of vsi_idx %d failed: %d\n",
6690 v, ret);
6691 goto end_reconstitute;
6692 }
6693 i40e_vsi_reset_stats(vsi);
6694 }
6695 }
6696
6697 /* create any VEBs attached to this VEB - RECURSION */
6698 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6699 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6700 pf->veb[veb_idx]->uplink_seid = veb->seid;
6701 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6702 if (ret)
6703 break;
6704 }
6705 }
6706
6707end_reconstitute:
6708 return ret;
6709}
6710
6711/**
6712 * i40e_get_capabilities - get info about the HW
6713 * @pf: the PF struct
6714 **/
6715static int i40e_get_capabilities(struct i40e_pf *pf)
6716{
6717 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6718 u16 data_size;
6719 int buf_len;
6720 int err;
6721
6722 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6723 do {
6724 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6725 if (!cap_buf)
6726 return -ENOMEM;
6727
6728 /* this loads the data into the hw struct for us */
6729 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6730 &data_size,
6731 i40e_aqc_opc_list_func_capabilities,
6732 NULL);
6733 /* data loaded, buffer no longer needed */
6734 kfree(cap_buf);
6735
6736 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6737 /* retry with a larger buffer */
6738 buf_len = data_size;
6739 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6740 dev_info(&pf->pdev->dev,
f1c7e72e
SN
6741 "capability discovery failed, err %s aq_err %s\n",
6742 i40e_stat_str(&pf->hw, err),
6743 i40e_aq_str(&pf->hw,
6744 pf->hw.aq.asq_last_status));
41c445ff
JB
6745 return -ENODEV;
6746 }
6747 } while (err);
6748
6749 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6750 dev_info(&pf->pdev->dev,
6751 "pf=%d, num_vfs=%d, msix_pf=%d, msix_vf=%d, fd_g=%d, fd_b=%d, pf_max_q=%d num_vsi=%d\n",
6752 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6753 pf->hw.func_caps.num_msix_vectors,
6754 pf->hw.func_caps.num_msix_vectors_vf,
6755 pf->hw.func_caps.fd_filters_guaranteed,
6756 pf->hw.func_caps.fd_filters_best_effort,
6757 pf->hw.func_caps.num_tx_qp,
6758 pf->hw.func_caps.num_vsis);
6759
7134f9ce
JB
6760#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6761 + pf->hw.func_caps.num_vfs)
6762 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6763 dev_info(&pf->pdev->dev,
6764 "got num_vsis %d, setting num_vsis to %d\n",
6765 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6766 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6767 }
6768
41c445ff
JB
6769 return 0;
6770}
6771
cbf61325
ASJ
6772static int i40e_vsi_clear(struct i40e_vsi *vsi);
6773
41c445ff 6774/**
cbf61325 6775 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
41c445ff
JB
6776 * @pf: board private structure
6777 **/
cbf61325 6778static void i40e_fdir_sb_setup(struct i40e_pf *pf)
41c445ff
JB
6779{
6780 struct i40e_vsi *vsi;
41c445ff 6781
407e063c
JB
6782 /* quick workaround for an NVM issue that leaves a critical register
6783 * uninitialized
6784 */
6785 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6786 static const u32 hkey[] = {
6787 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6788 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6789 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6790 0x95b3a76d};
4b816446 6791 int i;
407e063c
JB
6792
6793 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6794 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6795 }
6796
cbf61325 6797 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
41c445ff
JB
6798 return;
6799
cbf61325 6800 /* find existing VSI and see if it needs configuring */
4b816446 6801 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
cbf61325
ASJ
6802
6803 /* create a new VSI if none exists */
41c445ff 6804 if (!vsi) {
cbf61325
ASJ
6805 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6806 pf->vsi[pf->lan_vsi]->seid, 0);
41c445ff
JB
6807 if (!vsi) {
6808 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
8a9eb7d3
SN
6809 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6810 return;
41c445ff 6811 }
cbf61325 6812 }
41c445ff 6813
8a9eb7d3 6814 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
41c445ff
JB
6815}
6816
6817/**
6818 * i40e_fdir_teardown - release the Flow Director resources
6819 * @pf: board private structure
6820 **/
6821static void i40e_fdir_teardown(struct i40e_pf *pf)
6822{
4b816446 6823 struct i40e_vsi *vsi;
41c445ff 6824
17a73f6b 6825 i40e_fdir_filter_exit(pf);
4b816446
AD
6826 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
6827 if (vsi)
6828 i40e_vsi_release(vsi);
41c445ff
JB
6829}
6830
6831/**
f650a38b 6832 * i40e_prep_for_reset - prep for the core to reset
41c445ff
JB
6833 * @pf: board private structure
6834 *
b40c82e6 6835 * Close up the VFs and other things in prep for PF Reset.
f650a38b 6836 **/
23cfbe07 6837static void i40e_prep_for_reset(struct i40e_pf *pf)
41c445ff 6838{
41c445ff 6839 struct i40e_hw *hw = &pf->hw;
60442dea 6840 i40e_status ret = 0;
41c445ff
JB
6841 u32 v;
6842
6843 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
6844 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
23cfbe07 6845 return;
d3ce5734
MW
6846 if (i40e_check_asq_alive(&pf->hw))
6847 i40e_vc_notify_reset(pf);
41c445ff 6848
69bfb110 6849 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
41c445ff 6850
41c445ff
JB
6851 /* quiesce the VSIs and their queues that are not already DOWN */
6852 i40e_pf_quiesce_all_vsi(pf);
6853
505682cd 6854 for (v = 0; v < pf->num_alloc_vsi; v++) {
41c445ff
JB
6855 if (pf->vsi[v])
6856 pf->vsi[v]->seid = 0;
6857 }
6858
6859 i40e_shutdown_adminq(&pf->hw);
6860
f650a38b 6861 /* call shutdown HMC */
60442dea
SN
6862 if (hw->hmc.hmc_obj) {
6863 ret = i40e_shutdown_lan_hmc(hw);
23cfbe07 6864 if (ret)
60442dea
SN
6865 dev_warn(&pf->pdev->dev,
6866 "shutdown_lan_hmc failed: %d\n", ret);
f650a38b 6867 }
f650a38b
ASJ
6868}
6869
44033fac
JB
6870/**
6871 * i40e_send_version - update firmware with driver version
6872 * @pf: PF struct
6873 */
6874static void i40e_send_version(struct i40e_pf *pf)
6875{
6876 struct i40e_driver_version dv;
6877
6878 dv.major_version = DRV_VERSION_MAJOR;
6879 dv.minor_version = DRV_VERSION_MINOR;
6880 dv.build_version = DRV_VERSION_BUILD;
6881 dv.subbuild_version = 0;
35a7d804 6882 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
44033fac
JB
6883 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
6884}
6885
f650a38b 6886/**
4dda12e6 6887 * i40e_reset_and_rebuild - reset and rebuild using a saved config
f650a38b 6888 * @pf: board private structure
bc7d338f 6889 * @reinit: if the Main VSI needs to re-initialized.
f650a38b 6890 **/
bc7d338f 6891static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
f650a38b 6892{
f650a38b 6893 struct i40e_hw *hw = &pf->hw;
cafa2ee6 6894 u8 set_fc_aq_fail = 0;
f650a38b 6895 i40e_status ret;
4f2f017c 6896 u32 val;
f650a38b
ASJ
6897 u32 v;
6898
41c445ff
JB
6899 /* Now we wait for GRST to settle out.
6900 * We don't have to delete the VEBs or VSIs from the hw switch
6901 * because the reset will make them disappear.
6902 */
6903 ret = i40e_pf_reset(hw);
b5565400 6904 if (ret) {
41c445ff 6905 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
a316f651
ASJ
6906 set_bit(__I40E_RESET_FAILED, &pf->state);
6907 goto clear_recovery;
b5565400 6908 }
41c445ff
JB
6909 pf->pfr_count++;
6910
6911 if (test_bit(__I40E_DOWN, &pf->state))
a316f651 6912 goto clear_recovery;
69bfb110 6913 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
41c445ff
JB
6914
6915 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
6916 ret = i40e_init_adminq(&pf->hw);
6917 if (ret) {
f1c7e72e
SN
6918 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
6919 i40e_stat_str(&pf->hw, ret),
6920 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
a316f651 6921 goto clear_recovery;
41c445ff
JB
6922 }
6923
4eb3f768 6924 /* re-verify the eeprom if we just had an EMP reset */
9df42d1a 6925 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
4eb3f768 6926 i40e_verify_eeprom(pf);
4eb3f768 6927
e78ac4bf 6928 i40e_clear_pxe_mode(hw);
41c445ff 6929 ret = i40e_get_capabilities(pf);
f1c7e72e 6930 if (ret)
41c445ff 6931 goto end_core_reset;
41c445ff 6932
41c445ff
JB
6933 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
6934 hw->func_caps.num_rx_qp,
6935 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
6936 if (ret) {
6937 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
6938 goto end_core_reset;
6939 }
6940 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
6941 if (ret) {
6942 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
6943 goto end_core_reset;
6944 }
6945
4e3b35b0
NP
6946#ifdef CONFIG_I40E_DCB
6947 ret = i40e_init_pf_dcb(pf);
6948 if (ret) {
aebfc816
SN
6949 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
6950 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
6951 /* Continue without DCB enabled */
4e3b35b0
NP
6952 }
6953#endif /* CONFIG_I40E_DCB */
38e00438 6954#ifdef I40E_FCOE
21364bcf 6955 i40e_init_pf_fcoe(pf);
4e3b35b0 6956
38e00438 6957#endif
41c445ff 6958 /* do basic switch setup */
bc7d338f 6959 ret = i40e_setup_pf_switch(pf, reinit);
41c445ff
JB
6960 if (ret)
6961 goto end_core_reset;
6962
2f0aff41
SN
6963 /* The driver only wants link up/down and module qualification
6964 * reports from firmware. Note the negative logic.
7e2453fe
JB
6965 */
6966 ret = i40e_aq_set_phy_int_mask(&pf->hw,
2f0aff41 6967 ~(I40E_AQ_EVENT_LINK_UPDOWN |
867a79e3 6968 I40E_AQ_EVENT_MEDIA_NA |
2f0aff41 6969 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
7e2453fe 6970 if (ret)
f1c7e72e
SN
6971 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
6972 i40e_stat_str(&pf->hw, ret),
6973 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7e2453fe 6974
cafa2ee6
ASJ
6975 /* make sure our flow control settings are restored */
6976 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
6977 if (ret)
8279e495
NP
6978 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
6979 i40e_stat_str(&pf->hw, ret),
6980 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
cafa2ee6 6981
41c445ff
JB
6982 /* Rebuild the VSIs and VEBs that existed before reset.
6983 * They are still in our local switch element arrays, so only
6984 * need to rebuild the switch model in the HW.
6985 *
6986 * If there were VEBs but the reconstitution failed, we'll try
6987 * try to recover minimal use by getting the basic PF VSI working.
6988 */
6989 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
69bfb110 6990 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
41c445ff
JB
6991 /* find the one VEB connected to the MAC, and find orphans */
6992 for (v = 0; v < I40E_MAX_VEB; v++) {
6993 if (!pf->veb[v])
6994 continue;
6995
6996 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
6997 pf->veb[v]->uplink_seid == 0) {
6998 ret = i40e_reconstitute_veb(pf->veb[v]);
6999
7000 if (!ret)
7001 continue;
7002
7003 /* If Main VEB failed, we're in deep doodoo,
7004 * so give up rebuilding the switch and set up
7005 * for minimal rebuild of PF VSI.
7006 * If orphan failed, we'll report the error
7007 * but try to keep going.
7008 */
7009 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
7010 dev_info(&pf->pdev->dev,
7011 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
7012 ret);
7013 pf->vsi[pf->lan_vsi]->uplink_seid
7014 = pf->mac_seid;
7015 break;
7016 } else if (pf->veb[v]->uplink_seid == 0) {
7017 dev_info(&pf->pdev->dev,
7018 "rebuild of orphan VEB failed: %d\n",
7019 ret);
7020 }
7021 }
7022 }
7023 }
7024
7025 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
cde4cbc7 7026 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
41c445ff
JB
7027 /* no VEB, so rebuild only the Main VSI */
7028 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
7029 if (ret) {
7030 dev_info(&pf->pdev->dev,
7031 "rebuild of Main VSI failed: %d\n", ret);
7032 goto end_core_reset;
7033 }
7034 }
7035
4f2f017c
ASJ
7036 /* Reconfigure hardware for allowing smaller MSS in the case
7037 * of TSO, so that we avoid the MDD being fired and causing
7038 * a reset in the case of small MSS+TSO.
7039 */
7040#define I40E_REG_MSS 0x000E64DC
7041#define I40E_REG_MSS_MIN_MASK 0x3FF0000
7042#define I40E_64BYTE_MSS 0x400000
7043 val = rd32(hw, I40E_REG_MSS);
7044 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
7045 val &= ~I40E_REG_MSS_MIN_MASK;
7046 val |= I40E_64BYTE_MSS;
7047 wr32(hw, I40E_REG_MSS, val);
7048 }
7049
8eed76fa 7050 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
025b4a54
ASJ
7051 msleep(75);
7052 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
7053 if (ret)
f1c7e72e
SN
7054 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
7055 i40e_stat_str(&pf->hw, ret),
7056 i40e_aq_str(&pf->hw,
7057 pf->hw.aq.asq_last_status));
cafa2ee6 7058 }
41c445ff
JB
7059 /* reinit the misc interrupt */
7060 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7061 ret = i40e_setup_misc_vector(pf);
7062
e7358f54
ASJ
7063 /* Add a filter to drop all Flow control frames from any VSI from being
7064 * transmitted. By doing so we stop a malicious VF from sending out
7065 * PAUSE or PFC frames and potentially controlling traffic for other
7066 * PF/VF VSIs.
7067 * The FW can still send Flow control frames if enabled.
7068 */
7069 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
7070 pf->main_vsi_seid);
7071
41c445ff
JB
7072 /* restart the VSIs that were rebuilt and running before the reset */
7073 i40e_pf_unquiesce_all_vsi(pf);
7074
69f64b2b
MW
7075 if (pf->num_alloc_vfs) {
7076 for (v = 0; v < pf->num_alloc_vfs; v++)
7077 i40e_reset_vf(&pf->vf[v], true);
7078 }
7079
41c445ff 7080 /* tell the firmware that we're starting */
44033fac 7081 i40e_send_version(pf);
41c445ff
JB
7082
7083end_core_reset:
a316f651
ASJ
7084 clear_bit(__I40E_RESET_FAILED, &pf->state);
7085clear_recovery:
41c445ff
JB
7086 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
7087}
7088
f650a38b 7089/**
b40c82e6 7090 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
f650a38b
ASJ
7091 * @pf: board private structure
7092 *
7093 * Close up the VFs and other things in prep for a Core Reset,
7094 * then get ready to rebuild the world.
7095 **/
7096static void i40e_handle_reset_warning(struct i40e_pf *pf)
7097{
23cfbe07
SN
7098 i40e_prep_for_reset(pf);
7099 i40e_reset_and_rebuild(pf, false);
f650a38b
ASJ
7100}
7101
41c445ff
JB
7102/**
7103 * i40e_handle_mdd_event
b40c82e6 7104 * @pf: pointer to the PF structure
41c445ff
JB
7105 *
7106 * Called from the MDD irq handler to identify possibly malicious vfs
7107 **/
7108static void i40e_handle_mdd_event(struct i40e_pf *pf)
7109{
7110 struct i40e_hw *hw = &pf->hw;
7111 bool mdd_detected = false;
df430b12 7112 bool pf_mdd_detected = false;
41c445ff
JB
7113 struct i40e_vf *vf;
7114 u32 reg;
7115 int i;
7116
7117 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
7118 return;
7119
7120 /* find what triggered the MDD event */
7121 reg = rd32(hw, I40E_GL_MDET_TX);
7122 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
4c33f83a
ASJ
7123 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
7124 I40E_GL_MDET_TX_PF_NUM_SHIFT;
2089ad03 7125 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
4c33f83a 7126 I40E_GL_MDET_TX_VF_NUM_SHIFT;
013f6579 7127 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
4c33f83a 7128 I40E_GL_MDET_TX_EVENT_SHIFT;
2089ad03
MW
7129 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
7130 I40E_GL_MDET_TX_QUEUE_SHIFT) -
7131 pf->hw.func_caps.base_queue;
faf32978 7132 if (netif_msg_tx_err(pf))
b40c82e6 7133 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
faf32978 7134 event, queue, pf_num, vf_num);
41c445ff
JB
7135 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
7136 mdd_detected = true;
7137 }
7138 reg = rd32(hw, I40E_GL_MDET_RX);
7139 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
4c33f83a
ASJ
7140 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
7141 I40E_GL_MDET_RX_FUNCTION_SHIFT;
013f6579 7142 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
4c33f83a 7143 I40E_GL_MDET_RX_EVENT_SHIFT;
2089ad03
MW
7144 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
7145 I40E_GL_MDET_RX_QUEUE_SHIFT) -
7146 pf->hw.func_caps.base_queue;
faf32978
JB
7147 if (netif_msg_rx_err(pf))
7148 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
7149 event, queue, func);
41c445ff
JB
7150 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
7151 mdd_detected = true;
7152 }
7153
df430b12
NP
7154 if (mdd_detected) {
7155 reg = rd32(hw, I40E_PF_MDET_TX);
7156 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
7157 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
faf32978 7158 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
df430b12
NP
7159 pf_mdd_detected = true;
7160 }
7161 reg = rd32(hw, I40E_PF_MDET_RX);
7162 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
7163 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
faf32978 7164 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
df430b12
NP
7165 pf_mdd_detected = true;
7166 }
7167 /* Queue belongs to the PF, initiate a reset */
7168 if (pf_mdd_detected) {
7169 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
7170 i40e_service_event_schedule(pf);
7171 }
7172 }
7173
41c445ff
JB
7174 /* see if one of the VFs needs its hand slapped */
7175 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
7176 vf = &(pf->vf[i]);
7177 reg = rd32(hw, I40E_VP_MDET_TX(i));
7178 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
7179 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
7180 vf->num_mdd_events++;
faf32978
JB
7181 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
7182 i);
41c445ff
JB
7183 }
7184
7185 reg = rd32(hw, I40E_VP_MDET_RX(i));
7186 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
7187 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
7188 vf->num_mdd_events++;
faf32978
JB
7189 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
7190 i);
41c445ff
JB
7191 }
7192
7193 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
7194 dev_info(&pf->pdev->dev,
7195 "Too many MDD events on VF %d, disabled\n", i);
7196 dev_info(&pf->pdev->dev,
7197 "Use PF Control I/F to re-enable the VF\n");
7198 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
7199 }
7200 }
7201
7202 /* re-enable mdd interrupt cause */
7203 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
7204 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7205 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7206 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7207 i40e_flush(hw);
7208}
7209
a1c9a9d9 7210/**
6a899024 7211 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
a1c9a9d9
JK
7212 * @pf: board private structure
7213 **/
6a899024 7214static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
a1c9a9d9 7215{
a1c9a9d9
JK
7216 struct i40e_hw *hw = &pf->hw;
7217 i40e_status ret;
a1c9a9d9
JK
7218 __be16 port;
7219 int i;
7220
6a899024 7221 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
a1c9a9d9
JK
7222 return;
7223
6a899024 7224 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
a1c9a9d9
JK
7225
7226 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
6a899024
SA
7227 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
7228 pf->pending_udp_bitmap &= ~BIT_ULL(i);
7229 port = pf->udp_ports[i].index;
c22c06c8 7230 if (port)
b3f5c7bc
CW
7231 ret = i40e_aq_add_udp_tunnel(hw, port,
7232 pf->udp_ports[i].type,
7233 NULL, NULL);
c22c06c8
SN
7234 else
7235 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
a1c9a9d9
JK
7236
7237 if (ret) {
730a8f87
CW
7238 dev_dbg(&pf->pdev->dev,
7239 "%s %s port %d, index %d failed, err %s aq_err %s\n",
7240 pf->udp_ports[i].type ? "vxlan" : "geneve",
7241 port ? "add" : "delete",
7242 ntohs(port), i,
7243 i40e_stat_str(&pf->hw, ret),
7244 i40e_aq_str(&pf->hw,
f1c7e72e 7245 pf->hw.aq.asq_last_status));
6a899024 7246 pf->udp_ports[i].index = 0;
a1c9a9d9
JK
7247 }
7248 }
7249 }
7250}
7251
41c445ff
JB
7252/**
7253 * i40e_service_task - Run the driver's async subtasks
7254 * @work: pointer to work_struct containing our data
7255 **/
7256static void i40e_service_task(struct work_struct *work)
7257{
7258 struct i40e_pf *pf = container_of(work,
7259 struct i40e_pf,
7260 service_task);
7261 unsigned long start_time = jiffies;
7262
e57a2fea
SN
7263 /* don't bother with service tasks if a reset is in progress */
7264 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
e57a2fea
SN
7265 return;
7266 }
7267
91089033
MW
7268 if (test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
7269 return;
7270
b03a8c1f 7271 i40e_detect_recover_hung(pf);
2818ccd9 7272 i40e_sync_filters_subtask(pf);
41c445ff
JB
7273 i40e_reset_subtask(pf);
7274 i40e_handle_mdd_event(pf);
7275 i40e_vc_process_vflr_event(pf);
7276 i40e_watchdog_subtask(pf);
7277 i40e_fdir_reinit_subtask(pf);
e3219ce6 7278 i40e_client_subtask(pf);
41c445ff 7279 i40e_sync_filters_subtask(pf);
6a899024 7280 i40e_sync_udp_filters_subtask(pf);
41c445ff
JB
7281 i40e_clean_adminq_subtask(pf);
7282
91089033
MW
7283 /* flush memory to make sure state is correct before next watchdog */
7284 smp_mb__before_atomic();
7285 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
41c445ff
JB
7286
7287 /* If the tasks have taken longer than one timer cycle or there
7288 * is more work to be done, reschedule the service task now
7289 * rather than wait for the timer to tick again.
7290 */
7291 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
7292 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
7293 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
7294 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
7295 i40e_service_event_schedule(pf);
7296}
7297
7298/**
7299 * i40e_service_timer - timer callback
7300 * @data: pointer to PF struct
7301 **/
7302static void i40e_service_timer(unsigned long data)
7303{
7304 struct i40e_pf *pf = (struct i40e_pf *)data;
7305
7306 mod_timer(&pf->service_timer,
7307 round_jiffies(jiffies + pf->service_timer_period));
7308 i40e_service_event_schedule(pf);
7309}
7310
7311/**
7312 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7313 * @vsi: the VSI being configured
7314 **/
7315static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7316{
7317 struct i40e_pf *pf = vsi->back;
7318
7319 switch (vsi->type) {
7320 case I40E_VSI_MAIN:
7321 vsi->alloc_queue_pairs = pf->num_lan_qps;
7322 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7323 I40E_REQ_DESCRIPTOR_MULTIPLE);
7324 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7325 vsi->num_q_vectors = pf->num_lan_msix;
7326 else
7327 vsi->num_q_vectors = 1;
7328
7329 break;
7330
7331 case I40E_VSI_FDIR:
7332 vsi->alloc_queue_pairs = 1;
7333 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7334 I40E_REQ_DESCRIPTOR_MULTIPLE);
a70e407f 7335 vsi->num_q_vectors = pf->num_fdsb_msix;
41c445ff
JB
7336 break;
7337
7338 case I40E_VSI_VMDQ2:
7339 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7340 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7341 I40E_REQ_DESCRIPTOR_MULTIPLE);
7342 vsi->num_q_vectors = pf->num_vmdq_msix;
7343 break;
7344
7345 case I40E_VSI_SRIOV:
7346 vsi->alloc_queue_pairs = pf->num_vf_qps;
7347 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7348 I40E_REQ_DESCRIPTOR_MULTIPLE);
7349 break;
7350
38e00438
VD
7351#ifdef I40E_FCOE
7352 case I40E_VSI_FCOE:
7353 vsi->alloc_queue_pairs = pf->num_fcoe_qps;
7354 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7355 I40E_REQ_DESCRIPTOR_MULTIPLE);
7356 vsi->num_q_vectors = pf->num_fcoe_msix;
7357 break;
7358
7359#endif /* I40E_FCOE */
41c445ff
JB
7360 default:
7361 WARN_ON(1);
7362 return -ENODATA;
7363 }
7364
7365 return 0;
7366}
7367
f650a38b
ASJ
7368/**
7369 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7370 * @type: VSI pointer
bc7d338f 7371 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
f650a38b
ASJ
7372 *
7373 * On error: returns error code (negative)
7374 * On success: returns 0
7375 **/
bc7d338f 7376static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
f650a38b
ASJ
7377{
7378 int size;
7379 int ret = 0;
7380
ac6c5e3d 7381 /* allocate memory for both Tx and Rx ring pointers */
f650a38b
ASJ
7382 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
7383 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7384 if (!vsi->tx_rings)
7385 return -ENOMEM;
f650a38b
ASJ
7386 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
7387
bc7d338f
ASJ
7388 if (alloc_qvectors) {
7389 /* allocate memory for q_vector pointers */
f57e4fbd 7390 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
bc7d338f
ASJ
7391 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7392 if (!vsi->q_vectors) {
7393 ret = -ENOMEM;
7394 goto err_vectors;
7395 }
f650a38b
ASJ
7396 }
7397 return ret;
7398
7399err_vectors:
7400 kfree(vsi->tx_rings);
7401 return ret;
7402}
7403
41c445ff
JB
7404/**
7405 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7406 * @pf: board private structure
7407 * @type: type of VSI
7408 *
7409 * On error: returns error code (negative)
7410 * On success: returns vsi index in PF (positive)
7411 **/
7412static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7413{
7414 int ret = -ENODEV;
7415 struct i40e_vsi *vsi;
7416 int vsi_idx;
7417 int i;
7418
7419 /* Need to protect the allocation of the VSIs at the PF level */
7420 mutex_lock(&pf->switch_mutex);
7421
7422 /* VSI list may be fragmented if VSI creation/destruction has
7423 * been happening. We can afford to do a quick scan to look
7424 * for any free VSIs in the list.
7425 *
7426 * find next empty vsi slot, looping back around if necessary
7427 */
7428 i = pf->next_vsi;
505682cd 7429 while (i < pf->num_alloc_vsi && pf->vsi[i])
41c445ff 7430 i++;
505682cd 7431 if (i >= pf->num_alloc_vsi) {
41c445ff
JB
7432 i = 0;
7433 while (i < pf->next_vsi && pf->vsi[i])
7434 i++;
7435 }
7436
505682cd 7437 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
41c445ff
JB
7438 vsi_idx = i; /* Found one! */
7439 } else {
7440 ret = -ENODEV;
493fb300 7441 goto unlock_pf; /* out of VSI slots! */
41c445ff
JB
7442 }
7443 pf->next_vsi = ++i;
7444
7445 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7446 if (!vsi) {
7447 ret = -ENOMEM;
493fb300 7448 goto unlock_pf;
41c445ff
JB
7449 }
7450 vsi->type = type;
7451 vsi->back = pf;
7452 set_bit(__I40E_DOWN, &vsi->state);
7453 vsi->flags = 0;
7454 vsi->idx = vsi_idx;
ac26fc13 7455 vsi->int_rate_limit = 0;
5db4cb59
ASJ
7456 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7457 pf->rss_table_size : 64;
41c445ff
JB
7458 vsi->netdev_registered = false;
7459 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
278e7d0b 7460 hash_init(vsi->mac_filter_hash);
63741846 7461 vsi->irqs_ready = false;
41c445ff 7462
9f65e15b
AD
7463 ret = i40e_set_num_rings_in_vsi(vsi);
7464 if (ret)
7465 goto err_rings;
7466
bc7d338f 7467 ret = i40e_vsi_alloc_arrays(vsi, true);
f650a38b 7468 if (ret)
9f65e15b 7469 goto err_rings;
493fb300 7470
41c445ff
JB
7471 /* Setup default MSIX irq handler for VSI */
7472 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7473
21659035 7474 /* Initialize VSI lock */
278e7d0b 7475 spin_lock_init(&vsi->mac_filter_hash_lock);
41c445ff
JB
7476 pf->vsi[vsi_idx] = vsi;
7477 ret = vsi_idx;
493fb300
AD
7478 goto unlock_pf;
7479
9f65e15b 7480err_rings:
493fb300
AD
7481 pf->next_vsi = i - 1;
7482 kfree(vsi);
7483unlock_pf:
41c445ff
JB
7484 mutex_unlock(&pf->switch_mutex);
7485 return ret;
7486}
7487
f650a38b
ASJ
7488/**
7489 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7490 * @type: VSI pointer
bc7d338f 7491 * @free_qvectors: a bool to specify if q_vectors need to be freed.
f650a38b
ASJ
7492 *
7493 * On error: returns error code (negative)
7494 * On success: returns 0
7495 **/
bc7d338f 7496static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
f650a38b
ASJ
7497{
7498 /* free the ring and vector containers */
bc7d338f
ASJ
7499 if (free_qvectors) {
7500 kfree(vsi->q_vectors);
7501 vsi->q_vectors = NULL;
7502 }
f650a38b
ASJ
7503 kfree(vsi->tx_rings);
7504 vsi->tx_rings = NULL;
7505 vsi->rx_rings = NULL;
7506}
7507
28c5869f
HZ
7508/**
7509 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7510 * and lookup table
7511 * @vsi: Pointer to VSI structure
7512 */
7513static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7514{
7515 if (!vsi)
7516 return;
7517
7518 kfree(vsi->rss_hkey_user);
7519 vsi->rss_hkey_user = NULL;
7520
7521 kfree(vsi->rss_lut_user);
7522 vsi->rss_lut_user = NULL;
7523}
7524
41c445ff
JB
7525/**
7526 * i40e_vsi_clear - Deallocate the VSI provided
7527 * @vsi: the VSI being un-configured
7528 **/
7529static int i40e_vsi_clear(struct i40e_vsi *vsi)
7530{
7531 struct i40e_pf *pf;
7532
7533 if (!vsi)
7534 return 0;
7535
7536 if (!vsi->back)
7537 goto free_vsi;
7538 pf = vsi->back;
7539
7540 mutex_lock(&pf->switch_mutex);
7541 if (!pf->vsi[vsi->idx]) {
7542 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7543 vsi->idx, vsi->idx, vsi, vsi->type);
7544 goto unlock_vsi;
7545 }
7546
7547 if (pf->vsi[vsi->idx] != vsi) {
7548 dev_err(&pf->pdev->dev,
7549 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7550 pf->vsi[vsi->idx]->idx,
7551 pf->vsi[vsi->idx],
7552 pf->vsi[vsi->idx]->type,
7553 vsi->idx, vsi, vsi->type);
7554 goto unlock_vsi;
7555 }
7556
b40c82e6 7557 /* updates the PF for this cleared vsi */
41c445ff
JB
7558 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7559 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7560
bc7d338f 7561 i40e_vsi_free_arrays(vsi, true);
28c5869f 7562 i40e_clear_rss_config_user(vsi);
493fb300 7563
41c445ff
JB
7564 pf->vsi[vsi->idx] = NULL;
7565 if (vsi->idx < pf->next_vsi)
7566 pf->next_vsi = vsi->idx;
7567
7568unlock_vsi:
7569 mutex_unlock(&pf->switch_mutex);
7570free_vsi:
7571 kfree(vsi);
7572
7573 return 0;
7574}
7575
9f65e15b
AD
7576/**
7577 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7578 * @vsi: the VSI being cleaned
7579 **/
be1d5eea 7580static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
9f65e15b
AD
7581{
7582 int i;
7583
8e9dca53 7584 if (vsi->tx_rings && vsi->tx_rings[0]) {
d7397644 7585 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
00403f04
MW
7586 kfree_rcu(vsi->tx_rings[i], rcu);
7587 vsi->tx_rings[i] = NULL;
7588 vsi->rx_rings[i] = NULL;
7589 }
be1d5eea 7590 }
9f65e15b
AD
7591}
7592
41c445ff
JB
7593/**
7594 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7595 * @vsi: the VSI being configured
7596 **/
7597static int i40e_alloc_rings(struct i40e_vsi *vsi)
7598{
e7046ee1 7599 struct i40e_ring *tx_ring, *rx_ring;
41c445ff 7600 struct i40e_pf *pf = vsi->back;
41c445ff
JB
7601 int i;
7602
41c445ff 7603 /* Set basic values in the rings to be used later during open() */
d7397644 7604 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
ac6c5e3d 7605 /* allocate space for both Tx and Rx in one shot */
9f65e15b
AD
7606 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
7607 if (!tx_ring)
7608 goto err_out;
41c445ff
JB
7609
7610 tx_ring->queue_index = i;
7611 tx_ring->reg_idx = vsi->base_queue + i;
7612 tx_ring->ring_active = false;
7613 tx_ring->vsi = vsi;
7614 tx_ring->netdev = vsi->netdev;
7615 tx_ring->dev = &pf->pdev->dev;
7616 tx_ring->count = vsi->num_desc;
7617 tx_ring->size = 0;
7618 tx_ring->dcb_tc = 0;
8e0764b4
ASJ
7619 if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
7620 tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
a75e8005 7621 tx_ring->tx_itr_setting = pf->tx_itr_default;
9f65e15b 7622 vsi->tx_rings[i] = tx_ring;
41c445ff 7623
9f65e15b 7624 rx_ring = &tx_ring[1];
41c445ff
JB
7625 rx_ring->queue_index = i;
7626 rx_ring->reg_idx = vsi->base_queue + i;
7627 rx_ring->ring_active = false;
7628 rx_ring->vsi = vsi;
7629 rx_ring->netdev = vsi->netdev;
7630 rx_ring->dev = &pf->pdev->dev;
7631 rx_ring->count = vsi->num_desc;
7632 rx_ring->size = 0;
7633 rx_ring->dcb_tc = 0;
a75e8005 7634 rx_ring->rx_itr_setting = pf->rx_itr_default;
9f65e15b 7635 vsi->rx_rings[i] = rx_ring;
41c445ff
JB
7636 }
7637
7638 return 0;
9f65e15b
AD
7639
7640err_out:
7641 i40e_vsi_clear_rings(vsi);
7642 return -ENOMEM;
41c445ff
JB
7643}
7644
7645/**
7646 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7647 * @pf: board private structure
7648 * @vectors: the number of MSI-X vectors to request
7649 *
7650 * Returns the number of vectors reserved, or error
7651 **/
7652static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7653{
7b37f376
AG
7654 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7655 I40E_MIN_MSIX, vectors);
7656 if (vectors < 0) {
41c445ff 7657 dev_info(&pf->pdev->dev,
7b37f376 7658 "MSI-X vector reservation failed: %d\n", vectors);
41c445ff
JB
7659 vectors = 0;
7660 }
7661
7662 return vectors;
7663}
7664
7665/**
7666 * i40e_init_msix - Setup the MSIX capability
7667 * @pf: board private structure
7668 *
7669 * Work with the OS to set up the MSIX vectors needed.
7670 *
3b444399 7671 * Returns the number of vectors reserved or negative on failure
41c445ff
JB
7672 **/
7673static int i40e_init_msix(struct i40e_pf *pf)
7674{
41c445ff 7675 struct i40e_hw *hw = &pf->hw;
1e200e4a 7676 int vectors_left;
41c445ff 7677 int v_budget, i;
3b444399 7678 int v_actual;
e3219ce6 7679 int iwarp_requested = 0;
41c445ff
JB
7680
7681 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7682 return -ENODEV;
7683
7684 /* The number of vectors we'll request will be comprised of:
7685 * - Add 1 for "other" cause for Admin Queue events, etc.
7686 * - The number of LAN queue pairs
f8ff1464
ASJ
7687 * - Queues being used for RSS.
7688 * We don't need as many as max_rss_size vectors.
7689 * use rss_size instead in the calculation since that
7690 * is governed by number of cpus in the system.
7691 * - assumes symmetric Tx/Rx pairing
41c445ff 7692 * - The number of VMDq pairs
e3219ce6 7693 * - The CPU count within the NUMA node if iWARP is enabled
38e00438
VD
7694#ifdef I40E_FCOE
7695 * - The number of FCOE qps.
7696#endif
41c445ff
JB
7697 * Once we count this up, try the request.
7698 *
7699 * If we can't get what we want, we'll simplify to nearly nothing
7700 * and try again. If that still fails, we punt.
7701 */
1e200e4a
SN
7702 vectors_left = hw->func_caps.num_msix_vectors;
7703 v_budget = 0;
7704
7705 /* reserve one vector for miscellaneous handler */
7706 if (vectors_left) {
7707 v_budget++;
7708 vectors_left--;
7709 }
7710
7711 /* reserve vectors for the main PF traffic queues */
7712 pf->num_lan_msix = min_t(int, num_online_cpus(), vectors_left);
7713 vectors_left -= pf->num_lan_msix;
7714 v_budget += pf->num_lan_msix;
7715
7716 /* reserve one vector for sideband flow director */
7717 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7718 if (vectors_left) {
a70e407f 7719 pf->num_fdsb_msix = 1;
1e200e4a
SN
7720 v_budget++;
7721 vectors_left--;
7722 } else {
a70e407f 7723 pf->num_fdsb_msix = 0;
1e200e4a
SN
7724 }
7725 }
83840e4b 7726
38e00438 7727#ifdef I40E_FCOE
1e200e4a 7728 /* can we reserve enough for FCoE? */
38e00438 7729 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
1e200e4a
SN
7730 if (!vectors_left)
7731 pf->num_fcoe_msix = 0;
7732 else if (vectors_left >= pf->num_fcoe_qps)
7733 pf->num_fcoe_msix = pf->num_fcoe_qps;
7734 else
7735 pf->num_fcoe_msix = 1;
38e00438 7736 v_budget += pf->num_fcoe_msix;
1e200e4a 7737 vectors_left -= pf->num_fcoe_msix;
38e00438 7738 }
1e200e4a 7739
38e00438 7740#endif
e3219ce6
ASJ
7741 /* can we reserve enough for iWARP? */
7742 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
4ce20abc
SA
7743 iwarp_requested = pf->num_iwarp_msix;
7744
e3219ce6
ASJ
7745 if (!vectors_left)
7746 pf->num_iwarp_msix = 0;
7747 else if (vectors_left < pf->num_iwarp_msix)
7748 pf->num_iwarp_msix = 1;
7749 v_budget += pf->num_iwarp_msix;
7750 vectors_left -= pf->num_iwarp_msix;
7751 }
7752
1e200e4a
SN
7753 /* any vectors left over go for VMDq support */
7754 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
7755 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
7756 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
7757
9ca57e97
SA
7758 if (!vectors_left) {
7759 pf->num_vmdq_msix = 0;
7760 pf->num_vmdq_qps = 0;
7761 } else {
7762 /* if we're short on vectors for what's desired, we limit
7763 * the queues per vmdq. If this is still more than are
7764 * available, the user will need to change the number of
7765 * queues/vectors used by the PF later with the ethtool
7766 * channels command
7767 */
7768 if (vmdq_vecs < vmdq_vecs_wanted)
7769 pf->num_vmdq_qps = 1;
7770 pf->num_vmdq_msix = pf->num_vmdq_qps;
1e200e4a 7771
9ca57e97
SA
7772 v_budget += vmdq_vecs;
7773 vectors_left -= vmdq_vecs;
7774 }
1e200e4a 7775 }
41c445ff
JB
7776
7777 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
7778 GFP_KERNEL);
7779 if (!pf->msix_entries)
7780 return -ENOMEM;
7781
7782 for (i = 0; i < v_budget; i++)
7783 pf->msix_entries[i].entry = i;
3b444399 7784 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
a34977ba 7785
3b444399 7786 if (v_actual < I40E_MIN_MSIX) {
41c445ff
JB
7787 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
7788 kfree(pf->msix_entries);
7789 pf->msix_entries = NULL;
4c95aa5d 7790 pci_disable_msix(pf->pdev);
41c445ff
JB
7791 return -ENODEV;
7792
3b444399 7793 } else if (v_actual == I40E_MIN_MSIX) {
41c445ff 7794 /* Adjust for minimal MSIX use */
41c445ff
JB
7795 pf->num_vmdq_vsis = 0;
7796 pf->num_vmdq_qps = 0;
41c445ff
JB
7797 pf->num_lan_qps = 1;
7798 pf->num_lan_msix = 1;
7799
4ce20abc
SA
7800 } else if (!vectors_left) {
7801 /* If we have limited resources, we will start with no vectors
7802 * for the special features and then allocate vectors to some
7803 * of these features based on the policy and at the end disable
7804 * the features that did not get any vectors.
7805 */
3b444399
SN
7806 int vec;
7807
4ce20abc
SA
7808 dev_info(&pf->pdev->dev,
7809 "MSI-X vector limit reached, attempting to redistribute vectors\n");
a34977ba 7810 /* reserve the misc vector */
3b444399 7811 vec = v_actual - 1;
a34977ba 7812
41c445ff
JB
7813 /* Scale vector usage down */
7814 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
a34977ba 7815 pf->num_vmdq_vsis = 1;
1e200e4a 7816 pf->num_vmdq_qps = 1;
4ce20abc
SA
7817#ifdef I40E_FCOE
7818 pf->num_fcoe_qps = 0;
7819 pf->num_fcoe_msix = 0;
7820#endif
41c445ff
JB
7821
7822 /* partition out the remaining vectors */
7823 switch (vec) {
7824 case 2:
41c445ff
JB
7825 pf->num_lan_msix = 1;
7826 break;
7827 case 3:
e3219ce6
ASJ
7828 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7829 pf->num_lan_msix = 1;
7830 pf->num_iwarp_msix = 1;
7831 } else {
7832 pf->num_lan_msix = 2;
7833 }
38e00438
VD
7834#ifdef I40E_FCOE
7835 /* give one vector to FCoE */
7836 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7837 pf->num_lan_msix = 1;
7838 pf->num_fcoe_msix = 1;
7839 }
38e00438 7840#endif
41c445ff
JB
7841 break;
7842 default:
e3219ce6
ASJ
7843 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7844 pf->num_iwarp_msix = min_t(int, (vec / 3),
7845 iwarp_requested);
7846 pf->num_vmdq_vsis = min_t(int, (vec / 3),
7847 I40E_DEFAULT_NUM_VMDQ_VSI);
7848 } else {
7849 pf->num_vmdq_vsis = min_t(int, (vec / 2),
7850 I40E_DEFAULT_NUM_VMDQ_VSI);
7851 }
abd97a94
SA
7852 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7853 pf->num_fdsb_msix = 1;
7854 vec--;
7855 }
e3219ce6
ASJ
7856 pf->num_lan_msix = min_t(int,
7857 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
7858 pf->num_lan_msix);
4ce20abc 7859 pf->num_lan_qps = pf->num_lan_msix;
38e00438
VD
7860#ifdef I40E_FCOE
7861 /* give one vector to FCoE */
7862 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7863 pf->num_fcoe_msix = 1;
7864 vec--;
7865 }
7866#endif
41c445ff
JB
7867 break;
7868 }
7869 }
7870
abd97a94
SA
7871 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
7872 (pf->num_fdsb_msix == 0)) {
7873 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
7874 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7875 }
a34977ba
ASJ
7876 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7877 (pf->num_vmdq_msix == 0)) {
7878 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
7879 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
7880 }
e3219ce6
ASJ
7881
7882 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
7883 (pf->num_iwarp_msix == 0)) {
7884 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
7885 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
7886 }
38e00438
VD
7887#ifdef I40E_FCOE
7888
7889 if ((pf->flags & I40E_FLAG_FCOE_ENABLED) && (pf->num_fcoe_msix == 0)) {
7890 dev_info(&pf->pdev->dev, "FCOE disabled, not enough MSI-X vectors\n");
7891 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
7892 }
7893#endif
4ce20abc
SA
7894 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
7895 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
7896 pf->num_lan_msix,
7897 pf->num_vmdq_msix * pf->num_vmdq_vsis,
7898 pf->num_fdsb_msix,
7899 pf->num_iwarp_msix);
7900
3b444399 7901 return v_actual;
41c445ff
JB
7902}
7903
493fb300 7904/**
90e04070 7905 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
493fb300
AD
7906 * @vsi: the VSI being configured
7907 * @v_idx: index of the vector in the vsi struct
7f6c5539 7908 * @cpu: cpu to be used on affinity_mask
493fb300
AD
7909 *
7910 * We allocate one q_vector. If allocation fails we return -ENOMEM.
7911 **/
7f6c5539 7912static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
493fb300
AD
7913{
7914 struct i40e_q_vector *q_vector;
7915
7916 /* allocate q_vector */
7917 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
7918 if (!q_vector)
7919 return -ENOMEM;
7920
7921 q_vector->vsi = vsi;
7922 q_vector->v_idx = v_idx;
7f6c5539
GP
7923 cpumask_set_cpu(cpu, &q_vector->affinity_mask);
7924
493fb300
AD
7925 if (vsi->netdev)
7926 netif_napi_add(vsi->netdev, &q_vector->napi,
eefeacee 7927 i40e_napi_poll, NAPI_POLL_WEIGHT);
493fb300 7928
cd0b6fa6
AD
7929 q_vector->rx.latency_range = I40E_LOW_LATENCY;
7930 q_vector->tx.latency_range = I40E_LOW_LATENCY;
7931
493fb300
AD
7932 /* tie q_vector and vsi together */
7933 vsi->q_vectors[v_idx] = q_vector;
7934
7935 return 0;
7936}
7937
41c445ff 7938/**
90e04070 7939 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
41c445ff
JB
7940 * @vsi: the VSI being configured
7941 *
7942 * We allocate one q_vector per queue interrupt. If allocation fails we
7943 * return -ENOMEM.
7944 **/
90e04070 7945static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
41c445ff
JB
7946{
7947 struct i40e_pf *pf = vsi->back;
7f6c5539 7948 int err, v_idx, num_q_vectors, current_cpu;
41c445ff
JB
7949
7950 /* if not MSIX, give the one vector only to the LAN VSI */
7951 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7952 num_q_vectors = vsi->num_q_vectors;
7953 else if (vsi == pf->vsi[pf->lan_vsi])
7954 num_q_vectors = 1;
7955 else
7956 return -EINVAL;
7957
7f6c5539
GP
7958 current_cpu = cpumask_first(cpu_online_mask);
7959
41c445ff 7960 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
7f6c5539 7961 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
493fb300
AD
7962 if (err)
7963 goto err_out;
7f6c5539
GP
7964 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
7965 if (unlikely(current_cpu >= nr_cpu_ids))
7966 current_cpu = cpumask_first(cpu_online_mask);
41c445ff
JB
7967 }
7968
7969 return 0;
493fb300
AD
7970
7971err_out:
7972 while (v_idx--)
7973 i40e_free_q_vector(vsi, v_idx);
7974
7975 return err;
41c445ff
JB
7976}
7977
7978/**
7979 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
7980 * @pf: board private structure to initialize
7981 **/
c1147280 7982static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
41c445ff 7983{
3b444399
SN
7984 int vectors = 0;
7985 ssize_t size;
41c445ff
JB
7986
7987 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3b444399
SN
7988 vectors = i40e_init_msix(pf);
7989 if (vectors < 0) {
60ea5f83 7990 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
e3219ce6 7991 I40E_FLAG_IWARP_ENABLED |
38e00438
VD
7992#ifdef I40E_FCOE
7993 I40E_FLAG_FCOE_ENABLED |
7994#endif
60ea5f83 7995 I40E_FLAG_RSS_ENABLED |
4d9b6043 7996 I40E_FLAG_DCB_CAPABLE |
a036244c 7997 I40E_FLAG_DCB_ENABLED |
60ea5f83
JB
7998 I40E_FLAG_SRIOV_ENABLED |
7999 I40E_FLAG_FD_SB_ENABLED |
8000 I40E_FLAG_FD_ATR_ENABLED |
8001 I40E_FLAG_VMDQ_ENABLED);
41c445ff
JB
8002
8003 /* rework the queue expectations without MSIX */
8004 i40e_determine_queue_usage(pf);
8005 }
8006 }
8007
8008 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
8009 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
77fa28be 8010 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
3b444399
SN
8011 vectors = pci_enable_msi(pf->pdev);
8012 if (vectors < 0) {
8013 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
8014 vectors);
41c445ff
JB
8015 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
8016 }
3b444399 8017 vectors = 1; /* one MSI or Legacy vector */
41c445ff
JB
8018 }
8019
958a3e3b 8020 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
77fa28be 8021 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
958a3e3b 8022
3b444399
SN
8023 /* set up vector assignment tracking */
8024 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
8025 pf->irq_pile = kzalloc(size, GFP_KERNEL);
c1147280
JB
8026 if (!pf->irq_pile) {
8027 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
8028 return -ENOMEM;
8029 }
3b444399
SN
8030 pf->irq_pile->num_entries = vectors;
8031 pf->irq_pile->search_hint = 0;
8032
c1147280 8033 /* track first vector for misc interrupts, ignore return */
3b444399 8034 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
c1147280
JB
8035
8036 return 0;
41c445ff
JB
8037}
8038
8039/**
8040 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
8041 * @pf: board private structure
8042 *
8043 * This sets up the handler for MSIX 0, which is used to manage the
8044 * non-queue interrupts, e.g. AdminQ and errors. This is not used
8045 * when in MSI or Legacy interrupt mode.
8046 **/
8047static int i40e_setup_misc_vector(struct i40e_pf *pf)
8048{
8049 struct i40e_hw *hw = &pf->hw;
8050 int err = 0;
8051
8052 /* Only request the irq if this is the first time through, and
8053 * not when we're rebuilding after a Reset
8054 */
8055 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
8056 err = request_irq(pf->msix_entries[0].vector,
b294ac70 8057 i40e_intr, 0, pf->int_name, pf);
41c445ff
JB
8058 if (err) {
8059 dev_info(&pf->pdev->dev,
77fa28be 8060 "request_irq for %s failed: %d\n",
b294ac70 8061 pf->int_name, err);
41c445ff
JB
8062 return -EFAULT;
8063 }
8064 }
8065
ab437b5a 8066 i40e_enable_misc_int_causes(pf);
41c445ff
JB
8067
8068 /* associate no queues to the misc vector */
8069 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
8070 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
8071
8072 i40e_flush(hw);
8073
40d72a50 8074 i40e_irq_dynamic_enable_icr0(pf, true);
41c445ff
JB
8075
8076 return err;
8077}
8078
8079/**
e25d00b8
ASJ
8080 * i40e_config_rss_aq - Prepare for RSS using AQ commands
8081 * @vsi: vsi structure
8082 * @seed: RSS hash seed
8083 **/
e69ff813
HZ
8084static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8085 u8 *lut, u16 lut_size)
e25d00b8 8086{
e25d00b8
ASJ
8087 struct i40e_pf *pf = vsi->back;
8088 struct i40e_hw *hw = &pf->hw;
776b2e15 8089 int ret = 0;
e25d00b8 8090
776b2e15
JK
8091 if (seed) {
8092 struct i40e_aqc_get_set_rss_key_data *seed_dw =
8093 (struct i40e_aqc_get_set_rss_key_data *)seed;
8094 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
8095 if (ret) {
8096 dev_info(&pf->pdev->dev,
8097 "Cannot set RSS key, err %s aq_err %s\n",
8098 i40e_stat_str(hw, ret),
8099 i40e_aq_str(hw, hw->aq.asq_last_status));
8100 return ret;
8101 }
e25d00b8 8102 }
776b2e15
JK
8103 if (lut) {
8104 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
e25d00b8 8105
776b2e15
JK
8106 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8107 if (ret) {
8108 dev_info(&pf->pdev->dev,
8109 "Cannot set RSS lut, err %s aq_err %s\n",
8110 i40e_stat_str(hw, ret),
8111 i40e_aq_str(hw, hw->aq.asq_last_status));
8112 return ret;
8113 }
8114 }
e25d00b8
ASJ
8115 return ret;
8116}
8117
95a73780
ASJ
8118/**
8119 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
8120 * @vsi: Pointer to vsi structure
8121 * @seed: Buffter to store the hash keys
8122 * @lut: Buffer to store the lookup table entries
8123 * @lut_size: Size of buffer to store the lookup table entries
8124 *
8125 * Return 0 on success, negative on failure
8126 */
8127static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8128 u8 *lut, u16 lut_size)
8129{
8130 struct i40e_pf *pf = vsi->back;
8131 struct i40e_hw *hw = &pf->hw;
8132 int ret = 0;
8133
8134 if (seed) {
8135 ret = i40e_aq_get_rss_key(hw, vsi->id,
8136 (struct i40e_aqc_get_set_rss_key_data *)seed);
8137 if (ret) {
8138 dev_info(&pf->pdev->dev,
8139 "Cannot get RSS key, err %s aq_err %s\n",
8140 i40e_stat_str(&pf->hw, ret),
8141 i40e_aq_str(&pf->hw,
8142 pf->hw.aq.asq_last_status));
8143 return ret;
8144 }
8145 }
8146
8147 if (lut) {
8148 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
8149
8150 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8151 if (ret) {
8152 dev_info(&pf->pdev->dev,
8153 "Cannot get RSS lut, err %s aq_err %s\n",
8154 i40e_stat_str(&pf->hw, ret),
8155 i40e_aq_str(&pf->hw,
8156 pf->hw.aq.asq_last_status));
8157 return ret;
8158 }
8159 }
8160
8161 return ret;
8162}
8163
0582b964
JK
8164/**
8165 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
8166 * @vsi: VSI structure
8167 **/
8168static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
8169{
8170 u8 seed[I40E_HKEY_ARRAY_SIZE];
8171 struct i40e_pf *pf = vsi->back;
8172 u8 *lut;
8173 int ret;
8174
8175 if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
8176 return 0;
8177
552b9962
JK
8178 if (!vsi->rss_size)
8179 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8180 vsi->num_queue_pairs);
8181 if (!vsi->rss_size)
8182 return -EINVAL;
8183
0582b964
JK
8184 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8185 if (!lut)
8186 return -ENOMEM;
552b9962
JK
8187 /* Use the user configured hash keys and lookup table if there is one,
8188 * otherwise use default
8189 */
8190 if (vsi->rss_lut_user)
8191 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8192 else
8193 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
8194 if (vsi->rss_hkey_user)
8195 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8196 else
8197 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
0582b964
JK
8198 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
8199 kfree(lut);
8200
8201 return ret;
8202}
8203
e25d00b8 8204/**
043dd650 8205 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
e69ff813 8206 * @vsi: Pointer to vsi structure
e25d00b8 8207 * @seed: RSS hash seed
e69ff813
HZ
8208 * @lut: Lookup table
8209 * @lut_size: Lookup table size
8210 *
8211 * Returns 0 on success, negative on failure
41c445ff 8212 **/
e69ff813
HZ
8213static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
8214 const u8 *lut, u16 lut_size)
41c445ff 8215{
e69ff813 8216 struct i40e_pf *pf = vsi->back;
4617e8c0 8217 struct i40e_hw *hw = &pf->hw;
c4e1868c 8218 u16 vf_id = vsi->vf_id;
e69ff813 8219 u8 i;
41c445ff 8220
e25d00b8 8221 /* Fill out hash function seed */
e69ff813
HZ
8222 if (seed) {
8223 u32 *seed_dw = (u32 *)seed;
8224
c4e1868c
MW
8225 if (vsi->type == I40E_VSI_MAIN) {
8226 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
8227 i40e_write_rx_ctl(hw, I40E_PFQF_HKEY(i),
8228 seed_dw[i]);
8229 } else if (vsi->type == I40E_VSI_SRIOV) {
8230 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
8231 i40e_write_rx_ctl(hw,
8232 I40E_VFQF_HKEY1(i, vf_id),
8233 seed_dw[i]);
8234 } else {
8235 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
8236 }
e69ff813
HZ
8237 }
8238
8239 if (lut) {
8240 u32 *lut_dw = (u32 *)lut;
8241
c4e1868c
MW
8242 if (vsi->type == I40E_VSI_MAIN) {
8243 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8244 return -EINVAL;
8245 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8246 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
8247 } else if (vsi->type == I40E_VSI_SRIOV) {
8248 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
8249 return -EINVAL;
8250 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8251 i40e_write_rx_ctl(hw,
8252 I40E_VFQF_HLUT1(i, vf_id),
8253 lut_dw[i]);
8254 } else {
8255 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8256 }
e25d00b8
ASJ
8257 }
8258 i40e_flush(hw);
8259
8260 return 0;
8261}
8262
043dd650
HZ
8263/**
8264 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
8265 * @vsi: Pointer to VSI structure
8266 * @seed: Buffer to store the keys
8267 * @lut: Buffer to store the lookup table entries
8268 * @lut_size: Size of buffer to store the lookup table entries
8269 *
8270 * Returns 0 on success, negative on failure
8271 */
8272static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
8273 u8 *lut, u16 lut_size)
8274{
8275 struct i40e_pf *pf = vsi->back;
8276 struct i40e_hw *hw = &pf->hw;
8277 u16 i;
8278
8279 if (seed) {
8280 u32 *seed_dw = (u32 *)seed;
8281
8282 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
272cdaf2 8283 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
043dd650
HZ
8284 }
8285 if (lut) {
8286 u32 *lut_dw = (u32 *)lut;
8287
8288 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8289 return -EINVAL;
8290 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8291 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
8292 }
8293
8294 return 0;
8295}
8296
8297/**
8298 * i40e_config_rss - Configure RSS keys and lut
8299 * @vsi: Pointer to VSI structure
8300 * @seed: RSS hash seed
8301 * @lut: Lookup table
8302 * @lut_size: Lookup table size
8303 *
8304 * Returns 0 on success, negative on failure
8305 */
8306int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8307{
8308 struct i40e_pf *pf = vsi->back;
8309
8310 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8311 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
8312 else
8313 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
8314}
8315
8316/**
8317 * i40e_get_rss - Get RSS keys and lut
8318 * @vsi: Pointer to VSI structure
8319 * @seed: Buffer to store the keys
8320 * @lut: Buffer to store the lookup table entries
8321 * lut_size: Size of buffer to store the lookup table entries
8322 *
8323 * Returns 0 on success, negative on failure
8324 */
8325int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8326{
95a73780
ASJ
8327 struct i40e_pf *pf = vsi->back;
8328
8329 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8330 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
8331 else
8332 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
043dd650
HZ
8333}
8334
e69ff813
HZ
8335/**
8336 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8337 * @pf: Pointer to board private structure
8338 * @lut: Lookup table
8339 * @rss_table_size: Lookup table size
8340 * @rss_size: Range of queue number for hashing
8341 */
f1582351
AB
8342void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8343 u16 rss_table_size, u16 rss_size)
e69ff813
HZ
8344{
8345 u16 i;
8346
8347 for (i = 0; i < rss_table_size; i++)
8348 lut[i] = i % rss_size;
8349}
8350
e25d00b8 8351/**
043dd650 8352 * i40e_pf_config_rss - Prepare for RSS if used
e25d00b8
ASJ
8353 * @pf: board private structure
8354 **/
043dd650 8355static int i40e_pf_config_rss(struct i40e_pf *pf)
e25d00b8
ASJ
8356{
8357 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8358 u8 seed[I40E_HKEY_ARRAY_SIZE];
e69ff813 8359 u8 *lut;
e25d00b8
ASJ
8360 struct i40e_hw *hw = &pf->hw;
8361 u32 reg_val;
8362 u64 hena;
e69ff813 8363 int ret;
e25d00b8 8364
41c445ff 8365 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
272cdaf2
SN
8366 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
8367 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
e25d00b8
ASJ
8368 hena |= i40e_pf_get_default_rss_hena(pf);
8369
272cdaf2
SN
8370 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
8371 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
41c445ff 8372
e25d00b8 8373 /* Determine the RSS table size based on the hardware capabilities */
272cdaf2 8374 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
e25d00b8
ASJ
8375 reg_val = (pf->rss_table_size == 512) ?
8376 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8377 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
272cdaf2 8378 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
e157ea30 8379
28c5869f
HZ
8380 /* Determine the RSS size of the VSI */
8381 if (!vsi->rss_size)
acd65448
HZ
8382 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8383 vsi->num_queue_pairs);
a4fa59cc
MW
8384 if (!vsi->rss_size)
8385 return -EINVAL;
28c5869f 8386
e69ff813
HZ
8387 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8388 if (!lut)
8389 return -ENOMEM;
8390
28c5869f
HZ
8391 /* Use user configured lut if there is one, otherwise use default */
8392 if (vsi->rss_lut_user)
8393 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8394 else
8395 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
e69ff813 8396
28c5869f
HZ
8397 /* Use user configured hash key if there is one, otherwise
8398 * use default.
8399 */
8400 if (vsi->rss_hkey_user)
8401 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8402 else
8403 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
043dd650 8404 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
e69ff813
HZ
8405 kfree(lut);
8406
8407 return ret;
41c445ff
JB
8408}
8409
f8ff1464
ASJ
8410/**
8411 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8412 * @pf: board private structure
8413 * @queue_count: the requested queue count for rss.
8414 *
8415 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8416 * count which may be different from the requested queue count.
8417 **/
8418int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8419{
9a3bd2f1
ASJ
8420 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8421 int new_rss_size;
8422
f8ff1464
ASJ
8423 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8424 return 0;
8425
9a3bd2f1 8426 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
f8ff1464 8427
9a3bd2f1
ASJ
8428 if (queue_count != vsi->num_queue_pairs) {
8429 vsi->req_queue_pairs = queue_count;
f8ff1464
ASJ
8430 i40e_prep_for_reset(pf);
8431
acd65448 8432 pf->alloc_rss_size = new_rss_size;
f8ff1464
ASJ
8433
8434 i40e_reset_and_rebuild(pf, true);
28c5869f
HZ
8435
8436 /* Discard the user configured hash keys and lut, if less
8437 * queues are enabled.
8438 */
8439 if (queue_count < vsi->rss_size) {
8440 i40e_clear_rss_config_user(vsi);
8441 dev_dbg(&pf->pdev->dev,
8442 "discard user configured hash keys and lut\n");
8443 }
8444
8445 /* Reset vsi->rss_size, as number of enabled queues changed */
acd65448
HZ
8446 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8447 vsi->num_queue_pairs);
28c5869f 8448
043dd650 8449 i40e_pf_config_rss(pf);
f8ff1464 8450 }
12815057
LY
8451 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
8452 vsi->req_queue_pairs, pf->rss_size_max);
acd65448 8453 return pf->alloc_rss_size;
f8ff1464
ASJ
8454}
8455
f4492db1
GR
8456/**
8457 * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
8458 * @pf: board private structure
8459 **/
8460i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
8461{
8462 i40e_status status;
8463 bool min_valid, max_valid;
8464 u32 max_bw, min_bw;
8465
8466 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8467 &min_valid, &max_valid);
8468
8469 if (!status) {
8470 if (min_valid)
8471 pf->npar_min_bw = min_bw;
8472 if (max_valid)
8473 pf->npar_max_bw = max_bw;
8474 }
8475
8476 return status;
8477}
8478
8479/**
8480 * i40e_set_npar_bw_setting - Set BW settings for this PF partition
8481 * @pf: board private structure
8482 **/
8483i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
8484{
8485 struct i40e_aqc_configure_partition_bw_data bw_data;
8486 i40e_status status;
8487
b40c82e6 8488 /* Set the valid bit for this PF */
41a1d04b 8489 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
f4492db1
GR
8490 bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
8491 bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
8492
8493 /* Set the new bandwidths */
8494 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8495
8496 return status;
8497}
8498
8499/**
8500 * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
8501 * @pf: board private structure
8502 **/
8503i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
8504{
8505 /* Commit temporary BW setting to permanent NVM image */
8506 enum i40e_admin_queue_err last_aq_status;
8507 i40e_status ret;
8508 u16 nvm_word;
8509
8510 if (pf->hw.partition_id != 1) {
8511 dev_info(&pf->pdev->dev,
8512 "Commit BW only works on partition 1! This is partition %d",
8513 pf->hw.partition_id);
8514 ret = I40E_NOT_SUPPORTED;
8515 goto bw_commit_out;
8516 }
8517
8518 /* Acquire NVM for read access */
8519 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8520 last_aq_status = pf->hw.aq.asq_last_status;
8521 if (ret) {
8522 dev_info(&pf->pdev->dev,
f1c7e72e
SN
8523 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8524 i40e_stat_str(&pf->hw, ret),
8525 i40e_aq_str(&pf->hw, last_aq_status));
f4492db1
GR
8526 goto bw_commit_out;
8527 }
8528
8529 /* Read word 0x10 of NVM - SW compatibility word 1 */
8530 ret = i40e_aq_read_nvm(&pf->hw,
8531 I40E_SR_NVM_CONTROL_WORD,
8532 0x10, sizeof(nvm_word), &nvm_word,
8533 false, NULL);
8534 /* Save off last admin queue command status before releasing
8535 * the NVM
8536 */
8537 last_aq_status = pf->hw.aq.asq_last_status;
8538 i40e_release_nvm(&pf->hw);
8539 if (ret) {
f1c7e72e
SN
8540 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
8541 i40e_stat_str(&pf->hw, ret),
8542 i40e_aq_str(&pf->hw, last_aq_status));
f4492db1
GR
8543 goto bw_commit_out;
8544 }
8545
8546 /* Wait a bit for NVM release to complete */
8547 msleep(50);
8548
8549 /* Acquire NVM for write access */
8550 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8551 last_aq_status = pf->hw.aq.asq_last_status;
8552 if (ret) {
8553 dev_info(&pf->pdev->dev,
f1c7e72e
SN
8554 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8555 i40e_stat_str(&pf->hw, ret),
8556 i40e_aq_str(&pf->hw, last_aq_status));
f4492db1
GR
8557 goto bw_commit_out;
8558 }
8559 /* Write it back out unchanged to initiate update NVM,
8560 * which will force a write of the shadow (alt) RAM to
8561 * the NVM - thus storing the bandwidth values permanently.
8562 */
8563 ret = i40e_aq_update_nvm(&pf->hw,
8564 I40E_SR_NVM_CONTROL_WORD,
8565 0x10, sizeof(nvm_word),
8566 &nvm_word, true, NULL);
8567 /* Save off last admin queue command status before releasing
8568 * the NVM
8569 */
8570 last_aq_status = pf->hw.aq.asq_last_status;
8571 i40e_release_nvm(&pf->hw);
8572 if (ret)
8573 dev_info(&pf->pdev->dev,
f1c7e72e
SN
8574 "BW settings NOT SAVED, err %s aq_err %s\n",
8575 i40e_stat_str(&pf->hw, ret),
8576 i40e_aq_str(&pf->hw, last_aq_status));
f4492db1
GR
8577bw_commit_out:
8578
8579 return ret;
8580}
8581
41c445ff
JB
8582/**
8583 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8584 * @pf: board private structure to initialize
8585 *
8586 * i40e_sw_init initializes the Adapter private data structure.
8587 * Fields are initialized based on PCI device information and
8588 * OS network device settings (MTU size).
8589 **/
8590static int i40e_sw_init(struct i40e_pf *pf)
8591{
8592 int err = 0;
8593 int size;
8594
41c445ff
JB
8595 /* Set default capability flags */
8596 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8597 I40E_FLAG_MSI_ENABLED |
2bc7ee8a
MW
8598 I40E_FLAG_MSIX_ENABLED;
8599
ca99eb99
MW
8600 /* Set default ITR */
8601 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8602 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8603
7134f9ce
JB
8604 /* Depending on PF configurations, it is possible that the RSS
8605 * maximum might end up larger than the available queues
8606 */
41a1d04b 8607 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
acd65448 8608 pf->alloc_rss_size = 1;
5db4cb59 8609 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
7134f9ce
JB
8610 pf->rss_size_max = min_t(int, pf->rss_size_max,
8611 pf->hw.func_caps.num_tx_qp);
41c445ff
JB
8612 if (pf->hw.func_caps.rss) {
8613 pf->flags |= I40E_FLAG_RSS_ENABLED;
acd65448
HZ
8614 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
8615 num_online_cpus());
41c445ff
JB
8616 }
8617
2050bc65 8618 /* MFP mode enabled */
c78b953e 8619 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
2050bc65
CS
8620 pf->flags |= I40E_FLAG_MFP_ENABLED;
8621 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
f4492db1
GR
8622 if (i40e_get_npar_bw_setting(pf))
8623 dev_warn(&pf->pdev->dev,
8624 "Could not get NPAR bw settings\n");
8625 else
8626 dev_info(&pf->pdev->dev,
8627 "Min BW = %8.8x, Max BW = %8.8x\n",
8628 pf->npar_min_bw, pf->npar_max_bw);
2050bc65
CS
8629 }
8630
cbf61325
ASJ
8631 /* FW/NVM is not yet fixed in this regard */
8632 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
8633 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
8634 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
8635 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
6eae9c6a
SN
8636 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
8637 pf->hw.num_partitions > 1)
cbf61325 8638 dev_info(&pf->pdev->dev,
0b67584f 8639 "Flow Director Sideband mode Disabled in MFP mode\n");
6eae9c6a
SN
8640 else
8641 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
cbf61325
ASJ
8642 pf->fdir_pf_filter_count =
8643 pf->hw.func_caps.fd_filters_guaranteed;
8644 pf->hw.fdir_shared_filter_count =
8645 pf->hw.func_caps.fd_filters_best_effort;
41c445ff
JB
8646 }
8647
f1bbad33 8648 if (i40e_is_mac_710(&pf->hw) &&
8eed76fa 8649 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
f1bbad33 8650 (pf->hw.aq.fw_maj_ver < 4))) {
8eed76fa 8651 pf->flags |= I40E_FLAG_RESTART_AUTONEG;
f1bbad33
NP
8652 /* No DCB support for FW < v4.33 */
8653 pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
8654 }
8655
8656 /* Disable FW LLDP if FW < v4.3 */
8657 if (i40e_is_mac_710(&pf->hw) &&
8658 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
8659 (pf->hw.aq.fw_maj_ver < 4)))
8660 pf->flags |= I40E_FLAG_STOP_FW_LLDP;
8661
8662 /* Use the FW Set LLDP MIB API if FW > v4.40 */
8663 if (i40e_is_mac_710(&pf->hw) &&
8664 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
8665 (pf->hw.aq.fw_maj_ver >= 5)))
8666 pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
8eed76fa 8667
41c445ff 8668 if (pf->hw.func_caps.vmdq) {
41c445ff 8669 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
e25d00b8 8670 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
e9e53662 8671 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
41c445ff
JB
8672 }
8673
e3219ce6
ASJ
8674 if (pf->hw.func_caps.iwarp) {
8675 pf->flags |= I40E_FLAG_IWARP_ENABLED;
8676 /* IWARP needs one extra vector for CQP just like MISC.*/
8677 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
8678 }
8679
38e00438 8680#ifdef I40E_FCOE
21364bcf 8681 i40e_init_pf_fcoe(pf);
38e00438
VD
8682
8683#endif /* I40E_FCOE */
41c445ff 8684#ifdef CONFIG_PCI_IOV
ba252f13 8685 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
41c445ff
JB
8686 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
8687 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
8688 pf->num_req_vfs = min_t(int,
8689 pf->hw.func_caps.num_vfs,
8690 I40E_MAX_VF_COUNT);
8691 }
8692#endif /* CONFIG_PCI_IOV */
d502ce01
ASJ
8693 if (pf->hw.mac.type == I40E_MAC_X722) {
8694 pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE |
8695 I40E_FLAG_128_QP_RSS_CAPABLE |
8696 I40E_FLAG_HW_ATR_EVICT_CAPABLE |
8697 I40E_FLAG_OUTER_UDP_CSUM_CAPABLE |
8698 I40E_FLAG_WB_ON_ITR_CAPABLE |
6a899024 8699 I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE |
8e2cc0e6 8700 I40E_FLAG_NO_PCI_LINK_CHECK |
f1bbad33 8701 I40E_FLAG_USE_SET_LLDP_MIB |
6a899024 8702 I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
a340c789
AS
8703 } else if ((pf->hw.aq.api_maj_ver > 1) ||
8704 ((pf->hw.aq.api_maj_ver == 1) &&
8705 (pf->hw.aq.api_min_ver > 4))) {
8706 /* Supported in FW API version higher than 1.4 */
8707 pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
72b74869
ASJ
8708 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
8709 } else {
8710 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
d502ce01 8711 }
a340c789 8712
41c445ff
JB
8713 pf->eeprom_version = 0xDEAD;
8714 pf->lan_veb = I40E_NO_VEB;
8715 pf->lan_vsi = I40E_NO_VSI;
8716
d1a8d275
ASJ
8717 /* By default FW has this off for performance reasons */
8718 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
8719
41c445ff
JB
8720 /* set up queue assignment tracking */
8721 size = sizeof(struct i40e_lump_tracking)
8722 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
8723 pf->qp_pile = kzalloc(size, GFP_KERNEL);
8724 if (!pf->qp_pile) {
8725 err = -ENOMEM;
8726 goto sw_init_done;
8727 }
8728 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
8729 pf->qp_pile->search_hint = 0;
8730
327fe04b
ASJ
8731 pf->tx_timeout_recovery_level = 1;
8732
41c445ff
JB
8733 mutex_init(&pf->switch_mutex);
8734
c668a12c
GR
8735 /* If NPAR is enabled nudge the Tx scheduler */
8736 if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
8737 i40e_set_npar_bw_setting(pf);
8738
41c445ff
JB
8739sw_init_done:
8740 return err;
8741}
8742
7c3c288b
ASJ
8743/**
8744 * i40e_set_ntuple - set the ntuple feature flag and take action
8745 * @pf: board private structure to initialize
8746 * @features: the feature set that the stack is suggesting
8747 *
8748 * returns a bool to indicate if reset needs to happen
8749 **/
8750bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
8751{
8752 bool need_reset = false;
8753
8754 /* Check if Flow Director n-tuple support was enabled or disabled. If
8755 * the state changed, we need to reset.
8756 */
8757 if (features & NETIF_F_NTUPLE) {
8758 /* Enable filters and mark for reset */
8759 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
8760 need_reset = true;
a70e407f
TD
8761 /* enable FD_SB only if there is MSI-X vector */
8762 if (pf->num_fdsb_msix > 0)
8763 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
7c3c288b
ASJ
8764 } else {
8765 /* turn off filters, mark for reset and clear SW filter list */
8766 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8767 need_reset = true;
8768 i40e_fdir_filter_exit(pf);
8769 }
8770 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8a4f34fb 8771 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
1e1be8f6
ASJ
8772 /* reset fd counters */
8773 pf->fd_add_err = pf->fd_atr_cnt = pf->fd_tcp_rule = 0;
8774 pf->fdir_pf_active_filters = 0;
8a4f34fb
ASJ
8775 /* if ATR was auto disabled it can be re-enabled. */
8776 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
234dc4e6 8777 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
8a4f34fb 8778 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
234dc4e6
JK
8779 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8780 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
8781 }
7c3c288b
ASJ
8782 }
8783 return need_reset;
8784}
8785
d8ec9864
AB
8786/**
8787 * i40e_clear_rss_lut - clear the rx hash lookup table
8788 * @vsi: the VSI being configured
8789 **/
8790static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
8791{
8792 struct i40e_pf *pf = vsi->back;
8793 struct i40e_hw *hw = &pf->hw;
8794 u16 vf_id = vsi->vf_id;
8795 u8 i;
8796
8797 if (vsi->type == I40E_VSI_MAIN) {
8798 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8799 wr32(hw, I40E_PFQF_HLUT(i), 0);
8800 } else if (vsi->type == I40E_VSI_SRIOV) {
8801 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8802 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
8803 } else {
8804 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8805 }
8806}
8807
41c445ff
JB
8808/**
8809 * i40e_set_features - set the netdev feature flags
8810 * @netdev: ptr to the netdev being adjusted
8811 * @features: the feature set that the stack is suggesting
8812 **/
8813static int i40e_set_features(struct net_device *netdev,
8814 netdev_features_t features)
8815{
8816 struct i40e_netdev_priv *np = netdev_priv(netdev);
8817 struct i40e_vsi *vsi = np->vsi;
7c3c288b
ASJ
8818 struct i40e_pf *pf = vsi->back;
8819 bool need_reset;
41c445ff 8820
d8ec9864
AB
8821 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
8822 i40e_pf_config_rss(pf);
8823 else if (!(features & NETIF_F_RXHASH) &&
8824 netdev->features & NETIF_F_RXHASH)
8825 i40e_clear_rss_lut(vsi);
8826
41c445ff
JB
8827 if (features & NETIF_F_HW_VLAN_CTAG_RX)
8828 i40e_vlan_stripping_enable(vsi);
8829 else
8830 i40e_vlan_stripping_disable(vsi);
8831
7c3c288b
ASJ
8832 need_reset = i40e_set_ntuple(pf, features);
8833
8834 if (need_reset)
41a1d04b 8835 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
7c3c288b 8836
41c445ff
JB
8837 return 0;
8838}
8839
a1c9a9d9 8840/**
6a899024 8841 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
a1c9a9d9
JK
8842 * @pf: board private structure
8843 * @port: The UDP port to look up
8844 *
8845 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
8846 **/
6a899024 8847static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, __be16 port)
a1c9a9d9
JK
8848{
8849 u8 i;
8850
8851 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
6a899024 8852 if (pf->udp_ports[i].index == port)
a1c9a9d9
JK
8853 return i;
8854 }
8855
8856 return i;
8857}
8858
8859/**
06a5f7f1 8860 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
a1c9a9d9 8861 * @netdev: This physical port's netdev
06a5f7f1 8862 * @ti: Tunnel endpoint information
a1c9a9d9 8863 **/
06a5f7f1
AD
8864static void i40e_udp_tunnel_add(struct net_device *netdev,
8865 struct udp_tunnel_info *ti)
a1c9a9d9
JK
8866{
8867 struct i40e_netdev_priv *np = netdev_priv(netdev);
8868 struct i40e_vsi *vsi = np->vsi;
8869 struct i40e_pf *pf = vsi->back;
06a5f7f1 8870 __be16 port = ti->port;
a1c9a9d9
JK
8871 u8 next_idx;
8872 u8 idx;
8873
6a899024 8874 idx = i40e_get_udp_port_idx(pf, port);
a1c9a9d9
JK
8875
8876 /* Check if port already exists */
8877 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
06a5f7f1 8878 netdev_info(netdev, "port %d already offloaded\n",
c22c06c8 8879 ntohs(port));
a1c9a9d9
JK
8880 return;
8881 }
8882
8883 /* Now check if there is space to add the new port */
6a899024 8884 next_idx = i40e_get_udp_port_idx(pf, 0);
a1c9a9d9
JK
8885
8886 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
06a5f7f1 8887 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
6a899024
SA
8888 ntohs(port));
8889 return;
8890 }
8891
06a5f7f1
AD
8892 switch (ti->type) {
8893 case UDP_TUNNEL_TYPE_VXLAN:
8894 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
8895 break;
8896 case UDP_TUNNEL_TYPE_GENEVE:
8897 if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
8898 return;
8899 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
8900 break;
8901 default:
6a899024
SA
8902 return;
8903 }
8904
8905 /* New port: add it and mark its index in the bitmap */
8906 pf->udp_ports[next_idx].index = port;
6a899024
SA
8907 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
8908 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
a1c9a9d9
JK
8909}
8910
6a899024 8911/**
06a5f7f1 8912 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
6a899024 8913 * @netdev: This physical port's netdev
06a5f7f1 8914 * @ti: Tunnel endpoint information
6a899024 8915 **/
06a5f7f1
AD
8916static void i40e_udp_tunnel_del(struct net_device *netdev,
8917 struct udp_tunnel_info *ti)
6a899024 8918{
6a899024
SA
8919 struct i40e_netdev_priv *np = netdev_priv(netdev);
8920 struct i40e_vsi *vsi = np->vsi;
8921 struct i40e_pf *pf = vsi->back;
06a5f7f1 8922 __be16 port = ti->port;
6a899024
SA
8923 u8 idx;
8924
6a899024
SA
8925 idx = i40e_get_udp_port_idx(pf, port);
8926
8927 /* Check if port already exists */
06a5f7f1
AD
8928 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
8929 goto not_found;
6a899024 8930
06a5f7f1
AD
8931 switch (ti->type) {
8932 case UDP_TUNNEL_TYPE_VXLAN:
8933 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
8934 goto not_found;
8935 break;
8936 case UDP_TUNNEL_TYPE_GENEVE:
8937 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
8938 goto not_found;
8939 break;
8940 default:
8941 goto not_found;
6a899024 8942 }
06a5f7f1
AD
8943
8944 /* if port exists, set it to 0 (mark for deletion)
8945 * and make it pending
8946 */
8947 pf->udp_ports[idx].index = 0;
8948 pf->pending_udp_bitmap |= BIT_ULL(idx);
8949 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
8950
8951 return;
8952not_found:
8953 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
8954 ntohs(port));
6a899024
SA
8955}
8956
1f224ad2 8957static int i40e_get_phys_port_id(struct net_device *netdev,
02637fce 8958 struct netdev_phys_item_id *ppid)
1f224ad2
NP
8959{
8960 struct i40e_netdev_priv *np = netdev_priv(netdev);
8961 struct i40e_pf *pf = np->vsi->back;
8962 struct i40e_hw *hw = &pf->hw;
8963
8964 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
8965 return -EOPNOTSUPP;
8966
8967 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
8968 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
8969
8970 return 0;
8971}
8972
2f90ade6
JB
8973/**
8974 * i40e_ndo_fdb_add - add an entry to the hardware database
8975 * @ndm: the input from the stack
8976 * @tb: pointer to array of nladdr (unused)
8977 * @dev: the net device pointer
8978 * @addr: the MAC address entry being added
8979 * @flags: instructions from stack about fdb operation
8980 */
4ba0dea5
GR
8981static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
8982 struct net_device *dev,
f6f6424b 8983 const unsigned char *addr, u16 vid,
4ba0dea5 8984 u16 flags)
4ba0dea5
GR
8985{
8986 struct i40e_netdev_priv *np = netdev_priv(dev);
8987 struct i40e_pf *pf = np->vsi->back;
8988 int err = 0;
8989
8990 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
8991 return -EOPNOTSUPP;
8992
65891fea
OG
8993 if (vid) {
8994 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
8995 return -EINVAL;
8996 }
8997
4ba0dea5
GR
8998 /* Hardware does not support aging addresses so if a
8999 * ndm_state is given only allow permanent addresses
9000 */
9001 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
9002 netdev_info(dev, "FDB only supports static addresses\n");
9003 return -EINVAL;
9004 }
9005
9006 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
9007 err = dev_uc_add_excl(dev, addr);
9008 else if (is_multicast_ether_addr(addr))
9009 err = dev_mc_add_excl(dev, addr);
9010 else
9011 err = -EINVAL;
9012
9013 /* Only return duplicate errors if NLM_F_EXCL is set */
9014 if (err == -EEXIST && !(flags & NLM_F_EXCL))
9015 err = 0;
9016
9017 return err;
9018}
9019
51616018
NP
9020/**
9021 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
9022 * @dev: the netdev being configured
9023 * @nlh: RTNL message
9024 *
9025 * Inserts a new hardware bridge if not already created and
9026 * enables the bridging mode requested (VEB or VEPA). If the
9027 * hardware bridge has already been inserted and the request
9028 * is to change the mode then that requires a PF reset to
9029 * allow rebuild of the components with required hardware
9030 * bridge mode enabled.
9031 **/
9032static int i40e_ndo_bridge_setlink(struct net_device *dev,
9df70b66
CW
9033 struct nlmsghdr *nlh,
9034 u16 flags)
51616018
NP
9035{
9036 struct i40e_netdev_priv *np = netdev_priv(dev);
9037 struct i40e_vsi *vsi = np->vsi;
9038 struct i40e_pf *pf = vsi->back;
9039 struct i40e_veb *veb = NULL;
9040 struct nlattr *attr, *br_spec;
9041 int i, rem;
9042
9043 /* Only for PF VSI for now */
9044 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9045 return -EOPNOTSUPP;
9046
9047 /* Find the HW bridge for PF VSI */
9048 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9049 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9050 veb = pf->veb[i];
9051 }
9052
9053 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
9054
9055 nla_for_each_nested(attr, br_spec, rem) {
9056 __u16 mode;
9057
9058 if (nla_type(attr) != IFLA_BRIDGE_MODE)
9059 continue;
9060
9061 mode = nla_get_u16(attr);
9062 if ((mode != BRIDGE_MODE_VEPA) &&
9063 (mode != BRIDGE_MODE_VEB))
9064 return -EINVAL;
9065
9066 /* Insert a new HW bridge */
9067 if (!veb) {
9068 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9069 vsi->tc_config.enabled_tc);
9070 if (veb) {
9071 veb->bridge_mode = mode;
9072 i40e_config_bridge_mode(veb);
9073 } else {
9074 /* No Bridge HW offload available */
9075 return -ENOENT;
9076 }
9077 break;
9078 } else if (mode != veb->bridge_mode) {
9079 /* Existing HW bridge but different mode needs reset */
9080 veb->bridge_mode = mode;
fc60861e
ASJ
9081 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
9082 if (mode == BRIDGE_MODE_VEB)
9083 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
9084 else
9085 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9086 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
51616018
NP
9087 break;
9088 }
9089 }
9090
9091 return 0;
9092}
9093
9094/**
9095 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
9096 * @skb: skb buff
9097 * @pid: process id
9098 * @seq: RTNL message seq #
9099 * @dev: the netdev being configured
9100 * @filter_mask: unused
d4b2f9fe 9101 * @nlflags: netlink flags passed in
51616018
NP
9102 *
9103 * Return the mode in which the hardware bridge is operating in
9104 * i.e VEB or VEPA.
9105 **/
51616018
NP
9106static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
9107 struct net_device *dev,
9f4ffc44
CW
9108 u32 __always_unused filter_mask,
9109 int nlflags)
51616018
NP
9110{
9111 struct i40e_netdev_priv *np = netdev_priv(dev);
9112 struct i40e_vsi *vsi = np->vsi;
9113 struct i40e_pf *pf = vsi->back;
9114 struct i40e_veb *veb = NULL;
9115 int i;
9116
9117 /* Only for PF VSI for now */
9118 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9119 return -EOPNOTSUPP;
9120
9121 /* Find the HW bridge for the PF VSI */
9122 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9123 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9124 veb = pf->veb[i];
9125 }
9126
9127 if (!veb)
9128 return 0;
9129
46c264da 9130 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
599b076d 9131 0, 0, nlflags, filter_mask, NULL);
51616018 9132}
51616018 9133
f44a75e2
JS
9134/**
9135 * i40e_features_check - Validate encapsulated packet conforms to limits
9136 * @skb: skb buff
2bc11c63 9137 * @dev: This physical port's netdev
f44a75e2
JS
9138 * @features: Offload features that the stack believes apply
9139 **/
9140static netdev_features_t i40e_features_check(struct sk_buff *skb,
9141 struct net_device *dev,
9142 netdev_features_t features)
9143{
f114dca2
AD
9144 size_t len;
9145
9146 /* No point in doing any of this if neither checksum nor GSO are
9147 * being requested for this frame. We can rule out both by just
9148 * checking for CHECKSUM_PARTIAL
9149 */
9150 if (skb->ip_summed != CHECKSUM_PARTIAL)
9151 return features;
9152
9153 /* We cannot support GSO if the MSS is going to be less than
9154 * 64 bytes. If it is then we need to drop support for GSO.
9155 */
9156 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
9157 features &= ~NETIF_F_GSO_MASK;
9158
9159 /* MACLEN can support at most 63 words */
9160 len = skb_network_header(skb) - skb->data;
9161 if (len & ~(63 * 2))
9162 goto out_err;
9163
9164 /* IPLEN and EIPLEN can support at most 127 dwords */
9165 len = skb_transport_header(skb) - skb_network_header(skb);
9166 if (len & ~(127 * 4))
9167 goto out_err;
9168
9169 if (skb->encapsulation) {
9170 /* L4TUNLEN can support 127 words */
9171 len = skb_inner_network_header(skb) - skb_transport_header(skb);
9172 if (len & ~(127 * 2))
9173 goto out_err;
9174
9175 /* IPLEN can support at most 127 dwords */
9176 len = skb_inner_transport_header(skb) -
9177 skb_inner_network_header(skb);
9178 if (len & ~(127 * 4))
9179 goto out_err;
9180 }
9181
9182 /* No need to validate L4LEN as TCP is the only protocol with a
9183 * a flexible value and we support all possible values supported
9184 * by TCP, which is at most 15 dwords
9185 */
f44a75e2
JS
9186
9187 return features;
f114dca2
AD
9188out_err:
9189 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
f44a75e2
JS
9190}
9191
37a2973a 9192static const struct net_device_ops i40e_netdev_ops = {
41c445ff
JB
9193 .ndo_open = i40e_open,
9194 .ndo_stop = i40e_close,
9195 .ndo_start_xmit = i40e_lan_xmit_frame,
9196 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
9197 .ndo_set_rx_mode = i40e_set_rx_mode,
9198 .ndo_validate_addr = eth_validate_addr,
9199 .ndo_set_mac_address = i40e_set_mac,
9200 .ndo_change_mtu = i40e_change_mtu,
beb0dff1 9201 .ndo_do_ioctl = i40e_ioctl,
41c445ff
JB
9202 .ndo_tx_timeout = i40e_tx_timeout,
9203 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
9204 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
9205#ifdef CONFIG_NET_POLL_CONTROLLER
9206 .ndo_poll_controller = i40e_netpoll,
9207#endif
e4c6734e 9208 .ndo_setup_tc = __i40e_setup_tc,
38e00438
VD
9209#ifdef I40E_FCOE
9210 .ndo_fcoe_enable = i40e_fcoe_enable,
9211 .ndo_fcoe_disable = i40e_fcoe_disable,
9212#endif
41c445ff
JB
9213 .ndo_set_features = i40e_set_features,
9214 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
9215 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
ed616689 9216 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
41c445ff 9217 .ndo_get_vf_config = i40e_ndo_get_vf_config,
588aefa0 9218 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
e6d9004d 9219 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
c3bbbd20 9220 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
06a5f7f1
AD
9221 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
9222 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
1f224ad2 9223 .ndo_get_phys_port_id = i40e_get_phys_port_id,
4ba0dea5 9224 .ndo_fdb_add = i40e_ndo_fdb_add,
f44a75e2 9225 .ndo_features_check = i40e_features_check,
51616018
NP
9226 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
9227 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
41c445ff
JB
9228};
9229
9230/**
9231 * i40e_config_netdev - Setup the netdev flags
9232 * @vsi: the VSI being configured
9233 *
9234 * Returns 0 on success, negative value on failure
9235 **/
9236static int i40e_config_netdev(struct i40e_vsi *vsi)
9237{
9238 struct i40e_pf *pf = vsi->back;
9239 struct i40e_hw *hw = &pf->hw;
9240 struct i40e_netdev_priv *np;
9241 struct net_device *netdev;
435c084a 9242 u8 broadcast[ETH_ALEN];
41c445ff
JB
9243 u8 mac_addr[ETH_ALEN];
9244 int etherdev_size;
9245
9246 etherdev_size = sizeof(struct i40e_netdev_priv);
f8ff1464 9247 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
41c445ff
JB
9248 if (!netdev)
9249 return -ENOMEM;
9250
9251 vsi->netdev = netdev;
9252 np = netdev_priv(netdev);
9253 np->vsi = vsi;
9254
b0fe3306
AD
9255 netdev->hw_enc_features |= NETIF_F_SG |
9256 NETIF_F_IP_CSUM |
9257 NETIF_F_IPV6_CSUM |
9258 NETIF_F_HIGHDMA |
9259 NETIF_F_SOFT_FEATURES |
9260 NETIF_F_TSO |
9261 NETIF_F_TSO_ECN |
9262 NETIF_F_TSO6 |
9263 NETIF_F_GSO_GRE |
1c7b4a23 9264 NETIF_F_GSO_GRE_CSUM |
7e13318d 9265 NETIF_F_GSO_IPXIP4 |
bf2d1df3 9266 NETIF_F_GSO_IPXIP6 |
b0fe3306
AD
9267 NETIF_F_GSO_UDP_TUNNEL |
9268 NETIF_F_GSO_UDP_TUNNEL_CSUM |
1c7b4a23 9269 NETIF_F_GSO_PARTIAL |
b0fe3306
AD
9270 NETIF_F_SCTP_CRC |
9271 NETIF_F_RXHASH |
9272 NETIF_F_RXCSUM |
5afdaaa0 9273 0;
41c445ff 9274
b0fe3306 9275 if (!(pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE))
1c7b4a23
AD
9276 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
9277
9278 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
b0fe3306
AD
9279
9280 /* record features VLANs can make use of */
1c7b4a23
AD
9281 netdev->vlan_features |= netdev->hw_enc_features |
9282 NETIF_F_TSO_MANGLEID;
41c445ff 9283
2e86a0b6 9284 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
b0fe3306
AD
9285 netdev->hw_features |= NETIF_F_NTUPLE;
9286
9287 netdev->hw_features |= netdev->hw_enc_features |
9288 NETIF_F_HW_VLAN_CTAG_TX |
9289 NETIF_F_HW_VLAN_CTAG_RX;
2e86a0b6 9290
b0fe3306 9291 netdev->features |= netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
1c7b4a23 9292 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
41c445ff
JB
9293
9294 if (vsi->type == I40E_VSI_MAIN) {
9295 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
9a173901 9296 ether_addr_copy(mac_addr, hw->mac.perm_addr);
1596b5dd
JK
9297 /* The following steps are necessary to prevent reception
9298 * of tagged packets - some older NVM configurations load a
9299 * default a MAC-VLAN filter that accepts any tagged packet
9300 * which must be replaced by a normal filter.
9301 */
9302 i40e_rm_default_mac_filter(vsi, mac_addr);
278e7d0b 9303 spin_lock_bh(&vsi->mac_filter_hash_lock);
1bc87e80 9304 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY);
278e7d0b 9305 spin_unlock_bh(&vsi->mac_filter_hash_lock);
41c445ff
JB
9306 } else {
9307 /* relate the VSI_VMDQ name to the VSI_MAIN name */
9308 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
9309 pf->vsi[pf->lan_vsi]->netdev->name);
9310 random_ether_addr(mac_addr);
21659035 9311
278e7d0b 9312 spin_lock_bh(&vsi->mac_filter_hash_lock);
1bc87e80 9313 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY);
278e7d0b 9314 spin_unlock_bh(&vsi->mac_filter_hash_lock);
41c445ff 9315 }
21659035 9316
435c084a
JK
9317 /* Add the broadcast filter so that we initially will receive
9318 * broadcast packets. Note that when a new VLAN is first added the
9319 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
9320 * specific filters as part of transitioning into "vlan" operation.
9321 * When more VLANs are added, the driver will copy each existing MAC
9322 * filter and add it for the new VLAN.
9323 *
9324 * Broadcast filters are handled specially by
9325 * i40e_sync_filters_subtask, as the driver must to set the broadcast
9326 * promiscuous bit instead of adding this directly as a MAC/VLAN
9327 * filter. The subtask will update the correct broadcast promiscuous
9328 * bits as VLANs become active or inactive.
9329 */
9330 eth_broadcast_addr(broadcast);
9331 spin_lock_bh(&vsi->mac_filter_hash_lock);
9332 i40e_add_filter(vsi, broadcast, I40E_VLAN_ANY);
9333 spin_unlock_bh(&vsi->mac_filter_hash_lock);
9334
9a173901
GR
9335 ether_addr_copy(netdev->dev_addr, mac_addr);
9336 ether_addr_copy(netdev->perm_addr, mac_addr);
b0fe3306 9337
41c445ff
JB
9338 netdev->priv_flags |= IFF_UNICAST_FLT;
9339 netdev->priv_flags |= IFF_SUPP_NOFCS;
9340 /* Setup netdev TC information */
9341 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
9342
9343 netdev->netdev_ops = &i40e_netdev_ops;
9344 netdev->watchdog_timeo = 5 * HZ;
9345 i40e_set_ethtool_ops(netdev);
38e00438
VD
9346#ifdef I40E_FCOE
9347 i40e_fcoe_config_netdev(netdev, vsi);
9348#endif
41c445ff 9349
91c527a5
JW
9350 /* MTU range: 68 - 9706 */
9351 netdev->min_mtu = ETH_MIN_MTU;
9352 netdev->max_mtu = I40E_MAX_RXBUFFER -
9353 (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
9354
41c445ff
JB
9355 return 0;
9356}
9357
9358/**
9359 * i40e_vsi_delete - Delete a VSI from the switch
9360 * @vsi: the VSI being removed
9361 *
9362 * Returns 0 on success, negative value on failure
9363 **/
9364static void i40e_vsi_delete(struct i40e_vsi *vsi)
9365{
9366 /* remove default VSI is not allowed */
9367 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
9368 return;
9369
41c445ff 9370 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
41c445ff
JB
9371}
9372
51616018
NP
9373/**
9374 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
9375 * @vsi: the VSI being queried
9376 *
9377 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
9378 **/
9379int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
9380{
9381 struct i40e_veb *veb;
9382 struct i40e_pf *pf = vsi->back;
9383
9384 /* Uplink is not a bridge so default to VEB */
9385 if (vsi->veb_idx == I40E_NO_VEB)
9386 return 1;
9387
9388 veb = pf->veb[vsi->veb_idx];
09603eaa
AA
9389 if (!veb) {
9390 dev_info(&pf->pdev->dev,
9391 "There is no veb associated with the bridge\n");
9392 return -ENOENT;
9393 }
9394
51616018 9395 /* Uplink is a bridge in VEPA mode */
09603eaa 9396 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
51616018 9397 return 0;
09603eaa
AA
9398 } else {
9399 /* Uplink is a bridge in VEB mode */
9400 return 1;
9401 }
51616018 9402
09603eaa
AA
9403 /* VEPA is now default bridge, so return 0 */
9404 return 0;
51616018
NP
9405}
9406
41c445ff
JB
9407/**
9408 * i40e_add_vsi - Add a VSI to the switch
9409 * @vsi: the VSI being configured
9410 *
9411 * This initializes a VSI context depending on the VSI type to be added and
9412 * passes it down to the add_vsi aq command.
9413 **/
9414static int i40e_add_vsi(struct i40e_vsi *vsi)
9415{
9416 int ret = -ENODEV;
41c445ff
JB
9417 struct i40e_pf *pf = vsi->back;
9418 struct i40e_hw *hw = &pf->hw;
9419 struct i40e_vsi_context ctxt;
278e7d0b
JK
9420 struct i40e_mac_filter *f;
9421 struct hlist_node *h;
9422 int bkt;
21659035 9423
41c445ff
JB
9424 u8 enabled_tc = 0x1; /* TC0 enabled */
9425 int f_count = 0;
9426
9427 memset(&ctxt, 0, sizeof(ctxt));
9428 switch (vsi->type) {
9429 case I40E_VSI_MAIN:
9430 /* The PF's main VSI is already setup as part of the
9431 * device initialization, so we'll not bother with
9432 * the add_vsi call, but we will retrieve the current
9433 * VSI context.
9434 */
9435 ctxt.seid = pf->main_vsi_seid;
9436 ctxt.pf_num = pf->hw.pf_id;
9437 ctxt.vf_num = 0;
9438 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9439 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9440 if (ret) {
9441 dev_info(&pf->pdev->dev,
f1c7e72e
SN
9442 "couldn't get PF vsi config, err %s aq_err %s\n",
9443 i40e_stat_str(&pf->hw, ret),
9444 i40e_aq_str(&pf->hw,
9445 pf->hw.aq.asq_last_status));
41c445ff
JB
9446 return -ENOENT;
9447 }
1a2f6248 9448 vsi->info = ctxt.info;
41c445ff
JB
9449 vsi->info.valid_sections = 0;
9450
9451 vsi->seid = ctxt.seid;
9452 vsi->id = ctxt.vsi_number;
9453
9454 enabled_tc = i40e_pf_get_tc_map(pf);
9455
9456 /* MFP mode setup queue map and update VSI */
63d7e5a4
NP
9457 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
9458 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
41c445ff
JB
9459 memset(&ctxt, 0, sizeof(ctxt));
9460 ctxt.seid = pf->main_vsi_seid;
9461 ctxt.pf_num = pf->hw.pf_id;
9462 ctxt.vf_num = 0;
9463 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
9464 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9465 if (ret) {
9466 dev_info(&pf->pdev->dev,
f1c7e72e
SN
9467 "update vsi failed, err %s aq_err %s\n",
9468 i40e_stat_str(&pf->hw, ret),
9469 i40e_aq_str(&pf->hw,
9470 pf->hw.aq.asq_last_status));
41c445ff
JB
9471 ret = -ENOENT;
9472 goto err;
9473 }
9474 /* update the local VSI info queue map */
9475 i40e_vsi_update_queue_map(vsi, &ctxt);
9476 vsi->info.valid_sections = 0;
9477 } else {
9478 /* Default/Main VSI is only enabled for TC0
9479 * reconfigure it to enable all TCs that are
9480 * available on the port in SFP mode.
63d7e5a4
NP
9481 * For MFP case the iSCSI PF would use this
9482 * flow to enable LAN+iSCSI TC.
41c445ff
JB
9483 */
9484 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9485 if (ret) {
9486 dev_info(&pf->pdev->dev,
f1c7e72e
SN
9487 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9488 enabled_tc,
9489 i40e_stat_str(&pf->hw, ret),
9490 i40e_aq_str(&pf->hw,
9491 pf->hw.aq.asq_last_status));
41c445ff
JB
9492 ret = -ENOENT;
9493 }
9494 }
9495 break;
9496
9497 case I40E_VSI_FDIR:
cbf61325
ASJ
9498 ctxt.pf_num = hw->pf_id;
9499 ctxt.vf_num = 0;
9500 ctxt.uplink_seid = vsi->uplink_seid;
2b18e591 9501 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
cbf61325 9502 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
fc60861e
ASJ
9503 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
9504 (i40e_is_vsi_uplink_mode_veb(vsi))) {
51616018 9505 ctxt.info.valid_sections |=
fc60861e 9506 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
51616018 9507 ctxt.info.switch_id =
fc60861e 9508 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
51616018 9509 }
41c445ff 9510 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
41c445ff
JB
9511 break;
9512
9513 case I40E_VSI_VMDQ2:
9514 ctxt.pf_num = hw->pf_id;
9515 ctxt.vf_num = 0;
9516 ctxt.uplink_seid = vsi->uplink_seid;
2b18e591 9517 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
41c445ff
JB
9518 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
9519
41c445ff
JB
9520 /* This VSI is connected to VEB so the switch_id
9521 * should be set to zero by default.
9522 */
51616018
NP
9523 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9524 ctxt.info.valid_sections |=
9525 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9526 ctxt.info.switch_id =
9527 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9528 }
41c445ff
JB
9529
9530 /* Setup the VSI tx/rx queue map for TC0 only for now */
9531 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9532 break;
9533
9534 case I40E_VSI_SRIOV:
9535 ctxt.pf_num = hw->pf_id;
9536 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
9537 ctxt.uplink_seid = vsi->uplink_seid;
2b18e591 9538 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
41c445ff
JB
9539 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
9540
41c445ff
JB
9541 /* This VSI is connected to VEB so the switch_id
9542 * should be set to zero by default.
9543 */
51616018
NP
9544 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9545 ctxt.info.valid_sections |=
9546 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9547 ctxt.info.switch_id =
9548 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9549 }
41c445ff 9550
e3219ce6
ASJ
9551 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
9552 ctxt.info.valid_sections |=
9553 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
9554 ctxt.info.queueing_opt_flags |=
4b28cdba
AS
9555 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
9556 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
e3219ce6
ASJ
9557 }
9558
41c445ff
JB
9559 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
9560 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
c674d125
MW
9561 if (pf->vf[vsi->vf_id].spoofchk) {
9562 ctxt.info.valid_sections |=
9563 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
9564 ctxt.info.sec_flags |=
9565 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
9566 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
9567 }
41c445ff
JB
9568 /* Setup the VSI tx/rx queue map for TC0 only for now */
9569 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9570 break;
9571
38e00438
VD
9572#ifdef I40E_FCOE
9573 case I40E_VSI_FCOE:
9574 ret = i40e_fcoe_vsi_init(vsi, &ctxt);
9575 if (ret) {
9576 dev_info(&pf->pdev->dev, "failed to initialize FCoE VSI\n");
9577 return ret;
9578 }
9579 break;
9580
9581#endif /* I40E_FCOE */
e3219ce6
ASJ
9582 case I40E_VSI_IWARP:
9583 /* send down message to iWARP */
9584 break;
9585
41c445ff
JB
9586 default:
9587 return -ENODEV;
9588 }
9589
9590 if (vsi->type != I40E_VSI_MAIN) {
9591 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
9592 if (ret) {
9593 dev_info(&vsi->back->pdev->dev,
f1c7e72e
SN
9594 "add vsi failed, err %s aq_err %s\n",
9595 i40e_stat_str(&pf->hw, ret),
9596 i40e_aq_str(&pf->hw,
9597 pf->hw.aq.asq_last_status));
41c445ff
JB
9598 ret = -ENOENT;
9599 goto err;
9600 }
1a2f6248 9601 vsi->info = ctxt.info;
41c445ff
JB
9602 vsi->info.valid_sections = 0;
9603 vsi->seid = ctxt.seid;
9604 vsi->id = ctxt.vsi_number;
9605 }
9606
c3c7ea27
MW
9607 vsi->active_filters = 0;
9608 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
278e7d0b 9609 spin_lock_bh(&vsi->mac_filter_hash_lock);
41c445ff 9610 /* If macvlan filters already exist, force them to get loaded */
278e7d0b 9611 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
c3c7ea27 9612 f->state = I40E_FILTER_NEW;
41c445ff 9613 f_count++;
21659035 9614 }
278e7d0b 9615 spin_unlock_bh(&vsi->mac_filter_hash_lock);
30650cc5 9616
41c445ff
JB
9617 if (f_count) {
9618 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
9619 pf->flags |= I40E_FLAG_FILTER_SYNC;
9620 }
9621
9622 /* Update VSI BW information */
9623 ret = i40e_vsi_get_bw_info(vsi);
9624 if (ret) {
9625 dev_info(&pf->pdev->dev,
f1c7e72e
SN
9626 "couldn't get vsi bw info, err %s aq_err %s\n",
9627 i40e_stat_str(&pf->hw, ret),
9628 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
41c445ff
JB
9629 /* VSI is already added so not tearing that up */
9630 ret = 0;
9631 }
9632
9633err:
9634 return ret;
9635}
9636
9637/**
9638 * i40e_vsi_release - Delete a VSI and free its resources
9639 * @vsi: the VSI being removed
9640 *
9641 * Returns 0 on success or < 0 on error
9642 **/
9643int i40e_vsi_release(struct i40e_vsi *vsi)
9644{
278e7d0b
JK
9645 struct i40e_mac_filter *f;
9646 struct hlist_node *h;
41c445ff
JB
9647 struct i40e_veb *veb = NULL;
9648 struct i40e_pf *pf;
9649 u16 uplink_seid;
278e7d0b 9650 int i, n, bkt;
41c445ff
JB
9651
9652 pf = vsi->back;
9653
9654 /* release of a VEB-owner or last VSI is not allowed */
9655 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
9656 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
9657 vsi->seid, vsi->uplink_seid);
9658 return -ENODEV;
9659 }
9660 if (vsi == pf->vsi[pf->lan_vsi] &&
9661 !test_bit(__I40E_DOWN, &pf->state)) {
9662 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
9663 return -ENODEV;
9664 }
9665
9666 uplink_seid = vsi->uplink_seid;
9667 if (vsi->type != I40E_VSI_SRIOV) {
9668 if (vsi->netdev_registered) {
9669 vsi->netdev_registered = false;
9670 if (vsi->netdev) {
9671 /* results in a call to i40e_close() */
9672 unregister_netdev(vsi->netdev);
41c445ff
JB
9673 }
9674 } else {
90ef8d47 9675 i40e_vsi_close(vsi);
41c445ff
JB
9676 }
9677 i40e_vsi_disable_irq(vsi);
9678 }
9679
278e7d0b 9680 spin_lock_bh(&vsi->mac_filter_hash_lock);
6622f5cd
JK
9681
9682 /* clear the sync flag on all filters */
9683 if (vsi->netdev) {
9684 __dev_uc_unsync(vsi->netdev, NULL);
9685 __dev_mc_unsync(vsi->netdev, NULL);
9686 }
9687
9688 /* make sure any remaining filters are marked for deletion */
278e7d0b 9689 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
290d2557 9690 __i40e_del_filter(vsi, f);
6622f5cd 9691
278e7d0b 9692 spin_unlock_bh(&vsi->mac_filter_hash_lock);
21659035 9693
17652c63 9694 i40e_sync_vsi_filters(vsi);
41c445ff
JB
9695
9696 i40e_vsi_delete(vsi);
9697 i40e_vsi_free_q_vectors(vsi);
a4866597
SN
9698 if (vsi->netdev) {
9699 free_netdev(vsi->netdev);
9700 vsi->netdev = NULL;
9701 }
41c445ff
JB
9702 i40e_vsi_clear_rings(vsi);
9703 i40e_vsi_clear(vsi);
9704
9705 /* If this was the last thing on the VEB, except for the
9706 * controlling VSI, remove the VEB, which puts the controlling
9707 * VSI onto the next level down in the switch.
9708 *
9709 * Well, okay, there's one more exception here: don't remove
9710 * the orphan VEBs yet. We'll wait for an explicit remove request
9711 * from up the network stack.
9712 */
505682cd 9713 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
41c445ff
JB
9714 if (pf->vsi[i] &&
9715 pf->vsi[i]->uplink_seid == uplink_seid &&
9716 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9717 n++; /* count the VSIs */
9718 }
9719 }
9720 for (i = 0; i < I40E_MAX_VEB; i++) {
9721 if (!pf->veb[i])
9722 continue;
9723 if (pf->veb[i]->uplink_seid == uplink_seid)
9724 n++; /* count the VEBs */
9725 if (pf->veb[i]->seid == uplink_seid)
9726 veb = pf->veb[i];
9727 }
9728 if (n == 0 && veb && veb->uplink_seid != 0)
9729 i40e_veb_release(veb);
9730
9731 return 0;
9732}
9733
9734/**
9735 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
9736 * @vsi: ptr to the VSI
9737 *
9738 * This should only be called after i40e_vsi_mem_alloc() which allocates the
9739 * corresponding SW VSI structure and initializes num_queue_pairs for the
9740 * newly allocated VSI.
9741 *
9742 * Returns 0 on success or negative on failure
9743 **/
9744static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
9745{
9746 int ret = -ENOENT;
9747 struct i40e_pf *pf = vsi->back;
9748
493fb300 9749 if (vsi->q_vectors[0]) {
41c445ff
JB
9750 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
9751 vsi->seid);
9752 return -EEXIST;
9753 }
9754
9755 if (vsi->base_vector) {
f29eaa3d 9756 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
41c445ff
JB
9757 vsi->seid, vsi->base_vector);
9758 return -EEXIST;
9759 }
9760
90e04070 9761 ret = i40e_vsi_alloc_q_vectors(vsi);
41c445ff
JB
9762 if (ret) {
9763 dev_info(&pf->pdev->dev,
9764 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
9765 vsi->num_q_vectors, vsi->seid, ret);
9766 vsi->num_q_vectors = 0;
9767 goto vector_setup_out;
9768 }
9769
26cdc443
ASJ
9770 /* In Legacy mode, we do not have to get any other vector since we
9771 * piggyback on the misc/ICR0 for queue interrupts.
9772 */
9773 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
9774 return ret;
958a3e3b
SN
9775 if (vsi->num_q_vectors)
9776 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
9777 vsi->num_q_vectors, vsi->idx);
41c445ff
JB
9778 if (vsi->base_vector < 0) {
9779 dev_info(&pf->pdev->dev,
049a2be8
SN
9780 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
9781 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
41c445ff
JB
9782 i40e_vsi_free_q_vectors(vsi);
9783 ret = -ENOENT;
9784 goto vector_setup_out;
9785 }
9786
9787vector_setup_out:
9788 return ret;
9789}
9790
bc7d338f
ASJ
9791/**
9792 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
9793 * @vsi: pointer to the vsi.
9794 *
9795 * This re-allocates a vsi's queue resources.
9796 *
9797 * Returns pointer to the successfully allocated and configured VSI sw struct
9798 * on success, otherwise returns NULL on failure.
9799 **/
9800static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
9801{
f534039d 9802 struct i40e_pf *pf;
bc7d338f
ASJ
9803 u8 enabled_tc;
9804 int ret;
9805
f534039d
JU
9806 if (!vsi)
9807 return NULL;
9808
9809 pf = vsi->back;
9810
bc7d338f
ASJ
9811 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
9812 i40e_vsi_clear_rings(vsi);
9813
9814 i40e_vsi_free_arrays(vsi, false);
9815 i40e_set_num_rings_in_vsi(vsi);
9816 ret = i40e_vsi_alloc_arrays(vsi, false);
9817 if (ret)
9818 goto err_vsi;
9819
9820 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
9821 if (ret < 0) {
049a2be8 9822 dev_info(&pf->pdev->dev,
f1c7e72e 9823 "failed to get tracking for %d queues for VSI %d err %d\n",
049a2be8 9824 vsi->alloc_queue_pairs, vsi->seid, ret);
bc7d338f
ASJ
9825 goto err_vsi;
9826 }
9827 vsi->base_queue = ret;
9828
9829 /* Update the FW view of the VSI. Force a reset of TC and queue
9830 * layout configurations.
9831 */
9832 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
9833 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
9834 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
9835 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
1596b5dd
JK
9836 if (vsi->type == I40E_VSI_MAIN)
9837 i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
bc7d338f
ASJ
9838
9839 /* assign it some queues */
9840 ret = i40e_alloc_rings(vsi);
9841 if (ret)
9842 goto err_rings;
9843
9844 /* map all of the rings to the q_vectors */
9845 i40e_vsi_map_rings_to_vectors(vsi);
9846 return vsi;
9847
9848err_rings:
9849 i40e_vsi_free_q_vectors(vsi);
9850 if (vsi->netdev_registered) {
9851 vsi->netdev_registered = false;
9852 unregister_netdev(vsi->netdev);
9853 free_netdev(vsi->netdev);
9854 vsi->netdev = NULL;
9855 }
9856 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9857err_vsi:
9858 i40e_vsi_clear(vsi);
9859 return NULL;
9860}
9861
41c445ff
JB
9862/**
9863 * i40e_vsi_setup - Set up a VSI by a given type
9864 * @pf: board private structure
9865 * @type: VSI type
9866 * @uplink_seid: the switch element to link to
9867 * @param1: usage depends upon VSI type. For VF types, indicates VF id
9868 *
9869 * This allocates the sw VSI structure and its queue resources, then add a VSI
9870 * to the identified VEB.
9871 *
9872 * Returns pointer to the successfully allocated and configure VSI sw struct on
9873 * success, otherwise returns NULL on failure.
9874 **/
9875struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
9876 u16 uplink_seid, u32 param1)
9877{
9878 struct i40e_vsi *vsi = NULL;
9879 struct i40e_veb *veb = NULL;
9880 int ret, i;
9881 int v_idx;
9882
9883 /* The requested uplink_seid must be either
9884 * - the PF's port seid
9885 * no VEB is needed because this is the PF
9886 * or this is a Flow Director special case VSI
9887 * - seid of an existing VEB
9888 * - seid of a VSI that owns an existing VEB
9889 * - seid of a VSI that doesn't own a VEB
9890 * a new VEB is created and the VSI becomes the owner
9891 * - seid of the PF VSI, which is what creates the first VEB
9892 * this is a special case of the previous
9893 *
9894 * Find which uplink_seid we were given and create a new VEB if needed
9895 */
9896 for (i = 0; i < I40E_MAX_VEB; i++) {
9897 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
9898 veb = pf->veb[i];
9899 break;
9900 }
9901 }
9902
9903 if (!veb && uplink_seid != pf->mac_seid) {
9904
505682cd 9905 for (i = 0; i < pf->num_alloc_vsi; i++) {
41c445ff
JB
9906 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
9907 vsi = pf->vsi[i];
9908 break;
9909 }
9910 }
9911 if (!vsi) {
9912 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
9913 uplink_seid);
9914 return NULL;
9915 }
9916
9917 if (vsi->uplink_seid == pf->mac_seid)
9918 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
9919 vsi->tc_config.enabled_tc);
9920 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
9921 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9922 vsi->tc_config.enabled_tc);
79c21a82
ASJ
9923 if (veb) {
9924 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
9925 dev_info(&vsi->back->pdev->dev,
fb43201f 9926 "New VSI creation error, uplink seid of LAN VSI expected.\n");
79c21a82
ASJ
9927 return NULL;
9928 }
fa11cb3d
ASJ
9929 /* We come up by default in VEPA mode if SRIOV is not
9930 * already enabled, in which case we can't force VEPA
9931 * mode.
9932 */
9933 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
9934 veb->bridge_mode = BRIDGE_MODE_VEPA;
9935 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9936 }
51616018 9937 i40e_config_bridge_mode(veb);
79c21a82 9938 }
41c445ff
JB
9939 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9940 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9941 veb = pf->veb[i];
9942 }
9943 if (!veb) {
9944 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
9945 return NULL;
9946 }
9947
9948 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
9949 uplink_seid = veb->seid;
9950 }
9951
9952 /* get vsi sw struct */
9953 v_idx = i40e_vsi_mem_alloc(pf, type);
9954 if (v_idx < 0)
9955 goto err_alloc;
9956 vsi = pf->vsi[v_idx];
cbf61325
ASJ
9957 if (!vsi)
9958 goto err_alloc;
41c445ff
JB
9959 vsi->type = type;
9960 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
9961
9962 if (type == I40E_VSI_MAIN)
9963 pf->lan_vsi = v_idx;
9964 else if (type == I40E_VSI_SRIOV)
9965 vsi->vf_id = param1;
9966 /* assign it some queues */
cbf61325
ASJ
9967 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
9968 vsi->idx);
41c445ff 9969 if (ret < 0) {
049a2be8
SN
9970 dev_info(&pf->pdev->dev,
9971 "failed to get tracking for %d queues for VSI %d err=%d\n",
9972 vsi->alloc_queue_pairs, vsi->seid, ret);
41c445ff
JB
9973 goto err_vsi;
9974 }
9975 vsi->base_queue = ret;
9976
9977 /* get a VSI from the hardware */
9978 vsi->uplink_seid = uplink_seid;
9979 ret = i40e_add_vsi(vsi);
9980 if (ret)
9981 goto err_vsi;
9982
9983 switch (vsi->type) {
9984 /* setup the netdev if needed */
9985 case I40E_VSI_MAIN:
b499ffb0
SV
9986 /* Apply relevant filters if a platform-specific mac
9987 * address was selected.
9988 */
9989 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
9990 ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
9991 if (ret) {
9992 dev_warn(&pf->pdev->dev,
9993 "could not set up macaddr; err %d\n",
9994 ret);
9995 }
9996 }
41c445ff 9997 case I40E_VSI_VMDQ2:
38e00438 9998 case I40E_VSI_FCOE:
41c445ff
JB
9999 ret = i40e_config_netdev(vsi);
10000 if (ret)
10001 goto err_netdev;
10002 ret = register_netdev(vsi->netdev);
10003 if (ret)
10004 goto err_netdev;
10005 vsi->netdev_registered = true;
10006 netif_carrier_off(vsi->netdev);
4e3b35b0
NP
10007#ifdef CONFIG_I40E_DCB
10008 /* Setup DCB netlink interface */
10009 i40e_dcbnl_setup(vsi);
10010#endif /* CONFIG_I40E_DCB */
41c445ff
JB
10011 /* fall through */
10012
10013 case I40E_VSI_FDIR:
10014 /* set up vectors and rings if needed */
10015 ret = i40e_vsi_setup_vectors(vsi);
10016 if (ret)
10017 goto err_msix;
10018
10019 ret = i40e_alloc_rings(vsi);
10020 if (ret)
10021 goto err_rings;
10022
10023 /* map all of the rings to the q_vectors */
10024 i40e_vsi_map_rings_to_vectors(vsi);
10025
10026 i40e_vsi_reset_stats(vsi);
10027 break;
10028
10029 default:
10030 /* no netdev or rings for the other VSI types */
10031 break;
10032 }
10033
e25d00b8
ASJ
10034 if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
10035 (vsi->type == I40E_VSI_VMDQ2)) {
10036 ret = i40e_vsi_config_rss(vsi);
10037 }
41c445ff
JB
10038 return vsi;
10039
10040err_rings:
10041 i40e_vsi_free_q_vectors(vsi);
10042err_msix:
10043 if (vsi->netdev_registered) {
10044 vsi->netdev_registered = false;
10045 unregister_netdev(vsi->netdev);
10046 free_netdev(vsi->netdev);
10047 vsi->netdev = NULL;
10048 }
10049err_netdev:
10050 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
10051err_vsi:
10052 i40e_vsi_clear(vsi);
10053err_alloc:
10054 return NULL;
10055}
10056
10057/**
10058 * i40e_veb_get_bw_info - Query VEB BW information
10059 * @veb: the veb to query
10060 *
10061 * Query the Tx scheduler BW configuration data for given VEB
10062 **/
10063static int i40e_veb_get_bw_info(struct i40e_veb *veb)
10064{
10065 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
10066 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
10067 struct i40e_pf *pf = veb->pf;
10068 struct i40e_hw *hw = &pf->hw;
10069 u32 tc_bw_max;
10070 int ret = 0;
10071 int i;
10072
10073 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
10074 &bw_data, NULL);
10075 if (ret) {
10076 dev_info(&pf->pdev->dev,
f1c7e72e
SN
10077 "query veb bw config failed, err %s aq_err %s\n",
10078 i40e_stat_str(&pf->hw, ret),
10079 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
41c445ff
JB
10080 goto out;
10081 }
10082
10083 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
10084 &ets_data, NULL);
10085 if (ret) {
10086 dev_info(&pf->pdev->dev,
f1c7e72e
SN
10087 "query veb bw ets config failed, err %s aq_err %s\n",
10088 i40e_stat_str(&pf->hw, ret),
10089 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
41c445ff
JB
10090 goto out;
10091 }
10092
10093 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
10094 veb->bw_max_quanta = ets_data.tc_bw_max;
10095 veb->is_abs_credits = bw_data.absolute_credits_enable;
23cd1f09 10096 veb->enabled_tc = ets_data.tc_valid_bits;
41c445ff
JB
10097 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
10098 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
10099 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
10100 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
10101 veb->bw_tc_limit_credits[i] =
10102 le16_to_cpu(bw_data.tc_bw_limits[i]);
10103 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
10104 }
10105
10106out:
10107 return ret;
10108}
10109
10110/**
10111 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
10112 * @pf: board private structure
10113 *
10114 * On error: returns error code (negative)
10115 * On success: returns vsi index in PF (positive)
10116 **/
10117static int i40e_veb_mem_alloc(struct i40e_pf *pf)
10118{
10119 int ret = -ENOENT;
10120 struct i40e_veb *veb;
10121 int i;
10122
10123 /* Need to protect the allocation of switch elements at the PF level */
10124 mutex_lock(&pf->switch_mutex);
10125
10126 /* VEB list may be fragmented if VEB creation/destruction has
10127 * been happening. We can afford to do a quick scan to look
10128 * for any free slots in the list.
10129 *
10130 * find next empty veb slot, looping back around if necessary
10131 */
10132 i = 0;
10133 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
10134 i++;
10135 if (i >= I40E_MAX_VEB) {
10136 ret = -ENOMEM;
10137 goto err_alloc_veb; /* out of VEB slots! */
10138 }
10139
10140 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
10141 if (!veb) {
10142 ret = -ENOMEM;
10143 goto err_alloc_veb;
10144 }
10145 veb->pf = pf;
10146 veb->idx = i;
10147 veb->enabled_tc = 1;
10148
10149 pf->veb[i] = veb;
10150 ret = i;
10151err_alloc_veb:
10152 mutex_unlock(&pf->switch_mutex);
10153 return ret;
10154}
10155
10156/**
10157 * i40e_switch_branch_release - Delete a branch of the switch tree
10158 * @branch: where to start deleting
10159 *
10160 * This uses recursion to find the tips of the branch to be
10161 * removed, deleting until we get back to and can delete this VEB.
10162 **/
10163static void i40e_switch_branch_release(struct i40e_veb *branch)
10164{
10165 struct i40e_pf *pf = branch->pf;
10166 u16 branch_seid = branch->seid;
10167 u16 veb_idx = branch->idx;
10168 int i;
10169
10170 /* release any VEBs on this VEB - RECURSION */
10171 for (i = 0; i < I40E_MAX_VEB; i++) {
10172 if (!pf->veb[i])
10173 continue;
10174 if (pf->veb[i]->uplink_seid == branch->seid)
10175 i40e_switch_branch_release(pf->veb[i]);
10176 }
10177
10178 /* Release the VSIs on this VEB, but not the owner VSI.
10179 *
10180 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
10181 * the VEB itself, so don't use (*branch) after this loop.
10182 */
505682cd 10183 for (i = 0; i < pf->num_alloc_vsi; i++) {
41c445ff
JB
10184 if (!pf->vsi[i])
10185 continue;
10186 if (pf->vsi[i]->uplink_seid == branch_seid &&
10187 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10188 i40e_vsi_release(pf->vsi[i]);
10189 }
10190 }
10191
10192 /* There's one corner case where the VEB might not have been
10193 * removed, so double check it here and remove it if needed.
10194 * This case happens if the veb was created from the debugfs
10195 * commands and no VSIs were added to it.
10196 */
10197 if (pf->veb[veb_idx])
10198 i40e_veb_release(pf->veb[veb_idx]);
10199}
10200
10201/**
10202 * i40e_veb_clear - remove veb struct
10203 * @veb: the veb to remove
10204 **/
10205static void i40e_veb_clear(struct i40e_veb *veb)
10206{
10207 if (!veb)
10208 return;
10209
10210 if (veb->pf) {
10211 struct i40e_pf *pf = veb->pf;
10212
10213 mutex_lock(&pf->switch_mutex);
10214 if (pf->veb[veb->idx] == veb)
10215 pf->veb[veb->idx] = NULL;
10216 mutex_unlock(&pf->switch_mutex);
10217 }
10218
10219 kfree(veb);
10220}
10221
10222/**
10223 * i40e_veb_release - Delete a VEB and free its resources
10224 * @veb: the VEB being removed
10225 **/
10226void i40e_veb_release(struct i40e_veb *veb)
10227{
10228 struct i40e_vsi *vsi = NULL;
10229 struct i40e_pf *pf;
10230 int i, n = 0;
10231
10232 pf = veb->pf;
10233
10234 /* find the remaining VSI and check for extras */
505682cd 10235 for (i = 0; i < pf->num_alloc_vsi; i++) {
41c445ff
JB
10236 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
10237 n++;
10238 vsi = pf->vsi[i];
10239 }
10240 }
10241 if (n != 1) {
10242 dev_info(&pf->pdev->dev,
10243 "can't remove VEB %d with %d VSIs left\n",
10244 veb->seid, n);
10245 return;
10246 }
10247
10248 /* move the remaining VSI to uplink veb */
10249 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
10250 if (veb->uplink_seid) {
10251 vsi->uplink_seid = veb->uplink_seid;
10252 if (veb->uplink_seid == pf->mac_seid)
10253 vsi->veb_idx = I40E_NO_VEB;
10254 else
10255 vsi->veb_idx = veb->veb_idx;
10256 } else {
10257 /* floating VEB */
10258 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10259 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
10260 }
10261
10262 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
10263 i40e_veb_clear(veb);
41c445ff
JB
10264}
10265
10266/**
10267 * i40e_add_veb - create the VEB in the switch
10268 * @veb: the VEB to be instantiated
10269 * @vsi: the controlling VSI
10270 **/
10271static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
10272{
f1c7e72e 10273 struct i40e_pf *pf = veb->pf;
66fc360a 10274 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
41c445ff
JB
10275 int ret;
10276
f1c7e72e 10277 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
5bc16031 10278 veb->enabled_tc, false,
66fc360a 10279 &veb->seid, enable_stats, NULL);
5bc16031
MW
10280
10281 /* get a VEB from the hardware */
41c445ff 10282 if (ret) {
f1c7e72e
SN
10283 dev_info(&pf->pdev->dev,
10284 "couldn't add VEB, err %s aq_err %s\n",
10285 i40e_stat_str(&pf->hw, ret),
10286 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
41c445ff
JB
10287 return -EPERM;
10288 }
10289
10290 /* get statistics counter */
f1c7e72e 10291 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
41c445ff
JB
10292 &veb->stats_idx, NULL, NULL, NULL);
10293 if (ret) {
f1c7e72e
SN
10294 dev_info(&pf->pdev->dev,
10295 "couldn't get VEB statistics idx, err %s aq_err %s\n",
10296 i40e_stat_str(&pf->hw, ret),
10297 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
41c445ff
JB
10298 return -EPERM;
10299 }
10300 ret = i40e_veb_get_bw_info(veb);
10301 if (ret) {
f1c7e72e
SN
10302 dev_info(&pf->pdev->dev,
10303 "couldn't get VEB bw info, err %s aq_err %s\n",
10304 i40e_stat_str(&pf->hw, ret),
10305 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10306 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
41c445ff
JB
10307 return -ENOENT;
10308 }
10309
10310 vsi->uplink_seid = veb->seid;
10311 vsi->veb_idx = veb->idx;
10312 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10313
10314 return 0;
10315}
10316
10317/**
10318 * i40e_veb_setup - Set up a VEB
10319 * @pf: board private structure
10320 * @flags: VEB setup flags
10321 * @uplink_seid: the switch element to link to
10322 * @vsi_seid: the initial VSI seid
10323 * @enabled_tc: Enabled TC bit-map
10324 *
10325 * This allocates the sw VEB structure and links it into the switch
10326 * It is possible and legal for this to be a duplicate of an already
10327 * existing VEB. It is also possible for both uplink and vsi seids
10328 * to be zero, in order to create a floating VEB.
10329 *
10330 * Returns pointer to the successfully allocated VEB sw struct on
10331 * success, otherwise returns NULL on failure.
10332 **/
10333struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
10334 u16 uplink_seid, u16 vsi_seid,
10335 u8 enabled_tc)
10336{
10337 struct i40e_veb *veb, *uplink_veb = NULL;
10338 int vsi_idx, veb_idx;
10339 int ret;
10340
10341 /* if one seid is 0, the other must be 0 to create a floating relay */
10342 if ((uplink_seid == 0 || vsi_seid == 0) &&
10343 (uplink_seid + vsi_seid != 0)) {
10344 dev_info(&pf->pdev->dev,
10345 "one, not both seid's are 0: uplink=%d vsi=%d\n",
10346 uplink_seid, vsi_seid);
10347 return NULL;
10348 }
10349
10350 /* make sure there is such a vsi and uplink */
505682cd 10351 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
41c445ff
JB
10352 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
10353 break;
505682cd 10354 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
41c445ff
JB
10355 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
10356 vsi_seid);
10357 return NULL;
10358 }
10359
10360 if (uplink_seid && uplink_seid != pf->mac_seid) {
10361 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10362 if (pf->veb[veb_idx] &&
10363 pf->veb[veb_idx]->seid == uplink_seid) {
10364 uplink_veb = pf->veb[veb_idx];
10365 break;
10366 }
10367 }
10368 if (!uplink_veb) {
10369 dev_info(&pf->pdev->dev,
10370 "uplink seid %d not found\n", uplink_seid);
10371 return NULL;
10372 }
10373 }
10374
10375 /* get veb sw struct */
10376 veb_idx = i40e_veb_mem_alloc(pf);
10377 if (veb_idx < 0)
10378 goto err_alloc;
10379 veb = pf->veb[veb_idx];
10380 veb->flags = flags;
10381 veb->uplink_seid = uplink_seid;
10382 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
10383 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
10384
10385 /* create the VEB in the switch */
10386 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
10387 if (ret)
10388 goto err_veb;
1bb8b935
SN
10389 if (vsi_idx == pf->lan_vsi)
10390 pf->lan_veb = veb->idx;
41c445ff
JB
10391
10392 return veb;
10393
10394err_veb:
10395 i40e_veb_clear(veb);
10396err_alloc:
10397 return NULL;
10398}
10399
10400/**
b40c82e6 10401 * i40e_setup_pf_switch_element - set PF vars based on switch type
41c445ff
JB
10402 * @pf: board private structure
10403 * @ele: element we are building info from
10404 * @num_reported: total number of elements
10405 * @printconfig: should we print the contents
10406 *
10407 * helper function to assist in extracting a few useful SEID values.
10408 **/
10409static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
10410 struct i40e_aqc_switch_config_element_resp *ele,
10411 u16 num_reported, bool printconfig)
10412{
10413 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
10414 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
10415 u8 element_type = ele->element_type;
10416 u16 seid = le16_to_cpu(ele->seid);
10417
10418 if (printconfig)
10419 dev_info(&pf->pdev->dev,
10420 "type=%d seid=%d uplink=%d downlink=%d\n",
10421 element_type, seid, uplink_seid, downlink_seid);
10422
10423 switch (element_type) {
10424 case I40E_SWITCH_ELEMENT_TYPE_MAC:
10425 pf->mac_seid = seid;
10426 break;
10427 case I40E_SWITCH_ELEMENT_TYPE_VEB:
10428 /* Main VEB? */
10429 if (uplink_seid != pf->mac_seid)
10430 break;
10431 if (pf->lan_veb == I40E_NO_VEB) {
10432 int v;
10433
10434 /* find existing or else empty VEB */
10435 for (v = 0; v < I40E_MAX_VEB; v++) {
10436 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
10437 pf->lan_veb = v;
10438 break;
10439 }
10440 }
10441 if (pf->lan_veb == I40E_NO_VEB) {
10442 v = i40e_veb_mem_alloc(pf);
10443 if (v < 0)
10444 break;
10445 pf->lan_veb = v;
10446 }
10447 }
10448
10449 pf->veb[pf->lan_veb]->seid = seid;
10450 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
10451 pf->veb[pf->lan_veb]->pf = pf;
10452 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
10453 break;
10454 case I40E_SWITCH_ELEMENT_TYPE_VSI:
10455 if (num_reported != 1)
10456 break;
10457 /* This is immediately after a reset so we can assume this is
10458 * the PF's VSI
10459 */
10460 pf->mac_seid = uplink_seid;
10461 pf->pf_seid = downlink_seid;
10462 pf->main_vsi_seid = seid;
10463 if (printconfig)
10464 dev_info(&pf->pdev->dev,
10465 "pf_seid=%d main_vsi_seid=%d\n",
10466 pf->pf_seid, pf->main_vsi_seid);
10467 break;
10468 case I40E_SWITCH_ELEMENT_TYPE_PF:
10469 case I40E_SWITCH_ELEMENT_TYPE_VF:
10470 case I40E_SWITCH_ELEMENT_TYPE_EMP:
10471 case I40E_SWITCH_ELEMENT_TYPE_BMC:
10472 case I40E_SWITCH_ELEMENT_TYPE_PE:
10473 case I40E_SWITCH_ELEMENT_TYPE_PA:
10474 /* ignore these for now */
10475 break;
10476 default:
10477 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
10478 element_type, seid);
10479 break;
10480 }
10481}
10482
10483/**
10484 * i40e_fetch_switch_configuration - Get switch config from firmware
10485 * @pf: board private structure
10486 * @printconfig: should we print the contents
10487 *
10488 * Get the current switch configuration from the device and
10489 * extract a few useful SEID values.
10490 **/
10491int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10492{
10493 struct i40e_aqc_get_switch_config_resp *sw_config;
10494 u16 next_seid = 0;
10495 int ret = 0;
10496 u8 *aq_buf;
10497 int i;
10498
10499 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10500 if (!aq_buf)
10501 return -ENOMEM;
10502
10503 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10504 do {
10505 u16 num_reported, num_total;
10506
10507 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10508 I40E_AQ_LARGE_BUF,
10509 &next_seid, NULL);
10510 if (ret) {
10511 dev_info(&pf->pdev->dev,
f1c7e72e
SN
10512 "get switch config failed err %s aq_err %s\n",
10513 i40e_stat_str(&pf->hw, ret),
10514 i40e_aq_str(&pf->hw,
10515 pf->hw.aq.asq_last_status));
41c445ff
JB
10516 kfree(aq_buf);
10517 return -ENOENT;
10518 }
10519
10520 num_reported = le16_to_cpu(sw_config->header.num_reported);
10521 num_total = le16_to_cpu(sw_config->header.num_total);
10522
10523 if (printconfig)
10524 dev_info(&pf->pdev->dev,
10525 "header: %d reported %d total\n",
10526 num_reported, num_total);
10527
41c445ff
JB
10528 for (i = 0; i < num_reported; i++) {
10529 struct i40e_aqc_switch_config_element_resp *ele =
10530 &sw_config->element[i];
10531
10532 i40e_setup_pf_switch_element(pf, ele, num_reported,
10533 printconfig);
10534 }
10535 } while (next_seid != 0);
10536
10537 kfree(aq_buf);
10538 return ret;
10539}
10540
10541/**
10542 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
10543 * @pf: board private structure
bc7d338f 10544 * @reinit: if the Main VSI needs to re-initialized.
41c445ff
JB
10545 *
10546 * Returns 0 on success, negative value on failure
10547 **/
bc7d338f 10548static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
41c445ff 10549{
b5569892 10550 u16 flags = 0;
41c445ff
JB
10551 int ret;
10552
10553 /* find out what's out there already */
10554 ret = i40e_fetch_switch_configuration(pf, false);
10555 if (ret) {
10556 dev_info(&pf->pdev->dev,
f1c7e72e
SN
10557 "couldn't fetch switch config, err %s aq_err %s\n",
10558 i40e_stat_str(&pf->hw, ret),
10559 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
41c445ff
JB
10560 return ret;
10561 }
10562 i40e_pf_reset_stats(pf);
10563
b5569892
ASJ
10564 /* set the switch config bit for the whole device to
10565 * support limited promisc or true promisc
10566 * when user requests promisc. The default is limited
10567 * promisc.
10568 */
10569
10570 if ((pf->hw.pf_id == 0) &&
10571 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
10572 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10573
10574 if (pf->hw.pf_id == 0) {
10575 u16 valid_flags;
10576
10577 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10578 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
10579 NULL);
10580 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
10581 dev_info(&pf->pdev->dev,
10582 "couldn't set switch config bits, err %s aq_err %s\n",
10583 i40e_stat_str(&pf->hw, ret),
10584 i40e_aq_str(&pf->hw,
10585 pf->hw.aq.asq_last_status));
10586 /* not a fatal problem, just keep going */
10587 }
10588 }
10589
41c445ff 10590 /* first time setup */
bc7d338f 10591 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
41c445ff
JB
10592 struct i40e_vsi *vsi = NULL;
10593 u16 uplink_seid;
10594
10595 /* Set up the PF VSI associated with the PF's main VSI
10596 * that is already in the HW switch
10597 */
10598 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
10599 uplink_seid = pf->veb[pf->lan_veb]->seid;
10600 else
10601 uplink_seid = pf->mac_seid;
bc7d338f
ASJ
10602 if (pf->lan_vsi == I40E_NO_VSI)
10603 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
10604 else if (reinit)
10605 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
41c445ff
JB
10606 if (!vsi) {
10607 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
10608 i40e_fdir_teardown(pf);
10609 return -EAGAIN;
10610 }
41c445ff
JB
10611 } else {
10612 /* force a reset of TC and queue layout configurations */
10613 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
6995b36c 10614
41c445ff
JB
10615 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10616 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10617 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
10618 }
10619 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
10620
cbf61325
ASJ
10621 i40e_fdir_sb_setup(pf);
10622
41c445ff
JB
10623 /* Setup static PF queue filter control settings */
10624 ret = i40e_setup_pf_filter_control(pf);
10625 if (ret) {
10626 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
10627 ret);
10628 /* Failure here should not stop continuing other steps */
10629 }
10630
10631 /* enable RSS in the HW, even for only one queue, as the stack can use
10632 * the hash
10633 */
10634 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
043dd650 10635 i40e_pf_config_rss(pf);
41c445ff
JB
10636
10637 /* fill in link information and enable LSE reporting */
0a862b43 10638 i40e_update_link_info(&pf->hw);
a34a6711
MW
10639 i40e_link_event(pf);
10640
d52c20b7 10641 /* Initialize user-specific link properties */
41c445ff
JB
10642 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
10643 I40E_AQ_AN_COMPLETED) ? true : false);
d52c20b7 10644
beb0dff1
JK
10645 i40e_ptp_init(pf);
10646
41c445ff
JB
10647 return ret;
10648}
10649
41c445ff
JB
10650/**
10651 * i40e_determine_queue_usage - Work out queue distribution
10652 * @pf: board private structure
10653 **/
10654static void i40e_determine_queue_usage(struct i40e_pf *pf)
10655{
41c445ff
JB
10656 int queues_left;
10657
10658 pf->num_lan_qps = 0;
38e00438
VD
10659#ifdef I40E_FCOE
10660 pf->num_fcoe_qps = 0;
10661#endif
41c445ff
JB
10662
10663 /* Find the max queues to be put into basic use. We'll always be
10664 * using TC0, whether or not DCB is running, and TC0 will get the
10665 * big RSS set.
10666 */
10667 queues_left = pf->hw.func_caps.num_tx_qp;
10668
cbf61325 10669 if ((queues_left == 1) ||
9aa7e935 10670 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
41c445ff
JB
10671 /* one qp for PF, no queues for anything else */
10672 queues_left = 0;
acd65448 10673 pf->alloc_rss_size = pf->num_lan_qps = 1;
41c445ff
JB
10674
10675 /* make sure all the fancies are disabled */
60ea5f83 10676 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
e3219ce6 10677 I40E_FLAG_IWARP_ENABLED |
38e00438
VD
10678#ifdef I40E_FCOE
10679 I40E_FLAG_FCOE_ENABLED |
10680#endif
60ea5f83
JB
10681 I40E_FLAG_FD_SB_ENABLED |
10682 I40E_FLAG_FD_ATR_ENABLED |
4d9b6043 10683 I40E_FLAG_DCB_CAPABLE |
a036244c 10684 I40E_FLAG_DCB_ENABLED |
60ea5f83
JB
10685 I40E_FLAG_SRIOV_ENABLED |
10686 I40E_FLAG_VMDQ_ENABLED);
9aa7e935
FZ
10687 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
10688 I40E_FLAG_FD_SB_ENABLED |
bbe7d0e0 10689 I40E_FLAG_FD_ATR_ENABLED |
4d9b6043 10690 I40E_FLAG_DCB_CAPABLE))) {
9aa7e935 10691 /* one qp for PF */
acd65448 10692 pf->alloc_rss_size = pf->num_lan_qps = 1;
9aa7e935
FZ
10693 queues_left -= pf->num_lan_qps;
10694
10695 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
e3219ce6 10696 I40E_FLAG_IWARP_ENABLED |
38e00438
VD
10697#ifdef I40E_FCOE
10698 I40E_FLAG_FCOE_ENABLED |
10699#endif
9aa7e935
FZ
10700 I40E_FLAG_FD_SB_ENABLED |
10701 I40E_FLAG_FD_ATR_ENABLED |
10702 I40E_FLAG_DCB_ENABLED |
10703 I40E_FLAG_VMDQ_ENABLED);
41c445ff 10704 } else {
cbf61325 10705 /* Not enough queues for all TCs */
4d9b6043 10706 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
cbf61325 10707 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
a036244c
DE
10708 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
10709 I40E_FLAG_DCB_ENABLED);
cbf61325
ASJ
10710 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
10711 }
9a3bd2f1
ASJ
10712 pf->num_lan_qps = max_t(int, pf->rss_size_max,
10713 num_online_cpus());
10714 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
10715 pf->hw.func_caps.num_tx_qp);
10716
cbf61325
ASJ
10717 queues_left -= pf->num_lan_qps;
10718 }
10719
38e00438
VD
10720#ifdef I40E_FCOE
10721 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
10722 if (I40E_DEFAULT_FCOE <= queues_left) {
10723 pf->num_fcoe_qps = I40E_DEFAULT_FCOE;
10724 } else if (I40E_MINIMUM_FCOE <= queues_left) {
10725 pf->num_fcoe_qps = I40E_MINIMUM_FCOE;
10726 } else {
10727 pf->num_fcoe_qps = 0;
10728 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
10729 dev_info(&pf->pdev->dev, "not enough queues for FCoE. FCoE feature will be disabled\n");
10730 }
10731
10732 queues_left -= pf->num_fcoe_qps;
10733 }
10734
10735#endif
cbf61325
ASJ
10736 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10737 if (queues_left > 1) {
10738 queues_left -= 1; /* save 1 queue for FD */
10739 } else {
10740 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10741 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
10742 }
41c445ff
JB
10743 }
10744
10745 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10746 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
cbf61325
ASJ
10747 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
10748 (queues_left / pf->num_vf_qps));
41c445ff
JB
10749 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
10750 }
10751
10752 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10753 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
10754 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
10755 (queues_left / pf->num_vmdq_qps));
10756 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
10757 }
10758
f8ff1464 10759 pf->queues_left = queues_left;
8279e495
NP
10760 dev_dbg(&pf->pdev->dev,
10761 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
10762 pf->hw.func_caps.num_tx_qp,
10763 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
acd65448
HZ
10764 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
10765 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
10766 queues_left);
38e00438 10767#ifdef I40E_FCOE
8279e495 10768 dev_dbg(&pf->pdev->dev, "fcoe queues = %d\n", pf->num_fcoe_qps);
38e00438 10769#endif
41c445ff
JB
10770}
10771
10772/**
10773 * i40e_setup_pf_filter_control - Setup PF static filter control
10774 * @pf: PF to be setup
10775 *
b40c82e6 10776 * i40e_setup_pf_filter_control sets up a PF's initial filter control
41c445ff
JB
10777 * settings. If PE/FCoE are enabled then it will also set the per PF
10778 * based filter sizes required for them. It also enables Flow director,
10779 * ethertype and macvlan type filter settings for the pf.
10780 *
10781 * Returns 0 on success, negative on failure
10782 **/
10783static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
10784{
10785 struct i40e_filter_control_settings *settings = &pf->filter_settings;
10786
10787 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
10788
10789 /* Flow Director is enabled */
60ea5f83 10790 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
41c445ff
JB
10791 settings->enable_fdir = true;
10792
10793 /* Ethtype and MACVLAN filters enabled for PF */
10794 settings->enable_ethtype = true;
10795 settings->enable_macvlan = true;
10796
10797 if (i40e_set_filter_control(&pf->hw, settings))
10798 return -ENOENT;
10799
10800 return 0;
10801}
10802
0c22b3dd 10803#define INFO_STRING_LEN 255
7fd89545 10804#define REMAIN(__x) (INFO_STRING_LEN - (__x))
0c22b3dd
JB
10805static void i40e_print_features(struct i40e_pf *pf)
10806{
10807 struct i40e_hw *hw = &pf->hw;
3b195843
JP
10808 char *buf;
10809 int i;
0c22b3dd 10810
3b195843
JP
10811 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
10812 if (!buf)
0c22b3dd 10813 return;
0c22b3dd 10814
3b195843 10815 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
0c22b3dd 10816#ifdef CONFIG_PCI_IOV
3b195843 10817 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
0c22b3dd 10818#endif
1a557afc 10819 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
7fd89545 10820 pf->hw.func_caps.num_vsis,
1a557afc 10821 pf->vsi[pf->lan_vsi]->num_queue_pairs);
0c22b3dd 10822 if (pf->flags & I40E_FLAG_RSS_ENABLED)
3b195843 10823 i += snprintf(&buf[i], REMAIN(i), " RSS");
0c22b3dd 10824 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
3b195843 10825 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
c6423ff1 10826 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
3b195843
JP
10827 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
10828 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
c6423ff1 10829 }
4d9b6043 10830 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
3b195843 10831 i += snprintf(&buf[i], REMAIN(i), " DCB");
3b195843 10832 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
6a899024 10833 i += snprintf(&buf[i], REMAIN(i), " Geneve");
0c22b3dd 10834 if (pf->flags & I40E_FLAG_PTP)
3b195843 10835 i += snprintf(&buf[i], REMAIN(i), " PTP");
38e00438
VD
10836#ifdef I40E_FCOE
10837 if (pf->flags & I40E_FLAG_FCOE_ENABLED)
3b195843 10838 i += snprintf(&buf[i], REMAIN(i), " FCOE");
38e00438 10839#endif
6dec1017 10840 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
3b195843 10841 i += snprintf(&buf[i], REMAIN(i), " VEB");
6dec1017 10842 else
3b195843 10843 i += snprintf(&buf[i], REMAIN(i), " VEPA");
0c22b3dd 10844
3b195843
JP
10845 dev_info(&pf->pdev->dev, "%s\n", buf);
10846 kfree(buf);
7fd89545 10847 WARN_ON(i > INFO_STRING_LEN);
0c22b3dd
JB
10848}
10849
b499ffb0
SV
10850/**
10851 * i40e_get_platform_mac_addr - get platform-specific MAC address
10852 *
10853 * @pdev: PCI device information struct
10854 * @pf: board private structure
10855 *
10856 * Look up the MAC address in Open Firmware on systems that support it,
10857 * and use IDPROM on SPARC if no OF address is found. On return, the
10858 * I40E_FLAG_PF_MAC will be wset in pf->flags if a platform-specific value
10859 * has been selected.
10860 **/
10861static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
10862{
b499ffb0 10863 pf->flags &= ~I40E_FLAG_PF_MAC;
ba94272d 10864 if (!eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
b499ffb0 10865 pf->flags |= I40E_FLAG_PF_MAC;
b499ffb0
SV
10866}
10867
41c445ff
JB
10868/**
10869 * i40e_probe - Device initialization routine
10870 * @pdev: PCI device information struct
10871 * @ent: entry in i40e_pci_tbl
10872 *
b40c82e6
JK
10873 * i40e_probe initializes a PF identified by a pci_dev structure.
10874 * The OS initialization, configuring of the PF private structure,
41c445ff
JB
10875 * and a hardware reset occur.
10876 *
10877 * Returns 0 on success, negative on failure
10878 **/
10879static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
10880{
e827845c 10881 struct i40e_aq_get_phy_abilities_resp abilities;
41c445ff
JB
10882 struct i40e_pf *pf;
10883 struct i40e_hw *hw;
93cd765b 10884 static u16 pfs_found;
1d5109d1 10885 u16 wol_nvm_bits;
d4dfb81a 10886 u16 link_status;
6f66a484 10887 int err;
4f2f017c 10888 u32 val;
8a9eb7d3 10889 u32 i;
58fc3267 10890 u8 set_fc_aq_fail;
41c445ff
JB
10891
10892 err = pci_enable_device_mem(pdev);
10893 if (err)
10894 return err;
10895
10896 /* set up for high or low dma */
6494294f 10897 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
6494294f 10898 if (err) {
e3e3bfdd
JS
10899 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
10900 if (err) {
10901 dev_err(&pdev->dev,
10902 "DMA configuration failed: 0x%x\n", err);
10903 goto err_dma;
10904 }
41c445ff
JB
10905 }
10906
10907 /* set up pci connections */
56d766d6 10908 err = pci_request_mem_regions(pdev, i40e_driver_name);
41c445ff
JB
10909 if (err) {
10910 dev_info(&pdev->dev,
10911 "pci_request_selected_regions failed %d\n", err);
10912 goto err_pci_reg;
10913 }
10914
10915 pci_enable_pcie_error_reporting(pdev);
10916 pci_set_master(pdev);
10917
10918 /* Now that we have a PCI connection, we need to do the
10919 * low level device setup. This is primarily setting up
10920 * the Admin Queue structures and then querying for the
10921 * device's current profile information.
10922 */
10923 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
10924 if (!pf) {
10925 err = -ENOMEM;
10926 goto err_pf_alloc;
10927 }
10928 pf->next_vsi = 0;
10929 pf->pdev = pdev;
10930 set_bit(__I40E_DOWN, &pf->state);
10931
10932 hw = &pf->hw;
10933 hw->back = pf;
232f4706 10934
2ac8b675
SN
10935 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
10936 I40E_MAX_CSR_SPACE);
232f4706 10937
2ac8b675 10938 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
41c445ff
JB
10939 if (!hw->hw_addr) {
10940 err = -EIO;
10941 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
10942 (unsigned int)pci_resource_start(pdev, 0),
2ac8b675 10943 pf->ioremap_len, err);
41c445ff
JB
10944 goto err_ioremap;
10945 }
10946 hw->vendor_id = pdev->vendor;
10947 hw->device_id = pdev->device;
10948 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
10949 hw->subsystem_vendor_id = pdev->subsystem_vendor;
10950 hw->subsystem_device_id = pdev->subsystem_device;
10951 hw->bus.device = PCI_SLOT(pdev->devfn);
10952 hw->bus.func = PCI_FUNC(pdev->devfn);
93cd765b 10953 pf->instance = pfs_found;
41c445ff 10954
de03d2b0
SN
10955 /* set up the locks for the AQ, do this only once in probe
10956 * and destroy them only once in remove
10957 */
10958 mutex_init(&hw->aq.asq_mutex);
10959 mutex_init(&hw->aq.arq_mutex);
10960
5d4ca23e
AD
10961 pf->msg_enable = netif_msg_init(debug,
10962 NETIF_MSG_DRV |
10963 NETIF_MSG_PROBE |
10964 NETIF_MSG_LINK);
10965 if (debug < -1)
10966 pf->hw.debug_mask = debug;
5b5faa43 10967
7134f9ce
JB
10968 /* do a special CORER for clearing PXE mode once at init */
10969 if (hw->revision_id == 0 &&
10970 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
10971 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
10972 i40e_flush(hw);
10973 msleep(200);
10974 pf->corer_count++;
10975
10976 i40e_clear_pxe_mode(hw);
10977 }
10978
41c445ff 10979 /* Reset here to make sure all is clean and to define PF 'n' */
838d41d9 10980 i40e_clear_hw(hw);
41c445ff
JB
10981 err = i40e_pf_reset(hw);
10982 if (err) {
10983 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
10984 goto err_pf_reset;
10985 }
10986 pf->pfr_count++;
10987
10988 hw->aq.num_arq_entries = I40E_AQ_LEN;
10989 hw->aq.num_asq_entries = I40E_AQ_LEN;
10990 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10991 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10992 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
b2008cbf 10993
b294ac70 10994 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
b2008cbf
CW
10995 "%s-%s:misc",
10996 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
41c445ff
JB
10997
10998 err = i40e_init_shared_code(hw);
10999 if (err) {
b2a75c58
ASJ
11000 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
11001 err);
41c445ff
JB
11002 goto err_pf_reset;
11003 }
11004
d52c20b7
JB
11005 /* set up a default setting for link flow control */
11006 pf->hw.fc.requested_mode = I40E_FC_NONE;
11007
41c445ff 11008 err = i40e_init_adminq(hw);
2b2426a7
CW
11009 if (err) {
11010 if (err == I40E_ERR_FIRMWARE_API_VERSION)
11011 dev_info(&pdev->dev,
11012 "The driver for the device stopped because the NVM image is newer than expected. You must install the most recent version of the network driver.\n");
11013 else
11014 dev_info(&pdev->dev,
11015 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
11016
11017 goto err_pf_reset;
11018 }
f0b44440 11019
6dec1017
SN
11020 /* provide nvm, fw, api versions */
11021 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
11022 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
11023 hw->aq.api_maj_ver, hw->aq.api_min_ver,
11024 i40e_nvm_version_str(hw));
f0b44440 11025
7aa67613
CS
11026 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
11027 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
278b6f62 11028 dev_info(&pdev->dev,
7aa67613
CS
11029 "The driver for the device detected a newer version of the NVM image than expected. Please install the most recent version of the network driver.\n");
11030 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
11031 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
278b6f62 11032 dev_info(&pdev->dev,
7aa67613 11033 "The driver for the device detected an older version of the NVM image than expected. Please update the NVM image.\n");
278b6f62 11034
4eb3f768
SN
11035 i40e_verify_eeprom(pf);
11036
2c5fe33b
JB
11037 /* Rev 0 hardware was never productized */
11038 if (hw->revision_id < 1)
11039 dev_warn(&pdev->dev, "This device is a pre-production adapter/LOM. Please be aware there may be issues with your hardware. If you are experiencing problems please contact your Intel or hardware representative who provided you with this hardware.\n");
11040
6ff4ef86 11041 i40e_clear_pxe_mode(hw);
41c445ff
JB
11042 err = i40e_get_capabilities(pf);
11043 if (err)
11044 goto err_adminq_setup;
11045
11046 err = i40e_sw_init(pf);
11047 if (err) {
11048 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
11049 goto err_sw_init;
11050 }
11051
11052 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
11053 hw->func_caps.num_rx_qp,
11054 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
11055 if (err) {
11056 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
11057 goto err_init_lan_hmc;
11058 }
11059
11060 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
11061 if (err) {
11062 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
11063 err = -ENOENT;
11064 goto err_configure_lan_hmc;
11065 }
11066
b686ece5
NP
11067 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
11068 * Ignore error return codes because if it was already disabled via
11069 * hardware settings this will fail
11070 */
f1bbad33 11071 if (pf->flags & I40E_FLAG_STOP_FW_LLDP) {
b686ece5
NP
11072 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
11073 i40e_aq_stop_lldp(hw, true, NULL);
11074 }
11075
41c445ff 11076 i40e_get_mac_addr(hw, hw->mac.addr);
b499ffb0
SV
11077 /* allow a platform config to override the HW addr */
11078 i40e_get_platform_mac_addr(pdev, pf);
f62b5060 11079 if (!is_valid_ether_addr(hw->mac.addr)) {
41c445ff
JB
11080 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
11081 err = -EIO;
11082 goto err_mac_addr;
11083 }
11084 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
9a173901 11085 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
1f224ad2
NP
11086 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
11087 if (is_valid_ether_addr(hw->mac.port_addr))
11088 pf->flags |= I40E_FLAG_PORT_ID_VALID;
38e00438
VD
11089#ifdef I40E_FCOE
11090 err = i40e_get_san_mac_addr(hw, hw->mac.san_addr);
11091 if (err)
11092 dev_info(&pdev->dev,
11093 "(non-fatal) SAN MAC retrieval failed: %d\n", err);
11094 if (!is_valid_ether_addr(hw->mac.san_addr)) {
11095 dev_warn(&pdev->dev, "invalid SAN MAC address %pM, falling back to LAN MAC\n",
11096 hw->mac.san_addr);
11097 ether_addr_copy(hw->mac.san_addr, hw->mac.addr);
11098 }
11099 dev_info(&pf->pdev->dev, "SAN MAC: %pM\n", hw->mac.san_addr);
11100#endif /* I40E_FCOE */
41c445ff
JB
11101
11102 pci_set_drvdata(pdev, pf);
11103 pci_save_state(pdev);
4e3b35b0
NP
11104#ifdef CONFIG_I40E_DCB
11105 err = i40e_init_pf_dcb(pf);
11106 if (err) {
aebfc816 11107 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
c17ef430 11108 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
014269ff 11109 /* Continue without DCB enabled */
4e3b35b0
NP
11110 }
11111#endif /* CONFIG_I40E_DCB */
41c445ff
JB
11112
11113 /* set up periodic task facility */
11114 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
11115 pf->service_timer_period = HZ;
11116
11117 INIT_WORK(&pf->service_task, i40e_service_task);
11118 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
11119 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
41c445ff 11120
1d5109d1
SN
11121 /* NVM bit on means WoL disabled for the port */
11122 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
75f5cea9 11123 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
1d5109d1
SN
11124 pf->wol_en = false;
11125 else
11126 pf->wol_en = true;
8e2773ae
SN
11127 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
11128
41c445ff
JB
11129 /* set up the main switch operations */
11130 i40e_determine_queue_usage(pf);
c1147280
JB
11131 err = i40e_init_interrupt_scheme(pf);
11132 if (err)
11133 goto err_switch_setup;
41c445ff 11134
505682cd
MW
11135 /* The number of VSIs reported by the FW is the minimum guaranteed
11136 * to us; HW supports far more and we share the remaining pool with
11137 * the other PFs. We allocate space for more than the guarantee with
11138 * the understanding that we might not get them all later.
41c445ff 11139 */
505682cd
MW
11140 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
11141 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
11142 else
11143 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
11144
11145 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
d17038d6
JB
11146 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
11147 GFP_KERNEL);
ed87ac09
WY
11148 if (!pf->vsi) {
11149 err = -ENOMEM;
41c445ff 11150 goto err_switch_setup;
ed87ac09 11151 }
41c445ff 11152
fa11cb3d
ASJ
11153#ifdef CONFIG_PCI_IOV
11154 /* prep for VF support */
11155 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11156 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11157 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
11158 if (pci_num_vf(pdev))
11159 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11160 }
11161#endif
bc7d338f 11162 err = i40e_setup_pf_switch(pf, false);
41c445ff
JB
11163 if (err) {
11164 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
11165 goto err_vsis;
11166 }
58fc3267
HZ
11167
11168 /* Make sure flow control is set according to current settings */
11169 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
11170 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
11171 dev_dbg(&pf->pdev->dev,
11172 "Set fc with err %s aq_err %s on get_phy_cap\n",
11173 i40e_stat_str(hw, err),
11174 i40e_aq_str(hw, hw->aq.asq_last_status));
11175 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
11176 dev_dbg(&pf->pdev->dev,
11177 "Set fc with err %s aq_err %s on set_phy_config\n",
11178 i40e_stat_str(hw, err),
11179 i40e_aq_str(hw, hw->aq.asq_last_status));
11180 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
11181 dev_dbg(&pf->pdev->dev,
11182 "Set fc with err %s aq_err %s on get_link_info\n",
11183 i40e_stat_str(hw, err),
11184 i40e_aq_str(hw, hw->aq.asq_last_status));
11185
8a9eb7d3 11186 /* if FDIR VSI was set up, start it now */
505682cd 11187 for (i = 0; i < pf->num_alloc_vsi; i++) {
8a9eb7d3
SN
11188 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
11189 i40e_vsi_open(pf->vsi[i]);
11190 break;
11191 }
11192 }
41c445ff 11193
2f0aff41
SN
11194 /* The driver only wants link up/down and module qualification
11195 * reports from firmware. Note the negative logic.
7e2453fe
JB
11196 */
11197 err = i40e_aq_set_phy_int_mask(&pf->hw,
2f0aff41 11198 ~(I40E_AQ_EVENT_LINK_UPDOWN |
867a79e3 11199 I40E_AQ_EVENT_MEDIA_NA |
2f0aff41 11200 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
7e2453fe 11201 if (err)
f1c7e72e
SN
11202 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
11203 i40e_stat_str(&pf->hw, err),
11204 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7e2453fe 11205
4f2f017c
ASJ
11206 /* Reconfigure hardware for allowing smaller MSS in the case
11207 * of TSO, so that we avoid the MDD being fired and causing
11208 * a reset in the case of small MSS+TSO.
11209 */
11210 val = rd32(hw, I40E_REG_MSS);
11211 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11212 val &= ~I40E_REG_MSS_MIN_MASK;
11213 val |= I40E_64BYTE_MSS;
11214 wr32(hw, I40E_REG_MSS, val);
11215 }
11216
8eed76fa 11217 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
025b4a54
ASJ
11218 msleep(75);
11219 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11220 if (err)
f1c7e72e
SN
11221 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11222 i40e_stat_str(&pf->hw, err),
11223 i40e_aq_str(&pf->hw,
11224 pf->hw.aq.asq_last_status));
cafa2ee6 11225 }
41c445ff
JB
11226 /* The main driver is (mostly) up and happy. We need to set this state
11227 * before setting up the misc vector or we get a race and the vector
11228 * ends up disabled forever.
11229 */
11230 clear_bit(__I40E_DOWN, &pf->state);
11231
11232 /* In case of MSIX we are going to setup the misc vector right here
11233 * to handle admin queue events etc. In case of legacy and MSI
11234 * the misc functionality and queue processing is combined in
11235 * the same vector and that gets setup at open.
11236 */
11237 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11238 err = i40e_setup_misc_vector(pf);
11239 if (err) {
11240 dev_info(&pdev->dev,
11241 "setup of misc vector failed: %d\n", err);
11242 goto err_vsis;
11243 }
11244 }
11245
df805f62 11246#ifdef CONFIG_PCI_IOV
41c445ff
JB
11247 /* prep for VF support */
11248 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
4eb3f768
SN
11249 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11250 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
41c445ff
JB
11251 /* disable link interrupts for VFs */
11252 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
11253 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
11254 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
11255 i40e_flush(hw);
4aeec010
MW
11256
11257 if (pci_num_vf(pdev)) {
11258 dev_info(&pdev->dev,
11259 "Active VFs found, allocating resources.\n");
11260 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
11261 if (err)
11262 dev_info(&pdev->dev,
11263 "Error %d allocating resources for existing VFs\n",
11264 err);
11265 }
41c445ff 11266 }
df805f62 11267#endif /* CONFIG_PCI_IOV */
41c445ff 11268
e3219ce6
ASJ
11269 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11270 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
11271 pf->num_iwarp_msix,
11272 I40E_IWARP_IRQ_PILE_ID);
11273 if (pf->iwarp_base_vector < 0) {
11274 dev_info(&pdev->dev,
11275 "failed to get tracking for %d vectors for IWARP err=%d\n",
11276 pf->num_iwarp_msix, pf->iwarp_base_vector);
11277 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11278 }
11279 }
93cd765b 11280
41c445ff
JB
11281 i40e_dbg_pf_init(pf);
11282
11283 /* tell the firmware that we're starting */
44033fac 11284 i40e_send_version(pf);
41c445ff
JB
11285
11286 /* since everything's happy, start the service_task timer */
11287 mod_timer(&pf->service_timer,
11288 round_jiffies(jiffies + pf->service_timer_period));
11289
e3219ce6
ASJ
11290 /* add this PF to client device list and launch a client service task */
11291 err = i40e_lan_add_device(pf);
11292 if (err)
11293 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
11294 err);
11295
38e00438
VD
11296#ifdef I40E_FCOE
11297 /* create FCoE interface */
11298 i40e_fcoe_vsi_setup(pf);
11299
11300#endif
3fced535
ASJ
11301#define PCI_SPEED_SIZE 8
11302#define PCI_WIDTH_SIZE 8
11303 /* Devices on the IOSF bus do not have this information
11304 * and will report PCI Gen 1 x 1 by default so don't bother
11305 * checking them.
11306 */
11307 if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
11308 char speed[PCI_SPEED_SIZE] = "Unknown";
11309 char width[PCI_WIDTH_SIZE] = "Unknown";
11310
11311 /* Get the negotiated link width and speed from PCI config
11312 * space
11313 */
11314 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
11315 &link_status);
11316
11317 i40e_set_pci_config_data(hw, link_status);
11318
11319 switch (hw->bus.speed) {
11320 case i40e_bus_speed_8000:
11321 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
11322 case i40e_bus_speed_5000:
11323 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
11324 case i40e_bus_speed_2500:
11325 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
11326 default:
11327 break;
11328 }
11329 switch (hw->bus.width) {
11330 case i40e_bus_width_pcie_x8:
11331 strncpy(width, "8", PCI_WIDTH_SIZE); break;
11332 case i40e_bus_width_pcie_x4:
11333 strncpy(width, "4", PCI_WIDTH_SIZE); break;
11334 case i40e_bus_width_pcie_x2:
11335 strncpy(width, "2", PCI_WIDTH_SIZE); break;
11336 case i40e_bus_width_pcie_x1:
11337 strncpy(width, "1", PCI_WIDTH_SIZE); break;
11338 default:
11339 break;
11340 }
11341
11342 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
11343 speed, width);
11344
11345 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
11346 hw->bus.speed < i40e_bus_speed_8000) {
11347 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
11348 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
11349 }
d4dfb81a
CS
11350 }
11351
e827845c
CS
11352 /* get the requested speeds from the fw */
11353 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
11354 if (err)
8279e495
NP
11355 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
11356 i40e_stat_str(&pf->hw, err),
11357 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
e827845c
CS
11358 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
11359
fc72dbce
CS
11360 /* get the supported phy types from the fw */
11361 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
11362 if (err)
11363 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
11364 i40e_stat_str(&pf->hw, err),
11365 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
fc72dbce 11366
e7358f54
ASJ
11367 /* Add a filter to drop all Flow control frames from any VSI from being
11368 * transmitted. By doing so we stop a malicious VF from sending out
11369 * PAUSE or PFC frames and potentially controlling traffic for other
11370 * PF/VF VSIs.
11371 * The FW can still send Flow control frames if enabled.
11372 */
11373 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11374 pf->main_vsi_seid);
11375
31b606d0 11376 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
4f9b4307
HT
11377 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
11378 pf->flags |= I40E_FLAG_PHY_CONTROLS_LEDS;
4ad9f4f9
HR
11379 if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
11380 pf->flags |= I40E_FLAG_HAVE_CRT_RETIMER;
0c22b3dd
JB
11381 /* print a string summarizing features */
11382 i40e_print_features(pf);
11383
41c445ff
JB
11384 return 0;
11385
11386 /* Unwind what we've done if something failed in the setup */
11387err_vsis:
11388 set_bit(__I40E_DOWN, &pf->state);
41c445ff
JB
11389 i40e_clear_interrupt_scheme(pf);
11390 kfree(pf->vsi);
04b03013
SN
11391err_switch_setup:
11392 i40e_reset_interrupt_capability(pf);
41c445ff
JB
11393 del_timer_sync(&pf->service_timer);
11394err_mac_addr:
11395err_configure_lan_hmc:
11396 (void)i40e_shutdown_lan_hmc(hw);
11397err_init_lan_hmc:
11398 kfree(pf->qp_pile);
41c445ff
JB
11399err_sw_init:
11400err_adminq_setup:
41c445ff
JB
11401err_pf_reset:
11402 iounmap(hw->hw_addr);
11403err_ioremap:
11404 kfree(pf);
11405err_pf_alloc:
11406 pci_disable_pcie_error_reporting(pdev);
56d766d6 11407 pci_release_mem_regions(pdev);
41c445ff
JB
11408err_pci_reg:
11409err_dma:
11410 pci_disable_device(pdev);
11411 return err;
11412}
11413
11414/**
11415 * i40e_remove - Device removal routine
11416 * @pdev: PCI device information struct
11417 *
11418 * i40e_remove is called by the PCI subsystem to alert the driver
11419 * that is should release a PCI device. This could be caused by a
11420 * Hot-Plug event, or because the driver is going to be removed from
11421 * memory.
11422 **/
11423static void i40e_remove(struct pci_dev *pdev)
11424{
11425 struct i40e_pf *pf = pci_get_drvdata(pdev);
bcab2db9 11426 struct i40e_hw *hw = &pf->hw;
41c445ff 11427 i40e_status ret_code;
41c445ff
JB
11428 int i;
11429
11430 i40e_dbg_pf_exit(pf);
11431
beb0dff1
JK
11432 i40e_ptp_stop(pf);
11433
bcab2db9 11434 /* Disable RSS in hw */
272cdaf2
SN
11435 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
11436 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
bcab2db9 11437
41c445ff 11438 /* no more scheduling of any task */
a4618ec8 11439 set_bit(__I40E_SUSPENDED, &pf->state);
41c445ff 11440 set_bit(__I40E_DOWN, &pf->state);
c99abb4c
SN
11441 if (pf->service_timer.data)
11442 del_timer_sync(&pf->service_timer);
11443 if (pf->service_task.func)
11444 cancel_work_sync(&pf->service_task);
41c445ff 11445
eb2d80bc
MW
11446 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
11447 i40e_free_vfs(pf);
11448 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
11449 }
11450
41c445ff
JB
11451 i40e_fdir_teardown(pf);
11452
11453 /* If there is a switch structure or any orphans, remove them.
11454 * This will leave only the PF's VSI remaining.
11455 */
11456 for (i = 0; i < I40E_MAX_VEB; i++) {
11457 if (!pf->veb[i])
11458 continue;
11459
11460 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
11461 pf->veb[i]->uplink_seid == 0)
11462 i40e_switch_branch_release(pf->veb[i]);
11463 }
11464
11465 /* Now we can shutdown the PF's VSI, just before we kill
11466 * adminq and hmc.
11467 */
11468 if (pf->vsi[pf->lan_vsi])
11469 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
11470
e3219ce6
ASJ
11471 /* remove attached clients */
11472 ret_code = i40e_lan_del_device(pf);
11473 if (ret_code) {
11474 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
11475 ret_code);
11476 }
11477
41c445ff 11478 /* shutdown and destroy the HMC */
f734dfff
JB
11479 if (hw->hmc.hmc_obj) {
11480 ret_code = i40e_shutdown_lan_hmc(hw);
60442dea
SN
11481 if (ret_code)
11482 dev_warn(&pdev->dev,
11483 "Failed to destroy the HMC resources: %d\n",
11484 ret_code);
11485 }
41c445ff
JB
11486
11487 /* shutdown the adminq */
ac9c5c6d 11488 i40e_shutdown_adminq(hw);
41c445ff 11489
8ddb3326
JB
11490 /* destroy the locks only once, here */
11491 mutex_destroy(&hw->aq.arq_mutex);
11492 mutex_destroy(&hw->aq.asq_mutex);
11493
41c445ff
JB
11494 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
11495 i40e_clear_interrupt_scheme(pf);
505682cd 11496 for (i = 0; i < pf->num_alloc_vsi; i++) {
41c445ff
JB
11497 if (pf->vsi[i]) {
11498 i40e_vsi_clear_rings(pf->vsi[i]);
11499 i40e_vsi_clear(pf->vsi[i]);
11500 pf->vsi[i] = NULL;
11501 }
11502 }
11503
11504 for (i = 0; i < I40E_MAX_VEB; i++) {
11505 kfree(pf->veb[i]);
11506 pf->veb[i] = NULL;
11507 }
11508
11509 kfree(pf->qp_pile);
41c445ff
JB
11510 kfree(pf->vsi);
11511
f734dfff 11512 iounmap(hw->hw_addr);
41c445ff 11513 kfree(pf);
56d766d6 11514 pci_release_mem_regions(pdev);
41c445ff
JB
11515
11516 pci_disable_pcie_error_reporting(pdev);
11517 pci_disable_device(pdev);
11518}
11519
11520/**
11521 * i40e_pci_error_detected - warning that something funky happened in PCI land
11522 * @pdev: PCI device information struct
11523 *
11524 * Called to warn that something happened and the error handling steps
11525 * are in progress. Allows the driver to quiesce things, be ready for
11526 * remediation.
11527 **/
11528static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
11529 enum pci_channel_state error)
11530{
11531 struct i40e_pf *pf = pci_get_drvdata(pdev);
11532
11533 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
11534
edfc23ee
GP
11535 if (!pf) {
11536 dev_info(&pdev->dev,
11537 "Cannot recover - error happened during device probe\n");
11538 return PCI_ERS_RESULT_DISCONNECT;
11539 }
11540
41c445ff 11541 /* shutdown all operations */
9007bccd
SN
11542 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
11543 rtnl_lock();
11544 i40e_prep_for_reset(pf);
11545 rtnl_unlock();
11546 }
41c445ff
JB
11547
11548 /* Request a slot reset */
11549 return PCI_ERS_RESULT_NEED_RESET;
11550}
11551
11552/**
11553 * i40e_pci_error_slot_reset - a PCI slot reset just happened
11554 * @pdev: PCI device information struct
11555 *
11556 * Called to find if the driver can work with the device now that
11557 * the pci slot has been reset. If a basic connection seems good
11558 * (registers are readable and have sane content) then return a
11559 * happy little PCI_ERS_RESULT_xxx.
11560 **/
11561static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
11562{
11563 struct i40e_pf *pf = pci_get_drvdata(pdev);
11564 pci_ers_result_t result;
11565 int err;
11566 u32 reg;
11567
fb43201f 11568 dev_dbg(&pdev->dev, "%s\n", __func__);
41c445ff
JB
11569 if (pci_enable_device_mem(pdev)) {
11570 dev_info(&pdev->dev,
11571 "Cannot re-enable PCI device after reset.\n");
11572 result = PCI_ERS_RESULT_DISCONNECT;
11573 } else {
11574 pci_set_master(pdev);
11575 pci_restore_state(pdev);
11576 pci_save_state(pdev);
11577 pci_wake_from_d3(pdev, false);
11578
11579 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
11580 if (reg == 0)
11581 result = PCI_ERS_RESULT_RECOVERED;
11582 else
11583 result = PCI_ERS_RESULT_DISCONNECT;
11584 }
11585
11586 err = pci_cleanup_aer_uncorrect_error_status(pdev);
11587 if (err) {
11588 dev_info(&pdev->dev,
11589 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
11590 err);
11591 /* non-fatal, continue */
11592 }
11593
11594 return result;
11595}
11596
11597/**
11598 * i40e_pci_error_resume - restart operations after PCI error recovery
11599 * @pdev: PCI device information struct
11600 *
11601 * Called to allow the driver to bring things back up after PCI error
11602 * and/or reset recovery has finished.
11603 **/
11604static void i40e_pci_error_resume(struct pci_dev *pdev)
11605{
11606 struct i40e_pf *pf = pci_get_drvdata(pdev);
11607
fb43201f 11608 dev_dbg(&pdev->dev, "%s\n", __func__);
9007bccd
SN
11609 if (test_bit(__I40E_SUSPENDED, &pf->state))
11610 return;
11611
11612 rtnl_lock();
41c445ff 11613 i40e_handle_reset_warning(pf);
4c4935a9 11614 rtnl_unlock();
9007bccd
SN
11615}
11616
11617/**
11618 * i40e_shutdown - PCI callback for shutting down
11619 * @pdev: PCI device information struct
11620 **/
11621static void i40e_shutdown(struct pci_dev *pdev)
11622{
11623 struct i40e_pf *pf = pci_get_drvdata(pdev);
8e2773ae 11624 struct i40e_hw *hw = &pf->hw;
9007bccd
SN
11625
11626 set_bit(__I40E_SUSPENDED, &pf->state);
11627 set_bit(__I40E_DOWN, &pf->state);
11628 rtnl_lock();
11629 i40e_prep_for_reset(pf);
11630 rtnl_unlock();
11631
8e2773ae
SN
11632 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11633 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11634
02b42498
CS
11635 del_timer_sync(&pf->service_timer);
11636 cancel_work_sync(&pf->service_task);
11637 i40e_fdir_teardown(pf);
11638
11639 rtnl_lock();
11640 i40e_prep_for_reset(pf);
11641 rtnl_unlock();
11642
11643 wr32(hw, I40E_PFPM_APM,
11644 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11645 wr32(hw, I40E_PFPM_WUFC,
11646 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11647
e147758d
SN
11648 i40e_clear_interrupt_scheme(pf);
11649
9007bccd 11650 if (system_state == SYSTEM_POWER_OFF) {
8e2773ae 11651 pci_wake_from_d3(pdev, pf->wol_en);
9007bccd
SN
11652 pci_set_power_state(pdev, PCI_D3hot);
11653 }
11654}
11655
11656#ifdef CONFIG_PM
11657/**
11658 * i40e_suspend - PCI callback for moving to D3
11659 * @pdev: PCI device information struct
11660 **/
11661static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
11662{
11663 struct i40e_pf *pf = pci_get_drvdata(pdev);
8e2773ae 11664 struct i40e_hw *hw = &pf->hw;
059ff69b 11665 int retval = 0;
9007bccd
SN
11666
11667 set_bit(__I40E_SUSPENDED, &pf->state);
11668 set_bit(__I40E_DOWN, &pf->state);
3932dbfe 11669
9007bccd
SN
11670 rtnl_lock();
11671 i40e_prep_for_reset(pf);
11672 rtnl_unlock();
11673
8e2773ae
SN
11674 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11675 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11676
b33d3b73
GR
11677 i40e_stop_misc_vector(pf);
11678
059ff69b
GR
11679 retval = pci_save_state(pdev);
11680 if (retval)
11681 return retval;
11682
8e2773ae 11683 pci_wake_from_d3(pdev, pf->wol_en);
9007bccd
SN
11684 pci_set_power_state(pdev, PCI_D3hot);
11685
059ff69b 11686 return retval;
41c445ff
JB
11687}
11688
9007bccd
SN
11689/**
11690 * i40e_resume - PCI callback for waking up from D3
11691 * @pdev: PCI device information struct
11692 **/
11693static int i40e_resume(struct pci_dev *pdev)
11694{
11695 struct i40e_pf *pf = pci_get_drvdata(pdev);
11696 u32 err;
11697
11698 pci_set_power_state(pdev, PCI_D0);
11699 pci_restore_state(pdev);
11700 /* pci_restore_state() clears dev->state_saves, so
11701 * call pci_save_state() again to restore it.
11702 */
11703 pci_save_state(pdev);
11704
11705 err = pci_enable_device_mem(pdev);
11706 if (err) {
fb43201f 11707 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
9007bccd
SN
11708 return err;
11709 }
11710 pci_set_master(pdev);
11711
11712 /* no wakeup events while running */
11713 pci_wake_from_d3(pdev, false);
11714
11715 /* handling the reset will rebuild the device state */
11716 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
11717 clear_bit(__I40E_DOWN, &pf->state);
11718 rtnl_lock();
11719 i40e_reset_and_rebuild(pf, false);
11720 rtnl_unlock();
11721 }
11722
11723 return 0;
11724}
11725
11726#endif
41c445ff
JB
11727static const struct pci_error_handlers i40e_err_handler = {
11728 .error_detected = i40e_pci_error_detected,
11729 .slot_reset = i40e_pci_error_slot_reset,
11730 .resume = i40e_pci_error_resume,
11731};
11732
11733static struct pci_driver i40e_driver = {
11734 .name = i40e_driver_name,
11735 .id_table = i40e_pci_tbl,
11736 .probe = i40e_probe,
11737 .remove = i40e_remove,
9007bccd
SN
11738#ifdef CONFIG_PM
11739 .suspend = i40e_suspend,
11740 .resume = i40e_resume,
11741#endif
11742 .shutdown = i40e_shutdown,
41c445ff
JB
11743 .err_handler = &i40e_err_handler,
11744 .sriov_configure = i40e_pci_sriov_configure,
11745};
11746
11747/**
11748 * i40e_init_module - Driver registration routine
11749 *
11750 * i40e_init_module is the first routine called when the driver is
11751 * loaded. All it does is register with the PCI subsystem.
11752 **/
11753static int __init i40e_init_module(void)
11754{
11755 pr_info("%s: %s - version %s\n", i40e_driver_name,
11756 i40e_driver_string, i40e_driver_version_str);
11757 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
96664483 11758
2803b16c
JB
11759 /* we will see if single thread per module is enough for now,
11760 * it can't be any worse than using the system workqueue which
11761 * was already single threaded
11762 */
6992a6c9
JK
11763 i40e_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
11764 i40e_driver_name);
2803b16c
JB
11765 if (!i40e_wq) {
11766 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
11767 return -ENOMEM;
11768 }
11769
41c445ff
JB
11770 i40e_dbg_init();
11771 return pci_register_driver(&i40e_driver);
11772}
11773module_init(i40e_init_module);
11774
11775/**
11776 * i40e_exit_module - Driver exit cleanup routine
11777 *
11778 * i40e_exit_module is called just before the driver is removed
11779 * from memory.
11780 **/
11781static void __exit i40e_exit_module(void)
11782{
11783 pci_unregister_driver(&i40e_driver);
2803b16c 11784 destroy_workqueue(i40e_wq);
41c445ff
JB
11785 i40e_dbg_exit();
11786}
11787module_exit(i40e_exit_module);