]> git.ipfire.org Git - people/arne_f/kernel.git/blob - drivers/net/ethernet/intel/i40e/i40e_main.c
i40e: report correct statistics when XDP is enabled
[people/arne_f/kernel.git] / drivers / net / ethernet / intel / i40e / i40e_main.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2018 Intel Corporation. */
3
4 #include <linux/etherdevice.h>
5 #include <linux/of_net.h>
6 #include <linux/pci.h>
7 #include <linux/bpf.h>
8
9 /* Local includes */
10 #include "i40e.h"
11 #include "i40e_diag.h"
12 #include <net/udp_tunnel.h>
13 /* All i40e tracepoints are defined by the include below, which
14 * must be included exactly once across the whole kernel with
15 * CREATE_TRACE_POINTS defined
16 */
17 #define CREATE_TRACE_POINTS
18 #include "i40e_trace.h"
19
20 const char i40e_driver_name[] = "i40e";
21 static const char i40e_driver_string[] =
22 "Intel(R) Ethernet Connection XL710 Network Driver";
23
24 #define DRV_KERN "-k"
25
26 #define DRV_VERSION_MAJOR 2
27 #define DRV_VERSION_MINOR 3
28 #define DRV_VERSION_BUILD 2
29 #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
30 __stringify(DRV_VERSION_MINOR) "." \
31 __stringify(DRV_VERSION_BUILD) DRV_KERN
32 const char i40e_driver_version_str[] = DRV_VERSION;
33 static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
34
35 /* a bit of forward declarations */
36 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
37 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
38 static int i40e_add_vsi(struct i40e_vsi *vsi);
39 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
40 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
41 static int i40e_setup_misc_vector(struct i40e_pf *pf);
42 static void i40e_determine_queue_usage(struct i40e_pf *pf);
43 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
44 static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired);
45 static int i40e_reset(struct i40e_pf *pf);
46 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
47 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
48 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
49 static int i40e_get_capabilities(struct i40e_pf *pf,
50 enum i40e_admin_queue_opc list_type);
51
52
53 /* i40e_pci_tbl - PCI Device ID Table
54 *
55 * Last entry must be all 0s
56 *
57 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
58 * Class, Class Mask, private data (not used) }
59 */
60 static const struct pci_device_id i40e_pci_tbl[] = {
61 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
62 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
63 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
64 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
65 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
66 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
67 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
68 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
69 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
70 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
71 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
72 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
73 {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
74 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
75 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
76 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
77 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
78 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
79 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
80 /* required last entry */
81 {0, }
82 };
83 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
84
85 #define I40E_MAX_VF_COUNT 128
86 static int debug = -1;
87 module_param(debug, uint, 0);
88 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
89
90 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
91 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
92 MODULE_LICENSE("GPL");
93 MODULE_VERSION(DRV_VERSION);
94
95 static struct workqueue_struct *i40e_wq;
96
97 /**
98 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
99 * @hw: pointer to the HW structure
100 * @mem: ptr to mem struct to fill out
101 * @size: size of memory requested
102 * @alignment: what to align the allocation to
103 **/
104 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
105 u64 size, u32 alignment)
106 {
107 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
108
109 mem->size = ALIGN(size, alignment);
110 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
111 &mem->pa, GFP_KERNEL);
112 if (!mem->va)
113 return -ENOMEM;
114
115 return 0;
116 }
117
118 /**
119 * i40e_free_dma_mem_d - OS specific memory free for shared code
120 * @hw: pointer to the HW structure
121 * @mem: ptr to mem struct to free
122 **/
123 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
124 {
125 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
126
127 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
128 mem->va = NULL;
129 mem->pa = 0;
130 mem->size = 0;
131
132 return 0;
133 }
134
135 /**
136 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
137 * @hw: pointer to the HW structure
138 * @mem: ptr to mem struct to fill out
139 * @size: size of memory requested
140 **/
141 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
142 u32 size)
143 {
144 mem->size = size;
145 mem->va = kzalloc(size, GFP_KERNEL);
146
147 if (!mem->va)
148 return -ENOMEM;
149
150 return 0;
151 }
152
153 /**
154 * i40e_free_virt_mem_d - OS specific memory free for shared code
155 * @hw: pointer to the HW structure
156 * @mem: ptr to mem struct to free
157 **/
158 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
159 {
160 /* it's ok to kfree a NULL pointer */
161 kfree(mem->va);
162 mem->va = NULL;
163 mem->size = 0;
164
165 return 0;
166 }
167
168 /**
169 * i40e_get_lump - find a lump of free generic resource
170 * @pf: board private structure
171 * @pile: the pile of resource to search
172 * @needed: the number of items needed
173 * @id: an owner id to stick on the items assigned
174 *
175 * Returns the base item index of the lump, or negative for error
176 *
177 * The search_hint trick and lack of advanced fit-finding only work
178 * because we're highly likely to have all the same size lump requests.
179 * Linear search time and any fragmentation should be minimal.
180 **/
181 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
182 u16 needed, u16 id)
183 {
184 int ret = -ENOMEM;
185 int i, j;
186
187 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
188 dev_info(&pf->pdev->dev,
189 "param err: pile=%s needed=%d id=0x%04x\n",
190 pile ? "<valid>" : "<null>", needed, id);
191 return -EINVAL;
192 }
193
194 /* start the linear search with an imperfect hint */
195 i = pile->search_hint;
196 while (i < pile->num_entries) {
197 /* skip already allocated entries */
198 if (pile->list[i] & I40E_PILE_VALID_BIT) {
199 i++;
200 continue;
201 }
202
203 /* do we have enough in this lump? */
204 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
205 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
206 break;
207 }
208
209 if (j == needed) {
210 /* there was enough, so assign it to the requestor */
211 for (j = 0; j < needed; j++)
212 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
213 ret = i;
214 pile->search_hint = i + j;
215 break;
216 }
217
218 /* not enough, so skip over it and continue looking */
219 i += j;
220 }
221
222 return ret;
223 }
224
225 /**
226 * i40e_put_lump - return a lump of generic resource
227 * @pile: the pile of resource to search
228 * @index: the base item index
229 * @id: the owner id of the items assigned
230 *
231 * Returns the count of items in the lump
232 **/
233 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
234 {
235 int valid_id = (id | I40E_PILE_VALID_BIT);
236 int count = 0;
237 int i;
238
239 if (!pile || index >= pile->num_entries)
240 return -EINVAL;
241
242 for (i = index;
243 i < pile->num_entries && pile->list[i] == valid_id;
244 i++) {
245 pile->list[i] = 0;
246 count++;
247 }
248
249 if (count && index < pile->search_hint)
250 pile->search_hint = index;
251
252 return count;
253 }
254
255 /**
256 * i40e_find_vsi_from_id - searches for the vsi with the given id
257 * @pf: the pf structure to search for the vsi
258 * @id: id of the vsi it is searching for
259 **/
260 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
261 {
262 int i;
263
264 for (i = 0; i < pf->num_alloc_vsi; i++)
265 if (pf->vsi[i] && (pf->vsi[i]->id == id))
266 return pf->vsi[i];
267
268 return NULL;
269 }
270
271 /**
272 * i40e_service_event_schedule - Schedule the service task to wake up
273 * @pf: board private structure
274 *
275 * If not already scheduled, this puts the task into the work queue
276 **/
277 void i40e_service_event_schedule(struct i40e_pf *pf)
278 {
279 if (!test_bit(__I40E_DOWN, pf->state) &&
280 !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
281 queue_work(i40e_wq, &pf->service_task);
282 }
283
284 /**
285 * i40e_tx_timeout - Respond to a Tx Hang
286 * @netdev: network interface device structure
287 *
288 * If any port has noticed a Tx timeout, it is likely that the whole
289 * device is munged, not just the one netdev port, so go for the full
290 * reset.
291 **/
292 static void i40e_tx_timeout(struct net_device *netdev)
293 {
294 struct i40e_netdev_priv *np = netdev_priv(netdev);
295 struct i40e_vsi *vsi = np->vsi;
296 struct i40e_pf *pf = vsi->back;
297 struct i40e_ring *tx_ring = NULL;
298 unsigned int i, hung_queue = 0;
299 u32 head, val;
300
301 pf->tx_timeout_count++;
302
303 /* find the stopped queue the same way the stack does */
304 for (i = 0; i < netdev->num_tx_queues; i++) {
305 struct netdev_queue *q;
306 unsigned long trans_start;
307
308 q = netdev_get_tx_queue(netdev, i);
309 trans_start = q->trans_start;
310 if (netif_xmit_stopped(q) &&
311 time_after(jiffies,
312 (trans_start + netdev->watchdog_timeo))) {
313 hung_queue = i;
314 break;
315 }
316 }
317
318 if (i == netdev->num_tx_queues) {
319 netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
320 } else {
321 /* now that we have an index, find the tx_ring struct */
322 for (i = 0; i < vsi->num_queue_pairs; i++) {
323 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
324 if (hung_queue ==
325 vsi->tx_rings[i]->queue_index) {
326 tx_ring = vsi->tx_rings[i];
327 break;
328 }
329 }
330 }
331 }
332
333 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
334 pf->tx_timeout_recovery_level = 1; /* reset after some time */
335 else if (time_before(jiffies,
336 (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
337 return; /* don't do any new action before the next timeout */
338
339 /* don't kick off another recovery if one is already pending */
340 if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
341 return;
342
343 if (tx_ring) {
344 head = i40e_get_head(tx_ring);
345 /* Read interrupt register */
346 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
347 val = rd32(&pf->hw,
348 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
349 tx_ring->vsi->base_vector - 1));
350 else
351 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
352
353 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",
354 vsi->seid, hung_queue, tx_ring->next_to_clean,
355 head, tx_ring->next_to_use,
356 readl(tx_ring->tail), val);
357 }
358
359 pf->tx_timeout_last_recovery = jiffies;
360 netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
361 pf->tx_timeout_recovery_level, hung_queue);
362
363 switch (pf->tx_timeout_recovery_level) {
364 case 1:
365 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
366 break;
367 case 2:
368 set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
369 break;
370 case 3:
371 set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
372 break;
373 default:
374 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
375 break;
376 }
377
378 i40e_service_event_schedule(pf);
379 pf->tx_timeout_recovery_level++;
380 }
381
382 /**
383 * i40e_get_vsi_stats_struct - Get System Network Statistics
384 * @vsi: the VSI we care about
385 *
386 * Returns the address of the device statistics structure.
387 * The statistics are actually updated from the service task.
388 **/
389 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
390 {
391 return &vsi->net_stats;
392 }
393
394 /**
395 * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
396 * @ring: Tx ring to get statistics from
397 * @stats: statistics entry to be updated
398 **/
399 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
400 struct rtnl_link_stats64 *stats)
401 {
402 u64 bytes, packets;
403 unsigned int start;
404
405 do {
406 start = u64_stats_fetch_begin_irq(&ring->syncp);
407 packets = ring->stats.packets;
408 bytes = ring->stats.bytes;
409 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
410
411 stats->tx_packets += packets;
412 stats->tx_bytes += bytes;
413 }
414
415 /**
416 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
417 * @netdev: network interface device structure
418 * @stats: data structure to store statistics
419 *
420 * Returns the address of the device statistics structure.
421 * The statistics are actually updated from the service task.
422 **/
423 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
424 struct rtnl_link_stats64 *stats)
425 {
426 struct i40e_netdev_priv *np = netdev_priv(netdev);
427 struct i40e_vsi *vsi = np->vsi;
428 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
429 struct i40e_ring *ring;
430 int i;
431
432 if (test_bit(__I40E_VSI_DOWN, vsi->state))
433 return;
434
435 if (!vsi->tx_rings)
436 return;
437
438 rcu_read_lock();
439 for (i = 0; i < vsi->num_queue_pairs; i++) {
440 u64 bytes, packets;
441 unsigned int start;
442
443 ring = READ_ONCE(vsi->tx_rings[i]);
444 if (!ring)
445 continue;
446 i40e_get_netdev_stats_struct_tx(ring, stats);
447
448 if (i40e_enabled_xdp_vsi(vsi)) {
449 ring++;
450 i40e_get_netdev_stats_struct_tx(ring, stats);
451 }
452
453 ring++;
454 do {
455 start = u64_stats_fetch_begin_irq(&ring->syncp);
456 packets = ring->stats.packets;
457 bytes = ring->stats.bytes;
458 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
459
460 stats->rx_packets += packets;
461 stats->rx_bytes += bytes;
462
463 }
464 rcu_read_unlock();
465
466 /* following stats updated by i40e_watchdog_subtask() */
467 stats->multicast = vsi_stats->multicast;
468 stats->tx_errors = vsi_stats->tx_errors;
469 stats->tx_dropped = vsi_stats->tx_dropped;
470 stats->rx_errors = vsi_stats->rx_errors;
471 stats->rx_dropped = vsi_stats->rx_dropped;
472 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
473 stats->rx_length_errors = vsi_stats->rx_length_errors;
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 **/
480 void 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));
493 if (vsi->rx_rings && vsi->rx_rings[0]) {
494 for (i = 0; i < vsi->num_queue_pairs; i++) {
495 memset(&vsi->rx_rings[i]->stats, 0,
496 sizeof(vsi->rx_rings[i]->stats));
497 memset(&vsi->rx_rings[i]->rx_stats, 0,
498 sizeof(vsi->rx_rings[i]->rx_stats));
499 memset(&vsi->tx_rings[i]->stats, 0,
500 sizeof(vsi->tx_rings[i]->stats));
501 memset(&vsi->tx_rings[i]->tx_stats, 0,
502 sizeof(vsi->tx_rings[i]->tx_stats));
503 }
504 }
505 vsi->stat_offsets_loaded = false;
506 }
507
508 /**
509 * i40e_pf_reset_stats - Reset all of the stats for the given PF
510 * @pf: the PF to be reset
511 **/
512 void i40e_pf_reset_stats(struct i40e_pf *pf)
513 {
514 int i;
515
516 memset(&pf->stats, 0, sizeof(pf->stats));
517 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
518 pf->stat_offsets_loaded = false;
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 }
529 pf->hw_csum_rx_error = 0;
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 **/
547 static 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
552 if (hw->device_id == I40E_DEV_ID_QEMU) {
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
563 *stat = (new_data + BIT_ULL(48)) - *offset;
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 **/
575 static 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
586 *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
587 }
588
589 /**
590 * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
591 * @hw: ptr to the hardware info
592 * @reg: the hw reg to read and clear
593 * @stat: ptr to the stat
594 **/
595 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
596 {
597 u32 new_data = rd32(hw, reg);
598
599 wr32(hw, reg, 1); /* must write a nonzero value to clear register */
600 *stat += new_data;
601 }
602
603 /**
604 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
605 * @vsi: the VSI to be updated
606 **/
607 void i40e_update_eth_stats(struct i40e_vsi *vsi)
608 {
609 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
610 struct i40e_pf *pf = vsi->back;
611 struct i40e_hw *hw = &pf->hw;
612 struct i40e_eth_stats *oes;
613 struct i40e_eth_stats *es; /* device's eth stats */
614
615 es = &vsi->eth_stats;
616 oes = &vsi->eth_stats_offsets;
617
618 /* Gather up the stats that the hw collects */
619 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
620 vsi->stat_offsets_loaded,
621 &oes->tx_errors, &es->tx_errors);
622 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
623 vsi->stat_offsets_loaded,
624 &oes->rx_discards, &es->rx_discards);
625 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
626 vsi->stat_offsets_loaded,
627 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
628 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
629 vsi->stat_offsets_loaded,
630 &oes->tx_errors, &es->tx_errors);
631
632 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
633 I40E_GLV_GORCL(stat_idx),
634 vsi->stat_offsets_loaded,
635 &oes->rx_bytes, &es->rx_bytes);
636 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
637 I40E_GLV_UPRCL(stat_idx),
638 vsi->stat_offsets_loaded,
639 &oes->rx_unicast, &es->rx_unicast);
640 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
641 I40E_GLV_MPRCL(stat_idx),
642 vsi->stat_offsets_loaded,
643 &oes->rx_multicast, &es->rx_multicast);
644 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
645 I40E_GLV_BPRCL(stat_idx),
646 vsi->stat_offsets_loaded,
647 &oes->rx_broadcast, &es->rx_broadcast);
648
649 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
650 I40E_GLV_GOTCL(stat_idx),
651 vsi->stat_offsets_loaded,
652 &oes->tx_bytes, &es->tx_bytes);
653 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
654 I40E_GLV_UPTCL(stat_idx),
655 vsi->stat_offsets_loaded,
656 &oes->tx_unicast, &es->tx_unicast);
657 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
658 I40E_GLV_MPTCL(stat_idx),
659 vsi->stat_offsets_loaded,
660 &oes->tx_multicast, &es->tx_multicast);
661 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
662 I40E_GLV_BPTCL(stat_idx),
663 vsi->stat_offsets_loaded,
664 &oes->tx_broadcast, &es->tx_broadcast);
665 vsi->stat_offsets_loaded = true;
666 }
667
668 /**
669 * i40e_update_veb_stats - Update Switch component statistics
670 * @veb: the VEB being updated
671 **/
672 static void i40e_update_veb_stats(struct i40e_veb *veb)
673 {
674 struct i40e_pf *pf = veb->pf;
675 struct i40e_hw *hw = &pf->hw;
676 struct i40e_eth_stats *oes;
677 struct i40e_eth_stats *es; /* device's eth stats */
678 struct i40e_veb_tc_stats *veb_oes;
679 struct i40e_veb_tc_stats *veb_es;
680 int i, idx = 0;
681
682 idx = veb->stats_idx;
683 es = &veb->stats;
684 oes = &veb->stats_offsets;
685 veb_es = &veb->tc_stats;
686 veb_oes = &veb->tc_stats_offsets;
687
688 /* Gather up the stats that the hw collects */
689 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
690 veb->stat_offsets_loaded,
691 &oes->tx_discards, &es->tx_discards);
692 if (hw->revision_id > 0)
693 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
694 veb->stat_offsets_loaded,
695 &oes->rx_unknown_protocol,
696 &es->rx_unknown_protocol);
697 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
698 veb->stat_offsets_loaded,
699 &oes->rx_bytes, &es->rx_bytes);
700 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
701 veb->stat_offsets_loaded,
702 &oes->rx_unicast, &es->rx_unicast);
703 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
704 veb->stat_offsets_loaded,
705 &oes->rx_multicast, &es->rx_multicast);
706 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
707 veb->stat_offsets_loaded,
708 &oes->rx_broadcast, &es->rx_broadcast);
709
710 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
711 veb->stat_offsets_loaded,
712 &oes->tx_bytes, &es->tx_bytes);
713 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
714 veb->stat_offsets_loaded,
715 &oes->tx_unicast, &es->tx_unicast);
716 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
717 veb->stat_offsets_loaded,
718 &oes->tx_multicast, &es->tx_multicast);
719 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
720 veb->stat_offsets_loaded,
721 &oes->tx_broadcast, &es->tx_broadcast);
722 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
723 i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
724 I40E_GLVEBTC_RPCL(i, idx),
725 veb->stat_offsets_loaded,
726 &veb_oes->tc_rx_packets[i],
727 &veb_es->tc_rx_packets[i]);
728 i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
729 I40E_GLVEBTC_RBCL(i, idx),
730 veb->stat_offsets_loaded,
731 &veb_oes->tc_rx_bytes[i],
732 &veb_es->tc_rx_bytes[i]);
733 i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
734 I40E_GLVEBTC_TPCL(i, idx),
735 veb->stat_offsets_loaded,
736 &veb_oes->tc_tx_packets[i],
737 &veb_es->tc_tx_packets[i]);
738 i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
739 I40E_GLVEBTC_TBCL(i, idx),
740 veb->stat_offsets_loaded,
741 &veb_oes->tc_tx_bytes[i],
742 &veb_es->tc_tx_bytes[i]);
743 }
744 veb->stat_offsets_loaded = true;
745 }
746
747 /**
748 * i40e_update_vsi_stats - Update the vsi statistics counters.
749 * @vsi: the VSI to be updated
750 *
751 * There are a few instances where we store the same stat in a
752 * couple of different structs. This is partly because we have
753 * the netdev stats that need to be filled out, which is slightly
754 * different from the "eth_stats" defined by the chip and used in
755 * VF communications. We sort it out here.
756 **/
757 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
758 {
759 struct i40e_pf *pf = vsi->back;
760 struct rtnl_link_stats64 *ons;
761 struct rtnl_link_stats64 *ns; /* netdev stats */
762 struct i40e_eth_stats *oes;
763 struct i40e_eth_stats *es; /* device's eth stats */
764 u32 tx_restart, tx_busy;
765 struct i40e_ring *p;
766 u32 rx_page, rx_buf;
767 u64 bytes, packets;
768 unsigned int start;
769 u64 tx_linearize;
770 u64 tx_force_wb;
771 u64 rx_p, rx_b;
772 u64 tx_p, tx_b;
773 u16 q;
774
775 if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
776 test_bit(__I40E_CONFIG_BUSY, pf->state))
777 return;
778
779 ns = i40e_get_vsi_stats_struct(vsi);
780 ons = &vsi->net_stats_offsets;
781 es = &vsi->eth_stats;
782 oes = &vsi->eth_stats_offsets;
783
784 /* Gather up the netdev and vsi stats that the driver collects
785 * on the fly during packet processing
786 */
787 rx_b = rx_p = 0;
788 tx_b = tx_p = 0;
789 tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
790 rx_page = 0;
791 rx_buf = 0;
792 rcu_read_lock();
793 for (q = 0; q < vsi->num_queue_pairs; q++) {
794 /* locate Tx ring */
795 p = READ_ONCE(vsi->tx_rings[q]);
796
797 do {
798 start = u64_stats_fetch_begin_irq(&p->syncp);
799 packets = p->stats.packets;
800 bytes = p->stats.bytes;
801 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
802 tx_b += bytes;
803 tx_p += packets;
804 tx_restart += p->tx_stats.restart_queue;
805 tx_busy += p->tx_stats.tx_busy;
806 tx_linearize += p->tx_stats.tx_linearize;
807 tx_force_wb += p->tx_stats.tx_force_wb;
808
809 /* Rx queue is part of the same block as Tx queue */
810 p = &p[1];
811 do {
812 start = u64_stats_fetch_begin_irq(&p->syncp);
813 packets = p->stats.packets;
814 bytes = p->stats.bytes;
815 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
816 rx_b += bytes;
817 rx_p += packets;
818 rx_buf += p->rx_stats.alloc_buff_failed;
819 rx_page += p->rx_stats.alloc_page_failed;
820 }
821 rcu_read_unlock();
822 vsi->tx_restart = tx_restart;
823 vsi->tx_busy = tx_busy;
824 vsi->tx_linearize = tx_linearize;
825 vsi->tx_force_wb = tx_force_wb;
826 vsi->rx_page_failed = rx_page;
827 vsi->rx_buf_failed = rx_buf;
828
829 ns->rx_packets = rx_p;
830 ns->rx_bytes = rx_b;
831 ns->tx_packets = tx_p;
832 ns->tx_bytes = tx_b;
833
834 /* update netdev stats from eth stats */
835 i40e_update_eth_stats(vsi);
836 ons->tx_errors = oes->tx_errors;
837 ns->tx_errors = es->tx_errors;
838 ons->multicast = oes->rx_multicast;
839 ns->multicast = es->rx_multicast;
840 ons->rx_dropped = oes->rx_discards;
841 ns->rx_dropped = es->rx_discards;
842 ons->tx_dropped = oes->tx_discards;
843 ns->tx_dropped = es->tx_discards;
844
845 /* pull in a couple PF stats if this is the main vsi */
846 if (vsi == pf->vsi[pf->lan_vsi]) {
847 ns->rx_crc_errors = pf->stats.crc_errors;
848 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
849 ns->rx_length_errors = pf->stats.rx_length_errors;
850 }
851 }
852
853 /**
854 * i40e_update_pf_stats - Update the PF statistics counters.
855 * @pf: the PF to be updated
856 **/
857 static void i40e_update_pf_stats(struct i40e_pf *pf)
858 {
859 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
860 struct i40e_hw_port_stats *nsd = &pf->stats;
861 struct i40e_hw *hw = &pf->hw;
862 u32 val;
863 int i;
864
865 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
866 I40E_GLPRT_GORCL(hw->port),
867 pf->stat_offsets_loaded,
868 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
869 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
870 I40E_GLPRT_GOTCL(hw->port),
871 pf->stat_offsets_loaded,
872 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
873 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
874 pf->stat_offsets_loaded,
875 &osd->eth.rx_discards,
876 &nsd->eth.rx_discards);
877 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
878 I40E_GLPRT_UPRCL(hw->port),
879 pf->stat_offsets_loaded,
880 &osd->eth.rx_unicast,
881 &nsd->eth.rx_unicast);
882 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
883 I40E_GLPRT_MPRCL(hw->port),
884 pf->stat_offsets_loaded,
885 &osd->eth.rx_multicast,
886 &nsd->eth.rx_multicast);
887 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
888 I40E_GLPRT_BPRCL(hw->port),
889 pf->stat_offsets_loaded,
890 &osd->eth.rx_broadcast,
891 &nsd->eth.rx_broadcast);
892 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
893 I40E_GLPRT_UPTCL(hw->port),
894 pf->stat_offsets_loaded,
895 &osd->eth.tx_unicast,
896 &nsd->eth.tx_unicast);
897 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
898 I40E_GLPRT_MPTCL(hw->port),
899 pf->stat_offsets_loaded,
900 &osd->eth.tx_multicast,
901 &nsd->eth.tx_multicast);
902 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
903 I40E_GLPRT_BPTCL(hw->port),
904 pf->stat_offsets_loaded,
905 &osd->eth.tx_broadcast,
906 &nsd->eth.tx_broadcast);
907
908 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
909 pf->stat_offsets_loaded,
910 &osd->tx_dropped_link_down,
911 &nsd->tx_dropped_link_down);
912
913 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
914 pf->stat_offsets_loaded,
915 &osd->crc_errors, &nsd->crc_errors);
916
917 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
918 pf->stat_offsets_loaded,
919 &osd->illegal_bytes, &nsd->illegal_bytes);
920
921 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
922 pf->stat_offsets_loaded,
923 &osd->mac_local_faults,
924 &nsd->mac_local_faults);
925 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
926 pf->stat_offsets_loaded,
927 &osd->mac_remote_faults,
928 &nsd->mac_remote_faults);
929
930 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
931 pf->stat_offsets_loaded,
932 &osd->rx_length_errors,
933 &nsd->rx_length_errors);
934
935 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
936 pf->stat_offsets_loaded,
937 &osd->link_xon_rx, &nsd->link_xon_rx);
938 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
939 pf->stat_offsets_loaded,
940 &osd->link_xon_tx, &nsd->link_xon_tx);
941 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
942 pf->stat_offsets_loaded,
943 &osd->link_xoff_rx, &nsd->link_xoff_rx);
944 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
945 pf->stat_offsets_loaded,
946 &osd->link_xoff_tx, &nsd->link_xoff_tx);
947
948 for (i = 0; i < 8; i++) {
949 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
950 pf->stat_offsets_loaded,
951 &osd->priority_xoff_rx[i],
952 &nsd->priority_xoff_rx[i]);
953 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
954 pf->stat_offsets_loaded,
955 &osd->priority_xon_rx[i],
956 &nsd->priority_xon_rx[i]);
957 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
958 pf->stat_offsets_loaded,
959 &osd->priority_xon_tx[i],
960 &nsd->priority_xon_tx[i]);
961 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
962 pf->stat_offsets_loaded,
963 &osd->priority_xoff_tx[i],
964 &nsd->priority_xoff_tx[i]);
965 i40e_stat_update32(hw,
966 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
967 pf->stat_offsets_loaded,
968 &osd->priority_xon_2_xoff[i],
969 &nsd->priority_xon_2_xoff[i]);
970 }
971
972 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
973 I40E_GLPRT_PRC64L(hw->port),
974 pf->stat_offsets_loaded,
975 &osd->rx_size_64, &nsd->rx_size_64);
976 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
977 I40E_GLPRT_PRC127L(hw->port),
978 pf->stat_offsets_loaded,
979 &osd->rx_size_127, &nsd->rx_size_127);
980 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
981 I40E_GLPRT_PRC255L(hw->port),
982 pf->stat_offsets_loaded,
983 &osd->rx_size_255, &nsd->rx_size_255);
984 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
985 I40E_GLPRT_PRC511L(hw->port),
986 pf->stat_offsets_loaded,
987 &osd->rx_size_511, &nsd->rx_size_511);
988 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
989 I40E_GLPRT_PRC1023L(hw->port),
990 pf->stat_offsets_loaded,
991 &osd->rx_size_1023, &nsd->rx_size_1023);
992 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
993 I40E_GLPRT_PRC1522L(hw->port),
994 pf->stat_offsets_loaded,
995 &osd->rx_size_1522, &nsd->rx_size_1522);
996 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
997 I40E_GLPRT_PRC9522L(hw->port),
998 pf->stat_offsets_loaded,
999 &osd->rx_size_big, &nsd->rx_size_big);
1000
1001 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1002 I40E_GLPRT_PTC64L(hw->port),
1003 pf->stat_offsets_loaded,
1004 &osd->tx_size_64, &nsd->tx_size_64);
1005 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1006 I40E_GLPRT_PTC127L(hw->port),
1007 pf->stat_offsets_loaded,
1008 &osd->tx_size_127, &nsd->tx_size_127);
1009 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1010 I40E_GLPRT_PTC255L(hw->port),
1011 pf->stat_offsets_loaded,
1012 &osd->tx_size_255, &nsd->tx_size_255);
1013 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1014 I40E_GLPRT_PTC511L(hw->port),
1015 pf->stat_offsets_loaded,
1016 &osd->tx_size_511, &nsd->tx_size_511);
1017 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1018 I40E_GLPRT_PTC1023L(hw->port),
1019 pf->stat_offsets_loaded,
1020 &osd->tx_size_1023, &nsd->tx_size_1023);
1021 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1022 I40E_GLPRT_PTC1522L(hw->port),
1023 pf->stat_offsets_loaded,
1024 &osd->tx_size_1522, &nsd->tx_size_1522);
1025 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1026 I40E_GLPRT_PTC9522L(hw->port),
1027 pf->stat_offsets_loaded,
1028 &osd->tx_size_big, &nsd->tx_size_big);
1029
1030 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1031 pf->stat_offsets_loaded,
1032 &osd->rx_undersize, &nsd->rx_undersize);
1033 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1034 pf->stat_offsets_loaded,
1035 &osd->rx_fragments, &nsd->rx_fragments);
1036 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1037 pf->stat_offsets_loaded,
1038 &osd->rx_oversize, &nsd->rx_oversize);
1039 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1040 pf->stat_offsets_loaded,
1041 &osd->rx_jabber, &nsd->rx_jabber);
1042
1043 /* FDIR stats */
1044 i40e_stat_update_and_clear32(hw,
1045 I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1046 &nsd->fd_atr_match);
1047 i40e_stat_update_and_clear32(hw,
1048 I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1049 &nsd->fd_sb_match);
1050 i40e_stat_update_and_clear32(hw,
1051 I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1052 &nsd->fd_atr_tunnel_match);
1053
1054 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1055 nsd->tx_lpi_status =
1056 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1057 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1058 nsd->rx_lpi_status =
1059 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1060 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1061 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1062 pf->stat_offsets_loaded,
1063 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1064 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1065 pf->stat_offsets_loaded,
1066 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1067
1068 if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1069 !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1070 nsd->fd_sb_status = true;
1071 else
1072 nsd->fd_sb_status = false;
1073
1074 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1075 !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1076 nsd->fd_atr_status = true;
1077 else
1078 nsd->fd_atr_status = false;
1079
1080 pf->stat_offsets_loaded = true;
1081 }
1082
1083 /**
1084 * i40e_update_stats - Update the various statistics counters.
1085 * @vsi: the VSI to be updated
1086 *
1087 * Update the various stats for this VSI and its related entities.
1088 **/
1089 void i40e_update_stats(struct i40e_vsi *vsi)
1090 {
1091 struct i40e_pf *pf = vsi->back;
1092
1093 if (vsi == pf->vsi[pf->lan_vsi])
1094 i40e_update_pf_stats(pf);
1095
1096 i40e_update_vsi_stats(vsi);
1097 }
1098
1099 /**
1100 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1101 * @vsi: the VSI to be searched
1102 * @macaddr: the MAC address
1103 * @vlan: the vlan
1104 *
1105 * Returns ptr to the filter object or NULL
1106 **/
1107 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1108 const u8 *macaddr, s16 vlan)
1109 {
1110 struct i40e_mac_filter *f;
1111 u64 key;
1112
1113 if (!vsi || !macaddr)
1114 return NULL;
1115
1116 key = i40e_addr_to_hkey(macaddr);
1117 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1118 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1119 (vlan == f->vlan))
1120 return f;
1121 }
1122 return NULL;
1123 }
1124
1125 /**
1126 * i40e_find_mac - Find a mac addr in the macvlan filters list
1127 * @vsi: the VSI to be searched
1128 * @macaddr: the MAC address we are searching for
1129 *
1130 * Returns the first filter with the provided MAC address or NULL if
1131 * MAC address was not found
1132 **/
1133 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1134 {
1135 struct i40e_mac_filter *f;
1136 u64 key;
1137
1138 if (!vsi || !macaddr)
1139 return NULL;
1140
1141 key = i40e_addr_to_hkey(macaddr);
1142 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1143 if ((ether_addr_equal(macaddr, f->macaddr)))
1144 return f;
1145 }
1146 return NULL;
1147 }
1148
1149 /**
1150 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1151 * @vsi: the VSI to be searched
1152 *
1153 * Returns true if VSI is in vlan mode or false otherwise
1154 **/
1155 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1156 {
1157 /* If we have a PVID, always operate in VLAN mode */
1158 if (vsi->info.pvid)
1159 return true;
1160
1161 /* We need to operate in VLAN mode whenever we have any filters with
1162 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1163 * time, incurring search cost repeatedly. However, we can notice two
1164 * things:
1165 *
1166 * 1) the only place where we can gain a VLAN filter is in
1167 * i40e_add_filter.
1168 *
1169 * 2) the only place where filters are actually removed is in
1170 * i40e_sync_filters_subtask.
1171 *
1172 * Thus, we can simply use a boolean value, has_vlan_filters which we
1173 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1174 * we have to perform the full search after deleting filters in
1175 * i40e_sync_filters_subtask, but we already have to search
1176 * filters here and can perform the check at the same time. This
1177 * results in avoiding embedding a loop for VLAN mode inside another
1178 * loop over all the filters, and should maintain correctness as noted
1179 * above.
1180 */
1181 return vsi->has_vlan_filter;
1182 }
1183
1184 /**
1185 * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1186 * @vsi: the VSI to configure
1187 * @tmp_add_list: list of filters ready to be added
1188 * @tmp_del_list: list of filters ready to be deleted
1189 * @vlan_filters: the number of active VLAN filters
1190 *
1191 * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1192 * behave as expected. If we have any active VLAN filters remaining or about
1193 * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1194 * so that they only match against untagged traffic. If we no longer have any
1195 * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1196 * so that they match against both tagged and untagged traffic. In this way,
1197 * we ensure that we correctly receive the desired traffic. This ensures that
1198 * when we have an active VLAN we will receive only untagged traffic and
1199 * traffic matching active VLANs. If we have no active VLANs then we will
1200 * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1201 *
1202 * Finally, in a similar fashion, this function also corrects filters when
1203 * there is an active PVID assigned to this VSI.
1204 *
1205 * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1206 *
1207 * This function is only expected to be called from within
1208 * i40e_sync_vsi_filters.
1209 *
1210 * NOTE: This function expects to be called while under the
1211 * mac_filter_hash_lock
1212 */
1213 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1214 struct hlist_head *tmp_add_list,
1215 struct hlist_head *tmp_del_list,
1216 int vlan_filters)
1217 {
1218 s16 pvid = le16_to_cpu(vsi->info.pvid);
1219 struct i40e_mac_filter *f, *add_head;
1220 struct i40e_new_mac_filter *new;
1221 struct hlist_node *h;
1222 int bkt, new_vlan;
1223
1224 /* To determine if a particular filter needs to be replaced we
1225 * have the three following conditions:
1226 *
1227 * a) if we have a PVID assigned, then all filters which are
1228 * not marked as VLAN=PVID must be replaced with filters that
1229 * are.
1230 * b) otherwise, if we have any active VLANS, all filters
1231 * which are marked as VLAN=-1 must be replaced with
1232 * filters marked as VLAN=0
1233 * c) finally, if we do not have any active VLANS, all filters
1234 * which are marked as VLAN=0 must be replaced with filters
1235 * marked as VLAN=-1
1236 */
1237
1238 /* Update the filters about to be added in place */
1239 hlist_for_each_entry(new, tmp_add_list, hlist) {
1240 if (pvid && new->f->vlan != pvid)
1241 new->f->vlan = pvid;
1242 else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1243 new->f->vlan = 0;
1244 else if (!vlan_filters && new->f->vlan == 0)
1245 new->f->vlan = I40E_VLAN_ANY;
1246 }
1247
1248 /* Update the remaining active filters */
1249 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1250 /* Combine the checks for whether a filter needs to be changed
1251 * and then determine the new VLAN inside the if block, in
1252 * order to avoid duplicating code for adding the new filter
1253 * then deleting the old filter.
1254 */
1255 if ((pvid && f->vlan != pvid) ||
1256 (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1257 (!vlan_filters && f->vlan == 0)) {
1258 /* Determine the new vlan we will be adding */
1259 if (pvid)
1260 new_vlan = pvid;
1261 else if (vlan_filters)
1262 new_vlan = 0;
1263 else
1264 new_vlan = I40E_VLAN_ANY;
1265
1266 /* Create the new filter */
1267 add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1268 if (!add_head)
1269 return -ENOMEM;
1270
1271 /* Create a temporary i40e_new_mac_filter */
1272 new = kzalloc(sizeof(*new), GFP_ATOMIC);
1273 if (!new)
1274 return -ENOMEM;
1275
1276 new->f = add_head;
1277 new->state = add_head->state;
1278
1279 /* Add the new filter to the tmp list */
1280 hlist_add_head(&new->hlist, tmp_add_list);
1281
1282 /* Put the original filter into the delete list */
1283 f->state = I40E_FILTER_REMOVE;
1284 hash_del(&f->hlist);
1285 hlist_add_head(&f->hlist, tmp_del_list);
1286 }
1287 }
1288
1289 vsi->has_vlan_filter = !!vlan_filters;
1290
1291 return 0;
1292 }
1293
1294 /**
1295 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1296 * @vsi: the PF Main VSI - inappropriate for any other VSI
1297 * @macaddr: the MAC address
1298 *
1299 * Remove whatever filter the firmware set up so the driver can manage
1300 * its own filtering intelligently.
1301 **/
1302 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1303 {
1304 struct i40e_aqc_remove_macvlan_element_data element;
1305 struct i40e_pf *pf = vsi->back;
1306
1307 /* Only appropriate for the PF main VSI */
1308 if (vsi->type != I40E_VSI_MAIN)
1309 return;
1310
1311 memset(&element, 0, sizeof(element));
1312 ether_addr_copy(element.mac_addr, macaddr);
1313 element.vlan_tag = 0;
1314 /* Ignore error returns, some firmware does it this way... */
1315 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1316 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1317
1318 memset(&element, 0, sizeof(element));
1319 ether_addr_copy(element.mac_addr, macaddr);
1320 element.vlan_tag = 0;
1321 /* ...and some firmware does it this way. */
1322 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1323 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1324 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1325 }
1326
1327 /**
1328 * i40e_add_filter - Add a mac/vlan filter to the VSI
1329 * @vsi: the VSI to be searched
1330 * @macaddr: the MAC address
1331 * @vlan: the vlan
1332 *
1333 * Returns ptr to the filter object or NULL when no memory available.
1334 *
1335 * NOTE: This function is expected to be called with mac_filter_hash_lock
1336 * being held.
1337 **/
1338 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1339 const u8 *macaddr, s16 vlan)
1340 {
1341 struct i40e_mac_filter *f;
1342 u64 key;
1343
1344 if (!vsi || !macaddr)
1345 return NULL;
1346
1347 f = i40e_find_filter(vsi, macaddr, vlan);
1348 if (!f) {
1349 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1350 if (!f)
1351 return NULL;
1352
1353 /* Update the boolean indicating if we need to function in
1354 * VLAN mode.
1355 */
1356 if (vlan >= 0)
1357 vsi->has_vlan_filter = true;
1358
1359 ether_addr_copy(f->macaddr, macaddr);
1360 f->vlan = vlan;
1361 f->state = I40E_FILTER_NEW;
1362 INIT_HLIST_NODE(&f->hlist);
1363
1364 key = i40e_addr_to_hkey(macaddr);
1365 hash_add(vsi->mac_filter_hash, &f->hlist, key);
1366
1367 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1368 set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1369 }
1370
1371 /* If we're asked to add a filter that has been marked for removal, it
1372 * is safe to simply restore it to active state. __i40e_del_filter
1373 * will have simply deleted any filters which were previously marked
1374 * NEW or FAILED, so if it is currently marked REMOVE it must have
1375 * previously been ACTIVE. Since we haven't yet run the sync filters
1376 * task, just restore this filter to the ACTIVE state so that the
1377 * sync task leaves it in place
1378 */
1379 if (f->state == I40E_FILTER_REMOVE)
1380 f->state = I40E_FILTER_ACTIVE;
1381
1382 return f;
1383 }
1384
1385 /**
1386 * __i40e_del_filter - Remove a specific filter from the VSI
1387 * @vsi: VSI to remove from
1388 * @f: the filter to remove from the list
1389 *
1390 * This function should be called instead of i40e_del_filter only if you know
1391 * the exact filter you will remove already, such as via i40e_find_filter or
1392 * i40e_find_mac.
1393 *
1394 * NOTE: This function is expected to be called with mac_filter_hash_lock
1395 * being held.
1396 * ANOTHER NOTE: This function MUST be called from within the context of
1397 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1398 * instead of list_for_each_entry().
1399 **/
1400 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1401 {
1402 if (!f)
1403 return;
1404
1405 /* If the filter was never added to firmware then we can just delete it
1406 * directly and we don't want to set the status to remove or else an
1407 * admin queue command will unnecessarily fire.
1408 */
1409 if ((f->state == I40E_FILTER_FAILED) ||
1410 (f->state == I40E_FILTER_NEW)) {
1411 hash_del(&f->hlist);
1412 kfree(f);
1413 } else {
1414 f->state = I40E_FILTER_REMOVE;
1415 }
1416
1417 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1418 set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1419 }
1420
1421 /**
1422 * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1423 * @vsi: the VSI to be searched
1424 * @macaddr: the MAC address
1425 * @vlan: the VLAN
1426 *
1427 * NOTE: This function is expected to be called with mac_filter_hash_lock
1428 * being held.
1429 * ANOTHER NOTE: This function MUST be called from within the context of
1430 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1431 * instead of list_for_each_entry().
1432 **/
1433 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1434 {
1435 struct i40e_mac_filter *f;
1436
1437 if (!vsi || !macaddr)
1438 return;
1439
1440 f = i40e_find_filter(vsi, macaddr, vlan);
1441 __i40e_del_filter(vsi, f);
1442 }
1443
1444 /**
1445 * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1446 * @vsi: the VSI to be searched
1447 * @macaddr: the mac address to be filtered
1448 *
1449 * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1450 * go through all the macvlan filters and add a macvlan filter for each
1451 * unique vlan that already exists. If a PVID has been assigned, instead only
1452 * add the macaddr to that VLAN.
1453 *
1454 * Returns last filter added on success, else NULL
1455 **/
1456 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1457 const u8 *macaddr)
1458 {
1459 struct i40e_mac_filter *f, *add = NULL;
1460 struct hlist_node *h;
1461 int bkt;
1462
1463 if (vsi->info.pvid)
1464 return i40e_add_filter(vsi, macaddr,
1465 le16_to_cpu(vsi->info.pvid));
1466
1467 if (!i40e_is_vsi_in_vlan(vsi))
1468 return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1469
1470 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1471 if (f->state == I40E_FILTER_REMOVE)
1472 continue;
1473 add = i40e_add_filter(vsi, macaddr, f->vlan);
1474 if (!add)
1475 return NULL;
1476 }
1477
1478 return add;
1479 }
1480
1481 /**
1482 * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1483 * @vsi: the VSI to be searched
1484 * @macaddr: the mac address to be removed
1485 *
1486 * Removes a given MAC address from a VSI regardless of what VLAN it has been
1487 * associated with.
1488 *
1489 * Returns 0 for success, or error
1490 **/
1491 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1492 {
1493 struct i40e_mac_filter *f;
1494 struct hlist_node *h;
1495 bool found = false;
1496 int bkt;
1497
1498 WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
1499 "Missing mac_filter_hash_lock\n");
1500 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1501 if (ether_addr_equal(macaddr, f->macaddr)) {
1502 __i40e_del_filter(vsi, f);
1503 found = true;
1504 }
1505 }
1506
1507 if (found)
1508 return 0;
1509 else
1510 return -ENOENT;
1511 }
1512
1513 /**
1514 * i40e_set_mac - NDO callback to set mac address
1515 * @netdev: network interface device structure
1516 * @p: pointer to an address structure
1517 *
1518 * Returns 0 on success, negative on failure
1519 **/
1520 static int i40e_set_mac(struct net_device *netdev, void *p)
1521 {
1522 struct i40e_netdev_priv *np = netdev_priv(netdev);
1523 struct i40e_vsi *vsi = np->vsi;
1524 struct i40e_pf *pf = vsi->back;
1525 struct i40e_hw *hw = &pf->hw;
1526 struct sockaddr *addr = p;
1527
1528 if (!is_valid_ether_addr(addr->sa_data))
1529 return -EADDRNOTAVAIL;
1530
1531 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1532 netdev_info(netdev, "already using mac address %pM\n",
1533 addr->sa_data);
1534 return 0;
1535 }
1536
1537 if (test_bit(__I40E_VSI_DOWN, vsi->back->state) ||
1538 test_bit(__I40E_RESET_RECOVERY_PENDING, vsi->back->state))
1539 return -EADDRNOTAVAIL;
1540
1541 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1542 netdev_info(netdev, "returning to hw mac address %pM\n",
1543 hw->mac.addr);
1544 else
1545 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1546
1547 /* Copy the address first, so that we avoid a possible race with
1548 * .set_rx_mode().
1549 * - Remove old address from MAC filter
1550 * - Copy new address
1551 * - Add new address to MAC filter
1552 */
1553 spin_lock_bh(&vsi->mac_filter_hash_lock);
1554 i40e_del_mac_filter(vsi, netdev->dev_addr);
1555 ether_addr_copy(netdev->dev_addr, addr->sa_data);
1556 i40e_add_mac_filter(vsi, netdev->dev_addr);
1557 spin_unlock_bh(&vsi->mac_filter_hash_lock);
1558
1559 if (vsi->type == I40E_VSI_MAIN) {
1560 i40e_status ret;
1561
1562 ret = i40e_aq_mac_address_write(&vsi->back->hw,
1563 I40E_AQC_WRITE_TYPE_LAA_WOL,
1564 addr->sa_data, NULL);
1565 if (ret)
1566 netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1567 i40e_stat_str(hw, ret),
1568 i40e_aq_str(hw, hw->aq.asq_last_status));
1569 }
1570
1571 /* schedule our worker thread which will take care of
1572 * applying the new filter changes
1573 */
1574 i40e_service_event_schedule(vsi->back);
1575 return 0;
1576 }
1577
1578 /**
1579 * i40e_config_rss_aq - Prepare for RSS using AQ commands
1580 * @vsi: vsi structure
1581 * @seed: RSS hash seed
1582 **/
1583 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1584 u8 *lut, u16 lut_size)
1585 {
1586 struct i40e_pf *pf = vsi->back;
1587 struct i40e_hw *hw = &pf->hw;
1588 int ret = 0;
1589
1590 if (seed) {
1591 struct i40e_aqc_get_set_rss_key_data *seed_dw =
1592 (struct i40e_aqc_get_set_rss_key_data *)seed;
1593 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1594 if (ret) {
1595 dev_info(&pf->pdev->dev,
1596 "Cannot set RSS key, err %s aq_err %s\n",
1597 i40e_stat_str(hw, ret),
1598 i40e_aq_str(hw, hw->aq.asq_last_status));
1599 return ret;
1600 }
1601 }
1602 if (lut) {
1603 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
1604
1605 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1606 if (ret) {
1607 dev_info(&pf->pdev->dev,
1608 "Cannot set RSS lut, err %s aq_err %s\n",
1609 i40e_stat_str(hw, ret),
1610 i40e_aq_str(hw, hw->aq.asq_last_status));
1611 return ret;
1612 }
1613 }
1614 return ret;
1615 }
1616
1617 /**
1618 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1619 * @vsi: VSI structure
1620 **/
1621 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1622 {
1623 struct i40e_pf *pf = vsi->back;
1624 u8 seed[I40E_HKEY_ARRAY_SIZE];
1625 u8 *lut;
1626 int ret;
1627
1628 if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1629 return 0;
1630 if (!vsi->rss_size)
1631 vsi->rss_size = min_t(int, pf->alloc_rss_size,
1632 vsi->num_queue_pairs);
1633 if (!vsi->rss_size)
1634 return -EINVAL;
1635 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1636 if (!lut)
1637 return -ENOMEM;
1638
1639 /* Use the user configured hash keys and lookup table if there is one,
1640 * otherwise use default
1641 */
1642 if (vsi->rss_lut_user)
1643 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1644 else
1645 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1646 if (vsi->rss_hkey_user)
1647 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1648 else
1649 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1650 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1651 kfree(lut);
1652 return ret;
1653 }
1654
1655 /**
1656 * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1657 * @vsi: the VSI being configured,
1658 * @ctxt: VSI context structure
1659 * @enabled_tc: number of traffic classes to enable
1660 *
1661 * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1662 **/
1663 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1664 struct i40e_vsi_context *ctxt,
1665 u8 enabled_tc)
1666 {
1667 u16 qcount = 0, max_qcount, qmap, sections = 0;
1668 int i, override_q, pow, num_qps, ret;
1669 u8 netdev_tc = 0, offset = 0;
1670
1671 if (vsi->type != I40E_VSI_MAIN)
1672 return -EINVAL;
1673 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1674 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1675 vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1676 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1677 num_qps = vsi->mqprio_qopt.qopt.count[0];
1678
1679 /* find the next higher power-of-2 of num queue pairs */
1680 pow = ilog2(num_qps);
1681 if (!is_power_of_2(num_qps))
1682 pow++;
1683 qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1684 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1685
1686 /* Setup queue offset/count for all TCs for given VSI */
1687 max_qcount = vsi->mqprio_qopt.qopt.count[0];
1688 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1689 /* See if the given TC is enabled for the given VSI */
1690 if (vsi->tc_config.enabled_tc & BIT(i)) {
1691 offset = vsi->mqprio_qopt.qopt.offset[i];
1692 qcount = vsi->mqprio_qopt.qopt.count[i];
1693 if (qcount > max_qcount)
1694 max_qcount = qcount;
1695 vsi->tc_config.tc_info[i].qoffset = offset;
1696 vsi->tc_config.tc_info[i].qcount = qcount;
1697 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1698 } else {
1699 /* TC is not enabled so set the offset to
1700 * default queue and allocate one queue
1701 * for the given TC.
1702 */
1703 vsi->tc_config.tc_info[i].qoffset = 0;
1704 vsi->tc_config.tc_info[i].qcount = 1;
1705 vsi->tc_config.tc_info[i].netdev_tc = 0;
1706 }
1707 }
1708
1709 /* Set actual Tx/Rx queue pairs */
1710 vsi->num_queue_pairs = offset + qcount;
1711
1712 /* Setup queue TC[0].qmap for given VSI context */
1713 ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1714 ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1715 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1716 ctxt->info.valid_sections |= cpu_to_le16(sections);
1717
1718 /* Reconfigure RSS for main VSI with max queue count */
1719 vsi->rss_size = max_qcount;
1720 ret = i40e_vsi_config_rss(vsi);
1721 if (ret) {
1722 dev_info(&vsi->back->pdev->dev,
1723 "Failed to reconfig rss for num_queues (%u)\n",
1724 max_qcount);
1725 return ret;
1726 }
1727 vsi->reconfig_rss = true;
1728 dev_dbg(&vsi->back->pdev->dev,
1729 "Reconfigured rss with num_queues (%u)\n", max_qcount);
1730
1731 /* Find queue count available for channel VSIs and starting offset
1732 * for channel VSIs
1733 */
1734 override_q = vsi->mqprio_qopt.qopt.count[0];
1735 if (override_q && override_q < vsi->num_queue_pairs) {
1736 vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1737 vsi->next_base_queue = override_q;
1738 }
1739 return 0;
1740 }
1741
1742 /**
1743 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1744 * @vsi: the VSI being setup
1745 * @ctxt: VSI context structure
1746 * @enabled_tc: Enabled TCs bitmap
1747 * @is_add: True if called before Add VSI
1748 *
1749 * Setup VSI queue mapping for enabled traffic classes.
1750 **/
1751 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1752 struct i40e_vsi_context *ctxt,
1753 u8 enabled_tc,
1754 bool is_add)
1755 {
1756 struct i40e_pf *pf = vsi->back;
1757 u16 sections = 0;
1758 u8 netdev_tc = 0;
1759 u16 numtc = 1;
1760 u16 qcount;
1761 u8 offset;
1762 u16 qmap;
1763 int i;
1764 u16 num_tc_qps = 0;
1765
1766 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1767 offset = 0;
1768
1769 /* Number of queues per enabled TC */
1770 num_tc_qps = vsi->alloc_queue_pairs;
1771 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1772 /* Find numtc from enabled TC bitmap */
1773 for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1774 if (enabled_tc & BIT(i)) /* TC is enabled */
1775 numtc++;
1776 }
1777 if (!numtc) {
1778 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1779 numtc = 1;
1780 }
1781 num_tc_qps = num_tc_qps / numtc;
1782 num_tc_qps = min_t(int, num_tc_qps,
1783 i40e_pf_get_max_q_per_tc(pf));
1784 }
1785
1786 vsi->tc_config.numtc = numtc;
1787 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1788
1789 /* Do not allow use more TC queue pairs than MSI-X vectors exist */
1790 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1791 num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
1792
1793 /* Setup queue offset/count for all TCs for given VSI */
1794 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1795 /* See if the given TC is enabled for the given VSI */
1796 if (vsi->tc_config.enabled_tc & BIT(i)) {
1797 /* TC is enabled */
1798 int pow, num_qps;
1799
1800 switch (vsi->type) {
1801 case I40E_VSI_MAIN:
1802 if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
1803 I40E_FLAG_FD_ATR_ENABLED)) ||
1804 vsi->tc_config.enabled_tc != 1) {
1805 qcount = min_t(int, pf->alloc_rss_size,
1806 num_tc_qps);
1807 break;
1808 }
1809 /* fall through */
1810 case I40E_VSI_FDIR:
1811 case I40E_VSI_SRIOV:
1812 case I40E_VSI_VMDQ2:
1813 default:
1814 qcount = num_tc_qps;
1815 WARN_ON(i != 0);
1816 break;
1817 }
1818 vsi->tc_config.tc_info[i].qoffset = offset;
1819 vsi->tc_config.tc_info[i].qcount = qcount;
1820
1821 /* find the next higher power-of-2 of num queue pairs */
1822 num_qps = qcount;
1823 pow = 0;
1824 while (num_qps && (BIT_ULL(pow) < qcount)) {
1825 pow++;
1826 num_qps >>= 1;
1827 }
1828
1829 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1830 qmap =
1831 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1832 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1833
1834 offset += qcount;
1835 } else {
1836 /* TC is not enabled so set the offset to
1837 * default queue and allocate one queue
1838 * for the given TC.
1839 */
1840 vsi->tc_config.tc_info[i].qoffset = 0;
1841 vsi->tc_config.tc_info[i].qcount = 1;
1842 vsi->tc_config.tc_info[i].netdev_tc = 0;
1843
1844 qmap = 0;
1845 }
1846 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1847 }
1848
1849 /* Set actual Tx/Rx queue pairs */
1850 vsi->num_queue_pairs = offset;
1851 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1852 if (vsi->req_queue_pairs > 0)
1853 vsi->num_queue_pairs = vsi->req_queue_pairs;
1854 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1855 vsi->num_queue_pairs = pf->num_lan_msix;
1856 }
1857
1858 /* Scheduler section valid can only be set for ADD VSI */
1859 if (is_add) {
1860 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1861
1862 ctxt->info.up_enable_bits = enabled_tc;
1863 }
1864 if (vsi->type == I40E_VSI_SRIOV) {
1865 ctxt->info.mapping_flags |=
1866 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1867 for (i = 0; i < vsi->num_queue_pairs; i++)
1868 ctxt->info.queue_mapping[i] =
1869 cpu_to_le16(vsi->base_queue + i);
1870 } else {
1871 ctxt->info.mapping_flags |=
1872 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1873 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1874 }
1875 ctxt->info.valid_sections |= cpu_to_le16(sections);
1876 }
1877
1878 /**
1879 * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1880 * @netdev: the netdevice
1881 * @addr: address to add
1882 *
1883 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1884 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1885 */
1886 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1887 {
1888 struct i40e_netdev_priv *np = netdev_priv(netdev);
1889 struct i40e_vsi *vsi = np->vsi;
1890
1891 if (i40e_add_mac_filter(vsi, addr))
1892 return 0;
1893 else
1894 return -ENOMEM;
1895 }
1896
1897 /**
1898 * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1899 * @netdev: the netdevice
1900 * @addr: address to add
1901 *
1902 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1903 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1904 */
1905 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1906 {
1907 struct i40e_netdev_priv *np = netdev_priv(netdev);
1908 struct i40e_vsi *vsi = np->vsi;
1909
1910 /* Under some circumstances, we might receive a request to delete
1911 * our own device address from our uc list. Because we store the
1912 * device address in the VSI's MAC/VLAN filter list, we need to ignore
1913 * such requests and not delete our device address from this list.
1914 */
1915 if (ether_addr_equal(addr, netdev->dev_addr))
1916 return 0;
1917
1918 i40e_del_mac_filter(vsi, addr);
1919
1920 return 0;
1921 }
1922
1923 /**
1924 * i40e_set_rx_mode - NDO callback to set the netdev filters
1925 * @netdev: network interface device structure
1926 **/
1927 static void i40e_set_rx_mode(struct net_device *netdev)
1928 {
1929 struct i40e_netdev_priv *np = netdev_priv(netdev);
1930 struct i40e_vsi *vsi = np->vsi;
1931
1932 spin_lock_bh(&vsi->mac_filter_hash_lock);
1933
1934 __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1935 __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1936
1937 spin_unlock_bh(&vsi->mac_filter_hash_lock);
1938
1939 /* check for other flag changes */
1940 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1941 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1942 set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1943 }
1944 }
1945
1946 /**
1947 * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
1948 * @vsi: Pointer to VSI struct
1949 * @from: Pointer to list which contains MAC filter entries - changes to
1950 * those entries needs to be undone.
1951 *
1952 * MAC filter entries from this list were slated for deletion.
1953 **/
1954 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1955 struct hlist_head *from)
1956 {
1957 struct i40e_mac_filter *f;
1958 struct hlist_node *h;
1959
1960 hlist_for_each_entry_safe(f, h, from, hlist) {
1961 u64 key = i40e_addr_to_hkey(f->macaddr);
1962
1963 /* Move the element back into MAC filter list*/
1964 hlist_del(&f->hlist);
1965 hash_add(vsi->mac_filter_hash, &f->hlist, key);
1966 }
1967 }
1968
1969 /**
1970 * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
1971 * @vsi: Pointer to vsi struct
1972 * @from: Pointer to list which contains MAC filter entries - changes to
1973 * those entries needs to be undone.
1974 *
1975 * MAC filter entries from this list were slated for addition.
1976 **/
1977 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
1978 struct hlist_head *from)
1979 {
1980 struct i40e_new_mac_filter *new;
1981 struct hlist_node *h;
1982
1983 hlist_for_each_entry_safe(new, h, from, hlist) {
1984 /* We can simply free the wrapper structure */
1985 hlist_del(&new->hlist);
1986 kfree(new);
1987 }
1988 }
1989
1990 /**
1991 * i40e_next_entry - Get the next non-broadcast filter from a list
1992 * @next: pointer to filter in list
1993 *
1994 * Returns the next non-broadcast filter in the list. Required so that we
1995 * ignore broadcast filters within the list, since these are not handled via
1996 * the normal firmware update path.
1997 */
1998 static
1999 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2000 {
2001 hlist_for_each_entry_continue(next, hlist) {
2002 if (!is_broadcast_ether_addr(next->f->macaddr))
2003 return next;
2004 }
2005
2006 return NULL;
2007 }
2008
2009 /**
2010 * i40e_update_filter_state - Update filter state based on return data
2011 * from firmware
2012 * @count: Number of filters added
2013 * @add_list: return data from fw
2014 * @add_head: pointer to first filter in current batch
2015 *
2016 * MAC filter entries from list were slated to be added to device. Returns
2017 * number of successful filters. Note that 0 does NOT mean success!
2018 **/
2019 static int
2020 i40e_update_filter_state(int count,
2021 struct i40e_aqc_add_macvlan_element_data *add_list,
2022 struct i40e_new_mac_filter *add_head)
2023 {
2024 int retval = 0;
2025 int i;
2026
2027 for (i = 0; i < count; i++) {
2028 /* Always check status of each filter. We don't need to check
2029 * the firmware return status because we pre-set the filter
2030 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2031 * request to the adminq. Thus, if it no longer matches then
2032 * we know the filter is active.
2033 */
2034 if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2035 add_head->state = I40E_FILTER_FAILED;
2036 } else {
2037 add_head->state = I40E_FILTER_ACTIVE;
2038 retval++;
2039 }
2040
2041 add_head = i40e_next_filter(add_head);
2042 if (!add_head)
2043 break;
2044 }
2045
2046 return retval;
2047 }
2048
2049 /**
2050 * i40e_aqc_del_filters - Request firmware to delete a set of filters
2051 * @vsi: ptr to the VSI
2052 * @vsi_name: name to display in messages
2053 * @list: the list of filters to send to firmware
2054 * @num_del: the number of filters to delete
2055 * @retval: Set to -EIO on failure to delete
2056 *
2057 * Send a request to firmware via AdminQ to delete a set of filters. Uses
2058 * *retval instead of a return value so that success does not force ret_val to
2059 * be set to 0. This ensures that a sequence of calls to this function
2060 * preserve the previous value of *retval on successful delete.
2061 */
2062 static
2063 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2064 struct i40e_aqc_remove_macvlan_element_data *list,
2065 int num_del, int *retval)
2066 {
2067 struct i40e_hw *hw = &vsi->back->hw;
2068 i40e_status aq_ret;
2069 int aq_err;
2070
2071 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
2072 aq_err = hw->aq.asq_last_status;
2073
2074 /* Explicitly ignore and do not report when firmware returns ENOENT */
2075 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
2076 *retval = -EIO;
2077 dev_info(&vsi->back->pdev->dev,
2078 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2079 vsi_name, i40e_stat_str(hw, aq_ret),
2080 i40e_aq_str(hw, aq_err));
2081 }
2082 }
2083
2084 /**
2085 * i40e_aqc_add_filters - Request firmware to add a set of filters
2086 * @vsi: ptr to the VSI
2087 * @vsi_name: name to display in messages
2088 * @list: the list of filters to send to firmware
2089 * @add_head: Position in the add hlist
2090 * @num_add: the number of filters to add
2091 *
2092 * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2093 * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2094 * space for more filters.
2095 */
2096 static
2097 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2098 struct i40e_aqc_add_macvlan_element_data *list,
2099 struct i40e_new_mac_filter *add_head,
2100 int num_add)
2101 {
2102 struct i40e_hw *hw = &vsi->back->hw;
2103 int aq_err, fcnt;
2104
2105 i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
2106 aq_err = hw->aq.asq_last_status;
2107 fcnt = i40e_update_filter_state(num_add, list, add_head);
2108
2109 if (fcnt != num_add) {
2110 set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2111 dev_warn(&vsi->back->pdev->dev,
2112 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2113 i40e_aq_str(hw, aq_err),
2114 vsi_name);
2115 }
2116 }
2117
2118 /**
2119 * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2120 * @vsi: pointer to the VSI
2121 * @vsi_name: the VSI name
2122 * @f: filter data
2123 *
2124 * This function sets or clears the promiscuous broadcast flags for VLAN
2125 * filters in order to properly receive broadcast frames. Assumes that only
2126 * broadcast filters are passed.
2127 *
2128 * Returns status indicating success or failure;
2129 **/
2130 static i40e_status
2131 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2132 struct i40e_mac_filter *f)
2133 {
2134 bool enable = f->state == I40E_FILTER_NEW;
2135 struct i40e_hw *hw = &vsi->back->hw;
2136 i40e_status aq_ret;
2137
2138 if (f->vlan == I40E_VLAN_ANY) {
2139 aq_ret = i40e_aq_set_vsi_broadcast(hw,
2140 vsi->seid,
2141 enable,
2142 NULL);
2143 } else {
2144 aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2145 vsi->seid,
2146 enable,
2147 f->vlan,
2148 NULL);
2149 }
2150
2151 if (aq_ret) {
2152 set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2153 dev_warn(&vsi->back->pdev->dev,
2154 "Error %s, forcing overflow promiscuous on %s\n",
2155 i40e_aq_str(hw, hw->aq.asq_last_status),
2156 vsi_name);
2157 }
2158
2159 return aq_ret;
2160 }
2161
2162 /**
2163 * i40e_set_promiscuous - set promiscuous mode
2164 * @pf: board private structure
2165 * @promisc: promisc on or off
2166 *
2167 * There are different ways of setting promiscuous mode on a PF depending on
2168 * what state/environment we're in. This identifies and sets it appropriately.
2169 * Returns 0 on success.
2170 **/
2171 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2172 {
2173 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2174 struct i40e_hw *hw = &pf->hw;
2175 i40e_status aq_ret;
2176
2177 if (vsi->type == I40E_VSI_MAIN &&
2178 pf->lan_veb != I40E_NO_VEB &&
2179 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2180 /* set defport ON for Main VSI instead of true promisc
2181 * this way we will get all unicast/multicast and VLAN
2182 * promisc behavior but will not get VF or VMDq traffic
2183 * replicated on the Main VSI.
2184 */
2185 if (promisc)
2186 aq_ret = i40e_aq_set_default_vsi(hw,
2187 vsi->seid,
2188 NULL);
2189 else
2190 aq_ret = i40e_aq_clear_default_vsi(hw,
2191 vsi->seid,
2192 NULL);
2193 if (aq_ret) {
2194 dev_info(&pf->pdev->dev,
2195 "Set default VSI failed, err %s, aq_err %s\n",
2196 i40e_stat_str(hw, aq_ret),
2197 i40e_aq_str(hw, hw->aq.asq_last_status));
2198 }
2199 } else {
2200 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2201 hw,
2202 vsi->seid,
2203 promisc, NULL,
2204 true);
2205 if (aq_ret) {
2206 dev_info(&pf->pdev->dev,
2207 "set unicast promisc failed, err %s, aq_err %s\n",
2208 i40e_stat_str(hw, aq_ret),
2209 i40e_aq_str(hw, hw->aq.asq_last_status));
2210 }
2211 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2212 hw,
2213 vsi->seid,
2214 promisc, NULL);
2215 if (aq_ret) {
2216 dev_info(&pf->pdev->dev,
2217 "set multicast promisc failed, err %s, aq_err %s\n",
2218 i40e_stat_str(hw, aq_ret),
2219 i40e_aq_str(hw, hw->aq.asq_last_status));
2220 }
2221 }
2222
2223 if (!aq_ret)
2224 pf->cur_promisc = promisc;
2225
2226 return aq_ret;
2227 }
2228
2229 /**
2230 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2231 * @vsi: ptr to the VSI
2232 *
2233 * Push any outstanding VSI filter changes through the AdminQ.
2234 *
2235 * Returns 0 or error value
2236 **/
2237 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2238 {
2239 struct hlist_head tmp_add_list, tmp_del_list;
2240 struct i40e_mac_filter *f;
2241 struct i40e_new_mac_filter *new, *add_head = NULL;
2242 struct i40e_hw *hw = &vsi->back->hw;
2243 bool old_overflow, new_overflow;
2244 unsigned int failed_filters = 0;
2245 unsigned int vlan_filters = 0;
2246 char vsi_name[16] = "PF";
2247 int filter_list_len = 0;
2248 i40e_status aq_ret = 0;
2249 u32 changed_flags = 0;
2250 struct hlist_node *h;
2251 struct i40e_pf *pf;
2252 int num_add = 0;
2253 int num_del = 0;
2254 int retval = 0;
2255 u16 cmd_flags;
2256 int list_size;
2257 int bkt;
2258
2259 /* empty array typed pointers, kcalloc later */
2260 struct i40e_aqc_add_macvlan_element_data *add_list;
2261 struct i40e_aqc_remove_macvlan_element_data *del_list;
2262
2263 while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2264 usleep_range(1000, 2000);
2265 pf = vsi->back;
2266
2267 old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2268
2269 if (vsi->netdev) {
2270 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2271 vsi->current_netdev_flags = vsi->netdev->flags;
2272 }
2273
2274 INIT_HLIST_HEAD(&tmp_add_list);
2275 INIT_HLIST_HEAD(&tmp_del_list);
2276
2277 if (vsi->type == I40E_VSI_SRIOV)
2278 snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2279 else if (vsi->type != I40E_VSI_MAIN)
2280 snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2281
2282 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2283 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2284
2285 spin_lock_bh(&vsi->mac_filter_hash_lock);
2286 /* Create a list of filters to delete. */
2287 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2288 if (f->state == I40E_FILTER_REMOVE) {
2289 /* Move the element into temporary del_list */
2290 hash_del(&f->hlist);
2291 hlist_add_head(&f->hlist, &tmp_del_list);
2292
2293 /* Avoid counting removed filters */
2294 continue;
2295 }
2296 if (f->state == I40E_FILTER_NEW) {
2297 /* Create a temporary i40e_new_mac_filter */
2298 new = kzalloc(sizeof(*new), GFP_ATOMIC);
2299 if (!new)
2300 goto err_no_memory_locked;
2301
2302 /* Store pointer to the real filter */
2303 new->f = f;
2304 new->state = f->state;
2305
2306 /* Add it to the hash list */
2307 hlist_add_head(&new->hlist, &tmp_add_list);
2308 }
2309
2310 /* Count the number of active (current and new) VLAN
2311 * filters we have now. Does not count filters which
2312 * are marked for deletion.
2313 */
2314 if (f->vlan > 0)
2315 vlan_filters++;
2316 }
2317
2318 retval = i40e_correct_mac_vlan_filters(vsi,
2319 &tmp_add_list,
2320 &tmp_del_list,
2321 vlan_filters);
2322 if (retval)
2323 goto err_no_memory_locked;
2324
2325 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2326 }
2327
2328 /* Now process 'del_list' outside the lock */
2329 if (!hlist_empty(&tmp_del_list)) {
2330 filter_list_len = hw->aq.asq_buf_size /
2331 sizeof(struct i40e_aqc_remove_macvlan_element_data);
2332 list_size = filter_list_len *
2333 sizeof(struct i40e_aqc_remove_macvlan_element_data);
2334 del_list = kzalloc(list_size, GFP_ATOMIC);
2335 if (!del_list)
2336 goto err_no_memory;
2337
2338 hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2339 cmd_flags = 0;
2340
2341 /* handle broadcast filters by updating the broadcast
2342 * promiscuous flag and release filter list.
2343 */
2344 if (is_broadcast_ether_addr(f->macaddr)) {
2345 i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2346
2347 hlist_del(&f->hlist);
2348 kfree(f);
2349 continue;
2350 }
2351
2352 /* add to delete list */
2353 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2354 if (f->vlan == I40E_VLAN_ANY) {
2355 del_list[num_del].vlan_tag = 0;
2356 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2357 } else {
2358 del_list[num_del].vlan_tag =
2359 cpu_to_le16((u16)(f->vlan));
2360 }
2361
2362 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2363 del_list[num_del].flags = cmd_flags;
2364 num_del++;
2365
2366 /* flush a full buffer */
2367 if (num_del == filter_list_len) {
2368 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2369 num_del, &retval);
2370 memset(del_list, 0, list_size);
2371 num_del = 0;
2372 }
2373 /* Release memory for MAC filter entries which were
2374 * synced up with HW.
2375 */
2376 hlist_del(&f->hlist);
2377 kfree(f);
2378 }
2379
2380 if (num_del) {
2381 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2382 num_del, &retval);
2383 }
2384
2385 kfree(del_list);
2386 del_list = NULL;
2387 }
2388
2389 if (!hlist_empty(&tmp_add_list)) {
2390 /* Do all the adds now. */
2391 filter_list_len = hw->aq.asq_buf_size /
2392 sizeof(struct i40e_aqc_add_macvlan_element_data);
2393 list_size = filter_list_len *
2394 sizeof(struct i40e_aqc_add_macvlan_element_data);
2395 add_list = kzalloc(list_size, GFP_ATOMIC);
2396 if (!add_list)
2397 goto err_no_memory;
2398
2399 num_add = 0;
2400 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2401 /* handle broadcast filters by updating the broadcast
2402 * promiscuous flag instead of adding a MAC filter.
2403 */
2404 if (is_broadcast_ether_addr(new->f->macaddr)) {
2405 if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2406 new->f))
2407 new->state = I40E_FILTER_FAILED;
2408 else
2409 new->state = I40E_FILTER_ACTIVE;
2410 continue;
2411 }
2412
2413 /* add to add array */
2414 if (num_add == 0)
2415 add_head = new;
2416 cmd_flags = 0;
2417 ether_addr_copy(add_list[num_add].mac_addr,
2418 new->f->macaddr);
2419 if (new->f->vlan == I40E_VLAN_ANY) {
2420 add_list[num_add].vlan_tag = 0;
2421 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2422 } else {
2423 add_list[num_add].vlan_tag =
2424 cpu_to_le16((u16)(new->f->vlan));
2425 }
2426 add_list[num_add].queue_number = 0;
2427 /* set invalid match method for later detection */
2428 add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2429 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2430 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2431 num_add++;
2432
2433 /* flush a full buffer */
2434 if (num_add == filter_list_len) {
2435 i40e_aqc_add_filters(vsi, vsi_name, add_list,
2436 add_head, num_add);
2437 memset(add_list, 0, list_size);
2438 num_add = 0;
2439 }
2440 }
2441 if (num_add) {
2442 i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2443 num_add);
2444 }
2445 /* Now move all of the filters from the temp add list back to
2446 * the VSI's list.
2447 */
2448 spin_lock_bh(&vsi->mac_filter_hash_lock);
2449 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2450 /* Only update the state if we're still NEW */
2451 if (new->f->state == I40E_FILTER_NEW)
2452 new->f->state = new->state;
2453 hlist_del(&new->hlist);
2454 kfree(new);
2455 }
2456 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2457 kfree(add_list);
2458 add_list = NULL;
2459 }
2460
2461 /* Determine the number of active and failed filters. */
2462 spin_lock_bh(&vsi->mac_filter_hash_lock);
2463 vsi->active_filters = 0;
2464 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2465 if (f->state == I40E_FILTER_ACTIVE)
2466 vsi->active_filters++;
2467 else if (f->state == I40E_FILTER_FAILED)
2468 failed_filters++;
2469 }
2470 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2471
2472 /* Check if we are able to exit overflow promiscuous mode. We can
2473 * safely exit if we didn't just enter, we no longer have any failed
2474 * filters, and we have reduced filters below the threshold value.
2475 */
2476 if (old_overflow && !failed_filters &&
2477 vsi->active_filters < vsi->promisc_threshold) {
2478 dev_info(&pf->pdev->dev,
2479 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2480 vsi_name);
2481 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2482 vsi->promisc_threshold = 0;
2483 }
2484
2485 /* if the VF is not trusted do not do promisc */
2486 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2487 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2488 goto out;
2489 }
2490
2491 new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2492
2493 /* If we are entering overflow promiscuous, we need to calculate a new
2494 * threshold for when we are safe to exit
2495 */
2496 if (!old_overflow && new_overflow)
2497 vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2498
2499 /* check for changes in promiscuous modes */
2500 if (changed_flags & IFF_ALLMULTI) {
2501 bool cur_multipromisc;
2502
2503 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2504 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2505 vsi->seid,
2506 cur_multipromisc,
2507 NULL);
2508 if (aq_ret) {
2509 retval = i40e_aq_rc_to_posix(aq_ret,
2510 hw->aq.asq_last_status);
2511 dev_info(&pf->pdev->dev,
2512 "set multi promisc failed on %s, err %s aq_err %s\n",
2513 vsi_name,
2514 i40e_stat_str(hw, aq_ret),
2515 i40e_aq_str(hw, hw->aq.asq_last_status));
2516 }
2517 }
2518
2519 if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2520 bool cur_promisc;
2521
2522 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2523 new_overflow);
2524 aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2525 if (aq_ret) {
2526 retval = i40e_aq_rc_to_posix(aq_ret,
2527 hw->aq.asq_last_status);
2528 dev_info(&pf->pdev->dev,
2529 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2530 cur_promisc ? "on" : "off",
2531 vsi_name,
2532 i40e_stat_str(hw, aq_ret),
2533 i40e_aq_str(hw, hw->aq.asq_last_status));
2534 }
2535 }
2536 out:
2537 /* if something went wrong then set the changed flag so we try again */
2538 if (retval)
2539 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2540
2541 clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2542 return retval;
2543
2544 err_no_memory:
2545 /* Restore elements on the temporary add and delete lists */
2546 spin_lock_bh(&vsi->mac_filter_hash_lock);
2547 err_no_memory_locked:
2548 i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2549 i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2550 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2551
2552 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2553 clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2554 return -ENOMEM;
2555 }
2556
2557 /**
2558 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2559 * @pf: board private structure
2560 **/
2561 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2562 {
2563 int v;
2564
2565 if (!pf)
2566 return;
2567 if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2568 return;
2569
2570 for (v = 0; v < pf->num_alloc_vsi; v++) {
2571 if (pf->vsi[v] &&
2572 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2573 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2574
2575 if (ret) {
2576 /* come back and try again later */
2577 set_bit(__I40E_MACVLAN_SYNC_PENDING,
2578 pf->state);
2579 break;
2580 }
2581 }
2582 }
2583 }
2584
2585 /**
2586 * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2587 * @vsi: the vsi
2588 **/
2589 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2590 {
2591 if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2592 return I40E_RXBUFFER_2048;
2593 else
2594 return I40E_RXBUFFER_3072;
2595 }
2596
2597 /**
2598 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2599 * @netdev: network interface device structure
2600 * @new_mtu: new value for maximum frame size
2601 *
2602 * Returns 0 on success, negative on failure
2603 **/
2604 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2605 {
2606 struct i40e_netdev_priv *np = netdev_priv(netdev);
2607 struct i40e_vsi *vsi = np->vsi;
2608 struct i40e_pf *pf = vsi->back;
2609
2610 if (i40e_enabled_xdp_vsi(vsi)) {
2611 int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2612
2613 if (frame_size > i40e_max_xdp_frame_size(vsi))
2614 return -EINVAL;
2615 }
2616
2617 netdev_info(netdev, "changing MTU from %d to %d\n",
2618 netdev->mtu, new_mtu);
2619 netdev->mtu = new_mtu;
2620 if (netif_running(netdev))
2621 i40e_vsi_reinit_locked(vsi);
2622 set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2623 set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2624 return 0;
2625 }
2626
2627 /**
2628 * i40e_ioctl - Access the hwtstamp interface
2629 * @netdev: network interface device structure
2630 * @ifr: interface request data
2631 * @cmd: ioctl command
2632 **/
2633 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2634 {
2635 struct i40e_netdev_priv *np = netdev_priv(netdev);
2636 struct i40e_pf *pf = np->vsi->back;
2637
2638 switch (cmd) {
2639 case SIOCGHWTSTAMP:
2640 return i40e_ptp_get_ts_config(pf, ifr);
2641 case SIOCSHWTSTAMP:
2642 return i40e_ptp_set_ts_config(pf, ifr);
2643 default:
2644 return -EOPNOTSUPP;
2645 }
2646 }
2647
2648 /**
2649 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2650 * @vsi: the vsi being adjusted
2651 **/
2652 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2653 {
2654 struct i40e_vsi_context ctxt;
2655 i40e_status ret;
2656
2657 if ((vsi->info.valid_sections &
2658 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2659 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2660 return; /* already enabled */
2661
2662 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2663 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2664 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2665
2666 ctxt.seid = vsi->seid;
2667 ctxt.info = vsi->info;
2668 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2669 if (ret) {
2670 dev_info(&vsi->back->pdev->dev,
2671 "update vlan stripping failed, err %s aq_err %s\n",
2672 i40e_stat_str(&vsi->back->hw, ret),
2673 i40e_aq_str(&vsi->back->hw,
2674 vsi->back->hw.aq.asq_last_status));
2675 }
2676 }
2677
2678 /**
2679 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2680 * @vsi: the vsi being adjusted
2681 **/
2682 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2683 {
2684 struct i40e_vsi_context ctxt;
2685 i40e_status ret;
2686
2687 if ((vsi->info.valid_sections &
2688 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2689 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2690 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2691 return; /* already disabled */
2692
2693 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2694 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2695 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2696
2697 ctxt.seid = vsi->seid;
2698 ctxt.info = vsi->info;
2699 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2700 if (ret) {
2701 dev_info(&vsi->back->pdev->dev,
2702 "update vlan stripping failed, err %s aq_err %s\n",
2703 i40e_stat_str(&vsi->back->hw, ret),
2704 i40e_aq_str(&vsi->back->hw,
2705 vsi->back->hw.aq.asq_last_status));
2706 }
2707 }
2708
2709 /**
2710 * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
2711 * @vsi: the vsi being configured
2712 * @vid: vlan id to be added (0 = untagged only , -1 = any)
2713 *
2714 * This is a helper function for adding a new MAC/VLAN filter with the
2715 * specified VLAN for each existing MAC address already in the hash table.
2716 * This function does *not* perform any accounting to update filters based on
2717 * VLAN mode.
2718 *
2719 * NOTE: this function expects to be called while under the
2720 * mac_filter_hash_lock
2721 **/
2722 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2723 {
2724 struct i40e_mac_filter *f, *add_f;
2725 struct hlist_node *h;
2726 int bkt;
2727
2728 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2729 if (f->state == I40E_FILTER_REMOVE)
2730 continue;
2731 add_f = i40e_add_filter(vsi, f->macaddr, vid);
2732 if (!add_f) {
2733 dev_info(&vsi->back->pdev->dev,
2734 "Could not add vlan filter %d for %pM\n",
2735 vid, f->macaddr);
2736 return -ENOMEM;
2737 }
2738 }
2739
2740 return 0;
2741 }
2742
2743 /**
2744 * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2745 * @vsi: the VSI being configured
2746 * @vid: VLAN id to be added
2747 **/
2748 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
2749 {
2750 int err;
2751
2752 if (vsi->info.pvid)
2753 return -EINVAL;
2754
2755 /* The network stack will attempt to add VID=0, with the intention to
2756 * receive priority tagged packets with a VLAN of 0. Our HW receives
2757 * these packets by default when configured to receive untagged
2758 * packets, so we don't need to add a filter for this case.
2759 * Additionally, HW interprets adding a VID=0 filter as meaning to
2760 * receive *only* tagged traffic and stops receiving untagged traffic.
2761 * Thus, we do not want to actually add a filter for VID=0
2762 */
2763 if (!vid)
2764 return 0;
2765
2766 /* Locked once because all functions invoked below iterates list*/
2767 spin_lock_bh(&vsi->mac_filter_hash_lock);
2768 err = i40e_add_vlan_all_mac(vsi, vid);
2769 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2770 if (err)
2771 return err;
2772
2773 /* schedule our worker thread which will take care of
2774 * applying the new filter changes
2775 */
2776 i40e_service_event_schedule(vsi->back);
2777 return 0;
2778 }
2779
2780 /**
2781 * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
2782 * @vsi: the vsi being configured
2783 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
2784 *
2785 * This function should be used to remove all VLAN filters which match the
2786 * given VID. It does not schedule the service event and does not take the
2787 * mac_filter_hash_lock so it may be combined with other operations under
2788 * a single invocation of the mac_filter_hash_lock.
2789 *
2790 * NOTE: this function expects to be called while under the
2791 * mac_filter_hash_lock
2792 */
2793 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2794 {
2795 struct i40e_mac_filter *f;
2796 struct hlist_node *h;
2797 int bkt;
2798
2799 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2800 if (f->vlan == vid)
2801 __i40e_del_filter(vsi, f);
2802 }
2803 }
2804
2805 /**
2806 * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2807 * @vsi: the VSI being configured
2808 * @vid: VLAN id to be removed
2809 **/
2810 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
2811 {
2812 if (!vid || vsi->info.pvid)
2813 return;
2814
2815 spin_lock_bh(&vsi->mac_filter_hash_lock);
2816 i40e_rm_vlan_all_mac(vsi, vid);
2817 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2818
2819 /* schedule our worker thread which will take care of
2820 * applying the new filter changes
2821 */
2822 i40e_service_event_schedule(vsi->back);
2823 }
2824
2825 /**
2826 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2827 * @netdev: network interface to be adjusted
2828 * @proto: unused protocol value
2829 * @vid: vlan id to be added
2830 *
2831 * net_device_ops implementation for adding vlan ids
2832 **/
2833 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2834 __always_unused __be16 proto, u16 vid)
2835 {
2836 struct i40e_netdev_priv *np = netdev_priv(netdev);
2837 struct i40e_vsi *vsi = np->vsi;
2838 int ret = 0;
2839
2840 if (vid >= VLAN_N_VID)
2841 return -EINVAL;
2842
2843 ret = i40e_vsi_add_vlan(vsi, vid);
2844 if (!ret)
2845 set_bit(vid, vsi->active_vlans);
2846
2847 return ret;
2848 }
2849
2850 /**
2851 * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
2852 * @netdev: network interface to be adjusted
2853 * @proto: unused protocol value
2854 * @vid: vlan id to be added
2855 **/
2856 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
2857 __always_unused __be16 proto, u16 vid)
2858 {
2859 struct i40e_netdev_priv *np = netdev_priv(netdev);
2860 struct i40e_vsi *vsi = np->vsi;
2861
2862 if (vid >= VLAN_N_VID)
2863 return;
2864 set_bit(vid, vsi->active_vlans);
2865 }
2866
2867 /**
2868 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2869 * @netdev: network interface to be adjusted
2870 * @proto: unused protocol value
2871 * @vid: vlan id to be removed
2872 *
2873 * net_device_ops implementation for removing vlan ids
2874 **/
2875 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2876 __always_unused __be16 proto, u16 vid)
2877 {
2878 struct i40e_netdev_priv *np = netdev_priv(netdev);
2879 struct i40e_vsi *vsi = np->vsi;
2880
2881 /* return code is ignored as there is nothing a user
2882 * can do about failure to remove and a log message was
2883 * already printed from the other function
2884 */
2885 i40e_vsi_kill_vlan(vsi, vid);
2886
2887 clear_bit(vid, vsi->active_vlans);
2888
2889 return 0;
2890 }
2891
2892 /**
2893 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2894 * @vsi: the vsi being brought back up
2895 **/
2896 static void i40e_restore_vlan(struct i40e_vsi *vsi)
2897 {
2898 u16 vid;
2899
2900 if (!vsi->netdev)
2901 return;
2902
2903 if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
2904 i40e_vlan_stripping_enable(vsi);
2905 else
2906 i40e_vlan_stripping_disable(vsi);
2907
2908 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2909 i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
2910 vid);
2911 }
2912
2913 /**
2914 * i40e_vsi_add_pvid - Add pvid for the VSI
2915 * @vsi: the vsi being adjusted
2916 * @vid: the vlan id to set as a PVID
2917 **/
2918 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
2919 {
2920 struct i40e_vsi_context ctxt;
2921 i40e_status ret;
2922
2923 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2924 vsi->info.pvid = cpu_to_le16(vid);
2925 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2926 I40E_AQ_VSI_PVLAN_INSERT_PVID |
2927 I40E_AQ_VSI_PVLAN_EMOD_STR;
2928
2929 ctxt.seid = vsi->seid;
2930 ctxt.info = vsi->info;
2931 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2932 if (ret) {
2933 dev_info(&vsi->back->pdev->dev,
2934 "add pvid failed, err %s aq_err %s\n",
2935 i40e_stat_str(&vsi->back->hw, ret),
2936 i40e_aq_str(&vsi->back->hw,
2937 vsi->back->hw.aq.asq_last_status));
2938 return -ENOENT;
2939 }
2940
2941 return 0;
2942 }
2943
2944 /**
2945 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2946 * @vsi: the vsi being adjusted
2947 *
2948 * Just use the vlan_rx_register() service to put it back to normal
2949 **/
2950 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2951 {
2952 i40e_vlan_stripping_disable(vsi);
2953
2954 vsi->info.pvid = 0;
2955 }
2956
2957 /**
2958 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2959 * @vsi: ptr to the VSI
2960 *
2961 * If this function returns with an error, then it's possible one or
2962 * more of the rings is populated (while the rest are not). It is the
2963 * callers duty to clean those orphaned rings.
2964 *
2965 * Return 0 on success, negative on failure
2966 **/
2967 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2968 {
2969 int i, err = 0;
2970
2971 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
2972 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
2973
2974 if (!i40e_enabled_xdp_vsi(vsi))
2975 return err;
2976
2977 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
2978 err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
2979
2980 return err;
2981 }
2982
2983 /**
2984 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2985 * @vsi: ptr to the VSI
2986 *
2987 * Free VSI's transmit software resources
2988 **/
2989 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2990 {
2991 int i;
2992
2993 if (vsi->tx_rings) {
2994 for (i = 0; i < vsi->num_queue_pairs; i++)
2995 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2996 i40e_free_tx_resources(vsi->tx_rings[i]);
2997 }
2998
2999 if (vsi->xdp_rings) {
3000 for (i = 0; i < vsi->num_queue_pairs; i++)
3001 if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3002 i40e_free_tx_resources(vsi->xdp_rings[i]);
3003 }
3004 }
3005
3006 /**
3007 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3008 * @vsi: ptr to the VSI
3009 *
3010 * If this function returns with an error, then it's possible one or
3011 * more of the rings is populated (while the rest are not). It is the
3012 * callers duty to clean those orphaned rings.
3013 *
3014 * Return 0 on success, negative on failure
3015 **/
3016 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3017 {
3018 int i, err = 0;
3019
3020 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3021 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3022 return err;
3023 }
3024
3025 /**
3026 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3027 * @vsi: ptr to the VSI
3028 *
3029 * Free all receive software resources
3030 **/
3031 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3032 {
3033 int i;
3034
3035 if (!vsi->rx_rings)
3036 return;
3037
3038 for (i = 0; i < vsi->num_queue_pairs; i++)
3039 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3040 i40e_free_rx_resources(vsi->rx_rings[i]);
3041 }
3042
3043 /**
3044 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3045 * @ring: The Tx ring to configure
3046 *
3047 * This enables/disables XPS for a given Tx descriptor ring
3048 * based on the TCs enabled for the VSI that ring belongs to.
3049 **/
3050 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3051 {
3052 int cpu;
3053
3054 if (!ring->q_vector || !ring->netdev || ring->ch)
3055 return;
3056
3057 /* We only initialize XPS once, so as not to overwrite user settings */
3058 if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3059 return;
3060
3061 cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3062 netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3063 ring->queue_index);
3064 }
3065
3066 /**
3067 * i40e_configure_tx_ring - Configure a transmit ring context and rest
3068 * @ring: The Tx ring to configure
3069 *
3070 * Configure the Tx descriptor ring in the HMC context.
3071 **/
3072 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3073 {
3074 struct i40e_vsi *vsi = ring->vsi;
3075 u16 pf_q = vsi->base_queue + ring->queue_index;
3076 struct i40e_hw *hw = &vsi->back->hw;
3077 struct i40e_hmc_obj_txq tx_ctx;
3078 i40e_status err = 0;
3079 u32 qtx_ctl = 0;
3080
3081 /* some ATR related tx ring init */
3082 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3083 ring->atr_sample_rate = vsi->back->atr_sample_rate;
3084 ring->atr_count = 0;
3085 } else {
3086 ring->atr_sample_rate = 0;
3087 }
3088
3089 /* configure XPS */
3090 i40e_config_xps_tx_ring(ring);
3091
3092 /* clear the context structure first */
3093 memset(&tx_ctx, 0, sizeof(tx_ctx));
3094
3095 tx_ctx.new_context = 1;
3096 tx_ctx.base = (ring->dma / 128);
3097 tx_ctx.qlen = ring->count;
3098 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3099 I40E_FLAG_FD_ATR_ENABLED));
3100 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3101 /* FDIR VSI tx ring can still use RS bit and writebacks */
3102 if (vsi->type != I40E_VSI_FDIR)
3103 tx_ctx.head_wb_ena = 1;
3104 tx_ctx.head_wb_addr = ring->dma +
3105 (ring->count * sizeof(struct i40e_tx_desc));
3106
3107 /* As part of VSI creation/update, FW allocates certain
3108 * Tx arbitration queue sets for each TC enabled for
3109 * the VSI. The FW returns the handles to these queue
3110 * sets as part of the response buffer to Add VSI,
3111 * Update VSI, etc. AQ commands. It is expected that
3112 * these queue set handles be associated with the Tx
3113 * queues by the driver as part of the TX queue context
3114 * initialization. This has to be done regardless of
3115 * DCB as by default everything is mapped to TC0.
3116 */
3117
3118 if (ring->ch)
3119 tx_ctx.rdylist =
3120 le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3121
3122 else
3123 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3124
3125 tx_ctx.rdylist_act = 0;
3126
3127 /* clear the context in the HMC */
3128 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3129 if (err) {
3130 dev_info(&vsi->back->pdev->dev,
3131 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3132 ring->queue_index, pf_q, err);
3133 return -ENOMEM;
3134 }
3135
3136 /* set the context in the HMC */
3137 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3138 if (err) {
3139 dev_info(&vsi->back->pdev->dev,
3140 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3141 ring->queue_index, pf_q, err);
3142 return -ENOMEM;
3143 }
3144
3145 /* Now associate this queue with this PCI function */
3146 if (ring->ch) {
3147 if (ring->ch->type == I40E_VSI_VMDQ2)
3148 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3149 else
3150 return -EINVAL;
3151
3152 qtx_ctl |= (ring->ch->vsi_number <<
3153 I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3154 I40E_QTX_CTL_VFVM_INDX_MASK;
3155 } else {
3156 if (vsi->type == I40E_VSI_VMDQ2) {
3157 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3158 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3159 I40E_QTX_CTL_VFVM_INDX_MASK;
3160 } else {
3161 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3162 }
3163 }
3164
3165 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3166 I40E_QTX_CTL_PF_INDX_MASK);
3167 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3168 i40e_flush(hw);
3169
3170 /* cache tail off for easier writes later */
3171 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3172
3173 return 0;
3174 }
3175
3176 /**
3177 * i40e_configure_rx_ring - Configure a receive ring context
3178 * @ring: The Rx ring to configure
3179 *
3180 * Configure the Rx descriptor ring in the HMC context.
3181 **/
3182 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3183 {
3184 struct i40e_vsi *vsi = ring->vsi;
3185 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3186 u16 pf_q = vsi->base_queue + ring->queue_index;
3187 struct i40e_hw *hw = &vsi->back->hw;
3188 struct i40e_hmc_obj_rxq rx_ctx;
3189 i40e_status err = 0;
3190
3191 bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3192
3193 /* clear the context structure first */
3194 memset(&rx_ctx, 0, sizeof(rx_ctx));
3195
3196 ring->rx_buf_len = vsi->rx_buf_len;
3197
3198 rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3199 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3200
3201 rx_ctx.base = (ring->dma / 128);
3202 rx_ctx.qlen = ring->count;
3203
3204 /* use 32 byte descriptors */
3205 rx_ctx.dsize = 1;
3206
3207 /* descriptor type is always zero
3208 * rx_ctx.dtype = 0;
3209 */
3210 rx_ctx.hsplit_0 = 0;
3211
3212 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3213 if (hw->revision_id == 0)
3214 rx_ctx.lrxqthresh = 0;
3215 else
3216 rx_ctx.lrxqthresh = 1;
3217 rx_ctx.crcstrip = 1;
3218 rx_ctx.l2tsel = 1;
3219 /* this controls whether VLAN is stripped from inner headers */
3220 rx_ctx.showiv = 0;
3221 /* set the prefena field to 1 because the manual says to */
3222 rx_ctx.prefena = 1;
3223
3224 /* clear the context in the HMC */
3225 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3226 if (err) {
3227 dev_info(&vsi->back->pdev->dev,
3228 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3229 ring->queue_index, pf_q, err);
3230 return -ENOMEM;
3231 }
3232
3233 /* set the context in the HMC */
3234 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3235 if (err) {
3236 dev_info(&vsi->back->pdev->dev,
3237 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3238 ring->queue_index, pf_q, err);
3239 return -ENOMEM;
3240 }
3241
3242 /* configure Rx buffer alignment */
3243 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3244 clear_ring_build_skb_enabled(ring);
3245 else
3246 set_ring_build_skb_enabled(ring);
3247
3248 /* cache tail for quicker writes, and clear the reg before use */
3249 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3250 writel(0, ring->tail);
3251
3252 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3253
3254 return 0;
3255 }
3256
3257 /**
3258 * i40e_vsi_configure_tx - Configure the VSI for Tx
3259 * @vsi: VSI structure describing this set of rings and resources
3260 *
3261 * Configure the Tx VSI for operation.
3262 **/
3263 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3264 {
3265 int err = 0;
3266 u16 i;
3267
3268 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3269 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3270
3271 if (!i40e_enabled_xdp_vsi(vsi))
3272 return err;
3273
3274 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3275 err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3276
3277 return err;
3278 }
3279
3280 /**
3281 * i40e_vsi_configure_rx - Configure the VSI for Rx
3282 * @vsi: the VSI being configured
3283 *
3284 * Configure the Rx VSI for operation.
3285 **/
3286 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3287 {
3288 int err = 0;
3289 u16 i;
3290
3291 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3292 vsi->max_frame = I40E_MAX_RXBUFFER;
3293 vsi->rx_buf_len = I40E_RXBUFFER_2048;
3294 #if (PAGE_SIZE < 8192)
3295 } else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3296 (vsi->netdev->mtu <= ETH_DATA_LEN)) {
3297 vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3298 vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3299 #endif
3300 } else {
3301 vsi->max_frame = I40E_MAX_RXBUFFER;
3302 vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3303 I40E_RXBUFFER_2048;
3304 }
3305
3306 /* set up individual rings */
3307 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3308 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3309
3310 return err;
3311 }
3312
3313 /**
3314 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3315 * @vsi: ptr to the VSI
3316 **/
3317 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3318 {
3319 struct i40e_ring *tx_ring, *rx_ring;
3320 u16 qoffset, qcount;
3321 int i, n;
3322
3323 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3324 /* Reset the TC information */
3325 for (i = 0; i < vsi->num_queue_pairs; i++) {
3326 rx_ring = vsi->rx_rings[i];
3327 tx_ring = vsi->tx_rings[i];
3328 rx_ring->dcb_tc = 0;
3329 tx_ring->dcb_tc = 0;
3330 }
3331 return;
3332 }
3333
3334 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3335 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3336 continue;
3337
3338 qoffset = vsi->tc_config.tc_info[n].qoffset;
3339 qcount = vsi->tc_config.tc_info[n].qcount;
3340 for (i = qoffset; i < (qoffset + qcount); i++) {
3341 rx_ring = vsi->rx_rings[i];
3342 tx_ring = vsi->tx_rings[i];
3343 rx_ring->dcb_tc = n;
3344 tx_ring->dcb_tc = n;
3345 }
3346 }
3347 }
3348
3349 /**
3350 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3351 * @vsi: ptr to the VSI
3352 **/
3353 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3354 {
3355 if (vsi->netdev)
3356 i40e_set_rx_mode(vsi->netdev);
3357 }
3358
3359 /**
3360 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3361 * @vsi: Pointer to the targeted VSI
3362 *
3363 * This function replays the hlist on the hw where all the SB Flow Director
3364 * filters were saved.
3365 **/
3366 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3367 {
3368 struct i40e_fdir_filter *filter;
3369 struct i40e_pf *pf = vsi->back;
3370 struct hlist_node *node;
3371
3372 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3373 return;
3374
3375 /* Reset FDir counters as we're replaying all existing filters */
3376 pf->fd_tcp4_filter_cnt = 0;
3377 pf->fd_udp4_filter_cnt = 0;
3378 pf->fd_sctp4_filter_cnt = 0;
3379 pf->fd_ip4_filter_cnt = 0;
3380
3381 hlist_for_each_entry_safe(filter, node,
3382 &pf->fdir_filter_list, fdir_node) {
3383 i40e_add_del_fdir(vsi, filter, true);
3384 }
3385 }
3386
3387 /**
3388 * i40e_vsi_configure - Set up the VSI for action
3389 * @vsi: the VSI being configured
3390 **/
3391 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3392 {
3393 int err;
3394
3395 i40e_set_vsi_rx_mode(vsi);
3396 i40e_restore_vlan(vsi);
3397 i40e_vsi_config_dcb_rings(vsi);
3398 err = i40e_vsi_configure_tx(vsi);
3399 if (!err)
3400 err = i40e_vsi_configure_rx(vsi);
3401
3402 return err;
3403 }
3404
3405 /**
3406 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3407 * @vsi: the VSI being configured
3408 **/
3409 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3410 {
3411 bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3412 struct i40e_pf *pf = vsi->back;
3413 struct i40e_hw *hw = &pf->hw;
3414 u16 vector;
3415 int i, q;
3416 u32 qp;
3417
3418 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3419 * and PFINT_LNKLSTn registers, e.g.:
3420 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3421 */
3422 qp = vsi->base_queue;
3423 vector = vsi->base_vector;
3424 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3425 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3426
3427 q_vector->rx.next_update = jiffies + 1;
3428 q_vector->rx.target_itr =
3429 ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3430 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3431 q_vector->rx.target_itr);
3432 q_vector->rx.current_itr = q_vector->rx.target_itr;
3433
3434 q_vector->tx.next_update = jiffies + 1;
3435 q_vector->tx.target_itr =
3436 ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3437 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3438 q_vector->tx.target_itr);
3439 q_vector->tx.current_itr = q_vector->tx.target_itr;
3440
3441 wr32(hw, I40E_PFINT_RATEN(vector - 1),
3442 i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3443
3444 /* Linked list for the queuepairs assigned to this vector */
3445 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3446 for (q = 0; q < q_vector->num_ringpairs; q++) {
3447 u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3448 u32 val;
3449
3450 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3451 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3452 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3453 (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3454 (I40E_QUEUE_TYPE_TX <<
3455 I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3456
3457 wr32(hw, I40E_QINT_RQCTL(qp), val);
3458
3459 if (has_xdp) {
3460 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3461 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3462 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3463 (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3464 (I40E_QUEUE_TYPE_TX <<
3465 I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3466
3467 wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3468 }
3469
3470 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3471 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3472 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3473 ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3474 (I40E_QUEUE_TYPE_RX <<
3475 I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3476
3477 /* Terminate the linked list */
3478 if (q == (q_vector->num_ringpairs - 1))
3479 val |= (I40E_QUEUE_END_OF_LIST <<
3480 I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3481
3482 wr32(hw, I40E_QINT_TQCTL(qp), val);
3483 qp++;
3484 }
3485 }
3486
3487 i40e_flush(hw);
3488 }
3489
3490 /**
3491 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3492 * @pf: pointer to private device data structure
3493 **/
3494 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3495 {
3496 struct i40e_hw *hw = &pf->hw;
3497 u32 val;
3498
3499 /* clear things first */
3500 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3501 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3502
3503 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3504 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3505 I40E_PFINT_ICR0_ENA_GRST_MASK |
3506 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3507 I40E_PFINT_ICR0_ENA_GPIO_MASK |
3508 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3509 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3510 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3511
3512 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3513 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3514
3515 if (pf->flags & I40E_FLAG_PTP)
3516 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3517
3518 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3519
3520 /* SW_ITR_IDX = 0, but don't change INTENA */
3521 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3522 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3523
3524 /* OTHER_ITR_IDX = 0 */
3525 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3526 }
3527
3528 /**
3529 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3530 * @vsi: the VSI being configured
3531 **/
3532 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3533 {
3534 u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3535 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3536 struct i40e_pf *pf = vsi->back;
3537 struct i40e_hw *hw = &pf->hw;
3538 u32 val;
3539
3540 /* set the ITR configuration */
3541 q_vector->rx.next_update = jiffies + 1;
3542 q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3543 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr);
3544 q_vector->rx.current_itr = q_vector->rx.target_itr;
3545 q_vector->tx.next_update = jiffies + 1;
3546 q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3547 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr);
3548 q_vector->tx.current_itr = q_vector->tx.target_itr;
3549
3550 i40e_enable_misc_int_causes(pf);
3551
3552 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3553 wr32(hw, I40E_PFINT_LNKLST0, 0);
3554
3555 /* Associate the queue pair to the vector and enable the queue int */
3556 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3557 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3558 (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3559 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3560
3561 wr32(hw, I40E_QINT_RQCTL(0), val);
3562
3563 if (i40e_enabled_xdp_vsi(vsi)) {
3564 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3565 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3566 (I40E_QUEUE_TYPE_TX
3567 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3568
3569 wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3570 }
3571
3572 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3573 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3574 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3575
3576 wr32(hw, I40E_QINT_TQCTL(0), val);
3577 i40e_flush(hw);
3578 }
3579
3580 /**
3581 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3582 * @pf: board private structure
3583 **/
3584 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3585 {
3586 struct i40e_hw *hw = &pf->hw;
3587
3588 wr32(hw, I40E_PFINT_DYN_CTL0,
3589 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3590 i40e_flush(hw);
3591 }
3592
3593 /**
3594 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3595 * @pf: board private structure
3596 **/
3597 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
3598 {
3599 struct i40e_hw *hw = &pf->hw;
3600 u32 val;
3601
3602 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
3603 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
3604 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3605
3606 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3607 i40e_flush(hw);
3608 }
3609
3610 /**
3611 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3612 * @irq: interrupt number
3613 * @data: pointer to a q_vector
3614 **/
3615 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3616 {
3617 struct i40e_q_vector *q_vector = data;
3618
3619 if (!q_vector->tx.ring && !q_vector->rx.ring)
3620 return IRQ_HANDLED;
3621
3622 napi_schedule_irqoff(&q_vector->napi);
3623
3624 return IRQ_HANDLED;
3625 }
3626
3627 /**
3628 * i40e_irq_affinity_notify - Callback for affinity changes
3629 * @notify: context as to what irq was changed
3630 * @mask: the new affinity mask
3631 *
3632 * This is a callback function used by the irq_set_affinity_notifier function
3633 * so that we may register to receive changes to the irq affinity masks.
3634 **/
3635 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3636 const cpumask_t *mask)
3637 {
3638 struct i40e_q_vector *q_vector =
3639 container_of(notify, struct i40e_q_vector, affinity_notify);
3640
3641 cpumask_copy(&q_vector->affinity_mask, mask);
3642 }
3643
3644 /**
3645 * i40e_irq_affinity_release - Callback for affinity notifier release
3646 * @ref: internal core kernel usage
3647 *
3648 * This is a callback function used by the irq_set_affinity_notifier function
3649 * to inform the current notification subscriber that they will no longer
3650 * receive notifications.
3651 **/
3652 static void i40e_irq_affinity_release(struct kref *ref) {}
3653
3654 /**
3655 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3656 * @vsi: the VSI being configured
3657 * @basename: name for the vector
3658 *
3659 * Allocates MSI-X vectors and requests interrupts from the kernel.
3660 **/
3661 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3662 {
3663 int q_vectors = vsi->num_q_vectors;
3664 struct i40e_pf *pf = vsi->back;
3665 int base = vsi->base_vector;
3666 int rx_int_idx = 0;
3667 int tx_int_idx = 0;
3668 int vector, err;
3669 int irq_num;
3670 int cpu;
3671
3672 for (vector = 0; vector < q_vectors; vector++) {
3673 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
3674
3675 irq_num = pf->msix_entries[base + vector].vector;
3676
3677 if (q_vector->tx.ring && q_vector->rx.ring) {
3678 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3679 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3680 tx_int_idx++;
3681 } else if (q_vector->rx.ring) {
3682 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3683 "%s-%s-%d", basename, "rx", rx_int_idx++);
3684 } else if (q_vector->tx.ring) {
3685 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3686 "%s-%s-%d", basename, "tx", tx_int_idx++);
3687 } else {
3688 /* skip this unused q_vector */
3689 continue;
3690 }
3691 err = request_irq(irq_num,
3692 vsi->irq_handler,
3693 0,
3694 q_vector->name,
3695 q_vector);
3696 if (err) {
3697 dev_info(&pf->pdev->dev,
3698 "MSIX request_irq failed, error: %d\n", err);
3699 goto free_queue_irqs;
3700 }
3701
3702 /* register for affinity change notifications */
3703 q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3704 q_vector->affinity_notify.release = i40e_irq_affinity_release;
3705 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
3706 /* Spread affinity hints out across online CPUs.
3707 *
3708 * get_cpu_mask returns a static constant mask with
3709 * a permanent lifetime so it's ok to pass to
3710 * irq_set_affinity_hint without making a copy.
3711 */
3712 cpu = cpumask_local_spread(q_vector->v_idx, -1);
3713 irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
3714 }
3715
3716 vsi->irqs_ready = true;
3717 return 0;
3718
3719 free_queue_irqs:
3720 while (vector) {
3721 vector--;
3722 irq_num = pf->msix_entries[base + vector].vector;
3723 irq_set_affinity_notifier(irq_num, NULL);
3724 irq_set_affinity_hint(irq_num, NULL);
3725 free_irq(irq_num, &vsi->q_vectors[vector]);
3726 }
3727 return err;
3728 }
3729
3730 /**
3731 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3732 * @vsi: the VSI being un-configured
3733 **/
3734 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3735 {
3736 struct i40e_pf *pf = vsi->back;
3737 struct i40e_hw *hw = &pf->hw;
3738 int base = vsi->base_vector;
3739 int i;
3740
3741 /* disable interrupt causation from each queue */
3742 for (i = 0; i < vsi->num_queue_pairs; i++) {
3743 u32 val;
3744
3745 val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
3746 val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3747 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
3748
3749 val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
3750 val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3751 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
3752
3753 if (!i40e_enabled_xdp_vsi(vsi))
3754 continue;
3755 wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
3756 }
3757
3758 /* disable each interrupt */
3759 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3760 for (i = vsi->base_vector;
3761 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3762 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3763
3764 i40e_flush(hw);
3765 for (i = 0; i < vsi->num_q_vectors; i++)
3766 synchronize_irq(pf->msix_entries[i + base].vector);
3767 } else {
3768 /* Legacy and MSI mode - this stops all interrupt handling */
3769 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3770 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3771 i40e_flush(hw);
3772 synchronize_irq(pf->pdev->irq);
3773 }
3774 }
3775
3776 /**
3777 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3778 * @vsi: the VSI being configured
3779 **/
3780 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3781 {
3782 struct i40e_pf *pf = vsi->back;
3783 int i;
3784
3785 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3786 for (i = 0; i < vsi->num_q_vectors; i++)
3787 i40e_irq_dynamic_enable(vsi, i);
3788 } else {
3789 i40e_irq_dynamic_enable_icr0(pf);
3790 }
3791
3792 i40e_flush(&pf->hw);
3793 return 0;
3794 }
3795
3796 /**
3797 * i40e_free_misc_vector - Free the vector that handles non-queue events
3798 * @pf: board private structure
3799 **/
3800 static void i40e_free_misc_vector(struct i40e_pf *pf)
3801 {
3802 /* Disable ICR 0 */
3803 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3804 i40e_flush(&pf->hw);
3805
3806 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
3807 synchronize_irq(pf->msix_entries[0].vector);
3808 free_irq(pf->msix_entries[0].vector, pf);
3809 clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
3810 }
3811 }
3812
3813 /**
3814 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3815 * @irq: interrupt number
3816 * @data: pointer to a q_vector
3817 *
3818 * This is the handler used for all MSI/Legacy interrupts, and deals
3819 * with both queue and non-queue interrupts. This is also used in
3820 * MSIX mode to handle the non-queue interrupts.
3821 **/
3822 static irqreturn_t i40e_intr(int irq, void *data)
3823 {
3824 struct i40e_pf *pf = (struct i40e_pf *)data;
3825 struct i40e_hw *hw = &pf->hw;
3826 irqreturn_t ret = IRQ_NONE;
3827 u32 icr0, icr0_remaining;
3828 u32 val, ena_mask;
3829
3830 icr0 = rd32(hw, I40E_PFINT_ICR0);
3831 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
3832
3833 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3834 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
3835 goto enable_intr;
3836
3837 /* if interrupt but no bits showing, must be SWINT */
3838 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3839 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3840 pf->sw_int_count++;
3841
3842 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3843 (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3844 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3845 dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
3846 set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
3847 }
3848
3849 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3850 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
3851 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3852 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3853
3854 /* We do not have a way to disarm Queue causes while leaving
3855 * interrupt enabled for all other causes, ideally
3856 * interrupt should be disabled while we are in NAPI but
3857 * this is not a performance path and napi_schedule()
3858 * can deal with rescheduling.
3859 */
3860 if (!test_bit(__I40E_DOWN, pf->state))
3861 napi_schedule_irqoff(&q_vector->napi);
3862 }
3863
3864 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3865 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3866 set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
3867 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
3868 }
3869
3870 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3871 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3872 set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
3873 }
3874
3875 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3876 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3877 set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
3878 }
3879
3880 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3881 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
3882 set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
3883 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3884 val = rd32(hw, I40E_GLGEN_RSTAT);
3885 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3886 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
3887 if (val == I40E_RESET_CORER) {
3888 pf->corer_count++;
3889 } else if (val == I40E_RESET_GLOBR) {
3890 pf->globr_count++;
3891 } else if (val == I40E_RESET_EMPR) {
3892 pf->empr_count++;
3893 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
3894 }
3895 }
3896
3897 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3898 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3899 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
3900 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3901 rd32(hw, I40E_PFHMC_ERRORINFO),
3902 rd32(hw, I40E_PFHMC_ERRORDATA));
3903 }
3904
3905 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3906 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3907
3908 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
3909 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3910 i40e_ptp_tx_hwtstamp(pf);
3911 }
3912 }
3913
3914 /* If a critical error is pending we have no choice but to reset the
3915 * device.
3916 * Report and mask out any remaining unexpected interrupts.
3917 */
3918 icr0_remaining = icr0 & ena_mask;
3919 if (icr0_remaining) {
3920 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3921 icr0_remaining);
3922 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
3923 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
3924 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
3925 dev_info(&pf->pdev->dev, "device will be reset\n");
3926 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
3927 i40e_service_event_schedule(pf);
3928 }
3929 ena_mask &= ~icr0_remaining;
3930 }
3931 ret = IRQ_HANDLED;
3932
3933 enable_intr:
3934 /* re-enable interrupt causes */
3935 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
3936 if (!test_bit(__I40E_DOWN, pf->state)) {
3937 i40e_service_event_schedule(pf);
3938 i40e_irq_dynamic_enable_icr0(pf);
3939 }
3940
3941 return ret;
3942 }
3943
3944 /**
3945 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3946 * @tx_ring: tx ring to clean
3947 * @budget: how many cleans we're allowed
3948 *
3949 * Returns true if there's any budget left (e.g. the clean is finished)
3950 **/
3951 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3952 {
3953 struct i40e_vsi *vsi = tx_ring->vsi;
3954 u16 i = tx_ring->next_to_clean;
3955 struct i40e_tx_buffer *tx_buf;
3956 struct i40e_tx_desc *tx_desc;
3957
3958 tx_buf = &tx_ring->tx_bi[i];
3959 tx_desc = I40E_TX_DESC(tx_ring, i);
3960 i -= tx_ring->count;
3961
3962 do {
3963 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3964
3965 /* if next_to_watch is not set then there is no work pending */
3966 if (!eop_desc)
3967 break;
3968
3969 /* prevent any other reads prior to eop_desc */
3970 smp_rmb();
3971
3972 /* if the descriptor isn't done, no work yet to do */
3973 if (!(eop_desc->cmd_type_offset_bsz &
3974 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3975 break;
3976
3977 /* clear next_to_watch to prevent false hangs */
3978 tx_buf->next_to_watch = NULL;
3979
3980 tx_desc->buffer_addr = 0;
3981 tx_desc->cmd_type_offset_bsz = 0;
3982 /* move past filter desc */
3983 tx_buf++;
3984 tx_desc++;
3985 i++;
3986 if (unlikely(!i)) {
3987 i -= tx_ring->count;
3988 tx_buf = tx_ring->tx_bi;
3989 tx_desc = I40E_TX_DESC(tx_ring, 0);
3990 }
3991 /* unmap skb header data */
3992 dma_unmap_single(tx_ring->dev,
3993 dma_unmap_addr(tx_buf, dma),
3994 dma_unmap_len(tx_buf, len),
3995 DMA_TO_DEVICE);
3996 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3997 kfree(tx_buf->raw_buf);
3998
3999 tx_buf->raw_buf = NULL;
4000 tx_buf->tx_flags = 0;
4001 tx_buf->next_to_watch = NULL;
4002 dma_unmap_len_set(tx_buf, len, 0);
4003 tx_desc->buffer_addr = 0;
4004 tx_desc->cmd_type_offset_bsz = 0;
4005
4006 /* move us past the eop_desc for start of next FD desc */
4007 tx_buf++;
4008 tx_desc++;
4009 i++;
4010 if (unlikely(!i)) {
4011 i -= tx_ring->count;
4012 tx_buf = tx_ring->tx_bi;
4013 tx_desc = I40E_TX_DESC(tx_ring, 0);
4014 }
4015
4016 /* update budget accounting */
4017 budget--;
4018 } while (likely(budget));
4019
4020 i += tx_ring->count;
4021 tx_ring->next_to_clean = i;
4022
4023 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4024 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4025
4026 return budget > 0;
4027 }
4028
4029 /**
4030 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4031 * @irq: interrupt number
4032 * @data: pointer to a q_vector
4033 **/
4034 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4035 {
4036 struct i40e_q_vector *q_vector = data;
4037 struct i40e_vsi *vsi;
4038
4039 if (!q_vector->tx.ring)
4040 return IRQ_HANDLED;
4041
4042 vsi = q_vector->tx.ring->vsi;
4043 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4044
4045 return IRQ_HANDLED;
4046 }
4047
4048 /**
4049 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4050 * @vsi: the VSI being configured
4051 * @v_idx: vector index
4052 * @qp_idx: queue pair index
4053 **/
4054 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4055 {
4056 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4057 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4058 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4059
4060 tx_ring->q_vector = q_vector;
4061 tx_ring->next = q_vector->tx.ring;
4062 q_vector->tx.ring = tx_ring;
4063 q_vector->tx.count++;
4064
4065 /* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4066 if (i40e_enabled_xdp_vsi(vsi)) {
4067 struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4068
4069 xdp_ring->q_vector = q_vector;
4070 xdp_ring->next = q_vector->tx.ring;
4071 q_vector->tx.ring = xdp_ring;
4072 q_vector->tx.count++;
4073 }
4074
4075 rx_ring->q_vector = q_vector;
4076 rx_ring->next = q_vector->rx.ring;
4077 q_vector->rx.ring = rx_ring;
4078 q_vector->rx.count++;
4079 }
4080
4081 /**
4082 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4083 * @vsi: the VSI being configured
4084 *
4085 * This function maps descriptor rings to the queue-specific vectors
4086 * we were allotted through the MSI-X enabling code. Ideally, we'd have
4087 * one vector per queue pair, but on a constrained vector budget, we
4088 * group the queue pairs as "efficiently" as possible.
4089 **/
4090 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4091 {
4092 int qp_remaining = vsi->num_queue_pairs;
4093 int q_vectors = vsi->num_q_vectors;
4094 int num_ringpairs;
4095 int v_start = 0;
4096 int qp_idx = 0;
4097
4098 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4099 * group them so there are multiple queues per vector.
4100 * It is also important to go through all the vectors available to be
4101 * sure that if we don't use all the vectors, that the remaining vectors
4102 * are cleared. This is especially important when decreasing the
4103 * number of queues in use.
4104 */
4105 for (; v_start < q_vectors; v_start++) {
4106 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4107
4108 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4109
4110 q_vector->num_ringpairs = num_ringpairs;
4111 q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4112
4113 q_vector->rx.count = 0;
4114 q_vector->tx.count = 0;
4115 q_vector->rx.ring = NULL;
4116 q_vector->tx.ring = NULL;
4117
4118 while (num_ringpairs--) {
4119 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4120 qp_idx++;
4121 qp_remaining--;
4122 }
4123 }
4124 }
4125
4126 /**
4127 * i40e_vsi_request_irq - Request IRQ from the OS
4128 * @vsi: the VSI being configured
4129 * @basename: name for the vector
4130 **/
4131 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4132 {
4133 struct i40e_pf *pf = vsi->back;
4134 int err;
4135
4136 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4137 err = i40e_vsi_request_irq_msix(vsi, basename);
4138 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4139 err = request_irq(pf->pdev->irq, i40e_intr, 0,
4140 pf->int_name, pf);
4141 else
4142 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4143 pf->int_name, pf);
4144
4145 if (err)
4146 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4147
4148 return err;
4149 }
4150
4151 #ifdef CONFIG_NET_POLL_CONTROLLER
4152 /**
4153 * i40e_netpoll - A Polling 'interrupt' handler
4154 * @netdev: network interface device structure
4155 *
4156 * This is used by netconsole to send skbs without having to re-enable
4157 * interrupts. It's not called while the normal interrupt routine is executing.
4158 **/
4159 static void i40e_netpoll(struct net_device *netdev)
4160 {
4161 struct i40e_netdev_priv *np = netdev_priv(netdev);
4162 struct i40e_vsi *vsi = np->vsi;
4163 struct i40e_pf *pf = vsi->back;
4164 int i;
4165
4166 /* if interface is down do nothing */
4167 if (test_bit(__I40E_VSI_DOWN, vsi->state))
4168 return;
4169
4170 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4171 for (i = 0; i < vsi->num_q_vectors; i++)
4172 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4173 } else {
4174 i40e_intr(pf->pdev->irq, netdev);
4175 }
4176 }
4177 #endif
4178
4179 #define I40E_QTX_ENA_WAIT_COUNT 50
4180
4181 /**
4182 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4183 * @pf: the PF being configured
4184 * @pf_q: the PF queue
4185 * @enable: enable or disable state of the queue
4186 *
4187 * This routine will wait for the given Tx queue of the PF to reach the
4188 * enabled or disabled state.
4189 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4190 * multiple retries; else will return 0 in case of success.
4191 **/
4192 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4193 {
4194 int i;
4195 u32 tx_reg;
4196
4197 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4198 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4199 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4200 break;
4201
4202 usleep_range(10, 20);
4203 }
4204 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4205 return -ETIMEDOUT;
4206
4207 return 0;
4208 }
4209
4210 /**
4211 * i40e_control_tx_q - Start or stop a particular Tx queue
4212 * @pf: the PF structure
4213 * @pf_q: the PF queue to configure
4214 * @enable: start or stop the queue
4215 *
4216 * This function enables or disables a single queue. Note that any delay
4217 * required after the operation is expected to be handled by the caller of
4218 * this function.
4219 **/
4220 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4221 {
4222 struct i40e_hw *hw = &pf->hw;
4223 u32 tx_reg;
4224 int i;
4225
4226 /* warn the TX unit of coming changes */
4227 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4228 if (!enable)
4229 usleep_range(10, 20);
4230
4231 for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4232 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4233 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4234 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4235 break;
4236 usleep_range(1000, 2000);
4237 }
4238
4239 /* Skip if the queue is already in the requested state */
4240 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4241 return;
4242
4243 /* turn on/off the queue */
4244 if (enable) {
4245 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4246 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4247 } else {
4248 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4249 }
4250
4251 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4252 }
4253
4254 /**
4255 * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4256 * @seid: VSI SEID
4257 * @pf: the PF structure
4258 * @pf_q: the PF queue to configure
4259 * @is_xdp: true if the queue is used for XDP
4260 * @enable: start or stop the queue
4261 **/
4262 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4263 bool is_xdp, bool enable)
4264 {
4265 int ret;
4266
4267 i40e_control_tx_q(pf, pf_q, enable);
4268
4269 /* wait for the change to finish */
4270 ret = i40e_pf_txq_wait(pf, pf_q, enable);
4271 if (ret) {
4272 dev_info(&pf->pdev->dev,
4273 "VSI seid %d %sTx ring %d %sable timeout\n",
4274 seid, (is_xdp ? "XDP " : ""), pf_q,
4275 (enable ? "en" : "dis"));
4276 }
4277
4278 return ret;
4279 }
4280
4281 /**
4282 * i40e_vsi_control_tx - Start or stop a VSI's rings
4283 * @vsi: the VSI being configured
4284 * @enable: start or stop the rings
4285 **/
4286 static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
4287 {
4288 struct i40e_pf *pf = vsi->back;
4289 int i, pf_q, ret = 0;
4290
4291 pf_q = vsi->base_queue;
4292 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4293 ret = i40e_control_wait_tx_q(vsi->seid, pf,
4294 pf_q,
4295 false /*is xdp*/, enable);
4296 if (ret)
4297 break;
4298
4299 if (!i40e_enabled_xdp_vsi(vsi))
4300 continue;
4301
4302 ret = i40e_control_wait_tx_q(vsi->seid, pf,
4303 pf_q + vsi->alloc_queue_pairs,
4304 true /*is xdp*/, enable);
4305 if (ret)
4306 break;
4307 }
4308 return ret;
4309 }
4310
4311 /**
4312 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4313 * @pf: the PF being configured
4314 * @pf_q: the PF queue
4315 * @enable: enable or disable state of the queue
4316 *
4317 * This routine will wait for the given Rx queue of the PF to reach the
4318 * enabled or disabled state.
4319 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4320 * multiple retries; else will return 0 in case of success.
4321 **/
4322 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4323 {
4324 int i;
4325 u32 rx_reg;
4326
4327 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4328 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4329 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4330 break;
4331
4332 usleep_range(10, 20);
4333 }
4334 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4335 return -ETIMEDOUT;
4336
4337 return 0;
4338 }
4339
4340 /**
4341 * i40e_control_rx_q - Start or stop a particular Rx queue
4342 * @pf: the PF structure
4343 * @pf_q: the PF queue to configure
4344 * @enable: start or stop the queue
4345 *
4346 * This function enables or disables a single queue. Note that
4347 * any delay required after the operation is expected to be
4348 * handled by the caller of this function.
4349 **/
4350 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4351 {
4352 struct i40e_hw *hw = &pf->hw;
4353 u32 rx_reg;
4354 int i;
4355
4356 for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4357 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4358 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4359 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4360 break;
4361 usleep_range(1000, 2000);
4362 }
4363
4364 /* Skip if the queue is already in the requested state */
4365 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4366 return;
4367
4368 /* turn on/off the queue */
4369 if (enable)
4370 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4371 else
4372 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4373
4374 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4375 }
4376
4377 /**
4378 * i40e_control_wait_rx_q
4379 * @pf: the PF structure
4380 * @pf_q: queue being configured
4381 * @enable: start or stop the rings
4382 *
4383 * This function enables or disables a single queue along with waiting
4384 * for the change to finish. The caller of this function should handle
4385 * the delays needed in the case of disabling queues.
4386 **/
4387 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4388 {
4389 int ret = 0;
4390
4391 i40e_control_rx_q(pf, pf_q, enable);
4392
4393 /* wait for the change to finish */
4394 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4395 if (ret)
4396 return ret;
4397
4398 return ret;
4399 }
4400
4401 /**
4402 * i40e_vsi_control_rx - Start or stop a VSI's rings
4403 * @vsi: the VSI being configured
4404 * @enable: start or stop the rings
4405 **/
4406 static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4407 {
4408 struct i40e_pf *pf = vsi->back;
4409 int i, pf_q, ret = 0;
4410
4411 pf_q = vsi->base_queue;
4412 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4413 ret = i40e_control_wait_rx_q(pf, pf_q, enable);
4414 if (ret) {
4415 dev_info(&pf->pdev->dev,
4416 "VSI seid %d Rx ring %d %sable timeout\n",
4417 vsi->seid, pf_q, (enable ? "en" : "dis"));
4418 break;
4419 }
4420 }
4421
4422 /* Due to HW errata, on Rx disable only, the register can indicate done
4423 * before it really is. Needs 50ms to be sure
4424 */
4425 if (!enable)
4426 mdelay(50);
4427
4428 return ret;
4429 }
4430
4431 /**
4432 * i40e_vsi_start_rings - Start a VSI's rings
4433 * @vsi: the VSI being configured
4434 **/
4435 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4436 {
4437 int ret = 0;
4438
4439 /* do rx first for enable and last for disable */
4440 ret = i40e_vsi_control_rx(vsi, true);
4441 if (ret)
4442 return ret;
4443 ret = i40e_vsi_control_tx(vsi, true);
4444
4445 return ret;
4446 }
4447
4448 /**
4449 * i40e_vsi_stop_rings - Stop a VSI's rings
4450 * @vsi: the VSI being configured
4451 **/
4452 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4453 {
4454 /* When port TX is suspended, don't wait */
4455 if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4456 return i40e_vsi_stop_rings_no_wait(vsi);
4457
4458 /* do rx first for enable and last for disable
4459 * Ignore return value, we need to shutdown whatever we can
4460 */
4461 i40e_vsi_control_tx(vsi, false);
4462 i40e_vsi_control_rx(vsi, false);
4463 }
4464
4465 /**
4466 * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4467 * @vsi: the VSI being shutdown
4468 *
4469 * This function stops all the rings for a VSI but does not delay to verify
4470 * that rings have been disabled. It is expected that the caller is shutting
4471 * down multiple VSIs at once and will delay together for all the VSIs after
4472 * initiating the shutdown. This is particularly useful for shutting down lots
4473 * of VFs together. Otherwise, a large delay can be incurred while configuring
4474 * each VSI in serial.
4475 **/
4476 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4477 {
4478 struct i40e_pf *pf = vsi->back;
4479 int i, pf_q;
4480
4481 pf_q = vsi->base_queue;
4482 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4483 i40e_control_tx_q(pf, pf_q, false);
4484 i40e_control_rx_q(pf, pf_q, false);
4485 }
4486 }
4487
4488 /**
4489 * i40e_vsi_free_irq - Free the irq association with the OS
4490 * @vsi: the VSI being configured
4491 **/
4492 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4493 {
4494 struct i40e_pf *pf = vsi->back;
4495 struct i40e_hw *hw = &pf->hw;
4496 int base = vsi->base_vector;
4497 u32 val, qp;
4498 int i;
4499
4500 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4501 if (!vsi->q_vectors)
4502 return;
4503
4504 if (!vsi->irqs_ready)
4505 return;
4506
4507 vsi->irqs_ready = false;
4508 for (i = 0; i < vsi->num_q_vectors; i++) {
4509 int irq_num;
4510 u16 vector;
4511
4512 vector = i + base;
4513 irq_num = pf->msix_entries[vector].vector;
4514
4515 /* free only the irqs that were actually requested */
4516 if (!vsi->q_vectors[i] ||
4517 !vsi->q_vectors[i]->num_ringpairs)
4518 continue;
4519
4520 /* clear the affinity notifier in the IRQ descriptor */
4521 irq_set_affinity_notifier(irq_num, NULL);
4522 /* remove our suggested affinity mask for this IRQ */
4523 irq_set_affinity_hint(irq_num, NULL);
4524 synchronize_irq(irq_num);
4525 free_irq(irq_num, vsi->q_vectors[i]);
4526
4527 /* Tear down the interrupt queue link list
4528 *
4529 * We know that they come in pairs and always
4530 * the Rx first, then the Tx. To clear the
4531 * link list, stick the EOL value into the
4532 * next_q field of the registers.
4533 */
4534 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4535 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4536 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4537 val |= I40E_QUEUE_END_OF_LIST
4538 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4539 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4540
4541 while (qp != I40E_QUEUE_END_OF_LIST) {
4542 u32 next;
4543
4544 val = rd32(hw, I40E_QINT_RQCTL(qp));
4545
4546 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4547 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4548 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4549 I40E_QINT_RQCTL_INTEVENT_MASK);
4550
4551 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4552 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4553
4554 wr32(hw, I40E_QINT_RQCTL(qp), val);
4555
4556 val = rd32(hw, I40E_QINT_TQCTL(qp));
4557
4558 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4559 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4560
4561 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4562 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4563 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4564 I40E_QINT_TQCTL_INTEVENT_MASK);
4565
4566 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4567 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4568
4569 wr32(hw, I40E_QINT_TQCTL(qp), val);
4570 qp = next;
4571 }
4572 }
4573 } else {
4574 free_irq(pf->pdev->irq, pf);
4575
4576 val = rd32(hw, I40E_PFINT_LNKLST0);
4577 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4578 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4579 val |= I40E_QUEUE_END_OF_LIST
4580 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4581 wr32(hw, I40E_PFINT_LNKLST0, val);
4582
4583 val = rd32(hw, I40E_QINT_RQCTL(qp));
4584 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4585 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4586 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4587 I40E_QINT_RQCTL_INTEVENT_MASK);
4588
4589 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4590 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4591
4592 wr32(hw, I40E_QINT_RQCTL(qp), val);
4593
4594 val = rd32(hw, I40E_QINT_TQCTL(qp));
4595
4596 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4597 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4598 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4599 I40E_QINT_TQCTL_INTEVENT_MASK);
4600
4601 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4602 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4603
4604 wr32(hw, I40E_QINT_TQCTL(qp), val);
4605 }
4606 }
4607
4608 /**
4609 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4610 * @vsi: the VSI being configured
4611 * @v_idx: Index of vector to be freed
4612 *
4613 * This function frees the memory allocated to the q_vector. In addition if
4614 * NAPI is enabled it will delete any references to the NAPI struct prior
4615 * to freeing the q_vector.
4616 **/
4617 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4618 {
4619 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4620 struct i40e_ring *ring;
4621
4622 if (!q_vector)
4623 return;
4624
4625 /* disassociate q_vector from rings */
4626 i40e_for_each_ring(ring, q_vector->tx)
4627 ring->q_vector = NULL;
4628
4629 i40e_for_each_ring(ring, q_vector->rx)
4630 ring->q_vector = NULL;
4631
4632 /* only VSI w/ an associated netdev is set up w/ NAPI */
4633 if (vsi->netdev)
4634 netif_napi_del(&q_vector->napi);
4635
4636 vsi->q_vectors[v_idx] = NULL;
4637
4638 kfree_rcu(q_vector, rcu);
4639 }
4640
4641 /**
4642 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4643 * @vsi: the VSI being un-configured
4644 *
4645 * This frees the memory allocated to the q_vectors and
4646 * deletes references to the NAPI struct.
4647 **/
4648 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4649 {
4650 int v_idx;
4651
4652 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4653 i40e_free_q_vector(vsi, v_idx);
4654 }
4655
4656 /**
4657 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4658 * @pf: board private structure
4659 **/
4660 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4661 {
4662 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4663 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4664 pci_disable_msix(pf->pdev);
4665 kfree(pf->msix_entries);
4666 pf->msix_entries = NULL;
4667 kfree(pf->irq_pile);
4668 pf->irq_pile = NULL;
4669 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4670 pci_disable_msi(pf->pdev);
4671 }
4672 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4673 }
4674
4675 /**
4676 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4677 * @pf: board private structure
4678 *
4679 * We go through and clear interrupt specific resources and reset the structure
4680 * to pre-load conditions
4681 **/
4682 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4683 {
4684 int i;
4685
4686 i40e_free_misc_vector(pf);
4687
4688 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4689 I40E_IWARP_IRQ_PILE_ID);
4690
4691 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
4692 for (i = 0; i < pf->num_alloc_vsi; i++)
4693 if (pf->vsi[i])
4694 i40e_vsi_free_q_vectors(pf->vsi[i]);
4695 i40e_reset_interrupt_capability(pf);
4696 }
4697
4698 /**
4699 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4700 * @vsi: the VSI being configured
4701 **/
4702 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4703 {
4704 int q_idx;
4705
4706 if (!vsi->netdev)
4707 return;
4708
4709 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4710 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4711
4712 if (q_vector->rx.ring || q_vector->tx.ring)
4713 napi_enable(&q_vector->napi);
4714 }
4715 }
4716
4717 /**
4718 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4719 * @vsi: the VSI being configured
4720 **/
4721 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4722 {
4723 int q_idx;
4724
4725 if (!vsi->netdev)
4726 return;
4727
4728 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4729 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4730
4731 if (q_vector->rx.ring || q_vector->tx.ring)
4732 napi_disable(&q_vector->napi);
4733 }
4734 }
4735
4736 /**
4737 * i40e_vsi_close - Shut down a VSI
4738 * @vsi: the vsi to be quelled
4739 **/
4740 static void i40e_vsi_close(struct i40e_vsi *vsi)
4741 {
4742 struct i40e_pf *pf = vsi->back;
4743 if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
4744 i40e_down(vsi);
4745 i40e_vsi_free_irq(vsi);
4746 i40e_vsi_free_tx_resources(vsi);
4747 i40e_vsi_free_rx_resources(vsi);
4748 vsi->current_netdev_flags = 0;
4749 set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
4750 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4751 set_bit(__I40E_CLIENT_RESET, pf->state);
4752 }
4753
4754 /**
4755 * i40e_quiesce_vsi - Pause a given VSI
4756 * @vsi: the VSI being paused
4757 **/
4758 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4759 {
4760 if (test_bit(__I40E_VSI_DOWN, vsi->state))
4761 return;
4762
4763 set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
4764 if (vsi->netdev && netif_running(vsi->netdev))
4765 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
4766 else
4767 i40e_vsi_close(vsi);
4768 }
4769
4770 /**
4771 * i40e_unquiesce_vsi - Resume a given VSI
4772 * @vsi: the VSI being resumed
4773 **/
4774 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4775 {
4776 if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
4777 return;
4778
4779 if (vsi->netdev && netif_running(vsi->netdev))
4780 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4781 else
4782 i40e_vsi_open(vsi); /* this clears the DOWN bit */
4783 }
4784
4785 /**
4786 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4787 * @pf: the PF
4788 **/
4789 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4790 {
4791 int v;
4792
4793 for (v = 0; v < pf->num_alloc_vsi; v++) {
4794 if (pf->vsi[v])
4795 i40e_quiesce_vsi(pf->vsi[v]);
4796 }
4797 }
4798
4799 /**
4800 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4801 * @pf: the PF
4802 **/
4803 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4804 {
4805 int v;
4806
4807 for (v = 0; v < pf->num_alloc_vsi; v++) {
4808 if (pf->vsi[v])
4809 i40e_unquiesce_vsi(pf->vsi[v]);
4810 }
4811 }
4812
4813 /**
4814 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
4815 * @vsi: the VSI being configured
4816 *
4817 * Wait until all queues on a given VSI have been disabled.
4818 **/
4819 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
4820 {
4821 struct i40e_pf *pf = vsi->back;
4822 int i, pf_q, ret;
4823
4824 pf_q = vsi->base_queue;
4825 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4826 /* Check and wait for the Tx queue */
4827 ret = i40e_pf_txq_wait(pf, pf_q, false);
4828 if (ret) {
4829 dev_info(&pf->pdev->dev,
4830 "VSI seid %d Tx ring %d disable timeout\n",
4831 vsi->seid, pf_q);
4832 return ret;
4833 }
4834
4835 if (!i40e_enabled_xdp_vsi(vsi))
4836 goto wait_rx;
4837
4838 /* Check and wait for the XDP Tx queue */
4839 ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
4840 false);
4841 if (ret) {
4842 dev_info(&pf->pdev->dev,
4843 "VSI seid %d XDP Tx ring %d disable timeout\n",
4844 vsi->seid, pf_q);
4845 return ret;
4846 }
4847 wait_rx:
4848 /* Check and wait for the Rx queue */
4849 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4850 if (ret) {
4851 dev_info(&pf->pdev->dev,
4852 "VSI seid %d Rx ring %d disable timeout\n",
4853 vsi->seid, pf_q);
4854 return ret;
4855 }
4856 }
4857
4858 return 0;
4859 }
4860
4861 #ifdef CONFIG_I40E_DCB
4862 /**
4863 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
4864 * @pf: the PF
4865 *
4866 * This function waits for the queues to be in disabled state for all the
4867 * VSIs that are managed by this PF.
4868 **/
4869 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
4870 {
4871 int v, ret = 0;
4872
4873 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4874 if (pf->vsi[v]) {
4875 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
4876 if (ret)
4877 break;
4878 }
4879 }
4880
4881 return ret;
4882 }
4883
4884 #endif
4885
4886 /**
4887 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
4888 * @pf: pointer to PF
4889 *
4890 * Get TC map for ISCSI PF type that will include iSCSI TC
4891 * and LAN TC.
4892 **/
4893 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4894 {
4895 struct i40e_dcb_app_priority_table app;
4896 struct i40e_hw *hw = &pf->hw;
4897 u8 enabled_tc = 1; /* TC0 is always enabled */
4898 u8 tc, i;
4899 /* Get the iSCSI APP TLV */
4900 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4901
4902 for (i = 0; i < dcbcfg->numapps; i++) {
4903 app = dcbcfg->app[i];
4904 if (app.selector == I40E_APP_SEL_TCPIP &&
4905 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4906 tc = dcbcfg->etscfg.prioritytable[app.priority];
4907 enabled_tc |= BIT(tc);
4908 break;
4909 }
4910 }
4911
4912 return enabled_tc;
4913 }
4914
4915 /**
4916 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4917 * @dcbcfg: the corresponding DCBx configuration structure
4918 *
4919 * Return the number of TCs from given DCBx configuration
4920 **/
4921 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4922 {
4923 int i, tc_unused = 0;
4924 u8 num_tc = 0;
4925 u8 ret = 0;
4926
4927 /* Scan the ETS Config Priority Table to find
4928 * traffic class enabled for a given priority
4929 * and create a bitmask of enabled TCs
4930 */
4931 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4932 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
4933
4934 /* Now scan the bitmask to check for
4935 * contiguous TCs starting with TC0
4936 */
4937 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4938 if (num_tc & BIT(i)) {
4939 if (!tc_unused) {
4940 ret++;
4941 } else {
4942 pr_err("Non-contiguous TC - Disabling DCB\n");
4943 return 1;
4944 }
4945 } else {
4946 tc_unused = 1;
4947 }
4948 }
4949
4950 /* There is always at least TC0 */
4951 if (!ret)
4952 ret = 1;
4953
4954 return ret;
4955 }
4956
4957 /**
4958 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4959 * @dcbcfg: the corresponding DCBx configuration structure
4960 *
4961 * Query the current DCB configuration and return the number of
4962 * traffic classes enabled from the given DCBX config
4963 **/
4964 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4965 {
4966 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4967 u8 enabled_tc = 1;
4968 u8 i;
4969
4970 for (i = 0; i < num_tc; i++)
4971 enabled_tc |= BIT(i);
4972
4973 return enabled_tc;
4974 }
4975
4976 /**
4977 * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
4978 * @pf: PF being queried
4979 *
4980 * Query the current MQPRIO configuration and return the number of
4981 * traffic classes enabled.
4982 **/
4983 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
4984 {
4985 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4986 u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
4987 u8 enabled_tc = 1, i;
4988
4989 for (i = 1; i < num_tc; i++)
4990 enabled_tc |= BIT(i);
4991 return enabled_tc;
4992 }
4993
4994 /**
4995 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4996 * @pf: PF being queried
4997 *
4998 * Return number of traffic classes enabled for the given PF
4999 **/
5000 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5001 {
5002 struct i40e_hw *hw = &pf->hw;
5003 u8 i, enabled_tc = 1;
5004 u8 num_tc = 0;
5005 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5006
5007 if (pf->flags & I40E_FLAG_TC_MQPRIO)
5008 return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5009
5010 /* If neither MQPRIO nor DCB is enabled, then always use single TC */
5011 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5012 return 1;
5013
5014 /* SFP mode will be enabled for all TCs on port */
5015 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5016 return i40e_dcb_get_num_tc(dcbcfg);
5017
5018 /* MFP mode return count of enabled TCs for this PF */
5019 if (pf->hw.func_caps.iscsi)
5020 enabled_tc = i40e_get_iscsi_tc_map(pf);
5021 else
5022 return 1; /* Only TC0 */
5023
5024 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5025 if (enabled_tc & BIT(i))
5026 num_tc++;
5027 }
5028 return num_tc;
5029 }
5030
5031 /**
5032 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
5033 * @pf: PF being queried
5034 *
5035 * Return a bitmap for enabled traffic classes for this PF.
5036 **/
5037 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5038 {
5039 if (pf->flags & I40E_FLAG_TC_MQPRIO)
5040 return i40e_mqprio_get_enabled_tc(pf);
5041
5042 /* If neither MQPRIO nor DCB is enabled for this PF then just return
5043 * default TC
5044 */
5045 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5046 return I40E_DEFAULT_TRAFFIC_CLASS;
5047
5048 /* SFP mode we want PF to be enabled for all TCs */
5049 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5050 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5051
5052 /* MFP enabled and iSCSI PF type */
5053 if (pf->hw.func_caps.iscsi)
5054 return i40e_get_iscsi_tc_map(pf);
5055 else
5056 return I40E_DEFAULT_TRAFFIC_CLASS;
5057 }
5058
5059 /**
5060 * i40e_vsi_get_bw_info - Query VSI BW Information
5061 * @vsi: the VSI being queried
5062 *
5063 * Returns 0 on success, negative value on failure
5064 **/
5065 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5066 {
5067 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5068 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5069 struct i40e_pf *pf = vsi->back;
5070 struct i40e_hw *hw = &pf->hw;
5071 i40e_status ret;
5072 u32 tc_bw_max;
5073 int i;
5074
5075 /* Get the VSI level BW configuration */
5076 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5077 if (ret) {
5078 dev_info(&pf->pdev->dev,
5079 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5080 i40e_stat_str(&pf->hw, ret),
5081 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5082 return -EINVAL;
5083 }
5084
5085 /* Get the VSI level BW configuration per TC */
5086 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5087 NULL);
5088 if (ret) {
5089 dev_info(&pf->pdev->dev,
5090 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5091 i40e_stat_str(&pf->hw, ret),
5092 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5093 return -EINVAL;
5094 }
5095
5096 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5097 dev_info(&pf->pdev->dev,
5098 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5099 bw_config.tc_valid_bits,
5100 bw_ets_config.tc_valid_bits);
5101 /* Still continuing */
5102 }
5103
5104 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5105 vsi->bw_max_quanta = bw_config.max_bw;
5106 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5107 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5108 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5109 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5110 vsi->bw_ets_limit_credits[i] =
5111 le16_to_cpu(bw_ets_config.credits[i]);
5112 /* 3 bits out of 4 for each TC */
5113 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5114 }
5115
5116 return 0;
5117 }
5118
5119 /**
5120 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5121 * @vsi: the VSI being configured
5122 * @enabled_tc: TC bitmap
5123 * @bw_share: BW shared credits per TC
5124 *
5125 * Returns 0 on success, negative value on failure
5126 **/
5127 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5128 u8 *bw_share)
5129 {
5130 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5131 struct i40e_pf *pf = vsi->back;
5132 i40e_status ret;
5133 int i;
5134
5135 /* There is no need to reset BW when mqprio mode is on. */
5136 if (pf->flags & I40E_FLAG_TC_MQPRIO)
5137 return 0;
5138 if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5139 ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5140 if (ret)
5141 dev_info(&pf->pdev->dev,
5142 "Failed to reset tx rate for vsi->seid %u\n",
5143 vsi->seid);
5144 return ret;
5145 }
5146 bw_data.tc_valid_bits = enabled_tc;
5147 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5148 bw_data.tc_bw_credits[i] = bw_share[i];
5149
5150 ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5151 if (ret) {
5152 dev_info(&pf->pdev->dev,
5153 "AQ command Config VSI BW allocation per TC failed = %d\n",
5154 pf->hw.aq.asq_last_status);
5155 return -EINVAL;
5156 }
5157
5158 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5159 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5160
5161 return 0;
5162 }
5163
5164 /**
5165 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5166 * @vsi: the VSI being configured
5167 * @enabled_tc: TC map to be enabled
5168 *
5169 **/
5170 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5171 {
5172 struct net_device *netdev = vsi->netdev;
5173 struct i40e_pf *pf = vsi->back;
5174 struct i40e_hw *hw = &pf->hw;
5175 u8 netdev_tc = 0;
5176 int i;
5177 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5178
5179 if (!netdev)
5180 return;
5181
5182 if (!enabled_tc) {
5183 netdev_reset_tc(netdev);
5184 return;
5185 }
5186
5187 /* Set up actual enabled TCs on the VSI */
5188 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5189 return;
5190
5191 /* set per TC queues for the VSI */
5192 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5193 /* Only set TC queues for enabled tcs
5194 *
5195 * e.g. For a VSI that has TC0 and TC3 enabled the
5196 * enabled_tc bitmap would be 0x00001001; the driver
5197 * will set the numtc for netdev as 2 that will be
5198 * referenced by the netdev layer as TC 0 and 1.
5199 */
5200 if (vsi->tc_config.enabled_tc & BIT(i))
5201 netdev_set_tc_queue(netdev,
5202 vsi->tc_config.tc_info[i].netdev_tc,
5203 vsi->tc_config.tc_info[i].qcount,
5204 vsi->tc_config.tc_info[i].qoffset);
5205 }
5206
5207 if (pf->flags & I40E_FLAG_TC_MQPRIO)
5208 return;
5209
5210 /* Assign UP2TC map for the VSI */
5211 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5212 /* Get the actual TC# for the UP */
5213 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5214 /* Get the mapped netdev TC# for the UP */
5215 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
5216 netdev_set_prio_tc_map(netdev, i, netdev_tc);
5217 }
5218 }
5219
5220 /**
5221 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5222 * @vsi: the VSI being configured
5223 * @ctxt: the ctxt buffer returned from AQ VSI update param command
5224 **/
5225 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5226 struct i40e_vsi_context *ctxt)
5227 {
5228 /* copy just the sections touched not the entire info
5229 * since not all sections are valid as returned by
5230 * update vsi params
5231 */
5232 vsi->info.mapping_flags = ctxt->info.mapping_flags;
5233 memcpy(&vsi->info.queue_mapping,
5234 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5235 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5236 sizeof(vsi->info.tc_mapping));
5237 }
5238
5239 /**
5240 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5241 * @vsi: VSI to be configured
5242 * @enabled_tc: TC bitmap
5243 *
5244 * This configures a particular VSI for TCs that are mapped to the
5245 * given TC bitmap. It uses default bandwidth share for TCs across
5246 * VSIs to configure TC for a particular VSI.
5247 *
5248 * NOTE:
5249 * It is expected that the VSI queues have been quisced before calling
5250 * this function.
5251 **/
5252 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5253 {
5254 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5255 struct i40e_pf *pf = vsi->back;
5256 struct i40e_hw *hw = &pf->hw;
5257 struct i40e_vsi_context ctxt;
5258 int ret = 0;
5259 int i;
5260
5261 /* Check if enabled_tc is same as existing or new TCs */
5262 if (vsi->tc_config.enabled_tc == enabled_tc &&
5263 vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5264 return ret;
5265
5266 /* Enable ETS TCs with equal BW Share for now across all VSIs */
5267 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5268 if (enabled_tc & BIT(i))
5269 bw_share[i] = 1;
5270 }
5271
5272 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5273 if (ret) {
5274 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5275
5276 dev_info(&pf->pdev->dev,
5277 "Failed configuring TC map %d for VSI %d\n",
5278 enabled_tc, vsi->seid);
5279 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5280 &bw_config, NULL);
5281 if (ret) {
5282 dev_info(&pf->pdev->dev,
5283 "Failed querying vsi bw info, err %s aq_err %s\n",
5284 i40e_stat_str(hw, ret),
5285 i40e_aq_str(hw, hw->aq.asq_last_status));
5286 goto out;
5287 }
5288 if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5289 u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5290
5291 if (!valid_tc)
5292 valid_tc = bw_config.tc_valid_bits;
5293 /* Always enable TC0, no matter what */
5294 valid_tc |= 1;
5295 dev_info(&pf->pdev->dev,
5296 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5297 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5298 enabled_tc = valid_tc;
5299 }
5300
5301 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5302 if (ret) {
5303 dev_err(&pf->pdev->dev,
5304 "Unable to configure TC map %d for VSI %d\n",
5305 enabled_tc, vsi->seid);
5306 goto out;
5307 }
5308 }
5309
5310 /* Update Queue Pairs Mapping for currently enabled UPs */
5311 ctxt.seid = vsi->seid;
5312 ctxt.pf_num = vsi->back->hw.pf_id;
5313 ctxt.vf_num = 0;
5314 ctxt.uplink_seid = vsi->uplink_seid;
5315 ctxt.info = vsi->info;
5316 if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
5317 ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5318 if (ret)
5319 goto out;
5320 } else {
5321 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5322 }
5323
5324 /* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5325 * queues changed.
5326 */
5327 if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5328 vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5329 vsi->num_queue_pairs);
5330 ret = i40e_vsi_config_rss(vsi);
5331 if (ret) {
5332 dev_info(&vsi->back->pdev->dev,
5333 "Failed to reconfig rss for num_queues\n");
5334 return ret;
5335 }
5336 vsi->reconfig_rss = false;
5337 }
5338 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5339 ctxt.info.valid_sections |=
5340 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5341 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5342 }
5343
5344 /* Update the VSI after updating the VSI queue-mapping
5345 * information
5346 */
5347 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5348 if (ret) {
5349 dev_info(&pf->pdev->dev,
5350 "Update vsi tc config failed, err %s aq_err %s\n",
5351 i40e_stat_str(hw, ret),
5352 i40e_aq_str(hw, hw->aq.asq_last_status));
5353 goto out;
5354 }
5355 /* update the local VSI info with updated queue map */
5356 i40e_vsi_update_queue_map(vsi, &ctxt);
5357 vsi->info.valid_sections = 0;
5358
5359 /* Update current VSI BW information */
5360 ret = i40e_vsi_get_bw_info(vsi);
5361 if (ret) {
5362 dev_info(&pf->pdev->dev,
5363 "Failed updating vsi bw info, err %s aq_err %s\n",
5364 i40e_stat_str(hw, ret),
5365 i40e_aq_str(hw, hw->aq.asq_last_status));
5366 goto out;
5367 }
5368
5369 /* Update the netdev TC setup */
5370 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5371 out:
5372 return ret;
5373 }
5374
5375 /**
5376 * i40e_get_link_speed - Returns link speed for the interface
5377 * @vsi: VSI to be configured
5378 *
5379 **/
5380 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5381 {
5382 struct i40e_pf *pf = vsi->back;
5383
5384 switch (pf->hw.phy.link_info.link_speed) {
5385 case I40E_LINK_SPEED_40GB:
5386 return 40000;
5387 case I40E_LINK_SPEED_25GB:
5388 return 25000;
5389 case I40E_LINK_SPEED_20GB:
5390 return 20000;
5391 case I40E_LINK_SPEED_10GB:
5392 return 10000;
5393 case I40E_LINK_SPEED_1GB:
5394 return 1000;
5395 default:
5396 return -EINVAL;
5397 }
5398 }
5399
5400 /**
5401 * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5402 * @vsi: VSI to be configured
5403 * @seid: seid of the channel/VSI
5404 * @max_tx_rate: max TX rate to be configured as BW limit
5405 *
5406 * Helper function to set BW limit for a given VSI
5407 **/
5408 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5409 {
5410 struct i40e_pf *pf = vsi->back;
5411 u64 credits = 0;
5412 int speed = 0;
5413 int ret = 0;
5414
5415 speed = i40e_get_link_speed(vsi);
5416 if (max_tx_rate > speed) {
5417 dev_err(&pf->pdev->dev,
5418 "Invalid max tx rate %llu specified for VSI seid %d.",
5419 max_tx_rate, seid);
5420 return -EINVAL;
5421 }
5422 if (max_tx_rate && max_tx_rate < 50) {
5423 dev_warn(&pf->pdev->dev,
5424 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5425 max_tx_rate = 50;
5426 }
5427
5428 /* Tx rate credits are in values of 50Mbps, 0 is disabled */
5429 credits = max_tx_rate;
5430 do_div(credits, I40E_BW_CREDIT_DIVISOR);
5431 ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5432 I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5433 if (ret)
5434 dev_err(&pf->pdev->dev,
5435 "Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5436 max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5437 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5438 return ret;
5439 }
5440
5441 /**
5442 * i40e_remove_queue_channels - Remove queue channels for the TCs
5443 * @vsi: VSI to be configured
5444 *
5445 * Remove queue channels for the TCs
5446 **/
5447 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5448 {
5449 enum i40e_admin_queue_err last_aq_status;
5450 struct i40e_cloud_filter *cfilter;
5451 struct i40e_channel *ch, *ch_tmp;
5452 struct i40e_pf *pf = vsi->back;
5453 struct hlist_node *node;
5454 int ret, i;
5455
5456 /* Reset rss size that was stored when reconfiguring rss for
5457 * channel VSIs with non-power-of-2 queue count.
5458 */
5459 vsi->current_rss_size = 0;
5460
5461 /* perform cleanup for channels if they exist */
5462 if (list_empty(&vsi->ch_list))
5463 return;
5464
5465 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5466 struct i40e_vsi *p_vsi;
5467
5468 list_del(&ch->list);
5469 p_vsi = ch->parent_vsi;
5470 if (!p_vsi || !ch->initialized) {
5471 kfree(ch);
5472 continue;
5473 }
5474 /* Reset queue contexts */
5475 for (i = 0; i < ch->num_queue_pairs; i++) {
5476 struct i40e_ring *tx_ring, *rx_ring;
5477 u16 pf_q;
5478
5479 pf_q = ch->base_queue + i;
5480 tx_ring = vsi->tx_rings[pf_q];
5481 tx_ring->ch = NULL;
5482
5483 rx_ring = vsi->rx_rings[pf_q];
5484 rx_ring->ch = NULL;
5485 }
5486
5487 /* Reset BW configured for this VSI via mqprio */
5488 ret = i40e_set_bw_limit(vsi, ch->seid, 0);
5489 if (ret)
5490 dev_info(&vsi->back->pdev->dev,
5491 "Failed to reset tx rate for ch->seid %u\n",
5492 ch->seid);
5493
5494 /* delete cloud filters associated with this channel */
5495 hlist_for_each_entry_safe(cfilter, node,
5496 &pf->cloud_filter_list, cloud_node) {
5497 if (cfilter->seid != ch->seid)
5498 continue;
5499
5500 hash_del(&cfilter->cloud_node);
5501 if (cfilter->dst_port)
5502 ret = i40e_add_del_cloud_filter_big_buf(vsi,
5503 cfilter,
5504 false);
5505 else
5506 ret = i40e_add_del_cloud_filter(vsi, cfilter,
5507 false);
5508 last_aq_status = pf->hw.aq.asq_last_status;
5509 if (ret)
5510 dev_info(&pf->pdev->dev,
5511 "Failed to delete cloud filter, err %s aq_err %s\n",
5512 i40e_stat_str(&pf->hw, ret),
5513 i40e_aq_str(&pf->hw, last_aq_status));
5514 kfree(cfilter);
5515 }
5516
5517 /* delete VSI from FW */
5518 ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
5519 NULL);
5520 if (ret)
5521 dev_err(&vsi->back->pdev->dev,
5522 "unable to remove channel (%d) for parent VSI(%d)\n",
5523 ch->seid, p_vsi->seid);
5524 kfree(ch);
5525 }
5526 INIT_LIST_HEAD(&vsi->ch_list);
5527 }
5528
5529 /**
5530 * i40e_is_any_channel - channel exist or not
5531 * @vsi: ptr to VSI to which channels are associated with
5532 *
5533 * Returns true or false if channel(s) exist for associated VSI or not
5534 **/
5535 static bool i40e_is_any_channel(struct i40e_vsi *vsi)
5536 {
5537 struct i40e_channel *ch, *ch_tmp;
5538
5539 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5540 if (ch->initialized)
5541 return true;
5542 }
5543
5544 return false;
5545 }
5546
5547 /**
5548 * i40e_get_max_queues_for_channel
5549 * @vsi: ptr to VSI to which channels are associated with
5550 *
5551 * Helper function which returns max value among the queue counts set on the
5552 * channels/TCs created.
5553 **/
5554 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
5555 {
5556 struct i40e_channel *ch, *ch_tmp;
5557 int max = 0;
5558
5559 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5560 if (!ch->initialized)
5561 continue;
5562 if (ch->num_queue_pairs > max)
5563 max = ch->num_queue_pairs;
5564 }
5565
5566 return max;
5567 }
5568
5569 /**
5570 * i40e_validate_num_queues - validate num_queues w.r.t channel
5571 * @pf: ptr to PF device
5572 * @num_queues: number of queues
5573 * @vsi: the parent VSI
5574 * @reconfig_rss: indicates should the RSS be reconfigured or not
5575 *
5576 * This function validates number of queues in the context of new channel
5577 * which is being established and determines if RSS should be reconfigured
5578 * or not for parent VSI.
5579 **/
5580 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
5581 struct i40e_vsi *vsi, bool *reconfig_rss)
5582 {
5583 int max_ch_queues;
5584
5585 if (!reconfig_rss)
5586 return -EINVAL;
5587
5588 *reconfig_rss = false;
5589 if (vsi->current_rss_size) {
5590 if (num_queues > vsi->current_rss_size) {
5591 dev_dbg(&pf->pdev->dev,
5592 "Error: num_queues (%d) > vsi's current_size(%d)\n",
5593 num_queues, vsi->current_rss_size);
5594 return -EINVAL;
5595 } else if ((num_queues < vsi->current_rss_size) &&
5596 (!is_power_of_2(num_queues))) {
5597 dev_dbg(&pf->pdev->dev,
5598 "Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
5599 num_queues, vsi->current_rss_size);
5600 return -EINVAL;
5601 }
5602 }
5603
5604 if (!is_power_of_2(num_queues)) {
5605 /* Find the max num_queues configured for channel if channel
5606 * exist.
5607 * if channel exist, then enforce 'num_queues' to be more than
5608 * max ever queues configured for channel.
5609 */
5610 max_ch_queues = i40e_get_max_queues_for_channel(vsi);
5611 if (num_queues < max_ch_queues) {
5612 dev_dbg(&pf->pdev->dev,
5613 "Error: num_queues (%d) < max queues configured for channel(%d)\n",
5614 num_queues, max_ch_queues);
5615 return -EINVAL;
5616 }
5617 *reconfig_rss = true;
5618 }
5619
5620 return 0;
5621 }
5622
5623 /**
5624 * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
5625 * @vsi: the VSI being setup
5626 * @rss_size: size of RSS, accordingly LUT gets reprogrammed
5627 *
5628 * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
5629 **/
5630 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
5631 {
5632 struct i40e_pf *pf = vsi->back;
5633 u8 seed[I40E_HKEY_ARRAY_SIZE];
5634 struct i40e_hw *hw = &pf->hw;
5635 int local_rss_size;
5636 u8 *lut;
5637 int ret;
5638
5639 if (!vsi->rss_size)
5640 return -EINVAL;
5641
5642 if (rss_size > vsi->rss_size)
5643 return -EINVAL;
5644
5645 local_rss_size = min_t(int, vsi->rss_size, rss_size);
5646 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
5647 if (!lut)
5648 return -ENOMEM;
5649
5650 /* Ignoring user configured lut if there is one */
5651 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
5652
5653 /* Use user configured hash key if there is one, otherwise
5654 * use default.
5655 */
5656 if (vsi->rss_hkey_user)
5657 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
5658 else
5659 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
5660
5661 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
5662 if (ret) {
5663 dev_info(&pf->pdev->dev,
5664 "Cannot set RSS lut, err %s aq_err %s\n",
5665 i40e_stat_str(hw, ret),
5666 i40e_aq_str(hw, hw->aq.asq_last_status));
5667 kfree(lut);
5668 return ret;
5669 }
5670 kfree(lut);
5671
5672 /* Do the update w.r.t. storing rss_size */
5673 if (!vsi->orig_rss_size)
5674 vsi->orig_rss_size = vsi->rss_size;
5675 vsi->current_rss_size = local_rss_size;
5676
5677 return ret;
5678 }
5679
5680 /**
5681 * i40e_channel_setup_queue_map - Setup a channel queue map
5682 * @pf: ptr to PF device
5683 * @vsi: the VSI being setup
5684 * @ctxt: VSI context structure
5685 * @ch: ptr to channel structure
5686 *
5687 * Setup queue map for a specific channel
5688 **/
5689 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
5690 struct i40e_vsi_context *ctxt,
5691 struct i40e_channel *ch)
5692 {
5693 u16 qcount, qmap, sections = 0;
5694 u8 offset = 0;
5695 int pow;
5696
5697 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
5698 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
5699
5700 qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
5701 ch->num_queue_pairs = qcount;
5702
5703 /* find the next higher power-of-2 of num queue pairs */
5704 pow = ilog2(qcount);
5705 if (!is_power_of_2(qcount))
5706 pow++;
5707
5708 qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
5709 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
5710
5711 /* Setup queue TC[0].qmap for given VSI context */
5712 ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
5713
5714 ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
5715 ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
5716 ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
5717 ctxt->info.valid_sections |= cpu_to_le16(sections);
5718 }
5719
5720 /**
5721 * i40e_add_channel - add a channel by adding VSI
5722 * @pf: ptr to PF device
5723 * @uplink_seid: underlying HW switching element (VEB) ID
5724 * @ch: ptr to channel structure
5725 *
5726 * Add a channel (VSI) using add_vsi and queue_map
5727 **/
5728 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
5729 struct i40e_channel *ch)
5730 {
5731 struct i40e_hw *hw = &pf->hw;
5732 struct i40e_vsi_context ctxt;
5733 u8 enabled_tc = 0x1; /* TC0 enabled */
5734 int ret;
5735
5736 if (ch->type != I40E_VSI_VMDQ2) {
5737 dev_info(&pf->pdev->dev,
5738 "add new vsi failed, ch->type %d\n", ch->type);
5739 return -EINVAL;
5740 }
5741
5742 memset(&ctxt, 0, sizeof(ctxt));
5743 ctxt.pf_num = hw->pf_id;
5744 ctxt.vf_num = 0;
5745 ctxt.uplink_seid = uplink_seid;
5746 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5747 if (ch->type == I40E_VSI_VMDQ2)
5748 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
5749
5750 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
5751 ctxt.info.valid_sections |=
5752 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
5753 ctxt.info.switch_id =
5754 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
5755 }
5756
5757 /* Set queue map for a given VSI context */
5758 i40e_channel_setup_queue_map(pf, &ctxt, ch);
5759
5760 /* Now time to create VSI */
5761 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
5762 if (ret) {
5763 dev_info(&pf->pdev->dev,
5764 "add new vsi failed, err %s aq_err %s\n",
5765 i40e_stat_str(&pf->hw, ret),
5766 i40e_aq_str(&pf->hw,
5767 pf->hw.aq.asq_last_status));
5768 return -ENOENT;
5769 }
5770
5771 /* Success, update channel */
5772 ch->enabled_tc = enabled_tc;
5773 ch->seid = ctxt.seid;
5774 ch->vsi_number = ctxt.vsi_number;
5775 ch->stat_counter_idx = cpu_to_le16(ctxt.info.stat_counter_idx);
5776
5777 /* copy just the sections touched not the entire info
5778 * since not all sections are valid as returned by
5779 * update vsi params
5780 */
5781 ch->info.mapping_flags = ctxt.info.mapping_flags;
5782 memcpy(&ch->info.queue_mapping,
5783 &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
5784 memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
5785 sizeof(ctxt.info.tc_mapping));
5786
5787 return 0;
5788 }
5789
5790 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
5791 u8 *bw_share)
5792 {
5793 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5794 i40e_status ret;
5795 int i;
5796
5797 bw_data.tc_valid_bits = ch->enabled_tc;
5798 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5799 bw_data.tc_bw_credits[i] = bw_share[i];
5800
5801 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
5802 &bw_data, NULL);
5803 if (ret) {
5804 dev_info(&vsi->back->pdev->dev,
5805 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
5806 vsi->back->hw.aq.asq_last_status, ch->seid);
5807 return -EINVAL;
5808 }
5809
5810 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5811 ch->info.qs_handle[i] = bw_data.qs_handles[i];
5812
5813 return 0;
5814 }
5815
5816 /**
5817 * i40e_channel_config_tx_ring - config TX ring associated with new channel
5818 * @pf: ptr to PF device
5819 * @vsi: the VSI being setup
5820 * @ch: ptr to channel structure
5821 *
5822 * Configure TX rings associated with channel (VSI) since queues are being
5823 * from parent VSI.
5824 **/
5825 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
5826 struct i40e_vsi *vsi,
5827 struct i40e_channel *ch)
5828 {
5829 i40e_status ret;
5830 int i;
5831 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5832
5833 /* Enable ETS TCs with equal BW Share for now across all VSIs */
5834 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5835 if (ch->enabled_tc & BIT(i))
5836 bw_share[i] = 1;
5837 }
5838
5839 /* configure BW for new VSI */
5840 ret = i40e_channel_config_bw(vsi, ch, bw_share);
5841 if (ret) {
5842 dev_info(&vsi->back->pdev->dev,
5843 "Failed configuring TC map %d for channel (seid %u)\n",
5844 ch->enabled_tc, ch->seid);
5845 return ret;
5846 }
5847
5848 for (i = 0; i < ch->num_queue_pairs; i++) {
5849 struct i40e_ring *tx_ring, *rx_ring;
5850 u16 pf_q;
5851
5852 pf_q = ch->base_queue + i;
5853
5854 /* Get to TX ring ptr of main VSI, for re-setup TX queue
5855 * context
5856 */
5857 tx_ring = vsi->tx_rings[pf_q];
5858 tx_ring->ch = ch;
5859
5860 /* Get the RX ring ptr */
5861 rx_ring = vsi->rx_rings[pf_q];
5862 rx_ring->ch = ch;
5863 }
5864
5865 return 0;
5866 }
5867
5868 /**
5869 * i40e_setup_hw_channel - setup new channel
5870 * @pf: ptr to PF device
5871 * @vsi: the VSI being setup
5872 * @ch: ptr to channel structure
5873 * @uplink_seid: underlying HW switching element (VEB) ID
5874 * @type: type of channel to be created (VMDq2/VF)
5875 *
5876 * Setup new channel (VSI) based on specified type (VMDq2/VF)
5877 * and configures TX rings accordingly
5878 **/
5879 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
5880 struct i40e_vsi *vsi,
5881 struct i40e_channel *ch,
5882 u16 uplink_seid, u8 type)
5883 {
5884 int ret;
5885
5886 ch->initialized = false;
5887 ch->base_queue = vsi->next_base_queue;
5888 ch->type = type;
5889
5890 /* Proceed with creation of channel (VMDq2) VSI */
5891 ret = i40e_add_channel(pf, uplink_seid, ch);
5892 if (ret) {
5893 dev_info(&pf->pdev->dev,
5894 "failed to add_channel using uplink_seid %u\n",
5895 uplink_seid);
5896 return ret;
5897 }
5898
5899 /* Mark the successful creation of channel */
5900 ch->initialized = true;
5901
5902 /* Reconfigure TX queues using QTX_CTL register */
5903 ret = i40e_channel_config_tx_ring(pf, vsi, ch);
5904 if (ret) {
5905 dev_info(&pf->pdev->dev,
5906 "failed to configure TX rings for channel %u\n",
5907 ch->seid);
5908 return ret;
5909 }
5910
5911 /* update 'next_base_queue' */
5912 vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
5913 dev_dbg(&pf->pdev->dev,
5914 "Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
5915 ch->seid, ch->vsi_number, ch->stat_counter_idx,
5916 ch->num_queue_pairs,
5917 vsi->next_base_queue);
5918 return ret;
5919 }
5920
5921 /**
5922 * i40e_setup_channel - setup new channel using uplink element
5923 * @pf: ptr to PF device
5924 * @type: type of channel to be created (VMDq2/VF)
5925 * @uplink_seid: underlying HW switching element (VEB) ID
5926 * @ch: ptr to channel structure
5927 *
5928 * Setup new channel (VSI) based on specified type (VMDq2/VF)
5929 * and uplink switching element (uplink_seid)
5930 **/
5931 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
5932 struct i40e_channel *ch)
5933 {
5934 u8 vsi_type;
5935 u16 seid;
5936 int ret;
5937
5938 if (vsi->type == I40E_VSI_MAIN) {
5939 vsi_type = I40E_VSI_VMDQ2;
5940 } else {
5941 dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
5942 vsi->type);
5943 return false;
5944 }
5945
5946 /* underlying switching element */
5947 seid = pf->vsi[pf->lan_vsi]->uplink_seid;
5948
5949 /* create channel (VSI), configure TX rings */
5950 ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
5951 if (ret) {
5952 dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
5953 return false;
5954 }
5955
5956 return ch->initialized ? true : false;
5957 }
5958
5959 /**
5960 * i40e_validate_and_set_switch_mode - sets up switch mode correctly
5961 * @vsi: ptr to VSI which has PF backing
5962 *
5963 * Sets up switch mode correctly if it needs to be changed and perform
5964 * what are allowed modes.
5965 **/
5966 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
5967 {
5968 u8 mode;
5969 struct i40e_pf *pf = vsi->back;
5970 struct i40e_hw *hw = &pf->hw;
5971 int ret;
5972
5973 ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
5974 if (ret)
5975 return -EINVAL;
5976
5977 if (hw->dev_caps.switch_mode) {
5978 /* if switch mode is set, support mode2 (non-tunneled for
5979 * cloud filter) for now
5980 */
5981 u32 switch_mode = hw->dev_caps.switch_mode &
5982 I40E_SWITCH_MODE_MASK;
5983 if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
5984 if (switch_mode == I40E_CLOUD_FILTER_MODE2)
5985 return 0;
5986 dev_err(&pf->pdev->dev,
5987 "Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
5988 hw->dev_caps.switch_mode);
5989 return -EINVAL;
5990 }
5991 }
5992
5993 /* Set Bit 7 to be valid */
5994 mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
5995
5996 /* Set L4type for TCP support */
5997 mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
5998
5999 /* Set cloud filter mode */
6000 mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6001
6002 /* Prep mode field for set_switch_config */
6003 ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6004 pf->last_sw_conf_valid_flags,
6005 mode, NULL);
6006 if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6007 dev_err(&pf->pdev->dev,
6008 "couldn't set switch config bits, err %s aq_err %s\n",
6009 i40e_stat_str(hw, ret),
6010 i40e_aq_str(hw,
6011 hw->aq.asq_last_status));
6012
6013 return ret;
6014 }
6015
6016 /**
6017 * i40e_create_queue_channel - function to create channel
6018 * @vsi: VSI to be configured
6019 * @ch: ptr to channel (it contains channel specific params)
6020 *
6021 * This function creates channel (VSI) using num_queues specified by user,
6022 * reconfigs RSS if needed.
6023 **/
6024 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6025 struct i40e_channel *ch)
6026 {
6027 struct i40e_pf *pf = vsi->back;
6028 bool reconfig_rss;
6029 int err;
6030
6031 if (!ch)
6032 return -EINVAL;
6033
6034 if (!ch->num_queue_pairs) {
6035 dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6036 ch->num_queue_pairs);
6037 return -EINVAL;
6038 }
6039
6040 /* validate user requested num_queues for channel */
6041 err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6042 &reconfig_rss);
6043 if (err) {
6044 dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6045 ch->num_queue_pairs);
6046 return -EINVAL;
6047 }
6048
6049 /* By default we are in VEPA mode, if this is the first VF/VMDq
6050 * VSI to be added switch to VEB mode.
6051 */
6052 if ((!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) ||
6053 (!i40e_is_any_channel(vsi))) {
6054 if (!is_power_of_2(vsi->tc_config.tc_info[0].qcount)) {
6055 dev_dbg(&pf->pdev->dev,
6056 "Failed to create channel. Override queues (%u) not power of 2\n",
6057 vsi->tc_config.tc_info[0].qcount);
6058 return -EINVAL;
6059 }
6060
6061 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6062 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6063
6064 if (vsi->type == I40E_VSI_MAIN) {
6065 if (pf->flags & I40E_FLAG_TC_MQPRIO)
6066 i40e_do_reset(pf, I40E_PF_RESET_FLAG,
6067 true);
6068 else
6069 i40e_do_reset_safe(pf,
6070 I40E_PF_RESET_FLAG);
6071 }
6072 }
6073 /* now onwards for main VSI, number of queues will be value
6074 * of TC0's queue count
6075 */
6076 }
6077
6078 /* By this time, vsi->cnt_q_avail shall be set to non-zero and
6079 * it should be more than num_queues
6080 */
6081 if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6082 dev_dbg(&pf->pdev->dev,
6083 "Error: cnt_q_avail (%u) less than num_queues %d\n",
6084 vsi->cnt_q_avail, ch->num_queue_pairs);
6085 return -EINVAL;
6086 }
6087
6088 /* reconfig_rss only if vsi type is MAIN_VSI */
6089 if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6090 err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6091 if (err) {
6092 dev_info(&pf->pdev->dev,
6093 "Error: unable to reconfig rss for num_queues (%u)\n",
6094 ch->num_queue_pairs);
6095 return -EINVAL;
6096 }
6097 }
6098
6099 if (!i40e_setup_channel(pf, vsi, ch)) {
6100 dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6101 return -EINVAL;
6102 }
6103
6104 dev_info(&pf->pdev->dev,
6105 "Setup channel (id:%u) utilizing num_queues %d\n",
6106 ch->seid, ch->num_queue_pairs);
6107
6108 /* configure VSI for BW limit */
6109 if (ch->max_tx_rate) {
6110 u64 credits = ch->max_tx_rate;
6111
6112 if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6113 return -EINVAL;
6114
6115 do_div(credits, I40E_BW_CREDIT_DIVISOR);
6116 dev_dbg(&pf->pdev->dev,
6117 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6118 ch->max_tx_rate,
6119 credits,
6120 ch->seid);
6121 }
6122
6123 /* in case of VF, this will be main SRIOV VSI */
6124 ch->parent_vsi = vsi;
6125
6126 /* and update main_vsi's count for queue_available to use */
6127 vsi->cnt_q_avail -= ch->num_queue_pairs;
6128
6129 return 0;
6130 }
6131
6132 /**
6133 * i40e_configure_queue_channels - Add queue channel for the given TCs
6134 * @vsi: VSI to be configured
6135 *
6136 * Configures queue channel mapping to the given TCs
6137 **/
6138 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6139 {
6140 struct i40e_channel *ch;
6141 u64 max_rate = 0;
6142 int ret = 0, i;
6143
6144 /* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6145 vsi->tc_seid_map[0] = vsi->seid;
6146 for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6147 if (vsi->tc_config.enabled_tc & BIT(i)) {
6148 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6149 if (!ch) {
6150 ret = -ENOMEM;
6151 goto err_free;
6152 }
6153
6154 INIT_LIST_HEAD(&ch->list);
6155 ch->num_queue_pairs =
6156 vsi->tc_config.tc_info[i].qcount;
6157 ch->base_queue =
6158 vsi->tc_config.tc_info[i].qoffset;
6159
6160 /* Bandwidth limit through tc interface is in bytes/s,
6161 * change to Mbit/s
6162 */
6163 max_rate = vsi->mqprio_qopt.max_rate[i];
6164 do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6165 ch->max_tx_rate = max_rate;
6166
6167 list_add_tail(&ch->list, &vsi->ch_list);
6168
6169 ret = i40e_create_queue_channel(vsi, ch);
6170 if (ret) {
6171 dev_err(&vsi->back->pdev->dev,
6172 "Failed creating queue channel with TC%d: queues %d\n",
6173 i, ch->num_queue_pairs);
6174 goto err_free;
6175 }
6176 vsi->tc_seid_map[i] = ch->seid;
6177 }
6178 }
6179 return ret;
6180
6181 err_free:
6182 i40e_remove_queue_channels(vsi);
6183 return ret;
6184 }
6185
6186 /**
6187 * i40e_veb_config_tc - Configure TCs for given VEB
6188 * @veb: given VEB
6189 * @enabled_tc: TC bitmap
6190 *
6191 * Configures given TC bitmap for VEB (switching) element
6192 **/
6193 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6194 {
6195 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6196 struct i40e_pf *pf = veb->pf;
6197 int ret = 0;
6198 int i;
6199
6200 /* No TCs or already enabled TCs just return */
6201 if (!enabled_tc || veb->enabled_tc == enabled_tc)
6202 return ret;
6203
6204 bw_data.tc_valid_bits = enabled_tc;
6205 /* bw_data.absolute_credits is not set (relative) */
6206
6207 /* Enable ETS TCs with equal BW Share for now */
6208 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6209 if (enabled_tc & BIT(i))
6210 bw_data.tc_bw_share_credits[i] = 1;
6211 }
6212
6213 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6214 &bw_data, NULL);
6215 if (ret) {
6216 dev_info(&pf->pdev->dev,
6217 "VEB bw config failed, err %s aq_err %s\n",
6218 i40e_stat_str(&pf->hw, ret),
6219 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6220 goto out;
6221 }
6222
6223 /* Update the BW information */
6224 ret = i40e_veb_get_bw_info(veb);
6225 if (ret) {
6226 dev_info(&pf->pdev->dev,
6227 "Failed getting veb bw config, err %s aq_err %s\n",
6228 i40e_stat_str(&pf->hw, ret),
6229 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6230 }
6231
6232 out:
6233 return ret;
6234 }
6235
6236 #ifdef CONFIG_I40E_DCB
6237 /**
6238 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6239 * @pf: PF struct
6240 *
6241 * Reconfigure VEB/VSIs on a given PF; it is assumed that
6242 * the caller would've quiesce all the VSIs before calling
6243 * this function
6244 **/
6245 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6246 {
6247 u8 tc_map = 0;
6248 int ret;
6249 u8 v;
6250
6251 /* Enable the TCs available on PF to all VEBs */
6252 tc_map = i40e_pf_get_tc_map(pf);
6253 for (v = 0; v < I40E_MAX_VEB; v++) {
6254 if (!pf->veb[v])
6255 continue;
6256 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6257 if (ret) {
6258 dev_info(&pf->pdev->dev,
6259 "Failed configuring TC for VEB seid=%d\n",
6260 pf->veb[v]->seid);
6261 /* Will try to configure as many components */
6262 }
6263 }
6264
6265 /* Update each VSI */
6266 for (v = 0; v < pf->num_alloc_vsi; v++) {
6267 if (!pf->vsi[v])
6268 continue;
6269
6270 /* - Enable all TCs for the LAN VSI
6271 * - For all others keep them at TC0 for now
6272 */
6273 if (v == pf->lan_vsi)
6274 tc_map = i40e_pf_get_tc_map(pf);
6275 else
6276 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6277
6278 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6279 if (ret) {
6280 dev_info(&pf->pdev->dev,
6281 "Failed configuring TC for VSI seid=%d\n",
6282 pf->vsi[v]->seid);
6283 /* Will try to configure as many components */
6284 } else {
6285 /* Re-configure VSI vectors based on updated TC map */
6286 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6287 if (pf->vsi[v]->netdev)
6288 i40e_dcbnl_set_all(pf->vsi[v]);
6289 }
6290 }
6291 }
6292
6293 /**
6294 * i40e_resume_port_tx - Resume port Tx
6295 * @pf: PF struct
6296 *
6297 * Resume a port's Tx and issue a PF reset in case of failure to
6298 * resume.
6299 **/
6300 static int i40e_resume_port_tx(struct i40e_pf *pf)
6301 {
6302 struct i40e_hw *hw = &pf->hw;
6303 int ret;
6304
6305 ret = i40e_aq_resume_port_tx(hw, NULL);
6306 if (ret) {
6307 dev_info(&pf->pdev->dev,
6308 "Resume Port Tx failed, err %s aq_err %s\n",
6309 i40e_stat_str(&pf->hw, ret),
6310 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6311 /* Schedule PF reset to recover */
6312 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6313 i40e_service_event_schedule(pf);
6314 }
6315
6316 return ret;
6317 }
6318
6319 /**
6320 * i40e_init_pf_dcb - Initialize DCB configuration
6321 * @pf: PF being configured
6322 *
6323 * Query the current DCB configuration and cache it
6324 * in the hardware structure
6325 **/
6326 static int i40e_init_pf_dcb(struct i40e_pf *pf)
6327 {
6328 struct i40e_hw *hw = &pf->hw;
6329 int err = 0;
6330
6331 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable
6332 * Also do not enable DCBx if FW LLDP agent is disabled
6333 */
6334 if ((pf->hw_features & I40E_HW_NO_DCB_SUPPORT) ||
6335 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP))
6336 goto out;
6337
6338 /* Get the initial DCB configuration */
6339 err = i40e_init_dcb(hw);
6340 if (!err) {
6341 /* Device/Function is not DCBX capable */
6342 if ((!hw->func_caps.dcb) ||
6343 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
6344 dev_info(&pf->pdev->dev,
6345 "DCBX offload is not supported or is disabled for this PF.\n");
6346 } else {
6347 /* When status is not DISABLED then DCBX in FW */
6348 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
6349 DCB_CAP_DCBX_VER_IEEE;
6350
6351 pf->flags |= I40E_FLAG_DCB_CAPABLE;
6352 /* Enable DCB tagging only when more than one TC
6353 * or explicitly disable if only one TC
6354 */
6355 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
6356 pf->flags |= I40E_FLAG_DCB_ENABLED;
6357 else
6358 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6359 dev_dbg(&pf->pdev->dev,
6360 "DCBX offload is supported for this PF.\n");
6361 }
6362 } else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
6363 dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
6364 pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
6365 } else {
6366 dev_info(&pf->pdev->dev,
6367 "Query for DCB configuration failed, err %s aq_err %s\n",
6368 i40e_stat_str(&pf->hw, err),
6369 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6370 }
6371
6372 out:
6373 return err;
6374 }
6375 #endif /* CONFIG_I40E_DCB */
6376 #define SPEED_SIZE 14
6377 #define FC_SIZE 8
6378 /**
6379 * i40e_print_link_message - print link up or down
6380 * @vsi: the VSI for which link needs a message
6381 * @isup: true of link is up, false otherwise
6382 */
6383 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
6384 {
6385 enum i40e_aq_link_speed new_speed;
6386 struct i40e_pf *pf = vsi->back;
6387 char *speed = "Unknown";
6388 char *fc = "Unknown";
6389 char *fec = "";
6390 char *req_fec = "";
6391 char *an = "";
6392
6393 new_speed = pf->hw.phy.link_info.link_speed;
6394
6395 if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
6396 return;
6397 vsi->current_isup = isup;
6398 vsi->current_speed = new_speed;
6399 if (!isup) {
6400 netdev_info(vsi->netdev, "NIC Link is Down\n");
6401 return;
6402 }
6403
6404 /* Warn user if link speed on NPAR enabled partition is not at
6405 * least 10GB
6406 */
6407 if (pf->hw.func_caps.npar_enable &&
6408 (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
6409 pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
6410 netdev_warn(vsi->netdev,
6411 "The partition detected link speed that is less than 10Gbps\n");
6412
6413 switch (pf->hw.phy.link_info.link_speed) {
6414 case I40E_LINK_SPEED_40GB:
6415 speed = "40 G";
6416 break;
6417 case I40E_LINK_SPEED_20GB:
6418 speed = "20 G";
6419 break;
6420 case I40E_LINK_SPEED_25GB:
6421 speed = "25 G";
6422 break;
6423 case I40E_LINK_SPEED_10GB:
6424 speed = "10 G";
6425 break;
6426 case I40E_LINK_SPEED_1GB:
6427 speed = "1000 M";
6428 break;
6429 case I40E_LINK_SPEED_100MB:
6430 speed = "100 M";
6431 break;
6432 default:
6433 break;
6434 }
6435
6436 switch (pf->hw.fc.current_mode) {
6437 case I40E_FC_FULL:
6438 fc = "RX/TX";
6439 break;
6440 case I40E_FC_TX_PAUSE:
6441 fc = "TX";
6442 break;
6443 case I40E_FC_RX_PAUSE:
6444 fc = "RX";
6445 break;
6446 default:
6447 fc = "None";
6448 break;
6449 }
6450
6451 if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
6452 req_fec = ", Requested FEC: None";
6453 fec = ", FEC: None";
6454 an = ", Autoneg: False";
6455
6456 if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
6457 an = ", Autoneg: True";
6458
6459 if (pf->hw.phy.link_info.fec_info &
6460 I40E_AQ_CONFIG_FEC_KR_ENA)
6461 fec = ", FEC: CL74 FC-FEC/BASE-R";
6462 else if (pf->hw.phy.link_info.fec_info &
6463 I40E_AQ_CONFIG_FEC_RS_ENA)
6464 fec = ", FEC: CL108 RS-FEC";
6465
6466 /* 'CL108 RS-FEC' should be displayed when RS is requested, or
6467 * both RS and FC are requested
6468 */
6469 if (vsi->back->hw.phy.link_info.req_fec_info &
6470 (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
6471 if (vsi->back->hw.phy.link_info.req_fec_info &
6472 I40E_AQ_REQUEST_FEC_RS)
6473 req_fec = ", Requested FEC: CL108 RS-FEC";
6474 else
6475 req_fec = ", Requested FEC: CL74 FC-FEC/BASE-R";
6476 }
6477 }
6478
6479 netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s%s, Flow Control: %s\n",
6480 speed, req_fec, fec, an, fc);
6481 }
6482
6483 /**
6484 * i40e_up_complete - Finish the last steps of bringing up a connection
6485 * @vsi: the VSI being configured
6486 **/
6487 static int i40e_up_complete(struct i40e_vsi *vsi)
6488 {
6489 struct i40e_pf *pf = vsi->back;
6490 int err;
6491
6492 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6493 i40e_vsi_configure_msix(vsi);
6494 else
6495 i40e_configure_msi_and_legacy(vsi);
6496
6497 /* start rings */
6498 err = i40e_vsi_start_rings(vsi);
6499 if (err)
6500 return err;
6501
6502 clear_bit(__I40E_VSI_DOWN, vsi->state);
6503 i40e_napi_enable_all(vsi);
6504 i40e_vsi_enable_irq(vsi);
6505
6506 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
6507 (vsi->netdev)) {
6508 i40e_print_link_message(vsi, true);
6509 netif_tx_start_all_queues(vsi->netdev);
6510 netif_carrier_on(vsi->netdev);
6511 }
6512
6513 /* replay FDIR SB filters */
6514 if (vsi->type == I40E_VSI_FDIR) {
6515 /* reset fd counters */
6516 pf->fd_add_err = 0;
6517 pf->fd_atr_cnt = 0;
6518 i40e_fdir_filter_restore(vsi);
6519 }
6520
6521 /* On the next run of the service_task, notify any clients of the new
6522 * opened netdev
6523 */
6524 set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
6525 i40e_service_event_schedule(pf);
6526
6527 return 0;
6528 }
6529
6530 /**
6531 * i40e_vsi_reinit_locked - Reset the VSI
6532 * @vsi: the VSI being configured
6533 *
6534 * Rebuild the ring structs after some configuration
6535 * has changed, e.g. MTU size.
6536 **/
6537 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
6538 {
6539 struct i40e_pf *pf = vsi->back;
6540
6541 WARN_ON(in_interrupt());
6542 while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
6543 usleep_range(1000, 2000);
6544 i40e_down(vsi);
6545
6546 i40e_up(vsi);
6547 clear_bit(__I40E_CONFIG_BUSY, pf->state);
6548 }
6549
6550 /**
6551 * i40e_up - Bring the connection back up after being down
6552 * @vsi: the VSI being configured
6553 **/
6554 int i40e_up(struct i40e_vsi *vsi)
6555 {
6556 int err;
6557
6558 err = i40e_vsi_configure(vsi);
6559 if (!err)
6560 err = i40e_up_complete(vsi);
6561
6562 return err;
6563 }
6564
6565 /**
6566 * i40e_force_link_state - Force the link status
6567 * @pf: board private structure
6568 * @is_up: whether the link state should be forced up or down
6569 **/
6570 static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
6571 {
6572 struct i40e_aq_get_phy_abilities_resp abilities;
6573 struct i40e_aq_set_phy_config config = {0};
6574 struct i40e_hw *hw = &pf->hw;
6575 i40e_status err;
6576 u64 mask;
6577
6578 /* Get the current phy config */
6579 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
6580 NULL);
6581 if (err) {
6582 dev_err(&pf->pdev->dev,
6583 "failed to get phy cap., ret = %s last_status = %s\n",
6584 i40e_stat_str(hw, err),
6585 i40e_aq_str(hw, hw->aq.asq_last_status));
6586 return err;
6587 }
6588
6589 /* If link needs to go up, but was not forced to go down,
6590 * no need for a flap
6591 */
6592 if (is_up && abilities.phy_type != 0)
6593 return I40E_SUCCESS;
6594
6595 /* To force link we need to set bits for all supported PHY types,
6596 * but there are now more than 32, so we need to split the bitmap
6597 * across two fields.
6598 */
6599 mask = I40E_PHY_TYPES_BITMASK;
6600 config.phy_type = is_up ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
6601 config.phy_type_ext = is_up ? (u8)((mask >> 32) & 0xff) : 0;
6602 /* Copy the old settings, except of phy_type */
6603 config.abilities = abilities.abilities;
6604 config.link_speed = abilities.link_speed;
6605 config.eee_capability = abilities.eee_capability;
6606 config.eeer = abilities.eeer_val;
6607 config.low_power_ctrl = abilities.d3_lpan;
6608 config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
6609 I40E_AQ_PHY_FEC_CONFIG_MASK;
6610 err = i40e_aq_set_phy_config(hw, &config, NULL);
6611
6612 if (err) {
6613 dev_err(&pf->pdev->dev,
6614 "set phy config ret = %s last_status = %s\n",
6615 i40e_stat_str(&pf->hw, err),
6616 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6617 return err;
6618 }
6619
6620 /* Update the link info */
6621 err = i40e_update_link_info(hw);
6622 if (err) {
6623 /* Wait a little bit (on 40G cards it sometimes takes a really
6624 * long time for link to come back from the atomic reset)
6625 * and try once more
6626 */
6627 msleep(1000);
6628 i40e_update_link_info(hw);
6629 }
6630
6631 i40e_aq_set_link_restart_an(hw, true, NULL);
6632
6633 return I40E_SUCCESS;
6634 }
6635
6636 /**
6637 * i40e_down - Shutdown the connection processing
6638 * @vsi: the VSI being stopped
6639 **/
6640 void i40e_down(struct i40e_vsi *vsi)
6641 {
6642 int i;
6643
6644 /* It is assumed that the caller of this function
6645 * sets the vsi->state __I40E_VSI_DOWN bit.
6646 */
6647 if (vsi->netdev) {
6648 netif_carrier_off(vsi->netdev);
6649 netif_tx_disable(vsi->netdev);
6650 }
6651 i40e_vsi_disable_irq(vsi);
6652 i40e_vsi_stop_rings(vsi);
6653 if (vsi->type == I40E_VSI_MAIN &&
6654 vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED)
6655 i40e_force_link_state(vsi->back, false);
6656 i40e_napi_disable_all(vsi);
6657
6658 for (i = 0; i < vsi->num_queue_pairs; i++) {
6659 i40e_clean_tx_ring(vsi->tx_rings[i]);
6660 if (i40e_enabled_xdp_vsi(vsi))
6661 i40e_clean_tx_ring(vsi->xdp_rings[i]);
6662 i40e_clean_rx_ring(vsi->rx_rings[i]);
6663 }
6664
6665 }
6666
6667 /**
6668 * i40e_validate_mqprio_qopt- validate queue mapping info
6669 * @vsi: the VSI being configured
6670 * @mqprio_qopt: queue parametrs
6671 **/
6672 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
6673 struct tc_mqprio_qopt_offload *mqprio_qopt)
6674 {
6675 u64 sum_max_rate = 0;
6676 u64 max_rate = 0;
6677 int i;
6678
6679 if (mqprio_qopt->qopt.offset[0] != 0 ||
6680 mqprio_qopt->qopt.num_tc < 1 ||
6681 mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
6682 return -EINVAL;
6683 for (i = 0; ; i++) {
6684 if (!mqprio_qopt->qopt.count[i])
6685 return -EINVAL;
6686 if (mqprio_qopt->min_rate[i]) {
6687 dev_err(&vsi->back->pdev->dev,
6688 "Invalid min tx rate (greater than 0) specified\n");
6689 return -EINVAL;
6690 }
6691 max_rate = mqprio_qopt->max_rate[i];
6692 do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6693 sum_max_rate += max_rate;
6694
6695 if (i >= mqprio_qopt->qopt.num_tc - 1)
6696 break;
6697 if (mqprio_qopt->qopt.offset[i + 1] !=
6698 (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
6699 return -EINVAL;
6700 }
6701 if (vsi->num_queue_pairs <
6702 (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
6703 return -EINVAL;
6704 }
6705 if (sum_max_rate > i40e_get_link_speed(vsi)) {
6706 dev_err(&vsi->back->pdev->dev,
6707 "Invalid max tx rate specified\n");
6708 return -EINVAL;
6709 }
6710 return 0;
6711 }
6712
6713 /**
6714 * i40e_vsi_set_default_tc_config - set default values for tc configuration
6715 * @vsi: the VSI being configured
6716 **/
6717 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
6718 {
6719 u16 qcount;
6720 int i;
6721
6722 /* Only TC0 is enabled */
6723 vsi->tc_config.numtc = 1;
6724 vsi->tc_config.enabled_tc = 1;
6725 qcount = min_t(int, vsi->alloc_queue_pairs,
6726 i40e_pf_get_max_q_per_tc(vsi->back));
6727 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6728 /* For the TC that is not enabled set the offset to to default
6729 * queue and allocate one queue for the given TC.
6730 */
6731 vsi->tc_config.tc_info[i].qoffset = 0;
6732 if (i == 0)
6733 vsi->tc_config.tc_info[i].qcount = qcount;
6734 else
6735 vsi->tc_config.tc_info[i].qcount = 1;
6736 vsi->tc_config.tc_info[i].netdev_tc = 0;
6737 }
6738 }
6739
6740 /**
6741 * i40e_setup_tc - configure multiple traffic classes
6742 * @netdev: net device to configure
6743 * @type_data: tc offload data
6744 **/
6745 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
6746 {
6747 struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
6748 struct i40e_netdev_priv *np = netdev_priv(netdev);
6749 struct i40e_vsi *vsi = np->vsi;
6750 struct i40e_pf *pf = vsi->back;
6751 u8 enabled_tc = 0, num_tc, hw;
6752 bool need_reset = false;
6753 int ret = -EINVAL;
6754 u16 mode;
6755 int i;
6756
6757 num_tc = mqprio_qopt->qopt.num_tc;
6758 hw = mqprio_qopt->qopt.hw;
6759 mode = mqprio_qopt->mode;
6760 if (!hw) {
6761 pf->flags &= ~I40E_FLAG_TC_MQPRIO;
6762 memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
6763 goto config_tc;
6764 }
6765
6766 /* Check if MFP enabled */
6767 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
6768 netdev_info(netdev,
6769 "Configuring TC not supported in MFP mode\n");
6770 return ret;
6771 }
6772 switch (mode) {
6773 case TC_MQPRIO_MODE_DCB:
6774 pf->flags &= ~I40E_FLAG_TC_MQPRIO;
6775
6776 /* Check if DCB enabled to continue */
6777 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
6778 netdev_info(netdev,
6779 "DCB is not enabled for adapter\n");
6780 return ret;
6781 }
6782
6783 /* Check whether tc count is within enabled limit */
6784 if (num_tc > i40e_pf_get_num_tc(pf)) {
6785 netdev_info(netdev,
6786 "TC count greater than enabled on link for adapter\n");
6787 return ret;
6788 }
6789 break;
6790 case TC_MQPRIO_MODE_CHANNEL:
6791 if (pf->flags & I40E_FLAG_DCB_ENABLED) {
6792 netdev_info(netdev,
6793 "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
6794 return ret;
6795 }
6796 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
6797 return ret;
6798 ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
6799 if (ret)
6800 return ret;
6801 memcpy(&vsi->mqprio_qopt, mqprio_qopt,
6802 sizeof(*mqprio_qopt));
6803 pf->flags |= I40E_FLAG_TC_MQPRIO;
6804 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6805 break;
6806 default:
6807 return -EINVAL;
6808 }
6809
6810 config_tc:
6811 /* Generate TC map for number of tc requested */
6812 for (i = 0; i < num_tc; i++)
6813 enabled_tc |= BIT(i);
6814
6815 /* Requesting same TC configuration as already enabled */
6816 if (enabled_tc == vsi->tc_config.enabled_tc &&
6817 mode != TC_MQPRIO_MODE_CHANNEL)
6818 return 0;
6819
6820 /* Quiesce VSI queues */
6821 i40e_quiesce_vsi(vsi);
6822
6823 if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
6824 i40e_remove_queue_channels(vsi);
6825
6826 /* Configure VSI for enabled TCs */
6827 ret = i40e_vsi_config_tc(vsi, enabled_tc);
6828 if (ret) {
6829 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
6830 vsi->seid);
6831 need_reset = true;
6832 goto exit;
6833 }
6834
6835 if (pf->flags & I40E_FLAG_TC_MQPRIO) {
6836 if (vsi->mqprio_qopt.max_rate[0]) {
6837 u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
6838
6839 do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
6840 ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
6841 if (!ret) {
6842 u64 credits = max_tx_rate;
6843
6844 do_div(credits, I40E_BW_CREDIT_DIVISOR);
6845 dev_dbg(&vsi->back->pdev->dev,
6846 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6847 max_tx_rate,
6848 credits,
6849 vsi->seid);
6850 } else {
6851 need_reset = true;
6852 goto exit;
6853 }
6854 }
6855 ret = i40e_configure_queue_channels(vsi);
6856 if (ret) {
6857 netdev_info(netdev,
6858 "Failed configuring queue channels\n");
6859 need_reset = true;
6860 goto exit;
6861 }
6862 }
6863
6864 exit:
6865 /* Reset the configuration data to defaults, only TC0 is enabled */
6866 if (need_reset) {
6867 i40e_vsi_set_default_tc_config(vsi);
6868 need_reset = false;
6869 }
6870
6871 /* Unquiesce VSI */
6872 i40e_unquiesce_vsi(vsi);
6873 return ret;
6874 }
6875
6876 /**
6877 * i40e_set_cld_element - sets cloud filter element data
6878 * @filter: cloud filter rule
6879 * @cld: ptr to cloud filter element data
6880 *
6881 * This is helper function to copy data into cloud filter element
6882 **/
6883 static inline void
6884 i40e_set_cld_element(struct i40e_cloud_filter *filter,
6885 struct i40e_aqc_cloud_filters_element_data *cld)
6886 {
6887 int i, j;
6888 u32 ipa;
6889
6890 memset(cld, 0, sizeof(*cld));
6891 ether_addr_copy(cld->outer_mac, filter->dst_mac);
6892 ether_addr_copy(cld->inner_mac, filter->src_mac);
6893
6894 if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
6895 return;
6896
6897 if (filter->n_proto == ETH_P_IPV6) {
6898 #define IPV6_MAX_INDEX (ARRAY_SIZE(filter->dst_ipv6) - 1)
6899 for (i = 0, j = 0; i < ARRAY_SIZE(filter->dst_ipv6);
6900 i++, j += 2) {
6901 ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
6902 ipa = cpu_to_le32(ipa);
6903 memcpy(&cld->ipaddr.raw_v6.data[j], &ipa, sizeof(ipa));
6904 }
6905 } else {
6906 ipa = be32_to_cpu(filter->dst_ipv4);
6907 memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
6908 }
6909
6910 cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
6911
6912 /* tenant_id is not supported by FW now, once the support is enabled
6913 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
6914 */
6915 if (filter->tenant_id)
6916 return;
6917 }
6918
6919 /**
6920 * i40e_add_del_cloud_filter - Add/del cloud filter
6921 * @vsi: pointer to VSI
6922 * @filter: cloud filter rule
6923 * @add: if true, add, if false, delete
6924 *
6925 * Add or delete a cloud filter for a specific flow spec.
6926 * Returns 0 if the filter were successfully added.
6927 **/
6928 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
6929 struct i40e_cloud_filter *filter, bool add)
6930 {
6931 struct i40e_aqc_cloud_filters_element_data cld_filter;
6932 struct i40e_pf *pf = vsi->back;
6933 int ret;
6934 static const u16 flag_table[128] = {
6935 [I40E_CLOUD_FILTER_FLAGS_OMAC] =
6936 I40E_AQC_ADD_CLOUD_FILTER_OMAC,
6937 [I40E_CLOUD_FILTER_FLAGS_IMAC] =
6938 I40E_AQC_ADD_CLOUD_FILTER_IMAC,
6939 [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN] =
6940 I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
6941 [I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
6942 I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
6943 [I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
6944 I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
6945 [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
6946 I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
6947 [I40E_CLOUD_FILTER_FLAGS_IIP] =
6948 I40E_AQC_ADD_CLOUD_FILTER_IIP,
6949 };
6950
6951 if (filter->flags >= ARRAY_SIZE(flag_table))
6952 return I40E_ERR_CONFIG;
6953
6954 /* copy element needed to add cloud filter from filter */
6955 i40e_set_cld_element(filter, &cld_filter);
6956
6957 if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
6958 cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
6959 I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
6960
6961 if (filter->n_proto == ETH_P_IPV6)
6962 cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
6963 I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
6964 else
6965 cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
6966 I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
6967
6968 if (add)
6969 ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
6970 &cld_filter, 1);
6971 else
6972 ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
6973 &cld_filter, 1);
6974 if (ret)
6975 dev_dbg(&pf->pdev->dev,
6976 "Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
6977 add ? "add" : "delete", filter->dst_port, ret,
6978 pf->hw.aq.asq_last_status);
6979 else
6980 dev_info(&pf->pdev->dev,
6981 "%s cloud filter for VSI: %d\n",
6982 add ? "Added" : "Deleted", filter->seid);
6983 return ret;
6984 }
6985
6986 /**
6987 * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
6988 * @vsi: pointer to VSI
6989 * @filter: cloud filter rule
6990 * @add: if true, add, if false, delete
6991 *
6992 * Add or delete a cloud filter for a specific flow spec using big buffer.
6993 * Returns 0 if the filter were successfully added.
6994 **/
6995 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
6996 struct i40e_cloud_filter *filter,
6997 bool add)
6998 {
6999 struct i40e_aqc_cloud_filters_element_bb cld_filter;
7000 struct i40e_pf *pf = vsi->back;
7001 int ret;
7002
7003 /* Both (src/dst) valid mac_addr are not supported */
7004 if ((is_valid_ether_addr(filter->dst_mac) &&
7005 is_valid_ether_addr(filter->src_mac)) ||
7006 (is_multicast_ether_addr(filter->dst_mac) &&
7007 is_multicast_ether_addr(filter->src_mac)))
7008 return -EOPNOTSUPP;
7009
7010 /* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
7011 * ports are not supported via big buffer now.
7012 */
7013 if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
7014 return -EOPNOTSUPP;
7015
7016 /* adding filter using src_port/src_ip is not supported at this stage */
7017 if (filter->src_port || filter->src_ipv4 ||
7018 !ipv6_addr_any(&filter->ip.v6.src_ip6))
7019 return -EOPNOTSUPP;
7020
7021 /* copy element needed to add cloud filter from filter */
7022 i40e_set_cld_element(filter, &cld_filter.element);
7023
7024 if (is_valid_ether_addr(filter->dst_mac) ||
7025 is_valid_ether_addr(filter->src_mac) ||
7026 is_multicast_ether_addr(filter->dst_mac) ||
7027 is_multicast_ether_addr(filter->src_mac)) {
7028 /* MAC + IP : unsupported mode */
7029 if (filter->dst_ipv4)
7030 return -EOPNOTSUPP;
7031
7032 /* since we validated that L4 port must be valid before
7033 * we get here, start with respective "flags" value
7034 * and update if vlan is present or not
7035 */
7036 cld_filter.element.flags =
7037 cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
7038
7039 if (filter->vlan_id) {
7040 cld_filter.element.flags =
7041 cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
7042 }
7043
7044 } else if (filter->dst_ipv4 ||
7045 !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
7046 cld_filter.element.flags =
7047 cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
7048 if (filter->n_proto == ETH_P_IPV6)
7049 cld_filter.element.flags |=
7050 cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
7051 else
7052 cld_filter.element.flags |=
7053 cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
7054 } else {
7055 dev_err(&pf->pdev->dev,
7056 "either mac or ip has to be valid for cloud filter\n");
7057 return -EINVAL;
7058 }
7059
7060 /* Now copy L4 port in Byte 6..7 in general fields */
7061 cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
7062 be16_to_cpu(filter->dst_port);
7063
7064 if (add) {
7065 /* Validate current device switch mode, change if necessary */
7066 ret = i40e_validate_and_set_switch_mode(vsi);
7067 if (ret) {
7068 dev_err(&pf->pdev->dev,
7069 "failed to set switch mode, ret %d\n",
7070 ret);
7071 return ret;
7072 }
7073
7074 ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
7075 &cld_filter, 1);
7076 } else {
7077 ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
7078 &cld_filter, 1);
7079 }
7080
7081 if (ret)
7082 dev_dbg(&pf->pdev->dev,
7083 "Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
7084 add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
7085 else
7086 dev_info(&pf->pdev->dev,
7087 "%s cloud filter for VSI: %d, L4 port: %d\n",
7088 add ? "add" : "delete", filter->seid,
7089 ntohs(filter->dst_port));
7090 return ret;
7091 }
7092
7093 /**
7094 * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
7095 * @vsi: Pointer to VSI
7096 * @cls_flower: Pointer to struct tc_cls_flower_offload
7097 * @filter: Pointer to cloud filter structure
7098 *
7099 **/
7100 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
7101 struct tc_cls_flower_offload *f,
7102 struct i40e_cloud_filter *filter)
7103 {
7104 u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
7105 struct i40e_pf *pf = vsi->back;
7106 u8 field_flags = 0;
7107
7108 if (f->dissector->used_keys &
7109 ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
7110 BIT(FLOW_DISSECTOR_KEY_BASIC) |
7111 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
7112 BIT(FLOW_DISSECTOR_KEY_VLAN) |
7113 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
7114 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
7115 BIT(FLOW_DISSECTOR_KEY_PORTS) |
7116 BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
7117 dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
7118 f->dissector->used_keys);
7119 return -EOPNOTSUPP;
7120 }
7121
7122 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
7123 struct flow_dissector_key_keyid *key =
7124 skb_flow_dissector_target(f->dissector,
7125 FLOW_DISSECTOR_KEY_ENC_KEYID,
7126 f->key);
7127
7128 struct flow_dissector_key_keyid *mask =
7129 skb_flow_dissector_target(f->dissector,
7130 FLOW_DISSECTOR_KEY_ENC_KEYID,
7131 f->mask);
7132
7133 if (mask->keyid != 0)
7134 field_flags |= I40E_CLOUD_FIELD_TEN_ID;
7135
7136 filter->tenant_id = be32_to_cpu(key->keyid);
7137 }
7138
7139 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
7140 struct flow_dissector_key_basic *key =
7141 skb_flow_dissector_target(f->dissector,
7142 FLOW_DISSECTOR_KEY_BASIC,
7143 f->key);
7144
7145 struct flow_dissector_key_basic *mask =
7146 skb_flow_dissector_target(f->dissector,
7147 FLOW_DISSECTOR_KEY_BASIC,
7148 f->mask);
7149
7150 n_proto_key = ntohs(key->n_proto);
7151 n_proto_mask = ntohs(mask->n_proto);
7152
7153 if (n_proto_key == ETH_P_ALL) {
7154 n_proto_key = 0;
7155 n_proto_mask = 0;
7156 }
7157 filter->n_proto = n_proto_key & n_proto_mask;
7158 filter->ip_proto = key->ip_proto;
7159 }
7160
7161 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
7162 struct flow_dissector_key_eth_addrs *key =
7163 skb_flow_dissector_target(f->dissector,
7164 FLOW_DISSECTOR_KEY_ETH_ADDRS,
7165 f->key);
7166
7167 struct flow_dissector_key_eth_addrs *mask =
7168 skb_flow_dissector_target(f->dissector,
7169 FLOW_DISSECTOR_KEY_ETH_ADDRS,
7170 f->mask);
7171
7172 /* use is_broadcast and is_zero to check for all 0xf or 0 */
7173 if (!is_zero_ether_addr(mask->dst)) {
7174 if (is_broadcast_ether_addr(mask->dst)) {
7175 field_flags |= I40E_CLOUD_FIELD_OMAC;
7176 } else {
7177 dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
7178 mask->dst);
7179 return I40E_ERR_CONFIG;
7180 }
7181 }
7182
7183 if (!is_zero_ether_addr(mask->src)) {
7184 if (is_broadcast_ether_addr(mask->src)) {
7185 field_flags |= I40E_CLOUD_FIELD_IMAC;
7186 } else {
7187 dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
7188 mask->src);
7189 return I40E_ERR_CONFIG;
7190 }
7191 }
7192 ether_addr_copy(filter->dst_mac, key->dst);
7193 ether_addr_copy(filter->src_mac, key->src);
7194 }
7195
7196 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
7197 struct flow_dissector_key_vlan *key =
7198 skb_flow_dissector_target(f->dissector,
7199 FLOW_DISSECTOR_KEY_VLAN,
7200 f->key);
7201 struct flow_dissector_key_vlan *mask =
7202 skb_flow_dissector_target(f->dissector,
7203 FLOW_DISSECTOR_KEY_VLAN,
7204 f->mask);
7205
7206 if (mask->vlan_id) {
7207 if (mask->vlan_id == VLAN_VID_MASK) {
7208 field_flags |= I40E_CLOUD_FIELD_IVLAN;
7209
7210 } else {
7211 dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
7212 mask->vlan_id);
7213 return I40E_ERR_CONFIG;
7214 }
7215 }
7216
7217 filter->vlan_id = cpu_to_be16(key->vlan_id);
7218 }
7219
7220 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
7221 struct flow_dissector_key_control *key =
7222 skb_flow_dissector_target(f->dissector,
7223 FLOW_DISSECTOR_KEY_CONTROL,
7224 f->key);
7225
7226 addr_type = key->addr_type;
7227 }
7228
7229 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
7230 struct flow_dissector_key_ipv4_addrs *key =
7231 skb_flow_dissector_target(f->dissector,
7232 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
7233 f->key);
7234 struct flow_dissector_key_ipv4_addrs *mask =
7235 skb_flow_dissector_target(f->dissector,
7236 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
7237 f->mask);
7238
7239 if (mask->dst) {
7240 if (mask->dst == cpu_to_be32(0xffffffff)) {
7241 field_flags |= I40E_CLOUD_FIELD_IIP;
7242 } else {
7243 dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
7244 &mask->dst);
7245 return I40E_ERR_CONFIG;
7246 }
7247 }
7248
7249 if (mask->src) {
7250 if (mask->src == cpu_to_be32(0xffffffff)) {
7251 field_flags |= I40E_CLOUD_FIELD_IIP;
7252 } else {
7253 dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
7254 &mask->src);
7255 return I40E_ERR_CONFIG;
7256 }
7257 }
7258
7259 if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
7260 dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
7261 return I40E_ERR_CONFIG;
7262 }
7263 filter->dst_ipv4 = key->dst;
7264 filter->src_ipv4 = key->src;
7265 }
7266
7267 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
7268 struct flow_dissector_key_ipv6_addrs *key =
7269 skb_flow_dissector_target(f->dissector,
7270 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
7271 f->key);
7272 struct flow_dissector_key_ipv6_addrs *mask =
7273 skb_flow_dissector_target(f->dissector,
7274 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
7275 f->mask);
7276
7277 /* src and dest IPV6 address should not be LOOPBACK
7278 * (0:0:0:0:0:0:0:1), which can be represented as ::1
7279 */
7280 if (ipv6_addr_loopback(&key->dst) ||
7281 ipv6_addr_loopback(&key->src)) {
7282 dev_err(&pf->pdev->dev,
7283 "Bad ipv6, addr is LOOPBACK\n");
7284 return I40E_ERR_CONFIG;
7285 }
7286 if (!ipv6_addr_any(&mask->dst) || !ipv6_addr_any(&mask->src))
7287 field_flags |= I40E_CLOUD_FIELD_IIP;
7288
7289 memcpy(&filter->src_ipv6, &key->src.s6_addr32,
7290 sizeof(filter->src_ipv6));
7291 memcpy(&filter->dst_ipv6, &key->dst.s6_addr32,
7292 sizeof(filter->dst_ipv6));
7293 }
7294
7295 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
7296 struct flow_dissector_key_ports *key =
7297 skb_flow_dissector_target(f->dissector,
7298 FLOW_DISSECTOR_KEY_PORTS,
7299 f->key);
7300 struct flow_dissector_key_ports *mask =
7301 skb_flow_dissector_target(f->dissector,
7302 FLOW_DISSECTOR_KEY_PORTS,
7303 f->mask);
7304
7305 if (mask->src) {
7306 if (mask->src == cpu_to_be16(0xffff)) {
7307 field_flags |= I40E_CLOUD_FIELD_IIP;
7308 } else {
7309 dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
7310 be16_to_cpu(mask->src));
7311 return I40E_ERR_CONFIG;
7312 }
7313 }
7314
7315 if (mask->dst) {
7316 if (mask->dst == cpu_to_be16(0xffff)) {
7317 field_flags |= I40E_CLOUD_FIELD_IIP;
7318 } else {
7319 dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
7320 be16_to_cpu(mask->dst));
7321 return I40E_ERR_CONFIG;
7322 }
7323 }
7324
7325 filter->dst_port = key->dst;
7326 filter->src_port = key->src;
7327
7328 switch (filter->ip_proto) {
7329 case IPPROTO_TCP:
7330 case IPPROTO_UDP:
7331 break;
7332 default:
7333 dev_err(&pf->pdev->dev,
7334 "Only UDP and TCP transport are supported\n");
7335 return -EINVAL;
7336 }
7337 }
7338 filter->flags = field_flags;
7339 return 0;
7340 }
7341
7342 /**
7343 * i40e_handle_tclass: Forward to a traffic class on the device
7344 * @vsi: Pointer to VSI
7345 * @tc: traffic class index on the device
7346 * @filter: Pointer to cloud filter structure
7347 *
7348 **/
7349 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
7350 struct i40e_cloud_filter *filter)
7351 {
7352 struct i40e_channel *ch, *ch_tmp;
7353
7354 /* direct to a traffic class on the same device */
7355 if (tc == 0) {
7356 filter->seid = vsi->seid;
7357 return 0;
7358 } else if (vsi->tc_config.enabled_tc & BIT(tc)) {
7359 if (!filter->dst_port) {
7360 dev_err(&vsi->back->pdev->dev,
7361 "Specify destination port to direct to traffic class that is not default\n");
7362 return -EINVAL;
7363 }
7364 if (list_empty(&vsi->ch_list))
7365 return -EINVAL;
7366 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
7367 list) {
7368 if (ch->seid == vsi->tc_seid_map[tc])
7369 filter->seid = ch->seid;
7370 }
7371 return 0;
7372 }
7373 dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
7374 return -EINVAL;
7375 }
7376
7377 /**
7378 * i40e_configure_clsflower - Configure tc flower filters
7379 * @vsi: Pointer to VSI
7380 * @cls_flower: Pointer to struct tc_cls_flower_offload
7381 *
7382 **/
7383 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
7384 struct tc_cls_flower_offload *cls_flower)
7385 {
7386 int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
7387 struct i40e_cloud_filter *filter = NULL;
7388 struct i40e_pf *pf = vsi->back;
7389 int err = 0;
7390
7391 if (tc < 0) {
7392 dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
7393 return -EOPNOTSUPP;
7394 }
7395
7396 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
7397 test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
7398 return -EBUSY;
7399
7400 if (pf->fdir_pf_active_filters ||
7401 (!hlist_empty(&pf->fdir_filter_list))) {
7402 dev_err(&vsi->back->pdev->dev,
7403 "Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
7404 return -EINVAL;
7405 }
7406
7407 if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
7408 dev_err(&vsi->back->pdev->dev,
7409 "Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
7410 vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7411 vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
7412 }
7413
7414 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
7415 if (!filter)
7416 return -ENOMEM;
7417
7418 filter->cookie = cls_flower->cookie;
7419
7420 err = i40e_parse_cls_flower(vsi, cls_flower, filter);
7421 if (err < 0)
7422 goto err;
7423
7424 err = i40e_handle_tclass(vsi, tc, filter);
7425 if (err < 0)
7426 goto err;
7427
7428 /* Add cloud filter */
7429 if (filter->dst_port)
7430 err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
7431 else
7432 err = i40e_add_del_cloud_filter(vsi, filter, true);
7433
7434 if (err) {
7435 dev_err(&pf->pdev->dev,
7436 "Failed to add cloud filter, err %s\n",
7437 i40e_stat_str(&pf->hw, err));
7438 goto err;
7439 }
7440
7441 /* add filter to the ordered list */
7442 INIT_HLIST_NODE(&filter->cloud_node);
7443
7444 hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
7445
7446 pf->num_cloud_filters++;
7447
7448 return err;
7449 err:
7450 kfree(filter);
7451 return err;
7452 }
7453
7454 /**
7455 * i40e_find_cloud_filter - Find the could filter in the list
7456 * @vsi: Pointer to VSI
7457 * @cookie: filter specific cookie
7458 *
7459 **/
7460 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
7461 unsigned long *cookie)
7462 {
7463 struct i40e_cloud_filter *filter = NULL;
7464 struct hlist_node *node2;
7465
7466 hlist_for_each_entry_safe(filter, node2,
7467 &vsi->back->cloud_filter_list, cloud_node)
7468 if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
7469 return filter;
7470 return NULL;
7471 }
7472
7473 /**
7474 * i40e_delete_clsflower - Remove tc flower filters
7475 * @vsi: Pointer to VSI
7476 * @cls_flower: Pointer to struct tc_cls_flower_offload
7477 *
7478 **/
7479 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
7480 struct tc_cls_flower_offload *cls_flower)
7481 {
7482 struct i40e_cloud_filter *filter = NULL;
7483 struct i40e_pf *pf = vsi->back;
7484 int err = 0;
7485
7486 filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
7487
7488 if (!filter)
7489 return -EINVAL;
7490
7491 hash_del(&filter->cloud_node);
7492
7493 if (filter->dst_port)
7494 err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
7495 else
7496 err = i40e_add_del_cloud_filter(vsi, filter, false);
7497
7498 kfree(filter);
7499 if (err) {
7500 dev_err(&pf->pdev->dev,
7501 "Failed to delete cloud filter, err %s\n",
7502 i40e_stat_str(&pf->hw, err));
7503 return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
7504 }
7505
7506 pf->num_cloud_filters--;
7507 if (!pf->num_cloud_filters)
7508 if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
7509 !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
7510 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
7511 pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
7512 pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
7513 }
7514 return 0;
7515 }
7516
7517 /**
7518 * i40e_setup_tc_cls_flower - flower classifier offloads
7519 * @netdev: net device to configure
7520 * @type_data: offload data
7521 **/
7522 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
7523 struct tc_cls_flower_offload *cls_flower)
7524 {
7525 struct i40e_vsi *vsi = np->vsi;
7526
7527 switch (cls_flower->command) {
7528 case TC_CLSFLOWER_REPLACE:
7529 return i40e_configure_clsflower(vsi, cls_flower);
7530 case TC_CLSFLOWER_DESTROY:
7531 return i40e_delete_clsflower(vsi, cls_flower);
7532 case TC_CLSFLOWER_STATS:
7533 return -EOPNOTSUPP;
7534 default:
7535 return -EOPNOTSUPP;
7536 }
7537 }
7538
7539 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
7540 void *cb_priv)
7541 {
7542 struct i40e_netdev_priv *np = cb_priv;
7543
7544 if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
7545 return -EOPNOTSUPP;
7546
7547 switch (type) {
7548 case TC_SETUP_CLSFLOWER:
7549 return i40e_setup_tc_cls_flower(np, type_data);
7550
7551 default:
7552 return -EOPNOTSUPP;
7553 }
7554 }
7555
7556 static int i40e_setup_tc_block(struct net_device *dev,
7557 struct tc_block_offload *f)
7558 {
7559 struct i40e_netdev_priv *np = netdev_priv(dev);
7560
7561 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
7562 return -EOPNOTSUPP;
7563
7564 switch (f->command) {
7565 case TC_BLOCK_BIND:
7566 return tcf_block_cb_register(f->block, i40e_setup_tc_block_cb,
7567 np, np, f->extack);
7568 case TC_BLOCK_UNBIND:
7569 tcf_block_cb_unregister(f->block, i40e_setup_tc_block_cb, np);
7570 return 0;
7571 default:
7572 return -EOPNOTSUPP;
7573 }
7574 }
7575
7576 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
7577 void *type_data)
7578 {
7579 switch (type) {
7580 case TC_SETUP_QDISC_MQPRIO:
7581 return i40e_setup_tc(netdev, type_data);
7582 case TC_SETUP_BLOCK:
7583 return i40e_setup_tc_block(netdev, type_data);
7584 default:
7585 return -EOPNOTSUPP;
7586 }
7587 }
7588
7589 /**
7590 * i40e_open - Called when a network interface is made active
7591 * @netdev: network interface device structure
7592 *
7593 * The open entry point is called when a network interface is made
7594 * active by the system (IFF_UP). At this point all resources needed
7595 * for transmit and receive operations are allocated, the interrupt
7596 * handler is registered with the OS, the netdev watchdog subtask is
7597 * enabled, and the stack is notified that the interface is ready.
7598 *
7599 * Returns 0 on success, negative value on failure
7600 **/
7601 int i40e_open(struct net_device *netdev)
7602 {
7603 struct i40e_netdev_priv *np = netdev_priv(netdev);
7604 struct i40e_vsi *vsi = np->vsi;
7605 struct i40e_pf *pf = vsi->back;
7606 int err;
7607
7608 /* disallow open during test or if eeprom is broken */
7609 if (test_bit(__I40E_TESTING, pf->state) ||
7610 test_bit(__I40E_BAD_EEPROM, pf->state))
7611 return -EBUSY;
7612
7613 netif_carrier_off(netdev);
7614
7615 if (i40e_force_link_state(pf, true))
7616 return -EAGAIN;
7617
7618 err = i40e_vsi_open(vsi);
7619 if (err)
7620 return err;
7621
7622 /* configure global TSO hardware offload settings */
7623 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
7624 TCP_FLAG_FIN) >> 16);
7625 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
7626 TCP_FLAG_FIN |
7627 TCP_FLAG_CWR) >> 16);
7628 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
7629
7630 udp_tunnel_get_rx_info(netdev);
7631
7632 return 0;
7633 }
7634
7635 /**
7636 * i40e_vsi_open -
7637 * @vsi: the VSI to open
7638 *
7639 * Finish initialization of the VSI.
7640 *
7641 * Returns 0 on success, negative value on failure
7642 *
7643 * Note: expects to be called while under rtnl_lock()
7644 **/
7645 int i40e_vsi_open(struct i40e_vsi *vsi)
7646 {
7647 struct i40e_pf *pf = vsi->back;
7648 char int_name[I40E_INT_NAME_STR_LEN];
7649 int err;
7650
7651 /* allocate descriptors */
7652 err = i40e_vsi_setup_tx_resources(vsi);
7653 if (err)
7654 goto err_setup_tx;
7655 err = i40e_vsi_setup_rx_resources(vsi);
7656 if (err)
7657 goto err_setup_rx;
7658
7659 err = i40e_vsi_configure(vsi);
7660 if (err)
7661 goto err_setup_rx;
7662
7663 if (vsi->netdev) {
7664 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
7665 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
7666 err = i40e_vsi_request_irq(vsi, int_name);
7667 if (err)
7668 goto err_setup_rx;
7669
7670 /* Notify the stack of the actual queue counts. */
7671 err = netif_set_real_num_tx_queues(vsi->netdev,
7672 vsi->num_queue_pairs);
7673 if (err)
7674 goto err_set_queues;
7675
7676 err = netif_set_real_num_rx_queues(vsi->netdev,
7677 vsi->num_queue_pairs);
7678 if (err)
7679 goto err_set_queues;
7680
7681 } else if (vsi->type == I40E_VSI_FDIR) {
7682 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
7683 dev_driver_string(&pf->pdev->dev),
7684 dev_name(&pf->pdev->dev));
7685 err = i40e_vsi_request_irq(vsi, int_name);
7686
7687 } else {
7688 err = -EINVAL;
7689 goto err_setup_rx;
7690 }
7691
7692 err = i40e_up_complete(vsi);
7693 if (err)
7694 goto err_up_complete;
7695
7696 return 0;
7697
7698 err_up_complete:
7699 i40e_down(vsi);
7700 err_set_queues:
7701 i40e_vsi_free_irq(vsi);
7702 err_setup_rx:
7703 i40e_vsi_free_rx_resources(vsi);
7704 err_setup_tx:
7705 i40e_vsi_free_tx_resources(vsi);
7706 if (vsi == pf->vsi[pf->lan_vsi])
7707 i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
7708
7709 return err;
7710 }
7711
7712 /**
7713 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
7714 * @pf: Pointer to PF
7715 *
7716 * This function destroys the hlist where all the Flow Director
7717 * filters were saved.
7718 **/
7719 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
7720 {
7721 struct i40e_fdir_filter *filter;
7722 struct i40e_flex_pit *pit_entry, *tmp;
7723 struct hlist_node *node2;
7724
7725 hlist_for_each_entry_safe(filter, node2,
7726 &pf->fdir_filter_list, fdir_node) {
7727 hlist_del(&filter->fdir_node);
7728 kfree(filter);
7729 }
7730
7731 list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
7732 list_del(&pit_entry->list);
7733 kfree(pit_entry);
7734 }
7735 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
7736
7737 list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
7738 list_del(&pit_entry->list);
7739 kfree(pit_entry);
7740 }
7741 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
7742
7743 pf->fdir_pf_active_filters = 0;
7744 pf->fd_tcp4_filter_cnt = 0;
7745 pf->fd_udp4_filter_cnt = 0;
7746 pf->fd_sctp4_filter_cnt = 0;
7747 pf->fd_ip4_filter_cnt = 0;
7748
7749 /* Reprogram the default input set for TCP/IPv4 */
7750 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
7751 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
7752 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
7753
7754 /* Reprogram the default input set for UDP/IPv4 */
7755 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
7756 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
7757 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
7758
7759 /* Reprogram the default input set for SCTP/IPv4 */
7760 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
7761 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
7762 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
7763
7764 /* Reprogram the default input set for Other/IPv4 */
7765 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
7766 I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
7767
7768 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
7769 I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
7770 }
7771
7772 /**
7773 * i40e_cloud_filter_exit - Cleans up the cloud filters
7774 * @pf: Pointer to PF
7775 *
7776 * This function destroys the hlist where all the cloud filters
7777 * were saved.
7778 **/
7779 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
7780 {
7781 struct i40e_cloud_filter *cfilter;
7782 struct hlist_node *node;
7783
7784 hlist_for_each_entry_safe(cfilter, node,
7785 &pf->cloud_filter_list, cloud_node) {
7786 hlist_del(&cfilter->cloud_node);
7787 kfree(cfilter);
7788 }
7789 pf->num_cloud_filters = 0;
7790
7791 if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
7792 !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
7793 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
7794 pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
7795 pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
7796 }
7797 }
7798
7799 /**
7800 * i40e_close - Disables a network interface
7801 * @netdev: network interface device structure
7802 *
7803 * The close entry point is called when an interface is de-activated
7804 * by the OS. The hardware is still under the driver's control, but
7805 * this netdev interface is disabled.
7806 *
7807 * Returns 0, this is not allowed to fail
7808 **/
7809 int i40e_close(struct net_device *netdev)
7810 {
7811 struct i40e_netdev_priv *np = netdev_priv(netdev);
7812 struct i40e_vsi *vsi = np->vsi;
7813
7814 i40e_vsi_close(vsi);
7815
7816 return 0;
7817 }
7818
7819 /**
7820 * i40e_do_reset - Start a PF or Core Reset sequence
7821 * @pf: board private structure
7822 * @reset_flags: which reset is requested
7823 * @lock_acquired: indicates whether or not the lock has been acquired
7824 * before this function was called.
7825 *
7826 * The essential difference in resets is that the PF Reset
7827 * doesn't clear the packet buffers, doesn't reset the PE
7828 * firmware, and doesn't bother the other PFs on the chip.
7829 **/
7830 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
7831 {
7832 u32 val;
7833
7834 WARN_ON(in_interrupt());
7835
7836
7837 /* do the biggest reset indicated */
7838 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
7839
7840 /* Request a Global Reset
7841 *
7842 * This will start the chip's countdown to the actual full
7843 * chip reset event, and a warning interrupt to be sent
7844 * to all PFs, including the requestor. Our handler
7845 * for the warning interrupt will deal with the shutdown
7846 * and recovery of the switch setup.
7847 */
7848 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
7849 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
7850 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
7851 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
7852
7853 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
7854
7855 /* Request a Core Reset
7856 *
7857 * Same as Global Reset, except does *not* include the MAC/PHY
7858 */
7859 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
7860 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
7861 val |= I40E_GLGEN_RTRIG_CORER_MASK;
7862 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
7863 i40e_flush(&pf->hw);
7864
7865 } else if (reset_flags & I40E_PF_RESET_FLAG) {
7866
7867 /* Request a PF Reset
7868 *
7869 * Resets only the PF-specific registers
7870 *
7871 * This goes directly to the tear-down and rebuild of
7872 * the switch, since we need to do all the recovery as
7873 * for the Core Reset.
7874 */
7875 dev_dbg(&pf->pdev->dev, "PFR requested\n");
7876 i40e_handle_reset_warning(pf, lock_acquired);
7877
7878 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
7879 int v;
7880
7881 /* Find the VSI(s) that requested a re-init */
7882 dev_info(&pf->pdev->dev,
7883 "VSI reinit requested\n");
7884 for (v = 0; v < pf->num_alloc_vsi; v++) {
7885 struct i40e_vsi *vsi = pf->vsi[v];
7886
7887 if (vsi != NULL &&
7888 test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
7889 vsi->state))
7890 i40e_vsi_reinit_locked(pf->vsi[v]);
7891 }
7892 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
7893 int v;
7894
7895 /* Find the VSI(s) that needs to be brought down */
7896 dev_info(&pf->pdev->dev, "VSI down requested\n");
7897 for (v = 0; v < pf->num_alloc_vsi; v++) {
7898 struct i40e_vsi *vsi = pf->vsi[v];
7899
7900 if (vsi != NULL &&
7901 test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
7902 vsi->state)) {
7903 set_bit(__I40E_VSI_DOWN, vsi->state);
7904 i40e_down(vsi);
7905 }
7906 }
7907 } else {
7908 dev_info(&pf->pdev->dev,
7909 "bad reset request 0x%08x\n", reset_flags);
7910 }
7911 }
7912
7913 #ifdef CONFIG_I40E_DCB
7914 /**
7915 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
7916 * @pf: board private structure
7917 * @old_cfg: current DCB config
7918 * @new_cfg: new DCB config
7919 **/
7920 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
7921 struct i40e_dcbx_config *old_cfg,
7922 struct i40e_dcbx_config *new_cfg)
7923 {
7924 bool need_reconfig = false;
7925
7926 /* Check if ETS configuration has changed */
7927 if (memcmp(&new_cfg->etscfg,
7928 &old_cfg->etscfg,
7929 sizeof(new_cfg->etscfg))) {
7930 /* If Priority Table has changed reconfig is needed */
7931 if (memcmp(&new_cfg->etscfg.prioritytable,
7932 &old_cfg->etscfg.prioritytable,
7933 sizeof(new_cfg->etscfg.prioritytable))) {
7934 need_reconfig = true;
7935 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
7936 }
7937
7938 if (memcmp(&new_cfg->etscfg.tcbwtable,
7939 &old_cfg->etscfg.tcbwtable,
7940 sizeof(new_cfg->etscfg.tcbwtable)))
7941 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
7942
7943 if (memcmp(&new_cfg->etscfg.tsatable,
7944 &old_cfg->etscfg.tsatable,
7945 sizeof(new_cfg->etscfg.tsatable)))
7946 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
7947 }
7948
7949 /* Check if PFC configuration has changed */
7950 if (memcmp(&new_cfg->pfc,
7951 &old_cfg->pfc,
7952 sizeof(new_cfg->pfc))) {
7953 need_reconfig = true;
7954 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
7955 }
7956
7957 /* Check if APP Table has changed */
7958 if (memcmp(&new_cfg->app,
7959 &old_cfg->app,
7960 sizeof(new_cfg->app))) {
7961 need_reconfig = true;
7962 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
7963 }
7964
7965 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
7966 return need_reconfig;
7967 }
7968
7969 /**
7970 * i40e_handle_lldp_event - Handle LLDP Change MIB event
7971 * @pf: board private structure
7972 * @e: event info posted on ARQ
7973 **/
7974 static int i40e_handle_lldp_event(struct i40e_pf *pf,
7975 struct i40e_arq_event_info *e)
7976 {
7977 struct i40e_aqc_lldp_get_mib *mib =
7978 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
7979 struct i40e_hw *hw = &pf->hw;
7980 struct i40e_dcbx_config tmp_dcbx_cfg;
7981 bool need_reconfig = false;
7982 int ret = 0;
7983 u8 type;
7984
7985 /* Not DCB capable or capability disabled */
7986 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
7987 return ret;
7988
7989 /* Ignore if event is not for Nearest Bridge */
7990 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
7991 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
7992 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
7993 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
7994 return ret;
7995
7996 /* Check MIB Type and return if event for Remote MIB update */
7997 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
7998 dev_dbg(&pf->pdev->dev,
7999 "LLDP event mib type %s\n", type ? "remote" : "local");
8000 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
8001 /* Update the remote cached instance and return */
8002 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
8003 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
8004 &hw->remote_dcbx_config);
8005 goto exit;
8006 }
8007
8008 /* Store the old configuration */
8009 tmp_dcbx_cfg = hw->local_dcbx_config;
8010
8011 /* Reset the old DCBx configuration data */
8012 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
8013 /* Get updated DCBX data from firmware */
8014 ret = i40e_get_dcb_config(&pf->hw);
8015 if (ret) {
8016 dev_info(&pf->pdev->dev,
8017 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
8018 i40e_stat_str(&pf->hw, ret),
8019 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8020 goto exit;
8021 }
8022
8023 /* No change detected in DCBX configs */
8024 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
8025 sizeof(tmp_dcbx_cfg))) {
8026 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
8027 goto exit;
8028 }
8029
8030 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
8031 &hw->local_dcbx_config);
8032
8033 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
8034
8035 if (!need_reconfig)
8036 goto exit;
8037
8038 /* Enable DCB tagging only when more than one TC */
8039 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
8040 pf->flags |= I40E_FLAG_DCB_ENABLED;
8041 else
8042 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8043
8044 set_bit(__I40E_PORT_SUSPENDED, pf->state);
8045 /* Reconfiguration needed quiesce all VSIs */
8046 i40e_pf_quiesce_all_vsi(pf);
8047
8048 /* Changes in configuration update VEB/VSI */
8049 i40e_dcb_reconfigure(pf);
8050
8051 ret = i40e_resume_port_tx(pf);
8052
8053 clear_bit(__I40E_PORT_SUSPENDED, pf->state);
8054 /* In case of error no point in resuming VSIs */
8055 if (ret)
8056 goto exit;
8057
8058 /* Wait for the PF's queues to be disabled */
8059 ret = i40e_pf_wait_queues_disabled(pf);
8060 if (ret) {
8061 /* Schedule PF reset to recover */
8062 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
8063 i40e_service_event_schedule(pf);
8064 } else {
8065 i40e_pf_unquiesce_all_vsi(pf);
8066 set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
8067 set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
8068 }
8069
8070 exit:
8071 return ret;
8072 }
8073 #endif /* CONFIG_I40E_DCB */
8074
8075 /**
8076 * i40e_do_reset_safe - Protected reset path for userland calls.
8077 * @pf: board private structure
8078 * @reset_flags: which reset is requested
8079 *
8080 **/
8081 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
8082 {
8083 rtnl_lock();
8084 i40e_do_reset(pf, reset_flags, true);
8085 rtnl_unlock();
8086 }
8087
8088 /**
8089 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
8090 * @pf: board private structure
8091 * @e: event info posted on ARQ
8092 *
8093 * Handler for LAN Queue Overflow Event generated by the firmware for PF
8094 * and VF queues
8095 **/
8096 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
8097 struct i40e_arq_event_info *e)
8098 {
8099 struct i40e_aqc_lan_overflow *data =
8100 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
8101 u32 queue = le32_to_cpu(data->prtdcb_rupto);
8102 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
8103 struct i40e_hw *hw = &pf->hw;
8104 struct i40e_vf *vf;
8105 u16 vf_id;
8106
8107 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
8108 queue, qtx_ctl);
8109
8110 /* Queue belongs to VF, find the VF and issue VF reset */
8111 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
8112 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
8113 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
8114 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
8115 vf_id -= hw->func_caps.vf_base_id;
8116 vf = &pf->vf[vf_id];
8117 i40e_vc_notify_vf_reset(vf);
8118 /* Allow VF to process pending reset notification */
8119 msleep(20);
8120 i40e_reset_vf(vf, false);
8121 }
8122 }
8123
8124 /**
8125 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
8126 * @pf: board private structure
8127 **/
8128 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
8129 {
8130 u32 val, fcnt_prog;
8131
8132 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
8133 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
8134 return fcnt_prog;
8135 }
8136
8137 /**
8138 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
8139 * @pf: board private structure
8140 **/
8141 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
8142 {
8143 u32 val, fcnt_prog;
8144
8145 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
8146 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
8147 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
8148 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
8149 return fcnt_prog;
8150 }
8151
8152 /**
8153 * i40e_get_global_fd_count - Get total FD filters programmed on device
8154 * @pf: board private structure
8155 **/
8156 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
8157 {
8158 u32 val, fcnt_prog;
8159
8160 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
8161 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
8162 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
8163 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
8164 return fcnt_prog;
8165 }
8166
8167 /**
8168 * i40e_reenable_fdir_sb - Restore FDir SB capability
8169 * @pf: board private structure
8170 **/
8171 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
8172 {
8173 if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
8174 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
8175 (I40E_DEBUG_FD & pf->hw.debug_mask))
8176 dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
8177 }
8178
8179 /**
8180 * i40e_reenable_fdir_atr - Restore FDir ATR capability
8181 * @pf: board private structure
8182 **/
8183 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
8184 {
8185 if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
8186 /* ATR uses the same filtering logic as SB rules. It only
8187 * functions properly if the input set mask is at the default
8188 * settings. It is safe to restore the default input set
8189 * because there are no active TCPv4 filter rules.
8190 */
8191 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
8192 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8193 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8194
8195 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
8196 (I40E_DEBUG_FD & pf->hw.debug_mask))
8197 dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
8198 }
8199 }
8200
8201 /**
8202 * i40e_delete_invalid_filter - Delete an invalid FDIR filter
8203 * @pf: board private structure
8204 * @filter: FDir filter to remove
8205 */
8206 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
8207 struct i40e_fdir_filter *filter)
8208 {
8209 /* Update counters */
8210 pf->fdir_pf_active_filters--;
8211 pf->fd_inv = 0;
8212
8213 switch (filter->flow_type) {
8214 case TCP_V4_FLOW:
8215 pf->fd_tcp4_filter_cnt--;
8216 break;
8217 case UDP_V4_FLOW:
8218 pf->fd_udp4_filter_cnt--;
8219 break;
8220 case SCTP_V4_FLOW:
8221 pf->fd_sctp4_filter_cnt--;
8222 break;
8223 case IP_USER_FLOW:
8224 switch (filter->ip4_proto) {
8225 case IPPROTO_TCP:
8226 pf->fd_tcp4_filter_cnt--;
8227 break;
8228 case IPPROTO_UDP:
8229 pf->fd_udp4_filter_cnt--;
8230 break;
8231 case IPPROTO_SCTP:
8232 pf->fd_sctp4_filter_cnt--;
8233 break;
8234 case IPPROTO_IP:
8235 pf->fd_ip4_filter_cnt--;
8236 break;
8237 }
8238 break;
8239 }
8240
8241 /* Remove the filter from the list and free memory */
8242 hlist_del(&filter->fdir_node);
8243 kfree(filter);
8244 }
8245
8246 /**
8247 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
8248 * @pf: board private structure
8249 **/
8250 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
8251 {
8252 struct i40e_fdir_filter *filter;
8253 u32 fcnt_prog, fcnt_avail;
8254 struct hlist_node *node;
8255
8256 if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
8257 return;
8258
8259 /* Check if we have enough room to re-enable FDir SB capability. */
8260 fcnt_prog = i40e_get_global_fd_count(pf);
8261 fcnt_avail = pf->fdir_pf_filter_count;
8262 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
8263 (pf->fd_add_err == 0) ||
8264 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
8265 i40e_reenable_fdir_sb(pf);
8266
8267 /* We should wait for even more space before re-enabling ATR.
8268 * Additionally, we cannot enable ATR as long as we still have TCP SB
8269 * rules active.
8270 */
8271 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
8272 (pf->fd_tcp4_filter_cnt == 0))
8273 i40e_reenable_fdir_atr(pf);
8274
8275 /* if hw had a problem adding a filter, delete it */
8276 if (pf->fd_inv > 0) {
8277 hlist_for_each_entry_safe(filter, node,
8278 &pf->fdir_filter_list, fdir_node)
8279 if (filter->fd_id == pf->fd_inv)
8280 i40e_delete_invalid_filter(pf, filter);
8281 }
8282 }
8283
8284 #define I40E_MIN_FD_FLUSH_INTERVAL 10
8285 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
8286 /**
8287 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
8288 * @pf: board private structure
8289 **/
8290 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
8291 {
8292 unsigned long min_flush_time;
8293 int flush_wait_retry = 50;
8294 bool disable_atr = false;
8295 int fd_room;
8296 int reg;
8297
8298 if (!time_after(jiffies, pf->fd_flush_timestamp +
8299 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
8300 return;
8301
8302 /* If the flush is happening too quick and we have mostly SB rules we
8303 * should not re-enable ATR for some time.
8304 */
8305 min_flush_time = pf->fd_flush_timestamp +
8306 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
8307 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
8308
8309 if (!(time_after(jiffies, min_flush_time)) &&
8310 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
8311 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8312 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
8313 disable_atr = true;
8314 }
8315
8316 pf->fd_flush_timestamp = jiffies;
8317 set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
8318 /* flush all filters */
8319 wr32(&pf->hw, I40E_PFQF_CTL_1,
8320 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
8321 i40e_flush(&pf->hw);
8322 pf->fd_flush_cnt++;
8323 pf->fd_add_err = 0;
8324 do {
8325 /* Check FD flush status every 5-6msec */
8326 usleep_range(5000, 6000);
8327 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
8328 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
8329 break;
8330 } while (flush_wait_retry--);
8331 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
8332 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
8333 } else {
8334 /* replay sideband filters */
8335 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
8336 if (!disable_atr && !pf->fd_tcp4_filter_cnt)
8337 clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
8338 clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
8339 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8340 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
8341 }
8342 }
8343
8344 /**
8345 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
8346 * @pf: board private structure
8347 **/
8348 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
8349 {
8350 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
8351 }
8352
8353 /* We can see up to 256 filter programming desc in transit if the filters are
8354 * being applied really fast; before we see the first
8355 * filter miss error on Rx queue 0. Accumulating enough error messages before
8356 * reacting will make sure we don't cause flush too often.
8357 */
8358 #define I40E_MAX_FD_PROGRAM_ERROR 256
8359
8360 /**
8361 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
8362 * @pf: board private structure
8363 **/
8364 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
8365 {
8366
8367 /* if interface is down do nothing */
8368 if (test_bit(__I40E_DOWN, pf->state))
8369 return;
8370
8371 if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
8372 i40e_fdir_flush_and_replay(pf);
8373
8374 i40e_fdir_check_and_reenable(pf);
8375
8376 }
8377
8378 /**
8379 * i40e_vsi_link_event - notify VSI of a link event
8380 * @vsi: vsi to be notified
8381 * @link_up: link up or down
8382 **/
8383 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
8384 {
8385 if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
8386 return;
8387
8388 switch (vsi->type) {
8389 case I40E_VSI_MAIN:
8390 if (!vsi->netdev || !vsi->netdev_registered)
8391 break;
8392
8393 if (link_up) {
8394 netif_carrier_on(vsi->netdev);
8395 netif_tx_wake_all_queues(vsi->netdev);
8396 } else {
8397 netif_carrier_off(vsi->netdev);
8398 netif_tx_stop_all_queues(vsi->netdev);
8399 }
8400 break;
8401
8402 case I40E_VSI_SRIOV:
8403 case I40E_VSI_VMDQ2:
8404 case I40E_VSI_CTRL:
8405 case I40E_VSI_IWARP:
8406 case I40E_VSI_MIRROR:
8407 default:
8408 /* there is no notification for other VSIs */
8409 break;
8410 }
8411 }
8412
8413 /**
8414 * i40e_veb_link_event - notify elements on the veb of a link event
8415 * @veb: veb to be notified
8416 * @link_up: link up or down
8417 **/
8418 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
8419 {
8420 struct i40e_pf *pf;
8421 int i;
8422
8423 if (!veb || !veb->pf)
8424 return;
8425 pf = veb->pf;
8426
8427 /* depth first... */
8428 for (i = 0; i < I40E_MAX_VEB; i++)
8429 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
8430 i40e_veb_link_event(pf->veb[i], link_up);
8431
8432 /* ... now the local VSIs */
8433 for (i = 0; i < pf->num_alloc_vsi; i++)
8434 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
8435 i40e_vsi_link_event(pf->vsi[i], link_up);
8436 }
8437
8438 /**
8439 * i40e_link_event - Update netif_carrier status
8440 * @pf: board private structure
8441 **/
8442 static void i40e_link_event(struct i40e_pf *pf)
8443 {
8444 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8445 u8 new_link_speed, old_link_speed;
8446 i40e_status status;
8447 bool new_link, old_link;
8448
8449 /* save off old link status information */
8450 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
8451
8452 /* set this to force the get_link_status call to refresh state */
8453 pf->hw.phy.get_link_info = true;
8454
8455 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
8456
8457 status = i40e_get_link_status(&pf->hw, &new_link);
8458
8459 /* On success, disable temp link polling */
8460 if (status == I40E_SUCCESS) {
8461 clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
8462 } else {
8463 /* Enable link polling temporarily until i40e_get_link_status
8464 * returns I40E_SUCCESS
8465 */
8466 set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
8467 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
8468 status);
8469 return;
8470 }
8471
8472 old_link_speed = pf->hw.phy.link_info_old.link_speed;
8473 new_link_speed = pf->hw.phy.link_info.link_speed;
8474
8475 if (new_link == old_link &&
8476 new_link_speed == old_link_speed &&
8477 (test_bit(__I40E_VSI_DOWN, vsi->state) ||
8478 new_link == netif_carrier_ok(vsi->netdev)))
8479 return;
8480
8481 i40e_print_link_message(vsi, new_link);
8482
8483 /* Notify the base of the switch tree connected to
8484 * the link. Floating VEBs are not notified.
8485 */
8486 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
8487 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
8488 else
8489 i40e_vsi_link_event(vsi, new_link);
8490
8491 if (pf->vf)
8492 i40e_vc_notify_link_state(pf);
8493
8494 if (pf->flags & I40E_FLAG_PTP)
8495 i40e_ptp_set_increment(pf);
8496 }
8497
8498 /**
8499 * i40e_watchdog_subtask - periodic checks not using event driven response
8500 * @pf: board private structure
8501 **/
8502 static void i40e_watchdog_subtask(struct i40e_pf *pf)
8503 {
8504 int i;
8505
8506 /* if interface is down do nothing */
8507 if (test_bit(__I40E_DOWN, pf->state) ||
8508 test_bit(__I40E_CONFIG_BUSY, pf->state))
8509 return;
8510
8511 /* make sure we don't do these things too often */
8512 if (time_before(jiffies, (pf->service_timer_previous +
8513 pf->service_timer_period)))
8514 return;
8515 pf->service_timer_previous = jiffies;
8516
8517 if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
8518 test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
8519 i40e_link_event(pf);
8520
8521 /* Update the stats for active netdevs so the network stack
8522 * can look at updated numbers whenever it cares to
8523 */
8524 for (i = 0; i < pf->num_alloc_vsi; i++)
8525 if (pf->vsi[i] && pf->vsi[i]->netdev)
8526 i40e_update_stats(pf->vsi[i]);
8527
8528 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
8529 /* Update the stats for the active switching components */
8530 for (i = 0; i < I40E_MAX_VEB; i++)
8531 if (pf->veb[i])
8532 i40e_update_veb_stats(pf->veb[i]);
8533 }
8534
8535 i40e_ptp_rx_hang(pf);
8536 i40e_ptp_tx_hang(pf);
8537 }
8538
8539 /**
8540 * i40e_reset_subtask - Set up for resetting the device and driver
8541 * @pf: board private structure
8542 **/
8543 static void i40e_reset_subtask(struct i40e_pf *pf)
8544 {
8545 u32 reset_flags = 0;
8546
8547 if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
8548 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
8549 clear_bit(__I40E_REINIT_REQUESTED, pf->state);
8550 }
8551 if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
8552 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
8553 clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
8554 }
8555 if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
8556 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
8557 clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
8558 }
8559 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
8560 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
8561 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
8562 }
8563 if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
8564 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
8565 clear_bit(__I40E_DOWN_REQUESTED, pf->state);
8566 }
8567
8568 /* If there's a recovery already waiting, it takes
8569 * precedence before starting a new reset sequence.
8570 */
8571 if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
8572 i40e_prep_for_reset(pf, false);
8573 i40e_reset(pf);
8574 i40e_rebuild(pf, false, false);
8575 }
8576
8577 /* If we're already down or resetting, just bail */
8578 if (reset_flags &&
8579 !test_bit(__I40E_DOWN, pf->state) &&
8580 !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
8581 i40e_do_reset(pf, reset_flags, false);
8582 }
8583 }
8584
8585 /**
8586 * i40e_handle_link_event - Handle link event
8587 * @pf: board private structure
8588 * @e: event info posted on ARQ
8589 **/
8590 static void i40e_handle_link_event(struct i40e_pf *pf,
8591 struct i40e_arq_event_info *e)
8592 {
8593 struct i40e_aqc_get_link_status *status =
8594 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
8595
8596 /* Do a new status request to re-enable LSE reporting
8597 * and load new status information into the hw struct
8598 * This completely ignores any state information
8599 * in the ARQ event info, instead choosing to always
8600 * issue the AQ update link status command.
8601 */
8602 i40e_link_event(pf);
8603
8604 /* Check if module meets thermal requirements */
8605 if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
8606 dev_err(&pf->pdev->dev,
8607 "Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
8608 dev_err(&pf->pdev->dev,
8609 "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
8610 } else {
8611 /* check for unqualified module, if link is down, suppress
8612 * the message if link was forced to be down.
8613 */
8614 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
8615 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
8616 (!(status->link_info & I40E_AQ_LINK_UP)) &&
8617 (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
8618 dev_err(&pf->pdev->dev,
8619 "Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
8620 dev_err(&pf->pdev->dev,
8621 "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
8622 }
8623 }
8624 }
8625
8626 /**
8627 * i40e_clean_adminq_subtask - Clean the AdminQ rings
8628 * @pf: board private structure
8629 **/
8630 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
8631 {
8632 struct i40e_arq_event_info event;
8633 struct i40e_hw *hw = &pf->hw;
8634 u16 pending, i = 0;
8635 i40e_status ret;
8636 u16 opcode;
8637 u32 oldval;
8638 u32 val;
8639
8640 /* Do not run clean AQ when PF reset fails */
8641 if (test_bit(__I40E_RESET_FAILED, pf->state))
8642 return;
8643
8644 /* check for error indications */
8645 val = rd32(&pf->hw, pf->hw.aq.arq.len);
8646 oldval = val;
8647 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
8648 if (hw->debug_mask & I40E_DEBUG_AQ)
8649 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
8650 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
8651 }
8652 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
8653 if (hw->debug_mask & I40E_DEBUG_AQ)
8654 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
8655 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
8656 pf->arq_overflows++;
8657 }
8658 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
8659 if (hw->debug_mask & I40E_DEBUG_AQ)
8660 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
8661 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
8662 }
8663 if (oldval != val)
8664 wr32(&pf->hw, pf->hw.aq.arq.len, val);
8665
8666 val = rd32(&pf->hw, pf->hw.aq.asq.len);
8667 oldval = val;
8668 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
8669 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
8670 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
8671 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
8672 }
8673 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
8674 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
8675 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
8676 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
8677 }
8678 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
8679 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
8680 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
8681 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
8682 }
8683 if (oldval != val)
8684 wr32(&pf->hw, pf->hw.aq.asq.len, val);
8685
8686 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
8687 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
8688 if (!event.msg_buf)
8689 return;
8690
8691 do {
8692 ret = i40e_clean_arq_element(hw, &event, &pending);
8693 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
8694 break;
8695 else if (ret) {
8696 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
8697 break;
8698 }
8699
8700 opcode = le16_to_cpu(event.desc.opcode);
8701 switch (opcode) {
8702
8703 case i40e_aqc_opc_get_link_status:
8704 i40e_handle_link_event(pf, &event);
8705 break;
8706 case i40e_aqc_opc_send_msg_to_pf:
8707 ret = i40e_vc_process_vf_msg(pf,
8708 le16_to_cpu(event.desc.retval),
8709 le32_to_cpu(event.desc.cookie_high),
8710 le32_to_cpu(event.desc.cookie_low),
8711 event.msg_buf,
8712 event.msg_len);
8713 break;
8714 case i40e_aqc_opc_lldp_update_mib:
8715 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
8716 #ifdef CONFIG_I40E_DCB
8717 rtnl_lock();
8718 ret = i40e_handle_lldp_event(pf, &event);
8719 rtnl_unlock();
8720 #endif /* CONFIG_I40E_DCB */
8721 break;
8722 case i40e_aqc_opc_event_lan_overflow:
8723 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
8724 i40e_handle_lan_overflow_event(pf, &event);
8725 break;
8726 case i40e_aqc_opc_send_msg_to_peer:
8727 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
8728 break;
8729 case i40e_aqc_opc_nvm_erase:
8730 case i40e_aqc_opc_nvm_update:
8731 case i40e_aqc_opc_oem_post_update:
8732 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
8733 "ARQ NVM operation 0x%04x completed\n",
8734 opcode);
8735 break;
8736 default:
8737 dev_info(&pf->pdev->dev,
8738 "ARQ: Unknown event 0x%04x ignored\n",
8739 opcode);
8740 break;
8741 }
8742 } while (i++ < pf->adminq_work_limit);
8743
8744 if (i < pf->adminq_work_limit)
8745 clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
8746
8747 /* re-enable Admin queue interrupt cause */
8748 val = rd32(hw, I40E_PFINT_ICR0_ENA);
8749 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
8750 wr32(hw, I40E_PFINT_ICR0_ENA, val);
8751 i40e_flush(hw);
8752
8753 kfree(event.msg_buf);
8754 }
8755
8756 /**
8757 * i40e_verify_eeprom - make sure eeprom is good to use
8758 * @pf: board private structure
8759 **/
8760 static void i40e_verify_eeprom(struct i40e_pf *pf)
8761 {
8762 int err;
8763
8764 err = i40e_diag_eeprom_test(&pf->hw);
8765 if (err) {
8766 /* retry in case of garbage read */
8767 err = i40e_diag_eeprom_test(&pf->hw);
8768 if (err) {
8769 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
8770 err);
8771 set_bit(__I40E_BAD_EEPROM, pf->state);
8772 }
8773 }
8774
8775 if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
8776 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
8777 clear_bit(__I40E_BAD_EEPROM, pf->state);
8778 }
8779 }
8780
8781 /**
8782 * i40e_enable_pf_switch_lb
8783 * @pf: pointer to the PF structure
8784 *
8785 * enable switch loop back or die - no point in a return value
8786 **/
8787 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
8788 {
8789 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8790 struct i40e_vsi_context ctxt;
8791 int ret;
8792
8793 ctxt.seid = pf->main_vsi_seid;
8794 ctxt.pf_num = pf->hw.pf_id;
8795 ctxt.vf_num = 0;
8796 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
8797 if (ret) {
8798 dev_info(&pf->pdev->dev,
8799 "couldn't get PF vsi config, err %s aq_err %s\n",
8800 i40e_stat_str(&pf->hw, ret),
8801 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8802 return;
8803 }
8804 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
8805 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
8806 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
8807
8808 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
8809 if (ret) {
8810 dev_info(&pf->pdev->dev,
8811 "update vsi switch failed, err %s aq_err %s\n",
8812 i40e_stat_str(&pf->hw, ret),
8813 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8814 }
8815 }
8816
8817 /**
8818 * i40e_disable_pf_switch_lb
8819 * @pf: pointer to the PF structure
8820 *
8821 * disable switch loop back or die - no point in a return value
8822 **/
8823 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
8824 {
8825 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8826 struct i40e_vsi_context ctxt;
8827 int ret;
8828
8829 ctxt.seid = pf->main_vsi_seid;
8830 ctxt.pf_num = pf->hw.pf_id;
8831 ctxt.vf_num = 0;
8832 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
8833 if (ret) {
8834 dev_info(&pf->pdev->dev,
8835 "couldn't get PF vsi config, err %s aq_err %s\n",
8836 i40e_stat_str(&pf->hw, ret),
8837 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8838 return;
8839 }
8840 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
8841 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
8842 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
8843
8844 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
8845 if (ret) {
8846 dev_info(&pf->pdev->dev,
8847 "update vsi switch failed, err %s aq_err %s\n",
8848 i40e_stat_str(&pf->hw, ret),
8849 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8850 }
8851 }
8852
8853 /**
8854 * i40e_config_bridge_mode - Configure the HW bridge mode
8855 * @veb: pointer to the bridge instance
8856 *
8857 * Configure the loop back mode for the LAN VSI that is downlink to the
8858 * specified HW bridge instance. It is expected this function is called
8859 * when a new HW bridge is instantiated.
8860 **/
8861 static void i40e_config_bridge_mode(struct i40e_veb *veb)
8862 {
8863 struct i40e_pf *pf = veb->pf;
8864
8865 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
8866 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
8867 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
8868 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
8869 i40e_disable_pf_switch_lb(pf);
8870 else
8871 i40e_enable_pf_switch_lb(pf);
8872 }
8873
8874 /**
8875 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
8876 * @veb: pointer to the VEB instance
8877 *
8878 * This is a recursive function that first builds the attached VSIs then
8879 * recurses in to build the next layer of VEB. We track the connections
8880 * through our own index numbers because the seid's from the HW could
8881 * change across the reset.
8882 **/
8883 static int i40e_reconstitute_veb(struct i40e_veb *veb)
8884 {
8885 struct i40e_vsi *ctl_vsi = NULL;
8886 struct i40e_pf *pf = veb->pf;
8887 int v, veb_idx;
8888 int ret;
8889
8890 /* build VSI that owns this VEB, temporarily attached to base VEB */
8891 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
8892 if (pf->vsi[v] &&
8893 pf->vsi[v]->veb_idx == veb->idx &&
8894 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
8895 ctl_vsi = pf->vsi[v];
8896 break;
8897 }
8898 }
8899 if (!ctl_vsi) {
8900 dev_info(&pf->pdev->dev,
8901 "missing owner VSI for veb_idx %d\n", veb->idx);
8902 ret = -ENOENT;
8903 goto end_reconstitute;
8904 }
8905 if (ctl_vsi != pf->vsi[pf->lan_vsi])
8906 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
8907 ret = i40e_add_vsi(ctl_vsi);
8908 if (ret) {
8909 dev_info(&pf->pdev->dev,
8910 "rebuild of veb_idx %d owner VSI failed: %d\n",
8911 veb->idx, ret);
8912 goto end_reconstitute;
8913 }
8914 i40e_vsi_reset_stats(ctl_vsi);
8915
8916 /* create the VEB in the switch and move the VSI onto the VEB */
8917 ret = i40e_add_veb(veb, ctl_vsi);
8918 if (ret)
8919 goto end_reconstitute;
8920
8921 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
8922 veb->bridge_mode = BRIDGE_MODE_VEB;
8923 else
8924 veb->bridge_mode = BRIDGE_MODE_VEPA;
8925 i40e_config_bridge_mode(veb);
8926
8927 /* create the remaining VSIs attached to this VEB */
8928 for (v = 0; v < pf->num_alloc_vsi; v++) {
8929 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
8930 continue;
8931
8932 if (pf->vsi[v]->veb_idx == veb->idx) {
8933 struct i40e_vsi *vsi = pf->vsi[v];
8934
8935 vsi->uplink_seid = veb->seid;
8936 ret = i40e_add_vsi(vsi);
8937 if (ret) {
8938 dev_info(&pf->pdev->dev,
8939 "rebuild of vsi_idx %d failed: %d\n",
8940 v, ret);
8941 goto end_reconstitute;
8942 }
8943 i40e_vsi_reset_stats(vsi);
8944 }
8945 }
8946
8947 /* create any VEBs attached to this VEB - RECURSION */
8948 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
8949 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
8950 pf->veb[veb_idx]->uplink_seid = veb->seid;
8951 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
8952 if (ret)
8953 break;
8954 }
8955 }
8956
8957 end_reconstitute:
8958 return ret;
8959 }
8960
8961 /**
8962 * i40e_get_capabilities - get info about the HW
8963 * @pf: the PF struct
8964 **/
8965 static int i40e_get_capabilities(struct i40e_pf *pf,
8966 enum i40e_admin_queue_opc list_type)
8967 {
8968 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
8969 u16 data_size;
8970 int buf_len;
8971 int err;
8972
8973 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
8974 do {
8975 cap_buf = kzalloc(buf_len, GFP_KERNEL);
8976 if (!cap_buf)
8977 return -ENOMEM;
8978
8979 /* this loads the data into the hw struct for us */
8980 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
8981 &data_size, list_type,
8982 NULL);
8983 /* data loaded, buffer no longer needed */
8984 kfree(cap_buf);
8985
8986 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
8987 /* retry with a larger buffer */
8988 buf_len = data_size;
8989 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
8990 dev_info(&pf->pdev->dev,
8991 "capability discovery failed, err %s aq_err %s\n",
8992 i40e_stat_str(&pf->hw, err),
8993 i40e_aq_str(&pf->hw,
8994 pf->hw.aq.asq_last_status));
8995 return -ENODEV;
8996 }
8997 } while (err);
8998
8999 if (pf->hw.debug_mask & I40E_DEBUG_USER) {
9000 if (list_type == i40e_aqc_opc_list_func_capabilities) {
9001 dev_info(&pf->pdev->dev,
9002 "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",
9003 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
9004 pf->hw.func_caps.num_msix_vectors,
9005 pf->hw.func_caps.num_msix_vectors_vf,
9006 pf->hw.func_caps.fd_filters_guaranteed,
9007 pf->hw.func_caps.fd_filters_best_effort,
9008 pf->hw.func_caps.num_tx_qp,
9009 pf->hw.func_caps.num_vsis);
9010 } else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
9011 dev_info(&pf->pdev->dev,
9012 "switch_mode=0x%04x, function_valid=0x%08x\n",
9013 pf->hw.dev_caps.switch_mode,
9014 pf->hw.dev_caps.valid_functions);
9015 dev_info(&pf->pdev->dev,
9016 "SR-IOV=%d, num_vfs for all function=%u\n",
9017 pf->hw.dev_caps.sr_iov_1_1,
9018 pf->hw.dev_caps.num_vfs);
9019 dev_info(&pf->pdev->dev,
9020 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
9021 pf->hw.dev_caps.num_vsis,
9022 pf->hw.dev_caps.num_rx_qp,
9023 pf->hw.dev_caps.num_tx_qp);
9024 }
9025 }
9026 if (list_type == i40e_aqc_opc_list_func_capabilities) {
9027 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
9028 + pf->hw.func_caps.num_vfs)
9029 if (pf->hw.revision_id == 0 &&
9030 pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
9031 dev_info(&pf->pdev->dev,
9032 "got num_vsis %d, setting num_vsis to %d\n",
9033 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
9034 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
9035 }
9036 }
9037 return 0;
9038 }
9039
9040 static int i40e_vsi_clear(struct i40e_vsi *vsi);
9041
9042 /**
9043 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
9044 * @pf: board private structure
9045 **/
9046 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
9047 {
9048 struct i40e_vsi *vsi;
9049
9050 /* quick workaround for an NVM issue that leaves a critical register
9051 * uninitialized
9052 */
9053 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
9054 static const u32 hkey[] = {
9055 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
9056 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
9057 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
9058 0x95b3a76d};
9059 int i;
9060
9061 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
9062 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
9063 }
9064
9065 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
9066 return;
9067
9068 /* find existing VSI and see if it needs configuring */
9069 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
9070
9071 /* create a new VSI if none exists */
9072 if (!vsi) {
9073 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
9074 pf->vsi[pf->lan_vsi]->seid, 0);
9075 if (!vsi) {
9076 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
9077 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
9078 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
9079 return;
9080 }
9081 }
9082
9083 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
9084 }
9085
9086 /**
9087 * i40e_fdir_teardown - release the Flow Director resources
9088 * @pf: board private structure
9089 **/
9090 static void i40e_fdir_teardown(struct i40e_pf *pf)
9091 {
9092 struct i40e_vsi *vsi;
9093
9094 i40e_fdir_filter_exit(pf);
9095 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
9096 if (vsi)
9097 i40e_vsi_release(vsi);
9098 }
9099
9100 /**
9101 * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
9102 * @vsi: PF main vsi
9103 * @seid: seid of main or channel VSIs
9104 *
9105 * Rebuilds cloud filters associated with main VSI and channel VSIs if they
9106 * existed before reset
9107 **/
9108 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
9109 {
9110 struct i40e_cloud_filter *cfilter;
9111 struct i40e_pf *pf = vsi->back;
9112 struct hlist_node *node;
9113 i40e_status ret;
9114
9115 /* Add cloud filters back if they exist */
9116 hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
9117 cloud_node) {
9118 if (cfilter->seid != seid)
9119 continue;
9120
9121 if (cfilter->dst_port)
9122 ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
9123 true);
9124 else
9125 ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
9126
9127 if (ret) {
9128 dev_dbg(&pf->pdev->dev,
9129 "Failed to rebuild cloud filter, err %s aq_err %s\n",
9130 i40e_stat_str(&pf->hw, ret),
9131 i40e_aq_str(&pf->hw,
9132 pf->hw.aq.asq_last_status));
9133 return ret;
9134 }
9135 }
9136 return 0;
9137 }
9138
9139 /**
9140 * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
9141 * @vsi: PF main vsi
9142 *
9143 * Rebuilds channel VSIs if they existed before reset
9144 **/
9145 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
9146 {
9147 struct i40e_channel *ch, *ch_tmp;
9148 i40e_status ret;
9149
9150 if (list_empty(&vsi->ch_list))
9151 return 0;
9152
9153 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
9154 if (!ch->initialized)
9155 break;
9156 /* Proceed with creation of channel (VMDq2) VSI */
9157 ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
9158 if (ret) {
9159 dev_info(&vsi->back->pdev->dev,
9160 "failed to rebuild channels using uplink_seid %u\n",
9161 vsi->uplink_seid);
9162 return ret;
9163 }
9164 /* Reconfigure TX queues using QTX_CTL register */
9165 ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
9166 if (ret) {
9167 dev_info(&vsi->back->pdev->dev,
9168 "failed to configure TX rings for channel %u\n",
9169 ch->seid);
9170 return ret;
9171 }
9172 /* update 'next_base_queue' */
9173 vsi->next_base_queue = vsi->next_base_queue +
9174 ch->num_queue_pairs;
9175 if (ch->max_tx_rate) {
9176 u64 credits = ch->max_tx_rate;
9177
9178 if (i40e_set_bw_limit(vsi, ch->seid,
9179 ch->max_tx_rate))
9180 return -EINVAL;
9181
9182 do_div(credits, I40E_BW_CREDIT_DIVISOR);
9183 dev_dbg(&vsi->back->pdev->dev,
9184 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
9185 ch->max_tx_rate,
9186 credits,
9187 ch->seid);
9188 }
9189 ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
9190 if (ret) {
9191 dev_dbg(&vsi->back->pdev->dev,
9192 "Failed to rebuild cloud filters for channel VSI %u\n",
9193 ch->seid);
9194 return ret;
9195 }
9196 }
9197 return 0;
9198 }
9199
9200 /**
9201 * i40e_prep_for_reset - prep for the core to reset
9202 * @pf: board private structure
9203 * @lock_acquired: indicates whether or not the lock has been acquired
9204 * before this function was called.
9205 *
9206 * Close up the VFs and other things in prep for PF Reset.
9207 **/
9208 static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
9209 {
9210 struct i40e_hw *hw = &pf->hw;
9211 i40e_status ret = 0;
9212 u32 v;
9213
9214 clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
9215 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
9216 return;
9217 if (i40e_check_asq_alive(&pf->hw))
9218 i40e_vc_notify_reset(pf);
9219
9220 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
9221
9222 /* quiesce the VSIs and their queues that are not already DOWN */
9223 /* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
9224 if (!lock_acquired)
9225 rtnl_lock();
9226 i40e_pf_quiesce_all_vsi(pf);
9227 if (!lock_acquired)
9228 rtnl_unlock();
9229
9230 for (v = 0; v < pf->num_alloc_vsi; v++) {
9231 if (pf->vsi[v])
9232 pf->vsi[v]->seid = 0;
9233 }
9234
9235 i40e_shutdown_adminq(&pf->hw);
9236
9237 /* call shutdown HMC */
9238 if (hw->hmc.hmc_obj) {
9239 ret = i40e_shutdown_lan_hmc(hw);
9240 if (ret)
9241 dev_warn(&pf->pdev->dev,
9242 "shutdown_lan_hmc failed: %d\n", ret);
9243 }
9244 }
9245
9246 /**
9247 * i40e_send_version - update firmware with driver version
9248 * @pf: PF struct
9249 */
9250 static void i40e_send_version(struct i40e_pf *pf)
9251 {
9252 struct i40e_driver_version dv;
9253
9254 dv.major_version = DRV_VERSION_MAJOR;
9255 dv.minor_version = DRV_VERSION_MINOR;
9256 dv.build_version = DRV_VERSION_BUILD;
9257 dv.subbuild_version = 0;
9258 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
9259 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
9260 }
9261
9262 /**
9263 * i40e_get_oem_version - get OEM specific version information
9264 * @hw: pointer to the hardware structure
9265 **/
9266 static void i40e_get_oem_version(struct i40e_hw *hw)
9267 {
9268 u16 block_offset = 0xffff;
9269 u16 block_length = 0;
9270 u16 capabilities = 0;
9271 u16 gen_snap = 0;
9272 u16 release = 0;
9273
9274 #define I40E_SR_NVM_OEM_VERSION_PTR 0x1B
9275 #define I40E_NVM_OEM_LENGTH_OFFSET 0x00
9276 #define I40E_NVM_OEM_CAPABILITIES_OFFSET 0x01
9277 #define I40E_NVM_OEM_GEN_OFFSET 0x02
9278 #define I40E_NVM_OEM_RELEASE_OFFSET 0x03
9279 #define I40E_NVM_OEM_CAPABILITIES_MASK 0x000F
9280 #define I40E_NVM_OEM_LENGTH 3
9281
9282 /* Check if pointer to OEM version block is valid. */
9283 i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
9284 if (block_offset == 0xffff)
9285 return;
9286
9287 /* Check if OEM version block has correct length. */
9288 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
9289 &block_length);
9290 if (block_length < I40E_NVM_OEM_LENGTH)
9291 return;
9292
9293 /* Check if OEM version format is as expected. */
9294 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
9295 &capabilities);
9296 if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
9297 return;
9298
9299 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
9300 &gen_snap);
9301 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
9302 &release);
9303 hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
9304 hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
9305 }
9306
9307 /**
9308 * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
9309 * @pf: board private structure
9310 **/
9311 static int i40e_reset(struct i40e_pf *pf)
9312 {
9313 struct i40e_hw *hw = &pf->hw;
9314 i40e_status ret;
9315
9316 ret = i40e_pf_reset(hw);
9317 if (ret) {
9318 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
9319 set_bit(__I40E_RESET_FAILED, pf->state);
9320 clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
9321 } else {
9322 pf->pfr_count++;
9323 }
9324 return ret;
9325 }
9326
9327 /**
9328 * i40e_rebuild - rebuild using a saved config
9329 * @pf: board private structure
9330 * @reinit: if the Main VSI needs to re-initialized.
9331 * @lock_acquired: indicates whether or not the lock has been acquired
9332 * before this function was called.
9333 **/
9334 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
9335 {
9336 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9337 struct i40e_hw *hw = &pf->hw;
9338 u8 set_fc_aq_fail = 0;
9339 i40e_status ret;
9340 u32 val;
9341 int v;
9342
9343 if (test_bit(__I40E_DOWN, pf->state))
9344 goto clear_recovery;
9345 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
9346
9347 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
9348 ret = i40e_init_adminq(&pf->hw);
9349 if (ret) {
9350 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
9351 i40e_stat_str(&pf->hw, ret),
9352 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9353 goto clear_recovery;
9354 }
9355 i40e_get_oem_version(&pf->hw);
9356
9357 if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
9358 ((hw->aq.fw_maj_ver == 4 && hw->aq.fw_min_ver <= 33) ||
9359 hw->aq.fw_maj_ver < 4) && hw->mac.type == I40E_MAC_XL710) {
9360 /* The following delay is necessary for 4.33 firmware and older
9361 * to recover after EMP reset. 200 ms should suffice but we
9362 * put here 300 ms to be sure that FW is ready to operate
9363 * after reset.
9364 */
9365 mdelay(300);
9366 }
9367
9368 /* re-verify the eeprom if we just had an EMP reset */
9369 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
9370 i40e_verify_eeprom(pf);
9371
9372 i40e_clear_pxe_mode(hw);
9373 ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
9374 if (ret)
9375 goto end_core_reset;
9376
9377 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
9378 hw->func_caps.num_rx_qp, 0, 0);
9379 if (ret) {
9380 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
9381 goto end_core_reset;
9382 }
9383 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
9384 if (ret) {
9385 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
9386 goto end_core_reset;
9387 }
9388
9389 /* Enable FW to write a default DCB config on link-up */
9390 i40e_aq_set_dcb_parameters(hw, true, NULL);
9391
9392 #ifdef CONFIG_I40E_DCB
9393 ret = i40e_init_pf_dcb(pf);
9394 if (ret) {
9395 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
9396 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9397 /* Continue without DCB enabled */
9398 }
9399 #endif /* CONFIG_I40E_DCB */
9400 /* do basic switch setup */
9401 if (!lock_acquired)
9402 rtnl_lock();
9403 ret = i40e_setup_pf_switch(pf, reinit);
9404 if (ret)
9405 goto end_unlock;
9406
9407 /* The driver only wants link up/down and module qualification
9408 * reports from firmware. Note the negative logic.
9409 */
9410 ret = i40e_aq_set_phy_int_mask(&pf->hw,
9411 ~(I40E_AQ_EVENT_LINK_UPDOWN |
9412 I40E_AQ_EVENT_MEDIA_NA |
9413 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
9414 if (ret)
9415 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
9416 i40e_stat_str(&pf->hw, ret),
9417 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9418
9419 /* make sure our flow control settings are restored */
9420 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
9421 if (ret)
9422 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
9423 i40e_stat_str(&pf->hw, ret),
9424 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9425
9426 /* Rebuild the VSIs and VEBs that existed before reset.
9427 * They are still in our local switch element arrays, so only
9428 * need to rebuild the switch model in the HW.
9429 *
9430 * If there were VEBs but the reconstitution failed, we'll try
9431 * try to recover minimal use by getting the basic PF VSI working.
9432 */
9433 if (vsi->uplink_seid != pf->mac_seid) {
9434 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
9435 /* find the one VEB connected to the MAC, and find orphans */
9436 for (v = 0; v < I40E_MAX_VEB; v++) {
9437 if (!pf->veb[v])
9438 continue;
9439
9440 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
9441 pf->veb[v]->uplink_seid == 0) {
9442 ret = i40e_reconstitute_veb(pf->veb[v]);
9443
9444 if (!ret)
9445 continue;
9446
9447 /* If Main VEB failed, we're in deep doodoo,
9448 * so give up rebuilding the switch and set up
9449 * for minimal rebuild of PF VSI.
9450 * If orphan failed, we'll report the error
9451 * but try to keep going.
9452 */
9453 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
9454 dev_info(&pf->pdev->dev,
9455 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
9456 ret);
9457 vsi->uplink_seid = pf->mac_seid;
9458 break;
9459 } else if (pf->veb[v]->uplink_seid == 0) {
9460 dev_info(&pf->pdev->dev,
9461 "rebuild of orphan VEB failed: %d\n",
9462 ret);
9463 }
9464 }
9465 }
9466 }
9467
9468 if (vsi->uplink_seid == pf->mac_seid) {
9469 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
9470 /* no VEB, so rebuild only the Main VSI */
9471 ret = i40e_add_vsi(vsi);
9472 if (ret) {
9473 dev_info(&pf->pdev->dev,
9474 "rebuild of Main VSI failed: %d\n", ret);
9475 goto end_unlock;
9476 }
9477 }
9478
9479 if (vsi->mqprio_qopt.max_rate[0]) {
9480 u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
9481 u64 credits = 0;
9482
9483 do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
9484 ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
9485 if (ret)
9486 goto end_unlock;
9487
9488 credits = max_tx_rate;
9489 do_div(credits, I40E_BW_CREDIT_DIVISOR);
9490 dev_dbg(&vsi->back->pdev->dev,
9491 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
9492 max_tx_rate,
9493 credits,
9494 vsi->seid);
9495 }
9496
9497 ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
9498 if (ret)
9499 goto end_unlock;
9500
9501 /* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
9502 * for this main VSI if they exist
9503 */
9504 ret = i40e_rebuild_channels(vsi);
9505 if (ret)
9506 goto end_unlock;
9507
9508 /* Reconfigure hardware for allowing smaller MSS in the case
9509 * of TSO, so that we avoid the MDD being fired and causing
9510 * a reset in the case of small MSS+TSO.
9511 */
9512 #define I40E_REG_MSS 0x000E64DC
9513 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
9514 #define I40E_64BYTE_MSS 0x400000
9515 val = rd32(hw, I40E_REG_MSS);
9516 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
9517 val &= ~I40E_REG_MSS_MIN_MASK;
9518 val |= I40E_64BYTE_MSS;
9519 wr32(hw, I40E_REG_MSS, val);
9520 }
9521
9522 if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
9523 msleep(75);
9524 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
9525 if (ret)
9526 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
9527 i40e_stat_str(&pf->hw, ret),
9528 i40e_aq_str(&pf->hw,
9529 pf->hw.aq.asq_last_status));
9530 }
9531 /* reinit the misc interrupt */
9532 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
9533 ret = i40e_setup_misc_vector(pf);
9534
9535 /* Add a filter to drop all Flow control frames from any VSI from being
9536 * transmitted. By doing so we stop a malicious VF from sending out
9537 * PAUSE or PFC frames and potentially controlling traffic for other
9538 * PF/VF VSIs.
9539 * The FW can still send Flow control frames if enabled.
9540 */
9541 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
9542 pf->main_vsi_seid);
9543
9544 /* restart the VSIs that were rebuilt and running before the reset */
9545 i40e_pf_unquiesce_all_vsi(pf);
9546
9547 /* Release the RTNL lock before we start resetting VFs */
9548 if (!lock_acquired)
9549 rtnl_unlock();
9550
9551 /* Restore promiscuous settings */
9552 ret = i40e_set_promiscuous(pf, pf->cur_promisc);
9553 if (ret)
9554 dev_warn(&pf->pdev->dev,
9555 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
9556 pf->cur_promisc ? "on" : "off",
9557 i40e_stat_str(&pf->hw, ret),
9558 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9559
9560 i40e_reset_all_vfs(pf, true);
9561
9562 /* tell the firmware that we're starting */
9563 i40e_send_version(pf);
9564
9565 /* We've already released the lock, so don't do it again */
9566 goto end_core_reset;
9567
9568 end_unlock:
9569 if (!lock_acquired)
9570 rtnl_unlock();
9571 end_core_reset:
9572 clear_bit(__I40E_RESET_FAILED, pf->state);
9573 clear_recovery:
9574 clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
9575 clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
9576 }
9577
9578 /**
9579 * i40e_reset_and_rebuild - reset and rebuild using a saved config
9580 * @pf: board private structure
9581 * @reinit: if the Main VSI needs to re-initialized.
9582 * @lock_acquired: indicates whether or not the lock has been acquired
9583 * before this function was called.
9584 **/
9585 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
9586 bool lock_acquired)
9587 {
9588 int ret;
9589 /* Now we wait for GRST to settle out.
9590 * We don't have to delete the VEBs or VSIs from the hw switch
9591 * because the reset will make them disappear.
9592 */
9593 ret = i40e_reset(pf);
9594 if (!ret)
9595 i40e_rebuild(pf, reinit, lock_acquired);
9596 }
9597
9598 /**
9599 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
9600 * @pf: board private structure
9601 *
9602 * Close up the VFs and other things in prep for a Core Reset,
9603 * then get ready to rebuild the world.
9604 * @lock_acquired: indicates whether or not the lock has been acquired
9605 * before this function was called.
9606 **/
9607 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
9608 {
9609 i40e_prep_for_reset(pf, lock_acquired);
9610 i40e_reset_and_rebuild(pf, false, lock_acquired);
9611 }
9612
9613 /**
9614 * i40e_handle_mdd_event
9615 * @pf: pointer to the PF structure
9616 *
9617 * Called from the MDD irq handler to identify possibly malicious vfs
9618 **/
9619 static void i40e_handle_mdd_event(struct i40e_pf *pf)
9620 {
9621 struct i40e_hw *hw = &pf->hw;
9622 bool mdd_detected = false;
9623 bool pf_mdd_detected = false;
9624 struct i40e_vf *vf;
9625 u32 reg;
9626 int i;
9627
9628 if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
9629 return;
9630
9631 /* find what triggered the MDD event */
9632 reg = rd32(hw, I40E_GL_MDET_TX);
9633 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
9634 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
9635 I40E_GL_MDET_TX_PF_NUM_SHIFT;
9636 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
9637 I40E_GL_MDET_TX_VF_NUM_SHIFT;
9638 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
9639 I40E_GL_MDET_TX_EVENT_SHIFT;
9640 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
9641 I40E_GL_MDET_TX_QUEUE_SHIFT) -
9642 pf->hw.func_caps.base_queue;
9643 if (netif_msg_tx_err(pf))
9644 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
9645 event, queue, pf_num, vf_num);
9646 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
9647 mdd_detected = true;
9648 }
9649 reg = rd32(hw, I40E_GL_MDET_RX);
9650 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
9651 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
9652 I40E_GL_MDET_RX_FUNCTION_SHIFT;
9653 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
9654 I40E_GL_MDET_RX_EVENT_SHIFT;
9655 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
9656 I40E_GL_MDET_RX_QUEUE_SHIFT) -
9657 pf->hw.func_caps.base_queue;
9658 if (netif_msg_rx_err(pf))
9659 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
9660 event, queue, func);
9661 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
9662 mdd_detected = true;
9663 }
9664
9665 if (mdd_detected) {
9666 reg = rd32(hw, I40E_PF_MDET_TX);
9667 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
9668 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
9669 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
9670 pf_mdd_detected = true;
9671 }
9672 reg = rd32(hw, I40E_PF_MDET_RX);
9673 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
9674 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
9675 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
9676 pf_mdd_detected = true;
9677 }
9678 /* Queue belongs to the PF, initiate a reset */
9679 if (pf_mdd_detected) {
9680 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9681 i40e_service_event_schedule(pf);
9682 }
9683 }
9684
9685 /* see if one of the VFs needs its hand slapped */
9686 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
9687 vf = &(pf->vf[i]);
9688 reg = rd32(hw, I40E_VP_MDET_TX(i));
9689 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
9690 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
9691 vf->num_mdd_events++;
9692 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
9693 i);
9694 }
9695
9696 reg = rd32(hw, I40E_VP_MDET_RX(i));
9697 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
9698 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
9699 vf->num_mdd_events++;
9700 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
9701 i);
9702 }
9703
9704 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
9705 dev_info(&pf->pdev->dev,
9706 "Too many MDD events on VF %d, disabled\n", i);
9707 dev_info(&pf->pdev->dev,
9708 "Use PF Control I/F to re-enable the VF\n");
9709 set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
9710 }
9711 }
9712
9713 /* re-enable mdd interrupt cause */
9714 clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
9715 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
9716 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
9717 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
9718 i40e_flush(hw);
9719 }
9720
9721 static const char *i40e_tunnel_name(u8 type)
9722 {
9723 switch (type) {
9724 case UDP_TUNNEL_TYPE_VXLAN:
9725 return "vxlan";
9726 case UDP_TUNNEL_TYPE_GENEVE:
9727 return "geneve";
9728 default:
9729 return "unknown";
9730 }
9731 }
9732
9733 /**
9734 * i40e_sync_udp_filters - Trigger a sync event for existing UDP filters
9735 * @pf: board private structure
9736 **/
9737 static void i40e_sync_udp_filters(struct i40e_pf *pf)
9738 {
9739 int i;
9740
9741 /* loop through and set pending bit for all active UDP filters */
9742 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
9743 if (pf->udp_ports[i].port)
9744 pf->pending_udp_bitmap |= BIT_ULL(i);
9745 }
9746
9747 set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
9748 }
9749
9750 /**
9751 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
9752 * @pf: board private structure
9753 **/
9754 static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
9755 {
9756 struct i40e_hw *hw = &pf->hw;
9757 u8 filter_index, type;
9758 u16 port;
9759 int i;
9760
9761 if (!test_and_clear_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state))
9762 return;
9763
9764 /* acquire RTNL to maintain state of flags and port requests */
9765 rtnl_lock();
9766
9767 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
9768 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
9769 struct i40e_udp_port_config *udp_port;
9770 i40e_status ret = 0;
9771
9772 udp_port = &pf->udp_ports[i];
9773 pf->pending_udp_bitmap &= ~BIT_ULL(i);
9774
9775 port = READ_ONCE(udp_port->port);
9776 type = READ_ONCE(udp_port->type);
9777 filter_index = READ_ONCE(udp_port->filter_index);
9778
9779 /* release RTNL while we wait on AQ command */
9780 rtnl_unlock();
9781
9782 if (port)
9783 ret = i40e_aq_add_udp_tunnel(hw, port,
9784 type,
9785 &filter_index,
9786 NULL);
9787 else if (filter_index != I40E_UDP_PORT_INDEX_UNUSED)
9788 ret = i40e_aq_del_udp_tunnel(hw, filter_index,
9789 NULL);
9790
9791 /* reacquire RTNL so we can update filter_index */
9792 rtnl_lock();
9793
9794 if (ret) {
9795 dev_info(&pf->pdev->dev,
9796 "%s %s port %d, index %d failed, err %s aq_err %s\n",
9797 i40e_tunnel_name(type),
9798 port ? "add" : "delete",
9799 port,
9800 filter_index,
9801 i40e_stat_str(&pf->hw, ret),
9802 i40e_aq_str(&pf->hw,
9803 pf->hw.aq.asq_last_status));
9804 if (port) {
9805 /* failed to add, just reset port,
9806 * drop pending bit for any deletion
9807 */
9808 udp_port->port = 0;
9809 pf->pending_udp_bitmap &= ~BIT_ULL(i);
9810 }
9811 } else if (port) {
9812 /* record filter index on success */
9813 udp_port->filter_index = filter_index;
9814 }
9815 }
9816 }
9817
9818 rtnl_unlock();
9819 }
9820
9821 /**
9822 * i40e_service_task - Run the driver's async subtasks
9823 * @work: pointer to work_struct containing our data
9824 **/
9825 static void i40e_service_task(struct work_struct *work)
9826 {
9827 struct i40e_pf *pf = container_of(work,
9828 struct i40e_pf,
9829 service_task);
9830 unsigned long start_time = jiffies;
9831
9832 /* don't bother with service tasks if a reset is in progress */
9833 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
9834 return;
9835
9836 if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
9837 return;
9838
9839 i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
9840 i40e_sync_filters_subtask(pf);
9841 i40e_reset_subtask(pf);
9842 i40e_handle_mdd_event(pf);
9843 i40e_vc_process_vflr_event(pf);
9844 i40e_watchdog_subtask(pf);
9845 i40e_fdir_reinit_subtask(pf);
9846 if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
9847 /* Client subtask will reopen next time through. */
9848 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
9849 } else {
9850 i40e_client_subtask(pf);
9851 if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
9852 pf->state))
9853 i40e_notify_client_of_l2_param_changes(
9854 pf->vsi[pf->lan_vsi]);
9855 }
9856 i40e_sync_filters_subtask(pf);
9857 i40e_sync_udp_filters_subtask(pf);
9858 i40e_clean_adminq_subtask(pf);
9859
9860 /* flush memory to make sure state is correct before next watchdog */
9861 smp_mb__before_atomic();
9862 clear_bit(__I40E_SERVICE_SCHED, pf->state);
9863
9864 /* If the tasks have taken longer than one timer cycle or there
9865 * is more work to be done, reschedule the service task now
9866 * rather than wait for the timer to tick again.
9867 */
9868 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
9869 test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state) ||
9870 test_bit(__I40E_MDD_EVENT_PENDING, pf->state) ||
9871 test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
9872 i40e_service_event_schedule(pf);
9873 }
9874
9875 /**
9876 * i40e_service_timer - timer callback
9877 * @data: pointer to PF struct
9878 **/
9879 static void i40e_service_timer(struct timer_list *t)
9880 {
9881 struct i40e_pf *pf = from_timer(pf, t, service_timer);
9882
9883 mod_timer(&pf->service_timer,
9884 round_jiffies(jiffies + pf->service_timer_period));
9885 i40e_service_event_schedule(pf);
9886 }
9887
9888 /**
9889 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
9890 * @vsi: the VSI being configured
9891 **/
9892 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
9893 {
9894 struct i40e_pf *pf = vsi->back;
9895
9896 switch (vsi->type) {
9897 case I40E_VSI_MAIN:
9898 vsi->alloc_queue_pairs = pf->num_lan_qps;
9899 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
9900 I40E_REQ_DESCRIPTOR_MULTIPLE);
9901 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
9902 vsi->num_q_vectors = pf->num_lan_msix;
9903 else
9904 vsi->num_q_vectors = 1;
9905
9906 break;
9907
9908 case I40E_VSI_FDIR:
9909 vsi->alloc_queue_pairs = 1;
9910 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
9911 I40E_REQ_DESCRIPTOR_MULTIPLE);
9912 vsi->num_q_vectors = pf->num_fdsb_msix;
9913 break;
9914
9915 case I40E_VSI_VMDQ2:
9916 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
9917 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
9918 I40E_REQ_DESCRIPTOR_MULTIPLE);
9919 vsi->num_q_vectors = pf->num_vmdq_msix;
9920 break;
9921
9922 case I40E_VSI_SRIOV:
9923 vsi->alloc_queue_pairs = pf->num_vf_qps;
9924 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
9925 I40E_REQ_DESCRIPTOR_MULTIPLE);
9926 break;
9927
9928 default:
9929 WARN_ON(1);
9930 return -ENODATA;
9931 }
9932
9933 return 0;
9934 }
9935
9936 /**
9937 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
9938 * @vsi: VSI pointer
9939 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
9940 *
9941 * On error: returns error code (negative)
9942 * On success: returns 0
9943 **/
9944 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
9945 {
9946 struct i40e_ring **next_rings;
9947 int size;
9948 int ret = 0;
9949
9950 /* allocate memory for both Tx, XDP Tx and Rx ring pointers */
9951 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
9952 (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
9953 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
9954 if (!vsi->tx_rings)
9955 return -ENOMEM;
9956 next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
9957 if (i40e_enabled_xdp_vsi(vsi)) {
9958 vsi->xdp_rings = next_rings;
9959 next_rings += vsi->alloc_queue_pairs;
9960 }
9961 vsi->rx_rings = next_rings;
9962
9963 if (alloc_qvectors) {
9964 /* allocate memory for q_vector pointers */
9965 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
9966 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
9967 if (!vsi->q_vectors) {
9968 ret = -ENOMEM;
9969 goto err_vectors;
9970 }
9971 }
9972 return ret;
9973
9974 err_vectors:
9975 kfree(vsi->tx_rings);
9976 return ret;
9977 }
9978
9979 /**
9980 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
9981 * @pf: board private structure
9982 * @type: type of VSI
9983 *
9984 * On error: returns error code (negative)
9985 * On success: returns vsi index in PF (positive)
9986 **/
9987 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
9988 {
9989 int ret = -ENODEV;
9990 struct i40e_vsi *vsi;
9991 int vsi_idx;
9992 int i;
9993
9994 /* Need to protect the allocation of the VSIs at the PF level */
9995 mutex_lock(&pf->switch_mutex);
9996
9997 /* VSI list may be fragmented if VSI creation/destruction has
9998 * been happening. We can afford to do a quick scan to look
9999 * for any free VSIs in the list.
10000 *
10001 * find next empty vsi slot, looping back around if necessary
10002 */
10003 i = pf->next_vsi;
10004 while (i < pf->num_alloc_vsi && pf->vsi[i])
10005 i++;
10006 if (i >= pf->num_alloc_vsi) {
10007 i = 0;
10008 while (i < pf->next_vsi && pf->vsi[i])
10009 i++;
10010 }
10011
10012 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
10013 vsi_idx = i; /* Found one! */
10014 } else {
10015 ret = -ENODEV;
10016 goto unlock_pf; /* out of VSI slots! */
10017 }
10018 pf->next_vsi = ++i;
10019
10020 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
10021 if (!vsi) {
10022 ret = -ENOMEM;
10023 goto unlock_pf;
10024 }
10025 vsi->type = type;
10026 vsi->back = pf;
10027 set_bit(__I40E_VSI_DOWN, vsi->state);
10028 vsi->flags = 0;
10029 vsi->idx = vsi_idx;
10030 vsi->int_rate_limit = 0;
10031 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
10032 pf->rss_table_size : 64;
10033 vsi->netdev_registered = false;
10034 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
10035 hash_init(vsi->mac_filter_hash);
10036 vsi->irqs_ready = false;
10037
10038 ret = i40e_set_num_rings_in_vsi(vsi);
10039 if (ret)
10040 goto err_rings;
10041
10042 ret = i40e_vsi_alloc_arrays(vsi, true);
10043 if (ret)
10044 goto err_rings;
10045
10046 /* Setup default MSIX irq handler for VSI */
10047 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
10048
10049 /* Initialize VSI lock */
10050 spin_lock_init(&vsi->mac_filter_hash_lock);
10051 pf->vsi[vsi_idx] = vsi;
10052 ret = vsi_idx;
10053 goto unlock_pf;
10054
10055 err_rings:
10056 pf->next_vsi = i - 1;
10057 kfree(vsi);
10058 unlock_pf:
10059 mutex_unlock(&pf->switch_mutex);
10060 return ret;
10061 }
10062
10063 /**
10064 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
10065 * @vsi: VSI pointer
10066 * @free_qvectors: a bool to specify if q_vectors need to be freed.
10067 *
10068 * On error: returns error code (negative)
10069 * On success: returns 0
10070 **/
10071 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
10072 {
10073 /* free the ring and vector containers */
10074 if (free_qvectors) {
10075 kfree(vsi->q_vectors);
10076 vsi->q_vectors = NULL;
10077 }
10078 kfree(vsi->tx_rings);
10079 vsi->tx_rings = NULL;
10080 vsi->rx_rings = NULL;
10081 vsi->xdp_rings = NULL;
10082 }
10083
10084 /**
10085 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
10086 * and lookup table
10087 * @vsi: Pointer to VSI structure
10088 */
10089 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
10090 {
10091 if (!vsi)
10092 return;
10093
10094 kfree(vsi->rss_hkey_user);
10095 vsi->rss_hkey_user = NULL;
10096
10097 kfree(vsi->rss_lut_user);
10098 vsi->rss_lut_user = NULL;
10099 }
10100
10101 /**
10102 * i40e_vsi_clear - Deallocate the VSI provided
10103 * @vsi: the VSI being un-configured
10104 **/
10105 static int i40e_vsi_clear(struct i40e_vsi *vsi)
10106 {
10107 struct i40e_pf *pf;
10108
10109 if (!vsi)
10110 return 0;
10111
10112 if (!vsi->back)
10113 goto free_vsi;
10114 pf = vsi->back;
10115
10116 mutex_lock(&pf->switch_mutex);
10117 if (!pf->vsi[vsi->idx]) {
10118 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
10119 vsi->idx, vsi->idx, vsi->type);
10120 goto unlock_vsi;
10121 }
10122
10123 if (pf->vsi[vsi->idx] != vsi) {
10124 dev_err(&pf->pdev->dev,
10125 "pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
10126 pf->vsi[vsi->idx]->idx,
10127 pf->vsi[vsi->idx]->type,
10128 vsi->idx, vsi->type);
10129 goto unlock_vsi;
10130 }
10131
10132 /* updates the PF for this cleared vsi */
10133 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
10134 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
10135
10136 i40e_vsi_free_arrays(vsi, true);
10137 i40e_clear_rss_config_user(vsi);
10138
10139 pf->vsi[vsi->idx] = NULL;
10140 if (vsi->idx < pf->next_vsi)
10141 pf->next_vsi = vsi->idx;
10142
10143 unlock_vsi:
10144 mutex_unlock(&pf->switch_mutex);
10145 free_vsi:
10146 kfree(vsi);
10147
10148 return 0;
10149 }
10150
10151 /**
10152 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
10153 * @vsi: the VSI being cleaned
10154 **/
10155 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
10156 {
10157 int i;
10158
10159 if (vsi->tx_rings && vsi->tx_rings[0]) {
10160 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
10161 kfree_rcu(vsi->tx_rings[i], rcu);
10162 vsi->tx_rings[i] = NULL;
10163 vsi->rx_rings[i] = NULL;
10164 if (vsi->xdp_rings)
10165 vsi->xdp_rings[i] = NULL;
10166 }
10167 }
10168 }
10169
10170 /**
10171 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
10172 * @vsi: the VSI being configured
10173 **/
10174 static int i40e_alloc_rings(struct i40e_vsi *vsi)
10175 {
10176 int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
10177 struct i40e_pf *pf = vsi->back;
10178 struct i40e_ring *ring;
10179
10180 /* Set basic values in the rings to be used later during open() */
10181 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
10182 /* allocate space for both Tx and Rx in one shot */
10183 ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
10184 if (!ring)
10185 goto err_out;
10186
10187 ring->queue_index = i;
10188 ring->reg_idx = vsi->base_queue + i;
10189 ring->ring_active = false;
10190 ring->vsi = vsi;
10191 ring->netdev = vsi->netdev;
10192 ring->dev = &pf->pdev->dev;
10193 ring->count = vsi->num_desc;
10194 ring->size = 0;
10195 ring->dcb_tc = 0;
10196 if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
10197 ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
10198 ring->itr_setting = pf->tx_itr_default;
10199 vsi->tx_rings[i] = ring++;
10200
10201 if (!i40e_enabled_xdp_vsi(vsi))
10202 goto setup_rx;
10203
10204 ring->queue_index = vsi->alloc_queue_pairs + i;
10205 ring->reg_idx = vsi->base_queue + ring->queue_index;
10206 ring->ring_active = false;
10207 ring->vsi = vsi;
10208 ring->netdev = NULL;
10209 ring->dev = &pf->pdev->dev;
10210 ring->count = vsi->num_desc;
10211 ring->size = 0;
10212 ring->dcb_tc = 0;
10213 if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
10214 ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
10215 set_ring_xdp(ring);
10216 ring->itr_setting = pf->tx_itr_default;
10217 vsi->xdp_rings[i] = ring++;
10218
10219 setup_rx:
10220 ring->queue_index = i;
10221 ring->reg_idx = vsi->base_queue + i;
10222 ring->ring_active = false;
10223 ring->vsi = vsi;
10224 ring->netdev = vsi->netdev;
10225 ring->dev = &pf->pdev->dev;
10226 ring->count = vsi->num_desc;
10227 ring->size = 0;
10228 ring->dcb_tc = 0;
10229 ring->itr_setting = pf->rx_itr_default;
10230 vsi->rx_rings[i] = ring;
10231 }
10232
10233 return 0;
10234
10235 err_out:
10236 i40e_vsi_clear_rings(vsi);
10237 return -ENOMEM;
10238 }
10239
10240 /**
10241 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
10242 * @pf: board private structure
10243 * @vectors: the number of MSI-X vectors to request
10244 *
10245 * Returns the number of vectors reserved, or error
10246 **/
10247 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
10248 {
10249 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
10250 I40E_MIN_MSIX, vectors);
10251 if (vectors < 0) {
10252 dev_info(&pf->pdev->dev,
10253 "MSI-X vector reservation failed: %d\n", vectors);
10254 vectors = 0;
10255 }
10256
10257 return vectors;
10258 }
10259
10260 /**
10261 * i40e_init_msix - Setup the MSIX capability
10262 * @pf: board private structure
10263 *
10264 * Work with the OS to set up the MSIX vectors needed.
10265 *
10266 * Returns the number of vectors reserved or negative on failure
10267 **/
10268 static int i40e_init_msix(struct i40e_pf *pf)
10269 {
10270 struct i40e_hw *hw = &pf->hw;
10271 int cpus, extra_vectors;
10272 int vectors_left;
10273 int v_budget, i;
10274 int v_actual;
10275 int iwarp_requested = 0;
10276
10277 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
10278 return -ENODEV;
10279
10280 /* The number of vectors we'll request will be comprised of:
10281 * - Add 1 for "other" cause for Admin Queue events, etc.
10282 * - The number of LAN queue pairs
10283 * - Queues being used for RSS.
10284 * We don't need as many as max_rss_size vectors.
10285 * use rss_size instead in the calculation since that
10286 * is governed by number of cpus in the system.
10287 * - assumes symmetric Tx/Rx pairing
10288 * - The number of VMDq pairs
10289 * - The CPU count within the NUMA node if iWARP is enabled
10290 * Once we count this up, try the request.
10291 *
10292 * If we can't get what we want, we'll simplify to nearly nothing
10293 * and try again. If that still fails, we punt.
10294 */
10295 vectors_left = hw->func_caps.num_msix_vectors;
10296 v_budget = 0;
10297
10298 /* reserve one vector for miscellaneous handler */
10299 if (vectors_left) {
10300 v_budget++;
10301 vectors_left--;
10302 }
10303
10304 /* reserve some vectors for the main PF traffic queues. Initially we
10305 * only reserve at most 50% of the available vectors, in the case that
10306 * the number of online CPUs is large. This ensures that we can enable
10307 * extra features as well. Once we've enabled the other features, we
10308 * will use any remaining vectors to reach as close as we can to the
10309 * number of online CPUs.
10310 */
10311 cpus = num_online_cpus();
10312 pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
10313 vectors_left -= pf->num_lan_msix;
10314
10315 /* reserve one vector for sideband flow director */
10316 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10317 if (vectors_left) {
10318 pf->num_fdsb_msix = 1;
10319 v_budget++;
10320 vectors_left--;
10321 } else {
10322 pf->num_fdsb_msix = 0;
10323 }
10324 }
10325
10326 /* can we reserve enough for iWARP? */
10327 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
10328 iwarp_requested = pf->num_iwarp_msix;
10329
10330 if (!vectors_left)
10331 pf->num_iwarp_msix = 0;
10332 else if (vectors_left < pf->num_iwarp_msix)
10333 pf->num_iwarp_msix = 1;
10334 v_budget += pf->num_iwarp_msix;
10335 vectors_left -= pf->num_iwarp_msix;
10336 }
10337
10338 /* any vectors left over go for VMDq support */
10339 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
10340 if (!vectors_left) {
10341 pf->num_vmdq_msix = 0;
10342 pf->num_vmdq_qps = 0;
10343 } else {
10344 int vmdq_vecs_wanted =
10345 pf->num_vmdq_vsis * pf->num_vmdq_qps;
10346 int vmdq_vecs =
10347 min_t(int, vectors_left, vmdq_vecs_wanted);
10348
10349 /* if we're short on vectors for what's desired, we limit
10350 * the queues per vmdq. If this is still more than are
10351 * available, the user will need to change the number of
10352 * queues/vectors used by the PF later with the ethtool
10353 * channels command
10354 */
10355 if (vectors_left < vmdq_vecs_wanted) {
10356 pf->num_vmdq_qps = 1;
10357 vmdq_vecs_wanted = pf->num_vmdq_vsis;
10358 vmdq_vecs = min_t(int,
10359 vectors_left,
10360 vmdq_vecs_wanted);
10361 }
10362 pf->num_vmdq_msix = pf->num_vmdq_qps;
10363
10364 v_budget += vmdq_vecs;
10365 vectors_left -= vmdq_vecs;
10366 }
10367 }
10368
10369 /* On systems with a large number of SMP cores, we previously limited
10370 * the number of vectors for num_lan_msix to be at most 50% of the
10371 * available vectors, to allow for other features. Now, we add back
10372 * the remaining vectors. However, we ensure that the total
10373 * num_lan_msix will not exceed num_online_cpus(). To do this, we
10374 * calculate the number of vectors we can add without going over the
10375 * cap of CPUs. For systems with a small number of CPUs this will be
10376 * zero.
10377 */
10378 extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
10379 pf->num_lan_msix += extra_vectors;
10380 vectors_left -= extra_vectors;
10381
10382 WARN(vectors_left < 0,
10383 "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
10384
10385 v_budget += pf->num_lan_msix;
10386 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
10387 GFP_KERNEL);
10388 if (!pf->msix_entries)
10389 return -ENOMEM;
10390
10391 for (i = 0; i < v_budget; i++)
10392 pf->msix_entries[i].entry = i;
10393 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
10394
10395 if (v_actual < I40E_MIN_MSIX) {
10396 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
10397 kfree(pf->msix_entries);
10398 pf->msix_entries = NULL;
10399 pci_disable_msix(pf->pdev);
10400 return -ENODEV;
10401
10402 } else if (v_actual == I40E_MIN_MSIX) {
10403 /* Adjust for minimal MSIX use */
10404 pf->num_vmdq_vsis = 0;
10405 pf->num_vmdq_qps = 0;
10406 pf->num_lan_qps = 1;
10407 pf->num_lan_msix = 1;
10408
10409 } else if (v_actual != v_budget) {
10410 /* If we have limited resources, we will start with no vectors
10411 * for the special features and then allocate vectors to some
10412 * of these features based on the policy and at the end disable
10413 * the features that did not get any vectors.
10414 */
10415 int vec;
10416
10417 dev_info(&pf->pdev->dev,
10418 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
10419 v_actual, v_budget);
10420 /* reserve the misc vector */
10421 vec = v_actual - 1;
10422
10423 /* Scale vector usage down */
10424 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
10425 pf->num_vmdq_vsis = 1;
10426 pf->num_vmdq_qps = 1;
10427
10428 /* partition out the remaining vectors */
10429 switch (vec) {
10430 case 2:
10431 pf->num_lan_msix = 1;
10432 break;
10433 case 3:
10434 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
10435 pf->num_lan_msix = 1;
10436 pf->num_iwarp_msix = 1;
10437 } else {
10438 pf->num_lan_msix = 2;
10439 }
10440 break;
10441 default:
10442 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
10443 pf->num_iwarp_msix = min_t(int, (vec / 3),
10444 iwarp_requested);
10445 pf->num_vmdq_vsis = min_t(int, (vec / 3),
10446 I40E_DEFAULT_NUM_VMDQ_VSI);
10447 } else {
10448 pf->num_vmdq_vsis = min_t(int, (vec / 2),
10449 I40E_DEFAULT_NUM_VMDQ_VSI);
10450 }
10451 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10452 pf->num_fdsb_msix = 1;
10453 vec--;
10454 }
10455 pf->num_lan_msix = min_t(int,
10456 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
10457 pf->num_lan_msix);
10458 pf->num_lan_qps = pf->num_lan_msix;
10459 break;
10460 }
10461 }
10462
10463 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
10464 (pf->num_fdsb_msix == 0)) {
10465 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
10466 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10467 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10468 }
10469 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10470 (pf->num_vmdq_msix == 0)) {
10471 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
10472 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
10473 }
10474
10475 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
10476 (pf->num_iwarp_msix == 0)) {
10477 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
10478 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
10479 }
10480 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
10481 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
10482 pf->num_lan_msix,
10483 pf->num_vmdq_msix * pf->num_vmdq_vsis,
10484 pf->num_fdsb_msix,
10485 pf->num_iwarp_msix);
10486
10487 return v_actual;
10488 }
10489
10490 /**
10491 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
10492 * @vsi: the VSI being configured
10493 * @v_idx: index of the vector in the vsi struct
10494 * @cpu: cpu to be used on affinity_mask
10495 *
10496 * We allocate one q_vector. If allocation fails we return -ENOMEM.
10497 **/
10498 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
10499 {
10500 struct i40e_q_vector *q_vector;
10501
10502 /* allocate q_vector */
10503 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
10504 if (!q_vector)
10505 return -ENOMEM;
10506
10507 q_vector->vsi = vsi;
10508 q_vector->v_idx = v_idx;
10509 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
10510
10511 if (vsi->netdev)
10512 netif_napi_add(vsi->netdev, &q_vector->napi,
10513 i40e_napi_poll, NAPI_POLL_WEIGHT);
10514
10515 /* tie q_vector and vsi together */
10516 vsi->q_vectors[v_idx] = q_vector;
10517
10518 return 0;
10519 }
10520
10521 /**
10522 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
10523 * @vsi: the VSI being configured
10524 *
10525 * We allocate one q_vector per queue interrupt. If allocation fails we
10526 * return -ENOMEM.
10527 **/
10528 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
10529 {
10530 struct i40e_pf *pf = vsi->back;
10531 int err, v_idx, num_q_vectors, current_cpu;
10532
10533 /* if not MSIX, give the one vector only to the LAN VSI */
10534 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10535 num_q_vectors = vsi->num_q_vectors;
10536 else if (vsi == pf->vsi[pf->lan_vsi])
10537 num_q_vectors = 1;
10538 else
10539 return -EINVAL;
10540
10541 current_cpu = cpumask_first(cpu_online_mask);
10542
10543 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
10544 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
10545 if (err)
10546 goto err_out;
10547 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
10548 if (unlikely(current_cpu >= nr_cpu_ids))
10549 current_cpu = cpumask_first(cpu_online_mask);
10550 }
10551
10552 return 0;
10553
10554 err_out:
10555 while (v_idx--)
10556 i40e_free_q_vector(vsi, v_idx);
10557
10558 return err;
10559 }
10560
10561 /**
10562 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
10563 * @pf: board private structure to initialize
10564 **/
10565 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
10566 {
10567 int vectors = 0;
10568 ssize_t size;
10569
10570 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
10571 vectors = i40e_init_msix(pf);
10572 if (vectors < 0) {
10573 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
10574 I40E_FLAG_IWARP_ENABLED |
10575 I40E_FLAG_RSS_ENABLED |
10576 I40E_FLAG_DCB_CAPABLE |
10577 I40E_FLAG_DCB_ENABLED |
10578 I40E_FLAG_SRIOV_ENABLED |
10579 I40E_FLAG_FD_SB_ENABLED |
10580 I40E_FLAG_FD_ATR_ENABLED |
10581 I40E_FLAG_VMDQ_ENABLED);
10582 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10583
10584 /* rework the queue expectations without MSIX */
10585 i40e_determine_queue_usage(pf);
10586 }
10587 }
10588
10589 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
10590 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
10591 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
10592 vectors = pci_enable_msi(pf->pdev);
10593 if (vectors < 0) {
10594 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
10595 vectors);
10596 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
10597 }
10598 vectors = 1; /* one MSI or Legacy vector */
10599 }
10600
10601 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
10602 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
10603
10604 /* set up vector assignment tracking */
10605 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
10606 pf->irq_pile = kzalloc(size, GFP_KERNEL);
10607 if (!pf->irq_pile)
10608 return -ENOMEM;
10609
10610 pf->irq_pile->num_entries = vectors;
10611 pf->irq_pile->search_hint = 0;
10612
10613 /* track first vector for misc interrupts, ignore return */
10614 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
10615
10616 return 0;
10617 }
10618
10619 /**
10620 * i40e_restore_interrupt_scheme - Restore the interrupt scheme
10621 * @pf: private board data structure
10622 *
10623 * Restore the interrupt scheme that was cleared when we suspended the
10624 * device. This should be called during resume to re-allocate the q_vectors
10625 * and reacquire IRQs.
10626 */
10627 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
10628 {
10629 int err, i;
10630
10631 /* We cleared the MSI and MSI-X flags when disabling the old interrupt
10632 * scheme. We need to re-enabled them here in order to attempt to
10633 * re-acquire the MSI or MSI-X vectors
10634 */
10635 pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
10636
10637 err = i40e_init_interrupt_scheme(pf);
10638 if (err)
10639 return err;
10640
10641 /* Now that we've re-acquired IRQs, we need to remap the vectors and
10642 * rings together again.
10643 */
10644 for (i = 0; i < pf->num_alloc_vsi; i++) {
10645 if (pf->vsi[i]) {
10646 err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
10647 if (err)
10648 goto err_unwind;
10649 i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
10650 }
10651 }
10652
10653 err = i40e_setup_misc_vector(pf);
10654 if (err)
10655 goto err_unwind;
10656
10657 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
10658 i40e_client_update_msix_info(pf);
10659
10660 return 0;
10661
10662 err_unwind:
10663 while (i--) {
10664 if (pf->vsi[i])
10665 i40e_vsi_free_q_vectors(pf->vsi[i]);
10666 }
10667
10668 return err;
10669 }
10670
10671 /**
10672 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
10673 * @pf: board private structure
10674 *
10675 * This sets up the handler for MSIX 0, which is used to manage the
10676 * non-queue interrupts, e.g. AdminQ and errors. This is not used
10677 * when in MSI or Legacy interrupt mode.
10678 **/
10679 static int i40e_setup_misc_vector(struct i40e_pf *pf)
10680 {
10681 struct i40e_hw *hw = &pf->hw;
10682 int err = 0;
10683
10684 /* Only request the IRQ once, the first time through. */
10685 if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
10686 err = request_irq(pf->msix_entries[0].vector,
10687 i40e_intr, 0, pf->int_name, pf);
10688 if (err) {
10689 clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
10690 dev_info(&pf->pdev->dev,
10691 "request_irq for %s failed: %d\n",
10692 pf->int_name, err);
10693 return -EFAULT;
10694 }
10695 }
10696
10697 i40e_enable_misc_int_causes(pf);
10698
10699 /* associate no queues to the misc vector */
10700 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
10701 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
10702
10703 i40e_flush(hw);
10704
10705 i40e_irq_dynamic_enable_icr0(pf);
10706
10707 return err;
10708 }
10709
10710 /**
10711 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
10712 * @vsi: Pointer to vsi structure
10713 * @seed: Buffter to store the hash keys
10714 * @lut: Buffer to store the lookup table entries
10715 * @lut_size: Size of buffer to store the lookup table entries
10716 *
10717 * Return 0 on success, negative on failure
10718 */
10719 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
10720 u8 *lut, u16 lut_size)
10721 {
10722 struct i40e_pf *pf = vsi->back;
10723 struct i40e_hw *hw = &pf->hw;
10724 int ret = 0;
10725
10726 if (seed) {
10727 ret = i40e_aq_get_rss_key(hw, vsi->id,
10728 (struct i40e_aqc_get_set_rss_key_data *)seed);
10729 if (ret) {
10730 dev_info(&pf->pdev->dev,
10731 "Cannot get RSS key, err %s aq_err %s\n",
10732 i40e_stat_str(&pf->hw, ret),
10733 i40e_aq_str(&pf->hw,
10734 pf->hw.aq.asq_last_status));
10735 return ret;
10736 }
10737 }
10738
10739 if (lut) {
10740 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
10741
10742 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
10743 if (ret) {
10744 dev_info(&pf->pdev->dev,
10745 "Cannot get RSS lut, err %s aq_err %s\n",
10746 i40e_stat_str(&pf->hw, ret),
10747 i40e_aq_str(&pf->hw,
10748 pf->hw.aq.asq_last_status));
10749 return ret;
10750 }
10751 }
10752
10753 return ret;
10754 }
10755
10756 /**
10757 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
10758 * @vsi: Pointer to vsi structure
10759 * @seed: RSS hash seed
10760 * @lut: Lookup table
10761 * @lut_size: Lookup table size
10762 *
10763 * Returns 0 on success, negative on failure
10764 **/
10765 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
10766 const u8 *lut, u16 lut_size)
10767 {
10768 struct i40e_pf *pf = vsi->back;
10769 struct i40e_hw *hw = &pf->hw;
10770 u16 vf_id = vsi->vf_id;
10771 u8 i;
10772
10773 /* Fill out hash function seed */
10774 if (seed) {
10775 u32 *seed_dw = (u32 *)seed;
10776
10777 if (vsi->type == I40E_VSI_MAIN) {
10778 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
10779 wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
10780 } else if (vsi->type == I40E_VSI_SRIOV) {
10781 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
10782 wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
10783 } else {
10784 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
10785 }
10786 }
10787
10788 if (lut) {
10789 u32 *lut_dw = (u32 *)lut;
10790
10791 if (vsi->type == I40E_VSI_MAIN) {
10792 if (lut_size != I40E_HLUT_ARRAY_SIZE)
10793 return -EINVAL;
10794 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
10795 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
10796 } else if (vsi->type == I40E_VSI_SRIOV) {
10797 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
10798 return -EINVAL;
10799 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
10800 wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
10801 } else {
10802 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
10803 }
10804 }
10805 i40e_flush(hw);
10806
10807 return 0;
10808 }
10809
10810 /**
10811 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
10812 * @vsi: Pointer to VSI structure
10813 * @seed: Buffer to store the keys
10814 * @lut: Buffer to store the lookup table entries
10815 * @lut_size: Size of buffer to store the lookup table entries
10816 *
10817 * Returns 0 on success, negative on failure
10818 */
10819 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
10820 u8 *lut, u16 lut_size)
10821 {
10822 struct i40e_pf *pf = vsi->back;
10823 struct i40e_hw *hw = &pf->hw;
10824 u16 i;
10825
10826 if (seed) {
10827 u32 *seed_dw = (u32 *)seed;
10828
10829 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
10830 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
10831 }
10832 if (lut) {
10833 u32 *lut_dw = (u32 *)lut;
10834
10835 if (lut_size != I40E_HLUT_ARRAY_SIZE)
10836 return -EINVAL;
10837 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
10838 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
10839 }
10840
10841 return 0;
10842 }
10843
10844 /**
10845 * i40e_config_rss - Configure RSS keys and lut
10846 * @vsi: Pointer to VSI structure
10847 * @seed: RSS hash seed
10848 * @lut: Lookup table
10849 * @lut_size: Lookup table size
10850 *
10851 * Returns 0 on success, negative on failure
10852 */
10853 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
10854 {
10855 struct i40e_pf *pf = vsi->back;
10856
10857 if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
10858 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
10859 else
10860 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
10861 }
10862
10863 /**
10864 * i40e_get_rss - Get RSS keys and lut
10865 * @vsi: Pointer to VSI structure
10866 * @seed: Buffer to store the keys
10867 * @lut: Buffer to store the lookup table entries
10868 * @lut_size: Size of buffer to store the lookup table entries
10869 *
10870 * Returns 0 on success, negative on failure
10871 */
10872 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
10873 {
10874 struct i40e_pf *pf = vsi->back;
10875
10876 if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
10877 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
10878 else
10879 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
10880 }
10881
10882 /**
10883 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
10884 * @pf: Pointer to board private structure
10885 * @lut: Lookup table
10886 * @rss_table_size: Lookup table size
10887 * @rss_size: Range of queue number for hashing
10888 */
10889 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
10890 u16 rss_table_size, u16 rss_size)
10891 {
10892 u16 i;
10893
10894 for (i = 0; i < rss_table_size; i++)
10895 lut[i] = i % rss_size;
10896 }
10897
10898 /**
10899 * i40e_pf_config_rss - Prepare for RSS if used
10900 * @pf: board private structure
10901 **/
10902 static int i40e_pf_config_rss(struct i40e_pf *pf)
10903 {
10904 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10905 u8 seed[I40E_HKEY_ARRAY_SIZE];
10906 u8 *lut;
10907 struct i40e_hw *hw = &pf->hw;
10908 u32 reg_val;
10909 u64 hena;
10910 int ret;
10911
10912 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
10913 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
10914 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
10915 hena |= i40e_pf_get_default_rss_hena(pf);
10916
10917 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
10918 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
10919
10920 /* Determine the RSS table size based on the hardware capabilities */
10921 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
10922 reg_val = (pf->rss_table_size == 512) ?
10923 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
10924 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
10925 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
10926
10927 /* Determine the RSS size of the VSI */
10928 if (!vsi->rss_size) {
10929 u16 qcount;
10930 /* If the firmware does something weird during VSI init, we
10931 * could end up with zero TCs. Check for that to avoid
10932 * divide-by-zero. It probably won't pass traffic, but it also
10933 * won't panic.
10934 */
10935 qcount = vsi->num_queue_pairs /
10936 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
10937 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
10938 }
10939 if (!vsi->rss_size)
10940 return -EINVAL;
10941
10942 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
10943 if (!lut)
10944 return -ENOMEM;
10945
10946 /* Use user configured lut if there is one, otherwise use default */
10947 if (vsi->rss_lut_user)
10948 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
10949 else
10950 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
10951
10952 /* Use user configured hash key if there is one, otherwise
10953 * use default.
10954 */
10955 if (vsi->rss_hkey_user)
10956 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
10957 else
10958 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
10959 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
10960 kfree(lut);
10961
10962 return ret;
10963 }
10964
10965 /**
10966 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
10967 * @pf: board private structure
10968 * @queue_count: the requested queue count for rss.
10969 *
10970 * returns 0 if rss is not enabled, if enabled returns the final rss queue
10971 * count which may be different from the requested queue count.
10972 * Note: expects to be called while under rtnl_lock()
10973 **/
10974 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
10975 {
10976 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10977 int new_rss_size;
10978
10979 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
10980 return 0;
10981
10982 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
10983
10984 if (queue_count != vsi->num_queue_pairs) {
10985 u16 qcount;
10986
10987 vsi->req_queue_pairs = queue_count;
10988 i40e_prep_for_reset(pf, true);
10989
10990 pf->alloc_rss_size = new_rss_size;
10991
10992 i40e_reset_and_rebuild(pf, true, true);
10993
10994 /* Discard the user configured hash keys and lut, if less
10995 * queues are enabled.
10996 */
10997 if (queue_count < vsi->rss_size) {
10998 i40e_clear_rss_config_user(vsi);
10999 dev_dbg(&pf->pdev->dev,
11000 "discard user configured hash keys and lut\n");
11001 }
11002
11003 /* Reset vsi->rss_size, as number of enabled queues changed */
11004 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
11005 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
11006
11007 i40e_pf_config_rss(pf);
11008 }
11009 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
11010 vsi->req_queue_pairs, pf->rss_size_max);
11011 return pf->alloc_rss_size;
11012 }
11013
11014 /**
11015 * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
11016 * @pf: board private structure
11017 **/
11018 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
11019 {
11020 i40e_status status;
11021 bool min_valid, max_valid;
11022 u32 max_bw, min_bw;
11023
11024 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
11025 &min_valid, &max_valid);
11026
11027 if (!status) {
11028 if (min_valid)
11029 pf->min_bw = min_bw;
11030 if (max_valid)
11031 pf->max_bw = max_bw;
11032 }
11033
11034 return status;
11035 }
11036
11037 /**
11038 * i40e_set_partition_bw_setting - Set BW settings for this PF partition
11039 * @pf: board private structure
11040 **/
11041 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
11042 {
11043 struct i40e_aqc_configure_partition_bw_data bw_data;
11044 i40e_status status;
11045
11046 /* Set the valid bit for this PF */
11047 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
11048 bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
11049 bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
11050
11051 /* Set the new bandwidths */
11052 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
11053
11054 return status;
11055 }
11056
11057 /**
11058 * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
11059 * @pf: board private structure
11060 **/
11061 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
11062 {
11063 /* Commit temporary BW setting to permanent NVM image */
11064 enum i40e_admin_queue_err last_aq_status;
11065 i40e_status ret;
11066 u16 nvm_word;
11067
11068 if (pf->hw.partition_id != 1) {
11069 dev_info(&pf->pdev->dev,
11070 "Commit BW only works on partition 1! This is partition %d",
11071 pf->hw.partition_id);
11072 ret = I40E_NOT_SUPPORTED;
11073 goto bw_commit_out;
11074 }
11075
11076 /* Acquire NVM for read access */
11077 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
11078 last_aq_status = pf->hw.aq.asq_last_status;
11079 if (ret) {
11080 dev_info(&pf->pdev->dev,
11081 "Cannot acquire NVM for read access, err %s aq_err %s\n",
11082 i40e_stat_str(&pf->hw, ret),
11083 i40e_aq_str(&pf->hw, last_aq_status));
11084 goto bw_commit_out;
11085 }
11086
11087 /* Read word 0x10 of NVM - SW compatibility word 1 */
11088 ret = i40e_aq_read_nvm(&pf->hw,
11089 I40E_SR_NVM_CONTROL_WORD,
11090 0x10, sizeof(nvm_word), &nvm_word,
11091 false, NULL);
11092 /* Save off last admin queue command status before releasing
11093 * the NVM
11094 */
11095 last_aq_status = pf->hw.aq.asq_last_status;
11096 i40e_release_nvm(&pf->hw);
11097 if (ret) {
11098 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
11099 i40e_stat_str(&pf->hw, ret),
11100 i40e_aq_str(&pf->hw, last_aq_status));
11101 goto bw_commit_out;
11102 }
11103
11104 /* Wait a bit for NVM release to complete */
11105 msleep(50);
11106
11107 /* Acquire NVM for write access */
11108 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
11109 last_aq_status = pf->hw.aq.asq_last_status;
11110 if (ret) {
11111 dev_info(&pf->pdev->dev,
11112 "Cannot acquire NVM for write access, err %s aq_err %s\n",
11113 i40e_stat_str(&pf->hw, ret),
11114 i40e_aq_str(&pf->hw, last_aq_status));
11115 goto bw_commit_out;
11116 }
11117 /* Write it back out unchanged to initiate update NVM,
11118 * which will force a write of the shadow (alt) RAM to
11119 * the NVM - thus storing the bandwidth values permanently.
11120 */
11121 ret = i40e_aq_update_nvm(&pf->hw,
11122 I40E_SR_NVM_CONTROL_WORD,
11123 0x10, sizeof(nvm_word),
11124 &nvm_word, true, 0, NULL);
11125 /* Save off last admin queue command status before releasing
11126 * the NVM
11127 */
11128 last_aq_status = pf->hw.aq.asq_last_status;
11129 i40e_release_nvm(&pf->hw);
11130 if (ret)
11131 dev_info(&pf->pdev->dev,
11132 "BW settings NOT SAVED, err %s aq_err %s\n",
11133 i40e_stat_str(&pf->hw, ret),
11134 i40e_aq_str(&pf->hw, last_aq_status));
11135 bw_commit_out:
11136
11137 return ret;
11138 }
11139
11140 /**
11141 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
11142 * @pf: board private structure to initialize
11143 *
11144 * i40e_sw_init initializes the Adapter private data structure.
11145 * Fields are initialized based on PCI device information and
11146 * OS network device settings (MTU size).
11147 **/
11148 static int i40e_sw_init(struct i40e_pf *pf)
11149 {
11150 int err = 0;
11151 int size;
11152
11153 /* Set default capability flags */
11154 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
11155 I40E_FLAG_MSI_ENABLED |
11156 I40E_FLAG_MSIX_ENABLED;
11157
11158 /* Set default ITR */
11159 pf->rx_itr_default = I40E_ITR_RX_DEF;
11160 pf->tx_itr_default = I40E_ITR_TX_DEF;
11161
11162 /* Depending on PF configurations, it is possible that the RSS
11163 * maximum might end up larger than the available queues
11164 */
11165 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
11166 pf->alloc_rss_size = 1;
11167 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
11168 pf->rss_size_max = min_t(int, pf->rss_size_max,
11169 pf->hw.func_caps.num_tx_qp);
11170 if (pf->hw.func_caps.rss) {
11171 pf->flags |= I40E_FLAG_RSS_ENABLED;
11172 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
11173 num_online_cpus());
11174 }
11175
11176 /* MFP mode enabled */
11177 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
11178 pf->flags |= I40E_FLAG_MFP_ENABLED;
11179 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
11180 if (i40e_get_partition_bw_setting(pf)) {
11181 dev_warn(&pf->pdev->dev,
11182 "Could not get partition bw settings\n");
11183 } else {
11184 dev_info(&pf->pdev->dev,
11185 "Partition BW Min = %8.8x, Max = %8.8x\n",
11186 pf->min_bw, pf->max_bw);
11187
11188 /* nudge the Tx scheduler */
11189 i40e_set_partition_bw_setting(pf);
11190 }
11191 }
11192
11193 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
11194 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
11195 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
11196 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
11197 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
11198 pf->hw.num_partitions > 1)
11199 dev_info(&pf->pdev->dev,
11200 "Flow Director Sideband mode Disabled in MFP mode\n");
11201 else
11202 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
11203 pf->fdir_pf_filter_count =
11204 pf->hw.func_caps.fd_filters_guaranteed;
11205 pf->hw.fdir_shared_filter_count =
11206 pf->hw.func_caps.fd_filters_best_effort;
11207 }
11208
11209 if (pf->hw.mac.type == I40E_MAC_X722) {
11210 pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
11211 I40E_HW_128_QP_RSS_CAPABLE |
11212 I40E_HW_ATR_EVICT_CAPABLE |
11213 I40E_HW_WB_ON_ITR_CAPABLE |
11214 I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
11215 I40E_HW_NO_PCI_LINK_CHECK |
11216 I40E_HW_USE_SET_LLDP_MIB |
11217 I40E_HW_GENEVE_OFFLOAD_CAPABLE |
11218 I40E_HW_PTP_L4_CAPABLE |
11219 I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
11220 I40E_HW_OUTER_UDP_CSUM_CAPABLE);
11221
11222 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
11223 if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
11224 I40E_FDEVICT_PCTYPE_DEFAULT) {
11225 dev_warn(&pf->pdev->dev,
11226 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
11227 pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
11228 }
11229 } else if ((pf->hw.aq.api_maj_ver > 1) ||
11230 ((pf->hw.aq.api_maj_ver == 1) &&
11231 (pf->hw.aq.api_min_ver > 4))) {
11232 /* Supported in FW API version higher than 1.4 */
11233 pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
11234 }
11235
11236 /* Enable HW ATR eviction if possible */
11237 if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
11238 pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
11239
11240 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11241 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
11242 (pf->hw.aq.fw_maj_ver < 4))) {
11243 pf->hw_features |= I40E_HW_RESTART_AUTONEG;
11244 /* No DCB support for FW < v4.33 */
11245 pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
11246 }
11247
11248 /* Disable FW LLDP if FW < v4.3 */
11249 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11250 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
11251 (pf->hw.aq.fw_maj_ver < 4)))
11252 pf->hw_features |= I40E_HW_STOP_FW_LLDP;
11253
11254 /* Use the FW Set LLDP MIB API if FW > v4.40 */
11255 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11256 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
11257 (pf->hw.aq.fw_maj_ver >= 5)))
11258 pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
11259
11260 /* Enable PTP L4 if FW > v6.0 */
11261 if (pf->hw.mac.type == I40E_MAC_XL710 &&
11262 pf->hw.aq.fw_maj_ver >= 6)
11263 pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
11264
11265 if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
11266 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
11267 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
11268 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
11269 }
11270
11271 if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
11272 pf->flags |= I40E_FLAG_IWARP_ENABLED;
11273 /* IWARP needs one extra vector for CQP just like MISC.*/
11274 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
11275 }
11276 /* Stopping the FW LLDP engine is only supported on the
11277 * XL710 with a FW ver >= 1.7. Also, stopping FW LLDP
11278 * engine is not supported if NPAR is functioning on this
11279 * part
11280 */
11281 if (pf->hw.mac.type == I40E_MAC_XL710 &&
11282 !pf->hw.func_caps.npar_enable &&
11283 (pf->hw.aq.api_maj_ver > 1 ||
11284 (pf->hw.aq.api_maj_ver == 1 && pf->hw.aq.api_min_ver > 6)))
11285 pf->hw_features |= I40E_HW_STOPPABLE_FW_LLDP;
11286
11287 #ifdef CONFIG_PCI_IOV
11288 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
11289 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
11290 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
11291 pf->num_req_vfs = min_t(int,
11292 pf->hw.func_caps.num_vfs,
11293 I40E_MAX_VF_COUNT);
11294 }
11295 #endif /* CONFIG_PCI_IOV */
11296 pf->eeprom_version = 0xDEAD;
11297 pf->lan_veb = I40E_NO_VEB;
11298 pf->lan_vsi = I40E_NO_VSI;
11299
11300 /* By default FW has this off for performance reasons */
11301 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
11302
11303 /* set up queue assignment tracking */
11304 size = sizeof(struct i40e_lump_tracking)
11305 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
11306 pf->qp_pile = kzalloc(size, GFP_KERNEL);
11307 if (!pf->qp_pile) {
11308 err = -ENOMEM;
11309 goto sw_init_done;
11310 }
11311 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
11312 pf->qp_pile->search_hint = 0;
11313
11314 pf->tx_timeout_recovery_level = 1;
11315
11316 mutex_init(&pf->switch_mutex);
11317
11318 sw_init_done:
11319 return err;
11320 }
11321
11322 /**
11323 * i40e_set_ntuple - set the ntuple feature flag and take action
11324 * @pf: board private structure to initialize
11325 * @features: the feature set that the stack is suggesting
11326 *
11327 * returns a bool to indicate if reset needs to happen
11328 **/
11329 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
11330 {
11331 bool need_reset = false;
11332
11333 /* Check if Flow Director n-tuple support was enabled or disabled. If
11334 * the state changed, we need to reset.
11335 */
11336 if (features & NETIF_F_NTUPLE) {
11337 /* Enable filters and mark for reset */
11338 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
11339 need_reset = true;
11340 /* enable FD_SB only if there is MSI-X vector and no cloud
11341 * filters exist
11342 */
11343 if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
11344 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
11345 pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
11346 }
11347 } else {
11348 /* turn off filters, mark for reset and clear SW filter list */
11349 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11350 need_reset = true;
11351 i40e_fdir_filter_exit(pf);
11352 }
11353 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11354 clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
11355 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11356
11357 /* reset fd counters */
11358 pf->fd_add_err = 0;
11359 pf->fd_atr_cnt = 0;
11360 /* if ATR was auto disabled it can be re-enabled. */
11361 if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
11362 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
11363 (I40E_DEBUG_FD & pf->hw.debug_mask))
11364 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
11365 }
11366 return need_reset;
11367 }
11368
11369 /**
11370 * i40e_clear_rss_lut - clear the rx hash lookup table
11371 * @vsi: the VSI being configured
11372 **/
11373 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
11374 {
11375 struct i40e_pf *pf = vsi->back;
11376 struct i40e_hw *hw = &pf->hw;
11377 u16 vf_id = vsi->vf_id;
11378 u8 i;
11379
11380 if (vsi->type == I40E_VSI_MAIN) {
11381 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
11382 wr32(hw, I40E_PFQF_HLUT(i), 0);
11383 } else if (vsi->type == I40E_VSI_SRIOV) {
11384 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
11385 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
11386 } else {
11387 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
11388 }
11389 }
11390
11391 /**
11392 * i40e_set_features - set the netdev feature flags
11393 * @netdev: ptr to the netdev being adjusted
11394 * @features: the feature set that the stack is suggesting
11395 * Note: expects to be called while under rtnl_lock()
11396 **/
11397 static int i40e_set_features(struct net_device *netdev,
11398 netdev_features_t features)
11399 {
11400 struct i40e_netdev_priv *np = netdev_priv(netdev);
11401 struct i40e_vsi *vsi = np->vsi;
11402 struct i40e_pf *pf = vsi->back;
11403 bool need_reset;
11404
11405 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
11406 i40e_pf_config_rss(pf);
11407 else if (!(features & NETIF_F_RXHASH) &&
11408 netdev->features & NETIF_F_RXHASH)
11409 i40e_clear_rss_lut(vsi);
11410
11411 if (features & NETIF_F_HW_VLAN_CTAG_RX)
11412 i40e_vlan_stripping_enable(vsi);
11413 else
11414 i40e_vlan_stripping_disable(vsi);
11415
11416 if (!(features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
11417 dev_err(&pf->pdev->dev,
11418 "Offloaded tc filters active, can't turn hw_tc_offload off");
11419 return -EINVAL;
11420 }
11421
11422 need_reset = i40e_set_ntuple(pf, features);
11423
11424 if (need_reset)
11425 i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
11426
11427 return 0;
11428 }
11429
11430 /**
11431 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
11432 * @pf: board private structure
11433 * @port: The UDP port to look up
11434 *
11435 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
11436 **/
11437 static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
11438 {
11439 u8 i;
11440
11441 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
11442 /* Do not report ports with pending deletions as
11443 * being available.
11444 */
11445 if (!port && (pf->pending_udp_bitmap & BIT_ULL(i)))
11446 continue;
11447 if (pf->udp_ports[i].port == port)
11448 return i;
11449 }
11450
11451 return i;
11452 }
11453
11454 /**
11455 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
11456 * @netdev: This physical port's netdev
11457 * @ti: Tunnel endpoint information
11458 **/
11459 static void i40e_udp_tunnel_add(struct net_device *netdev,
11460 struct udp_tunnel_info *ti)
11461 {
11462 struct i40e_netdev_priv *np = netdev_priv(netdev);
11463 struct i40e_vsi *vsi = np->vsi;
11464 struct i40e_pf *pf = vsi->back;
11465 u16 port = ntohs(ti->port);
11466 u8 next_idx;
11467 u8 idx;
11468
11469 idx = i40e_get_udp_port_idx(pf, port);
11470
11471 /* Check if port already exists */
11472 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
11473 netdev_info(netdev, "port %d already offloaded\n", port);
11474 return;
11475 }
11476
11477 /* Now check if there is space to add the new port */
11478 next_idx = i40e_get_udp_port_idx(pf, 0);
11479
11480 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
11481 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
11482 port);
11483 return;
11484 }
11485
11486 switch (ti->type) {
11487 case UDP_TUNNEL_TYPE_VXLAN:
11488 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
11489 break;
11490 case UDP_TUNNEL_TYPE_GENEVE:
11491 if (!(pf->hw_features & I40E_HW_GENEVE_OFFLOAD_CAPABLE))
11492 return;
11493 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
11494 break;
11495 default:
11496 return;
11497 }
11498
11499 /* New port: add it and mark its index in the bitmap */
11500 pf->udp_ports[next_idx].port = port;
11501 pf->udp_ports[next_idx].filter_index = I40E_UDP_PORT_INDEX_UNUSED;
11502 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
11503 set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
11504 }
11505
11506 /**
11507 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
11508 * @netdev: This physical port's netdev
11509 * @ti: Tunnel endpoint information
11510 **/
11511 static void i40e_udp_tunnel_del(struct net_device *netdev,
11512 struct udp_tunnel_info *ti)
11513 {
11514 struct i40e_netdev_priv *np = netdev_priv(netdev);
11515 struct i40e_vsi *vsi = np->vsi;
11516 struct i40e_pf *pf = vsi->back;
11517 u16 port = ntohs(ti->port);
11518 u8 idx;
11519
11520 idx = i40e_get_udp_port_idx(pf, port);
11521
11522 /* Check if port already exists */
11523 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
11524 goto not_found;
11525
11526 switch (ti->type) {
11527 case UDP_TUNNEL_TYPE_VXLAN:
11528 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
11529 goto not_found;
11530 break;
11531 case UDP_TUNNEL_TYPE_GENEVE:
11532 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
11533 goto not_found;
11534 break;
11535 default:
11536 goto not_found;
11537 }
11538
11539 /* if port exists, set it to 0 (mark for deletion)
11540 * and make it pending
11541 */
11542 pf->udp_ports[idx].port = 0;
11543
11544 /* Toggle pending bit instead of setting it. This way if we are
11545 * deleting a port that has yet to be added we just clear the pending
11546 * bit and don't have to worry about it.
11547 */
11548 pf->pending_udp_bitmap ^= BIT_ULL(idx);
11549 set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
11550
11551 return;
11552 not_found:
11553 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
11554 port);
11555 }
11556
11557 static int i40e_get_phys_port_id(struct net_device *netdev,
11558 struct netdev_phys_item_id *ppid)
11559 {
11560 struct i40e_netdev_priv *np = netdev_priv(netdev);
11561 struct i40e_pf *pf = np->vsi->back;
11562 struct i40e_hw *hw = &pf->hw;
11563
11564 if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
11565 return -EOPNOTSUPP;
11566
11567 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
11568 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
11569
11570 return 0;
11571 }
11572
11573 /**
11574 * i40e_ndo_fdb_add - add an entry to the hardware database
11575 * @ndm: the input from the stack
11576 * @tb: pointer to array of nladdr (unused)
11577 * @dev: the net device pointer
11578 * @addr: the MAC address entry being added
11579 * @vid: VLAN ID
11580 * @flags: instructions from stack about fdb operation
11581 */
11582 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
11583 struct net_device *dev,
11584 const unsigned char *addr, u16 vid,
11585 u16 flags)
11586 {
11587 struct i40e_netdev_priv *np = netdev_priv(dev);
11588 struct i40e_pf *pf = np->vsi->back;
11589 int err = 0;
11590
11591 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
11592 return -EOPNOTSUPP;
11593
11594 if (vid) {
11595 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
11596 return -EINVAL;
11597 }
11598
11599 /* Hardware does not support aging addresses so if a
11600 * ndm_state is given only allow permanent addresses
11601 */
11602 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
11603 netdev_info(dev, "FDB only supports static addresses\n");
11604 return -EINVAL;
11605 }
11606
11607 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
11608 err = dev_uc_add_excl(dev, addr);
11609 else if (is_multicast_ether_addr(addr))
11610 err = dev_mc_add_excl(dev, addr);
11611 else
11612 err = -EINVAL;
11613
11614 /* Only return duplicate errors if NLM_F_EXCL is set */
11615 if (err == -EEXIST && !(flags & NLM_F_EXCL))
11616 err = 0;
11617
11618 return err;
11619 }
11620
11621 /**
11622 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
11623 * @dev: the netdev being configured
11624 * @nlh: RTNL message
11625 * @flags: bridge flags
11626 *
11627 * Inserts a new hardware bridge if not already created and
11628 * enables the bridging mode requested (VEB or VEPA). If the
11629 * hardware bridge has already been inserted and the request
11630 * is to change the mode then that requires a PF reset to
11631 * allow rebuild of the components with required hardware
11632 * bridge mode enabled.
11633 *
11634 * Note: expects to be called while under rtnl_lock()
11635 **/
11636 static int i40e_ndo_bridge_setlink(struct net_device *dev,
11637 struct nlmsghdr *nlh,
11638 u16 flags)
11639 {
11640 struct i40e_netdev_priv *np = netdev_priv(dev);
11641 struct i40e_vsi *vsi = np->vsi;
11642 struct i40e_pf *pf = vsi->back;
11643 struct i40e_veb *veb = NULL;
11644 struct nlattr *attr, *br_spec;
11645 int i, rem;
11646
11647 /* Only for PF VSI for now */
11648 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
11649 return -EOPNOTSUPP;
11650
11651 /* Find the HW bridge for PF VSI */
11652 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
11653 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
11654 veb = pf->veb[i];
11655 }
11656
11657 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
11658
11659 nla_for_each_nested(attr, br_spec, rem) {
11660 __u16 mode;
11661
11662 if (nla_type(attr) != IFLA_BRIDGE_MODE)
11663 continue;
11664
11665 mode = nla_get_u16(attr);
11666 if ((mode != BRIDGE_MODE_VEPA) &&
11667 (mode != BRIDGE_MODE_VEB))
11668 return -EINVAL;
11669
11670 /* Insert a new HW bridge */
11671 if (!veb) {
11672 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
11673 vsi->tc_config.enabled_tc);
11674 if (veb) {
11675 veb->bridge_mode = mode;
11676 i40e_config_bridge_mode(veb);
11677 } else {
11678 /* No Bridge HW offload available */
11679 return -ENOENT;
11680 }
11681 break;
11682 } else if (mode != veb->bridge_mode) {
11683 /* Existing HW bridge but different mode needs reset */
11684 veb->bridge_mode = mode;
11685 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
11686 if (mode == BRIDGE_MODE_VEB)
11687 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11688 else
11689 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
11690 i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
11691 break;
11692 }
11693 }
11694
11695 return 0;
11696 }
11697
11698 /**
11699 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
11700 * @skb: skb buff
11701 * @pid: process id
11702 * @seq: RTNL message seq #
11703 * @dev: the netdev being configured
11704 * @filter_mask: unused
11705 * @nlflags: netlink flags passed in
11706 *
11707 * Return the mode in which the hardware bridge is operating in
11708 * i.e VEB or VEPA.
11709 **/
11710 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
11711 struct net_device *dev,
11712 u32 __always_unused filter_mask,
11713 int nlflags)
11714 {
11715 struct i40e_netdev_priv *np = netdev_priv(dev);
11716 struct i40e_vsi *vsi = np->vsi;
11717 struct i40e_pf *pf = vsi->back;
11718 struct i40e_veb *veb = NULL;
11719 int i;
11720
11721 /* Only for PF VSI for now */
11722 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
11723 return -EOPNOTSUPP;
11724
11725 /* Find the HW bridge for the PF VSI */
11726 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
11727 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
11728 veb = pf->veb[i];
11729 }
11730
11731 if (!veb)
11732 return 0;
11733
11734 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
11735 0, 0, nlflags, filter_mask, NULL);
11736 }
11737
11738 /**
11739 * i40e_features_check - Validate encapsulated packet conforms to limits
11740 * @skb: skb buff
11741 * @dev: This physical port's netdev
11742 * @features: Offload features that the stack believes apply
11743 **/
11744 static netdev_features_t i40e_features_check(struct sk_buff *skb,
11745 struct net_device *dev,
11746 netdev_features_t features)
11747 {
11748 size_t len;
11749
11750 /* No point in doing any of this if neither checksum nor GSO are
11751 * being requested for this frame. We can rule out both by just
11752 * checking for CHECKSUM_PARTIAL
11753 */
11754 if (skb->ip_summed != CHECKSUM_PARTIAL)
11755 return features;
11756
11757 /* We cannot support GSO if the MSS is going to be less than
11758 * 64 bytes. If it is then we need to drop support for GSO.
11759 */
11760 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
11761 features &= ~NETIF_F_GSO_MASK;
11762
11763 /* MACLEN can support at most 63 words */
11764 len = skb_network_header(skb) - skb->data;
11765 if (len & ~(63 * 2))
11766 goto out_err;
11767
11768 /* IPLEN and EIPLEN can support at most 127 dwords */
11769 len = skb_transport_header(skb) - skb_network_header(skb);
11770 if (len & ~(127 * 4))
11771 goto out_err;
11772
11773 if (skb->encapsulation) {
11774 /* L4TUNLEN can support 127 words */
11775 len = skb_inner_network_header(skb) - skb_transport_header(skb);
11776 if (len & ~(127 * 2))
11777 goto out_err;
11778
11779 /* IPLEN can support at most 127 dwords */
11780 len = skb_inner_transport_header(skb) -
11781 skb_inner_network_header(skb);
11782 if (len & ~(127 * 4))
11783 goto out_err;
11784 }
11785
11786 /* No need to validate L4LEN as TCP is the only protocol with a
11787 * a flexible value and we support all possible values supported
11788 * by TCP, which is at most 15 dwords
11789 */
11790
11791 return features;
11792 out_err:
11793 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
11794 }
11795
11796 /**
11797 * i40e_xdp_setup - add/remove an XDP program
11798 * @vsi: VSI to changed
11799 * @prog: XDP program
11800 **/
11801 static int i40e_xdp_setup(struct i40e_vsi *vsi,
11802 struct bpf_prog *prog)
11803 {
11804 int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
11805 struct i40e_pf *pf = vsi->back;
11806 struct bpf_prog *old_prog;
11807 bool need_reset;
11808 int i;
11809
11810 /* Don't allow frames that span over multiple buffers */
11811 if (frame_size > vsi->rx_buf_len)
11812 return -EINVAL;
11813
11814 if (!i40e_enabled_xdp_vsi(vsi) && !prog)
11815 return 0;
11816
11817 /* When turning XDP on->off/off->on we reset and rebuild the rings. */
11818 need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
11819
11820 if (need_reset)
11821 i40e_prep_for_reset(pf, true);
11822
11823 old_prog = xchg(&vsi->xdp_prog, prog);
11824
11825 if (need_reset)
11826 i40e_reset_and_rebuild(pf, true, true);
11827
11828 for (i = 0; i < vsi->num_queue_pairs; i++)
11829 WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
11830
11831 if (old_prog)
11832 bpf_prog_put(old_prog);
11833
11834 return 0;
11835 }
11836
11837 /**
11838 * i40e_xdp - implements ndo_bpf for i40e
11839 * @dev: netdevice
11840 * @xdp: XDP command
11841 **/
11842 static int i40e_xdp(struct net_device *dev,
11843 struct netdev_bpf *xdp)
11844 {
11845 struct i40e_netdev_priv *np = netdev_priv(dev);
11846 struct i40e_vsi *vsi = np->vsi;
11847
11848 if (vsi->type != I40E_VSI_MAIN)
11849 return -EINVAL;
11850
11851 switch (xdp->command) {
11852 case XDP_SETUP_PROG:
11853 return i40e_xdp_setup(vsi, xdp->prog);
11854 case XDP_QUERY_PROG:
11855 xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0;
11856 return 0;
11857 default:
11858 return -EINVAL;
11859 }
11860 }
11861
11862 static const struct net_device_ops i40e_netdev_ops = {
11863 .ndo_open = i40e_open,
11864 .ndo_stop = i40e_close,
11865 .ndo_start_xmit = i40e_lan_xmit_frame,
11866 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
11867 .ndo_set_rx_mode = i40e_set_rx_mode,
11868 .ndo_validate_addr = eth_validate_addr,
11869 .ndo_set_mac_address = i40e_set_mac,
11870 .ndo_change_mtu = i40e_change_mtu,
11871 .ndo_do_ioctl = i40e_ioctl,
11872 .ndo_tx_timeout = i40e_tx_timeout,
11873 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
11874 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
11875 #ifdef CONFIG_NET_POLL_CONTROLLER
11876 .ndo_poll_controller = i40e_netpoll,
11877 #endif
11878 .ndo_setup_tc = __i40e_setup_tc,
11879 .ndo_set_features = i40e_set_features,
11880 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
11881 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
11882 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
11883 .ndo_get_vf_config = i40e_ndo_get_vf_config,
11884 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
11885 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
11886 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
11887 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
11888 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
11889 .ndo_get_phys_port_id = i40e_get_phys_port_id,
11890 .ndo_fdb_add = i40e_ndo_fdb_add,
11891 .ndo_features_check = i40e_features_check,
11892 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
11893 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
11894 .ndo_bpf = i40e_xdp,
11895 .ndo_xdp_xmit = i40e_xdp_xmit,
11896 };
11897
11898 /**
11899 * i40e_config_netdev - Setup the netdev flags
11900 * @vsi: the VSI being configured
11901 *
11902 * Returns 0 on success, negative value on failure
11903 **/
11904 static int i40e_config_netdev(struct i40e_vsi *vsi)
11905 {
11906 struct i40e_pf *pf = vsi->back;
11907 struct i40e_hw *hw = &pf->hw;
11908 struct i40e_netdev_priv *np;
11909 struct net_device *netdev;
11910 u8 broadcast[ETH_ALEN];
11911 u8 mac_addr[ETH_ALEN];
11912 int etherdev_size;
11913 netdev_features_t hw_enc_features;
11914 netdev_features_t hw_features;
11915
11916 etherdev_size = sizeof(struct i40e_netdev_priv);
11917 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
11918 if (!netdev)
11919 return -ENOMEM;
11920
11921 vsi->netdev = netdev;
11922 np = netdev_priv(netdev);
11923 np->vsi = vsi;
11924
11925 hw_enc_features = NETIF_F_SG |
11926 NETIF_F_IP_CSUM |
11927 NETIF_F_IPV6_CSUM |
11928 NETIF_F_HIGHDMA |
11929 NETIF_F_SOFT_FEATURES |
11930 NETIF_F_TSO |
11931 NETIF_F_TSO_ECN |
11932 NETIF_F_TSO6 |
11933 NETIF_F_GSO_GRE |
11934 NETIF_F_GSO_GRE_CSUM |
11935 NETIF_F_GSO_PARTIAL |
11936 NETIF_F_GSO_IPXIP4 |
11937 NETIF_F_GSO_IPXIP6 |
11938 NETIF_F_GSO_UDP_TUNNEL |
11939 NETIF_F_GSO_UDP_TUNNEL_CSUM |
11940 NETIF_F_SCTP_CRC |
11941 NETIF_F_RXHASH |
11942 NETIF_F_RXCSUM |
11943 0;
11944
11945 if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
11946 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
11947
11948 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
11949
11950 netdev->hw_enc_features |= hw_enc_features;
11951
11952 /* record features VLANs can make use of */
11953 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
11954
11955 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
11956 netdev->hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
11957
11958 hw_features = hw_enc_features |
11959 NETIF_F_HW_VLAN_CTAG_TX |
11960 NETIF_F_HW_VLAN_CTAG_RX;
11961
11962 netdev->hw_features |= hw_features;
11963
11964 netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
11965 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
11966
11967 if (vsi->type == I40E_VSI_MAIN) {
11968 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
11969 ether_addr_copy(mac_addr, hw->mac.perm_addr);
11970 /* The following steps are necessary for two reasons. First,
11971 * some older NVM configurations load a default MAC-VLAN
11972 * filter that will accept any tagged packet, and we want to
11973 * replace this with a normal filter. Additionally, it is
11974 * possible our MAC address was provided by the platform using
11975 * Open Firmware or similar.
11976 *
11977 * Thus, we need to remove the default filter and install one
11978 * specific to the MAC address.
11979 */
11980 i40e_rm_default_mac_filter(vsi, mac_addr);
11981 spin_lock_bh(&vsi->mac_filter_hash_lock);
11982 i40e_add_mac_filter(vsi, mac_addr);
11983 spin_unlock_bh(&vsi->mac_filter_hash_lock);
11984 } else {
11985 /* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
11986 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
11987 * the end, which is 4 bytes long, so force truncation of the
11988 * original name by IFNAMSIZ - 4
11989 */
11990 snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
11991 IFNAMSIZ - 4,
11992 pf->vsi[pf->lan_vsi]->netdev->name);
11993 eth_random_addr(mac_addr);
11994
11995 spin_lock_bh(&vsi->mac_filter_hash_lock);
11996 i40e_add_mac_filter(vsi, mac_addr);
11997 spin_unlock_bh(&vsi->mac_filter_hash_lock);
11998 }
11999
12000 /* Add the broadcast filter so that we initially will receive
12001 * broadcast packets. Note that when a new VLAN is first added the
12002 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
12003 * specific filters as part of transitioning into "vlan" operation.
12004 * When more VLANs are added, the driver will copy each existing MAC
12005 * filter and add it for the new VLAN.
12006 *
12007 * Broadcast filters are handled specially by
12008 * i40e_sync_filters_subtask, as the driver must to set the broadcast
12009 * promiscuous bit instead of adding this directly as a MAC/VLAN
12010 * filter. The subtask will update the correct broadcast promiscuous
12011 * bits as VLANs become active or inactive.
12012 */
12013 eth_broadcast_addr(broadcast);
12014 spin_lock_bh(&vsi->mac_filter_hash_lock);
12015 i40e_add_mac_filter(vsi, broadcast);
12016 spin_unlock_bh(&vsi->mac_filter_hash_lock);
12017
12018 ether_addr_copy(netdev->dev_addr, mac_addr);
12019 ether_addr_copy(netdev->perm_addr, mac_addr);
12020
12021 /* i40iw_net_event() reads 16 bytes from neigh->primary_key */
12022 netdev->neigh_priv_len = sizeof(u32) * 4;
12023
12024 netdev->priv_flags |= IFF_UNICAST_FLT;
12025 netdev->priv_flags |= IFF_SUPP_NOFCS;
12026 /* Setup netdev TC information */
12027 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
12028
12029 netdev->netdev_ops = &i40e_netdev_ops;
12030 netdev->watchdog_timeo = 5 * HZ;
12031 i40e_set_ethtool_ops(netdev);
12032
12033 /* MTU range: 68 - 9706 */
12034 netdev->min_mtu = ETH_MIN_MTU;
12035 netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
12036
12037 return 0;
12038 }
12039
12040 /**
12041 * i40e_vsi_delete - Delete a VSI from the switch
12042 * @vsi: the VSI being removed
12043 *
12044 * Returns 0 on success, negative value on failure
12045 **/
12046 static void i40e_vsi_delete(struct i40e_vsi *vsi)
12047 {
12048 /* remove default VSI is not allowed */
12049 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
12050 return;
12051
12052 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
12053 }
12054
12055 /**
12056 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
12057 * @vsi: the VSI being queried
12058 *
12059 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
12060 **/
12061 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
12062 {
12063 struct i40e_veb *veb;
12064 struct i40e_pf *pf = vsi->back;
12065
12066 /* Uplink is not a bridge so default to VEB */
12067 if (vsi->veb_idx == I40E_NO_VEB)
12068 return 1;
12069
12070 veb = pf->veb[vsi->veb_idx];
12071 if (!veb) {
12072 dev_info(&pf->pdev->dev,
12073 "There is no veb associated with the bridge\n");
12074 return -ENOENT;
12075 }
12076
12077 /* Uplink is a bridge in VEPA mode */
12078 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
12079 return 0;
12080 } else {
12081 /* Uplink is a bridge in VEB mode */
12082 return 1;
12083 }
12084
12085 /* VEPA is now default bridge, so return 0 */
12086 return 0;
12087 }
12088
12089 /**
12090 * i40e_add_vsi - Add a VSI to the switch
12091 * @vsi: the VSI being configured
12092 *
12093 * This initializes a VSI context depending on the VSI type to be added and
12094 * passes it down to the add_vsi aq command.
12095 **/
12096 static int i40e_add_vsi(struct i40e_vsi *vsi)
12097 {
12098 int ret = -ENODEV;
12099 struct i40e_pf *pf = vsi->back;
12100 struct i40e_hw *hw = &pf->hw;
12101 struct i40e_vsi_context ctxt;
12102 struct i40e_mac_filter *f;
12103 struct hlist_node *h;
12104 int bkt;
12105
12106 u8 enabled_tc = 0x1; /* TC0 enabled */
12107 int f_count = 0;
12108
12109 memset(&ctxt, 0, sizeof(ctxt));
12110 switch (vsi->type) {
12111 case I40E_VSI_MAIN:
12112 /* The PF's main VSI is already setup as part of the
12113 * device initialization, so we'll not bother with
12114 * the add_vsi call, but we will retrieve the current
12115 * VSI context.
12116 */
12117 ctxt.seid = pf->main_vsi_seid;
12118 ctxt.pf_num = pf->hw.pf_id;
12119 ctxt.vf_num = 0;
12120 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
12121 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
12122 if (ret) {
12123 dev_info(&pf->pdev->dev,
12124 "couldn't get PF vsi config, err %s aq_err %s\n",
12125 i40e_stat_str(&pf->hw, ret),
12126 i40e_aq_str(&pf->hw,
12127 pf->hw.aq.asq_last_status));
12128 return -ENOENT;
12129 }
12130 vsi->info = ctxt.info;
12131 vsi->info.valid_sections = 0;
12132
12133 vsi->seid = ctxt.seid;
12134 vsi->id = ctxt.vsi_number;
12135
12136 enabled_tc = i40e_pf_get_tc_map(pf);
12137
12138 /* Source pruning is enabled by default, so the flag is
12139 * negative logic - if it's set, we need to fiddle with
12140 * the VSI to disable source pruning.
12141 */
12142 if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
12143 memset(&ctxt, 0, sizeof(ctxt));
12144 ctxt.seid = pf->main_vsi_seid;
12145 ctxt.pf_num = pf->hw.pf_id;
12146 ctxt.vf_num = 0;
12147 ctxt.info.valid_sections |=
12148 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
12149 ctxt.info.switch_id =
12150 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
12151 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
12152 if (ret) {
12153 dev_info(&pf->pdev->dev,
12154 "update vsi failed, err %s aq_err %s\n",
12155 i40e_stat_str(&pf->hw, ret),
12156 i40e_aq_str(&pf->hw,
12157 pf->hw.aq.asq_last_status));
12158 ret = -ENOENT;
12159 goto err;
12160 }
12161 }
12162
12163 /* MFP mode setup queue map and update VSI */
12164 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
12165 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
12166 memset(&ctxt, 0, sizeof(ctxt));
12167 ctxt.seid = pf->main_vsi_seid;
12168 ctxt.pf_num = pf->hw.pf_id;
12169 ctxt.vf_num = 0;
12170 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
12171 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
12172 if (ret) {
12173 dev_info(&pf->pdev->dev,
12174 "update vsi failed, err %s aq_err %s\n",
12175 i40e_stat_str(&pf->hw, ret),
12176 i40e_aq_str(&pf->hw,
12177 pf->hw.aq.asq_last_status));
12178 ret = -ENOENT;
12179 goto err;
12180 }
12181 /* update the local VSI info queue map */
12182 i40e_vsi_update_queue_map(vsi, &ctxt);
12183 vsi->info.valid_sections = 0;
12184 } else {
12185 /* Default/Main VSI is only enabled for TC0
12186 * reconfigure it to enable all TCs that are
12187 * available on the port in SFP mode.
12188 * For MFP case the iSCSI PF would use this
12189 * flow to enable LAN+iSCSI TC.
12190 */
12191 ret = i40e_vsi_config_tc(vsi, enabled_tc);
12192 if (ret) {
12193 /* Single TC condition is not fatal,
12194 * message and continue
12195 */
12196 dev_info(&pf->pdev->dev,
12197 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
12198 enabled_tc,
12199 i40e_stat_str(&pf->hw, ret),
12200 i40e_aq_str(&pf->hw,
12201 pf->hw.aq.asq_last_status));
12202 }
12203 }
12204 break;
12205
12206 case I40E_VSI_FDIR:
12207 ctxt.pf_num = hw->pf_id;
12208 ctxt.vf_num = 0;
12209 ctxt.uplink_seid = vsi->uplink_seid;
12210 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
12211 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
12212 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
12213 (i40e_is_vsi_uplink_mode_veb(vsi))) {
12214 ctxt.info.valid_sections |=
12215 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
12216 ctxt.info.switch_id =
12217 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
12218 }
12219 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
12220 break;
12221
12222 case I40E_VSI_VMDQ2:
12223 ctxt.pf_num = hw->pf_id;
12224 ctxt.vf_num = 0;
12225 ctxt.uplink_seid = vsi->uplink_seid;
12226 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
12227 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
12228
12229 /* This VSI is connected to VEB so the switch_id
12230 * should be set to zero by default.
12231 */
12232 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
12233 ctxt.info.valid_sections |=
12234 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
12235 ctxt.info.switch_id =
12236 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
12237 }
12238
12239 /* Setup the VSI tx/rx queue map for TC0 only for now */
12240 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
12241 break;
12242
12243 case I40E_VSI_SRIOV:
12244 ctxt.pf_num = hw->pf_id;
12245 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
12246 ctxt.uplink_seid = vsi->uplink_seid;
12247 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
12248 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
12249
12250 /* This VSI is connected to VEB so the switch_id
12251 * should be set to zero by default.
12252 */
12253 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
12254 ctxt.info.valid_sections |=
12255 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
12256 ctxt.info.switch_id =
12257 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
12258 }
12259
12260 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
12261 ctxt.info.valid_sections |=
12262 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
12263 ctxt.info.queueing_opt_flags |=
12264 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
12265 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
12266 }
12267
12268 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
12269 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
12270 if (pf->vf[vsi->vf_id].spoofchk) {
12271 ctxt.info.valid_sections |=
12272 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
12273 ctxt.info.sec_flags |=
12274 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
12275 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
12276 }
12277 /* Setup the VSI tx/rx queue map for TC0 only for now */
12278 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
12279 break;
12280
12281 case I40E_VSI_IWARP:
12282 /* send down message to iWARP */
12283 break;
12284
12285 default:
12286 return -ENODEV;
12287 }
12288
12289 if (vsi->type != I40E_VSI_MAIN) {
12290 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
12291 if (ret) {
12292 dev_info(&vsi->back->pdev->dev,
12293 "add vsi failed, err %s aq_err %s\n",
12294 i40e_stat_str(&pf->hw, ret),
12295 i40e_aq_str(&pf->hw,
12296 pf->hw.aq.asq_last_status));
12297 ret = -ENOENT;
12298 goto err;
12299 }
12300 vsi->info = ctxt.info;
12301 vsi->info.valid_sections = 0;
12302 vsi->seid = ctxt.seid;
12303 vsi->id = ctxt.vsi_number;
12304 }
12305
12306 vsi->active_filters = 0;
12307 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
12308 spin_lock_bh(&vsi->mac_filter_hash_lock);
12309 /* If macvlan filters already exist, force them to get loaded */
12310 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
12311 f->state = I40E_FILTER_NEW;
12312 f_count++;
12313 }
12314 spin_unlock_bh(&vsi->mac_filter_hash_lock);
12315
12316 if (f_count) {
12317 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
12318 set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
12319 }
12320
12321 /* Update VSI BW information */
12322 ret = i40e_vsi_get_bw_info(vsi);
12323 if (ret) {
12324 dev_info(&pf->pdev->dev,
12325 "couldn't get vsi bw info, err %s aq_err %s\n",
12326 i40e_stat_str(&pf->hw, ret),
12327 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
12328 /* VSI is already added so not tearing that up */
12329 ret = 0;
12330 }
12331
12332 err:
12333 return ret;
12334 }
12335
12336 /**
12337 * i40e_vsi_release - Delete a VSI and free its resources
12338 * @vsi: the VSI being removed
12339 *
12340 * Returns 0 on success or < 0 on error
12341 **/
12342 int i40e_vsi_release(struct i40e_vsi *vsi)
12343 {
12344 struct i40e_mac_filter *f;
12345 struct hlist_node *h;
12346 struct i40e_veb *veb = NULL;
12347 struct i40e_pf *pf;
12348 u16 uplink_seid;
12349 int i, n, bkt;
12350
12351 pf = vsi->back;
12352
12353 /* release of a VEB-owner or last VSI is not allowed */
12354 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
12355 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
12356 vsi->seid, vsi->uplink_seid);
12357 return -ENODEV;
12358 }
12359 if (vsi == pf->vsi[pf->lan_vsi] &&
12360 !test_bit(__I40E_DOWN, pf->state)) {
12361 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
12362 return -ENODEV;
12363 }
12364
12365 uplink_seid = vsi->uplink_seid;
12366 if (vsi->type != I40E_VSI_SRIOV) {
12367 if (vsi->netdev_registered) {
12368 vsi->netdev_registered = false;
12369 if (vsi->netdev) {
12370 /* results in a call to i40e_close() */
12371 unregister_netdev(vsi->netdev);
12372 }
12373 } else {
12374 i40e_vsi_close(vsi);
12375 }
12376 i40e_vsi_disable_irq(vsi);
12377 }
12378
12379 spin_lock_bh(&vsi->mac_filter_hash_lock);
12380
12381 /* clear the sync flag on all filters */
12382 if (vsi->netdev) {
12383 __dev_uc_unsync(vsi->netdev, NULL);
12384 __dev_mc_unsync(vsi->netdev, NULL);
12385 }
12386
12387 /* make sure any remaining filters are marked for deletion */
12388 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
12389 __i40e_del_filter(vsi, f);
12390
12391 spin_unlock_bh(&vsi->mac_filter_hash_lock);
12392
12393 i40e_sync_vsi_filters(vsi);
12394
12395 i40e_vsi_delete(vsi);
12396 i40e_vsi_free_q_vectors(vsi);
12397 if (vsi->netdev) {
12398 free_netdev(vsi->netdev);
12399 vsi->netdev = NULL;
12400 }
12401 i40e_vsi_clear_rings(vsi);
12402 i40e_vsi_clear(vsi);
12403
12404 /* If this was the last thing on the VEB, except for the
12405 * controlling VSI, remove the VEB, which puts the controlling
12406 * VSI onto the next level down in the switch.
12407 *
12408 * Well, okay, there's one more exception here: don't remove
12409 * the orphan VEBs yet. We'll wait for an explicit remove request
12410 * from up the network stack.
12411 */
12412 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
12413 if (pf->vsi[i] &&
12414 pf->vsi[i]->uplink_seid == uplink_seid &&
12415 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
12416 n++; /* count the VSIs */
12417 }
12418 }
12419 for (i = 0; i < I40E_MAX_VEB; i++) {
12420 if (!pf->veb[i])
12421 continue;
12422 if (pf->veb[i]->uplink_seid == uplink_seid)
12423 n++; /* count the VEBs */
12424 if (pf->veb[i]->seid == uplink_seid)
12425 veb = pf->veb[i];
12426 }
12427 if (n == 0 && veb && veb->uplink_seid != 0)
12428 i40e_veb_release(veb);
12429
12430 return 0;
12431 }
12432
12433 /**
12434 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
12435 * @vsi: ptr to the VSI
12436 *
12437 * This should only be called after i40e_vsi_mem_alloc() which allocates the
12438 * corresponding SW VSI structure and initializes num_queue_pairs for the
12439 * newly allocated VSI.
12440 *
12441 * Returns 0 on success or negative on failure
12442 **/
12443 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
12444 {
12445 int ret = -ENOENT;
12446 struct i40e_pf *pf = vsi->back;
12447
12448 if (vsi->q_vectors[0]) {
12449 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
12450 vsi->seid);
12451 return -EEXIST;
12452 }
12453
12454 if (vsi->base_vector) {
12455 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
12456 vsi->seid, vsi->base_vector);
12457 return -EEXIST;
12458 }
12459
12460 ret = i40e_vsi_alloc_q_vectors(vsi);
12461 if (ret) {
12462 dev_info(&pf->pdev->dev,
12463 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
12464 vsi->num_q_vectors, vsi->seid, ret);
12465 vsi->num_q_vectors = 0;
12466 goto vector_setup_out;
12467 }
12468
12469 /* In Legacy mode, we do not have to get any other vector since we
12470 * piggyback on the misc/ICR0 for queue interrupts.
12471 */
12472 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
12473 return ret;
12474 if (vsi->num_q_vectors)
12475 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
12476 vsi->num_q_vectors, vsi->idx);
12477 if (vsi->base_vector < 0) {
12478 dev_info(&pf->pdev->dev,
12479 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
12480 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
12481 i40e_vsi_free_q_vectors(vsi);
12482 ret = -ENOENT;
12483 goto vector_setup_out;
12484 }
12485
12486 vector_setup_out:
12487 return ret;
12488 }
12489
12490 /**
12491 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
12492 * @vsi: pointer to the vsi.
12493 *
12494 * This re-allocates a vsi's queue resources.
12495 *
12496 * Returns pointer to the successfully allocated and configured VSI sw struct
12497 * on success, otherwise returns NULL on failure.
12498 **/
12499 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
12500 {
12501 u16 alloc_queue_pairs;
12502 struct i40e_pf *pf;
12503 u8 enabled_tc;
12504 int ret;
12505
12506 if (!vsi)
12507 return NULL;
12508
12509 pf = vsi->back;
12510
12511 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
12512 i40e_vsi_clear_rings(vsi);
12513
12514 i40e_vsi_free_arrays(vsi, false);
12515 i40e_set_num_rings_in_vsi(vsi);
12516 ret = i40e_vsi_alloc_arrays(vsi, false);
12517 if (ret)
12518 goto err_vsi;
12519
12520 alloc_queue_pairs = vsi->alloc_queue_pairs *
12521 (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
12522
12523 ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
12524 if (ret < 0) {
12525 dev_info(&pf->pdev->dev,
12526 "failed to get tracking for %d queues for VSI %d err %d\n",
12527 alloc_queue_pairs, vsi->seid, ret);
12528 goto err_vsi;
12529 }
12530 vsi->base_queue = ret;
12531
12532 /* Update the FW view of the VSI. Force a reset of TC and queue
12533 * layout configurations.
12534 */
12535 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
12536 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
12537 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
12538 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
12539 if (vsi->type == I40E_VSI_MAIN)
12540 i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
12541
12542 /* assign it some queues */
12543 ret = i40e_alloc_rings(vsi);
12544 if (ret)
12545 goto err_rings;
12546
12547 /* map all of the rings to the q_vectors */
12548 i40e_vsi_map_rings_to_vectors(vsi);
12549 return vsi;
12550
12551 err_rings:
12552 i40e_vsi_free_q_vectors(vsi);
12553 if (vsi->netdev_registered) {
12554 vsi->netdev_registered = false;
12555 unregister_netdev(vsi->netdev);
12556 free_netdev(vsi->netdev);
12557 vsi->netdev = NULL;
12558 }
12559 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
12560 err_vsi:
12561 i40e_vsi_clear(vsi);
12562 return NULL;
12563 }
12564
12565 /**
12566 * i40e_vsi_setup - Set up a VSI by a given type
12567 * @pf: board private structure
12568 * @type: VSI type
12569 * @uplink_seid: the switch element to link to
12570 * @param1: usage depends upon VSI type. For VF types, indicates VF id
12571 *
12572 * This allocates the sw VSI structure and its queue resources, then add a VSI
12573 * to the identified VEB.
12574 *
12575 * Returns pointer to the successfully allocated and configure VSI sw struct on
12576 * success, otherwise returns NULL on failure.
12577 **/
12578 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
12579 u16 uplink_seid, u32 param1)
12580 {
12581 struct i40e_vsi *vsi = NULL;
12582 struct i40e_veb *veb = NULL;
12583 u16 alloc_queue_pairs;
12584 int ret, i;
12585 int v_idx;
12586
12587 /* The requested uplink_seid must be either
12588 * - the PF's port seid
12589 * no VEB is needed because this is the PF
12590 * or this is a Flow Director special case VSI
12591 * - seid of an existing VEB
12592 * - seid of a VSI that owns an existing VEB
12593 * - seid of a VSI that doesn't own a VEB
12594 * a new VEB is created and the VSI becomes the owner
12595 * - seid of the PF VSI, which is what creates the first VEB
12596 * this is a special case of the previous
12597 *
12598 * Find which uplink_seid we were given and create a new VEB if needed
12599 */
12600 for (i = 0; i < I40E_MAX_VEB; i++) {
12601 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
12602 veb = pf->veb[i];
12603 break;
12604 }
12605 }
12606
12607 if (!veb && uplink_seid != pf->mac_seid) {
12608
12609 for (i = 0; i < pf->num_alloc_vsi; i++) {
12610 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
12611 vsi = pf->vsi[i];
12612 break;
12613 }
12614 }
12615 if (!vsi) {
12616 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
12617 uplink_seid);
12618 return NULL;
12619 }
12620
12621 if (vsi->uplink_seid == pf->mac_seid)
12622 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
12623 vsi->tc_config.enabled_tc);
12624 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
12625 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
12626 vsi->tc_config.enabled_tc);
12627 if (veb) {
12628 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
12629 dev_info(&vsi->back->pdev->dev,
12630 "New VSI creation error, uplink seid of LAN VSI expected.\n");
12631 return NULL;
12632 }
12633 /* We come up by default in VEPA mode if SRIOV is not
12634 * already enabled, in which case we can't force VEPA
12635 * mode.
12636 */
12637 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
12638 veb->bridge_mode = BRIDGE_MODE_VEPA;
12639 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
12640 }
12641 i40e_config_bridge_mode(veb);
12642 }
12643 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12644 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12645 veb = pf->veb[i];
12646 }
12647 if (!veb) {
12648 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
12649 return NULL;
12650 }
12651
12652 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
12653 uplink_seid = veb->seid;
12654 }
12655
12656 /* get vsi sw struct */
12657 v_idx = i40e_vsi_mem_alloc(pf, type);
12658 if (v_idx < 0)
12659 goto err_alloc;
12660 vsi = pf->vsi[v_idx];
12661 if (!vsi)
12662 goto err_alloc;
12663 vsi->type = type;
12664 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
12665
12666 if (type == I40E_VSI_MAIN)
12667 pf->lan_vsi = v_idx;
12668 else if (type == I40E_VSI_SRIOV)
12669 vsi->vf_id = param1;
12670 /* assign it some queues */
12671 alloc_queue_pairs = vsi->alloc_queue_pairs *
12672 (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
12673
12674 ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
12675 if (ret < 0) {
12676 dev_info(&pf->pdev->dev,
12677 "failed to get tracking for %d queues for VSI %d err=%d\n",
12678 alloc_queue_pairs, vsi->seid, ret);
12679 goto err_vsi;
12680 }
12681 vsi->base_queue = ret;
12682
12683 /* get a VSI from the hardware */
12684 vsi->uplink_seid = uplink_seid;
12685 ret = i40e_add_vsi(vsi);
12686 if (ret)
12687 goto err_vsi;
12688
12689 switch (vsi->type) {
12690 /* setup the netdev if needed */
12691 case I40E_VSI_MAIN:
12692 case I40E_VSI_VMDQ2:
12693 ret = i40e_config_netdev(vsi);
12694 if (ret)
12695 goto err_netdev;
12696 ret = register_netdev(vsi->netdev);
12697 if (ret)
12698 goto err_netdev;
12699 vsi->netdev_registered = true;
12700 netif_carrier_off(vsi->netdev);
12701 #ifdef CONFIG_I40E_DCB
12702 /* Setup DCB netlink interface */
12703 i40e_dcbnl_setup(vsi);
12704 #endif /* CONFIG_I40E_DCB */
12705 /* fall through */
12706
12707 case I40E_VSI_FDIR:
12708 /* set up vectors and rings if needed */
12709 ret = i40e_vsi_setup_vectors(vsi);
12710 if (ret)
12711 goto err_msix;
12712
12713 ret = i40e_alloc_rings(vsi);
12714 if (ret)
12715 goto err_rings;
12716
12717 /* map all of the rings to the q_vectors */
12718 i40e_vsi_map_rings_to_vectors(vsi);
12719
12720 i40e_vsi_reset_stats(vsi);
12721 break;
12722
12723 default:
12724 /* no netdev or rings for the other VSI types */
12725 break;
12726 }
12727
12728 if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
12729 (vsi->type == I40E_VSI_VMDQ2)) {
12730 ret = i40e_vsi_config_rss(vsi);
12731 }
12732 return vsi;
12733
12734 err_rings:
12735 i40e_vsi_free_q_vectors(vsi);
12736 err_msix:
12737 if (vsi->netdev_registered) {
12738 vsi->netdev_registered = false;
12739 unregister_netdev(vsi->netdev);
12740 free_netdev(vsi->netdev);
12741 vsi->netdev = NULL;
12742 }
12743 err_netdev:
12744 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
12745 err_vsi:
12746 i40e_vsi_clear(vsi);
12747 err_alloc:
12748 return NULL;
12749 }
12750
12751 /**
12752 * i40e_veb_get_bw_info - Query VEB BW information
12753 * @veb: the veb to query
12754 *
12755 * Query the Tx scheduler BW configuration data for given VEB
12756 **/
12757 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
12758 {
12759 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
12760 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
12761 struct i40e_pf *pf = veb->pf;
12762 struct i40e_hw *hw = &pf->hw;
12763 u32 tc_bw_max;
12764 int ret = 0;
12765 int i;
12766
12767 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
12768 &bw_data, NULL);
12769 if (ret) {
12770 dev_info(&pf->pdev->dev,
12771 "query veb bw config failed, err %s aq_err %s\n",
12772 i40e_stat_str(&pf->hw, ret),
12773 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
12774 goto out;
12775 }
12776
12777 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
12778 &ets_data, NULL);
12779 if (ret) {
12780 dev_info(&pf->pdev->dev,
12781 "query veb bw ets config failed, err %s aq_err %s\n",
12782 i40e_stat_str(&pf->hw, ret),
12783 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
12784 goto out;
12785 }
12786
12787 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
12788 veb->bw_max_quanta = ets_data.tc_bw_max;
12789 veb->is_abs_credits = bw_data.absolute_credits_enable;
12790 veb->enabled_tc = ets_data.tc_valid_bits;
12791 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
12792 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
12793 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
12794 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
12795 veb->bw_tc_limit_credits[i] =
12796 le16_to_cpu(bw_data.tc_bw_limits[i]);
12797 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
12798 }
12799
12800 out:
12801 return ret;
12802 }
12803
12804 /**
12805 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
12806 * @pf: board private structure
12807 *
12808 * On error: returns error code (negative)
12809 * On success: returns vsi index in PF (positive)
12810 **/
12811 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
12812 {
12813 int ret = -ENOENT;
12814 struct i40e_veb *veb;
12815 int i;
12816
12817 /* Need to protect the allocation of switch elements at the PF level */
12818 mutex_lock(&pf->switch_mutex);
12819
12820 /* VEB list may be fragmented if VEB creation/destruction has
12821 * been happening. We can afford to do a quick scan to look
12822 * for any free slots in the list.
12823 *
12824 * find next empty veb slot, looping back around if necessary
12825 */
12826 i = 0;
12827 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
12828 i++;
12829 if (i >= I40E_MAX_VEB) {
12830 ret = -ENOMEM;
12831 goto err_alloc_veb; /* out of VEB slots! */
12832 }
12833
12834 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
12835 if (!veb) {
12836 ret = -ENOMEM;
12837 goto err_alloc_veb;
12838 }
12839 veb->pf = pf;
12840 veb->idx = i;
12841 veb->enabled_tc = 1;
12842
12843 pf->veb[i] = veb;
12844 ret = i;
12845 err_alloc_veb:
12846 mutex_unlock(&pf->switch_mutex);
12847 return ret;
12848 }
12849
12850 /**
12851 * i40e_switch_branch_release - Delete a branch of the switch tree
12852 * @branch: where to start deleting
12853 *
12854 * This uses recursion to find the tips of the branch to be
12855 * removed, deleting until we get back to and can delete this VEB.
12856 **/
12857 static void i40e_switch_branch_release(struct i40e_veb *branch)
12858 {
12859 struct i40e_pf *pf = branch->pf;
12860 u16 branch_seid = branch->seid;
12861 u16 veb_idx = branch->idx;
12862 int i;
12863
12864 /* release any VEBs on this VEB - RECURSION */
12865 for (i = 0; i < I40E_MAX_VEB; i++) {
12866 if (!pf->veb[i])
12867 continue;
12868 if (pf->veb[i]->uplink_seid == branch->seid)
12869 i40e_switch_branch_release(pf->veb[i]);
12870 }
12871
12872 /* Release the VSIs on this VEB, but not the owner VSI.
12873 *
12874 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
12875 * the VEB itself, so don't use (*branch) after this loop.
12876 */
12877 for (i = 0; i < pf->num_alloc_vsi; i++) {
12878 if (!pf->vsi[i])
12879 continue;
12880 if (pf->vsi[i]->uplink_seid == branch_seid &&
12881 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
12882 i40e_vsi_release(pf->vsi[i]);
12883 }
12884 }
12885
12886 /* There's one corner case where the VEB might not have been
12887 * removed, so double check it here and remove it if needed.
12888 * This case happens if the veb was created from the debugfs
12889 * commands and no VSIs were added to it.
12890 */
12891 if (pf->veb[veb_idx])
12892 i40e_veb_release(pf->veb[veb_idx]);
12893 }
12894
12895 /**
12896 * i40e_veb_clear - remove veb struct
12897 * @veb: the veb to remove
12898 **/
12899 static void i40e_veb_clear(struct i40e_veb *veb)
12900 {
12901 if (!veb)
12902 return;
12903
12904 if (veb->pf) {
12905 struct i40e_pf *pf = veb->pf;
12906
12907 mutex_lock(&pf->switch_mutex);
12908 if (pf->veb[veb->idx] == veb)
12909 pf->veb[veb->idx] = NULL;
12910 mutex_unlock(&pf->switch_mutex);
12911 }
12912
12913 kfree(veb);
12914 }
12915
12916 /**
12917 * i40e_veb_release - Delete a VEB and free its resources
12918 * @veb: the VEB being removed
12919 **/
12920 void i40e_veb_release(struct i40e_veb *veb)
12921 {
12922 struct i40e_vsi *vsi = NULL;
12923 struct i40e_pf *pf;
12924 int i, n = 0;
12925
12926 pf = veb->pf;
12927
12928 /* find the remaining VSI and check for extras */
12929 for (i = 0; i < pf->num_alloc_vsi; i++) {
12930 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
12931 n++;
12932 vsi = pf->vsi[i];
12933 }
12934 }
12935 if (n != 1) {
12936 dev_info(&pf->pdev->dev,
12937 "can't remove VEB %d with %d VSIs left\n",
12938 veb->seid, n);
12939 return;
12940 }
12941
12942 /* move the remaining VSI to uplink veb */
12943 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
12944 if (veb->uplink_seid) {
12945 vsi->uplink_seid = veb->uplink_seid;
12946 if (veb->uplink_seid == pf->mac_seid)
12947 vsi->veb_idx = I40E_NO_VEB;
12948 else
12949 vsi->veb_idx = veb->veb_idx;
12950 } else {
12951 /* floating VEB */
12952 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
12953 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
12954 }
12955
12956 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
12957 i40e_veb_clear(veb);
12958 }
12959
12960 /**
12961 * i40e_add_veb - create the VEB in the switch
12962 * @veb: the VEB to be instantiated
12963 * @vsi: the controlling VSI
12964 **/
12965 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
12966 {
12967 struct i40e_pf *pf = veb->pf;
12968 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
12969 int ret;
12970
12971 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
12972 veb->enabled_tc, false,
12973 &veb->seid, enable_stats, NULL);
12974
12975 /* get a VEB from the hardware */
12976 if (ret) {
12977 dev_info(&pf->pdev->dev,
12978 "couldn't add VEB, err %s aq_err %s\n",
12979 i40e_stat_str(&pf->hw, ret),
12980 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
12981 return -EPERM;
12982 }
12983
12984 /* get statistics counter */
12985 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
12986 &veb->stats_idx, NULL, NULL, NULL);
12987 if (ret) {
12988 dev_info(&pf->pdev->dev,
12989 "couldn't get VEB statistics idx, err %s aq_err %s\n",
12990 i40e_stat_str(&pf->hw, ret),
12991 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
12992 return -EPERM;
12993 }
12994 ret = i40e_veb_get_bw_info(veb);
12995 if (ret) {
12996 dev_info(&pf->pdev->dev,
12997 "couldn't get VEB bw info, err %s aq_err %s\n",
12998 i40e_stat_str(&pf->hw, ret),
12999 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13000 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
13001 return -ENOENT;
13002 }
13003
13004 vsi->uplink_seid = veb->seid;
13005 vsi->veb_idx = veb->idx;
13006 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
13007
13008 return 0;
13009 }
13010
13011 /**
13012 * i40e_veb_setup - Set up a VEB
13013 * @pf: board private structure
13014 * @flags: VEB setup flags
13015 * @uplink_seid: the switch element to link to
13016 * @vsi_seid: the initial VSI seid
13017 * @enabled_tc: Enabled TC bit-map
13018 *
13019 * This allocates the sw VEB structure and links it into the switch
13020 * It is possible and legal for this to be a duplicate of an already
13021 * existing VEB. It is also possible for both uplink and vsi seids
13022 * to be zero, in order to create a floating VEB.
13023 *
13024 * Returns pointer to the successfully allocated VEB sw struct on
13025 * success, otherwise returns NULL on failure.
13026 **/
13027 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
13028 u16 uplink_seid, u16 vsi_seid,
13029 u8 enabled_tc)
13030 {
13031 struct i40e_veb *veb, *uplink_veb = NULL;
13032 int vsi_idx, veb_idx;
13033 int ret;
13034
13035 /* if one seid is 0, the other must be 0 to create a floating relay */
13036 if ((uplink_seid == 0 || vsi_seid == 0) &&
13037 (uplink_seid + vsi_seid != 0)) {
13038 dev_info(&pf->pdev->dev,
13039 "one, not both seid's are 0: uplink=%d vsi=%d\n",
13040 uplink_seid, vsi_seid);
13041 return NULL;
13042 }
13043
13044 /* make sure there is such a vsi and uplink */
13045 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
13046 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
13047 break;
13048 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
13049 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
13050 vsi_seid);
13051 return NULL;
13052 }
13053
13054 if (uplink_seid && uplink_seid != pf->mac_seid) {
13055 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
13056 if (pf->veb[veb_idx] &&
13057 pf->veb[veb_idx]->seid == uplink_seid) {
13058 uplink_veb = pf->veb[veb_idx];
13059 break;
13060 }
13061 }
13062 if (!uplink_veb) {
13063 dev_info(&pf->pdev->dev,
13064 "uplink seid %d not found\n", uplink_seid);
13065 return NULL;
13066 }
13067 }
13068
13069 /* get veb sw struct */
13070 veb_idx = i40e_veb_mem_alloc(pf);
13071 if (veb_idx < 0)
13072 goto err_alloc;
13073 veb = pf->veb[veb_idx];
13074 veb->flags = flags;
13075 veb->uplink_seid = uplink_seid;
13076 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
13077 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
13078
13079 /* create the VEB in the switch */
13080 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
13081 if (ret)
13082 goto err_veb;
13083 if (vsi_idx == pf->lan_vsi)
13084 pf->lan_veb = veb->idx;
13085
13086 return veb;
13087
13088 err_veb:
13089 i40e_veb_clear(veb);
13090 err_alloc:
13091 return NULL;
13092 }
13093
13094 /**
13095 * i40e_setup_pf_switch_element - set PF vars based on switch type
13096 * @pf: board private structure
13097 * @ele: element we are building info from
13098 * @num_reported: total number of elements
13099 * @printconfig: should we print the contents
13100 *
13101 * helper function to assist in extracting a few useful SEID values.
13102 **/
13103 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
13104 struct i40e_aqc_switch_config_element_resp *ele,
13105 u16 num_reported, bool printconfig)
13106 {
13107 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
13108 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
13109 u8 element_type = ele->element_type;
13110 u16 seid = le16_to_cpu(ele->seid);
13111
13112 if (printconfig)
13113 dev_info(&pf->pdev->dev,
13114 "type=%d seid=%d uplink=%d downlink=%d\n",
13115 element_type, seid, uplink_seid, downlink_seid);
13116
13117 switch (element_type) {
13118 case I40E_SWITCH_ELEMENT_TYPE_MAC:
13119 pf->mac_seid = seid;
13120 break;
13121 case I40E_SWITCH_ELEMENT_TYPE_VEB:
13122 /* Main VEB? */
13123 if (uplink_seid != pf->mac_seid)
13124 break;
13125 if (pf->lan_veb == I40E_NO_VEB) {
13126 int v;
13127
13128 /* find existing or else empty VEB */
13129 for (v = 0; v < I40E_MAX_VEB; v++) {
13130 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
13131 pf->lan_veb = v;
13132 break;
13133 }
13134 }
13135 if (pf->lan_veb == I40E_NO_VEB) {
13136 v = i40e_veb_mem_alloc(pf);
13137 if (v < 0)
13138 break;
13139 pf->lan_veb = v;
13140 }
13141 }
13142
13143 pf->veb[pf->lan_veb]->seid = seid;
13144 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
13145 pf->veb[pf->lan_veb]->pf = pf;
13146 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
13147 break;
13148 case I40E_SWITCH_ELEMENT_TYPE_VSI:
13149 if (num_reported != 1)
13150 break;
13151 /* This is immediately after a reset so we can assume this is
13152 * the PF's VSI
13153 */
13154 pf->mac_seid = uplink_seid;
13155 pf->pf_seid = downlink_seid;
13156 pf->main_vsi_seid = seid;
13157 if (printconfig)
13158 dev_info(&pf->pdev->dev,
13159 "pf_seid=%d main_vsi_seid=%d\n",
13160 pf->pf_seid, pf->main_vsi_seid);
13161 break;
13162 case I40E_SWITCH_ELEMENT_TYPE_PF:
13163 case I40E_SWITCH_ELEMENT_TYPE_VF:
13164 case I40E_SWITCH_ELEMENT_TYPE_EMP:
13165 case I40E_SWITCH_ELEMENT_TYPE_BMC:
13166 case I40E_SWITCH_ELEMENT_TYPE_PE:
13167 case I40E_SWITCH_ELEMENT_TYPE_PA:
13168 /* ignore these for now */
13169 break;
13170 default:
13171 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
13172 element_type, seid);
13173 break;
13174 }
13175 }
13176
13177 /**
13178 * i40e_fetch_switch_configuration - Get switch config from firmware
13179 * @pf: board private structure
13180 * @printconfig: should we print the contents
13181 *
13182 * Get the current switch configuration from the device and
13183 * extract a few useful SEID values.
13184 **/
13185 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
13186 {
13187 struct i40e_aqc_get_switch_config_resp *sw_config;
13188 u16 next_seid = 0;
13189 int ret = 0;
13190 u8 *aq_buf;
13191 int i;
13192
13193 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
13194 if (!aq_buf)
13195 return -ENOMEM;
13196
13197 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
13198 do {
13199 u16 num_reported, num_total;
13200
13201 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
13202 I40E_AQ_LARGE_BUF,
13203 &next_seid, NULL);
13204 if (ret) {
13205 dev_info(&pf->pdev->dev,
13206 "get switch config failed err %s aq_err %s\n",
13207 i40e_stat_str(&pf->hw, ret),
13208 i40e_aq_str(&pf->hw,
13209 pf->hw.aq.asq_last_status));
13210 kfree(aq_buf);
13211 return -ENOENT;
13212 }
13213
13214 num_reported = le16_to_cpu(sw_config->header.num_reported);
13215 num_total = le16_to_cpu(sw_config->header.num_total);
13216
13217 if (printconfig)
13218 dev_info(&pf->pdev->dev,
13219 "header: %d reported %d total\n",
13220 num_reported, num_total);
13221
13222 for (i = 0; i < num_reported; i++) {
13223 struct i40e_aqc_switch_config_element_resp *ele =
13224 &sw_config->element[i];
13225
13226 i40e_setup_pf_switch_element(pf, ele, num_reported,
13227 printconfig);
13228 }
13229 } while (next_seid != 0);
13230
13231 kfree(aq_buf);
13232 return ret;
13233 }
13234
13235 /**
13236 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
13237 * @pf: board private structure
13238 * @reinit: if the Main VSI needs to re-initialized.
13239 *
13240 * Returns 0 on success, negative value on failure
13241 **/
13242 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
13243 {
13244 u16 flags = 0;
13245 int ret;
13246
13247 /* find out what's out there already */
13248 ret = i40e_fetch_switch_configuration(pf, false);
13249 if (ret) {
13250 dev_info(&pf->pdev->dev,
13251 "couldn't fetch switch config, err %s aq_err %s\n",
13252 i40e_stat_str(&pf->hw, ret),
13253 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13254 return ret;
13255 }
13256 i40e_pf_reset_stats(pf);
13257
13258 /* set the switch config bit for the whole device to
13259 * support limited promisc or true promisc
13260 * when user requests promisc. The default is limited
13261 * promisc.
13262 */
13263
13264 if ((pf->hw.pf_id == 0) &&
13265 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
13266 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
13267 pf->last_sw_conf_flags = flags;
13268 }
13269
13270 if (pf->hw.pf_id == 0) {
13271 u16 valid_flags;
13272
13273 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
13274 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
13275 NULL);
13276 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
13277 dev_info(&pf->pdev->dev,
13278 "couldn't set switch config bits, err %s aq_err %s\n",
13279 i40e_stat_str(&pf->hw, ret),
13280 i40e_aq_str(&pf->hw,
13281 pf->hw.aq.asq_last_status));
13282 /* not a fatal problem, just keep going */
13283 }
13284 pf->last_sw_conf_valid_flags = valid_flags;
13285 }
13286
13287 /* first time setup */
13288 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
13289 struct i40e_vsi *vsi = NULL;
13290 u16 uplink_seid;
13291
13292 /* Set up the PF VSI associated with the PF's main VSI
13293 * that is already in the HW switch
13294 */
13295 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
13296 uplink_seid = pf->veb[pf->lan_veb]->seid;
13297 else
13298 uplink_seid = pf->mac_seid;
13299 if (pf->lan_vsi == I40E_NO_VSI)
13300 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
13301 else if (reinit)
13302 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
13303 if (!vsi) {
13304 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
13305 i40e_cloud_filter_exit(pf);
13306 i40e_fdir_teardown(pf);
13307 return -EAGAIN;
13308 }
13309 } else {
13310 /* force a reset of TC and queue layout configurations */
13311 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
13312
13313 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
13314 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
13315 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
13316 }
13317 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
13318
13319 i40e_fdir_sb_setup(pf);
13320
13321 /* Setup static PF queue filter control settings */
13322 ret = i40e_setup_pf_filter_control(pf);
13323 if (ret) {
13324 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
13325 ret);
13326 /* Failure here should not stop continuing other steps */
13327 }
13328
13329 /* enable RSS in the HW, even for only one queue, as the stack can use
13330 * the hash
13331 */
13332 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
13333 i40e_pf_config_rss(pf);
13334
13335 /* fill in link information and enable LSE reporting */
13336 i40e_link_event(pf);
13337
13338 /* Initialize user-specific link properties */
13339 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
13340 I40E_AQ_AN_COMPLETED) ? true : false);
13341
13342 i40e_ptp_init(pf);
13343
13344 /* repopulate tunnel port filters */
13345 i40e_sync_udp_filters(pf);
13346
13347 return ret;
13348 }
13349
13350 /**
13351 * i40e_determine_queue_usage - Work out queue distribution
13352 * @pf: board private structure
13353 **/
13354 static void i40e_determine_queue_usage(struct i40e_pf *pf)
13355 {
13356 int queues_left;
13357 int q_max;
13358
13359 pf->num_lan_qps = 0;
13360
13361 /* Find the max queues to be put into basic use. We'll always be
13362 * using TC0, whether or not DCB is running, and TC0 will get the
13363 * big RSS set.
13364 */
13365 queues_left = pf->hw.func_caps.num_tx_qp;
13366
13367 if ((queues_left == 1) ||
13368 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
13369 /* one qp for PF, no queues for anything else */
13370 queues_left = 0;
13371 pf->alloc_rss_size = pf->num_lan_qps = 1;
13372
13373 /* make sure all the fancies are disabled */
13374 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
13375 I40E_FLAG_IWARP_ENABLED |
13376 I40E_FLAG_FD_SB_ENABLED |
13377 I40E_FLAG_FD_ATR_ENABLED |
13378 I40E_FLAG_DCB_CAPABLE |
13379 I40E_FLAG_DCB_ENABLED |
13380 I40E_FLAG_SRIOV_ENABLED |
13381 I40E_FLAG_VMDQ_ENABLED);
13382 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
13383 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
13384 I40E_FLAG_FD_SB_ENABLED |
13385 I40E_FLAG_FD_ATR_ENABLED |
13386 I40E_FLAG_DCB_CAPABLE))) {
13387 /* one qp for PF */
13388 pf->alloc_rss_size = pf->num_lan_qps = 1;
13389 queues_left -= pf->num_lan_qps;
13390
13391 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
13392 I40E_FLAG_IWARP_ENABLED |
13393 I40E_FLAG_FD_SB_ENABLED |
13394 I40E_FLAG_FD_ATR_ENABLED |
13395 I40E_FLAG_DCB_ENABLED |
13396 I40E_FLAG_VMDQ_ENABLED);
13397 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
13398 } else {
13399 /* Not enough queues for all TCs */
13400 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
13401 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
13402 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
13403 I40E_FLAG_DCB_ENABLED);
13404 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
13405 }
13406
13407 /* limit lan qps to the smaller of qps, cpus or msix */
13408 q_max = max_t(int, pf->rss_size_max, num_online_cpus());
13409 q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
13410 q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
13411 pf->num_lan_qps = q_max;
13412
13413 queues_left -= pf->num_lan_qps;
13414 }
13415
13416 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
13417 if (queues_left > 1) {
13418 queues_left -= 1; /* save 1 queue for FD */
13419 } else {
13420 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
13421 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
13422 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
13423 }
13424 }
13425
13426 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
13427 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
13428 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
13429 (queues_left / pf->num_vf_qps));
13430 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
13431 }
13432
13433 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
13434 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
13435 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
13436 (queues_left / pf->num_vmdq_qps));
13437 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
13438 }
13439
13440 pf->queues_left = queues_left;
13441 dev_dbg(&pf->pdev->dev,
13442 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
13443 pf->hw.func_caps.num_tx_qp,
13444 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
13445 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
13446 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
13447 queues_left);
13448 }
13449
13450 /**
13451 * i40e_setup_pf_filter_control - Setup PF static filter control
13452 * @pf: PF to be setup
13453 *
13454 * i40e_setup_pf_filter_control sets up a PF's initial filter control
13455 * settings. If PE/FCoE are enabled then it will also set the per PF
13456 * based filter sizes required for them. It also enables Flow director,
13457 * ethertype and macvlan type filter settings for the pf.
13458 *
13459 * Returns 0 on success, negative on failure
13460 **/
13461 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
13462 {
13463 struct i40e_filter_control_settings *settings = &pf->filter_settings;
13464
13465 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
13466
13467 /* Flow Director is enabled */
13468 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
13469 settings->enable_fdir = true;
13470
13471 /* Ethtype and MACVLAN filters enabled for PF */
13472 settings->enable_ethtype = true;
13473 settings->enable_macvlan = true;
13474
13475 if (i40e_set_filter_control(&pf->hw, settings))
13476 return -ENOENT;
13477
13478 return 0;
13479 }
13480
13481 #define INFO_STRING_LEN 255
13482 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
13483 static void i40e_print_features(struct i40e_pf *pf)
13484 {
13485 struct i40e_hw *hw = &pf->hw;
13486 char *buf;
13487 int i;
13488
13489 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
13490 if (!buf)
13491 return;
13492
13493 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
13494 #ifdef CONFIG_PCI_IOV
13495 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
13496 #endif
13497 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
13498 pf->hw.func_caps.num_vsis,
13499 pf->vsi[pf->lan_vsi]->num_queue_pairs);
13500 if (pf->flags & I40E_FLAG_RSS_ENABLED)
13501 i += snprintf(&buf[i], REMAIN(i), " RSS");
13502 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
13503 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
13504 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
13505 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
13506 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
13507 }
13508 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
13509 i += snprintf(&buf[i], REMAIN(i), " DCB");
13510 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
13511 i += snprintf(&buf[i], REMAIN(i), " Geneve");
13512 if (pf->flags & I40E_FLAG_PTP)
13513 i += snprintf(&buf[i], REMAIN(i), " PTP");
13514 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
13515 i += snprintf(&buf[i], REMAIN(i), " VEB");
13516 else
13517 i += snprintf(&buf[i], REMAIN(i), " VEPA");
13518
13519 dev_info(&pf->pdev->dev, "%s\n", buf);
13520 kfree(buf);
13521 WARN_ON(i > INFO_STRING_LEN);
13522 }
13523
13524 /**
13525 * i40e_get_platform_mac_addr - get platform-specific MAC address
13526 * @pdev: PCI device information struct
13527 * @pf: board private structure
13528 *
13529 * Look up the MAC address for the device. First we'll try
13530 * eth_platform_get_mac_address, which will check Open Firmware, or arch
13531 * specific fallback. Otherwise, we'll default to the stored value in
13532 * firmware.
13533 **/
13534 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
13535 {
13536 if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
13537 i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
13538 }
13539
13540 /**
13541 * i40e_probe - Device initialization routine
13542 * @pdev: PCI device information struct
13543 * @ent: entry in i40e_pci_tbl
13544 *
13545 * i40e_probe initializes a PF identified by a pci_dev structure.
13546 * The OS initialization, configuring of the PF private structure,
13547 * and a hardware reset occur.
13548 *
13549 * Returns 0 on success, negative on failure
13550 **/
13551 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
13552 {
13553 struct i40e_aq_get_phy_abilities_resp abilities;
13554 struct i40e_pf *pf;
13555 struct i40e_hw *hw;
13556 static u16 pfs_found;
13557 u16 wol_nvm_bits;
13558 u16 link_status;
13559 int err;
13560 u32 val;
13561 u32 i;
13562 u8 set_fc_aq_fail;
13563
13564 err = pci_enable_device_mem(pdev);
13565 if (err)
13566 return err;
13567
13568 /* set up for high or low dma */
13569 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
13570 if (err) {
13571 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
13572 if (err) {
13573 dev_err(&pdev->dev,
13574 "DMA configuration failed: 0x%x\n", err);
13575 goto err_dma;
13576 }
13577 }
13578
13579 /* set up pci connections */
13580 err = pci_request_mem_regions(pdev, i40e_driver_name);
13581 if (err) {
13582 dev_info(&pdev->dev,
13583 "pci_request_selected_regions failed %d\n", err);
13584 goto err_pci_reg;
13585 }
13586
13587 pci_enable_pcie_error_reporting(pdev);
13588 pci_set_master(pdev);
13589
13590 /* Now that we have a PCI connection, we need to do the
13591 * low level device setup. This is primarily setting up
13592 * the Admin Queue structures and then querying for the
13593 * device's current profile information.
13594 */
13595 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
13596 if (!pf) {
13597 err = -ENOMEM;
13598 goto err_pf_alloc;
13599 }
13600 pf->next_vsi = 0;
13601 pf->pdev = pdev;
13602 set_bit(__I40E_DOWN, pf->state);
13603
13604 hw = &pf->hw;
13605 hw->back = pf;
13606
13607 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
13608 I40E_MAX_CSR_SPACE);
13609
13610 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
13611 if (!hw->hw_addr) {
13612 err = -EIO;
13613 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
13614 (unsigned int)pci_resource_start(pdev, 0),
13615 pf->ioremap_len, err);
13616 goto err_ioremap;
13617 }
13618 hw->vendor_id = pdev->vendor;
13619 hw->device_id = pdev->device;
13620 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
13621 hw->subsystem_vendor_id = pdev->subsystem_vendor;
13622 hw->subsystem_device_id = pdev->subsystem_device;
13623 hw->bus.device = PCI_SLOT(pdev->devfn);
13624 hw->bus.func = PCI_FUNC(pdev->devfn);
13625 hw->bus.bus_id = pdev->bus->number;
13626 pf->instance = pfs_found;
13627
13628 /* Select something other than the 802.1ad ethertype for the
13629 * switch to use internally and drop on ingress.
13630 */
13631 hw->switch_tag = 0xffff;
13632 hw->first_tag = ETH_P_8021AD;
13633 hw->second_tag = ETH_P_8021Q;
13634
13635 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
13636 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
13637
13638 /* set up the locks for the AQ, do this only once in probe
13639 * and destroy them only once in remove
13640 */
13641 mutex_init(&hw->aq.asq_mutex);
13642 mutex_init(&hw->aq.arq_mutex);
13643
13644 pf->msg_enable = netif_msg_init(debug,
13645 NETIF_MSG_DRV |
13646 NETIF_MSG_PROBE |
13647 NETIF_MSG_LINK);
13648 if (debug < -1)
13649 pf->hw.debug_mask = debug;
13650
13651 /* do a special CORER for clearing PXE mode once at init */
13652 if (hw->revision_id == 0 &&
13653 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
13654 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
13655 i40e_flush(hw);
13656 msleep(200);
13657 pf->corer_count++;
13658
13659 i40e_clear_pxe_mode(hw);
13660 }
13661
13662 /* Reset here to make sure all is clean and to define PF 'n' */
13663 i40e_clear_hw(hw);
13664 err = i40e_pf_reset(hw);
13665 if (err) {
13666 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
13667 goto err_pf_reset;
13668 }
13669 pf->pfr_count++;
13670
13671 hw->aq.num_arq_entries = I40E_AQ_LEN;
13672 hw->aq.num_asq_entries = I40E_AQ_LEN;
13673 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
13674 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
13675 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
13676
13677 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
13678 "%s-%s:misc",
13679 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
13680
13681 err = i40e_init_shared_code(hw);
13682 if (err) {
13683 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
13684 err);
13685 goto err_pf_reset;
13686 }
13687
13688 /* set up a default setting for link flow control */
13689 pf->hw.fc.requested_mode = I40E_FC_NONE;
13690
13691 err = i40e_init_adminq(hw);
13692 if (err) {
13693 if (err == I40E_ERR_FIRMWARE_API_VERSION)
13694 dev_info(&pdev->dev,
13695 "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");
13696 else
13697 dev_info(&pdev->dev,
13698 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
13699
13700 goto err_pf_reset;
13701 }
13702 i40e_get_oem_version(hw);
13703
13704 /* provide nvm, fw, api versions */
13705 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
13706 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
13707 hw->aq.api_maj_ver, hw->aq.api_min_ver,
13708 i40e_nvm_version_str(hw));
13709
13710 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
13711 hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
13712 dev_info(&pdev->dev,
13713 "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");
13714 else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
13715 dev_info(&pdev->dev,
13716 "The driver for the device detected an older version of the NVM image than expected. Please update the NVM image.\n");
13717
13718 i40e_verify_eeprom(pf);
13719
13720 /* Rev 0 hardware was never productized */
13721 if (hw->revision_id < 1)
13722 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");
13723
13724 i40e_clear_pxe_mode(hw);
13725 err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
13726 if (err)
13727 goto err_adminq_setup;
13728
13729 err = i40e_sw_init(pf);
13730 if (err) {
13731 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
13732 goto err_sw_init;
13733 }
13734
13735 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
13736 hw->func_caps.num_rx_qp, 0, 0);
13737 if (err) {
13738 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
13739 goto err_init_lan_hmc;
13740 }
13741
13742 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
13743 if (err) {
13744 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
13745 err = -ENOENT;
13746 goto err_configure_lan_hmc;
13747 }
13748
13749 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
13750 * Ignore error return codes because if it was already disabled via
13751 * hardware settings this will fail
13752 */
13753 if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
13754 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
13755 i40e_aq_stop_lldp(hw, true, NULL);
13756 }
13757
13758 /* allow a platform config to override the HW addr */
13759 i40e_get_platform_mac_addr(pdev, pf);
13760
13761 if (!is_valid_ether_addr(hw->mac.addr)) {
13762 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
13763 err = -EIO;
13764 goto err_mac_addr;
13765 }
13766 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
13767 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
13768 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
13769 if (is_valid_ether_addr(hw->mac.port_addr))
13770 pf->hw_features |= I40E_HW_PORT_ID_VALID;
13771
13772 pci_set_drvdata(pdev, pf);
13773 pci_save_state(pdev);
13774
13775 /* Enable FW to write default DCB config on link-up */
13776 i40e_aq_set_dcb_parameters(hw, true, NULL);
13777
13778 #ifdef CONFIG_I40E_DCB
13779 err = i40e_init_pf_dcb(pf);
13780 if (err) {
13781 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
13782 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
13783 /* Continue without DCB enabled */
13784 }
13785 #endif /* CONFIG_I40E_DCB */
13786
13787 /* set up periodic task facility */
13788 timer_setup(&pf->service_timer, i40e_service_timer, 0);
13789 pf->service_timer_period = HZ;
13790
13791 INIT_WORK(&pf->service_task, i40e_service_task);
13792 clear_bit(__I40E_SERVICE_SCHED, pf->state);
13793
13794 /* NVM bit on means WoL disabled for the port */
13795 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
13796 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
13797 pf->wol_en = false;
13798 else
13799 pf->wol_en = true;
13800 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
13801
13802 /* set up the main switch operations */
13803 i40e_determine_queue_usage(pf);
13804 err = i40e_init_interrupt_scheme(pf);
13805 if (err)
13806 goto err_switch_setup;
13807
13808 /* The number of VSIs reported by the FW is the minimum guaranteed
13809 * to us; HW supports far more and we share the remaining pool with
13810 * the other PFs. We allocate space for more than the guarantee with
13811 * the understanding that we might not get them all later.
13812 */
13813 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
13814 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
13815 else
13816 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
13817
13818 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
13819 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
13820 GFP_KERNEL);
13821 if (!pf->vsi) {
13822 err = -ENOMEM;
13823 goto err_switch_setup;
13824 }
13825
13826 #ifdef CONFIG_PCI_IOV
13827 /* prep for VF support */
13828 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
13829 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
13830 !test_bit(__I40E_BAD_EEPROM, pf->state)) {
13831 if (pci_num_vf(pdev))
13832 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
13833 }
13834 #endif
13835 err = i40e_setup_pf_switch(pf, false);
13836 if (err) {
13837 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
13838 goto err_vsis;
13839 }
13840 INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
13841
13842 /* Make sure flow control is set according to current settings */
13843 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
13844 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
13845 dev_dbg(&pf->pdev->dev,
13846 "Set fc with err %s aq_err %s on get_phy_cap\n",
13847 i40e_stat_str(hw, err),
13848 i40e_aq_str(hw, hw->aq.asq_last_status));
13849 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
13850 dev_dbg(&pf->pdev->dev,
13851 "Set fc with err %s aq_err %s on set_phy_config\n",
13852 i40e_stat_str(hw, err),
13853 i40e_aq_str(hw, hw->aq.asq_last_status));
13854 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
13855 dev_dbg(&pf->pdev->dev,
13856 "Set fc with err %s aq_err %s on get_link_info\n",
13857 i40e_stat_str(hw, err),
13858 i40e_aq_str(hw, hw->aq.asq_last_status));
13859
13860 /* if FDIR VSI was set up, start it now */
13861 for (i = 0; i < pf->num_alloc_vsi; i++) {
13862 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
13863 i40e_vsi_open(pf->vsi[i]);
13864 break;
13865 }
13866 }
13867
13868 /* The driver only wants link up/down and module qualification
13869 * reports from firmware. Note the negative logic.
13870 */
13871 err = i40e_aq_set_phy_int_mask(&pf->hw,
13872 ~(I40E_AQ_EVENT_LINK_UPDOWN |
13873 I40E_AQ_EVENT_MEDIA_NA |
13874 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
13875 if (err)
13876 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
13877 i40e_stat_str(&pf->hw, err),
13878 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13879
13880 /* Reconfigure hardware for allowing smaller MSS in the case
13881 * of TSO, so that we avoid the MDD being fired and causing
13882 * a reset in the case of small MSS+TSO.
13883 */
13884 val = rd32(hw, I40E_REG_MSS);
13885 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
13886 val &= ~I40E_REG_MSS_MIN_MASK;
13887 val |= I40E_64BYTE_MSS;
13888 wr32(hw, I40E_REG_MSS, val);
13889 }
13890
13891 if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
13892 msleep(75);
13893 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
13894 if (err)
13895 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
13896 i40e_stat_str(&pf->hw, err),
13897 i40e_aq_str(&pf->hw,
13898 pf->hw.aq.asq_last_status));
13899 }
13900 /* The main driver is (mostly) up and happy. We need to set this state
13901 * before setting up the misc vector or we get a race and the vector
13902 * ends up disabled forever.
13903 */
13904 clear_bit(__I40E_DOWN, pf->state);
13905
13906 /* In case of MSIX we are going to setup the misc vector right here
13907 * to handle admin queue events etc. In case of legacy and MSI
13908 * the misc functionality and queue processing is combined in
13909 * the same vector and that gets setup at open.
13910 */
13911 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13912 err = i40e_setup_misc_vector(pf);
13913 if (err) {
13914 dev_info(&pdev->dev,
13915 "setup of misc vector failed: %d\n", err);
13916 goto err_vsis;
13917 }
13918 }
13919
13920 #ifdef CONFIG_PCI_IOV
13921 /* prep for VF support */
13922 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
13923 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
13924 !test_bit(__I40E_BAD_EEPROM, pf->state)) {
13925 /* disable link interrupts for VFs */
13926 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
13927 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
13928 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
13929 i40e_flush(hw);
13930
13931 if (pci_num_vf(pdev)) {
13932 dev_info(&pdev->dev,
13933 "Active VFs found, allocating resources.\n");
13934 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
13935 if (err)
13936 dev_info(&pdev->dev,
13937 "Error %d allocating resources for existing VFs\n",
13938 err);
13939 }
13940 }
13941 #endif /* CONFIG_PCI_IOV */
13942
13943 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
13944 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
13945 pf->num_iwarp_msix,
13946 I40E_IWARP_IRQ_PILE_ID);
13947 if (pf->iwarp_base_vector < 0) {
13948 dev_info(&pdev->dev,
13949 "failed to get tracking for %d vectors for IWARP err=%d\n",
13950 pf->num_iwarp_msix, pf->iwarp_base_vector);
13951 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
13952 }
13953 }
13954
13955 i40e_dbg_pf_init(pf);
13956
13957 /* tell the firmware that we're starting */
13958 i40e_send_version(pf);
13959
13960 /* since everything's happy, start the service_task timer */
13961 mod_timer(&pf->service_timer,
13962 round_jiffies(jiffies + pf->service_timer_period));
13963
13964 /* add this PF to client device list and launch a client service task */
13965 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
13966 err = i40e_lan_add_device(pf);
13967 if (err)
13968 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
13969 err);
13970 }
13971
13972 #define PCI_SPEED_SIZE 8
13973 #define PCI_WIDTH_SIZE 8
13974 /* Devices on the IOSF bus do not have this information
13975 * and will report PCI Gen 1 x 1 by default so don't bother
13976 * checking them.
13977 */
13978 if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
13979 char speed[PCI_SPEED_SIZE] = "Unknown";
13980 char width[PCI_WIDTH_SIZE] = "Unknown";
13981
13982 /* Get the negotiated link width and speed from PCI config
13983 * space
13984 */
13985 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
13986 &link_status);
13987
13988 i40e_set_pci_config_data(hw, link_status);
13989
13990 switch (hw->bus.speed) {
13991 case i40e_bus_speed_8000:
13992 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
13993 case i40e_bus_speed_5000:
13994 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
13995 case i40e_bus_speed_2500:
13996 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
13997 default:
13998 break;
13999 }
14000 switch (hw->bus.width) {
14001 case i40e_bus_width_pcie_x8:
14002 strncpy(width, "8", PCI_WIDTH_SIZE); break;
14003 case i40e_bus_width_pcie_x4:
14004 strncpy(width, "4", PCI_WIDTH_SIZE); break;
14005 case i40e_bus_width_pcie_x2:
14006 strncpy(width, "2", PCI_WIDTH_SIZE); break;
14007 case i40e_bus_width_pcie_x1:
14008 strncpy(width, "1", PCI_WIDTH_SIZE); break;
14009 default:
14010 break;
14011 }
14012
14013 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
14014 speed, width);
14015
14016 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
14017 hw->bus.speed < i40e_bus_speed_8000) {
14018 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
14019 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
14020 }
14021 }
14022
14023 /* get the requested speeds from the fw */
14024 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
14025 if (err)
14026 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
14027 i40e_stat_str(&pf->hw, err),
14028 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14029 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
14030
14031 /* get the supported phy types from the fw */
14032 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
14033 if (err)
14034 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
14035 i40e_stat_str(&pf->hw, err),
14036 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14037
14038 /* Add a filter to drop all Flow control frames from any VSI from being
14039 * transmitted. By doing so we stop a malicious VF from sending out
14040 * PAUSE or PFC frames and potentially controlling traffic for other
14041 * PF/VF VSIs.
14042 * The FW can still send Flow control frames if enabled.
14043 */
14044 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
14045 pf->main_vsi_seid);
14046
14047 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
14048 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
14049 pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
14050 if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
14051 pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
14052 /* print a string summarizing features */
14053 i40e_print_features(pf);
14054
14055 return 0;
14056
14057 /* Unwind what we've done if something failed in the setup */
14058 err_vsis:
14059 set_bit(__I40E_DOWN, pf->state);
14060 i40e_clear_interrupt_scheme(pf);
14061 kfree(pf->vsi);
14062 err_switch_setup:
14063 i40e_reset_interrupt_capability(pf);
14064 del_timer_sync(&pf->service_timer);
14065 err_mac_addr:
14066 err_configure_lan_hmc:
14067 (void)i40e_shutdown_lan_hmc(hw);
14068 err_init_lan_hmc:
14069 kfree(pf->qp_pile);
14070 err_sw_init:
14071 err_adminq_setup:
14072 err_pf_reset:
14073 iounmap(hw->hw_addr);
14074 err_ioremap:
14075 kfree(pf);
14076 err_pf_alloc:
14077 pci_disable_pcie_error_reporting(pdev);
14078 pci_release_mem_regions(pdev);
14079 err_pci_reg:
14080 err_dma:
14081 pci_disable_device(pdev);
14082 return err;
14083 }
14084
14085 /**
14086 * i40e_remove - Device removal routine
14087 * @pdev: PCI device information struct
14088 *
14089 * i40e_remove is called by the PCI subsystem to alert the driver
14090 * that is should release a PCI device. This could be caused by a
14091 * Hot-Plug event, or because the driver is going to be removed from
14092 * memory.
14093 **/
14094 static void i40e_remove(struct pci_dev *pdev)
14095 {
14096 struct i40e_pf *pf = pci_get_drvdata(pdev);
14097 struct i40e_hw *hw = &pf->hw;
14098 i40e_status ret_code;
14099 int i;
14100
14101 i40e_dbg_pf_exit(pf);
14102
14103 i40e_ptp_stop(pf);
14104
14105 /* Disable RSS in hw */
14106 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
14107 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
14108
14109 /* no more scheduling of any task */
14110 set_bit(__I40E_SUSPENDED, pf->state);
14111 set_bit(__I40E_DOWN, pf->state);
14112 if (pf->service_timer.function)
14113 del_timer_sync(&pf->service_timer);
14114 if (pf->service_task.func)
14115 cancel_work_sync(&pf->service_task);
14116
14117 /* Client close must be called explicitly here because the timer
14118 * has been stopped.
14119 */
14120 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
14121
14122 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
14123 i40e_free_vfs(pf);
14124 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
14125 }
14126
14127 i40e_fdir_teardown(pf);
14128
14129 /* If there is a switch structure or any orphans, remove them.
14130 * This will leave only the PF's VSI remaining.
14131 */
14132 for (i = 0; i < I40E_MAX_VEB; i++) {
14133 if (!pf->veb[i])
14134 continue;
14135
14136 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
14137 pf->veb[i]->uplink_seid == 0)
14138 i40e_switch_branch_release(pf->veb[i]);
14139 }
14140
14141 /* Now we can shutdown the PF's VSI, just before we kill
14142 * adminq and hmc.
14143 */
14144 if (pf->vsi[pf->lan_vsi])
14145 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
14146
14147 i40e_cloud_filter_exit(pf);
14148
14149 /* remove attached clients */
14150 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
14151 ret_code = i40e_lan_del_device(pf);
14152 if (ret_code)
14153 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
14154 ret_code);
14155 }
14156
14157 /* shutdown and destroy the HMC */
14158 if (hw->hmc.hmc_obj) {
14159 ret_code = i40e_shutdown_lan_hmc(hw);
14160 if (ret_code)
14161 dev_warn(&pdev->dev,
14162 "Failed to destroy the HMC resources: %d\n",
14163 ret_code);
14164 }
14165
14166 /* shutdown the adminq */
14167 i40e_shutdown_adminq(hw);
14168
14169 /* destroy the locks only once, here */
14170 mutex_destroy(&hw->aq.arq_mutex);
14171 mutex_destroy(&hw->aq.asq_mutex);
14172
14173 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
14174 i40e_clear_interrupt_scheme(pf);
14175 for (i = 0; i < pf->num_alloc_vsi; i++) {
14176 if (pf->vsi[i]) {
14177 i40e_vsi_clear_rings(pf->vsi[i]);
14178 i40e_vsi_clear(pf->vsi[i]);
14179 pf->vsi[i] = NULL;
14180 }
14181 }
14182
14183 for (i = 0; i < I40E_MAX_VEB; i++) {
14184 kfree(pf->veb[i]);
14185 pf->veb[i] = NULL;
14186 }
14187
14188 kfree(pf->qp_pile);
14189 kfree(pf->vsi);
14190
14191 iounmap(hw->hw_addr);
14192 kfree(pf);
14193 pci_release_mem_regions(pdev);
14194
14195 pci_disable_pcie_error_reporting(pdev);
14196 pci_disable_device(pdev);
14197 }
14198
14199 /**
14200 * i40e_pci_error_detected - warning that something funky happened in PCI land
14201 * @pdev: PCI device information struct
14202 * @error: the type of PCI error
14203 *
14204 * Called to warn that something happened and the error handling steps
14205 * are in progress. Allows the driver to quiesce things, be ready for
14206 * remediation.
14207 **/
14208 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
14209 enum pci_channel_state error)
14210 {
14211 struct i40e_pf *pf = pci_get_drvdata(pdev);
14212
14213 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
14214
14215 if (!pf) {
14216 dev_info(&pdev->dev,
14217 "Cannot recover - error happened during device probe\n");
14218 return PCI_ERS_RESULT_DISCONNECT;
14219 }
14220
14221 /* shutdown all operations */
14222 if (!test_bit(__I40E_SUSPENDED, pf->state))
14223 i40e_prep_for_reset(pf, false);
14224
14225 /* Request a slot reset */
14226 return PCI_ERS_RESULT_NEED_RESET;
14227 }
14228
14229 /**
14230 * i40e_pci_error_slot_reset - a PCI slot reset just happened
14231 * @pdev: PCI device information struct
14232 *
14233 * Called to find if the driver can work with the device now that
14234 * the pci slot has been reset. If a basic connection seems good
14235 * (registers are readable and have sane content) then return a
14236 * happy little PCI_ERS_RESULT_xxx.
14237 **/
14238 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
14239 {
14240 struct i40e_pf *pf = pci_get_drvdata(pdev);
14241 pci_ers_result_t result;
14242 int err;
14243 u32 reg;
14244
14245 dev_dbg(&pdev->dev, "%s\n", __func__);
14246 if (pci_enable_device_mem(pdev)) {
14247 dev_info(&pdev->dev,
14248 "Cannot re-enable PCI device after reset.\n");
14249 result = PCI_ERS_RESULT_DISCONNECT;
14250 } else {
14251 pci_set_master(pdev);
14252 pci_restore_state(pdev);
14253 pci_save_state(pdev);
14254 pci_wake_from_d3(pdev, false);
14255
14256 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
14257 if (reg == 0)
14258 result = PCI_ERS_RESULT_RECOVERED;
14259 else
14260 result = PCI_ERS_RESULT_DISCONNECT;
14261 }
14262
14263 err = pci_cleanup_aer_uncorrect_error_status(pdev);
14264 if (err) {
14265 dev_info(&pdev->dev,
14266 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
14267 err);
14268 /* non-fatal, continue */
14269 }
14270
14271 return result;
14272 }
14273
14274 /**
14275 * i40e_pci_error_reset_prepare - prepare device driver for pci reset
14276 * @pdev: PCI device information struct
14277 */
14278 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
14279 {
14280 struct i40e_pf *pf = pci_get_drvdata(pdev);
14281
14282 i40e_prep_for_reset(pf, false);
14283 }
14284
14285 /**
14286 * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
14287 * @pdev: PCI device information struct
14288 */
14289 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
14290 {
14291 struct i40e_pf *pf = pci_get_drvdata(pdev);
14292
14293 i40e_reset_and_rebuild(pf, false, false);
14294 }
14295
14296 /**
14297 * i40e_pci_error_resume - restart operations after PCI error recovery
14298 * @pdev: PCI device information struct
14299 *
14300 * Called to allow the driver to bring things back up after PCI error
14301 * and/or reset recovery has finished.
14302 **/
14303 static void i40e_pci_error_resume(struct pci_dev *pdev)
14304 {
14305 struct i40e_pf *pf = pci_get_drvdata(pdev);
14306
14307 dev_dbg(&pdev->dev, "%s\n", __func__);
14308 if (test_bit(__I40E_SUSPENDED, pf->state))
14309 return;
14310
14311 i40e_handle_reset_warning(pf, false);
14312 }
14313
14314 /**
14315 * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
14316 * using the mac_address_write admin q function
14317 * @pf: pointer to i40e_pf struct
14318 **/
14319 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
14320 {
14321 struct i40e_hw *hw = &pf->hw;
14322 i40e_status ret;
14323 u8 mac_addr[6];
14324 u16 flags = 0;
14325
14326 /* Get current MAC address in case it's an LAA */
14327 if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
14328 ether_addr_copy(mac_addr,
14329 pf->vsi[pf->lan_vsi]->netdev->dev_addr);
14330 } else {
14331 dev_err(&pf->pdev->dev,
14332 "Failed to retrieve MAC address; using default\n");
14333 ether_addr_copy(mac_addr, hw->mac.addr);
14334 }
14335
14336 /* The FW expects the mac address write cmd to first be called with
14337 * one of these flags before calling it again with the multicast
14338 * enable flags.
14339 */
14340 flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
14341
14342 if (hw->func_caps.flex10_enable && hw->partition_id != 1)
14343 flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
14344
14345 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
14346 if (ret) {
14347 dev_err(&pf->pdev->dev,
14348 "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
14349 return;
14350 }
14351
14352 flags = I40E_AQC_MC_MAG_EN
14353 | I40E_AQC_WOL_PRESERVE_ON_PFR
14354 | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
14355 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
14356 if (ret)
14357 dev_err(&pf->pdev->dev,
14358 "Failed to enable Multicast Magic Packet wake up\n");
14359 }
14360
14361 /**
14362 * i40e_shutdown - PCI callback for shutting down
14363 * @pdev: PCI device information struct
14364 **/
14365 static void i40e_shutdown(struct pci_dev *pdev)
14366 {
14367 struct i40e_pf *pf = pci_get_drvdata(pdev);
14368 struct i40e_hw *hw = &pf->hw;
14369
14370 set_bit(__I40E_SUSPENDED, pf->state);
14371 set_bit(__I40E_DOWN, pf->state);
14372
14373 del_timer_sync(&pf->service_timer);
14374 cancel_work_sync(&pf->service_task);
14375 i40e_cloud_filter_exit(pf);
14376 i40e_fdir_teardown(pf);
14377
14378 /* Client close must be called explicitly here because the timer
14379 * has been stopped.
14380 */
14381 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
14382
14383 if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
14384 i40e_enable_mc_magic_wake(pf);
14385
14386 i40e_prep_for_reset(pf, false);
14387
14388 wr32(hw, I40E_PFPM_APM,
14389 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
14390 wr32(hw, I40E_PFPM_WUFC,
14391 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
14392
14393 i40e_clear_interrupt_scheme(pf);
14394
14395 if (system_state == SYSTEM_POWER_OFF) {
14396 pci_wake_from_d3(pdev, pf->wol_en);
14397 pci_set_power_state(pdev, PCI_D3hot);
14398 }
14399 }
14400
14401 /**
14402 * i40e_suspend - PM callback for moving to D3
14403 * @dev: generic device information structure
14404 **/
14405 static int __maybe_unused i40e_suspend(struct device *dev)
14406 {
14407 struct pci_dev *pdev = to_pci_dev(dev);
14408 struct i40e_pf *pf = pci_get_drvdata(pdev);
14409 struct i40e_hw *hw = &pf->hw;
14410
14411 /* If we're already suspended, then there is nothing to do */
14412 if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
14413 return 0;
14414
14415 set_bit(__I40E_DOWN, pf->state);
14416
14417 /* Ensure service task will not be running */
14418 del_timer_sync(&pf->service_timer);
14419 cancel_work_sync(&pf->service_task);
14420
14421 /* Client close must be called explicitly here because the timer
14422 * has been stopped.
14423 */
14424 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
14425
14426 if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
14427 i40e_enable_mc_magic_wake(pf);
14428
14429 /* Since we're going to destroy queues during the
14430 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
14431 * whole section
14432 */
14433 rtnl_lock();
14434
14435 i40e_prep_for_reset(pf, true);
14436
14437 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
14438 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
14439
14440 /* Clear the interrupt scheme and release our IRQs so that the system
14441 * can safely hibernate even when there are a large number of CPUs.
14442 * Otherwise hibernation might fail when mapping all the vectors back
14443 * to CPU0.
14444 */
14445 i40e_clear_interrupt_scheme(pf);
14446
14447 rtnl_unlock();
14448
14449 return 0;
14450 }
14451
14452 /**
14453 * i40e_resume - PM callback for waking up from D3
14454 * @dev: generic device information structure
14455 **/
14456 static int __maybe_unused i40e_resume(struct device *dev)
14457 {
14458 struct pci_dev *pdev = to_pci_dev(dev);
14459 struct i40e_pf *pf = pci_get_drvdata(pdev);
14460 int err;
14461
14462 /* If we're not suspended, then there is nothing to do */
14463 if (!test_bit(__I40E_SUSPENDED, pf->state))
14464 return 0;
14465
14466 /* We need to hold the RTNL lock prior to restoring interrupt schemes,
14467 * since we're going to be restoring queues
14468 */
14469 rtnl_lock();
14470
14471 /* We cleared the interrupt scheme when we suspended, so we need to
14472 * restore it now to resume device functionality.
14473 */
14474 err = i40e_restore_interrupt_scheme(pf);
14475 if (err) {
14476 dev_err(&pdev->dev, "Cannot restore interrupt scheme: %d\n",
14477 err);
14478 }
14479
14480 clear_bit(__I40E_DOWN, pf->state);
14481 i40e_reset_and_rebuild(pf, false, true);
14482
14483 rtnl_unlock();
14484
14485 /* Clear suspended state last after everything is recovered */
14486 clear_bit(__I40E_SUSPENDED, pf->state);
14487
14488 /* Restart the service task */
14489 mod_timer(&pf->service_timer,
14490 round_jiffies(jiffies + pf->service_timer_period));
14491
14492 return 0;
14493 }
14494
14495 static const struct pci_error_handlers i40e_err_handler = {
14496 .error_detected = i40e_pci_error_detected,
14497 .slot_reset = i40e_pci_error_slot_reset,
14498 .reset_prepare = i40e_pci_error_reset_prepare,
14499 .reset_done = i40e_pci_error_reset_done,
14500 .resume = i40e_pci_error_resume,
14501 };
14502
14503 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
14504
14505 static struct pci_driver i40e_driver = {
14506 .name = i40e_driver_name,
14507 .id_table = i40e_pci_tbl,
14508 .probe = i40e_probe,
14509 .remove = i40e_remove,
14510 .driver = {
14511 .pm = &i40e_pm_ops,
14512 },
14513 .shutdown = i40e_shutdown,
14514 .err_handler = &i40e_err_handler,
14515 .sriov_configure = i40e_pci_sriov_configure,
14516 };
14517
14518 /**
14519 * i40e_init_module - Driver registration routine
14520 *
14521 * i40e_init_module is the first routine called when the driver is
14522 * loaded. All it does is register with the PCI subsystem.
14523 **/
14524 static int __init i40e_init_module(void)
14525 {
14526 pr_info("%s: %s - version %s\n", i40e_driver_name,
14527 i40e_driver_string, i40e_driver_version_str);
14528 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
14529
14530 /* There is no need to throttle the number of active tasks because
14531 * each device limits its own task using a state bit for scheduling
14532 * the service task, and the device tasks do not interfere with each
14533 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
14534 * since we need to be able to guarantee forward progress even under
14535 * memory pressure.
14536 */
14537 i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
14538 if (!i40e_wq) {
14539 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
14540 return -ENOMEM;
14541 }
14542
14543 i40e_dbg_init();
14544 return pci_register_driver(&i40e_driver);
14545 }
14546 module_init(i40e_init_module);
14547
14548 /**
14549 * i40e_exit_module - Driver exit cleanup routine
14550 *
14551 * i40e_exit_module is called just before the driver is removed
14552 * from memory.
14553 **/
14554 static void __exit i40e_exit_module(void)
14555 {
14556 pci_unregister_driver(&i40e_driver);
14557 destroy_workqueue(i40e_wq);
14558 i40e_dbg_exit();
14559 }
14560 module_exit(i40e_exit_module);