]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/net/ethernet/intel/i40e/i40e_main.c
08f7bd9001bcf51bfa0d5309d0567ec71eb8977c
[mirror_ubuntu-bionic-kernel.git] / drivers / net / ethernet / intel / i40e / i40e_main.c
1 /*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
4 * Copyright(c) 2013 - 2017 Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 *
21 * Contact Information:
22 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 ******************************************************************************/
26
27 #include <linux/etherdevice.h>
28 #include <linux/of_net.h>
29 #include <linux/pci.h>
30 #include <linux/bpf.h>
31
32 /* Local includes */
33 #include "i40e.h"
34 #include "i40e_diag.h"
35 #include <net/udp_tunnel.h>
36 /* All i40e tracepoints are defined by the include below, which
37 * must be included exactly once across the whole kernel with
38 * CREATE_TRACE_POINTS defined
39 */
40 #define CREATE_TRACE_POINTS
41 #include "i40e_trace.h"
42
43 const char i40e_driver_name[] = "i40e";
44 static const char i40e_driver_string[] =
45 "Intel(R) Ethernet Connection XL710 Network Driver";
46
47 #define DRV_KERN "-k"
48
49 #define DRV_VERSION_MAJOR 2
50 #define DRV_VERSION_MINOR 1
51 #define DRV_VERSION_BUILD 14
52 #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
53 __stringify(DRV_VERSION_MINOR) "." \
54 __stringify(DRV_VERSION_BUILD) DRV_KERN
55 const char i40e_driver_version_str[] = DRV_VERSION;
56 static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
57
58 /* a bit of forward declarations */
59 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
60 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
61 static int i40e_add_vsi(struct i40e_vsi *vsi);
62 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
63 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
64 static int i40e_setup_misc_vector(struct i40e_pf *pf);
65 static void i40e_determine_queue_usage(struct i40e_pf *pf);
66 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
67 static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired);
68 static int i40e_reset(struct i40e_pf *pf);
69 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
70 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
71 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
72 static int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
73 struct i40e_cloud_filter *filter,
74 bool add);
75 static int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
76 struct i40e_cloud_filter *filter,
77 bool add);
78 static int i40e_get_capabilities(struct i40e_pf *pf,
79 enum i40e_admin_queue_opc list_type);
80
81
82 /* i40e_pci_tbl - PCI Device ID Table
83 *
84 * Last entry must be all 0s
85 *
86 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
87 * Class, Class Mask, private data (not used) }
88 */
89 static const struct pci_device_id i40e_pci_tbl[] = {
90 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
91 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
92 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
93 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
94 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
95 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
96 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
97 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
98 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
99 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
100 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
101 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
102 {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
103 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
104 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
105 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
106 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
107 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
108 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
109 /* required last entry */
110 {0, }
111 };
112 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
113
114 #define I40E_MAX_VF_COUNT 128
115 static int debug = -1;
116 module_param(debug, uint, 0);
117 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
118
119 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
120 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
121 MODULE_LICENSE("GPL");
122 MODULE_VERSION(DRV_VERSION);
123
124 static struct workqueue_struct *i40e_wq;
125
126 /**
127 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
128 * @hw: pointer to the HW structure
129 * @mem: ptr to mem struct to fill out
130 * @size: size of memory requested
131 * @alignment: what to align the allocation to
132 **/
133 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
134 u64 size, u32 alignment)
135 {
136 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
137
138 mem->size = ALIGN(size, alignment);
139 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
140 &mem->pa, GFP_KERNEL);
141 if (!mem->va)
142 return -ENOMEM;
143
144 return 0;
145 }
146
147 /**
148 * i40e_free_dma_mem_d - OS specific memory free for shared code
149 * @hw: pointer to the HW structure
150 * @mem: ptr to mem struct to free
151 **/
152 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
153 {
154 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
155
156 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
157 mem->va = NULL;
158 mem->pa = 0;
159 mem->size = 0;
160
161 return 0;
162 }
163
164 /**
165 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
166 * @hw: pointer to the HW structure
167 * @mem: ptr to mem struct to fill out
168 * @size: size of memory requested
169 **/
170 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
171 u32 size)
172 {
173 mem->size = size;
174 mem->va = kzalloc(size, GFP_KERNEL);
175
176 if (!mem->va)
177 return -ENOMEM;
178
179 return 0;
180 }
181
182 /**
183 * i40e_free_virt_mem_d - OS specific memory free for shared code
184 * @hw: pointer to the HW structure
185 * @mem: ptr to mem struct to free
186 **/
187 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
188 {
189 /* it's ok to kfree a NULL pointer */
190 kfree(mem->va);
191 mem->va = NULL;
192 mem->size = 0;
193
194 return 0;
195 }
196
197 /**
198 * i40e_get_lump - find a lump of free generic resource
199 * @pf: board private structure
200 * @pile: the pile of resource to search
201 * @needed: the number of items needed
202 * @id: an owner id to stick on the items assigned
203 *
204 * Returns the base item index of the lump, or negative for error
205 *
206 * The search_hint trick and lack of advanced fit-finding only work
207 * because we're highly likely to have all the same size lump requests.
208 * Linear search time and any fragmentation should be minimal.
209 **/
210 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
211 u16 needed, u16 id)
212 {
213 int ret = -ENOMEM;
214 int i, j;
215
216 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
217 dev_info(&pf->pdev->dev,
218 "param err: pile=%p needed=%d id=0x%04x\n",
219 pile, needed, id);
220 return -EINVAL;
221 }
222
223 /* start the linear search with an imperfect hint */
224 i = pile->search_hint;
225 while (i < pile->num_entries) {
226 /* skip already allocated entries */
227 if (pile->list[i] & I40E_PILE_VALID_BIT) {
228 i++;
229 continue;
230 }
231
232 /* do we have enough in this lump? */
233 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
234 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
235 break;
236 }
237
238 if (j == needed) {
239 /* there was enough, so assign it to the requestor */
240 for (j = 0; j < needed; j++)
241 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
242 ret = i;
243 pile->search_hint = i + j;
244 break;
245 }
246
247 /* not enough, so skip over it and continue looking */
248 i += j;
249 }
250
251 return ret;
252 }
253
254 /**
255 * i40e_put_lump - return a lump of generic resource
256 * @pile: the pile of resource to search
257 * @index: the base item index
258 * @id: the owner id of the items assigned
259 *
260 * Returns the count of items in the lump
261 **/
262 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
263 {
264 int valid_id = (id | I40E_PILE_VALID_BIT);
265 int count = 0;
266 int i;
267
268 if (!pile || index >= pile->num_entries)
269 return -EINVAL;
270
271 for (i = index;
272 i < pile->num_entries && pile->list[i] == valid_id;
273 i++) {
274 pile->list[i] = 0;
275 count++;
276 }
277
278 if (count && index < pile->search_hint)
279 pile->search_hint = index;
280
281 return count;
282 }
283
284 /**
285 * i40e_find_vsi_from_id - searches for the vsi with the given id
286 * @pf - the pf structure to search for the vsi
287 * @id - id of the vsi it is searching for
288 **/
289 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
290 {
291 int i;
292
293 for (i = 0; i < pf->num_alloc_vsi; i++)
294 if (pf->vsi[i] && (pf->vsi[i]->id == id))
295 return pf->vsi[i];
296
297 return NULL;
298 }
299
300 /**
301 * i40e_service_event_schedule - Schedule the service task to wake up
302 * @pf: board private structure
303 *
304 * If not already scheduled, this puts the task into the work queue
305 **/
306 void i40e_service_event_schedule(struct i40e_pf *pf)
307 {
308 if (!test_bit(__I40E_DOWN, pf->state) &&
309 !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
310 queue_work(i40e_wq, &pf->service_task);
311 }
312
313 /**
314 * i40e_tx_timeout - Respond to a Tx Hang
315 * @netdev: network interface device structure
316 *
317 * If any port has noticed a Tx timeout, it is likely that the whole
318 * device is munged, not just the one netdev port, so go for the full
319 * reset.
320 **/
321 static void i40e_tx_timeout(struct net_device *netdev)
322 {
323 struct i40e_netdev_priv *np = netdev_priv(netdev);
324 struct i40e_vsi *vsi = np->vsi;
325 struct i40e_pf *pf = vsi->back;
326 struct i40e_ring *tx_ring = NULL;
327 unsigned int i, hung_queue = 0;
328 u32 head, val;
329
330 pf->tx_timeout_count++;
331
332 /* find the stopped queue the same way the stack does */
333 for (i = 0; i < netdev->num_tx_queues; i++) {
334 struct netdev_queue *q;
335 unsigned long trans_start;
336
337 q = netdev_get_tx_queue(netdev, i);
338 trans_start = q->trans_start;
339 if (netif_xmit_stopped(q) &&
340 time_after(jiffies,
341 (trans_start + netdev->watchdog_timeo))) {
342 hung_queue = i;
343 break;
344 }
345 }
346
347 if (i == netdev->num_tx_queues) {
348 netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
349 } else {
350 /* now that we have an index, find the tx_ring struct */
351 for (i = 0; i < vsi->num_queue_pairs; i++) {
352 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
353 if (hung_queue ==
354 vsi->tx_rings[i]->queue_index) {
355 tx_ring = vsi->tx_rings[i];
356 break;
357 }
358 }
359 }
360 }
361
362 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
363 pf->tx_timeout_recovery_level = 1; /* reset after some time */
364 else if (time_before(jiffies,
365 (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
366 return; /* don't do any new action before the next timeout */
367
368 if (tx_ring) {
369 head = i40e_get_head(tx_ring);
370 /* Read interrupt register */
371 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
372 val = rd32(&pf->hw,
373 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
374 tx_ring->vsi->base_vector - 1));
375 else
376 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
377
378 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",
379 vsi->seid, hung_queue, tx_ring->next_to_clean,
380 head, tx_ring->next_to_use,
381 readl(tx_ring->tail), val);
382 }
383
384 pf->tx_timeout_last_recovery = jiffies;
385 netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
386 pf->tx_timeout_recovery_level, hung_queue);
387
388 switch (pf->tx_timeout_recovery_level) {
389 case 1:
390 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
391 break;
392 case 2:
393 set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
394 break;
395 case 3:
396 set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
397 break;
398 default:
399 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
400 break;
401 }
402
403 i40e_service_event_schedule(pf);
404 pf->tx_timeout_recovery_level++;
405 }
406
407 /**
408 * i40e_get_vsi_stats_struct - Get System Network Statistics
409 * @vsi: the VSI we care about
410 *
411 * Returns the address of the device statistics structure.
412 * The statistics are actually updated from the service task.
413 **/
414 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
415 {
416 return &vsi->net_stats;
417 }
418
419 /**
420 * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
421 * @ring: Tx ring to get statistics from
422 * @stats: statistics entry to be updated
423 **/
424 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
425 struct rtnl_link_stats64 *stats)
426 {
427 u64 bytes, packets;
428 unsigned int start;
429
430 do {
431 start = u64_stats_fetch_begin_irq(&ring->syncp);
432 packets = ring->stats.packets;
433 bytes = ring->stats.bytes;
434 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
435
436 stats->tx_packets += packets;
437 stats->tx_bytes += bytes;
438 }
439
440 /**
441 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
442 * @netdev: network interface device structure
443 *
444 * Returns the address of the device statistics structure.
445 * The statistics are actually updated from the service task.
446 **/
447 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
448 struct rtnl_link_stats64 *stats)
449 {
450 struct i40e_netdev_priv *np = netdev_priv(netdev);
451 struct i40e_ring *tx_ring, *rx_ring;
452 struct i40e_vsi *vsi = np->vsi;
453 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
454 int i;
455
456 if (test_bit(__I40E_VSI_DOWN, vsi->state))
457 return;
458
459 if (!vsi->tx_rings)
460 return;
461
462 rcu_read_lock();
463 for (i = 0; i < vsi->num_queue_pairs; i++) {
464 u64 bytes, packets;
465 unsigned int start;
466
467 tx_ring = READ_ONCE(vsi->tx_rings[i]);
468 if (!tx_ring)
469 continue;
470 i40e_get_netdev_stats_struct_tx(tx_ring, stats);
471
472 rx_ring = &tx_ring[1];
473
474 do {
475 start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
476 packets = rx_ring->stats.packets;
477 bytes = rx_ring->stats.bytes;
478 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
479
480 stats->rx_packets += packets;
481 stats->rx_bytes += bytes;
482
483 if (i40e_enabled_xdp_vsi(vsi))
484 i40e_get_netdev_stats_struct_tx(&rx_ring[1], stats);
485 }
486 rcu_read_unlock();
487
488 /* following stats updated by i40e_watchdog_subtask() */
489 stats->multicast = vsi_stats->multicast;
490 stats->tx_errors = vsi_stats->tx_errors;
491 stats->tx_dropped = vsi_stats->tx_dropped;
492 stats->rx_errors = vsi_stats->rx_errors;
493 stats->rx_dropped = vsi_stats->rx_dropped;
494 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
495 stats->rx_length_errors = vsi_stats->rx_length_errors;
496 }
497
498 /**
499 * i40e_vsi_reset_stats - Resets all stats of the given vsi
500 * @vsi: the VSI to have its stats reset
501 **/
502 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
503 {
504 struct rtnl_link_stats64 *ns;
505 int i;
506
507 if (!vsi)
508 return;
509
510 ns = i40e_get_vsi_stats_struct(vsi);
511 memset(ns, 0, sizeof(*ns));
512 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
513 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
514 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
515 if (vsi->rx_rings && vsi->rx_rings[0]) {
516 for (i = 0; i < vsi->num_queue_pairs; i++) {
517 memset(&vsi->rx_rings[i]->stats, 0,
518 sizeof(vsi->rx_rings[i]->stats));
519 memset(&vsi->rx_rings[i]->rx_stats, 0,
520 sizeof(vsi->rx_rings[i]->rx_stats));
521 memset(&vsi->tx_rings[i]->stats, 0,
522 sizeof(vsi->tx_rings[i]->stats));
523 memset(&vsi->tx_rings[i]->tx_stats, 0,
524 sizeof(vsi->tx_rings[i]->tx_stats));
525 }
526 }
527 vsi->stat_offsets_loaded = false;
528 }
529
530 /**
531 * i40e_pf_reset_stats - Reset all of the stats for the given PF
532 * @pf: the PF to be reset
533 **/
534 void i40e_pf_reset_stats(struct i40e_pf *pf)
535 {
536 int i;
537
538 memset(&pf->stats, 0, sizeof(pf->stats));
539 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
540 pf->stat_offsets_loaded = false;
541
542 for (i = 0; i < I40E_MAX_VEB; i++) {
543 if (pf->veb[i]) {
544 memset(&pf->veb[i]->stats, 0,
545 sizeof(pf->veb[i]->stats));
546 memset(&pf->veb[i]->stats_offsets, 0,
547 sizeof(pf->veb[i]->stats_offsets));
548 pf->veb[i]->stat_offsets_loaded = false;
549 }
550 }
551 pf->hw_csum_rx_error = 0;
552 }
553
554 /**
555 * i40e_stat_update48 - read and update a 48 bit stat from the chip
556 * @hw: ptr to the hardware info
557 * @hireg: the high 32 bit reg to read
558 * @loreg: the low 32 bit reg to read
559 * @offset_loaded: has the initial offset been loaded yet
560 * @offset: ptr to current offset value
561 * @stat: ptr to the stat
562 *
563 * Since the device stats are not reset at PFReset, they likely will not
564 * be zeroed when the driver starts. We'll save the first values read
565 * and use them as offsets to be subtracted from the raw values in order
566 * to report stats that count from zero. In the process, we also manage
567 * the potential roll-over.
568 **/
569 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
570 bool offset_loaded, u64 *offset, u64 *stat)
571 {
572 u64 new_data;
573
574 if (hw->device_id == I40E_DEV_ID_QEMU) {
575 new_data = rd32(hw, loreg);
576 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
577 } else {
578 new_data = rd64(hw, loreg);
579 }
580 if (!offset_loaded)
581 *offset = new_data;
582 if (likely(new_data >= *offset))
583 *stat = new_data - *offset;
584 else
585 *stat = (new_data + BIT_ULL(48)) - *offset;
586 *stat &= 0xFFFFFFFFFFFFULL;
587 }
588
589 /**
590 * i40e_stat_update32 - read and update a 32 bit stat from the chip
591 * @hw: ptr to the hardware info
592 * @reg: the hw reg to read
593 * @offset_loaded: has the initial offset been loaded yet
594 * @offset: ptr to current offset value
595 * @stat: ptr to the stat
596 **/
597 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
598 bool offset_loaded, u64 *offset, u64 *stat)
599 {
600 u32 new_data;
601
602 new_data = rd32(hw, reg);
603 if (!offset_loaded)
604 *offset = new_data;
605 if (likely(new_data >= *offset))
606 *stat = (u32)(new_data - *offset);
607 else
608 *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
609 }
610
611 /**
612 * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
613 * @hw: ptr to the hardware info
614 * @reg: the hw reg to read and clear
615 * @stat: ptr to the stat
616 **/
617 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
618 {
619 u32 new_data = rd32(hw, reg);
620
621 wr32(hw, reg, 1); /* must write a nonzero value to clear register */
622 *stat += new_data;
623 }
624
625 /**
626 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
627 * @vsi: the VSI to be updated
628 **/
629 void i40e_update_eth_stats(struct i40e_vsi *vsi)
630 {
631 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
632 struct i40e_pf *pf = vsi->back;
633 struct i40e_hw *hw = &pf->hw;
634 struct i40e_eth_stats *oes;
635 struct i40e_eth_stats *es; /* device's eth stats */
636
637 es = &vsi->eth_stats;
638 oes = &vsi->eth_stats_offsets;
639
640 /* Gather up the stats that the hw collects */
641 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
642 vsi->stat_offsets_loaded,
643 &oes->tx_errors, &es->tx_errors);
644 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
645 vsi->stat_offsets_loaded,
646 &oes->rx_discards, &es->rx_discards);
647 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
648 vsi->stat_offsets_loaded,
649 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
650 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
651 vsi->stat_offsets_loaded,
652 &oes->tx_errors, &es->tx_errors);
653
654 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
655 I40E_GLV_GORCL(stat_idx),
656 vsi->stat_offsets_loaded,
657 &oes->rx_bytes, &es->rx_bytes);
658 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
659 I40E_GLV_UPRCL(stat_idx),
660 vsi->stat_offsets_loaded,
661 &oes->rx_unicast, &es->rx_unicast);
662 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
663 I40E_GLV_MPRCL(stat_idx),
664 vsi->stat_offsets_loaded,
665 &oes->rx_multicast, &es->rx_multicast);
666 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
667 I40E_GLV_BPRCL(stat_idx),
668 vsi->stat_offsets_loaded,
669 &oes->rx_broadcast, &es->rx_broadcast);
670
671 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
672 I40E_GLV_GOTCL(stat_idx),
673 vsi->stat_offsets_loaded,
674 &oes->tx_bytes, &es->tx_bytes);
675 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
676 I40E_GLV_UPTCL(stat_idx),
677 vsi->stat_offsets_loaded,
678 &oes->tx_unicast, &es->tx_unicast);
679 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
680 I40E_GLV_MPTCL(stat_idx),
681 vsi->stat_offsets_loaded,
682 &oes->tx_multicast, &es->tx_multicast);
683 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
684 I40E_GLV_BPTCL(stat_idx),
685 vsi->stat_offsets_loaded,
686 &oes->tx_broadcast, &es->tx_broadcast);
687 vsi->stat_offsets_loaded = true;
688 }
689
690 /**
691 * i40e_update_veb_stats - Update Switch component statistics
692 * @veb: the VEB being updated
693 **/
694 static void i40e_update_veb_stats(struct i40e_veb *veb)
695 {
696 struct i40e_pf *pf = veb->pf;
697 struct i40e_hw *hw = &pf->hw;
698 struct i40e_eth_stats *oes;
699 struct i40e_eth_stats *es; /* device's eth stats */
700 struct i40e_veb_tc_stats *veb_oes;
701 struct i40e_veb_tc_stats *veb_es;
702 int i, idx = 0;
703
704 idx = veb->stats_idx;
705 es = &veb->stats;
706 oes = &veb->stats_offsets;
707 veb_es = &veb->tc_stats;
708 veb_oes = &veb->tc_stats_offsets;
709
710 /* Gather up the stats that the hw collects */
711 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
712 veb->stat_offsets_loaded,
713 &oes->tx_discards, &es->tx_discards);
714 if (hw->revision_id > 0)
715 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
716 veb->stat_offsets_loaded,
717 &oes->rx_unknown_protocol,
718 &es->rx_unknown_protocol);
719 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
720 veb->stat_offsets_loaded,
721 &oes->rx_bytes, &es->rx_bytes);
722 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
723 veb->stat_offsets_loaded,
724 &oes->rx_unicast, &es->rx_unicast);
725 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
726 veb->stat_offsets_loaded,
727 &oes->rx_multicast, &es->rx_multicast);
728 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
729 veb->stat_offsets_loaded,
730 &oes->rx_broadcast, &es->rx_broadcast);
731
732 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
733 veb->stat_offsets_loaded,
734 &oes->tx_bytes, &es->tx_bytes);
735 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
736 veb->stat_offsets_loaded,
737 &oes->tx_unicast, &es->tx_unicast);
738 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
739 veb->stat_offsets_loaded,
740 &oes->tx_multicast, &es->tx_multicast);
741 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
742 veb->stat_offsets_loaded,
743 &oes->tx_broadcast, &es->tx_broadcast);
744 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
745 i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
746 I40E_GLVEBTC_RPCL(i, idx),
747 veb->stat_offsets_loaded,
748 &veb_oes->tc_rx_packets[i],
749 &veb_es->tc_rx_packets[i]);
750 i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
751 I40E_GLVEBTC_RBCL(i, idx),
752 veb->stat_offsets_loaded,
753 &veb_oes->tc_rx_bytes[i],
754 &veb_es->tc_rx_bytes[i]);
755 i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
756 I40E_GLVEBTC_TPCL(i, idx),
757 veb->stat_offsets_loaded,
758 &veb_oes->tc_tx_packets[i],
759 &veb_es->tc_tx_packets[i]);
760 i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
761 I40E_GLVEBTC_TBCL(i, idx),
762 veb->stat_offsets_loaded,
763 &veb_oes->tc_tx_bytes[i],
764 &veb_es->tc_tx_bytes[i]);
765 }
766 veb->stat_offsets_loaded = true;
767 }
768
769 /**
770 * i40e_update_vsi_stats - Update the vsi statistics counters.
771 * @vsi: the VSI to be updated
772 *
773 * There are a few instances where we store the same stat in a
774 * couple of different structs. This is partly because we have
775 * the netdev stats that need to be filled out, which is slightly
776 * different from the "eth_stats" defined by the chip and used in
777 * VF communications. We sort it out here.
778 **/
779 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
780 {
781 struct i40e_pf *pf = vsi->back;
782 struct rtnl_link_stats64 *ons;
783 struct rtnl_link_stats64 *ns; /* netdev stats */
784 struct i40e_eth_stats *oes;
785 struct i40e_eth_stats *es; /* device's eth stats */
786 u32 tx_restart, tx_busy;
787 struct i40e_ring *p;
788 u32 rx_page, rx_buf;
789 u64 bytes, packets;
790 unsigned int start;
791 u64 tx_linearize;
792 u64 tx_force_wb;
793 u64 rx_p, rx_b;
794 u64 tx_p, tx_b;
795 u16 q;
796
797 if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
798 test_bit(__I40E_CONFIG_BUSY, pf->state))
799 return;
800
801 ns = i40e_get_vsi_stats_struct(vsi);
802 ons = &vsi->net_stats_offsets;
803 es = &vsi->eth_stats;
804 oes = &vsi->eth_stats_offsets;
805
806 /* Gather up the netdev and vsi stats that the driver collects
807 * on the fly during packet processing
808 */
809 rx_b = rx_p = 0;
810 tx_b = tx_p = 0;
811 tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
812 rx_page = 0;
813 rx_buf = 0;
814 rcu_read_lock();
815 for (q = 0; q < vsi->num_queue_pairs; q++) {
816 /* locate Tx ring */
817 p = READ_ONCE(vsi->tx_rings[q]);
818
819 do {
820 start = u64_stats_fetch_begin_irq(&p->syncp);
821 packets = p->stats.packets;
822 bytes = p->stats.bytes;
823 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
824 tx_b += bytes;
825 tx_p += packets;
826 tx_restart += p->tx_stats.restart_queue;
827 tx_busy += p->tx_stats.tx_busy;
828 tx_linearize += p->tx_stats.tx_linearize;
829 tx_force_wb += p->tx_stats.tx_force_wb;
830
831 /* Rx queue is part of the same block as Tx queue */
832 p = &p[1];
833 do {
834 start = u64_stats_fetch_begin_irq(&p->syncp);
835 packets = p->stats.packets;
836 bytes = p->stats.bytes;
837 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
838 rx_b += bytes;
839 rx_p += packets;
840 rx_buf += p->rx_stats.alloc_buff_failed;
841 rx_page += p->rx_stats.alloc_page_failed;
842 }
843 rcu_read_unlock();
844 vsi->tx_restart = tx_restart;
845 vsi->tx_busy = tx_busy;
846 vsi->tx_linearize = tx_linearize;
847 vsi->tx_force_wb = tx_force_wb;
848 vsi->rx_page_failed = rx_page;
849 vsi->rx_buf_failed = rx_buf;
850
851 ns->rx_packets = rx_p;
852 ns->rx_bytes = rx_b;
853 ns->tx_packets = tx_p;
854 ns->tx_bytes = tx_b;
855
856 /* update netdev stats from eth stats */
857 i40e_update_eth_stats(vsi);
858 ons->tx_errors = oes->tx_errors;
859 ns->tx_errors = es->tx_errors;
860 ons->multicast = oes->rx_multicast;
861 ns->multicast = es->rx_multicast;
862 ons->rx_dropped = oes->rx_discards;
863 ns->rx_dropped = es->rx_discards;
864 ons->tx_dropped = oes->tx_discards;
865 ns->tx_dropped = es->tx_discards;
866
867 /* pull in a couple PF stats if this is the main vsi */
868 if (vsi == pf->vsi[pf->lan_vsi]) {
869 ns->rx_crc_errors = pf->stats.crc_errors;
870 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
871 ns->rx_length_errors = pf->stats.rx_length_errors;
872 }
873 }
874
875 /**
876 * i40e_update_pf_stats - Update the PF statistics counters.
877 * @pf: the PF to be updated
878 **/
879 static void i40e_update_pf_stats(struct i40e_pf *pf)
880 {
881 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
882 struct i40e_hw_port_stats *nsd = &pf->stats;
883 struct i40e_hw *hw = &pf->hw;
884 u32 val;
885 int i;
886
887 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
888 I40E_GLPRT_GORCL(hw->port),
889 pf->stat_offsets_loaded,
890 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
891 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
892 I40E_GLPRT_GOTCL(hw->port),
893 pf->stat_offsets_loaded,
894 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
895 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
896 pf->stat_offsets_loaded,
897 &osd->eth.rx_discards,
898 &nsd->eth.rx_discards);
899 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
900 I40E_GLPRT_UPRCL(hw->port),
901 pf->stat_offsets_loaded,
902 &osd->eth.rx_unicast,
903 &nsd->eth.rx_unicast);
904 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
905 I40E_GLPRT_MPRCL(hw->port),
906 pf->stat_offsets_loaded,
907 &osd->eth.rx_multicast,
908 &nsd->eth.rx_multicast);
909 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
910 I40E_GLPRT_BPRCL(hw->port),
911 pf->stat_offsets_loaded,
912 &osd->eth.rx_broadcast,
913 &nsd->eth.rx_broadcast);
914 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
915 I40E_GLPRT_UPTCL(hw->port),
916 pf->stat_offsets_loaded,
917 &osd->eth.tx_unicast,
918 &nsd->eth.tx_unicast);
919 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
920 I40E_GLPRT_MPTCL(hw->port),
921 pf->stat_offsets_loaded,
922 &osd->eth.tx_multicast,
923 &nsd->eth.tx_multicast);
924 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
925 I40E_GLPRT_BPTCL(hw->port),
926 pf->stat_offsets_loaded,
927 &osd->eth.tx_broadcast,
928 &nsd->eth.tx_broadcast);
929
930 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
931 pf->stat_offsets_loaded,
932 &osd->tx_dropped_link_down,
933 &nsd->tx_dropped_link_down);
934
935 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
936 pf->stat_offsets_loaded,
937 &osd->crc_errors, &nsd->crc_errors);
938
939 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
940 pf->stat_offsets_loaded,
941 &osd->illegal_bytes, &nsd->illegal_bytes);
942
943 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
944 pf->stat_offsets_loaded,
945 &osd->mac_local_faults,
946 &nsd->mac_local_faults);
947 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
948 pf->stat_offsets_loaded,
949 &osd->mac_remote_faults,
950 &nsd->mac_remote_faults);
951
952 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
953 pf->stat_offsets_loaded,
954 &osd->rx_length_errors,
955 &nsd->rx_length_errors);
956
957 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
958 pf->stat_offsets_loaded,
959 &osd->link_xon_rx, &nsd->link_xon_rx);
960 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
961 pf->stat_offsets_loaded,
962 &osd->link_xon_tx, &nsd->link_xon_tx);
963 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
964 pf->stat_offsets_loaded,
965 &osd->link_xoff_rx, &nsd->link_xoff_rx);
966 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
967 pf->stat_offsets_loaded,
968 &osd->link_xoff_tx, &nsd->link_xoff_tx);
969
970 for (i = 0; i < 8; i++) {
971 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
972 pf->stat_offsets_loaded,
973 &osd->priority_xoff_rx[i],
974 &nsd->priority_xoff_rx[i]);
975 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
976 pf->stat_offsets_loaded,
977 &osd->priority_xon_rx[i],
978 &nsd->priority_xon_rx[i]);
979 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
980 pf->stat_offsets_loaded,
981 &osd->priority_xon_tx[i],
982 &nsd->priority_xon_tx[i]);
983 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
984 pf->stat_offsets_loaded,
985 &osd->priority_xoff_tx[i],
986 &nsd->priority_xoff_tx[i]);
987 i40e_stat_update32(hw,
988 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
989 pf->stat_offsets_loaded,
990 &osd->priority_xon_2_xoff[i],
991 &nsd->priority_xon_2_xoff[i]);
992 }
993
994 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
995 I40E_GLPRT_PRC64L(hw->port),
996 pf->stat_offsets_loaded,
997 &osd->rx_size_64, &nsd->rx_size_64);
998 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
999 I40E_GLPRT_PRC127L(hw->port),
1000 pf->stat_offsets_loaded,
1001 &osd->rx_size_127, &nsd->rx_size_127);
1002 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1003 I40E_GLPRT_PRC255L(hw->port),
1004 pf->stat_offsets_loaded,
1005 &osd->rx_size_255, &nsd->rx_size_255);
1006 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1007 I40E_GLPRT_PRC511L(hw->port),
1008 pf->stat_offsets_loaded,
1009 &osd->rx_size_511, &nsd->rx_size_511);
1010 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1011 I40E_GLPRT_PRC1023L(hw->port),
1012 pf->stat_offsets_loaded,
1013 &osd->rx_size_1023, &nsd->rx_size_1023);
1014 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1015 I40E_GLPRT_PRC1522L(hw->port),
1016 pf->stat_offsets_loaded,
1017 &osd->rx_size_1522, &nsd->rx_size_1522);
1018 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1019 I40E_GLPRT_PRC9522L(hw->port),
1020 pf->stat_offsets_loaded,
1021 &osd->rx_size_big, &nsd->rx_size_big);
1022
1023 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1024 I40E_GLPRT_PTC64L(hw->port),
1025 pf->stat_offsets_loaded,
1026 &osd->tx_size_64, &nsd->tx_size_64);
1027 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1028 I40E_GLPRT_PTC127L(hw->port),
1029 pf->stat_offsets_loaded,
1030 &osd->tx_size_127, &nsd->tx_size_127);
1031 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1032 I40E_GLPRT_PTC255L(hw->port),
1033 pf->stat_offsets_loaded,
1034 &osd->tx_size_255, &nsd->tx_size_255);
1035 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1036 I40E_GLPRT_PTC511L(hw->port),
1037 pf->stat_offsets_loaded,
1038 &osd->tx_size_511, &nsd->tx_size_511);
1039 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1040 I40E_GLPRT_PTC1023L(hw->port),
1041 pf->stat_offsets_loaded,
1042 &osd->tx_size_1023, &nsd->tx_size_1023);
1043 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1044 I40E_GLPRT_PTC1522L(hw->port),
1045 pf->stat_offsets_loaded,
1046 &osd->tx_size_1522, &nsd->tx_size_1522);
1047 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1048 I40E_GLPRT_PTC9522L(hw->port),
1049 pf->stat_offsets_loaded,
1050 &osd->tx_size_big, &nsd->tx_size_big);
1051
1052 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1053 pf->stat_offsets_loaded,
1054 &osd->rx_undersize, &nsd->rx_undersize);
1055 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1056 pf->stat_offsets_loaded,
1057 &osd->rx_fragments, &nsd->rx_fragments);
1058 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1059 pf->stat_offsets_loaded,
1060 &osd->rx_oversize, &nsd->rx_oversize);
1061 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1062 pf->stat_offsets_loaded,
1063 &osd->rx_jabber, &nsd->rx_jabber);
1064
1065 /* FDIR stats */
1066 i40e_stat_update_and_clear32(hw,
1067 I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1068 &nsd->fd_atr_match);
1069 i40e_stat_update_and_clear32(hw,
1070 I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1071 &nsd->fd_sb_match);
1072 i40e_stat_update_and_clear32(hw,
1073 I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1074 &nsd->fd_atr_tunnel_match);
1075
1076 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1077 nsd->tx_lpi_status =
1078 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1079 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1080 nsd->rx_lpi_status =
1081 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1082 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1083 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1084 pf->stat_offsets_loaded,
1085 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1086 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1087 pf->stat_offsets_loaded,
1088 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1089
1090 if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1091 !(pf->flags & I40E_FLAG_FD_SB_AUTO_DISABLED))
1092 nsd->fd_sb_status = true;
1093 else
1094 nsd->fd_sb_status = false;
1095
1096 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1097 !(pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED))
1098 nsd->fd_atr_status = true;
1099 else
1100 nsd->fd_atr_status = false;
1101
1102 pf->stat_offsets_loaded = true;
1103 }
1104
1105 /**
1106 * i40e_update_stats - Update the various statistics counters.
1107 * @vsi: the VSI to be updated
1108 *
1109 * Update the various stats for this VSI and its related entities.
1110 **/
1111 void i40e_update_stats(struct i40e_vsi *vsi)
1112 {
1113 struct i40e_pf *pf = vsi->back;
1114
1115 if (vsi == pf->vsi[pf->lan_vsi])
1116 i40e_update_pf_stats(pf);
1117
1118 i40e_update_vsi_stats(vsi);
1119 }
1120
1121 /**
1122 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1123 * @vsi: the VSI to be searched
1124 * @macaddr: the MAC address
1125 * @vlan: the vlan
1126 *
1127 * Returns ptr to the filter object or NULL
1128 **/
1129 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1130 const u8 *macaddr, s16 vlan)
1131 {
1132 struct i40e_mac_filter *f;
1133 u64 key;
1134
1135 if (!vsi || !macaddr)
1136 return NULL;
1137
1138 key = i40e_addr_to_hkey(macaddr);
1139 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1140 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1141 (vlan == f->vlan))
1142 return f;
1143 }
1144 return NULL;
1145 }
1146
1147 /**
1148 * i40e_find_mac - Find a mac addr in the macvlan filters list
1149 * @vsi: the VSI to be searched
1150 * @macaddr: the MAC address we are searching for
1151 *
1152 * Returns the first filter with the provided MAC address or NULL if
1153 * MAC address was not found
1154 **/
1155 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1156 {
1157 struct i40e_mac_filter *f;
1158 u64 key;
1159
1160 if (!vsi || !macaddr)
1161 return NULL;
1162
1163 key = i40e_addr_to_hkey(macaddr);
1164 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1165 if ((ether_addr_equal(macaddr, f->macaddr)))
1166 return f;
1167 }
1168 return NULL;
1169 }
1170
1171 /**
1172 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1173 * @vsi: the VSI to be searched
1174 *
1175 * Returns true if VSI is in vlan mode or false otherwise
1176 **/
1177 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1178 {
1179 /* If we have a PVID, always operate in VLAN mode */
1180 if (vsi->info.pvid)
1181 return true;
1182
1183 /* We need to operate in VLAN mode whenever we have any filters with
1184 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1185 * time, incurring search cost repeatedly. However, we can notice two
1186 * things:
1187 *
1188 * 1) the only place where we can gain a VLAN filter is in
1189 * i40e_add_filter.
1190 *
1191 * 2) the only place where filters are actually removed is in
1192 * i40e_sync_filters_subtask.
1193 *
1194 * Thus, we can simply use a boolean value, has_vlan_filters which we
1195 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1196 * we have to perform the full search after deleting filters in
1197 * i40e_sync_filters_subtask, but we already have to search
1198 * filters here and can perform the check at the same time. This
1199 * results in avoiding embedding a loop for VLAN mode inside another
1200 * loop over all the filters, and should maintain correctness as noted
1201 * above.
1202 */
1203 return vsi->has_vlan_filter;
1204 }
1205
1206 /**
1207 * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1208 * @vsi: the VSI to configure
1209 * @tmp_add_list: list of filters ready to be added
1210 * @tmp_del_list: list of filters ready to be deleted
1211 * @vlan_filters: the number of active VLAN filters
1212 *
1213 * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1214 * behave as expected. If we have any active VLAN filters remaining or about
1215 * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1216 * so that they only match against untagged traffic. If we no longer have any
1217 * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1218 * so that they match against both tagged and untagged traffic. In this way,
1219 * we ensure that we correctly receive the desired traffic. This ensures that
1220 * when we have an active VLAN we will receive only untagged traffic and
1221 * traffic matching active VLANs. If we have no active VLANs then we will
1222 * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1223 *
1224 * Finally, in a similar fashion, this function also corrects filters when
1225 * there is an active PVID assigned to this VSI.
1226 *
1227 * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1228 *
1229 * This function is only expected to be called from within
1230 * i40e_sync_vsi_filters.
1231 *
1232 * NOTE: This function expects to be called while under the
1233 * mac_filter_hash_lock
1234 */
1235 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1236 struct hlist_head *tmp_add_list,
1237 struct hlist_head *tmp_del_list,
1238 int vlan_filters)
1239 {
1240 s16 pvid = le16_to_cpu(vsi->info.pvid);
1241 struct i40e_mac_filter *f, *add_head;
1242 struct i40e_new_mac_filter *new;
1243 struct hlist_node *h;
1244 int bkt, new_vlan;
1245
1246 /* To determine if a particular filter needs to be replaced we
1247 * have the three following conditions:
1248 *
1249 * a) if we have a PVID assigned, then all filters which are
1250 * not marked as VLAN=PVID must be replaced with filters that
1251 * are.
1252 * b) otherwise, if we have any active VLANS, all filters
1253 * which are marked as VLAN=-1 must be replaced with
1254 * filters marked as VLAN=0
1255 * c) finally, if we do not have any active VLANS, all filters
1256 * which are marked as VLAN=0 must be replaced with filters
1257 * marked as VLAN=-1
1258 */
1259
1260 /* Update the filters about to be added in place */
1261 hlist_for_each_entry(new, tmp_add_list, hlist) {
1262 if (pvid && new->f->vlan != pvid)
1263 new->f->vlan = pvid;
1264 else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1265 new->f->vlan = 0;
1266 else if (!vlan_filters && new->f->vlan == 0)
1267 new->f->vlan = I40E_VLAN_ANY;
1268 }
1269
1270 /* Update the remaining active filters */
1271 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1272 /* Combine the checks for whether a filter needs to be changed
1273 * and then determine the new VLAN inside the if block, in
1274 * order to avoid duplicating code for adding the new filter
1275 * then deleting the old filter.
1276 */
1277 if ((pvid && f->vlan != pvid) ||
1278 (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1279 (!vlan_filters && f->vlan == 0)) {
1280 /* Determine the new vlan we will be adding */
1281 if (pvid)
1282 new_vlan = pvid;
1283 else if (vlan_filters)
1284 new_vlan = 0;
1285 else
1286 new_vlan = I40E_VLAN_ANY;
1287
1288 /* Create the new filter */
1289 add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1290 if (!add_head)
1291 return -ENOMEM;
1292
1293 /* Create a temporary i40e_new_mac_filter */
1294 new = kzalloc(sizeof(*new), GFP_ATOMIC);
1295 if (!new)
1296 return -ENOMEM;
1297
1298 new->f = add_head;
1299 new->state = add_head->state;
1300
1301 /* Add the new filter to the tmp list */
1302 hlist_add_head(&new->hlist, tmp_add_list);
1303
1304 /* Put the original filter into the delete list */
1305 f->state = I40E_FILTER_REMOVE;
1306 hash_del(&f->hlist);
1307 hlist_add_head(&f->hlist, tmp_del_list);
1308 }
1309 }
1310
1311 vsi->has_vlan_filter = !!vlan_filters;
1312
1313 return 0;
1314 }
1315
1316 /**
1317 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1318 * @vsi: the PF Main VSI - inappropriate for any other VSI
1319 * @macaddr: the MAC address
1320 *
1321 * Remove whatever filter the firmware set up so the driver can manage
1322 * its own filtering intelligently.
1323 **/
1324 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1325 {
1326 struct i40e_aqc_remove_macvlan_element_data element;
1327 struct i40e_pf *pf = vsi->back;
1328
1329 /* Only appropriate for the PF main VSI */
1330 if (vsi->type != I40E_VSI_MAIN)
1331 return;
1332
1333 memset(&element, 0, sizeof(element));
1334 ether_addr_copy(element.mac_addr, macaddr);
1335 element.vlan_tag = 0;
1336 /* Ignore error returns, some firmware does it this way... */
1337 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1338 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1339
1340 memset(&element, 0, sizeof(element));
1341 ether_addr_copy(element.mac_addr, macaddr);
1342 element.vlan_tag = 0;
1343 /* ...and some firmware does it this way. */
1344 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1345 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1346 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1347 }
1348
1349 /**
1350 * i40e_add_filter - Add a mac/vlan filter to the VSI
1351 * @vsi: the VSI to be searched
1352 * @macaddr: the MAC address
1353 * @vlan: the vlan
1354 *
1355 * Returns ptr to the filter object or NULL when no memory available.
1356 *
1357 * NOTE: This function is expected to be called with mac_filter_hash_lock
1358 * being held.
1359 **/
1360 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1361 const u8 *macaddr, s16 vlan)
1362 {
1363 struct i40e_mac_filter *f;
1364 u64 key;
1365
1366 if (!vsi || !macaddr)
1367 return NULL;
1368
1369 f = i40e_find_filter(vsi, macaddr, vlan);
1370 if (!f) {
1371 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1372 if (!f)
1373 return NULL;
1374
1375 /* Update the boolean indicating if we need to function in
1376 * VLAN mode.
1377 */
1378 if (vlan >= 0)
1379 vsi->has_vlan_filter = true;
1380
1381 ether_addr_copy(f->macaddr, macaddr);
1382 f->vlan = vlan;
1383 /* If we're in overflow promisc mode, set the state directly
1384 * to failed, so we don't bother to try sending the filter
1385 * to the hardware.
1386 */
1387 if (test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state))
1388 f->state = I40E_FILTER_FAILED;
1389 else
1390 f->state = I40E_FILTER_NEW;
1391 INIT_HLIST_NODE(&f->hlist);
1392
1393 key = i40e_addr_to_hkey(macaddr);
1394 hash_add(vsi->mac_filter_hash, &f->hlist, key);
1395
1396 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1397 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1398 }
1399
1400 /* If we're asked to add a filter that has been marked for removal, it
1401 * is safe to simply restore it to active state. __i40e_del_filter
1402 * will have simply deleted any filters which were previously marked
1403 * NEW or FAILED, so if it is currently marked REMOVE it must have
1404 * previously been ACTIVE. Since we haven't yet run the sync filters
1405 * task, just restore this filter to the ACTIVE state so that the
1406 * sync task leaves it in place
1407 */
1408 if (f->state == I40E_FILTER_REMOVE)
1409 f->state = I40E_FILTER_ACTIVE;
1410
1411 return f;
1412 }
1413
1414 /**
1415 * __i40e_del_filter - Remove a specific filter from the VSI
1416 * @vsi: VSI to remove from
1417 * @f: the filter to remove from the list
1418 *
1419 * This function should be called instead of i40e_del_filter only if you know
1420 * the exact filter you will remove already, such as via i40e_find_filter or
1421 * i40e_find_mac.
1422 *
1423 * NOTE: This function is expected to be called with mac_filter_hash_lock
1424 * being held.
1425 * ANOTHER NOTE: This function MUST be called from within the context of
1426 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1427 * instead of list_for_each_entry().
1428 **/
1429 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1430 {
1431 if (!f)
1432 return;
1433
1434 /* If the filter was never added to firmware then we can just delete it
1435 * directly and we don't want to set the status to remove or else an
1436 * admin queue command will unnecessarily fire.
1437 */
1438 if ((f->state == I40E_FILTER_FAILED) ||
1439 (f->state == I40E_FILTER_NEW)) {
1440 hash_del(&f->hlist);
1441 kfree(f);
1442 } else {
1443 f->state = I40E_FILTER_REMOVE;
1444 }
1445
1446 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1447 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1448 }
1449
1450 /**
1451 * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1452 * @vsi: the VSI to be searched
1453 * @macaddr: the MAC address
1454 * @vlan: the VLAN
1455 *
1456 * NOTE: This function is expected to be called with mac_filter_hash_lock
1457 * being held.
1458 * ANOTHER NOTE: This function MUST be called from within the context of
1459 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1460 * instead of list_for_each_entry().
1461 **/
1462 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1463 {
1464 struct i40e_mac_filter *f;
1465
1466 if (!vsi || !macaddr)
1467 return;
1468
1469 f = i40e_find_filter(vsi, macaddr, vlan);
1470 __i40e_del_filter(vsi, f);
1471 }
1472
1473 /**
1474 * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1475 * @vsi: the VSI to be searched
1476 * @macaddr: the mac address to be filtered
1477 *
1478 * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1479 * go through all the macvlan filters and add a macvlan filter for each
1480 * unique vlan that already exists. If a PVID has been assigned, instead only
1481 * add the macaddr to that VLAN.
1482 *
1483 * Returns last filter added on success, else NULL
1484 **/
1485 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1486 const u8 *macaddr)
1487 {
1488 struct i40e_mac_filter *f, *add = NULL;
1489 struct hlist_node *h;
1490 int bkt;
1491
1492 if (vsi->info.pvid)
1493 return i40e_add_filter(vsi, macaddr,
1494 le16_to_cpu(vsi->info.pvid));
1495
1496 if (!i40e_is_vsi_in_vlan(vsi))
1497 return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1498
1499 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1500 if (f->state == I40E_FILTER_REMOVE)
1501 continue;
1502 add = i40e_add_filter(vsi, macaddr, f->vlan);
1503 if (!add)
1504 return NULL;
1505 }
1506
1507 return add;
1508 }
1509
1510 /**
1511 * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1512 * @vsi: the VSI to be searched
1513 * @macaddr: the mac address to be removed
1514 *
1515 * Removes a given MAC address from a VSI regardless of what VLAN it has been
1516 * associated with.
1517 *
1518 * Returns 0 for success, or error
1519 **/
1520 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1521 {
1522 struct i40e_mac_filter *f;
1523 struct hlist_node *h;
1524 bool found = false;
1525 int bkt;
1526
1527 WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
1528 "Missing mac_filter_hash_lock\n");
1529 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1530 if (ether_addr_equal(macaddr, f->macaddr)) {
1531 __i40e_del_filter(vsi, f);
1532 found = true;
1533 }
1534 }
1535
1536 if (found)
1537 return 0;
1538 else
1539 return -ENOENT;
1540 }
1541
1542 /**
1543 * i40e_set_mac - NDO callback to set mac address
1544 * @netdev: network interface device structure
1545 * @p: pointer to an address structure
1546 *
1547 * Returns 0 on success, negative on failure
1548 **/
1549 static int i40e_set_mac(struct net_device *netdev, void *p)
1550 {
1551 struct i40e_netdev_priv *np = netdev_priv(netdev);
1552 struct i40e_vsi *vsi = np->vsi;
1553 struct i40e_pf *pf = vsi->back;
1554 struct i40e_hw *hw = &pf->hw;
1555 struct sockaddr *addr = p;
1556
1557 if (!is_valid_ether_addr(addr->sa_data))
1558 return -EADDRNOTAVAIL;
1559
1560 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1561 netdev_info(netdev, "already using mac address %pM\n",
1562 addr->sa_data);
1563 return 0;
1564 }
1565
1566 if (test_bit(__I40E_VSI_DOWN, vsi->back->state) ||
1567 test_bit(__I40E_RESET_RECOVERY_PENDING, vsi->back->state))
1568 return -EADDRNOTAVAIL;
1569
1570 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1571 netdev_info(netdev, "returning to hw mac address %pM\n",
1572 hw->mac.addr);
1573 else
1574 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1575
1576 /* Copy the address first, so that we avoid a possible race with
1577 * .set_rx_mode(). If we copy after changing the address in the filter
1578 * list, we might open ourselves to a narrow race window where
1579 * .set_rx_mode could delete our dev_addr filter and prevent traffic
1580 * from passing.
1581 */
1582 ether_addr_copy(netdev->dev_addr, addr->sa_data);
1583
1584 spin_lock_bh(&vsi->mac_filter_hash_lock);
1585 i40e_del_mac_filter(vsi, netdev->dev_addr);
1586 i40e_add_mac_filter(vsi, addr->sa_data);
1587 spin_unlock_bh(&vsi->mac_filter_hash_lock);
1588 if (vsi->type == I40E_VSI_MAIN) {
1589 i40e_status ret;
1590
1591 ret = i40e_aq_mac_address_write(&vsi->back->hw,
1592 I40E_AQC_WRITE_TYPE_LAA_WOL,
1593 addr->sa_data, NULL);
1594 if (ret)
1595 netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1596 i40e_stat_str(hw, ret),
1597 i40e_aq_str(hw, hw->aq.asq_last_status));
1598 }
1599
1600 /* schedule our worker thread which will take care of
1601 * applying the new filter changes
1602 */
1603 i40e_service_event_schedule(vsi->back);
1604 return 0;
1605 }
1606
1607 /**
1608 * i40e_config_rss_aq - Prepare for RSS using AQ commands
1609 * @vsi: vsi structure
1610 * @seed: RSS hash seed
1611 **/
1612 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1613 u8 *lut, u16 lut_size)
1614 {
1615 struct i40e_pf *pf = vsi->back;
1616 struct i40e_hw *hw = &pf->hw;
1617 int ret = 0;
1618
1619 if (seed) {
1620 struct i40e_aqc_get_set_rss_key_data *seed_dw =
1621 (struct i40e_aqc_get_set_rss_key_data *)seed;
1622 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1623 if (ret) {
1624 dev_info(&pf->pdev->dev,
1625 "Cannot set RSS key, err %s aq_err %s\n",
1626 i40e_stat_str(hw, ret),
1627 i40e_aq_str(hw, hw->aq.asq_last_status));
1628 return ret;
1629 }
1630 }
1631 if (lut) {
1632 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
1633
1634 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1635 if (ret) {
1636 dev_info(&pf->pdev->dev,
1637 "Cannot set RSS lut, err %s aq_err %s\n",
1638 i40e_stat_str(hw, ret),
1639 i40e_aq_str(hw, hw->aq.asq_last_status));
1640 return ret;
1641 }
1642 }
1643 return ret;
1644 }
1645
1646 /**
1647 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1648 * @vsi: VSI structure
1649 **/
1650 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1651 {
1652 struct i40e_pf *pf = vsi->back;
1653 u8 seed[I40E_HKEY_ARRAY_SIZE];
1654 u8 *lut;
1655 int ret;
1656
1657 if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1658 return 0;
1659 if (!vsi->rss_size)
1660 vsi->rss_size = min_t(int, pf->alloc_rss_size,
1661 vsi->num_queue_pairs);
1662 if (!vsi->rss_size)
1663 return -EINVAL;
1664 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1665 if (!lut)
1666 return -ENOMEM;
1667
1668 /* Use the user configured hash keys and lookup table if there is one,
1669 * otherwise use default
1670 */
1671 if (vsi->rss_lut_user)
1672 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1673 else
1674 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1675 if (vsi->rss_hkey_user)
1676 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1677 else
1678 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1679 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1680 kfree(lut);
1681 return ret;
1682 }
1683
1684 /**
1685 * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1686 * @vsi: the VSI being configured,
1687 * @ctxt: VSI context structure
1688 * @enabled_tc: number of traffic classes to enable
1689 *
1690 * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1691 **/
1692 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1693 struct i40e_vsi_context *ctxt,
1694 u8 enabled_tc)
1695 {
1696 u16 qcount = 0, max_qcount, qmap, sections = 0;
1697 int i, override_q, pow, num_qps, ret;
1698 u8 netdev_tc = 0, offset = 0;
1699
1700 if (vsi->type != I40E_VSI_MAIN)
1701 return -EINVAL;
1702 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1703 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1704 vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1705 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1706 num_qps = vsi->mqprio_qopt.qopt.count[0];
1707
1708 /* find the next higher power-of-2 of num queue pairs */
1709 pow = ilog2(num_qps);
1710 if (!is_power_of_2(num_qps))
1711 pow++;
1712 qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1713 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1714
1715 /* Setup queue offset/count for all TCs for given VSI */
1716 max_qcount = vsi->mqprio_qopt.qopt.count[0];
1717 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1718 /* See if the given TC is enabled for the given VSI */
1719 if (vsi->tc_config.enabled_tc & BIT(i)) {
1720 offset = vsi->mqprio_qopt.qopt.offset[i];
1721 qcount = vsi->mqprio_qopt.qopt.count[i];
1722 if (qcount > max_qcount)
1723 max_qcount = qcount;
1724 vsi->tc_config.tc_info[i].qoffset = offset;
1725 vsi->tc_config.tc_info[i].qcount = qcount;
1726 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1727 } else {
1728 /* TC is not enabled so set the offset to
1729 * default queue and allocate one queue
1730 * for the given TC.
1731 */
1732 vsi->tc_config.tc_info[i].qoffset = 0;
1733 vsi->tc_config.tc_info[i].qcount = 1;
1734 vsi->tc_config.tc_info[i].netdev_tc = 0;
1735 }
1736 }
1737
1738 /* Set actual Tx/Rx queue pairs */
1739 vsi->num_queue_pairs = offset + qcount;
1740
1741 /* Setup queue TC[0].qmap for given VSI context */
1742 ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1743 ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1744 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1745 ctxt->info.valid_sections |= cpu_to_le16(sections);
1746
1747 /* Reconfigure RSS for main VSI with max queue count */
1748 vsi->rss_size = max_qcount;
1749 ret = i40e_vsi_config_rss(vsi);
1750 if (ret) {
1751 dev_info(&vsi->back->pdev->dev,
1752 "Failed to reconfig rss for num_queues (%u)\n",
1753 max_qcount);
1754 return ret;
1755 }
1756 vsi->reconfig_rss = true;
1757 dev_dbg(&vsi->back->pdev->dev,
1758 "Reconfigured rss with num_queues (%u)\n", max_qcount);
1759
1760 /* Find queue count available for channel VSIs and starting offset
1761 * for channel VSIs
1762 */
1763 override_q = vsi->mqprio_qopt.qopt.count[0];
1764 if (override_q && override_q < vsi->num_queue_pairs) {
1765 vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1766 vsi->next_base_queue = override_q;
1767 }
1768 return 0;
1769 }
1770
1771 /**
1772 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1773 * @vsi: the VSI being setup
1774 * @ctxt: VSI context structure
1775 * @enabled_tc: Enabled TCs bitmap
1776 * @is_add: True if called before Add VSI
1777 *
1778 * Setup VSI queue mapping for enabled traffic classes.
1779 **/
1780 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1781 struct i40e_vsi_context *ctxt,
1782 u8 enabled_tc,
1783 bool is_add)
1784 {
1785 struct i40e_pf *pf = vsi->back;
1786 u16 sections = 0;
1787 u8 netdev_tc = 0;
1788 u16 numtc = 0;
1789 u16 qcount;
1790 u8 offset;
1791 u16 qmap;
1792 int i;
1793 u16 num_tc_qps = 0;
1794
1795 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1796 offset = 0;
1797
1798 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1799 /* Find numtc from enabled TC bitmap */
1800 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1801 if (enabled_tc & BIT(i)) /* TC is enabled */
1802 numtc++;
1803 }
1804 if (!numtc) {
1805 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1806 numtc = 1;
1807 }
1808 } else {
1809 /* At least TC0 is enabled in non-DCB, non-MQPRIO case */
1810 numtc = 1;
1811 }
1812
1813 vsi->tc_config.numtc = numtc;
1814 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1815 /* Number of queues per enabled TC */
1816 qcount = vsi->alloc_queue_pairs;
1817
1818 num_tc_qps = qcount / numtc;
1819 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
1820
1821 /* Setup queue offset/count for all TCs for given VSI */
1822 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1823 /* See if the given TC is enabled for the given VSI */
1824 if (vsi->tc_config.enabled_tc & BIT(i)) {
1825 /* TC is enabled */
1826 int pow, num_qps;
1827
1828 switch (vsi->type) {
1829 case I40E_VSI_MAIN:
1830 qcount = min_t(int, pf->alloc_rss_size,
1831 num_tc_qps);
1832 break;
1833 case I40E_VSI_FDIR:
1834 case I40E_VSI_SRIOV:
1835 case I40E_VSI_VMDQ2:
1836 default:
1837 qcount = num_tc_qps;
1838 WARN_ON(i != 0);
1839 break;
1840 }
1841 vsi->tc_config.tc_info[i].qoffset = offset;
1842 vsi->tc_config.tc_info[i].qcount = qcount;
1843
1844 /* find the next higher power-of-2 of num queue pairs */
1845 num_qps = qcount;
1846 pow = 0;
1847 while (num_qps && (BIT_ULL(pow) < qcount)) {
1848 pow++;
1849 num_qps >>= 1;
1850 }
1851
1852 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1853 qmap =
1854 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1855 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1856
1857 offset += qcount;
1858 } else {
1859 /* TC is not enabled so set the offset to
1860 * default queue and allocate one queue
1861 * for the given TC.
1862 */
1863 vsi->tc_config.tc_info[i].qoffset = 0;
1864 vsi->tc_config.tc_info[i].qcount = 1;
1865 vsi->tc_config.tc_info[i].netdev_tc = 0;
1866
1867 qmap = 0;
1868 }
1869 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1870 }
1871
1872 /* Set actual Tx/Rx queue pairs */
1873 vsi->num_queue_pairs = offset;
1874 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1875 if (vsi->req_queue_pairs > 0)
1876 vsi->num_queue_pairs = vsi->req_queue_pairs;
1877 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1878 vsi->num_queue_pairs = pf->num_lan_msix;
1879 }
1880
1881 /* Scheduler section valid can only be set for ADD VSI */
1882 if (is_add) {
1883 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1884
1885 ctxt->info.up_enable_bits = enabled_tc;
1886 }
1887 if (vsi->type == I40E_VSI_SRIOV) {
1888 ctxt->info.mapping_flags |=
1889 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1890 for (i = 0; i < vsi->num_queue_pairs; i++)
1891 ctxt->info.queue_mapping[i] =
1892 cpu_to_le16(vsi->base_queue + i);
1893 } else {
1894 ctxt->info.mapping_flags |=
1895 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1896 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1897 }
1898 ctxt->info.valid_sections |= cpu_to_le16(sections);
1899 }
1900
1901 /**
1902 * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1903 * @netdev: the netdevice
1904 * @addr: address to add
1905 *
1906 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1907 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1908 */
1909 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1910 {
1911 struct i40e_netdev_priv *np = netdev_priv(netdev);
1912 struct i40e_vsi *vsi = np->vsi;
1913
1914 if (i40e_add_mac_filter(vsi, addr))
1915 return 0;
1916 else
1917 return -ENOMEM;
1918 }
1919
1920 /**
1921 * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1922 * @netdev: the netdevice
1923 * @addr: address to add
1924 *
1925 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1926 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1927 */
1928 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1929 {
1930 struct i40e_netdev_priv *np = netdev_priv(netdev);
1931 struct i40e_vsi *vsi = np->vsi;
1932
1933 /* Under some circumstances, we might receive a request to delete
1934 * our own device address from our uc list. Because we store the
1935 * device address in the VSI's MAC/VLAN filter list, we need to ignore
1936 * such requests and not delete our device address from this list.
1937 */
1938 if (ether_addr_equal(addr, netdev->dev_addr))
1939 return 0;
1940
1941 i40e_del_mac_filter(vsi, addr);
1942
1943 return 0;
1944 }
1945
1946 /**
1947 * i40e_set_rx_mode - NDO callback to set the netdev filters
1948 * @netdev: network interface device structure
1949 **/
1950 static void i40e_set_rx_mode(struct net_device *netdev)
1951 {
1952 struct i40e_netdev_priv *np = netdev_priv(netdev);
1953 struct i40e_vsi *vsi = np->vsi;
1954
1955 spin_lock_bh(&vsi->mac_filter_hash_lock);
1956
1957 __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1958 __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1959
1960 spin_unlock_bh(&vsi->mac_filter_hash_lock);
1961
1962 /* check for other flag changes */
1963 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1964 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1965 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1966 }
1967 }
1968
1969 /**
1970 * i40e_undo_del_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 deletion.
1976 **/
1977 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1978 struct hlist_head *from)
1979 {
1980 struct i40e_mac_filter *f;
1981 struct hlist_node *h;
1982
1983 hlist_for_each_entry_safe(f, h, from, hlist) {
1984 u64 key = i40e_addr_to_hkey(f->macaddr);
1985
1986 /* Move the element back into MAC filter list*/
1987 hlist_del(&f->hlist);
1988 hash_add(vsi->mac_filter_hash, &f->hlist, key);
1989 }
1990 }
1991
1992 /**
1993 * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
1994 * @vsi: Pointer to vsi struct
1995 * @from: Pointer to list which contains MAC filter entries - changes to
1996 * those entries needs to be undone.
1997 *
1998 * MAC filter entries from this list were slated for addition.
1999 **/
2000 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2001 struct hlist_head *from)
2002 {
2003 struct i40e_new_mac_filter *new;
2004 struct hlist_node *h;
2005
2006 hlist_for_each_entry_safe(new, h, from, hlist) {
2007 /* We can simply free the wrapper structure */
2008 hlist_del(&new->hlist);
2009 kfree(new);
2010 }
2011 }
2012
2013 /**
2014 * i40e_next_entry - Get the next non-broadcast filter from a list
2015 * @next: pointer to filter in list
2016 *
2017 * Returns the next non-broadcast filter in the list. Required so that we
2018 * ignore broadcast filters within the list, since these are not handled via
2019 * the normal firmware update path.
2020 */
2021 static
2022 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2023 {
2024 hlist_for_each_entry_continue(next, hlist) {
2025 if (!is_broadcast_ether_addr(next->f->macaddr))
2026 return next;
2027 }
2028
2029 return NULL;
2030 }
2031
2032 /**
2033 * i40e_update_filter_state - Update filter state based on return data
2034 * from firmware
2035 * @count: Number of filters added
2036 * @add_list: return data from fw
2037 * @head: pointer to first filter in current batch
2038 *
2039 * MAC filter entries from list were slated to be added to device. Returns
2040 * number of successful filters. Note that 0 does NOT mean success!
2041 **/
2042 static int
2043 i40e_update_filter_state(int count,
2044 struct i40e_aqc_add_macvlan_element_data *add_list,
2045 struct i40e_new_mac_filter *add_head)
2046 {
2047 int retval = 0;
2048 int i;
2049
2050 for (i = 0; i < count; i++) {
2051 /* Always check status of each filter. We don't need to check
2052 * the firmware return status because we pre-set the filter
2053 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2054 * request to the adminq. Thus, if it no longer matches then
2055 * we know the filter is active.
2056 */
2057 if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2058 add_head->state = I40E_FILTER_FAILED;
2059 } else {
2060 add_head->state = I40E_FILTER_ACTIVE;
2061 retval++;
2062 }
2063
2064 add_head = i40e_next_filter(add_head);
2065 if (!add_head)
2066 break;
2067 }
2068
2069 return retval;
2070 }
2071
2072 /**
2073 * i40e_aqc_del_filters - Request firmware to delete a set of filters
2074 * @vsi: ptr to the VSI
2075 * @vsi_name: name to display in messages
2076 * @list: the list of filters to send to firmware
2077 * @num_del: the number of filters to delete
2078 * @retval: Set to -EIO on failure to delete
2079 *
2080 * Send a request to firmware via AdminQ to delete a set of filters. Uses
2081 * *retval instead of a return value so that success does not force ret_val to
2082 * be set to 0. This ensures that a sequence of calls to this function
2083 * preserve the previous value of *retval on successful delete.
2084 */
2085 static
2086 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2087 struct i40e_aqc_remove_macvlan_element_data *list,
2088 int num_del, int *retval)
2089 {
2090 struct i40e_hw *hw = &vsi->back->hw;
2091 i40e_status aq_ret;
2092 int aq_err;
2093
2094 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
2095 aq_err = hw->aq.asq_last_status;
2096
2097 /* Explicitly ignore and do not report when firmware returns ENOENT */
2098 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
2099 *retval = -EIO;
2100 dev_info(&vsi->back->pdev->dev,
2101 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2102 vsi_name, i40e_stat_str(hw, aq_ret),
2103 i40e_aq_str(hw, aq_err));
2104 }
2105 }
2106
2107 /**
2108 * i40e_aqc_add_filters - Request firmware to add a set of filters
2109 * @vsi: ptr to the VSI
2110 * @vsi_name: name to display in messages
2111 * @list: the list of filters to send to firmware
2112 * @add_head: Position in the add hlist
2113 * @num_add: the number of filters to add
2114 * @promisc_change: set to true on exit if promiscuous mode was forced on
2115 *
2116 * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2117 * promisc_changed to true if the firmware has run out of space for more
2118 * filters.
2119 */
2120 static
2121 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2122 struct i40e_aqc_add_macvlan_element_data *list,
2123 struct i40e_new_mac_filter *add_head,
2124 int num_add, bool *promisc_changed)
2125 {
2126 struct i40e_hw *hw = &vsi->back->hw;
2127 int aq_err, fcnt;
2128
2129 i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
2130 aq_err = hw->aq.asq_last_status;
2131 fcnt = i40e_update_filter_state(num_add, list, add_head);
2132
2133 if (fcnt != num_add) {
2134 *promisc_changed = true;
2135 set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2136 dev_warn(&vsi->back->pdev->dev,
2137 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2138 i40e_aq_str(hw, aq_err),
2139 vsi_name);
2140 }
2141 }
2142
2143 /**
2144 * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2145 * @vsi: pointer to the VSI
2146 * @f: filter data
2147 *
2148 * This function sets or clears the promiscuous broadcast flags for VLAN
2149 * filters in order to properly receive broadcast frames. Assumes that only
2150 * broadcast filters are passed.
2151 *
2152 * Returns status indicating success or failure;
2153 **/
2154 static i40e_status
2155 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2156 struct i40e_mac_filter *f)
2157 {
2158 bool enable = f->state == I40E_FILTER_NEW;
2159 struct i40e_hw *hw = &vsi->back->hw;
2160 i40e_status aq_ret;
2161
2162 if (f->vlan == I40E_VLAN_ANY) {
2163 aq_ret = i40e_aq_set_vsi_broadcast(hw,
2164 vsi->seid,
2165 enable,
2166 NULL);
2167 } else {
2168 aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2169 vsi->seid,
2170 enable,
2171 f->vlan,
2172 NULL);
2173 }
2174
2175 if (aq_ret)
2176 dev_warn(&vsi->back->pdev->dev,
2177 "Error %s setting broadcast promiscuous mode on %s\n",
2178 i40e_aq_str(hw, hw->aq.asq_last_status),
2179 vsi_name);
2180
2181 return aq_ret;
2182 }
2183
2184 /**
2185 * i40e_set_promiscuous - set promiscuous mode
2186 * @pf: board private structure
2187 * @promisc: promisc on or off
2188 *
2189 * There are different ways of setting promiscuous mode on a PF depending on
2190 * what state/environment we're in. This identifies and sets it appropriately.
2191 * Returns 0 on success.
2192 **/
2193 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2194 {
2195 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2196 struct i40e_hw *hw = &pf->hw;
2197 i40e_status aq_ret;
2198
2199 if (vsi->type == I40E_VSI_MAIN &&
2200 pf->lan_veb != I40E_NO_VEB &&
2201 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2202 /* set defport ON for Main VSI instead of true promisc
2203 * this way we will get all unicast/multicast and VLAN
2204 * promisc behavior but will not get VF or VMDq traffic
2205 * replicated on the Main VSI.
2206 */
2207 if (promisc)
2208 aq_ret = i40e_aq_set_default_vsi(hw,
2209 vsi->seid,
2210 NULL);
2211 else
2212 aq_ret = i40e_aq_clear_default_vsi(hw,
2213 vsi->seid,
2214 NULL);
2215 if (aq_ret) {
2216 dev_info(&pf->pdev->dev,
2217 "Set default VSI 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 } else {
2222 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2223 hw,
2224 vsi->seid,
2225 promisc, NULL,
2226 true);
2227 if (aq_ret) {
2228 dev_info(&pf->pdev->dev,
2229 "set unicast promisc failed, err %s, aq_err %s\n",
2230 i40e_stat_str(hw, aq_ret),
2231 i40e_aq_str(hw, hw->aq.asq_last_status));
2232 }
2233 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2234 hw,
2235 vsi->seid,
2236 promisc, NULL);
2237 if (aq_ret) {
2238 dev_info(&pf->pdev->dev,
2239 "set multicast promisc failed, err %s, aq_err %s\n",
2240 i40e_stat_str(hw, aq_ret),
2241 i40e_aq_str(hw, hw->aq.asq_last_status));
2242 }
2243 }
2244
2245 if (!aq_ret)
2246 pf->cur_promisc = promisc;
2247
2248 return aq_ret;
2249 }
2250
2251 /**
2252 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2253 * @vsi: ptr to the VSI
2254 *
2255 * Push any outstanding VSI filter changes through the AdminQ.
2256 *
2257 * Returns 0 or error value
2258 **/
2259 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2260 {
2261 struct hlist_head tmp_add_list, tmp_del_list;
2262 struct i40e_mac_filter *f;
2263 struct i40e_new_mac_filter *new, *add_head = NULL;
2264 struct i40e_hw *hw = &vsi->back->hw;
2265 unsigned int failed_filters = 0;
2266 unsigned int vlan_filters = 0;
2267 bool promisc_changed = false;
2268 char vsi_name[16] = "PF";
2269 int filter_list_len = 0;
2270 i40e_status aq_ret = 0;
2271 u32 changed_flags = 0;
2272 struct hlist_node *h;
2273 struct i40e_pf *pf;
2274 int num_add = 0;
2275 int num_del = 0;
2276 int retval = 0;
2277 u16 cmd_flags;
2278 int list_size;
2279 int bkt;
2280
2281 /* empty array typed pointers, kcalloc later */
2282 struct i40e_aqc_add_macvlan_element_data *add_list;
2283 struct i40e_aqc_remove_macvlan_element_data *del_list;
2284
2285 while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2286 usleep_range(1000, 2000);
2287 pf = vsi->back;
2288
2289 if (vsi->netdev) {
2290 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2291 vsi->current_netdev_flags = vsi->netdev->flags;
2292 }
2293
2294 INIT_HLIST_HEAD(&tmp_add_list);
2295 INIT_HLIST_HEAD(&tmp_del_list);
2296
2297 if (vsi->type == I40E_VSI_SRIOV)
2298 snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2299 else if (vsi->type != I40E_VSI_MAIN)
2300 snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2301
2302 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2303 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2304
2305 spin_lock_bh(&vsi->mac_filter_hash_lock);
2306 /* Create a list of filters to delete. */
2307 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2308 if (f->state == I40E_FILTER_REMOVE) {
2309 /* Move the element into temporary del_list */
2310 hash_del(&f->hlist);
2311 hlist_add_head(&f->hlist, &tmp_del_list);
2312
2313 /* Avoid counting removed filters */
2314 continue;
2315 }
2316 if (f->state == I40E_FILTER_NEW) {
2317 /* Create a temporary i40e_new_mac_filter */
2318 new = kzalloc(sizeof(*new), GFP_ATOMIC);
2319 if (!new)
2320 goto err_no_memory_locked;
2321
2322 /* Store pointer to the real filter */
2323 new->f = f;
2324 new->state = f->state;
2325
2326 /* Add it to the hash list */
2327 hlist_add_head(&new->hlist, &tmp_add_list);
2328 }
2329
2330 /* Count the number of active (current and new) VLAN
2331 * filters we have now. Does not count filters which
2332 * are marked for deletion.
2333 */
2334 if (f->vlan > 0)
2335 vlan_filters++;
2336 }
2337
2338 retval = i40e_correct_mac_vlan_filters(vsi,
2339 &tmp_add_list,
2340 &tmp_del_list,
2341 vlan_filters);
2342 if (retval)
2343 goto err_no_memory_locked;
2344
2345 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2346 }
2347
2348 /* Now process 'del_list' outside the lock */
2349 if (!hlist_empty(&tmp_del_list)) {
2350 filter_list_len = hw->aq.asq_buf_size /
2351 sizeof(struct i40e_aqc_remove_macvlan_element_data);
2352 list_size = filter_list_len *
2353 sizeof(struct i40e_aqc_remove_macvlan_element_data);
2354 del_list = kzalloc(list_size, GFP_ATOMIC);
2355 if (!del_list)
2356 goto err_no_memory;
2357
2358 hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2359 cmd_flags = 0;
2360
2361 /* handle broadcast filters by updating the broadcast
2362 * promiscuous flag and release filter list.
2363 */
2364 if (is_broadcast_ether_addr(f->macaddr)) {
2365 i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2366
2367 hlist_del(&f->hlist);
2368 kfree(f);
2369 continue;
2370 }
2371
2372 /* add to delete list */
2373 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2374 if (f->vlan == I40E_VLAN_ANY) {
2375 del_list[num_del].vlan_tag = 0;
2376 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2377 } else {
2378 del_list[num_del].vlan_tag =
2379 cpu_to_le16((u16)(f->vlan));
2380 }
2381
2382 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2383 del_list[num_del].flags = cmd_flags;
2384 num_del++;
2385
2386 /* flush a full buffer */
2387 if (num_del == filter_list_len) {
2388 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2389 num_del, &retval);
2390 memset(del_list, 0, list_size);
2391 num_del = 0;
2392 }
2393 /* Release memory for MAC filter entries which were
2394 * synced up with HW.
2395 */
2396 hlist_del(&f->hlist);
2397 kfree(f);
2398 }
2399
2400 if (num_del) {
2401 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2402 num_del, &retval);
2403 }
2404
2405 kfree(del_list);
2406 del_list = NULL;
2407 }
2408
2409 if (!hlist_empty(&tmp_add_list)) {
2410 /* Do all the adds now. */
2411 filter_list_len = hw->aq.asq_buf_size /
2412 sizeof(struct i40e_aqc_add_macvlan_element_data);
2413 list_size = filter_list_len *
2414 sizeof(struct i40e_aqc_add_macvlan_element_data);
2415 add_list = kzalloc(list_size, GFP_ATOMIC);
2416 if (!add_list)
2417 goto err_no_memory;
2418
2419 num_add = 0;
2420 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2421 if (test_bit(__I40E_VSI_OVERFLOW_PROMISC,
2422 vsi->state)) {
2423 new->state = I40E_FILTER_FAILED;
2424 continue;
2425 }
2426
2427 /* handle broadcast filters by updating the broadcast
2428 * promiscuous flag instead of adding a MAC filter.
2429 */
2430 if (is_broadcast_ether_addr(new->f->macaddr)) {
2431 if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2432 new->f))
2433 new->state = I40E_FILTER_FAILED;
2434 else
2435 new->state = I40E_FILTER_ACTIVE;
2436 continue;
2437 }
2438
2439 /* add to add array */
2440 if (num_add == 0)
2441 add_head = new;
2442 cmd_flags = 0;
2443 ether_addr_copy(add_list[num_add].mac_addr,
2444 new->f->macaddr);
2445 if (new->f->vlan == I40E_VLAN_ANY) {
2446 add_list[num_add].vlan_tag = 0;
2447 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2448 } else {
2449 add_list[num_add].vlan_tag =
2450 cpu_to_le16((u16)(new->f->vlan));
2451 }
2452 add_list[num_add].queue_number = 0;
2453 /* set invalid match method for later detection */
2454 add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2455 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2456 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2457 num_add++;
2458
2459 /* flush a full buffer */
2460 if (num_add == filter_list_len) {
2461 i40e_aqc_add_filters(vsi, vsi_name, add_list,
2462 add_head, num_add,
2463 &promisc_changed);
2464 memset(add_list, 0, list_size);
2465 num_add = 0;
2466 }
2467 }
2468 if (num_add) {
2469 i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2470 num_add, &promisc_changed);
2471 }
2472 /* Now move all of the filters from the temp add list back to
2473 * the VSI's list.
2474 */
2475 spin_lock_bh(&vsi->mac_filter_hash_lock);
2476 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2477 /* Only update the state if we're still NEW */
2478 if (new->f->state == I40E_FILTER_NEW)
2479 new->f->state = new->state;
2480 hlist_del(&new->hlist);
2481 kfree(new);
2482 }
2483 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2484 kfree(add_list);
2485 add_list = NULL;
2486 }
2487
2488 /* Determine the number of active and failed filters. */
2489 spin_lock_bh(&vsi->mac_filter_hash_lock);
2490 vsi->active_filters = 0;
2491 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2492 if (f->state == I40E_FILTER_ACTIVE)
2493 vsi->active_filters++;
2494 else if (f->state == I40E_FILTER_FAILED)
2495 failed_filters++;
2496 }
2497 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2498
2499 /* If promiscuous mode has changed, we need to calculate a new
2500 * threshold for when we are safe to exit
2501 */
2502 if (promisc_changed)
2503 vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2504
2505 /* Check if we are able to exit overflow promiscuous mode. We can
2506 * safely exit if we didn't just enter, we no longer have any failed
2507 * filters, and we have reduced filters below the threshold value.
2508 */
2509 if (test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state) &&
2510 !promisc_changed && !failed_filters &&
2511 (vsi->active_filters < vsi->promisc_threshold)) {
2512 dev_info(&pf->pdev->dev,
2513 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2514 vsi_name);
2515 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2516 promisc_changed = true;
2517 vsi->promisc_threshold = 0;
2518 }
2519
2520 /* if the VF is not trusted do not do promisc */
2521 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2522 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2523 goto out;
2524 }
2525
2526 /* check for changes in promiscuous modes */
2527 if (changed_flags & IFF_ALLMULTI) {
2528 bool cur_multipromisc;
2529
2530 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2531 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2532 vsi->seid,
2533 cur_multipromisc,
2534 NULL);
2535 if (aq_ret) {
2536 retval = i40e_aq_rc_to_posix(aq_ret,
2537 hw->aq.asq_last_status);
2538 dev_info(&pf->pdev->dev,
2539 "set multi promisc failed on %s, err %s aq_err %s\n",
2540 vsi_name,
2541 i40e_stat_str(hw, aq_ret),
2542 i40e_aq_str(hw, hw->aq.asq_last_status));
2543 }
2544 }
2545
2546 if ((changed_flags & IFF_PROMISC) || promisc_changed) {
2547 bool cur_promisc;
2548
2549 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2550 test_bit(__I40E_VSI_OVERFLOW_PROMISC,
2551 vsi->state));
2552 aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2553 if (aq_ret) {
2554 retval = i40e_aq_rc_to_posix(aq_ret,
2555 hw->aq.asq_last_status);
2556 dev_info(&pf->pdev->dev,
2557 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2558 cur_promisc ? "on" : "off",
2559 vsi_name,
2560 i40e_stat_str(hw, aq_ret),
2561 i40e_aq_str(hw, hw->aq.asq_last_status));
2562 }
2563 }
2564 out:
2565 /* if something went wrong then set the changed flag so we try again */
2566 if (retval)
2567 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2568
2569 clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2570 return retval;
2571
2572 err_no_memory:
2573 /* Restore elements on the temporary add and delete lists */
2574 spin_lock_bh(&vsi->mac_filter_hash_lock);
2575 err_no_memory_locked:
2576 i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2577 i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2578 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2579
2580 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2581 clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2582 return -ENOMEM;
2583 }
2584
2585 /**
2586 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2587 * @pf: board private structure
2588 **/
2589 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2590 {
2591 int v;
2592
2593 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2594 return;
2595 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2596
2597 for (v = 0; v < pf->num_alloc_vsi; v++) {
2598 if (pf->vsi[v] &&
2599 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2600 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2601
2602 if (ret) {
2603 /* come back and try again later */
2604 pf->flags |= I40E_FLAG_FILTER_SYNC;
2605 break;
2606 }
2607 }
2608 }
2609 }
2610
2611 /**
2612 * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2613 * @vsi: the vsi
2614 **/
2615 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2616 {
2617 if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2618 return I40E_RXBUFFER_2048;
2619 else
2620 return I40E_RXBUFFER_3072;
2621 }
2622
2623 /**
2624 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2625 * @netdev: network interface device structure
2626 * @new_mtu: new value for maximum frame size
2627 *
2628 * Returns 0 on success, negative on failure
2629 **/
2630 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2631 {
2632 struct i40e_netdev_priv *np = netdev_priv(netdev);
2633 struct i40e_vsi *vsi = np->vsi;
2634 struct i40e_pf *pf = vsi->back;
2635
2636 if (i40e_enabled_xdp_vsi(vsi)) {
2637 int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2638
2639 if (frame_size > i40e_max_xdp_frame_size(vsi))
2640 return -EINVAL;
2641 }
2642
2643 netdev_info(netdev, "changing MTU from %d to %d\n",
2644 netdev->mtu, new_mtu);
2645 netdev->mtu = new_mtu;
2646 if (netif_running(netdev))
2647 i40e_vsi_reinit_locked(vsi);
2648 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
2649 I40E_FLAG_CLIENT_L2_CHANGE);
2650 return 0;
2651 }
2652
2653 /**
2654 * i40e_ioctl - Access the hwtstamp interface
2655 * @netdev: network interface device structure
2656 * @ifr: interface request data
2657 * @cmd: ioctl command
2658 **/
2659 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2660 {
2661 struct i40e_netdev_priv *np = netdev_priv(netdev);
2662 struct i40e_pf *pf = np->vsi->back;
2663
2664 switch (cmd) {
2665 case SIOCGHWTSTAMP:
2666 return i40e_ptp_get_ts_config(pf, ifr);
2667 case SIOCSHWTSTAMP:
2668 return i40e_ptp_set_ts_config(pf, ifr);
2669 default:
2670 return -EOPNOTSUPP;
2671 }
2672 }
2673
2674 /**
2675 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2676 * @vsi: the vsi being adjusted
2677 **/
2678 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2679 {
2680 struct i40e_vsi_context ctxt;
2681 i40e_status ret;
2682
2683 if ((vsi->info.valid_sections &
2684 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2685 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2686 return; /* already enabled */
2687
2688 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2689 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2690 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2691
2692 ctxt.seid = vsi->seid;
2693 ctxt.info = vsi->info;
2694 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2695 if (ret) {
2696 dev_info(&vsi->back->pdev->dev,
2697 "update vlan stripping failed, err %s aq_err %s\n",
2698 i40e_stat_str(&vsi->back->hw, ret),
2699 i40e_aq_str(&vsi->back->hw,
2700 vsi->back->hw.aq.asq_last_status));
2701 }
2702 }
2703
2704 /**
2705 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2706 * @vsi: the vsi being adjusted
2707 **/
2708 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2709 {
2710 struct i40e_vsi_context ctxt;
2711 i40e_status ret;
2712
2713 if ((vsi->info.valid_sections &
2714 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2715 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2716 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2717 return; /* already disabled */
2718
2719 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2720 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2721 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2722
2723 ctxt.seid = vsi->seid;
2724 ctxt.info = vsi->info;
2725 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2726 if (ret) {
2727 dev_info(&vsi->back->pdev->dev,
2728 "update vlan stripping failed, err %s aq_err %s\n",
2729 i40e_stat_str(&vsi->back->hw, ret),
2730 i40e_aq_str(&vsi->back->hw,
2731 vsi->back->hw.aq.asq_last_status));
2732 }
2733 }
2734
2735 /**
2736 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2737 * @netdev: network interface to be adjusted
2738 * @features: netdev features to test if VLAN offload is enabled or not
2739 **/
2740 static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2741 {
2742 struct i40e_netdev_priv *np = netdev_priv(netdev);
2743 struct i40e_vsi *vsi = np->vsi;
2744
2745 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2746 i40e_vlan_stripping_enable(vsi);
2747 else
2748 i40e_vlan_stripping_disable(vsi);
2749 }
2750
2751 /**
2752 * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
2753 * @vsi: the vsi being configured
2754 * @vid: vlan id to be added (0 = untagged only , -1 = any)
2755 *
2756 * This is a helper function for adding a new MAC/VLAN filter with the
2757 * specified VLAN for each existing MAC address already in the hash table.
2758 * This function does *not* perform any accounting to update filters based on
2759 * VLAN mode.
2760 *
2761 * NOTE: this function expects to be called while under the
2762 * mac_filter_hash_lock
2763 **/
2764 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2765 {
2766 struct i40e_mac_filter *f, *add_f;
2767 struct hlist_node *h;
2768 int bkt;
2769
2770 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2771 if (f->state == I40E_FILTER_REMOVE)
2772 continue;
2773 add_f = i40e_add_filter(vsi, f->macaddr, vid);
2774 if (!add_f) {
2775 dev_info(&vsi->back->pdev->dev,
2776 "Could not add vlan filter %d for %pM\n",
2777 vid, f->macaddr);
2778 return -ENOMEM;
2779 }
2780 }
2781
2782 return 0;
2783 }
2784
2785 /**
2786 * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2787 * @vsi: the VSI being configured
2788 * @vid: VLAN id to be added
2789 **/
2790 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
2791 {
2792 int err;
2793
2794 if (vsi->info.pvid)
2795 return -EINVAL;
2796
2797 /* The network stack will attempt to add VID=0, with the intention to
2798 * receive priority tagged packets with a VLAN of 0. Our HW receives
2799 * these packets by default when configured to receive untagged
2800 * packets, so we don't need to add a filter for this case.
2801 * Additionally, HW interprets adding a VID=0 filter as meaning to
2802 * receive *only* tagged traffic and stops receiving untagged traffic.
2803 * Thus, we do not want to actually add a filter for VID=0
2804 */
2805 if (!vid)
2806 return 0;
2807
2808 /* Locked once because all functions invoked below iterates list*/
2809 spin_lock_bh(&vsi->mac_filter_hash_lock);
2810 err = i40e_add_vlan_all_mac(vsi, vid);
2811 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2812 if (err)
2813 return err;
2814
2815 /* schedule our worker thread which will take care of
2816 * applying the new filter changes
2817 */
2818 i40e_service_event_schedule(vsi->back);
2819 return 0;
2820 }
2821
2822 /**
2823 * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
2824 * @vsi: the vsi being configured
2825 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
2826 *
2827 * This function should be used to remove all VLAN filters which match the
2828 * given VID. It does not schedule the service event and does not take the
2829 * mac_filter_hash_lock so it may be combined with other operations under
2830 * a single invocation of the mac_filter_hash_lock.
2831 *
2832 * NOTE: this function expects to be called while under the
2833 * mac_filter_hash_lock
2834 */
2835 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2836 {
2837 struct i40e_mac_filter *f;
2838 struct hlist_node *h;
2839 int bkt;
2840
2841 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2842 if (f->vlan == vid)
2843 __i40e_del_filter(vsi, f);
2844 }
2845 }
2846
2847 /**
2848 * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2849 * @vsi: the VSI being configured
2850 * @vid: VLAN id to be removed
2851 **/
2852 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
2853 {
2854 if (!vid || vsi->info.pvid)
2855 return;
2856
2857 spin_lock_bh(&vsi->mac_filter_hash_lock);
2858 i40e_rm_vlan_all_mac(vsi, vid);
2859 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2860
2861 /* schedule our worker thread which will take care of
2862 * applying the new filter changes
2863 */
2864 i40e_service_event_schedule(vsi->back);
2865 }
2866
2867 /**
2868 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2869 * @netdev: network interface to be adjusted
2870 * @vid: vlan id to be added
2871 *
2872 * net_device_ops implementation for adding vlan ids
2873 **/
2874 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2875 __always_unused __be16 proto, u16 vid)
2876 {
2877 struct i40e_netdev_priv *np = netdev_priv(netdev);
2878 struct i40e_vsi *vsi = np->vsi;
2879 int ret = 0;
2880
2881 if (vid >= VLAN_N_VID)
2882 return -EINVAL;
2883
2884 ret = i40e_vsi_add_vlan(vsi, vid);
2885 if (!ret)
2886 set_bit(vid, vsi->active_vlans);
2887
2888 return ret;
2889 }
2890
2891 /**
2892 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2893 * @netdev: network interface to be adjusted
2894 * @vid: vlan id to be removed
2895 *
2896 * net_device_ops implementation for removing vlan ids
2897 **/
2898 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2899 __always_unused __be16 proto, u16 vid)
2900 {
2901 struct i40e_netdev_priv *np = netdev_priv(netdev);
2902 struct i40e_vsi *vsi = np->vsi;
2903
2904 /* return code is ignored as there is nothing a user
2905 * can do about failure to remove and a log message was
2906 * already printed from the other function
2907 */
2908 i40e_vsi_kill_vlan(vsi, vid);
2909
2910 clear_bit(vid, vsi->active_vlans);
2911
2912 return 0;
2913 }
2914
2915 /**
2916 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2917 * @vsi: the vsi being brought back up
2918 **/
2919 static void i40e_restore_vlan(struct i40e_vsi *vsi)
2920 {
2921 u16 vid;
2922
2923 if (!vsi->netdev)
2924 return;
2925
2926 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2927
2928 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2929 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2930 vid);
2931 }
2932
2933 /**
2934 * i40e_vsi_add_pvid - Add pvid for the VSI
2935 * @vsi: the vsi being adjusted
2936 * @vid: the vlan id to set as a PVID
2937 **/
2938 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
2939 {
2940 struct i40e_vsi_context ctxt;
2941 i40e_status ret;
2942
2943 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2944 vsi->info.pvid = cpu_to_le16(vid);
2945 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2946 I40E_AQ_VSI_PVLAN_INSERT_PVID |
2947 I40E_AQ_VSI_PVLAN_EMOD_STR;
2948
2949 ctxt.seid = vsi->seid;
2950 ctxt.info = vsi->info;
2951 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2952 if (ret) {
2953 dev_info(&vsi->back->pdev->dev,
2954 "add pvid failed, err %s aq_err %s\n",
2955 i40e_stat_str(&vsi->back->hw, ret),
2956 i40e_aq_str(&vsi->back->hw,
2957 vsi->back->hw.aq.asq_last_status));
2958 return -ENOENT;
2959 }
2960
2961 return 0;
2962 }
2963
2964 /**
2965 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2966 * @vsi: the vsi being adjusted
2967 *
2968 * Just use the vlan_rx_register() service to put it back to normal
2969 **/
2970 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2971 {
2972 i40e_vlan_stripping_disable(vsi);
2973
2974 vsi->info.pvid = 0;
2975 }
2976
2977 /**
2978 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2979 * @vsi: ptr to the VSI
2980 *
2981 * If this function returns with an error, then it's possible one or
2982 * more of the rings is populated (while the rest are not). It is the
2983 * callers duty to clean those orphaned rings.
2984 *
2985 * Return 0 on success, negative on failure
2986 **/
2987 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2988 {
2989 int i, err = 0;
2990
2991 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
2992 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
2993
2994 if (!i40e_enabled_xdp_vsi(vsi))
2995 return err;
2996
2997 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
2998 err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
2999
3000 return err;
3001 }
3002
3003 /**
3004 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3005 * @vsi: ptr to the VSI
3006 *
3007 * Free VSI's transmit software resources
3008 **/
3009 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3010 {
3011 int i;
3012
3013 if (vsi->tx_rings) {
3014 for (i = 0; i < vsi->num_queue_pairs; i++)
3015 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3016 i40e_free_tx_resources(vsi->tx_rings[i]);
3017 }
3018
3019 if (vsi->xdp_rings) {
3020 for (i = 0; i < vsi->num_queue_pairs; i++)
3021 if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3022 i40e_free_tx_resources(vsi->xdp_rings[i]);
3023 }
3024 }
3025
3026 /**
3027 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3028 * @vsi: ptr to the VSI
3029 *
3030 * If this function returns with an error, then it's possible one or
3031 * more of the rings is populated (while the rest are not). It is the
3032 * callers duty to clean those orphaned rings.
3033 *
3034 * Return 0 on success, negative on failure
3035 **/
3036 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3037 {
3038 int i, err = 0;
3039
3040 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3041 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3042 return err;
3043 }
3044
3045 /**
3046 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3047 * @vsi: ptr to the VSI
3048 *
3049 * Free all receive software resources
3050 **/
3051 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3052 {
3053 int i;
3054
3055 if (!vsi->rx_rings)
3056 return;
3057
3058 for (i = 0; i < vsi->num_queue_pairs; i++)
3059 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3060 i40e_free_rx_resources(vsi->rx_rings[i]);
3061 }
3062
3063 /**
3064 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3065 * @ring: The Tx ring to configure
3066 *
3067 * This enables/disables XPS for a given Tx descriptor ring
3068 * based on the TCs enabled for the VSI that ring belongs to.
3069 **/
3070 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3071 {
3072 int cpu;
3073
3074 if (!ring->q_vector || !ring->netdev || ring->ch)
3075 return;
3076
3077 /* We only initialize XPS once, so as not to overwrite user settings */
3078 if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3079 return;
3080
3081 cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3082 netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3083 ring->queue_index);
3084 }
3085
3086 /**
3087 * i40e_configure_tx_ring - Configure a transmit ring context and rest
3088 * @ring: The Tx ring to configure
3089 *
3090 * Configure the Tx descriptor ring in the HMC context.
3091 **/
3092 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3093 {
3094 struct i40e_vsi *vsi = ring->vsi;
3095 u16 pf_q = vsi->base_queue + ring->queue_index;
3096 struct i40e_hw *hw = &vsi->back->hw;
3097 struct i40e_hmc_obj_txq tx_ctx;
3098 i40e_status err = 0;
3099 u32 qtx_ctl = 0;
3100
3101 /* some ATR related tx ring init */
3102 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3103 ring->atr_sample_rate = vsi->back->atr_sample_rate;
3104 ring->atr_count = 0;
3105 } else {
3106 ring->atr_sample_rate = 0;
3107 }
3108
3109 /* configure XPS */
3110 i40e_config_xps_tx_ring(ring);
3111
3112 /* clear the context structure first */
3113 memset(&tx_ctx, 0, sizeof(tx_ctx));
3114
3115 tx_ctx.new_context = 1;
3116 tx_ctx.base = (ring->dma / 128);
3117 tx_ctx.qlen = ring->count;
3118 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3119 I40E_FLAG_FD_ATR_ENABLED));
3120 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3121 /* FDIR VSI tx ring can still use RS bit and writebacks */
3122 if (vsi->type != I40E_VSI_FDIR)
3123 tx_ctx.head_wb_ena = 1;
3124 tx_ctx.head_wb_addr = ring->dma +
3125 (ring->count * sizeof(struct i40e_tx_desc));
3126
3127 /* As part of VSI creation/update, FW allocates certain
3128 * Tx arbitration queue sets for each TC enabled for
3129 * the VSI. The FW returns the handles to these queue
3130 * sets as part of the response buffer to Add VSI,
3131 * Update VSI, etc. AQ commands. It is expected that
3132 * these queue set handles be associated with the Tx
3133 * queues by the driver as part of the TX queue context
3134 * initialization. This has to be done regardless of
3135 * DCB as by default everything is mapped to TC0.
3136 */
3137
3138 if (ring->ch)
3139 tx_ctx.rdylist =
3140 le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3141
3142 else
3143 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3144
3145 tx_ctx.rdylist_act = 0;
3146
3147 /* clear the context in the HMC */
3148 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3149 if (err) {
3150 dev_info(&vsi->back->pdev->dev,
3151 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3152 ring->queue_index, pf_q, err);
3153 return -ENOMEM;
3154 }
3155
3156 /* set the context in the HMC */
3157 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3158 if (err) {
3159 dev_info(&vsi->back->pdev->dev,
3160 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3161 ring->queue_index, pf_q, err);
3162 return -ENOMEM;
3163 }
3164
3165 /* Now associate this queue with this PCI function */
3166 if (ring->ch) {
3167 if (ring->ch->type == I40E_VSI_VMDQ2)
3168 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3169 else
3170 return -EINVAL;
3171
3172 qtx_ctl |= (ring->ch->vsi_number <<
3173 I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3174 I40E_QTX_CTL_VFVM_INDX_MASK;
3175 } else {
3176 if (vsi->type == I40E_VSI_VMDQ2) {
3177 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3178 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3179 I40E_QTX_CTL_VFVM_INDX_MASK;
3180 } else {
3181 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3182 }
3183 }
3184
3185 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3186 I40E_QTX_CTL_PF_INDX_MASK);
3187 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3188 i40e_flush(hw);
3189
3190 /* cache tail off for easier writes later */
3191 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3192
3193 return 0;
3194 }
3195
3196 /**
3197 * i40e_configure_rx_ring - Configure a receive ring context
3198 * @ring: The Rx ring to configure
3199 *
3200 * Configure the Rx descriptor ring in the HMC context.
3201 **/
3202 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3203 {
3204 struct i40e_vsi *vsi = ring->vsi;
3205 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3206 u16 pf_q = vsi->base_queue + ring->queue_index;
3207 struct i40e_hw *hw = &vsi->back->hw;
3208 struct i40e_hmc_obj_rxq rx_ctx;
3209 i40e_status err = 0;
3210
3211 bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3212
3213 /* clear the context structure first */
3214 memset(&rx_ctx, 0, sizeof(rx_ctx));
3215
3216 ring->rx_buf_len = vsi->rx_buf_len;
3217
3218 rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3219 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3220
3221 rx_ctx.base = (ring->dma / 128);
3222 rx_ctx.qlen = ring->count;
3223
3224 /* use 32 byte descriptors */
3225 rx_ctx.dsize = 1;
3226
3227 /* descriptor type is always zero
3228 * rx_ctx.dtype = 0;
3229 */
3230 rx_ctx.hsplit_0 = 0;
3231
3232 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3233 if (hw->revision_id == 0)
3234 rx_ctx.lrxqthresh = 0;
3235 else
3236 rx_ctx.lrxqthresh = 1;
3237 rx_ctx.crcstrip = 1;
3238 rx_ctx.l2tsel = 1;
3239 /* this controls whether VLAN is stripped from inner headers */
3240 rx_ctx.showiv = 0;
3241 /* set the prefena field to 1 because the manual says to */
3242 rx_ctx.prefena = 1;
3243
3244 /* clear the context in the HMC */
3245 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3246 if (err) {
3247 dev_info(&vsi->back->pdev->dev,
3248 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3249 ring->queue_index, pf_q, err);
3250 return -ENOMEM;
3251 }
3252
3253 /* set the context in the HMC */
3254 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3255 if (err) {
3256 dev_info(&vsi->back->pdev->dev,
3257 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3258 ring->queue_index, pf_q, err);
3259 return -ENOMEM;
3260 }
3261
3262 /* configure Rx buffer alignment */
3263 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3264 clear_ring_build_skb_enabled(ring);
3265 else
3266 set_ring_build_skb_enabled(ring);
3267
3268 /* cache tail for quicker writes, and clear the reg before use */
3269 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3270 writel(0, ring->tail);
3271
3272 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3273
3274 return 0;
3275 }
3276
3277 /**
3278 * i40e_vsi_configure_tx - Configure the VSI for Tx
3279 * @vsi: VSI structure describing this set of rings and resources
3280 *
3281 * Configure the Tx VSI for operation.
3282 **/
3283 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3284 {
3285 int err = 0;
3286 u16 i;
3287
3288 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3289 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3290
3291 if (!i40e_enabled_xdp_vsi(vsi))
3292 return err;
3293
3294 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3295 err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3296
3297 return err;
3298 }
3299
3300 /**
3301 * i40e_vsi_configure_rx - Configure the VSI for Rx
3302 * @vsi: the VSI being configured
3303 *
3304 * Configure the Rx VSI for operation.
3305 **/
3306 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3307 {
3308 int err = 0;
3309 u16 i;
3310
3311 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3312 vsi->max_frame = I40E_MAX_RXBUFFER;
3313 vsi->rx_buf_len = I40E_RXBUFFER_2048;
3314 #if (PAGE_SIZE < 8192)
3315 } else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3316 (vsi->netdev->mtu <= ETH_DATA_LEN)) {
3317 vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3318 vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3319 #endif
3320 } else {
3321 vsi->max_frame = I40E_MAX_RXBUFFER;
3322 vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3323 I40E_RXBUFFER_2048;
3324 }
3325
3326 /* set up individual rings */
3327 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3328 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3329
3330 return err;
3331 }
3332
3333 /**
3334 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3335 * @vsi: ptr to the VSI
3336 **/
3337 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3338 {
3339 struct i40e_ring *tx_ring, *rx_ring;
3340 u16 qoffset, qcount;
3341 int i, n;
3342
3343 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3344 /* Reset the TC information */
3345 for (i = 0; i < vsi->num_queue_pairs; i++) {
3346 rx_ring = vsi->rx_rings[i];
3347 tx_ring = vsi->tx_rings[i];
3348 rx_ring->dcb_tc = 0;
3349 tx_ring->dcb_tc = 0;
3350 }
3351 return;
3352 }
3353
3354 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3355 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3356 continue;
3357
3358 qoffset = vsi->tc_config.tc_info[n].qoffset;
3359 qcount = vsi->tc_config.tc_info[n].qcount;
3360 for (i = qoffset; i < (qoffset + qcount); i++) {
3361 rx_ring = vsi->rx_rings[i];
3362 tx_ring = vsi->tx_rings[i];
3363 rx_ring->dcb_tc = n;
3364 tx_ring->dcb_tc = n;
3365 }
3366 }
3367 }
3368
3369 /**
3370 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3371 * @vsi: ptr to the VSI
3372 **/
3373 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3374 {
3375 if (vsi->netdev)
3376 i40e_set_rx_mode(vsi->netdev);
3377 }
3378
3379 /**
3380 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3381 * @vsi: Pointer to the targeted VSI
3382 *
3383 * This function replays the hlist on the hw where all the SB Flow Director
3384 * filters were saved.
3385 **/
3386 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3387 {
3388 struct i40e_fdir_filter *filter;
3389 struct i40e_pf *pf = vsi->back;
3390 struct hlist_node *node;
3391
3392 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3393 return;
3394
3395 /* Reset FDir counters as we're replaying all existing filters */
3396 pf->fd_tcp4_filter_cnt = 0;
3397 pf->fd_udp4_filter_cnt = 0;
3398 pf->fd_sctp4_filter_cnt = 0;
3399 pf->fd_ip4_filter_cnt = 0;
3400
3401 hlist_for_each_entry_safe(filter, node,
3402 &pf->fdir_filter_list, fdir_node) {
3403 i40e_add_del_fdir(vsi, filter, true);
3404 }
3405 }
3406
3407 /**
3408 * i40e_vsi_configure - Set up the VSI for action
3409 * @vsi: the VSI being configured
3410 **/
3411 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3412 {
3413 int err;
3414
3415 i40e_set_vsi_rx_mode(vsi);
3416 i40e_restore_vlan(vsi);
3417 i40e_vsi_config_dcb_rings(vsi);
3418 err = i40e_vsi_configure_tx(vsi);
3419 if (!err)
3420 err = i40e_vsi_configure_rx(vsi);
3421
3422 return err;
3423 }
3424
3425 /**
3426 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3427 * @vsi: the VSI being configured
3428 **/
3429 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3430 {
3431 bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3432 struct i40e_pf *pf = vsi->back;
3433 struct i40e_hw *hw = &pf->hw;
3434 u16 vector;
3435 int i, q;
3436 u32 qp;
3437
3438 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3439 * and PFINT_LNKLSTn registers, e.g.:
3440 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3441 */
3442 qp = vsi->base_queue;
3443 vector = vsi->base_vector;
3444 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3445 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3446
3447 q_vector->itr_countdown = ITR_COUNTDOWN_START;
3448 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
3449 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3450 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3451 q_vector->rx.itr);
3452 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
3453 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3454 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3455 q_vector->tx.itr);
3456 wr32(hw, I40E_PFINT_RATEN(vector - 1),
3457 i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3458
3459 /* Linked list for the queuepairs assigned to this vector */
3460 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3461 for (q = 0; q < q_vector->num_ringpairs; q++) {
3462 u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3463 u32 val;
3464
3465 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3466 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3467 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3468 (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3469 (I40E_QUEUE_TYPE_TX <<
3470 I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3471
3472 wr32(hw, I40E_QINT_RQCTL(qp), val);
3473
3474 if (has_xdp) {
3475 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3476 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3477 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3478 (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3479 (I40E_QUEUE_TYPE_TX <<
3480 I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3481
3482 wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3483 }
3484
3485 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3486 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3487 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3488 ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3489 (I40E_QUEUE_TYPE_RX <<
3490 I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3491
3492 /* Terminate the linked list */
3493 if (q == (q_vector->num_ringpairs - 1))
3494 val |= (I40E_QUEUE_END_OF_LIST <<
3495 I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3496
3497 wr32(hw, I40E_QINT_TQCTL(qp), val);
3498 qp++;
3499 }
3500 }
3501
3502 i40e_flush(hw);
3503 }
3504
3505 /**
3506 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3507 * @hw: ptr to the hardware info
3508 **/
3509 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3510 {
3511 struct i40e_hw *hw = &pf->hw;
3512 u32 val;
3513
3514 /* clear things first */
3515 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3516 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3517
3518 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3519 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3520 I40E_PFINT_ICR0_ENA_GRST_MASK |
3521 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3522 I40E_PFINT_ICR0_ENA_GPIO_MASK |
3523 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3524 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3525 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3526
3527 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3528 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3529
3530 if (pf->flags & I40E_FLAG_PTP)
3531 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3532
3533 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3534
3535 /* SW_ITR_IDX = 0, but don't change INTENA */
3536 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3537 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3538
3539 /* OTHER_ITR_IDX = 0 */
3540 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3541 }
3542
3543 /**
3544 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3545 * @vsi: the VSI being configured
3546 **/
3547 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3548 {
3549 u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3550 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3551 struct i40e_pf *pf = vsi->back;
3552 struct i40e_hw *hw = &pf->hw;
3553 u32 val;
3554
3555 /* set the ITR configuration */
3556 q_vector->itr_countdown = ITR_COUNTDOWN_START;
3557 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
3558 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3559 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
3560 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
3561 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3562 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3563
3564 i40e_enable_misc_int_causes(pf);
3565
3566 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3567 wr32(hw, I40E_PFINT_LNKLST0, 0);
3568
3569 /* Associate the queue pair to the vector and enable the queue int */
3570 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3571 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3572 (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3573 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3574
3575 wr32(hw, I40E_QINT_RQCTL(0), val);
3576
3577 if (i40e_enabled_xdp_vsi(vsi)) {
3578 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3579 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3580 (I40E_QUEUE_TYPE_TX
3581 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3582
3583 wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3584 }
3585
3586 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3587 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3588 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3589
3590 wr32(hw, I40E_QINT_TQCTL(0), val);
3591 i40e_flush(hw);
3592 }
3593
3594 /**
3595 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3596 * @pf: board private structure
3597 **/
3598 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3599 {
3600 struct i40e_hw *hw = &pf->hw;
3601
3602 wr32(hw, I40E_PFINT_DYN_CTL0,
3603 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3604 i40e_flush(hw);
3605 }
3606
3607 /**
3608 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3609 * @pf: board private structure
3610 **/
3611 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
3612 {
3613 struct i40e_hw *hw = &pf->hw;
3614 u32 val;
3615
3616 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
3617 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
3618 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3619
3620 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3621 i40e_flush(hw);
3622 }
3623
3624 /**
3625 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3626 * @irq: interrupt number
3627 * @data: pointer to a q_vector
3628 **/
3629 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3630 {
3631 struct i40e_q_vector *q_vector = data;
3632
3633 if (!q_vector->tx.ring && !q_vector->rx.ring)
3634 return IRQ_HANDLED;
3635
3636 napi_schedule_irqoff(&q_vector->napi);
3637
3638 return IRQ_HANDLED;
3639 }
3640
3641 /**
3642 * i40e_irq_affinity_notify - Callback for affinity changes
3643 * @notify: context as to what irq was changed
3644 * @mask: the new affinity mask
3645 *
3646 * This is a callback function used by the irq_set_affinity_notifier function
3647 * so that we may register to receive changes to the irq affinity masks.
3648 **/
3649 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3650 const cpumask_t *mask)
3651 {
3652 struct i40e_q_vector *q_vector =
3653 container_of(notify, struct i40e_q_vector, affinity_notify);
3654
3655 cpumask_copy(&q_vector->affinity_mask, mask);
3656 }
3657
3658 /**
3659 * i40e_irq_affinity_release - Callback for affinity notifier release
3660 * @ref: internal core kernel usage
3661 *
3662 * This is a callback function used by the irq_set_affinity_notifier function
3663 * to inform the current notification subscriber that they will no longer
3664 * receive notifications.
3665 **/
3666 static void i40e_irq_affinity_release(struct kref *ref) {}
3667
3668 /**
3669 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3670 * @vsi: the VSI being configured
3671 * @basename: name for the vector
3672 *
3673 * Allocates MSI-X vectors and requests interrupts from the kernel.
3674 **/
3675 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3676 {
3677 int q_vectors = vsi->num_q_vectors;
3678 struct i40e_pf *pf = vsi->back;
3679 int base = vsi->base_vector;
3680 int rx_int_idx = 0;
3681 int tx_int_idx = 0;
3682 int vector, err;
3683 int irq_num;
3684 int cpu;
3685
3686 for (vector = 0; vector < q_vectors; vector++) {
3687 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
3688
3689 irq_num = pf->msix_entries[base + vector].vector;
3690
3691 if (q_vector->tx.ring && q_vector->rx.ring) {
3692 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3693 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3694 tx_int_idx++;
3695 } else if (q_vector->rx.ring) {
3696 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3697 "%s-%s-%d", basename, "rx", rx_int_idx++);
3698 } else if (q_vector->tx.ring) {
3699 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3700 "%s-%s-%d", basename, "tx", tx_int_idx++);
3701 } else {
3702 /* skip this unused q_vector */
3703 continue;
3704 }
3705 err = request_irq(irq_num,
3706 vsi->irq_handler,
3707 0,
3708 q_vector->name,
3709 q_vector);
3710 if (err) {
3711 dev_info(&pf->pdev->dev,
3712 "MSIX request_irq failed, error: %d\n", err);
3713 goto free_queue_irqs;
3714 }
3715
3716 /* register for affinity change notifications */
3717 q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3718 q_vector->affinity_notify.release = i40e_irq_affinity_release;
3719 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
3720 /* Spread affinity hints out across online CPUs.
3721 *
3722 * get_cpu_mask returns a static constant mask with
3723 * a permanent lifetime so it's ok to pass to
3724 * irq_set_affinity_hint without making a copy.
3725 */
3726 cpu = cpumask_local_spread(q_vector->v_idx, -1);
3727 irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
3728 }
3729
3730 vsi->irqs_ready = true;
3731 return 0;
3732
3733 free_queue_irqs:
3734 while (vector) {
3735 vector--;
3736 irq_num = pf->msix_entries[base + vector].vector;
3737 irq_set_affinity_notifier(irq_num, NULL);
3738 irq_set_affinity_hint(irq_num, NULL);
3739 free_irq(irq_num, &vsi->q_vectors[vector]);
3740 }
3741 return err;
3742 }
3743
3744 /**
3745 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3746 * @vsi: the VSI being un-configured
3747 **/
3748 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3749 {
3750 struct i40e_pf *pf = vsi->back;
3751 struct i40e_hw *hw = &pf->hw;
3752 int base = vsi->base_vector;
3753 int i;
3754
3755 /* disable interrupt causation from each queue */
3756 for (i = 0; i < vsi->num_queue_pairs; i++) {
3757 u32 val;
3758
3759 val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
3760 val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3761 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
3762
3763 val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
3764 val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3765 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
3766
3767 if (!i40e_enabled_xdp_vsi(vsi))
3768 continue;
3769 wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
3770 }
3771
3772 /* disable each interrupt */
3773 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3774 for (i = vsi->base_vector;
3775 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3776 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3777
3778 i40e_flush(hw);
3779 for (i = 0; i < vsi->num_q_vectors; i++)
3780 synchronize_irq(pf->msix_entries[i + base].vector);
3781 } else {
3782 /* Legacy and MSI mode - this stops all interrupt handling */
3783 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3784 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3785 i40e_flush(hw);
3786 synchronize_irq(pf->pdev->irq);
3787 }
3788 }
3789
3790 /**
3791 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3792 * @vsi: the VSI being configured
3793 **/
3794 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3795 {
3796 struct i40e_pf *pf = vsi->back;
3797 int i;
3798
3799 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3800 for (i = 0; i < vsi->num_q_vectors; i++)
3801 i40e_irq_dynamic_enable(vsi, i);
3802 } else {
3803 i40e_irq_dynamic_enable_icr0(pf);
3804 }
3805
3806 i40e_flush(&pf->hw);
3807 return 0;
3808 }
3809
3810 /**
3811 * i40e_free_misc_vector - Free the vector that handles non-queue events
3812 * @pf: board private structure
3813 **/
3814 static void i40e_free_misc_vector(struct i40e_pf *pf)
3815 {
3816 /* Disable ICR 0 */
3817 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3818 i40e_flush(&pf->hw);
3819
3820 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
3821 synchronize_irq(pf->msix_entries[0].vector);
3822 free_irq(pf->msix_entries[0].vector, pf);
3823 clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
3824 }
3825 }
3826
3827 /**
3828 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3829 * @irq: interrupt number
3830 * @data: pointer to a q_vector
3831 *
3832 * This is the handler used for all MSI/Legacy interrupts, and deals
3833 * with both queue and non-queue interrupts. This is also used in
3834 * MSIX mode to handle the non-queue interrupts.
3835 **/
3836 static irqreturn_t i40e_intr(int irq, void *data)
3837 {
3838 struct i40e_pf *pf = (struct i40e_pf *)data;
3839 struct i40e_hw *hw = &pf->hw;
3840 irqreturn_t ret = IRQ_NONE;
3841 u32 icr0, icr0_remaining;
3842 u32 val, ena_mask;
3843
3844 icr0 = rd32(hw, I40E_PFINT_ICR0);
3845 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
3846
3847 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3848 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
3849 goto enable_intr;
3850
3851 /* if interrupt but no bits showing, must be SWINT */
3852 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3853 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3854 pf->sw_int_count++;
3855
3856 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3857 (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3858 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3859 dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
3860 set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
3861 }
3862
3863 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3864 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
3865 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3866 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3867
3868 /* We do not have a way to disarm Queue causes while leaving
3869 * interrupt enabled for all other causes, ideally
3870 * interrupt should be disabled while we are in NAPI but
3871 * this is not a performance path and napi_schedule()
3872 * can deal with rescheduling.
3873 */
3874 if (!test_bit(__I40E_DOWN, pf->state))
3875 napi_schedule_irqoff(&q_vector->napi);
3876 }
3877
3878 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3879 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3880 set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
3881 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
3882 }
3883
3884 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3885 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3886 set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
3887 }
3888
3889 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3890 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3891 set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
3892 }
3893
3894 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3895 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
3896 set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
3897 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3898 val = rd32(hw, I40E_GLGEN_RSTAT);
3899 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3900 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
3901 if (val == I40E_RESET_CORER) {
3902 pf->corer_count++;
3903 } else if (val == I40E_RESET_GLOBR) {
3904 pf->globr_count++;
3905 } else if (val == I40E_RESET_EMPR) {
3906 pf->empr_count++;
3907 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
3908 }
3909 }
3910
3911 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3912 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3913 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
3914 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3915 rd32(hw, I40E_PFHMC_ERRORINFO),
3916 rd32(hw, I40E_PFHMC_ERRORDATA));
3917 }
3918
3919 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3920 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3921
3922 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
3923 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3924 i40e_ptp_tx_hwtstamp(pf);
3925 }
3926 }
3927
3928 /* If a critical error is pending we have no choice but to reset the
3929 * device.
3930 * Report and mask out any remaining unexpected interrupts.
3931 */
3932 icr0_remaining = icr0 & ena_mask;
3933 if (icr0_remaining) {
3934 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3935 icr0_remaining);
3936 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
3937 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
3938 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
3939 dev_info(&pf->pdev->dev, "device will be reset\n");
3940 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
3941 i40e_service_event_schedule(pf);
3942 }
3943 ena_mask &= ~icr0_remaining;
3944 }
3945 ret = IRQ_HANDLED;
3946
3947 enable_intr:
3948 /* re-enable interrupt causes */
3949 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
3950 if (!test_bit(__I40E_DOWN, pf->state)) {
3951 i40e_service_event_schedule(pf);
3952 i40e_irq_dynamic_enable_icr0(pf);
3953 }
3954
3955 return ret;
3956 }
3957
3958 /**
3959 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3960 * @tx_ring: tx ring to clean
3961 * @budget: how many cleans we're allowed
3962 *
3963 * Returns true if there's any budget left (e.g. the clean is finished)
3964 **/
3965 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3966 {
3967 struct i40e_vsi *vsi = tx_ring->vsi;
3968 u16 i = tx_ring->next_to_clean;
3969 struct i40e_tx_buffer *tx_buf;
3970 struct i40e_tx_desc *tx_desc;
3971
3972 tx_buf = &tx_ring->tx_bi[i];
3973 tx_desc = I40E_TX_DESC(tx_ring, i);
3974 i -= tx_ring->count;
3975
3976 do {
3977 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3978
3979 /* if next_to_watch is not set then there is no work pending */
3980 if (!eop_desc)
3981 break;
3982
3983 /* prevent any other reads prior to eop_desc */
3984 smp_rmb();
3985
3986 /* if the descriptor isn't done, no work yet to do */
3987 if (!(eop_desc->cmd_type_offset_bsz &
3988 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3989 break;
3990
3991 /* clear next_to_watch to prevent false hangs */
3992 tx_buf->next_to_watch = NULL;
3993
3994 tx_desc->buffer_addr = 0;
3995 tx_desc->cmd_type_offset_bsz = 0;
3996 /* move past filter desc */
3997 tx_buf++;
3998 tx_desc++;
3999 i++;
4000 if (unlikely(!i)) {
4001 i -= tx_ring->count;
4002 tx_buf = tx_ring->tx_bi;
4003 tx_desc = I40E_TX_DESC(tx_ring, 0);
4004 }
4005 /* unmap skb header data */
4006 dma_unmap_single(tx_ring->dev,
4007 dma_unmap_addr(tx_buf, dma),
4008 dma_unmap_len(tx_buf, len),
4009 DMA_TO_DEVICE);
4010 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4011 kfree(tx_buf->raw_buf);
4012
4013 tx_buf->raw_buf = NULL;
4014 tx_buf->tx_flags = 0;
4015 tx_buf->next_to_watch = NULL;
4016 dma_unmap_len_set(tx_buf, len, 0);
4017 tx_desc->buffer_addr = 0;
4018 tx_desc->cmd_type_offset_bsz = 0;
4019
4020 /* move us past the eop_desc for start of next FD desc */
4021 tx_buf++;
4022 tx_desc++;
4023 i++;
4024 if (unlikely(!i)) {
4025 i -= tx_ring->count;
4026 tx_buf = tx_ring->tx_bi;
4027 tx_desc = I40E_TX_DESC(tx_ring, 0);
4028 }
4029
4030 /* update budget accounting */
4031 budget--;
4032 } while (likely(budget));
4033
4034 i += tx_ring->count;
4035 tx_ring->next_to_clean = i;
4036
4037 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4038 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4039
4040 return budget > 0;
4041 }
4042
4043 /**
4044 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4045 * @irq: interrupt number
4046 * @data: pointer to a q_vector
4047 **/
4048 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4049 {
4050 struct i40e_q_vector *q_vector = data;
4051 struct i40e_vsi *vsi;
4052
4053 if (!q_vector->tx.ring)
4054 return IRQ_HANDLED;
4055
4056 vsi = q_vector->tx.ring->vsi;
4057 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4058
4059 return IRQ_HANDLED;
4060 }
4061
4062 /**
4063 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4064 * @vsi: the VSI being configured
4065 * @v_idx: vector index
4066 * @qp_idx: queue pair index
4067 **/
4068 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4069 {
4070 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4071 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4072 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4073
4074 tx_ring->q_vector = q_vector;
4075 tx_ring->next = q_vector->tx.ring;
4076 q_vector->tx.ring = tx_ring;
4077 q_vector->tx.count++;
4078
4079 /* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4080 if (i40e_enabled_xdp_vsi(vsi)) {
4081 struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4082
4083 xdp_ring->q_vector = q_vector;
4084 xdp_ring->next = q_vector->tx.ring;
4085 q_vector->tx.ring = xdp_ring;
4086 q_vector->tx.count++;
4087 }
4088
4089 rx_ring->q_vector = q_vector;
4090 rx_ring->next = q_vector->rx.ring;
4091 q_vector->rx.ring = rx_ring;
4092 q_vector->rx.count++;
4093 }
4094
4095 /**
4096 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4097 * @vsi: the VSI being configured
4098 *
4099 * This function maps descriptor rings to the queue-specific vectors
4100 * we were allotted through the MSI-X enabling code. Ideally, we'd have
4101 * one vector per queue pair, but on a constrained vector budget, we
4102 * group the queue pairs as "efficiently" as possible.
4103 **/
4104 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4105 {
4106 int qp_remaining = vsi->num_queue_pairs;
4107 int q_vectors = vsi->num_q_vectors;
4108 int num_ringpairs;
4109 int v_start = 0;
4110 int qp_idx = 0;
4111
4112 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4113 * group them so there are multiple queues per vector.
4114 * It is also important to go through all the vectors available to be
4115 * sure that if we don't use all the vectors, that the remaining vectors
4116 * are cleared. This is especially important when decreasing the
4117 * number of queues in use.
4118 */
4119 for (; v_start < q_vectors; v_start++) {
4120 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4121
4122 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4123
4124 q_vector->num_ringpairs = num_ringpairs;
4125
4126 q_vector->rx.count = 0;
4127 q_vector->tx.count = 0;
4128 q_vector->rx.ring = NULL;
4129 q_vector->tx.ring = NULL;
4130
4131 while (num_ringpairs--) {
4132 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4133 qp_idx++;
4134 qp_remaining--;
4135 }
4136 }
4137 }
4138
4139 /**
4140 * i40e_vsi_request_irq - Request IRQ from the OS
4141 * @vsi: the VSI being configured
4142 * @basename: name for the vector
4143 **/
4144 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4145 {
4146 struct i40e_pf *pf = vsi->back;
4147 int err;
4148
4149 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4150 err = i40e_vsi_request_irq_msix(vsi, basename);
4151 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4152 err = request_irq(pf->pdev->irq, i40e_intr, 0,
4153 pf->int_name, pf);
4154 else
4155 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4156 pf->int_name, pf);
4157
4158 if (err)
4159 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4160
4161 return err;
4162 }
4163
4164 #ifdef CONFIG_NET_POLL_CONTROLLER
4165 /**
4166 * i40e_netpoll - A Polling 'interrupt' handler
4167 * @netdev: network interface device structure
4168 *
4169 * This is used by netconsole to send skbs without having to re-enable
4170 * interrupts. It's not called while the normal interrupt routine is executing.
4171 **/
4172 static void i40e_netpoll(struct net_device *netdev)
4173 {
4174 struct i40e_netdev_priv *np = netdev_priv(netdev);
4175 struct i40e_vsi *vsi = np->vsi;
4176 struct i40e_pf *pf = vsi->back;
4177 int i;
4178
4179 /* if interface is down do nothing */
4180 if (test_bit(__I40E_VSI_DOWN, vsi->state))
4181 return;
4182
4183 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4184 for (i = 0; i < vsi->num_q_vectors; i++)
4185 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4186 } else {
4187 i40e_intr(pf->pdev->irq, netdev);
4188 }
4189 }
4190 #endif
4191
4192 #define I40E_QTX_ENA_WAIT_COUNT 50
4193
4194 /**
4195 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4196 * @pf: the PF being configured
4197 * @pf_q: the PF queue
4198 * @enable: enable or disable state of the queue
4199 *
4200 * This routine will wait for the given Tx queue of the PF to reach the
4201 * enabled or disabled state.
4202 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4203 * multiple retries; else will return 0 in case of success.
4204 **/
4205 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4206 {
4207 int i;
4208 u32 tx_reg;
4209
4210 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4211 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4212 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4213 break;
4214
4215 usleep_range(10, 20);
4216 }
4217 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4218 return -ETIMEDOUT;
4219
4220 return 0;
4221 }
4222
4223 /**
4224 * i40e_control_tx_q - Start or stop a particular Tx queue
4225 * @pf: the PF structure
4226 * @pf_q: the PF queue to configure
4227 * @enable: start or stop the queue
4228 *
4229 * This function enables or disables a single queue. Note that any delay
4230 * required after the operation is expected to be handled by the caller of
4231 * this function.
4232 **/
4233 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4234 {
4235 struct i40e_hw *hw = &pf->hw;
4236 u32 tx_reg;
4237 int i;
4238
4239 /* warn the TX unit of coming changes */
4240 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4241 if (!enable)
4242 usleep_range(10, 20);
4243
4244 for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4245 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4246 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4247 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4248 break;
4249 usleep_range(1000, 2000);
4250 }
4251
4252 /* Skip if the queue is already in the requested state */
4253 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4254 return;
4255
4256 /* turn on/off the queue */
4257 if (enable) {
4258 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4259 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4260 } else {
4261 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4262 }
4263
4264 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4265 }
4266
4267 /**
4268 * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4269 * @seid: VSI SEID
4270 * @pf: the PF structure
4271 * @pf_q: the PF queue to configure
4272 * @is_xdp: true if the queue is used for XDP
4273 * @enable: start or stop the queue
4274 **/
4275 static int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4276 bool is_xdp, bool enable)
4277 {
4278 int ret;
4279
4280 i40e_control_tx_q(pf, pf_q, enable);
4281
4282 /* wait for the change to finish */
4283 ret = i40e_pf_txq_wait(pf, pf_q, enable);
4284 if (ret) {
4285 dev_info(&pf->pdev->dev,
4286 "VSI seid %d %sTx ring %d %sable timeout\n",
4287 seid, (is_xdp ? "XDP " : ""), pf_q,
4288 (enable ? "en" : "dis"));
4289 }
4290
4291 return ret;
4292 }
4293
4294 /**
4295 * i40e_vsi_control_tx - Start or stop a VSI's rings
4296 * @vsi: the VSI being configured
4297 * @enable: start or stop the rings
4298 **/
4299 static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
4300 {
4301 struct i40e_pf *pf = vsi->back;
4302 int i, pf_q, ret = 0;
4303
4304 pf_q = vsi->base_queue;
4305 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4306 ret = i40e_control_wait_tx_q(vsi->seid, pf,
4307 pf_q,
4308 false /*is xdp*/, enable);
4309 if (ret)
4310 break;
4311
4312 if (!i40e_enabled_xdp_vsi(vsi))
4313 continue;
4314
4315 ret = i40e_control_wait_tx_q(vsi->seid, pf,
4316 pf_q + vsi->alloc_queue_pairs,
4317 true /*is xdp*/, enable);
4318 if (ret)
4319 break;
4320 }
4321
4322 return ret;
4323 }
4324
4325 /**
4326 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4327 * @pf: the PF being configured
4328 * @pf_q: the PF queue
4329 * @enable: enable or disable state of the queue
4330 *
4331 * This routine will wait for the given Rx queue of the PF to reach the
4332 * enabled or disabled state.
4333 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4334 * multiple retries; else will return 0 in case of success.
4335 **/
4336 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4337 {
4338 int i;
4339 u32 rx_reg;
4340
4341 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4342 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4343 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4344 break;
4345
4346 usleep_range(10, 20);
4347 }
4348 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4349 return -ETIMEDOUT;
4350
4351 return 0;
4352 }
4353
4354 /**
4355 * i40e_control_rx_q - Start or stop a particular Rx queue
4356 * @pf: the PF structure
4357 * @pf_q: the PF queue to configure
4358 * @enable: start or stop the queue
4359 *
4360 * This function enables or disables a single queue. Note that any delay
4361 * required after the operation is expected to be handled by the caller of
4362 * this function.
4363 **/
4364 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4365 {
4366 struct i40e_hw *hw = &pf->hw;
4367 u32 rx_reg;
4368 int i;
4369
4370 for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4371 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4372 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4373 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4374 break;
4375 usleep_range(1000, 2000);
4376 }
4377
4378 /* Skip if the queue is already in the requested state */
4379 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4380 return;
4381
4382 /* turn on/off the queue */
4383 if (enable)
4384 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4385 else
4386 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4387
4388 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4389 }
4390
4391 /**
4392 * i40e_vsi_control_rx - Start or stop a VSI's rings
4393 * @vsi: the VSI being configured
4394 * @enable: start or stop the rings
4395 **/
4396 static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4397 {
4398 struct i40e_pf *pf = vsi->back;
4399 int i, pf_q, ret = 0;
4400
4401 pf_q = vsi->base_queue;
4402 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4403 i40e_control_rx_q(pf, pf_q, enable);
4404
4405 /* wait for the change to finish */
4406 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4407 if (ret) {
4408 dev_info(&pf->pdev->dev,
4409 "VSI seid %d Rx ring %d %sable timeout\n",
4410 vsi->seid, pf_q, (enable ? "en" : "dis"));
4411 break;
4412 }
4413 }
4414
4415 /* Due to HW errata, on Rx disable only, the register can indicate done
4416 * before it really is. Needs 50ms to be sure
4417 */
4418 if (!enable)
4419 mdelay(50);
4420
4421 return ret;
4422 }
4423
4424 /**
4425 * i40e_vsi_start_rings - Start a VSI's rings
4426 * @vsi: the VSI being configured
4427 **/
4428 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4429 {
4430 int ret = 0;
4431
4432 /* do rx first for enable and last for disable */
4433 ret = i40e_vsi_control_rx(vsi, true);
4434 if (ret)
4435 return ret;
4436 ret = i40e_vsi_control_tx(vsi, true);
4437
4438 return ret;
4439 }
4440
4441 /**
4442 * i40e_vsi_stop_rings - Stop a VSI's rings
4443 * @vsi: the VSI being configured
4444 **/
4445 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4446 {
4447 /* When port TX is suspended, don't wait */
4448 if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4449 return i40e_vsi_stop_rings_no_wait(vsi);
4450
4451 /* do rx first for enable and last for disable
4452 * Ignore return value, we need to shutdown whatever we can
4453 */
4454 i40e_vsi_control_tx(vsi, false);
4455 i40e_vsi_control_rx(vsi, false);
4456 }
4457
4458 /**
4459 * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4460 * @vsi: the VSI being shutdown
4461 *
4462 * This function stops all the rings for a VSI but does not delay to verify
4463 * that rings have been disabled. It is expected that the caller is shutting
4464 * down multiple VSIs at once and will delay together for all the VSIs after
4465 * initiating the shutdown. This is particularly useful for shutting down lots
4466 * of VFs together. Otherwise, a large delay can be incurred while configuring
4467 * each VSI in serial.
4468 **/
4469 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4470 {
4471 struct i40e_pf *pf = vsi->back;
4472 int i, pf_q;
4473
4474 pf_q = vsi->base_queue;
4475 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4476 i40e_control_tx_q(pf, pf_q, false);
4477 i40e_control_rx_q(pf, pf_q, false);
4478 }
4479 }
4480
4481 /**
4482 * i40e_vsi_free_irq - Free the irq association with the OS
4483 * @vsi: the VSI being configured
4484 **/
4485 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4486 {
4487 struct i40e_pf *pf = vsi->back;
4488 struct i40e_hw *hw = &pf->hw;
4489 int base = vsi->base_vector;
4490 u32 val, qp;
4491 int i;
4492
4493 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4494 if (!vsi->q_vectors)
4495 return;
4496
4497 if (!vsi->irqs_ready)
4498 return;
4499
4500 vsi->irqs_ready = false;
4501 for (i = 0; i < vsi->num_q_vectors; i++) {
4502 int irq_num;
4503 u16 vector;
4504
4505 vector = i + base;
4506 irq_num = pf->msix_entries[vector].vector;
4507
4508 /* free only the irqs that were actually requested */
4509 if (!vsi->q_vectors[i] ||
4510 !vsi->q_vectors[i]->num_ringpairs)
4511 continue;
4512
4513 /* clear the affinity notifier in the IRQ descriptor */
4514 irq_set_affinity_notifier(irq_num, NULL);
4515 /* remove our suggested affinity mask for this IRQ */
4516 irq_set_affinity_hint(irq_num, NULL);
4517 synchronize_irq(irq_num);
4518 free_irq(irq_num, vsi->q_vectors[i]);
4519
4520 /* Tear down the interrupt queue link list
4521 *
4522 * We know that they come in pairs and always
4523 * the Rx first, then the Tx. To clear the
4524 * link list, stick the EOL value into the
4525 * next_q field of the registers.
4526 */
4527 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4528 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4529 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4530 val |= I40E_QUEUE_END_OF_LIST
4531 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4532 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4533
4534 while (qp != I40E_QUEUE_END_OF_LIST) {
4535 u32 next;
4536
4537 val = rd32(hw, I40E_QINT_RQCTL(qp));
4538
4539 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4540 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4541 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4542 I40E_QINT_RQCTL_INTEVENT_MASK);
4543
4544 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4545 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4546
4547 wr32(hw, I40E_QINT_RQCTL(qp), val);
4548
4549 val = rd32(hw, I40E_QINT_TQCTL(qp));
4550
4551 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4552 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4553
4554 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4555 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4556 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4557 I40E_QINT_TQCTL_INTEVENT_MASK);
4558
4559 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4560 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4561
4562 wr32(hw, I40E_QINT_TQCTL(qp), val);
4563 qp = next;
4564 }
4565 }
4566 } else {
4567 free_irq(pf->pdev->irq, pf);
4568
4569 val = rd32(hw, I40E_PFINT_LNKLST0);
4570 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4571 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4572 val |= I40E_QUEUE_END_OF_LIST
4573 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4574 wr32(hw, I40E_PFINT_LNKLST0, val);
4575
4576 val = rd32(hw, I40E_QINT_RQCTL(qp));
4577 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4578 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4579 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4580 I40E_QINT_RQCTL_INTEVENT_MASK);
4581
4582 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4583 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4584
4585 wr32(hw, I40E_QINT_RQCTL(qp), val);
4586
4587 val = rd32(hw, I40E_QINT_TQCTL(qp));
4588
4589 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4590 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4591 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4592 I40E_QINT_TQCTL_INTEVENT_MASK);
4593
4594 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4595 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4596
4597 wr32(hw, I40E_QINT_TQCTL(qp), val);
4598 }
4599 }
4600
4601 /**
4602 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4603 * @vsi: the VSI being configured
4604 * @v_idx: Index of vector to be freed
4605 *
4606 * This function frees the memory allocated to the q_vector. In addition if
4607 * NAPI is enabled it will delete any references to the NAPI struct prior
4608 * to freeing the q_vector.
4609 **/
4610 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4611 {
4612 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4613 struct i40e_ring *ring;
4614
4615 if (!q_vector)
4616 return;
4617
4618 /* disassociate q_vector from rings */
4619 i40e_for_each_ring(ring, q_vector->tx)
4620 ring->q_vector = NULL;
4621
4622 i40e_for_each_ring(ring, q_vector->rx)
4623 ring->q_vector = NULL;
4624
4625 /* only VSI w/ an associated netdev is set up w/ NAPI */
4626 if (vsi->netdev)
4627 netif_napi_del(&q_vector->napi);
4628
4629 vsi->q_vectors[v_idx] = NULL;
4630
4631 kfree_rcu(q_vector, rcu);
4632 }
4633
4634 /**
4635 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4636 * @vsi: the VSI being un-configured
4637 *
4638 * This frees the memory allocated to the q_vectors and
4639 * deletes references to the NAPI struct.
4640 **/
4641 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4642 {
4643 int v_idx;
4644
4645 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4646 i40e_free_q_vector(vsi, v_idx);
4647 }
4648
4649 /**
4650 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4651 * @pf: board private structure
4652 **/
4653 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4654 {
4655 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4656 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4657 pci_disable_msix(pf->pdev);
4658 kfree(pf->msix_entries);
4659 pf->msix_entries = NULL;
4660 kfree(pf->irq_pile);
4661 pf->irq_pile = NULL;
4662 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4663 pci_disable_msi(pf->pdev);
4664 }
4665 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4666 }
4667
4668 /**
4669 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4670 * @pf: board private structure
4671 *
4672 * We go through and clear interrupt specific resources and reset the structure
4673 * to pre-load conditions
4674 **/
4675 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4676 {
4677 int i;
4678
4679 i40e_free_misc_vector(pf);
4680
4681 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4682 I40E_IWARP_IRQ_PILE_ID);
4683
4684 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
4685 for (i = 0; i < pf->num_alloc_vsi; i++)
4686 if (pf->vsi[i])
4687 i40e_vsi_free_q_vectors(pf->vsi[i]);
4688 i40e_reset_interrupt_capability(pf);
4689 }
4690
4691 /**
4692 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4693 * @vsi: the VSI being configured
4694 **/
4695 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4696 {
4697 int q_idx;
4698
4699 if (!vsi->netdev)
4700 return;
4701
4702 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4703 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4704
4705 if (q_vector->rx.ring || q_vector->tx.ring)
4706 napi_enable(&q_vector->napi);
4707 }
4708 }
4709
4710 /**
4711 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4712 * @vsi: the VSI being configured
4713 **/
4714 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4715 {
4716 int q_idx;
4717
4718 if (!vsi->netdev)
4719 return;
4720
4721 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4722 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4723
4724 if (q_vector->rx.ring || q_vector->tx.ring)
4725 napi_disable(&q_vector->napi);
4726 }
4727 }
4728
4729 /**
4730 * i40e_vsi_close - Shut down a VSI
4731 * @vsi: the vsi to be quelled
4732 **/
4733 static void i40e_vsi_close(struct i40e_vsi *vsi)
4734 {
4735 struct i40e_pf *pf = vsi->back;
4736 if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
4737 i40e_down(vsi);
4738 i40e_vsi_free_irq(vsi);
4739 i40e_vsi_free_tx_resources(vsi);
4740 i40e_vsi_free_rx_resources(vsi);
4741 vsi->current_netdev_flags = 0;
4742 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
4743 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4744 pf->flags |= I40E_FLAG_CLIENT_RESET;
4745 }
4746
4747 /**
4748 * i40e_quiesce_vsi - Pause a given VSI
4749 * @vsi: the VSI being paused
4750 **/
4751 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4752 {
4753 if (test_bit(__I40E_VSI_DOWN, vsi->state))
4754 return;
4755
4756 set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
4757 if (vsi->netdev && netif_running(vsi->netdev))
4758 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
4759 else
4760 i40e_vsi_close(vsi);
4761 }
4762
4763 /**
4764 * i40e_unquiesce_vsi - Resume a given VSI
4765 * @vsi: the VSI being resumed
4766 **/
4767 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4768 {
4769 if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
4770 return;
4771
4772 if (vsi->netdev && netif_running(vsi->netdev))
4773 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4774 else
4775 i40e_vsi_open(vsi); /* this clears the DOWN bit */
4776 }
4777
4778 /**
4779 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4780 * @pf: the PF
4781 **/
4782 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4783 {
4784 int v;
4785
4786 for (v = 0; v < pf->num_alloc_vsi; v++) {
4787 if (pf->vsi[v])
4788 i40e_quiesce_vsi(pf->vsi[v]);
4789 }
4790 }
4791
4792 /**
4793 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4794 * @pf: the PF
4795 **/
4796 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4797 {
4798 int v;
4799
4800 for (v = 0; v < pf->num_alloc_vsi; v++) {
4801 if (pf->vsi[v])
4802 i40e_unquiesce_vsi(pf->vsi[v]);
4803 }
4804 }
4805
4806 /**
4807 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
4808 * @vsi: the VSI being configured
4809 *
4810 * Wait until all queues on a given VSI have been disabled.
4811 **/
4812 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
4813 {
4814 struct i40e_pf *pf = vsi->back;
4815 int i, pf_q, ret;
4816
4817 pf_q = vsi->base_queue;
4818 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4819 /* Check and wait for the Tx queue */
4820 ret = i40e_pf_txq_wait(pf, pf_q, false);
4821 if (ret) {
4822 dev_info(&pf->pdev->dev,
4823 "VSI seid %d Tx ring %d disable timeout\n",
4824 vsi->seid, pf_q);
4825 return ret;
4826 }
4827
4828 if (!i40e_enabled_xdp_vsi(vsi))
4829 goto wait_rx;
4830
4831 /* Check and wait for the XDP Tx queue */
4832 ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
4833 false);
4834 if (ret) {
4835 dev_info(&pf->pdev->dev,
4836 "VSI seid %d XDP Tx ring %d disable timeout\n",
4837 vsi->seid, pf_q);
4838 return ret;
4839 }
4840 wait_rx:
4841 /* Check and wait for the Rx queue */
4842 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4843 if (ret) {
4844 dev_info(&pf->pdev->dev,
4845 "VSI seid %d Rx ring %d disable timeout\n",
4846 vsi->seid, pf_q);
4847 return ret;
4848 }
4849 }
4850
4851 return 0;
4852 }
4853
4854 #ifdef CONFIG_I40E_DCB
4855 /**
4856 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
4857 * @pf: the PF
4858 *
4859 * This function waits for the queues to be in disabled state for all the
4860 * VSIs that are managed by this PF.
4861 **/
4862 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
4863 {
4864 int v, ret = 0;
4865
4866 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4867 if (pf->vsi[v]) {
4868 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
4869 if (ret)
4870 break;
4871 }
4872 }
4873
4874 return ret;
4875 }
4876
4877 #endif
4878
4879 /**
4880 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4881 * @q_idx: TX queue number
4882 * @vsi: Pointer to VSI struct
4883 *
4884 * This function checks specified queue for given VSI. Detects hung condition.
4885 * We proactively detect hung TX queues by checking if interrupts are disabled
4886 * but there are pending descriptors. If it appears hung, attempt to recover
4887 * by triggering a SW interrupt.
4888 **/
4889 static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4890 {
4891 struct i40e_ring *tx_ring = NULL;
4892 struct i40e_pf *pf;
4893 u32 val, tx_pending;
4894 int i;
4895
4896 pf = vsi->back;
4897
4898 /* now that we have an index, find the tx_ring struct */
4899 for (i = 0; i < vsi->num_queue_pairs; i++) {
4900 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4901 if (q_idx == vsi->tx_rings[i]->queue_index) {
4902 tx_ring = vsi->tx_rings[i];
4903 break;
4904 }
4905 }
4906 }
4907
4908 if (!tx_ring)
4909 return;
4910
4911 /* Read interrupt register */
4912 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4913 val = rd32(&pf->hw,
4914 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4915 tx_ring->vsi->base_vector - 1));
4916 else
4917 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4918
4919 tx_pending = i40e_get_tx_pending(tx_ring);
4920
4921 /* Interrupts are disabled and TX pending is non-zero,
4922 * trigger the SW interrupt (don't wait). Worst case
4923 * there will be one extra interrupt which may result
4924 * into not cleaning any queues because queues are cleaned.
4925 */
4926 if (tx_pending && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK)))
4927 i40e_force_wb(vsi, tx_ring->q_vector);
4928 }
4929
4930 /**
4931 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4932 * @pf: pointer to PF struct
4933 *
4934 * LAN VSI has netdev and netdev has TX queues. This function is to check
4935 * each of those TX queues if they are hung, trigger recovery by issuing
4936 * SW interrupt.
4937 **/
4938 static void i40e_detect_recover_hung(struct i40e_pf *pf)
4939 {
4940 struct net_device *netdev;
4941 struct i40e_vsi *vsi;
4942 unsigned int i;
4943
4944 /* Only for LAN VSI */
4945 vsi = pf->vsi[pf->lan_vsi];
4946
4947 if (!vsi)
4948 return;
4949
4950 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
4951 if (test_bit(__I40E_VSI_DOWN, vsi->back->state) ||
4952 test_bit(__I40E_RESET_RECOVERY_PENDING, vsi->back->state))
4953 return;
4954
4955 /* Make sure type is MAIN VSI */
4956 if (vsi->type != I40E_VSI_MAIN)
4957 return;
4958
4959 netdev = vsi->netdev;
4960 if (!netdev)
4961 return;
4962
4963 /* Bail out if netif_carrier is not OK */
4964 if (!netif_carrier_ok(netdev))
4965 return;
4966
4967 /* Go thru' TX queues for netdev */
4968 for (i = 0; i < netdev->num_tx_queues; i++) {
4969 struct netdev_queue *q;
4970
4971 q = netdev_get_tx_queue(netdev, i);
4972 if (q)
4973 i40e_detect_recover_hung_queue(i, vsi);
4974 }
4975 }
4976
4977 /**
4978 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
4979 * @pf: pointer to PF
4980 *
4981 * Get TC map for ISCSI PF type that will include iSCSI TC
4982 * and LAN TC.
4983 **/
4984 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4985 {
4986 struct i40e_dcb_app_priority_table app;
4987 struct i40e_hw *hw = &pf->hw;
4988 u8 enabled_tc = 1; /* TC0 is always enabled */
4989 u8 tc, i;
4990 /* Get the iSCSI APP TLV */
4991 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4992
4993 for (i = 0; i < dcbcfg->numapps; i++) {
4994 app = dcbcfg->app[i];
4995 if (app.selector == I40E_APP_SEL_TCPIP &&
4996 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4997 tc = dcbcfg->etscfg.prioritytable[app.priority];
4998 enabled_tc |= BIT(tc);
4999 break;
5000 }
5001 }
5002
5003 return enabled_tc;
5004 }
5005
5006 /**
5007 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
5008 * @dcbcfg: the corresponding DCBx configuration structure
5009 *
5010 * Return the number of TCs from given DCBx configuration
5011 **/
5012 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5013 {
5014 int i, tc_unused = 0;
5015 u8 num_tc = 0;
5016 u8 ret = 0;
5017
5018 /* Scan the ETS Config Priority Table to find
5019 * traffic class enabled for a given priority
5020 * and create a bitmask of enabled TCs
5021 */
5022 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5023 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5024
5025 /* Now scan the bitmask to check for
5026 * contiguous TCs starting with TC0
5027 */
5028 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5029 if (num_tc & BIT(i)) {
5030 if (!tc_unused) {
5031 ret++;
5032 } else {
5033 pr_err("Non-contiguous TC - Disabling DCB\n");
5034 return 1;
5035 }
5036 } else {
5037 tc_unused = 1;
5038 }
5039 }
5040
5041 /* There is always at least TC0 */
5042 if (!ret)
5043 ret = 1;
5044
5045 return ret;
5046 }
5047
5048 /**
5049 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5050 * @dcbcfg: the corresponding DCBx configuration structure
5051 *
5052 * Query the current DCB configuration and return the number of
5053 * traffic classes enabled from the given DCBX config
5054 **/
5055 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5056 {
5057 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5058 u8 enabled_tc = 1;
5059 u8 i;
5060
5061 for (i = 0; i < num_tc; i++)
5062 enabled_tc |= BIT(i);
5063
5064 return enabled_tc;
5065 }
5066
5067 /**
5068 * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5069 * @pf: PF being queried
5070 *
5071 * Query the current MQPRIO configuration and return the number of
5072 * traffic classes enabled.
5073 **/
5074 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5075 {
5076 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5077 u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5078 u8 enabled_tc = 1, i;
5079
5080 for (i = 1; i < num_tc; i++)
5081 enabled_tc |= BIT(i);
5082 return enabled_tc;
5083 }
5084
5085 /**
5086 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5087 * @pf: PF being queried
5088 *
5089 * Return number of traffic classes enabled for the given PF
5090 **/
5091 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5092 {
5093 struct i40e_hw *hw = &pf->hw;
5094 u8 i, enabled_tc = 1;
5095 u8 num_tc = 0;
5096 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5097
5098 if (pf->flags & I40E_FLAG_TC_MQPRIO)
5099 return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5100
5101 /* If neither MQPRIO nor DCB is enabled, then always use single TC */
5102 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5103 return 1;
5104
5105 /* SFP mode will be enabled for all TCs on port */
5106 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5107 return i40e_dcb_get_num_tc(dcbcfg);
5108
5109 /* MFP mode return count of enabled TCs for this PF */
5110 if (pf->hw.func_caps.iscsi)
5111 enabled_tc = i40e_get_iscsi_tc_map(pf);
5112 else
5113 return 1; /* Only TC0 */
5114
5115 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5116 if (enabled_tc & BIT(i))
5117 num_tc++;
5118 }
5119 return num_tc;
5120 }
5121
5122 /**
5123 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
5124 * @pf: PF being queried
5125 *
5126 * Return a bitmap for enabled traffic classes for this PF.
5127 **/
5128 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5129 {
5130 if (pf->flags & I40E_FLAG_TC_MQPRIO)
5131 return i40e_mqprio_get_enabled_tc(pf);
5132
5133 /* If neither MQPRIO nor DCB is enabled for this PF then just return
5134 * default TC
5135 */
5136 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5137 return I40E_DEFAULT_TRAFFIC_CLASS;
5138
5139 /* SFP mode we want PF to be enabled for all TCs */
5140 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5141 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5142
5143 /* MFP enabled and iSCSI PF type */
5144 if (pf->hw.func_caps.iscsi)
5145 return i40e_get_iscsi_tc_map(pf);
5146 else
5147 return I40E_DEFAULT_TRAFFIC_CLASS;
5148 }
5149
5150 /**
5151 * i40e_vsi_get_bw_info - Query VSI BW Information
5152 * @vsi: the VSI being queried
5153 *
5154 * Returns 0 on success, negative value on failure
5155 **/
5156 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5157 {
5158 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5159 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5160 struct i40e_pf *pf = vsi->back;
5161 struct i40e_hw *hw = &pf->hw;
5162 i40e_status ret;
5163 u32 tc_bw_max;
5164 int i;
5165
5166 /* Get the VSI level BW configuration */
5167 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5168 if (ret) {
5169 dev_info(&pf->pdev->dev,
5170 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5171 i40e_stat_str(&pf->hw, ret),
5172 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5173 return -EINVAL;
5174 }
5175
5176 /* Get the VSI level BW configuration per TC */
5177 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5178 NULL);
5179 if (ret) {
5180 dev_info(&pf->pdev->dev,
5181 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5182 i40e_stat_str(&pf->hw, ret),
5183 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5184 return -EINVAL;
5185 }
5186
5187 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5188 dev_info(&pf->pdev->dev,
5189 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5190 bw_config.tc_valid_bits,
5191 bw_ets_config.tc_valid_bits);
5192 /* Still continuing */
5193 }
5194
5195 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5196 vsi->bw_max_quanta = bw_config.max_bw;
5197 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5198 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5199 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5200 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5201 vsi->bw_ets_limit_credits[i] =
5202 le16_to_cpu(bw_ets_config.credits[i]);
5203 /* 3 bits out of 4 for each TC */
5204 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5205 }
5206
5207 return 0;
5208 }
5209
5210 /**
5211 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5212 * @vsi: the VSI being configured
5213 * @enabled_tc: TC bitmap
5214 * @bw_credits: BW shared credits per TC
5215 *
5216 * Returns 0 on success, negative value on failure
5217 **/
5218 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5219 u8 *bw_share)
5220 {
5221 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5222 i40e_status ret;
5223 int i;
5224
5225 if (vsi->back->flags & I40E_FLAG_TC_MQPRIO)
5226 return 0;
5227 if (!vsi->mqprio_qopt.qopt.hw) {
5228 ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5229 if (ret)
5230 dev_info(&vsi->back->pdev->dev,
5231 "Failed to reset tx rate for vsi->seid %u\n",
5232 vsi->seid);
5233 return ret;
5234 }
5235 bw_data.tc_valid_bits = enabled_tc;
5236 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5237 bw_data.tc_bw_credits[i] = bw_share[i];
5238
5239 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
5240 NULL);
5241 if (ret) {
5242 dev_info(&vsi->back->pdev->dev,
5243 "AQ command Config VSI BW allocation per TC failed = %d\n",
5244 vsi->back->hw.aq.asq_last_status);
5245 return -EINVAL;
5246 }
5247
5248 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5249 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5250
5251 return 0;
5252 }
5253
5254 /**
5255 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5256 * @vsi: the VSI being configured
5257 * @enabled_tc: TC map to be enabled
5258 *
5259 **/
5260 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5261 {
5262 struct net_device *netdev = vsi->netdev;
5263 struct i40e_pf *pf = vsi->back;
5264 struct i40e_hw *hw = &pf->hw;
5265 u8 netdev_tc = 0;
5266 int i;
5267 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5268
5269 if (!netdev)
5270 return;
5271
5272 if (!enabled_tc) {
5273 netdev_reset_tc(netdev);
5274 return;
5275 }
5276
5277 /* Set up actual enabled TCs on the VSI */
5278 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5279 return;
5280
5281 /* set per TC queues for the VSI */
5282 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5283 /* Only set TC queues for enabled tcs
5284 *
5285 * e.g. For a VSI that has TC0 and TC3 enabled the
5286 * enabled_tc bitmap would be 0x00001001; the driver
5287 * will set the numtc for netdev as 2 that will be
5288 * referenced by the netdev layer as TC 0 and 1.
5289 */
5290 if (vsi->tc_config.enabled_tc & BIT(i))
5291 netdev_set_tc_queue(netdev,
5292 vsi->tc_config.tc_info[i].netdev_tc,
5293 vsi->tc_config.tc_info[i].qcount,
5294 vsi->tc_config.tc_info[i].qoffset);
5295 }
5296
5297 if (pf->flags & I40E_FLAG_TC_MQPRIO)
5298 return;
5299
5300 /* Assign UP2TC map for the VSI */
5301 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5302 /* Get the actual TC# for the UP */
5303 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5304 /* Get the mapped netdev TC# for the UP */
5305 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
5306 netdev_set_prio_tc_map(netdev, i, netdev_tc);
5307 }
5308 }
5309
5310 /**
5311 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5312 * @vsi: the VSI being configured
5313 * @ctxt: the ctxt buffer returned from AQ VSI update param command
5314 **/
5315 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5316 struct i40e_vsi_context *ctxt)
5317 {
5318 /* copy just the sections touched not the entire info
5319 * since not all sections are valid as returned by
5320 * update vsi params
5321 */
5322 vsi->info.mapping_flags = ctxt->info.mapping_flags;
5323 memcpy(&vsi->info.queue_mapping,
5324 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5325 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5326 sizeof(vsi->info.tc_mapping));
5327 }
5328
5329 /**
5330 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5331 * @vsi: VSI to be configured
5332 * @enabled_tc: TC bitmap
5333 *
5334 * This configures a particular VSI for TCs that are mapped to the
5335 * given TC bitmap. It uses default bandwidth share for TCs across
5336 * VSIs to configure TC for a particular VSI.
5337 *
5338 * NOTE:
5339 * It is expected that the VSI queues have been quisced before calling
5340 * this function.
5341 **/
5342 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5343 {
5344 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5345 struct i40e_vsi_context ctxt;
5346 int ret = 0;
5347 int i;
5348
5349 /* Check if enabled_tc is same as existing or new TCs */
5350 if (vsi->tc_config.enabled_tc == enabled_tc &&
5351 vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5352 return ret;
5353
5354 /* Enable ETS TCs with equal BW Share for now across all VSIs */
5355 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5356 if (enabled_tc & BIT(i))
5357 bw_share[i] = 1;
5358 }
5359
5360 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5361 if (ret) {
5362 dev_info(&vsi->back->pdev->dev,
5363 "Failed configuring TC map %d for VSI %d\n",
5364 enabled_tc, vsi->seid);
5365 goto out;
5366 }
5367
5368 /* Update Queue Pairs Mapping for currently enabled UPs */
5369 ctxt.seid = vsi->seid;
5370 ctxt.pf_num = vsi->back->hw.pf_id;
5371 ctxt.vf_num = 0;
5372 ctxt.uplink_seid = vsi->uplink_seid;
5373 ctxt.info = vsi->info;
5374 if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
5375 ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5376 if (ret)
5377 goto out;
5378 } else {
5379 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5380 }
5381
5382 /* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5383 * queues changed.
5384 */
5385 if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5386 vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5387 vsi->num_queue_pairs);
5388 ret = i40e_vsi_config_rss(vsi);
5389 if (ret) {
5390 dev_info(&vsi->back->pdev->dev,
5391 "Failed to reconfig rss for num_queues\n");
5392 return ret;
5393 }
5394 vsi->reconfig_rss = false;
5395 }
5396 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5397 ctxt.info.valid_sections |=
5398 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5399 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5400 }
5401
5402 /* Update the VSI after updating the VSI queue-mapping
5403 * information
5404 */
5405 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
5406 if (ret) {
5407 dev_info(&vsi->back->pdev->dev,
5408 "Update vsi tc config failed, err %s aq_err %s\n",
5409 i40e_stat_str(&vsi->back->hw, ret),
5410 i40e_aq_str(&vsi->back->hw,
5411 vsi->back->hw.aq.asq_last_status));
5412 goto out;
5413 }
5414 /* update the local VSI info with updated queue map */
5415 i40e_vsi_update_queue_map(vsi, &ctxt);
5416 vsi->info.valid_sections = 0;
5417
5418 /* Update current VSI BW information */
5419 ret = i40e_vsi_get_bw_info(vsi);
5420 if (ret) {
5421 dev_info(&vsi->back->pdev->dev,
5422 "Failed updating vsi bw info, err %s aq_err %s\n",
5423 i40e_stat_str(&vsi->back->hw, ret),
5424 i40e_aq_str(&vsi->back->hw,
5425 vsi->back->hw.aq.asq_last_status));
5426 goto out;
5427 }
5428
5429 /* Update the netdev TC setup */
5430 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5431 out:
5432 return ret;
5433 }
5434
5435 /**
5436 * i40e_get_link_speed - Returns link speed for the interface
5437 * @vsi: VSI to be configured
5438 *
5439 **/
5440 int i40e_get_link_speed(struct i40e_vsi *vsi)
5441 {
5442 struct i40e_pf *pf = vsi->back;
5443
5444 switch (pf->hw.phy.link_info.link_speed) {
5445 case I40E_LINK_SPEED_40GB:
5446 return 40000;
5447 case I40E_LINK_SPEED_25GB:
5448 return 25000;
5449 case I40E_LINK_SPEED_20GB:
5450 return 20000;
5451 case I40E_LINK_SPEED_10GB:
5452 return 10000;
5453 case I40E_LINK_SPEED_1GB:
5454 return 1000;
5455 default:
5456 return -EINVAL;
5457 }
5458 }
5459
5460 /**
5461 * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5462 * @vsi: VSI to be configured
5463 * @seid: seid of the channel/VSI
5464 * @max_tx_rate: max TX rate to be configured as BW limit
5465 *
5466 * Helper function to set BW limit for a given VSI
5467 **/
5468 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5469 {
5470 struct i40e_pf *pf = vsi->back;
5471 u64 credits = 0;
5472 int speed = 0;
5473 int ret = 0;
5474
5475 speed = i40e_get_link_speed(vsi);
5476 if (max_tx_rate > speed) {
5477 dev_err(&pf->pdev->dev,
5478 "Invalid max tx rate %llu specified for VSI seid %d.",
5479 max_tx_rate, seid);
5480 return -EINVAL;
5481 }
5482 if (max_tx_rate && max_tx_rate < 50) {
5483 dev_warn(&pf->pdev->dev,
5484 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5485 max_tx_rate = 50;
5486 }
5487
5488 /* Tx rate credits are in values of 50Mbps, 0 is disabled */
5489 credits = max_tx_rate;
5490 do_div(credits, I40E_BW_CREDIT_DIVISOR);
5491 ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5492 I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5493 if (ret)
5494 dev_err(&pf->pdev->dev,
5495 "Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5496 max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5497 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5498 return ret;
5499 }
5500
5501 /**
5502 * i40e_remove_queue_channels - Remove queue channels for the TCs
5503 * @vsi: VSI to be configured
5504 *
5505 * Remove queue channels for the TCs
5506 **/
5507 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5508 {
5509 enum i40e_admin_queue_err last_aq_status;
5510 struct i40e_cloud_filter *cfilter;
5511 struct i40e_channel *ch, *ch_tmp;
5512 struct i40e_pf *pf = vsi->back;
5513 struct hlist_node *node;
5514 int ret, i;
5515
5516 /* Reset rss size that was stored when reconfiguring rss for
5517 * channel VSIs with non-power-of-2 queue count.
5518 */
5519 vsi->current_rss_size = 0;
5520
5521 /* perform cleanup for channels if they exist */
5522 if (list_empty(&vsi->ch_list))
5523 return;
5524
5525 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5526 struct i40e_vsi *p_vsi;
5527
5528 list_del(&ch->list);
5529 p_vsi = ch->parent_vsi;
5530 if (!p_vsi || !ch->initialized) {
5531 kfree(ch);
5532 continue;
5533 }
5534 /* Reset queue contexts */
5535 for (i = 0; i < ch->num_queue_pairs; i++) {
5536 struct i40e_ring *tx_ring, *rx_ring;
5537 u16 pf_q;
5538
5539 pf_q = ch->base_queue + i;
5540 tx_ring = vsi->tx_rings[pf_q];
5541 tx_ring->ch = NULL;
5542
5543 rx_ring = vsi->rx_rings[pf_q];
5544 rx_ring->ch = NULL;
5545 }
5546
5547 /* Reset BW configured for this VSI via mqprio */
5548 ret = i40e_set_bw_limit(vsi, ch->seid, 0);
5549 if (ret)
5550 dev_info(&vsi->back->pdev->dev,
5551 "Failed to reset tx rate for ch->seid %u\n",
5552 ch->seid);
5553
5554 /* delete cloud filters associated with this channel */
5555 hlist_for_each_entry_safe(cfilter, node,
5556 &pf->cloud_filter_list, cloud_node) {
5557 if (cfilter->seid != ch->seid)
5558 continue;
5559
5560 hash_del(&cfilter->cloud_node);
5561 if (cfilter->dst_port)
5562 ret = i40e_add_del_cloud_filter_big_buf(vsi,
5563 cfilter,
5564 false);
5565 else
5566 ret = i40e_add_del_cloud_filter(vsi, cfilter,
5567 false);
5568 last_aq_status = pf->hw.aq.asq_last_status;
5569 if (ret)
5570 dev_info(&pf->pdev->dev,
5571 "Failed to delete cloud filter, err %s aq_err %s\n",
5572 i40e_stat_str(&pf->hw, ret),
5573 i40e_aq_str(&pf->hw, last_aq_status));
5574 kfree(cfilter);
5575 }
5576
5577 /* delete VSI from FW */
5578 ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
5579 NULL);
5580 if (ret)
5581 dev_err(&vsi->back->pdev->dev,
5582 "unable to remove channel (%d) for parent VSI(%d)\n",
5583 ch->seid, p_vsi->seid);
5584 kfree(ch);
5585 }
5586 INIT_LIST_HEAD(&vsi->ch_list);
5587 }
5588
5589 /**
5590 * i40e_is_any_channel - channel exist or not
5591 * @vsi: ptr to VSI to which channels are associated with
5592 *
5593 * Returns true or false if channel(s) exist for associated VSI or not
5594 **/
5595 static bool i40e_is_any_channel(struct i40e_vsi *vsi)
5596 {
5597 struct i40e_channel *ch, *ch_tmp;
5598
5599 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5600 if (ch->initialized)
5601 return true;
5602 }
5603
5604 return false;
5605 }
5606
5607 /**
5608 * i40e_get_max_queues_for_channel
5609 * @vsi: ptr to VSI to which channels are associated with
5610 *
5611 * Helper function which returns max value among the queue counts set on the
5612 * channels/TCs created.
5613 **/
5614 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
5615 {
5616 struct i40e_channel *ch, *ch_tmp;
5617 int max = 0;
5618
5619 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5620 if (!ch->initialized)
5621 continue;
5622 if (ch->num_queue_pairs > max)
5623 max = ch->num_queue_pairs;
5624 }
5625
5626 return max;
5627 }
5628
5629 /**
5630 * i40e_validate_num_queues - validate num_queues w.r.t channel
5631 * @pf: ptr to PF device
5632 * @num_queues: number of queues
5633 * @vsi: the parent VSI
5634 * @reconfig_rss: indicates should the RSS be reconfigured or not
5635 *
5636 * This function validates number of queues in the context of new channel
5637 * which is being established and determines if RSS should be reconfigured
5638 * or not for parent VSI.
5639 **/
5640 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
5641 struct i40e_vsi *vsi, bool *reconfig_rss)
5642 {
5643 int max_ch_queues;
5644
5645 if (!reconfig_rss)
5646 return -EINVAL;
5647
5648 *reconfig_rss = false;
5649 if (vsi->current_rss_size) {
5650 if (num_queues > vsi->current_rss_size) {
5651 dev_dbg(&pf->pdev->dev,
5652 "Error: num_queues (%d) > vsi's current_size(%d)\n",
5653 num_queues, vsi->current_rss_size);
5654 return -EINVAL;
5655 } else if ((num_queues < vsi->current_rss_size) &&
5656 (!is_power_of_2(num_queues))) {
5657 dev_dbg(&pf->pdev->dev,
5658 "Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
5659 num_queues, vsi->current_rss_size);
5660 return -EINVAL;
5661 }
5662 }
5663
5664 if (!is_power_of_2(num_queues)) {
5665 /* Find the max num_queues configured for channel if channel
5666 * exist.
5667 * if channel exist, then enforce 'num_queues' to be more than
5668 * max ever queues configured for channel.
5669 */
5670 max_ch_queues = i40e_get_max_queues_for_channel(vsi);
5671 if (num_queues < max_ch_queues) {
5672 dev_dbg(&pf->pdev->dev,
5673 "Error: num_queues (%d) < max queues configured for channel(%d)\n",
5674 num_queues, max_ch_queues);
5675 return -EINVAL;
5676 }
5677 *reconfig_rss = true;
5678 }
5679
5680 return 0;
5681 }
5682
5683 /**
5684 * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
5685 * @vsi: the VSI being setup
5686 * @rss_size: size of RSS, accordingly LUT gets reprogrammed
5687 *
5688 * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
5689 **/
5690 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
5691 {
5692 struct i40e_pf *pf = vsi->back;
5693 u8 seed[I40E_HKEY_ARRAY_SIZE];
5694 struct i40e_hw *hw = &pf->hw;
5695 int local_rss_size;
5696 u8 *lut;
5697 int ret;
5698
5699 if (!vsi->rss_size)
5700 return -EINVAL;
5701
5702 if (rss_size > vsi->rss_size)
5703 return -EINVAL;
5704
5705 local_rss_size = min_t(int, vsi->rss_size, rss_size);
5706 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
5707 if (!lut)
5708 return -ENOMEM;
5709
5710 /* Ignoring user configured lut if there is one */
5711 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
5712
5713 /* Use user configured hash key if there is one, otherwise
5714 * use default.
5715 */
5716 if (vsi->rss_hkey_user)
5717 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
5718 else
5719 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
5720
5721 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
5722 if (ret) {
5723 dev_info(&pf->pdev->dev,
5724 "Cannot set RSS lut, err %s aq_err %s\n",
5725 i40e_stat_str(hw, ret),
5726 i40e_aq_str(hw, hw->aq.asq_last_status));
5727 kfree(lut);
5728 return ret;
5729 }
5730 kfree(lut);
5731
5732 /* Do the update w.r.t. storing rss_size */
5733 if (!vsi->orig_rss_size)
5734 vsi->orig_rss_size = vsi->rss_size;
5735 vsi->current_rss_size = local_rss_size;
5736
5737 return ret;
5738 }
5739
5740 /**
5741 * i40e_channel_setup_queue_map - Setup a channel queue map
5742 * @pf: ptr to PF device
5743 * @vsi: the VSI being setup
5744 * @ctxt: VSI context structure
5745 * @ch: ptr to channel structure
5746 *
5747 * Setup queue map for a specific channel
5748 **/
5749 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
5750 struct i40e_vsi_context *ctxt,
5751 struct i40e_channel *ch)
5752 {
5753 u16 qcount, qmap, sections = 0;
5754 u8 offset = 0;
5755 int pow;
5756
5757 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
5758 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
5759
5760 qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
5761 ch->num_queue_pairs = qcount;
5762
5763 /* find the next higher power-of-2 of num queue pairs */
5764 pow = ilog2(qcount);
5765 if (!is_power_of_2(qcount))
5766 pow++;
5767
5768 qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
5769 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
5770
5771 /* Setup queue TC[0].qmap for given VSI context */
5772 ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
5773
5774 ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
5775 ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
5776 ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
5777 ctxt->info.valid_sections |= cpu_to_le16(sections);
5778 }
5779
5780 /**
5781 * i40e_add_channel - add a channel by adding VSI
5782 * @pf: ptr to PF device
5783 * @uplink_seid: underlying HW switching element (VEB) ID
5784 * @ch: ptr to channel structure
5785 *
5786 * Add a channel (VSI) using add_vsi and queue_map
5787 **/
5788 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
5789 struct i40e_channel *ch)
5790 {
5791 struct i40e_hw *hw = &pf->hw;
5792 struct i40e_vsi_context ctxt;
5793 u8 enabled_tc = 0x1; /* TC0 enabled */
5794 int ret;
5795
5796 if (ch->type != I40E_VSI_VMDQ2) {
5797 dev_info(&pf->pdev->dev,
5798 "add new vsi failed, ch->type %d\n", ch->type);
5799 return -EINVAL;
5800 }
5801
5802 memset(&ctxt, 0, sizeof(ctxt));
5803 ctxt.pf_num = hw->pf_id;
5804 ctxt.vf_num = 0;
5805 ctxt.uplink_seid = uplink_seid;
5806 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5807 if (ch->type == I40E_VSI_VMDQ2)
5808 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
5809
5810 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
5811 ctxt.info.valid_sections |=
5812 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
5813 ctxt.info.switch_id =
5814 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
5815 }
5816
5817 /* Set queue map for a given VSI context */
5818 i40e_channel_setup_queue_map(pf, &ctxt, ch);
5819
5820 /* Now time to create VSI */
5821 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
5822 if (ret) {
5823 dev_info(&pf->pdev->dev,
5824 "add new vsi failed, err %s aq_err %s\n",
5825 i40e_stat_str(&pf->hw, ret),
5826 i40e_aq_str(&pf->hw,
5827 pf->hw.aq.asq_last_status));
5828 return -ENOENT;
5829 }
5830
5831 /* Success, update channel */
5832 ch->enabled_tc = enabled_tc;
5833 ch->seid = ctxt.seid;
5834 ch->vsi_number = ctxt.vsi_number;
5835 ch->stat_counter_idx = cpu_to_le16(ctxt.info.stat_counter_idx);
5836
5837 /* copy just the sections touched not the entire info
5838 * since not all sections are valid as returned by
5839 * update vsi params
5840 */
5841 ch->info.mapping_flags = ctxt.info.mapping_flags;
5842 memcpy(&ch->info.queue_mapping,
5843 &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
5844 memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
5845 sizeof(ctxt.info.tc_mapping));
5846
5847 return 0;
5848 }
5849
5850 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
5851 u8 *bw_share)
5852 {
5853 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5854 i40e_status ret;
5855 int i;
5856
5857 bw_data.tc_valid_bits = ch->enabled_tc;
5858 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5859 bw_data.tc_bw_credits[i] = bw_share[i];
5860
5861 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
5862 &bw_data, NULL);
5863 if (ret) {
5864 dev_info(&vsi->back->pdev->dev,
5865 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
5866 vsi->back->hw.aq.asq_last_status, ch->seid);
5867 return -EINVAL;
5868 }
5869
5870 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5871 ch->info.qs_handle[i] = bw_data.qs_handles[i];
5872
5873 return 0;
5874 }
5875
5876 /**
5877 * i40e_channel_config_tx_ring - config TX ring associated with new channel
5878 * @pf: ptr to PF device
5879 * @vsi: the VSI being setup
5880 * @ch: ptr to channel structure
5881 *
5882 * Configure TX rings associated with channel (VSI) since queues are being
5883 * from parent VSI.
5884 **/
5885 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
5886 struct i40e_vsi *vsi,
5887 struct i40e_channel *ch)
5888 {
5889 i40e_status ret;
5890 int i;
5891 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5892
5893 /* Enable ETS TCs with equal BW Share for now across all VSIs */
5894 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5895 if (ch->enabled_tc & BIT(i))
5896 bw_share[i] = 1;
5897 }
5898
5899 /* configure BW for new VSI */
5900 ret = i40e_channel_config_bw(vsi, ch, bw_share);
5901 if (ret) {
5902 dev_info(&vsi->back->pdev->dev,
5903 "Failed configuring TC map %d for channel (seid %u)\n",
5904 ch->enabled_tc, ch->seid);
5905 return ret;
5906 }
5907
5908 for (i = 0; i < ch->num_queue_pairs; i++) {
5909 struct i40e_ring *tx_ring, *rx_ring;
5910 u16 pf_q;
5911
5912 pf_q = ch->base_queue + i;
5913
5914 /* Get to TX ring ptr of main VSI, for re-setup TX queue
5915 * context
5916 */
5917 tx_ring = vsi->tx_rings[pf_q];
5918 tx_ring->ch = ch;
5919
5920 /* Get the RX ring ptr */
5921 rx_ring = vsi->rx_rings[pf_q];
5922 rx_ring->ch = ch;
5923 }
5924
5925 return 0;
5926 }
5927
5928 /**
5929 * i40e_setup_hw_channel - setup new channel
5930 * @pf: ptr to PF device
5931 * @vsi: the VSI being setup
5932 * @ch: ptr to channel structure
5933 * @uplink_seid: underlying HW switching element (VEB) ID
5934 * @type: type of channel to be created (VMDq2/VF)
5935 *
5936 * Setup new channel (VSI) based on specified type (VMDq2/VF)
5937 * and configures TX rings accordingly
5938 **/
5939 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
5940 struct i40e_vsi *vsi,
5941 struct i40e_channel *ch,
5942 u16 uplink_seid, u8 type)
5943 {
5944 int ret;
5945
5946 ch->initialized = false;
5947 ch->base_queue = vsi->next_base_queue;
5948 ch->type = type;
5949
5950 /* Proceed with creation of channel (VMDq2) VSI */
5951 ret = i40e_add_channel(pf, uplink_seid, ch);
5952 if (ret) {
5953 dev_info(&pf->pdev->dev,
5954 "failed to add_channel using uplink_seid %u\n",
5955 uplink_seid);
5956 return ret;
5957 }
5958
5959 /* Mark the successful creation of channel */
5960 ch->initialized = true;
5961
5962 /* Reconfigure TX queues using QTX_CTL register */
5963 ret = i40e_channel_config_tx_ring(pf, vsi, ch);
5964 if (ret) {
5965 dev_info(&pf->pdev->dev,
5966 "failed to configure TX rings for channel %u\n",
5967 ch->seid);
5968 return ret;
5969 }
5970
5971 /* update 'next_base_queue' */
5972 vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
5973 dev_dbg(&pf->pdev->dev,
5974 "Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
5975 ch->seid, ch->vsi_number, ch->stat_counter_idx,
5976 ch->num_queue_pairs,
5977 vsi->next_base_queue);
5978 return ret;
5979 }
5980
5981 /**
5982 * i40e_setup_channel - setup new channel using uplink element
5983 * @pf: ptr to PF device
5984 * @type: type of channel to be created (VMDq2/VF)
5985 * @uplink_seid: underlying HW switching element (VEB) ID
5986 * @ch: ptr to channel structure
5987 *
5988 * Setup new channel (VSI) based on specified type (VMDq2/VF)
5989 * and uplink switching element (uplink_seid)
5990 **/
5991 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
5992 struct i40e_channel *ch)
5993 {
5994 u8 vsi_type;
5995 u16 seid;
5996 int ret;
5997
5998 if (vsi->type == I40E_VSI_MAIN) {
5999 vsi_type = I40E_VSI_VMDQ2;
6000 } else {
6001 dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6002 vsi->type);
6003 return false;
6004 }
6005
6006 /* underlying switching element */
6007 seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6008
6009 /* create channel (VSI), configure TX rings */
6010 ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6011 if (ret) {
6012 dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6013 return false;
6014 }
6015
6016 return ch->initialized ? true : false;
6017 }
6018
6019 /**
6020 * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6021 * @vsi: ptr to VSI which has PF backing
6022 *
6023 * Sets up switch mode correctly if it needs to be changed and perform
6024 * what are allowed modes.
6025 **/
6026 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6027 {
6028 u8 mode;
6029 struct i40e_pf *pf = vsi->back;
6030 struct i40e_hw *hw = &pf->hw;
6031 int ret;
6032
6033 ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6034 if (ret)
6035 return -EINVAL;
6036
6037 if (hw->dev_caps.switch_mode) {
6038 /* if switch mode is set, support mode2 (non-tunneled for
6039 * cloud filter) for now
6040 */
6041 u32 switch_mode = hw->dev_caps.switch_mode &
6042 I40E_SWITCH_MODE_MASK;
6043 if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6044 if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6045 return 0;
6046 dev_err(&pf->pdev->dev,
6047 "Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6048 hw->dev_caps.switch_mode);
6049 return -EINVAL;
6050 }
6051 }
6052
6053 /* Set Bit 7 to be valid */
6054 mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6055
6056 /* Set L4type for TCP support */
6057 mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6058
6059 /* Set cloud filter mode */
6060 mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6061
6062 /* Prep mode field for set_switch_config */
6063 ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6064 pf->last_sw_conf_valid_flags,
6065 mode, NULL);
6066 if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6067 dev_err(&pf->pdev->dev,
6068 "couldn't set switch config bits, err %s aq_err %s\n",
6069 i40e_stat_str(hw, ret),
6070 i40e_aq_str(hw,
6071 hw->aq.asq_last_status));
6072
6073 return ret;
6074 }
6075
6076 /**
6077 * i40e_create_queue_channel - function to create channel
6078 * @vsi: VSI to be configured
6079 * @ch: ptr to channel (it contains channel specific params)
6080 *
6081 * This function creates channel (VSI) using num_queues specified by user,
6082 * reconfigs RSS if needed.
6083 **/
6084 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6085 struct i40e_channel *ch)
6086 {
6087 struct i40e_pf *pf = vsi->back;
6088 bool reconfig_rss;
6089 int err;
6090
6091 if (!ch)
6092 return -EINVAL;
6093
6094 if (!ch->num_queue_pairs) {
6095 dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6096 ch->num_queue_pairs);
6097 return -EINVAL;
6098 }
6099
6100 /* validate user requested num_queues for channel */
6101 err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6102 &reconfig_rss);
6103 if (err) {
6104 dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6105 ch->num_queue_pairs);
6106 return -EINVAL;
6107 }
6108
6109 /* By default we are in VEPA mode, if this is the first VF/VMDq
6110 * VSI to be added switch to VEB mode.
6111 */
6112 if ((!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) ||
6113 (!i40e_is_any_channel(vsi))) {
6114 if (!is_power_of_2(vsi->tc_config.tc_info[0].qcount)) {
6115 dev_dbg(&pf->pdev->dev,
6116 "Failed to create channel. Override queues (%u) not power of 2\n",
6117 vsi->tc_config.tc_info[0].qcount);
6118 return -EINVAL;
6119 }
6120
6121 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6122 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6123
6124 if (vsi->type == I40E_VSI_MAIN) {
6125 if (pf->flags & I40E_FLAG_TC_MQPRIO)
6126 i40e_do_reset(pf, I40E_PF_RESET_FLAG,
6127 true);
6128 else
6129 i40e_do_reset_safe(pf,
6130 I40E_PF_RESET_FLAG);
6131 }
6132 }
6133 /* now onwards for main VSI, number of queues will be value
6134 * of TC0's queue count
6135 */
6136 }
6137
6138 /* By this time, vsi->cnt_q_avail shall be set to non-zero and
6139 * it should be more than num_queues
6140 */
6141 if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6142 dev_dbg(&pf->pdev->dev,
6143 "Error: cnt_q_avail (%u) less than num_queues %d\n",
6144 vsi->cnt_q_avail, ch->num_queue_pairs);
6145 return -EINVAL;
6146 }
6147
6148 /* reconfig_rss only if vsi type is MAIN_VSI */
6149 if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6150 err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6151 if (err) {
6152 dev_info(&pf->pdev->dev,
6153 "Error: unable to reconfig rss for num_queues (%u)\n",
6154 ch->num_queue_pairs);
6155 return -EINVAL;
6156 }
6157 }
6158
6159 if (!i40e_setup_channel(pf, vsi, ch)) {
6160 dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6161 return -EINVAL;
6162 }
6163
6164 dev_info(&pf->pdev->dev,
6165 "Setup channel (id:%u) utilizing num_queues %d\n",
6166 ch->seid, ch->num_queue_pairs);
6167
6168 /* configure VSI for BW limit */
6169 if (ch->max_tx_rate) {
6170 u64 credits = ch->max_tx_rate;
6171
6172 if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6173 return -EINVAL;
6174
6175 do_div(credits, I40E_BW_CREDIT_DIVISOR);
6176 dev_dbg(&pf->pdev->dev,
6177 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6178 ch->max_tx_rate,
6179 credits,
6180 ch->seid);
6181 }
6182
6183 /* in case of VF, this will be main SRIOV VSI */
6184 ch->parent_vsi = vsi;
6185
6186 /* and update main_vsi's count for queue_available to use */
6187 vsi->cnt_q_avail -= ch->num_queue_pairs;
6188
6189 return 0;
6190 }
6191
6192 /**
6193 * i40e_configure_queue_channels - Add queue channel for the given TCs
6194 * @vsi: VSI to be configured
6195 *
6196 * Configures queue channel mapping to the given TCs
6197 **/
6198 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6199 {
6200 struct i40e_channel *ch;
6201 u64 max_rate = 0;
6202 int ret = 0, i;
6203
6204 /* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6205 vsi->tc_seid_map[0] = vsi->seid;
6206 for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6207 if (vsi->tc_config.enabled_tc & BIT(i)) {
6208 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6209 if (!ch) {
6210 ret = -ENOMEM;
6211 goto err_free;
6212 }
6213
6214 INIT_LIST_HEAD(&ch->list);
6215 ch->num_queue_pairs =
6216 vsi->tc_config.tc_info[i].qcount;
6217 ch->base_queue =
6218 vsi->tc_config.tc_info[i].qoffset;
6219
6220 /* Bandwidth limit through tc interface is in bytes/s,
6221 * change to Mbit/s
6222 */
6223 max_rate = vsi->mqprio_qopt.max_rate[i];
6224 do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6225 ch->max_tx_rate = max_rate;
6226
6227 list_add_tail(&ch->list, &vsi->ch_list);
6228
6229 ret = i40e_create_queue_channel(vsi, ch);
6230 if (ret) {
6231 dev_err(&vsi->back->pdev->dev,
6232 "Failed creating queue channel with TC%d: queues %d\n",
6233 i, ch->num_queue_pairs);
6234 goto err_free;
6235 }
6236 vsi->tc_seid_map[i] = ch->seid;
6237 }
6238 }
6239 return ret;
6240
6241 err_free:
6242 i40e_remove_queue_channels(vsi);
6243 return ret;
6244 }
6245
6246 /**
6247 * i40e_veb_config_tc - Configure TCs for given VEB
6248 * @veb: given VEB
6249 * @enabled_tc: TC bitmap
6250 *
6251 * Configures given TC bitmap for VEB (switching) element
6252 **/
6253 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6254 {
6255 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6256 struct i40e_pf *pf = veb->pf;
6257 int ret = 0;
6258 int i;
6259
6260 /* No TCs or already enabled TCs just return */
6261 if (!enabled_tc || veb->enabled_tc == enabled_tc)
6262 return ret;
6263
6264 bw_data.tc_valid_bits = enabled_tc;
6265 /* bw_data.absolute_credits is not set (relative) */
6266
6267 /* Enable ETS TCs with equal BW Share for now */
6268 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6269 if (enabled_tc & BIT(i))
6270 bw_data.tc_bw_share_credits[i] = 1;
6271 }
6272
6273 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6274 &bw_data, NULL);
6275 if (ret) {
6276 dev_info(&pf->pdev->dev,
6277 "VEB bw config failed, err %s aq_err %s\n",
6278 i40e_stat_str(&pf->hw, ret),
6279 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6280 goto out;
6281 }
6282
6283 /* Update the BW information */
6284 ret = i40e_veb_get_bw_info(veb);
6285 if (ret) {
6286 dev_info(&pf->pdev->dev,
6287 "Failed getting veb bw config, err %s aq_err %s\n",
6288 i40e_stat_str(&pf->hw, ret),
6289 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6290 }
6291
6292 out:
6293 return ret;
6294 }
6295
6296 #ifdef CONFIG_I40E_DCB
6297 /**
6298 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6299 * @pf: PF struct
6300 *
6301 * Reconfigure VEB/VSIs on a given PF; it is assumed that
6302 * the caller would've quiesce all the VSIs before calling
6303 * this function
6304 **/
6305 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6306 {
6307 u8 tc_map = 0;
6308 int ret;
6309 u8 v;
6310
6311 /* Enable the TCs available on PF to all VEBs */
6312 tc_map = i40e_pf_get_tc_map(pf);
6313 for (v = 0; v < I40E_MAX_VEB; v++) {
6314 if (!pf->veb[v])
6315 continue;
6316 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6317 if (ret) {
6318 dev_info(&pf->pdev->dev,
6319 "Failed configuring TC for VEB seid=%d\n",
6320 pf->veb[v]->seid);
6321 /* Will try to configure as many components */
6322 }
6323 }
6324
6325 /* Update each VSI */
6326 for (v = 0; v < pf->num_alloc_vsi; v++) {
6327 if (!pf->vsi[v])
6328 continue;
6329
6330 /* - Enable all TCs for the LAN VSI
6331 * - For all others keep them at TC0 for now
6332 */
6333 if (v == pf->lan_vsi)
6334 tc_map = i40e_pf_get_tc_map(pf);
6335 else
6336 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6337
6338 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6339 if (ret) {
6340 dev_info(&pf->pdev->dev,
6341 "Failed configuring TC for VSI seid=%d\n",
6342 pf->vsi[v]->seid);
6343 /* Will try to configure as many components */
6344 } else {
6345 /* Re-configure VSI vectors based on updated TC map */
6346 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6347 if (pf->vsi[v]->netdev)
6348 i40e_dcbnl_set_all(pf->vsi[v]);
6349 }
6350 }
6351 }
6352
6353 /**
6354 * i40e_resume_port_tx - Resume port Tx
6355 * @pf: PF struct
6356 *
6357 * Resume a port's Tx and issue a PF reset in case of failure to
6358 * resume.
6359 **/
6360 static int i40e_resume_port_tx(struct i40e_pf *pf)
6361 {
6362 struct i40e_hw *hw = &pf->hw;
6363 int ret;
6364
6365 ret = i40e_aq_resume_port_tx(hw, NULL);
6366 if (ret) {
6367 dev_info(&pf->pdev->dev,
6368 "Resume Port Tx failed, err %s aq_err %s\n",
6369 i40e_stat_str(&pf->hw, ret),
6370 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6371 /* Schedule PF reset to recover */
6372 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6373 i40e_service_event_schedule(pf);
6374 }
6375
6376 return ret;
6377 }
6378
6379 /**
6380 * i40e_init_pf_dcb - Initialize DCB configuration
6381 * @pf: PF being configured
6382 *
6383 * Query the current DCB configuration and cache it
6384 * in the hardware structure
6385 **/
6386 static int i40e_init_pf_dcb(struct i40e_pf *pf)
6387 {
6388 struct i40e_hw *hw = &pf->hw;
6389 int err = 0;
6390
6391 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
6392 if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT)
6393 goto out;
6394
6395 /* Get the initial DCB configuration */
6396 err = i40e_init_dcb(hw);
6397 if (!err) {
6398 /* Device/Function is not DCBX capable */
6399 if ((!hw->func_caps.dcb) ||
6400 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
6401 dev_info(&pf->pdev->dev,
6402 "DCBX offload is not supported or is disabled for this PF.\n");
6403 } else {
6404 /* When status is not DISABLED then DCBX in FW */
6405 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
6406 DCB_CAP_DCBX_VER_IEEE;
6407
6408 pf->flags |= I40E_FLAG_DCB_CAPABLE;
6409 /* Enable DCB tagging only when more than one TC
6410 * or explicitly disable if only one TC
6411 */
6412 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
6413 pf->flags |= I40E_FLAG_DCB_ENABLED;
6414 else
6415 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6416 dev_dbg(&pf->pdev->dev,
6417 "DCBX offload is supported for this PF.\n");
6418 }
6419 } else {
6420 dev_info(&pf->pdev->dev,
6421 "Query for DCB configuration failed, err %s aq_err %s\n",
6422 i40e_stat_str(&pf->hw, err),
6423 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6424 }
6425
6426 out:
6427 return err;
6428 }
6429 #endif /* CONFIG_I40E_DCB */
6430 #define SPEED_SIZE 14
6431 #define FC_SIZE 8
6432 /**
6433 * i40e_print_link_message - print link up or down
6434 * @vsi: the VSI for which link needs a message
6435 */
6436 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
6437 {
6438 enum i40e_aq_link_speed new_speed;
6439 struct i40e_pf *pf = vsi->back;
6440 char *speed = "Unknown";
6441 char *fc = "Unknown";
6442 char *fec = "";
6443 char *req_fec = "";
6444 char *an = "";
6445
6446 new_speed = pf->hw.phy.link_info.link_speed;
6447
6448 if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
6449 return;
6450 vsi->current_isup = isup;
6451 vsi->current_speed = new_speed;
6452 if (!isup) {
6453 netdev_info(vsi->netdev, "NIC Link is Down\n");
6454 return;
6455 }
6456
6457 /* Warn user if link speed on NPAR enabled partition is not at
6458 * least 10GB
6459 */
6460 if (pf->hw.func_caps.npar_enable &&
6461 (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
6462 pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
6463 netdev_warn(vsi->netdev,
6464 "The partition detected link speed that is less than 10Gbps\n");
6465
6466 switch (pf->hw.phy.link_info.link_speed) {
6467 case I40E_LINK_SPEED_40GB:
6468 speed = "40 G";
6469 break;
6470 case I40E_LINK_SPEED_20GB:
6471 speed = "20 G";
6472 break;
6473 case I40E_LINK_SPEED_25GB:
6474 speed = "25 G";
6475 break;
6476 case I40E_LINK_SPEED_10GB:
6477 speed = "10 G";
6478 break;
6479 case I40E_LINK_SPEED_1GB:
6480 speed = "1000 M";
6481 break;
6482 case I40E_LINK_SPEED_100MB:
6483 speed = "100 M";
6484 break;
6485 default:
6486 break;
6487 }
6488
6489 switch (pf->hw.fc.current_mode) {
6490 case I40E_FC_FULL:
6491 fc = "RX/TX";
6492 break;
6493 case I40E_FC_TX_PAUSE:
6494 fc = "TX";
6495 break;
6496 case I40E_FC_RX_PAUSE:
6497 fc = "RX";
6498 break;
6499 default:
6500 fc = "None";
6501 break;
6502 }
6503
6504 if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
6505 req_fec = ", Requested FEC: None";
6506 fec = ", FEC: None";
6507 an = ", Autoneg: False";
6508
6509 if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
6510 an = ", Autoneg: True";
6511
6512 if (pf->hw.phy.link_info.fec_info &
6513 I40E_AQ_CONFIG_FEC_KR_ENA)
6514 fec = ", FEC: CL74 FC-FEC/BASE-R";
6515 else if (pf->hw.phy.link_info.fec_info &
6516 I40E_AQ_CONFIG_FEC_RS_ENA)
6517 fec = ", FEC: CL108 RS-FEC";
6518
6519 /* 'CL108 RS-FEC' should be displayed when RS is requested, or
6520 * both RS and FC are requested
6521 */
6522 if (vsi->back->hw.phy.link_info.req_fec_info &
6523 (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
6524 if (vsi->back->hw.phy.link_info.req_fec_info &
6525 I40E_AQ_REQUEST_FEC_RS)
6526 req_fec = ", Requested FEC: CL108 RS-FEC";
6527 else
6528 req_fec = ", Requested FEC: CL74 FC-FEC/BASE-R";
6529 }
6530 }
6531
6532 netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s%s, Flow Control: %s\n",
6533 speed, req_fec, fec, an, fc);
6534 }
6535
6536 /**
6537 * i40e_up_complete - Finish the last steps of bringing up a connection
6538 * @vsi: the VSI being configured
6539 **/
6540 static int i40e_up_complete(struct i40e_vsi *vsi)
6541 {
6542 struct i40e_pf *pf = vsi->back;
6543 int err;
6544
6545 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6546 i40e_vsi_configure_msix(vsi);
6547 else
6548 i40e_configure_msi_and_legacy(vsi);
6549
6550 /* start rings */
6551 err = i40e_vsi_start_rings(vsi);
6552 if (err)
6553 return err;
6554
6555 clear_bit(__I40E_VSI_DOWN, vsi->state);
6556 i40e_napi_enable_all(vsi);
6557 i40e_vsi_enable_irq(vsi);
6558
6559 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
6560 (vsi->netdev)) {
6561 i40e_print_link_message(vsi, true);
6562 netif_tx_start_all_queues(vsi->netdev);
6563 netif_carrier_on(vsi->netdev);
6564 }
6565
6566 /* replay FDIR SB filters */
6567 if (vsi->type == I40E_VSI_FDIR) {
6568 /* reset fd counters */
6569 pf->fd_add_err = 0;
6570 pf->fd_atr_cnt = 0;
6571 i40e_fdir_filter_restore(vsi);
6572 }
6573
6574 /* On the next run of the service_task, notify any clients of the new
6575 * opened netdev
6576 */
6577 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
6578 i40e_service_event_schedule(pf);
6579
6580 return 0;
6581 }
6582
6583 /**
6584 * i40e_vsi_reinit_locked - Reset the VSI
6585 * @vsi: the VSI being configured
6586 *
6587 * Rebuild the ring structs after some configuration
6588 * has changed, e.g. MTU size.
6589 **/
6590 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
6591 {
6592 struct i40e_pf *pf = vsi->back;
6593
6594 WARN_ON(in_interrupt());
6595 while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
6596 usleep_range(1000, 2000);
6597 i40e_down(vsi);
6598
6599 i40e_up(vsi);
6600 clear_bit(__I40E_CONFIG_BUSY, pf->state);
6601 }
6602
6603 /**
6604 * i40e_up - Bring the connection back up after being down
6605 * @vsi: the VSI being configured
6606 **/
6607 int i40e_up(struct i40e_vsi *vsi)
6608 {
6609 int err;
6610
6611 err = i40e_vsi_configure(vsi);
6612 if (!err)
6613 err = i40e_up_complete(vsi);
6614
6615 return err;
6616 }
6617
6618 /**
6619 * i40e_down - Shutdown the connection processing
6620 * @vsi: the VSI being stopped
6621 **/
6622 void i40e_down(struct i40e_vsi *vsi)
6623 {
6624 int i;
6625
6626 /* It is assumed that the caller of this function
6627 * sets the vsi->state __I40E_VSI_DOWN bit.
6628 */
6629 if (vsi->netdev) {
6630 netif_carrier_off(vsi->netdev);
6631 netif_tx_disable(vsi->netdev);
6632 }
6633 i40e_vsi_disable_irq(vsi);
6634 i40e_vsi_stop_rings(vsi);
6635 i40e_napi_disable_all(vsi);
6636
6637 for (i = 0; i < vsi->num_queue_pairs; i++) {
6638 i40e_clean_tx_ring(vsi->tx_rings[i]);
6639 if (i40e_enabled_xdp_vsi(vsi))
6640 i40e_clean_tx_ring(vsi->xdp_rings[i]);
6641 i40e_clean_rx_ring(vsi->rx_rings[i]);
6642 }
6643
6644 }
6645
6646 /**
6647 * i40e_validate_mqprio_qopt- validate queue mapping info
6648 * @vsi: the VSI being configured
6649 * @mqprio_qopt: queue parametrs
6650 **/
6651 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
6652 struct tc_mqprio_qopt_offload *mqprio_qopt)
6653 {
6654 u64 sum_max_rate = 0;
6655 u64 max_rate = 0;
6656 int i;
6657
6658 if (mqprio_qopt->qopt.offset[0] != 0 ||
6659 mqprio_qopt->qopt.num_tc < 1 ||
6660 mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
6661 return -EINVAL;
6662 for (i = 0; ; i++) {
6663 if (!mqprio_qopt->qopt.count[i])
6664 return -EINVAL;
6665 if (mqprio_qopt->min_rate[i]) {
6666 dev_err(&vsi->back->pdev->dev,
6667 "Invalid min tx rate (greater than 0) specified\n");
6668 return -EINVAL;
6669 }
6670 max_rate = mqprio_qopt->max_rate[i];
6671 do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6672 sum_max_rate += max_rate;
6673
6674 if (i >= mqprio_qopt->qopt.num_tc - 1)
6675 break;
6676 if (mqprio_qopt->qopt.offset[i + 1] !=
6677 (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
6678 return -EINVAL;
6679 }
6680 if (vsi->num_queue_pairs <
6681 (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
6682 return -EINVAL;
6683 }
6684 if (sum_max_rate > i40e_get_link_speed(vsi)) {
6685 dev_err(&vsi->back->pdev->dev,
6686 "Invalid max tx rate specified\n");
6687 return -EINVAL;
6688 }
6689 return 0;
6690 }
6691
6692 /**
6693 * i40e_vsi_set_default_tc_config - set default values for tc configuration
6694 * @vsi: the VSI being configured
6695 **/
6696 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
6697 {
6698 u16 qcount;
6699 int i;
6700
6701 /* Only TC0 is enabled */
6702 vsi->tc_config.numtc = 1;
6703 vsi->tc_config.enabled_tc = 1;
6704 qcount = min_t(int, vsi->alloc_queue_pairs,
6705 i40e_pf_get_max_q_per_tc(vsi->back));
6706 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6707 /* For the TC that is not enabled set the offset to to default
6708 * queue and allocate one queue for the given TC.
6709 */
6710 vsi->tc_config.tc_info[i].qoffset = 0;
6711 if (i == 0)
6712 vsi->tc_config.tc_info[i].qcount = qcount;
6713 else
6714 vsi->tc_config.tc_info[i].qcount = 1;
6715 vsi->tc_config.tc_info[i].netdev_tc = 0;
6716 }
6717 }
6718
6719 /**
6720 * i40e_setup_tc - configure multiple traffic classes
6721 * @netdev: net device to configure
6722 * @type_data: tc offload data
6723 **/
6724 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
6725 {
6726 struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
6727 struct i40e_netdev_priv *np = netdev_priv(netdev);
6728 struct i40e_vsi *vsi = np->vsi;
6729 struct i40e_pf *pf = vsi->back;
6730 u8 enabled_tc = 0, num_tc, hw;
6731 bool need_reset = false;
6732 int ret = -EINVAL;
6733 u16 mode;
6734 int i;
6735
6736 num_tc = mqprio_qopt->qopt.num_tc;
6737 hw = mqprio_qopt->qopt.hw;
6738 mode = mqprio_qopt->mode;
6739 if (!hw) {
6740 pf->flags &= ~I40E_FLAG_TC_MQPRIO;
6741 memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
6742 goto config_tc;
6743 }
6744
6745 /* Check if MFP enabled */
6746 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
6747 netdev_info(netdev,
6748 "Configuring TC not supported in MFP mode\n");
6749 return ret;
6750 }
6751 switch (mode) {
6752 case TC_MQPRIO_MODE_DCB:
6753 pf->flags &= ~I40E_FLAG_TC_MQPRIO;
6754
6755 /* Check if DCB enabled to continue */
6756 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
6757 netdev_info(netdev,
6758 "DCB is not enabled for adapter\n");
6759 return ret;
6760 }
6761
6762 /* Check whether tc count is within enabled limit */
6763 if (num_tc > i40e_pf_get_num_tc(pf)) {
6764 netdev_info(netdev,
6765 "TC count greater than enabled on link for adapter\n");
6766 return ret;
6767 }
6768 break;
6769 case TC_MQPRIO_MODE_CHANNEL:
6770 if (pf->flags & I40E_FLAG_DCB_ENABLED) {
6771 netdev_info(netdev,
6772 "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
6773 return ret;
6774 }
6775 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
6776 return ret;
6777 ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
6778 if (ret)
6779 return ret;
6780 memcpy(&vsi->mqprio_qopt, mqprio_qopt,
6781 sizeof(*mqprio_qopt));
6782 pf->flags |= I40E_FLAG_TC_MQPRIO;
6783 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6784 break;
6785 default:
6786 return -EINVAL;
6787 }
6788
6789 config_tc:
6790 /* Generate TC map for number of tc requested */
6791 for (i = 0; i < num_tc; i++)
6792 enabled_tc |= BIT(i);
6793
6794 /* Requesting same TC configuration as already enabled */
6795 if (enabled_tc == vsi->tc_config.enabled_tc &&
6796 mode != TC_MQPRIO_MODE_CHANNEL)
6797 return 0;
6798
6799 /* Quiesce VSI queues */
6800 i40e_quiesce_vsi(vsi);
6801
6802 if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
6803 i40e_remove_queue_channels(vsi);
6804
6805 /* Configure VSI for enabled TCs */
6806 ret = i40e_vsi_config_tc(vsi, enabled_tc);
6807 if (ret) {
6808 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
6809 vsi->seid);
6810 need_reset = true;
6811 goto exit;
6812 }
6813
6814 if (pf->flags & I40E_FLAG_TC_MQPRIO) {
6815 if (vsi->mqprio_qopt.max_rate[0]) {
6816 u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
6817
6818 do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
6819 ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
6820 if (!ret) {
6821 u64 credits = max_tx_rate;
6822
6823 do_div(credits, I40E_BW_CREDIT_DIVISOR);
6824 dev_dbg(&vsi->back->pdev->dev,
6825 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6826 max_tx_rate,
6827 credits,
6828 vsi->seid);
6829 } else {
6830 need_reset = true;
6831 goto exit;
6832 }
6833 }
6834 ret = i40e_configure_queue_channels(vsi);
6835 if (ret) {
6836 netdev_info(netdev,
6837 "Failed configuring queue channels\n");
6838 need_reset = true;
6839 goto exit;
6840 }
6841 }
6842
6843 exit:
6844 /* Reset the configuration data to defaults, only TC0 is enabled */
6845 if (need_reset) {
6846 i40e_vsi_set_default_tc_config(vsi);
6847 need_reset = false;
6848 }
6849
6850 /* Unquiesce VSI */
6851 i40e_unquiesce_vsi(vsi);
6852 return ret;
6853 }
6854
6855 /**
6856 * i40e_set_cld_element - sets cloud filter element data
6857 * @filter: cloud filter rule
6858 * @cld: ptr to cloud filter element data
6859 *
6860 * This is helper function to copy data into cloud filter element
6861 **/
6862 static inline void
6863 i40e_set_cld_element(struct i40e_cloud_filter *filter,
6864 struct i40e_aqc_cloud_filters_element_data *cld)
6865 {
6866 int i, j;
6867 u32 ipa;
6868
6869 memset(cld, 0, sizeof(*cld));
6870 ether_addr_copy(cld->outer_mac, filter->dst_mac);
6871 ether_addr_copy(cld->inner_mac, filter->src_mac);
6872
6873 if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
6874 return;
6875
6876 if (filter->n_proto == ETH_P_IPV6) {
6877 #define IPV6_MAX_INDEX (ARRAY_SIZE(filter->dst_ipv6) - 1)
6878 for (i = 0, j = 0; i < ARRAY_SIZE(filter->dst_ipv6);
6879 i++, j += 2) {
6880 ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
6881 ipa = cpu_to_le32(ipa);
6882 memcpy(&cld->ipaddr.raw_v6.data[j], &ipa, sizeof(ipa));
6883 }
6884 } else {
6885 ipa = be32_to_cpu(filter->dst_ipv4);
6886 memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
6887 }
6888
6889 cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
6890
6891 /* tenant_id is not supported by FW now, once the support is enabled
6892 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
6893 */
6894 if (filter->tenant_id)
6895 return;
6896 }
6897
6898 /**
6899 * i40e_add_del_cloud_filter - Add/del cloud filter
6900 * @vsi: pointer to VSI
6901 * @filter: cloud filter rule
6902 * @add: if true, add, if false, delete
6903 *
6904 * Add or delete a cloud filter for a specific flow spec.
6905 * Returns 0 if the filter were successfully added.
6906 **/
6907 static int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
6908 struct i40e_cloud_filter *filter, bool add)
6909 {
6910 struct i40e_aqc_cloud_filters_element_data cld_filter;
6911 struct i40e_pf *pf = vsi->back;
6912 int ret;
6913 static const u16 flag_table[128] = {
6914 [I40E_CLOUD_FILTER_FLAGS_OMAC] =
6915 I40E_AQC_ADD_CLOUD_FILTER_OMAC,
6916 [I40E_CLOUD_FILTER_FLAGS_IMAC] =
6917 I40E_AQC_ADD_CLOUD_FILTER_IMAC,
6918 [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN] =
6919 I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
6920 [I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
6921 I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
6922 [I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
6923 I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
6924 [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
6925 I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
6926 [I40E_CLOUD_FILTER_FLAGS_IIP] =
6927 I40E_AQC_ADD_CLOUD_FILTER_IIP,
6928 };
6929
6930 if (filter->flags >= ARRAY_SIZE(flag_table))
6931 return I40E_ERR_CONFIG;
6932
6933 /* copy element needed to add cloud filter from filter */
6934 i40e_set_cld_element(filter, &cld_filter);
6935
6936 if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
6937 cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
6938 I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
6939
6940 if (filter->n_proto == ETH_P_IPV6)
6941 cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
6942 I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
6943 else
6944 cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
6945 I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
6946
6947 if (add)
6948 ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
6949 &cld_filter, 1);
6950 else
6951 ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
6952 &cld_filter, 1);
6953 if (ret)
6954 dev_dbg(&pf->pdev->dev,
6955 "Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
6956 add ? "add" : "delete", filter->dst_port, ret,
6957 pf->hw.aq.asq_last_status);
6958 else
6959 dev_info(&pf->pdev->dev,
6960 "%s cloud filter for VSI: %d\n",
6961 add ? "Added" : "Deleted", filter->seid);
6962 return ret;
6963 }
6964
6965 /**
6966 * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
6967 * @vsi: pointer to VSI
6968 * @filter: cloud filter rule
6969 * @add: if true, add, if false, delete
6970 *
6971 * Add or delete a cloud filter for a specific flow spec using big buffer.
6972 * Returns 0 if the filter were successfully added.
6973 **/
6974 static int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
6975 struct i40e_cloud_filter *filter,
6976 bool add)
6977 {
6978 struct i40e_aqc_cloud_filters_element_bb cld_filter;
6979 struct i40e_pf *pf = vsi->back;
6980 int ret;
6981
6982 /* Both (src/dst) valid mac_addr are not supported */
6983 if ((is_valid_ether_addr(filter->dst_mac) &&
6984 is_valid_ether_addr(filter->src_mac)) ||
6985 (is_multicast_ether_addr(filter->dst_mac) &&
6986 is_multicast_ether_addr(filter->src_mac)))
6987 return -EOPNOTSUPP;
6988
6989 /* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
6990 * ports are not supported via big buffer now.
6991 */
6992 if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
6993 return -EOPNOTSUPP;
6994
6995 /* adding filter using src_port/src_ip is not supported at this stage */
6996 if (filter->src_port || filter->src_ipv4 ||
6997 !ipv6_addr_any(&filter->ip.v6.src_ip6))
6998 return -EOPNOTSUPP;
6999
7000 /* copy element needed to add cloud filter from filter */
7001 i40e_set_cld_element(filter, &cld_filter.element);
7002
7003 if (is_valid_ether_addr(filter->dst_mac) ||
7004 is_valid_ether_addr(filter->src_mac) ||
7005 is_multicast_ether_addr(filter->dst_mac) ||
7006 is_multicast_ether_addr(filter->src_mac)) {
7007 /* MAC + IP : unsupported mode */
7008 if (filter->dst_ipv4)
7009 return -EOPNOTSUPP;
7010
7011 /* since we validated that L4 port must be valid before
7012 * we get here, start with respective "flags" value
7013 * and update if vlan is present or not
7014 */
7015 cld_filter.element.flags =
7016 cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
7017
7018 if (filter->vlan_id) {
7019 cld_filter.element.flags =
7020 cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
7021 }
7022
7023 } else if (filter->dst_ipv4 ||
7024 !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
7025 cld_filter.element.flags =
7026 cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
7027 if (filter->n_proto == ETH_P_IPV6)
7028 cld_filter.element.flags |=
7029 cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
7030 else
7031 cld_filter.element.flags |=
7032 cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
7033 } else {
7034 dev_err(&pf->pdev->dev,
7035 "either mac or ip has to be valid for cloud filter\n");
7036 return -EINVAL;
7037 }
7038
7039 /* Now copy L4 port in Byte 6..7 in general fields */
7040 cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
7041 be16_to_cpu(filter->dst_port);
7042
7043 if (add) {
7044 /* Validate current device switch mode, change if necessary */
7045 ret = i40e_validate_and_set_switch_mode(vsi);
7046 if (ret) {
7047 dev_err(&pf->pdev->dev,
7048 "failed to set switch mode, ret %d\n",
7049 ret);
7050 return ret;
7051 }
7052
7053 ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
7054 &cld_filter, 1);
7055 } else {
7056 ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
7057 &cld_filter, 1);
7058 }
7059
7060 if (ret)
7061 dev_dbg(&pf->pdev->dev,
7062 "Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
7063 add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
7064 else
7065 dev_info(&pf->pdev->dev,
7066 "%s cloud filter for VSI: %d, L4 port: %d\n",
7067 add ? "add" : "delete", filter->seid,
7068 ntohs(filter->dst_port));
7069 return ret;
7070 }
7071
7072 /**
7073 * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
7074 * @vsi: Pointer to VSI
7075 * @cls_flower: Pointer to struct tc_cls_flower_offload
7076 * @filter: Pointer to cloud filter structure
7077 *
7078 **/
7079 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
7080 struct tc_cls_flower_offload *f,
7081 struct i40e_cloud_filter *filter)
7082 {
7083 u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
7084 struct i40e_pf *pf = vsi->back;
7085 u8 field_flags = 0;
7086
7087 if (f->dissector->used_keys &
7088 ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
7089 BIT(FLOW_DISSECTOR_KEY_BASIC) |
7090 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
7091 BIT(FLOW_DISSECTOR_KEY_VLAN) |
7092 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
7093 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
7094 BIT(FLOW_DISSECTOR_KEY_PORTS) |
7095 BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
7096 dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
7097 f->dissector->used_keys);
7098 return -EOPNOTSUPP;
7099 }
7100
7101 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
7102 struct flow_dissector_key_keyid *key =
7103 skb_flow_dissector_target(f->dissector,
7104 FLOW_DISSECTOR_KEY_ENC_KEYID,
7105 f->key);
7106
7107 struct flow_dissector_key_keyid *mask =
7108 skb_flow_dissector_target(f->dissector,
7109 FLOW_DISSECTOR_KEY_ENC_KEYID,
7110 f->mask);
7111
7112 if (mask->keyid != 0)
7113 field_flags |= I40E_CLOUD_FIELD_TEN_ID;
7114
7115 filter->tenant_id = be32_to_cpu(key->keyid);
7116 }
7117
7118 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
7119 struct flow_dissector_key_basic *key =
7120 skb_flow_dissector_target(f->dissector,
7121 FLOW_DISSECTOR_KEY_BASIC,
7122 f->key);
7123
7124 struct flow_dissector_key_basic *mask =
7125 skb_flow_dissector_target(f->dissector,
7126 FLOW_DISSECTOR_KEY_BASIC,
7127 f->mask);
7128
7129 n_proto_key = ntohs(key->n_proto);
7130 n_proto_mask = ntohs(mask->n_proto);
7131
7132 if (n_proto_key == ETH_P_ALL) {
7133 n_proto_key = 0;
7134 n_proto_mask = 0;
7135 }
7136 filter->n_proto = n_proto_key & n_proto_mask;
7137 filter->ip_proto = key->ip_proto;
7138 }
7139
7140 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
7141 struct flow_dissector_key_eth_addrs *key =
7142 skb_flow_dissector_target(f->dissector,
7143 FLOW_DISSECTOR_KEY_ETH_ADDRS,
7144 f->key);
7145
7146 struct flow_dissector_key_eth_addrs *mask =
7147 skb_flow_dissector_target(f->dissector,
7148 FLOW_DISSECTOR_KEY_ETH_ADDRS,
7149 f->mask);
7150
7151 /* use is_broadcast and is_zero to check for all 0xf or 0 */
7152 if (!is_zero_ether_addr(mask->dst)) {
7153 if (is_broadcast_ether_addr(mask->dst)) {
7154 field_flags |= I40E_CLOUD_FIELD_OMAC;
7155 } else {
7156 dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
7157 mask->dst);
7158 return I40E_ERR_CONFIG;
7159 }
7160 }
7161
7162 if (!is_zero_ether_addr(mask->src)) {
7163 if (is_broadcast_ether_addr(mask->src)) {
7164 field_flags |= I40E_CLOUD_FIELD_IMAC;
7165 } else {
7166 dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
7167 mask->src);
7168 return I40E_ERR_CONFIG;
7169 }
7170 }
7171 ether_addr_copy(filter->dst_mac, key->dst);
7172 ether_addr_copy(filter->src_mac, key->src);
7173 }
7174
7175 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
7176 struct flow_dissector_key_vlan *key =
7177 skb_flow_dissector_target(f->dissector,
7178 FLOW_DISSECTOR_KEY_VLAN,
7179 f->key);
7180 struct flow_dissector_key_vlan *mask =
7181 skb_flow_dissector_target(f->dissector,
7182 FLOW_DISSECTOR_KEY_VLAN,
7183 f->mask);
7184
7185 if (mask->vlan_id) {
7186 if (mask->vlan_id == VLAN_VID_MASK) {
7187 field_flags |= I40E_CLOUD_FIELD_IVLAN;
7188
7189 } else {
7190 dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
7191 mask->vlan_id);
7192 return I40E_ERR_CONFIG;
7193 }
7194 }
7195
7196 filter->vlan_id = cpu_to_be16(key->vlan_id);
7197 }
7198
7199 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
7200 struct flow_dissector_key_control *key =
7201 skb_flow_dissector_target(f->dissector,
7202 FLOW_DISSECTOR_KEY_CONTROL,
7203 f->key);
7204
7205 addr_type = key->addr_type;
7206 }
7207
7208 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
7209 struct flow_dissector_key_ipv4_addrs *key =
7210 skb_flow_dissector_target(f->dissector,
7211 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
7212 f->key);
7213 struct flow_dissector_key_ipv4_addrs *mask =
7214 skb_flow_dissector_target(f->dissector,
7215 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
7216 f->mask);
7217
7218 if (mask->dst) {
7219 if (mask->dst == cpu_to_be32(0xffffffff)) {
7220 field_flags |= I40E_CLOUD_FIELD_IIP;
7221 } else {
7222 mask->dst = be32_to_cpu(mask->dst);
7223 dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4\n",
7224 &mask->dst);
7225 return I40E_ERR_CONFIG;
7226 }
7227 }
7228
7229 if (mask->src) {
7230 if (mask->src == cpu_to_be32(0xffffffff)) {
7231 field_flags |= I40E_CLOUD_FIELD_IIP;
7232 } else {
7233 mask->src = be32_to_cpu(mask->src);
7234 dev_err(&pf->pdev->dev, "Bad ip src mask %pI4\n",
7235 &mask->src);
7236 return I40E_ERR_CONFIG;
7237 }
7238 }
7239
7240 if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
7241 dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
7242 return I40E_ERR_CONFIG;
7243 }
7244 filter->dst_ipv4 = key->dst;
7245 filter->src_ipv4 = key->src;
7246 }
7247
7248 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
7249 struct flow_dissector_key_ipv6_addrs *key =
7250 skb_flow_dissector_target(f->dissector,
7251 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
7252 f->key);
7253 struct flow_dissector_key_ipv6_addrs *mask =
7254 skb_flow_dissector_target(f->dissector,
7255 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
7256 f->mask);
7257
7258 /* src and dest IPV6 address should not be LOOPBACK
7259 * (0:0:0:0:0:0:0:1), which can be represented as ::1
7260 */
7261 if (ipv6_addr_loopback(&key->dst) ||
7262 ipv6_addr_loopback(&key->src)) {
7263 dev_err(&pf->pdev->dev,
7264 "Bad ipv6, addr is LOOPBACK\n");
7265 return I40E_ERR_CONFIG;
7266 }
7267 if (!ipv6_addr_any(&mask->dst) || !ipv6_addr_any(&mask->src))
7268 field_flags |= I40E_CLOUD_FIELD_IIP;
7269
7270 memcpy(&filter->src_ipv6, &key->src.s6_addr32,
7271 sizeof(filter->src_ipv6));
7272 memcpy(&filter->dst_ipv6, &key->dst.s6_addr32,
7273 sizeof(filter->dst_ipv6));
7274 }
7275
7276 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
7277 struct flow_dissector_key_ports *key =
7278 skb_flow_dissector_target(f->dissector,
7279 FLOW_DISSECTOR_KEY_PORTS,
7280 f->key);
7281 struct flow_dissector_key_ports *mask =
7282 skb_flow_dissector_target(f->dissector,
7283 FLOW_DISSECTOR_KEY_PORTS,
7284 f->mask);
7285
7286 if (mask->src) {
7287 if (mask->src == cpu_to_be16(0xffff)) {
7288 field_flags |= I40E_CLOUD_FIELD_IIP;
7289 } else {
7290 dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
7291 be16_to_cpu(mask->src));
7292 return I40E_ERR_CONFIG;
7293 }
7294 }
7295
7296 if (mask->dst) {
7297 if (mask->dst == cpu_to_be16(0xffff)) {
7298 field_flags |= I40E_CLOUD_FIELD_IIP;
7299 } else {
7300 dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
7301 be16_to_cpu(mask->dst));
7302 return I40E_ERR_CONFIG;
7303 }
7304 }
7305
7306 filter->dst_port = key->dst;
7307 filter->src_port = key->src;
7308
7309 switch (filter->ip_proto) {
7310 case IPPROTO_TCP:
7311 case IPPROTO_UDP:
7312 break;
7313 default:
7314 dev_err(&pf->pdev->dev,
7315 "Only UDP and TCP transport are supported\n");
7316 return -EINVAL;
7317 }
7318 }
7319 filter->flags = field_flags;
7320 return 0;
7321 }
7322
7323 /**
7324 * i40e_handle_tclass: Forward to a traffic class on the device
7325 * @vsi: Pointer to VSI
7326 * @tc: traffic class index on the device
7327 * @filter: Pointer to cloud filter structure
7328 *
7329 **/
7330 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
7331 struct i40e_cloud_filter *filter)
7332 {
7333 struct i40e_channel *ch, *ch_tmp;
7334
7335 /* direct to a traffic class on the same device */
7336 if (tc == 0) {
7337 filter->seid = vsi->seid;
7338 return 0;
7339 } else if (vsi->tc_config.enabled_tc & BIT(tc)) {
7340 if (!filter->dst_port) {
7341 dev_err(&vsi->back->pdev->dev,
7342 "Specify destination port to direct to traffic class that is not default\n");
7343 return -EINVAL;
7344 }
7345 if (list_empty(&vsi->ch_list))
7346 return -EINVAL;
7347 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
7348 list) {
7349 if (ch->seid == vsi->tc_seid_map[tc])
7350 filter->seid = ch->seid;
7351 }
7352 return 0;
7353 }
7354 dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
7355 return -EINVAL;
7356 }
7357
7358 /**
7359 * i40e_configure_clsflower - Configure tc flower filters
7360 * @vsi: Pointer to VSI
7361 * @cls_flower: Pointer to struct tc_cls_flower_offload
7362 *
7363 **/
7364 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
7365 struct tc_cls_flower_offload *cls_flower)
7366 {
7367 int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
7368 struct i40e_cloud_filter *filter = NULL;
7369 struct i40e_pf *pf = vsi->back;
7370 int err = 0;
7371
7372 if (tc < 0) {
7373 dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
7374 return -EOPNOTSUPP;
7375 }
7376
7377 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
7378 test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
7379 return -EBUSY;
7380
7381 if (pf->fdir_pf_active_filters ||
7382 (!hlist_empty(&pf->fdir_filter_list))) {
7383 dev_err(&vsi->back->pdev->dev,
7384 "Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
7385 return -EINVAL;
7386 }
7387
7388 if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
7389 dev_err(&vsi->back->pdev->dev,
7390 "Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
7391 vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7392 vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
7393 }
7394
7395 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
7396 if (!filter)
7397 return -ENOMEM;
7398
7399 filter->cookie = cls_flower->cookie;
7400
7401 err = i40e_parse_cls_flower(vsi, cls_flower, filter);
7402 if (err < 0)
7403 goto err;
7404
7405 err = i40e_handle_tclass(vsi, tc, filter);
7406 if (err < 0)
7407 goto err;
7408
7409 /* Add cloud filter */
7410 if (filter->dst_port)
7411 err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
7412 else
7413 err = i40e_add_del_cloud_filter(vsi, filter, true);
7414
7415 if (err) {
7416 dev_err(&pf->pdev->dev,
7417 "Failed to add cloud filter, err %s\n",
7418 i40e_stat_str(&pf->hw, err));
7419 goto err;
7420 }
7421
7422 /* add filter to the ordered list */
7423 INIT_HLIST_NODE(&filter->cloud_node);
7424
7425 hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
7426
7427 pf->num_cloud_filters++;
7428
7429 return err;
7430 err:
7431 kfree(filter);
7432 return err;
7433 }
7434
7435 /**
7436 * i40e_find_cloud_filter - Find the could filter in the list
7437 * @vsi: Pointer to VSI
7438 * @cookie: filter specific cookie
7439 *
7440 **/
7441 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
7442 unsigned long *cookie)
7443 {
7444 struct i40e_cloud_filter *filter = NULL;
7445 struct hlist_node *node2;
7446
7447 hlist_for_each_entry_safe(filter, node2,
7448 &vsi->back->cloud_filter_list, cloud_node)
7449 if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
7450 return filter;
7451 return NULL;
7452 }
7453
7454 /**
7455 * i40e_delete_clsflower - Remove tc flower filters
7456 * @vsi: Pointer to VSI
7457 * @cls_flower: Pointer to struct tc_cls_flower_offload
7458 *
7459 **/
7460 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
7461 struct tc_cls_flower_offload *cls_flower)
7462 {
7463 struct i40e_cloud_filter *filter = NULL;
7464 struct i40e_pf *pf = vsi->back;
7465 int err = 0;
7466
7467 filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
7468
7469 if (!filter)
7470 return -EINVAL;
7471
7472 hash_del(&filter->cloud_node);
7473
7474 if (filter->dst_port)
7475 err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
7476 else
7477 err = i40e_add_del_cloud_filter(vsi, filter, false);
7478
7479 kfree(filter);
7480 if (err) {
7481 dev_err(&pf->pdev->dev,
7482 "Failed to delete cloud filter, err %s\n",
7483 i40e_stat_str(&pf->hw, err));
7484 return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
7485 }
7486
7487 pf->num_cloud_filters--;
7488 if (!pf->num_cloud_filters)
7489 if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
7490 !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
7491 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
7492 pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
7493 pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
7494 }
7495 return 0;
7496 }
7497
7498 /**
7499 * i40e_setup_tc_cls_flower - flower classifier offloads
7500 * @netdev: net device to configure
7501 * @type_data: offload data
7502 **/
7503 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
7504 struct tc_cls_flower_offload *cls_flower)
7505 {
7506 struct i40e_vsi *vsi = np->vsi;
7507
7508 if (!tc_can_offload(vsi->netdev))
7509 return -EOPNOTSUPP;
7510 if (cls_flower->common.chain_index)
7511 return -EOPNOTSUPP;
7512
7513 switch (cls_flower->command) {
7514 case TC_CLSFLOWER_REPLACE:
7515 return i40e_configure_clsflower(vsi, cls_flower);
7516 case TC_CLSFLOWER_DESTROY:
7517 return i40e_delete_clsflower(vsi, cls_flower);
7518 case TC_CLSFLOWER_STATS:
7519 return -EOPNOTSUPP;
7520 default:
7521 return -EINVAL;
7522 }
7523 }
7524
7525 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
7526 void *cb_priv)
7527 {
7528 struct i40e_netdev_priv *np = cb_priv;
7529
7530 switch (type) {
7531 case TC_SETUP_CLSFLOWER:
7532 return i40e_setup_tc_cls_flower(np, type_data);
7533
7534 default:
7535 return -EOPNOTSUPP;
7536 }
7537 }
7538
7539 static int i40e_setup_tc_block(struct net_device *dev,
7540 struct tc_block_offload *f)
7541 {
7542 struct i40e_netdev_priv *np = netdev_priv(dev);
7543
7544 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
7545 return -EOPNOTSUPP;
7546
7547 switch (f->command) {
7548 case TC_BLOCK_BIND:
7549 return tcf_block_cb_register(f->block, i40e_setup_tc_block_cb,
7550 np, np);
7551 case TC_BLOCK_UNBIND:
7552 tcf_block_cb_unregister(f->block, i40e_setup_tc_block_cb, np);
7553 return 0;
7554 default:
7555 return -EOPNOTSUPP;
7556 }
7557 }
7558
7559 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
7560 void *type_data)
7561 {
7562 switch (type) {
7563 case TC_SETUP_QDISC_MQPRIO:
7564 return i40e_setup_tc(netdev, type_data);
7565 case TC_SETUP_BLOCK:
7566 return i40e_setup_tc_block(netdev, type_data);
7567 default:
7568 return -EOPNOTSUPP;
7569 }
7570 }
7571
7572 /**
7573 * i40e_open - Called when a network interface is made active
7574 * @netdev: network interface device structure
7575 *
7576 * The open entry point is called when a network interface is made
7577 * active by the system (IFF_UP). At this point all resources needed
7578 * for transmit and receive operations are allocated, the interrupt
7579 * handler is registered with the OS, the netdev watchdog subtask is
7580 * enabled, and the stack is notified that the interface is ready.
7581 *
7582 * Returns 0 on success, negative value on failure
7583 **/
7584 int i40e_open(struct net_device *netdev)
7585 {
7586 struct i40e_netdev_priv *np = netdev_priv(netdev);
7587 struct i40e_vsi *vsi = np->vsi;
7588 struct i40e_pf *pf = vsi->back;
7589 int err;
7590
7591 /* disallow open during test or if eeprom is broken */
7592 if (test_bit(__I40E_TESTING, pf->state) ||
7593 test_bit(__I40E_BAD_EEPROM, pf->state))
7594 return -EBUSY;
7595
7596 netif_carrier_off(netdev);
7597
7598 err = i40e_vsi_open(vsi);
7599 if (err)
7600 return err;
7601
7602 /* configure global TSO hardware offload settings */
7603 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
7604 TCP_FLAG_FIN) >> 16);
7605 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
7606 TCP_FLAG_FIN |
7607 TCP_FLAG_CWR) >> 16);
7608 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
7609
7610 udp_tunnel_get_rx_info(netdev);
7611
7612 return 0;
7613 }
7614
7615 /**
7616 * i40e_vsi_open -
7617 * @vsi: the VSI to open
7618 *
7619 * Finish initialization of the VSI.
7620 *
7621 * Returns 0 on success, negative value on failure
7622 *
7623 * Note: expects to be called while under rtnl_lock()
7624 **/
7625 int i40e_vsi_open(struct i40e_vsi *vsi)
7626 {
7627 struct i40e_pf *pf = vsi->back;
7628 char int_name[I40E_INT_NAME_STR_LEN];
7629 int err;
7630
7631 /* allocate descriptors */
7632 err = i40e_vsi_setup_tx_resources(vsi);
7633 if (err)
7634 goto err_setup_tx;
7635 err = i40e_vsi_setup_rx_resources(vsi);
7636 if (err)
7637 goto err_setup_rx;
7638
7639 err = i40e_vsi_configure(vsi);
7640 if (err)
7641 goto err_setup_rx;
7642
7643 if (vsi->netdev) {
7644 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
7645 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
7646 err = i40e_vsi_request_irq(vsi, int_name);
7647 if (err)
7648 goto err_setup_rx;
7649
7650 /* Notify the stack of the actual queue counts. */
7651 err = netif_set_real_num_tx_queues(vsi->netdev,
7652 vsi->num_queue_pairs);
7653 if (err)
7654 goto err_set_queues;
7655
7656 err = netif_set_real_num_rx_queues(vsi->netdev,
7657 vsi->num_queue_pairs);
7658 if (err)
7659 goto err_set_queues;
7660
7661 } else if (vsi->type == I40E_VSI_FDIR) {
7662 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
7663 dev_driver_string(&pf->pdev->dev),
7664 dev_name(&pf->pdev->dev));
7665 err = i40e_vsi_request_irq(vsi, int_name);
7666
7667 } else {
7668 err = -EINVAL;
7669 goto err_setup_rx;
7670 }
7671
7672 err = i40e_up_complete(vsi);
7673 if (err)
7674 goto err_up_complete;
7675
7676 return 0;
7677
7678 err_up_complete:
7679 i40e_down(vsi);
7680 err_set_queues:
7681 i40e_vsi_free_irq(vsi);
7682 err_setup_rx:
7683 i40e_vsi_free_rx_resources(vsi);
7684 err_setup_tx:
7685 i40e_vsi_free_tx_resources(vsi);
7686 if (vsi == pf->vsi[pf->lan_vsi])
7687 i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
7688
7689 return err;
7690 }
7691
7692 /**
7693 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
7694 * @pf: Pointer to PF
7695 *
7696 * This function destroys the hlist where all the Flow Director
7697 * filters were saved.
7698 **/
7699 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
7700 {
7701 struct i40e_fdir_filter *filter;
7702 struct i40e_flex_pit *pit_entry, *tmp;
7703 struct hlist_node *node2;
7704
7705 hlist_for_each_entry_safe(filter, node2,
7706 &pf->fdir_filter_list, fdir_node) {
7707 hlist_del(&filter->fdir_node);
7708 kfree(filter);
7709 }
7710
7711 list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
7712 list_del(&pit_entry->list);
7713 kfree(pit_entry);
7714 }
7715 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
7716
7717 list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
7718 list_del(&pit_entry->list);
7719 kfree(pit_entry);
7720 }
7721 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
7722
7723 pf->fdir_pf_active_filters = 0;
7724 pf->fd_tcp4_filter_cnt = 0;
7725 pf->fd_udp4_filter_cnt = 0;
7726 pf->fd_sctp4_filter_cnt = 0;
7727 pf->fd_ip4_filter_cnt = 0;
7728
7729 /* Reprogram the default input set for TCP/IPv4 */
7730 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
7731 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
7732 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
7733
7734 /* Reprogram the default input set for UDP/IPv4 */
7735 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
7736 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
7737 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
7738
7739 /* Reprogram the default input set for SCTP/IPv4 */
7740 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
7741 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
7742 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
7743
7744 /* Reprogram the default input set for Other/IPv4 */
7745 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
7746 I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
7747
7748 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
7749 I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
7750 }
7751
7752 /**
7753 * i40e_cloud_filter_exit - Cleans up the cloud filters
7754 * @pf: Pointer to PF
7755 *
7756 * This function destroys the hlist where all the cloud filters
7757 * were saved.
7758 **/
7759 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
7760 {
7761 struct i40e_cloud_filter *cfilter;
7762 struct hlist_node *node;
7763
7764 hlist_for_each_entry_safe(cfilter, node,
7765 &pf->cloud_filter_list, cloud_node) {
7766 hlist_del(&cfilter->cloud_node);
7767 kfree(cfilter);
7768 }
7769 pf->num_cloud_filters = 0;
7770
7771 if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
7772 !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
7773 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
7774 pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
7775 pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
7776 }
7777 }
7778
7779 /**
7780 * i40e_close - Disables a network interface
7781 * @netdev: network interface device structure
7782 *
7783 * The close entry point is called when an interface is de-activated
7784 * by the OS. The hardware is still under the driver's control, but
7785 * this netdev interface is disabled.
7786 *
7787 * Returns 0, this is not allowed to fail
7788 **/
7789 int i40e_close(struct net_device *netdev)
7790 {
7791 struct i40e_netdev_priv *np = netdev_priv(netdev);
7792 struct i40e_vsi *vsi = np->vsi;
7793
7794 i40e_vsi_close(vsi);
7795
7796 return 0;
7797 }
7798
7799 /**
7800 * i40e_do_reset - Start a PF or Core Reset sequence
7801 * @pf: board private structure
7802 * @reset_flags: which reset is requested
7803 * @lock_acquired: indicates whether or not the lock has been acquired
7804 * before this function was called.
7805 *
7806 * The essential difference in resets is that the PF Reset
7807 * doesn't clear the packet buffers, doesn't reset the PE
7808 * firmware, and doesn't bother the other PFs on the chip.
7809 **/
7810 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
7811 {
7812 u32 val;
7813
7814 WARN_ON(in_interrupt());
7815
7816
7817 /* do the biggest reset indicated */
7818 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
7819
7820 /* Request a Global Reset
7821 *
7822 * This will start the chip's countdown to the actual full
7823 * chip reset event, and a warning interrupt to be sent
7824 * to all PFs, including the requestor. Our handler
7825 * for the warning interrupt will deal with the shutdown
7826 * and recovery of the switch setup.
7827 */
7828 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
7829 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
7830 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
7831 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
7832
7833 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
7834
7835 /* Request a Core Reset
7836 *
7837 * Same as Global Reset, except does *not* include the MAC/PHY
7838 */
7839 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
7840 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
7841 val |= I40E_GLGEN_RTRIG_CORER_MASK;
7842 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
7843 i40e_flush(&pf->hw);
7844
7845 } else if (reset_flags & I40E_PF_RESET_FLAG) {
7846
7847 /* Request a PF Reset
7848 *
7849 * Resets only the PF-specific registers
7850 *
7851 * This goes directly to the tear-down and rebuild of
7852 * the switch, since we need to do all the recovery as
7853 * for the Core Reset.
7854 */
7855 dev_dbg(&pf->pdev->dev, "PFR requested\n");
7856 i40e_handle_reset_warning(pf, lock_acquired);
7857
7858 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
7859 int v;
7860
7861 /* Find the VSI(s) that requested a re-init */
7862 dev_info(&pf->pdev->dev,
7863 "VSI reinit requested\n");
7864 for (v = 0; v < pf->num_alloc_vsi; v++) {
7865 struct i40e_vsi *vsi = pf->vsi[v];
7866
7867 if (vsi != NULL &&
7868 test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
7869 vsi->state))
7870 i40e_vsi_reinit_locked(pf->vsi[v]);
7871 }
7872 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
7873 int v;
7874
7875 /* Find the VSI(s) that needs to be brought down */
7876 dev_info(&pf->pdev->dev, "VSI down requested\n");
7877 for (v = 0; v < pf->num_alloc_vsi; v++) {
7878 struct i40e_vsi *vsi = pf->vsi[v];
7879
7880 if (vsi != NULL &&
7881 test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
7882 vsi->state)) {
7883 set_bit(__I40E_VSI_DOWN, vsi->state);
7884 i40e_down(vsi);
7885 }
7886 }
7887 } else {
7888 dev_info(&pf->pdev->dev,
7889 "bad reset request 0x%08x\n", reset_flags);
7890 }
7891 }
7892
7893 #ifdef CONFIG_I40E_DCB
7894 /**
7895 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
7896 * @pf: board private structure
7897 * @old_cfg: current DCB config
7898 * @new_cfg: new DCB config
7899 **/
7900 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
7901 struct i40e_dcbx_config *old_cfg,
7902 struct i40e_dcbx_config *new_cfg)
7903 {
7904 bool need_reconfig = false;
7905
7906 /* Check if ETS configuration has changed */
7907 if (memcmp(&new_cfg->etscfg,
7908 &old_cfg->etscfg,
7909 sizeof(new_cfg->etscfg))) {
7910 /* If Priority Table has changed reconfig is needed */
7911 if (memcmp(&new_cfg->etscfg.prioritytable,
7912 &old_cfg->etscfg.prioritytable,
7913 sizeof(new_cfg->etscfg.prioritytable))) {
7914 need_reconfig = true;
7915 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
7916 }
7917
7918 if (memcmp(&new_cfg->etscfg.tcbwtable,
7919 &old_cfg->etscfg.tcbwtable,
7920 sizeof(new_cfg->etscfg.tcbwtable)))
7921 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
7922
7923 if (memcmp(&new_cfg->etscfg.tsatable,
7924 &old_cfg->etscfg.tsatable,
7925 sizeof(new_cfg->etscfg.tsatable)))
7926 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
7927 }
7928
7929 /* Check if PFC configuration has changed */
7930 if (memcmp(&new_cfg->pfc,
7931 &old_cfg->pfc,
7932 sizeof(new_cfg->pfc))) {
7933 need_reconfig = true;
7934 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
7935 }
7936
7937 /* Check if APP Table has changed */
7938 if (memcmp(&new_cfg->app,
7939 &old_cfg->app,
7940 sizeof(new_cfg->app))) {
7941 need_reconfig = true;
7942 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
7943 }
7944
7945 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
7946 return need_reconfig;
7947 }
7948
7949 /**
7950 * i40e_handle_lldp_event - Handle LLDP Change MIB event
7951 * @pf: board private structure
7952 * @e: event info posted on ARQ
7953 **/
7954 static int i40e_handle_lldp_event(struct i40e_pf *pf,
7955 struct i40e_arq_event_info *e)
7956 {
7957 struct i40e_aqc_lldp_get_mib *mib =
7958 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
7959 struct i40e_hw *hw = &pf->hw;
7960 struct i40e_dcbx_config tmp_dcbx_cfg;
7961 bool need_reconfig = false;
7962 int ret = 0;
7963 u8 type;
7964
7965 /* Not DCB capable or capability disabled */
7966 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
7967 return ret;
7968
7969 /* Ignore if event is not for Nearest Bridge */
7970 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
7971 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
7972 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
7973 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
7974 return ret;
7975
7976 /* Check MIB Type and return if event for Remote MIB update */
7977 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
7978 dev_dbg(&pf->pdev->dev,
7979 "LLDP event mib type %s\n", type ? "remote" : "local");
7980 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
7981 /* Update the remote cached instance and return */
7982 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
7983 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
7984 &hw->remote_dcbx_config);
7985 goto exit;
7986 }
7987
7988 /* Store the old configuration */
7989 tmp_dcbx_cfg = hw->local_dcbx_config;
7990
7991 /* Reset the old DCBx configuration data */
7992 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
7993 /* Get updated DCBX data from firmware */
7994 ret = i40e_get_dcb_config(&pf->hw);
7995 if (ret) {
7996 dev_info(&pf->pdev->dev,
7997 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
7998 i40e_stat_str(&pf->hw, ret),
7999 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8000 goto exit;
8001 }
8002
8003 /* No change detected in DCBX configs */
8004 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
8005 sizeof(tmp_dcbx_cfg))) {
8006 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
8007 goto exit;
8008 }
8009
8010 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
8011 &hw->local_dcbx_config);
8012
8013 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
8014
8015 if (!need_reconfig)
8016 goto exit;
8017
8018 /* Enable DCB tagging only when more than one TC */
8019 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
8020 pf->flags |= I40E_FLAG_DCB_ENABLED;
8021 else
8022 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8023
8024 set_bit(__I40E_PORT_SUSPENDED, pf->state);
8025 /* Reconfiguration needed quiesce all VSIs */
8026 i40e_pf_quiesce_all_vsi(pf);
8027
8028 /* Changes in configuration update VEB/VSI */
8029 i40e_dcb_reconfigure(pf);
8030
8031 ret = i40e_resume_port_tx(pf);
8032
8033 clear_bit(__I40E_PORT_SUSPENDED, pf->state);
8034 /* In case of error no point in resuming VSIs */
8035 if (ret)
8036 goto exit;
8037
8038 /* Wait for the PF's queues to be disabled */
8039 ret = i40e_pf_wait_queues_disabled(pf);
8040 if (ret) {
8041 /* Schedule PF reset to recover */
8042 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
8043 i40e_service_event_schedule(pf);
8044 } else {
8045 i40e_pf_unquiesce_all_vsi(pf);
8046 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
8047 I40E_FLAG_CLIENT_L2_CHANGE);
8048 }
8049
8050 exit:
8051 return ret;
8052 }
8053 #endif /* CONFIG_I40E_DCB */
8054
8055 /**
8056 * i40e_do_reset_safe - Protected reset path for userland calls.
8057 * @pf: board private structure
8058 * @reset_flags: which reset is requested
8059 *
8060 **/
8061 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
8062 {
8063 rtnl_lock();
8064 i40e_do_reset(pf, reset_flags, true);
8065 rtnl_unlock();
8066 }
8067
8068 /**
8069 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
8070 * @pf: board private structure
8071 * @e: event info posted on ARQ
8072 *
8073 * Handler for LAN Queue Overflow Event generated by the firmware for PF
8074 * and VF queues
8075 **/
8076 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
8077 struct i40e_arq_event_info *e)
8078 {
8079 struct i40e_aqc_lan_overflow *data =
8080 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
8081 u32 queue = le32_to_cpu(data->prtdcb_rupto);
8082 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
8083 struct i40e_hw *hw = &pf->hw;
8084 struct i40e_vf *vf;
8085 u16 vf_id;
8086
8087 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
8088 queue, qtx_ctl);
8089
8090 /* Queue belongs to VF, find the VF and issue VF reset */
8091 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
8092 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
8093 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
8094 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
8095 vf_id -= hw->func_caps.vf_base_id;
8096 vf = &pf->vf[vf_id];
8097 i40e_vc_notify_vf_reset(vf);
8098 /* Allow VF to process pending reset notification */
8099 msleep(20);
8100 i40e_reset_vf(vf, false);
8101 }
8102 }
8103
8104 /**
8105 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
8106 * @pf: board private structure
8107 **/
8108 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
8109 {
8110 u32 val, fcnt_prog;
8111
8112 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
8113 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
8114 return fcnt_prog;
8115 }
8116
8117 /**
8118 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
8119 * @pf: board private structure
8120 **/
8121 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
8122 {
8123 u32 val, fcnt_prog;
8124
8125 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
8126 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
8127 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
8128 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
8129 return fcnt_prog;
8130 }
8131
8132 /**
8133 * i40e_get_global_fd_count - Get total FD filters programmed on device
8134 * @pf: board private structure
8135 **/
8136 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
8137 {
8138 u32 val, fcnt_prog;
8139
8140 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
8141 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
8142 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
8143 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
8144 return fcnt_prog;
8145 }
8146
8147 /**
8148 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
8149 * @pf: board private structure
8150 **/
8151 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
8152 {
8153 struct i40e_fdir_filter *filter;
8154 u32 fcnt_prog, fcnt_avail;
8155 struct hlist_node *node;
8156
8157 if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
8158 return;
8159
8160 /* Check if we have enough room to re-enable FDir SB capability. */
8161 fcnt_prog = i40e_get_global_fd_count(pf);
8162 fcnt_avail = pf->fdir_pf_filter_count;
8163 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
8164 (pf->fd_add_err == 0) ||
8165 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
8166 if (pf->flags & I40E_FLAG_FD_SB_AUTO_DISABLED) {
8167 pf->flags &= ~I40E_FLAG_FD_SB_AUTO_DISABLED;
8168 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
8169 (I40E_DEBUG_FD & pf->hw.debug_mask))
8170 dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
8171 }
8172 }
8173
8174 /* We should wait for even more space before re-enabling ATR.
8175 * Additionally, we cannot enable ATR as long as we still have TCP SB
8176 * rules active.
8177 */
8178 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
8179 (pf->fd_tcp4_filter_cnt == 0)) {
8180 if (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED) {
8181 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
8182 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
8183 (I40E_DEBUG_FD & pf->hw.debug_mask))
8184 dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
8185 }
8186 }
8187
8188 /* if hw had a problem adding a filter, delete it */
8189 if (pf->fd_inv > 0) {
8190 hlist_for_each_entry_safe(filter, node,
8191 &pf->fdir_filter_list, fdir_node) {
8192 if (filter->fd_id == pf->fd_inv) {
8193 hlist_del(&filter->fdir_node);
8194 kfree(filter);
8195 pf->fdir_pf_active_filters--;
8196 pf->fd_inv = 0;
8197 }
8198 }
8199 }
8200 }
8201
8202 #define I40E_MIN_FD_FLUSH_INTERVAL 10
8203 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
8204 /**
8205 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
8206 * @pf: board private structure
8207 **/
8208 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
8209 {
8210 unsigned long min_flush_time;
8211 int flush_wait_retry = 50;
8212 bool disable_atr = false;
8213 int fd_room;
8214 int reg;
8215
8216 if (!time_after(jiffies, pf->fd_flush_timestamp +
8217 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
8218 return;
8219
8220 /* If the flush is happening too quick and we have mostly SB rules we
8221 * should not re-enable ATR for some time.
8222 */
8223 min_flush_time = pf->fd_flush_timestamp +
8224 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
8225 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
8226
8227 if (!(time_after(jiffies, min_flush_time)) &&
8228 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
8229 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8230 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
8231 disable_atr = true;
8232 }
8233
8234 pf->fd_flush_timestamp = jiffies;
8235 pf->flags |= I40E_FLAG_FD_ATR_AUTO_DISABLED;
8236 /* flush all filters */
8237 wr32(&pf->hw, I40E_PFQF_CTL_1,
8238 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
8239 i40e_flush(&pf->hw);
8240 pf->fd_flush_cnt++;
8241 pf->fd_add_err = 0;
8242 do {
8243 /* Check FD flush status every 5-6msec */
8244 usleep_range(5000, 6000);
8245 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
8246 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
8247 break;
8248 } while (flush_wait_retry--);
8249 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
8250 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
8251 } else {
8252 /* replay sideband filters */
8253 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
8254 if (!disable_atr && !pf->fd_tcp4_filter_cnt)
8255 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
8256 clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
8257 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8258 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
8259 }
8260 }
8261
8262 /**
8263 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
8264 * @pf: board private structure
8265 **/
8266 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
8267 {
8268 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
8269 }
8270
8271 /* We can see up to 256 filter programming desc in transit if the filters are
8272 * being applied really fast; before we see the first
8273 * filter miss error on Rx queue 0. Accumulating enough error messages before
8274 * reacting will make sure we don't cause flush too often.
8275 */
8276 #define I40E_MAX_FD_PROGRAM_ERROR 256
8277
8278 /**
8279 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
8280 * @pf: board private structure
8281 **/
8282 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
8283 {
8284
8285 /* if interface is down do nothing */
8286 if (test_bit(__I40E_DOWN, pf->state))
8287 return;
8288
8289 if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
8290 i40e_fdir_flush_and_replay(pf);
8291
8292 i40e_fdir_check_and_reenable(pf);
8293
8294 }
8295
8296 /**
8297 * i40e_vsi_link_event - notify VSI of a link event
8298 * @vsi: vsi to be notified
8299 * @link_up: link up or down
8300 **/
8301 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
8302 {
8303 if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
8304 return;
8305
8306 switch (vsi->type) {
8307 case I40E_VSI_MAIN:
8308 if (!vsi->netdev || !vsi->netdev_registered)
8309 break;
8310
8311 if (link_up) {
8312 netif_carrier_on(vsi->netdev);
8313 netif_tx_wake_all_queues(vsi->netdev);
8314 } else {
8315 netif_carrier_off(vsi->netdev);
8316 netif_tx_stop_all_queues(vsi->netdev);
8317 }
8318 break;
8319
8320 case I40E_VSI_SRIOV:
8321 case I40E_VSI_VMDQ2:
8322 case I40E_VSI_CTRL:
8323 case I40E_VSI_IWARP:
8324 case I40E_VSI_MIRROR:
8325 default:
8326 /* there is no notification for other VSIs */
8327 break;
8328 }
8329 }
8330
8331 /**
8332 * i40e_veb_link_event - notify elements on the veb of a link event
8333 * @veb: veb to be notified
8334 * @link_up: link up or down
8335 **/
8336 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
8337 {
8338 struct i40e_pf *pf;
8339 int i;
8340
8341 if (!veb || !veb->pf)
8342 return;
8343 pf = veb->pf;
8344
8345 /* depth first... */
8346 for (i = 0; i < I40E_MAX_VEB; i++)
8347 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
8348 i40e_veb_link_event(pf->veb[i], link_up);
8349
8350 /* ... now the local VSIs */
8351 for (i = 0; i < pf->num_alloc_vsi; i++)
8352 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
8353 i40e_vsi_link_event(pf->vsi[i], link_up);
8354 }
8355
8356 /**
8357 * i40e_link_event - Update netif_carrier status
8358 * @pf: board private structure
8359 **/
8360 static void i40e_link_event(struct i40e_pf *pf)
8361 {
8362 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8363 u8 new_link_speed, old_link_speed;
8364 i40e_status status;
8365 bool new_link, old_link;
8366
8367 /* save off old link status information */
8368 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
8369
8370 /* set this to force the get_link_status call to refresh state */
8371 pf->hw.phy.get_link_info = true;
8372
8373 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
8374
8375 status = i40e_get_link_status(&pf->hw, &new_link);
8376
8377 /* On success, disable temp link polling */
8378 if (status == I40E_SUCCESS) {
8379 if (pf->flags & I40E_FLAG_TEMP_LINK_POLLING)
8380 pf->flags &= ~I40E_FLAG_TEMP_LINK_POLLING;
8381 } else {
8382 /* Enable link polling temporarily until i40e_get_link_status
8383 * returns I40E_SUCCESS
8384 */
8385 pf->flags |= I40E_FLAG_TEMP_LINK_POLLING;
8386 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
8387 status);
8388 return;
8389 }
8390
8391 old_link_speed = pf->hw.phy.link_info_old.link_speed;
8392 new_link_speed = pf->hw.phy.link_info.link_speed;
8393
8394 if (new_link == old_link &&
8395 new_link_speed == old_link_speed &&
8396 (test_bit(__I40E_VSI_DOWN, vsi->state) ||
8397 new_link == netif_carrier_ok(vsi->netdev)))
8398 return;
8399
8400 i40e_print_link_message(vsi, new_link);
8401
8402 /* Notify the base of the switch tree connected to
8403 * the link. Floating VEBs are not notified.
8404 */
8405 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
8406 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
8407 else
8408 i40e_vsi_link_event(vsi, new_link);
8409
8410 if (pf->vf)
8411 i40e_vc_notify_link_state(pf);
8412
8413 if (pf->flags & I40E_FLAG_PTP)
8414 i40e_ptp_set_increment(pf);
8415 }
8416
8417 /**
8418 * i40e_watchdog_subtask - periodic checks not using event driven response
8419 * @pf: board private structure
8420 **/
8421 static void i40e_watchdog_subtask(struct i40e_pf *pf)
8422 {
8423 int i;
8424
8425 /* if interface is down do nothing */
8426 if (test_bit(__I40E_DOWN, pf->state) ||
8427 test_bit(__I40E_CONFIG_BUSY, pf->state))
8428 return;
8429
8430 /* make sure we don't do these things too often */
8431 if (time_before(jiffies, (pf->service_timer_previous +
8432 pf->service_timer_period)))
8433 return;
8434 pf->service_timer_previous = jiffies;
8435
8436 if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
8437 (pf->flags & I40E_FLAG_TEMP_LINK_POLLING))
8438 i40e_link_event(pf);
8439
8440 /* Update the stats for active netdevs so the network stack
8441 * can look at updated numbers whenever it cares to
8442 */
8443 for (i = 0; i < pf->num_alloc_vsi; i++)
8444 if (pf->vsi[i] && pf->vsi[i]->netdev)
8445 i40e_update_stats(pf->vsi[i]);
8446
8447 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
8448 /* Update the stats for the active switching components */
8449 for (i = 0; i < I40E_MAX_VEB; i++)
8450 if (pf->veb[i])
8451 i40e_update_veb_stats(pf->veb[i]);
8452 }
8453
8454 i40e_ptp_rx_hang(pf);
8455 i40e_ptp_tx_hang(pf);
8456 }
8457
8458 /**
8459 * i40e_reset_subtask - Set up for resetting the device and driver
8460 * @pf: board private structure
8461 **/
8462 static void i40e_reset_subtask(struct i40e_pf *pf)
8463 {
8464 u32 reset_flags = 0;
8465
8466 if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
8467 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
8468 clear_bit(__I40E_REINIT_REQUESTED, pf->state);
8469 }
8470 if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
8471 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
8472 clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
8473 }
8474 if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
8475 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
8476 clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
8477 }
8478 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
8479 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
8480 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
8481 }
8482 if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
8483 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
8484 clear_bit(__I40E_DOWN_REQUESTED, pf->state);
8485 }
8486
8487 /* If there's a recovery already waiting, it takes
8488 * precedence before starting a new reset sequence.
8489 */
8490 if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
8491 i40e_prep_for_reset(pf, false);
8492 i40e_reset(pf);
8493 i40e_rebuild(pf, false, false);
8494 }
8495
8496 /* If we're already down or resetting, just bail */
8497 if (reset_flags &&
8498 !test_bit(__I40E_DOWN, pf->state) &&
8499 !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
8500 i40e_do_reset(pf, reset_flags, false);
8501 }
8502 }
8503
8504 /**
8505 * i40e_handle_link_event - Handle link event
8506 * @pf: board private structure
8507 * @e: event info posted on ARQ
8508 **/
8509 static void i40e_handle_link_event(struct i40e_pf *pf,
8510 struct i40e_arq_event_info *e)
8511 {
8512 struct i40e_aqc_get_link_status *status =
8513 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
8514
8515 /* Do a new status request to re-enable LSE reporting
8516 * and load new status information into the hw struct
8517 * This completely ignores any state information
8518 * in the ARQ event info, instead choosing to always
8519 * issue the AQ update link status command.
8520 */
8521 i40e_link_event(pf);
8522
8523 /* Check if module meets thermal requirements */
8524 if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
8525 dev_err(&pf->pdev->dev,
8526 "Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
8527 dev_err(&pf->pdev->dev,
8528 "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
8529 } else {
8530 /* check for unqualified module, if link is down, suppress
8531 * the message if link was forced to be down.
8532 */
8533 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
8534 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
8535 (!(status->link_info & I40E_AQ_LINK_UP)) &&
8536 (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
8537 dev_err(&pf->pdev->dev,
8538 "Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
8539 dev_err(&pf->pdev->dev,
8540 "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
8541 }
8542 }
8543 }
8544
8545 /**
8546 * i40e_clean_adminq_subtask - Clean the AdminQ rings
8547 * @pf: board private structure
8548 **/
8549 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
8550 {
8551 struct i40e_arq_event_info event;
8552 struct i40e_hw *hw = &pf->hw;
8553 u16 pending, i = 0;
8554 i40e_status ret;
8555 u16 opcode;
8556 u32 oldval;
8557 u32 val;
8558
8559 /* Do not run clean AQ when PF reset fails */
8560 if (test_bit(__I40E_RESET_FAILED, pf->state))
8561 return;
8562
8563 /* check for error indications */
8564 val = rd32(&pf->hw, pf->hw.aq.arq.len);
8565 oldval = val;
8566 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
8567 if (hw->debug_mask & I40E_DEBUG_AQ)
8568 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
8569 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
8570 }
8571 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
8572 if (hw->debug_mask & I40E_DEBUG_AQ)
8573 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
8574 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
8575 pf->arq_overflows++;
8576 }
8577 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
8578 if (hw->debug_mask & I40E_DEBUG_AQ)
8579 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
8580 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
8581 }
8582 if (oldval != val)
8583 wr32(&pf->hw, pf->hw.aq.arq.len, val);
8584
8585 val = rd32(&pf->hw, pf->hw.aq.asq.len);
8586 oldval = val;
8587 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
8588 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
8589 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
8590 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
8591 }
8592 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
8593 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
8594 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
8595 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
8596 }
8597 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
8598 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
8599 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
8600 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
8601 }
8602 if (oldval != val)
8603 wr32(&pf->hw, pf->hw.aq.asq.len, val);
8604
8605 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
8606 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
8607 if (!event.msg_buf)
8608 return;
8609
8610 do {
8611 ret = i40e_clean_arq_element(hw, &event, &pending);
8612 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
8613 break;
8614 else if (ret) {
8615 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
8616 break;
8617 }
8618
8619 opcode = le16_to_cpu(event.desc.opcode);
8620 switch (opcode) {
8621
8622 case i40e_aqc_opc_get_link_status:
8623 i40e_handle_link_event(pf, &event);
8624 break;
8625 case i40e_aqc_opc_send_msg_to_pf:
8626 ret = i40e_vc_process_vf_msg(pf,
8627 le16_to_cpu(event.desc.retval),
8628 le32_to_cpu(event.desc.cookie_high),
8629 le32_to_cpu(event.desc.cookie_low),
8630 event.msg_buf,
8631 event.msg_len);
8632 break;
8633 case i40e_aqc_opc_lldp_update_mib:
8634 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
8635 #ifdef CONFIG_I40E_DCB
8636 rtnl_lock();
8637 ret = i40e_handle_lldp_event(pf, &event);
8638 rtnl_unlock();
8639 #endif /* CONFIG_I40E_DCB */
8640 break;
8641 case i40e_aqc_opc_event_lan_overflow:
8642 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
8643 i40e_handle_lan_overflow_event(pf, &event);
8644 break;
8645 case i40e_aqc_opc_send_msg_to_peer:
8646 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
8647 break;
8648 case i40e_aqc_opc_nvm_erase:
8649 case i40e_aqc_opc_nvm_update:
8650 case i40e_aqc_opc_oem_post_update:
8651 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
8652 "ARQ NVM operation 0x%04x completed\n",
8653 opcode);
8654 break;
8655 default:
8656 dev_info(&pf->pdev->dev,
8657 "ARQ: Unknown event 0x%04x ignored\n",
8658 opcode);
8659 break;
8660 }
8661 } while (i++ < pf->adminq_work_limit);
8662
8663 if (i < pf->adminq_work_limit)
8664 clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
8665
8666 /* re-enable Admin queue interrupt cause */
8667 val = rd32(hw, I40E_PFINT_ICR0_ENA);
8668 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
8669 wr32(hw, I40E_PFINT_ICR0_ENA, val);
8670 i40e_flush(hw);
8671
8672 kfree(event.msg_buf);
8673 }
8674
8675 /**
8676 * i40e_verify_eeprom - make sure eeprom is good to use
8677 * @pf: board private structure
8678 **/
8679 static void i40e_verify_eeprom(struct i40e_pf *pf)
8680 {
8681 int err;
8682
8683 err = i40e_diag_eeprom_test(&pf->hw);
8684 if (err) {
8685 /* retry in case of garbage read */
8686 err = i40e_diag_eeprom_test(&pf->hw);
8687 if (err) {
8688 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
8689 err);
8690 set_bit(__I40E_BAD_EEPROM, pf->state);
8691 }
8692 }
8693
8694 if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
8695 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
8696 clear_bit(__I40E_BAD_EEPROM, pf->state);
8697 }
8698 }
8699
8700 /**
8701 * i40e_enable_pf_switch_lb
8702 * @pf: pointer to the PF structure
8703 *
8704 * enable switch loop back or die - no point in a return value
8705 **/
8706 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
8707 {
8708 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8709 struct i40e_vsi_context ctxt;
8710 int ret;
8711
8712 ctxt.seid = pf->main_vsi_seid;
8713 ctxt.pf_num = pf->hw.pf_id;
8714 ctxt.vf_num = 0;
8715 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
8716 if (ret) {
8717 dev_info(&pf->pdev->dev,
8718 "couldn't get PF vsi config, err %s aq_err %s\n",
8719 i40e_stat_str(&pf->hw, ret),
8720 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8721 return;
8722 }
8723 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
8724 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
8725 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
8726
8727 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
8728 if (ret) {
8729 dev_info(&pf->pdev->dev,
8730 "update vsi switch failed, err %s aq_err %s\n",
8731 i40e_stat_str(&pf->hw, ret),
8732 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8733 }
8734 }
8735
8736 /**
8737 * i40e_disable_pf_switch_lb
8738 * @pf: pointer to the PF structure
8739 *
8740 * disable switch loop back or die - no point in a return value
8741 **/
8742 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
8743 {
8744 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8745 struct i40e_vsi_context ctxt;
8746 int ret;
8747
8748 ctxt.seid = pf->main_vsi_seid;
8749 ctxt.pf_num = pf->hw.pf_id;
8750 ctxt.vf_num = 0;
8751 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
8752 if (ret) {
8753 dev_info(&pf->pdev->dev,
8754 "couldn't get PF vsi config, err %s aq_err %s\n",
8755 i40e_stat_str(&pf->hw, ret),
8756 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8757 return;
8758 }
8759 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
8760 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
8761 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
8762
8763 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
8764 if (ret) {
8765 dev_info(&pf->pdev->dev,
8766 "update vsi switch failed, err %s aq_err %s\n",
8767 i40e_stat_str(&pf->hw, ret),
8768 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8769 }
8770 }
8771
8772 /**
8773 * i40e_config_bridge_mode - Configure the HW bridge mode
8774 * @veb: pointer to the bridge instance
8775 *
8776 * Configure the loop back mode for the LAN VSI that is downlink to the
8777 * specified HW bridge instance. It is expected this function is called
8778 * when a new HW bridge is instantiated.
8779 **/
8780 static void i40e_config_bridge_mode(struct i40e_veb *veb)
8781 {
8782 struct i40e_pf *pf = veb->pf;
8783
8784 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
8785 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
8786 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
8787 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
8788 i40e_disable_pf_switch_lb(pf);
8789 else
8790 i40e_enable_pf_switch_lb(pf);
8791 }
8792
8793 /**
8794 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
8795 * @veb: pointer to the VEB instance
8796 *
8797 * This is a recursive function that first builds the attached VSIs then
8798 * recurses in to build the next layer of VEB. We track the connections
8799 * through our own index numbers because the seid's from the HW could
8800 * change across the reset.
8801 **/
8802 static int i40e_reconstitute_veb(struct i40e_veb *veb)
8803 {
8804 struct i40e_vsi *ctl_vsi = NULL;
8805 struct i40e_pf *pf = veb->pf;
8806 int v, veb_idx;
8807 int ret;
8808
8809 /* build VSI that owns this VEB, temporarily attached to base VEB */
8810 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
8811 if (pf->vsi[v] &&
8812 pf->vsi[v]->veb_idx == veb->idx &&
8813 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
8814 ctl_vsi = pf->vsi[v];
8815 break;
8816 }
8817 }
8818 if (!ctl_vsi) {
8819 dev_info(&pf->pdev->dev,
8820 "missing owner VSI for veb_idx %d\n", veb->idx);
8821 ret = -ENOENT;
8822 goto end_reconstitute;
8823 }
8824 if (ctl_vsi != pf->vsi[pf->lan_vsi])
8825 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
8826 ret = i40e_add_vsi(ctl_vsi);
8827 if (ret) {
8828 dev_info(&pf->pdev->dev,
8829 "rebuild of veb_idx %d owner VSI failed: %d\n",
8830 veb->idx, ret);
8831 goto end_reconstitute;
8832 }
8833 i40e_vsi_reset_stats(ctl_vsi);
8834
8835 /* create the VEB in the switch and move the VSI onto the VEB */
8836 ret = i40e_add_veb(veb, ctl_vsi);
8837 if (ret)
8838 goto end_reconstitute;
8839
8840 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
8841 veb->bridge_mode = BRIDGE_MODE_VEB;
8842 else
8843 veb->bridge_mode = BRIDGE_MODE_VEPA;
8844 i40e_config_bridge_mode(veb);
8845
8846 /* create the remaining VSIs attached to this VEB */
8847 for (v = 0; v < pf->num_alloc_vsi; v++) {
8848 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
8849 continue;
8850
8851 if (pf->vsi[v]->veb_idx == veb->idx) {
8852 struct i40e_vsi *vsi = pf->vsi[v];
8853
8854 vsi->uplink_seid = veb->seid;
8855 ret = i40e_add_vsi(vsi);
8856 if (ret) {
8857 dev_info(&pf->pdev->dev,
8858 "rebuild of vsi_idx %d failed: %d\n",
8859 v, ret);
8860 goto end_reconstitute;
8861 }
8862 i40e_vsi_reset_stats(vsi);
8863 }
8864 }
8865
8866 /* create any VEBs attached to this VEB - RECURSION */
8867 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
8868 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
8869 pf->veb[veb_idx]->uplink_seid = veb->seid;
8870 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
8871 if (ret)
8872 break;
8873 }
8874 }
8875
8876 end_reconstitute:
8877 return ret;
8878 }
8879
8880 /**
8881 * i40e_get_capabilities - get info about the HW
8882 * @pf: the PF struct
8883 **/
8884 static int i40e_get_capabilities(struct i40e_pf *pf,
8885 enum i40e_admin_queue_opc list_type)
8886 {
8887 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
8888 u16 data_size;
8889 int buf_len;
8890 int err;
8891
8892 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
8893 do {
8894 cap_buf = kzalloc(buf_len, GFP_KERNEL);
8895 if (!cap_buf)
8896 return -ENOMEM;
8897
8898 /* this loads the data into the hw struct for us */
8899 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
8900 &data_size, list_type,
8901 NULL);
8902 /* data loaded, buffer no longer needed */
8903 kfree(cap_buf);
8904
8905 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
8906 /* retry with a larger buffer */
8907 buf_len = data_size;
8908 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
8909 dev_info(&pf->pdev->dev,
8910 "capability discovery failed, err %s aq_err %s\n",
8911 i40e_stat_str(&pf->hw, err),
8912 i40e_aq_str(&pf->hw,
8913 pf->hw.aq.asq_last_status));
8914 return -ENODEV;
8915 }
8916 } while (err);
8917
8918 if (pf->hw.debug_mask & I40E_DEBUG_USER) {
8919 if (list_type == i40e_aqc_opc_list_func_capabilities) {
8920 dev_info(&pf->pdev->dev,
8921 "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",
8922 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
8923 pf->hw.func_caps.num_msix_vectors,
8924 pf->hw.func_caps.num_msix_vectors_vf,
8925 pf->hw.func_caps.fd_filters_guaranteed,
8926 pf->hw.func_caps.fd_filters_best_effort,
8927 pf->hw.func_caps.num_tx_qp,
8928 pf->hw.func_caps.num_vsis);
8929 } else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
8930 dev_info(&pf->pdev->dev,
8931 "switch_mode=0x%04x, function_valid=0x%08x\n",
8932 pf->hw.dev_caps.switch_mode,
8933 pf->hw.dev_caps.valid_functions);
8934 dev_info(&pf->pdev->dev,
8935 "SR-IOV=%d, num_vfs for all function=%u\n",
8936 pf->hw.dev_caps.sr_iov_1_1,
8937 pf->hw.dev_caps.num_vfs);
8938 dev_info(&pf->pdev->dev,
8939 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
8940 pf->hw.dev_caps.num_vsis,
8941 pf->hw.dev_caps.num_rx_qp,
8942 pf->hw.dev_caps.num_tx_qp);
8943 }
8944 }
8945 if (list_type == i40e_aqc_opc_list_func_capabilities) {
8946 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
8947 + pf->hw.func_caps.num_vfs)
8948 if (pf->hw.revision_id == 0 &&
8949 pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
8950 dev_info(&pf->pdev->dev,
8951 "got num_vsis %d, setting num_vsis to %d\n",
8952 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
8953 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
8954 }
8955 }
8956 return 0;
8957 }
8958
8959 static int i40e_vsi_clear(struct i40e_vsi *vsi);
8960
8961 /**
8962 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
8963 * @pf: board private structure
8964 **/
8965 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
8966 {
8967 struct i40e_vsi *vsi;
8968
8969 /* quick workaround for an NVM issue that leaves a critical register
8970 * uninitialized
8971 */
8972 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
8973 static const u32 hkey[] = {
8974 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
8975 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
8976 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
8977 0x95b3a76d};
8978 int i;
8979
8980 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
8981 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
8982 }
8983
8984 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
8985 return;
8986
8987 /* find existing VSI and see if it needs configuring */
8988 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
8989
8990 /* create a new VSI if none exists */
8991 if (!vsi) {
8992 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
8993 pf->vsi[pf->lan_vsi]->seid, 0);
8994 if (!vsi) {
8995 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
8996 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8997 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
8998 return;
8999 }
9000 }
9001
9002 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
9003 }
9004
9005 /**
9006 * i40e_fdir_teardown - release the Flow Director resources
9007 * @pf: board private structure
9008 **/
9009 static void i40e_fdir_teardown(struct i40e_pf *pf)
9010 {
9011 struct i40e_vsi *vsi;
9012
9013 i40e_fdir_filter_exit(pf);
9014 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
9015 if (vsi)
9016 i40e_vsi_release(vsi);
9017 }
9018
9019 /**
9020 * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
9021 * @vsi: PF main vsi
9022 * @seid: seid of main or channel VSIs
9023 *
9024 * Rebuilds cloud filters associated with main VSI and channel VSIs if they
9025 * existed before reset
9026 **/
9027 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
9028 {
9029 struct i40e_cloud_filter *cfilter;
9030 struct i40e_pf *pf = vsi->back;
9031 struct hlist_node *node;
9032 i40e_status ret;
9033
9034 /* Add cloud filters back if they exist */
9035 hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
9036 cloud_node) {
9037 if (cfilter->seid != seid)
9038 continue;
9039
9040 if (cfilter->dst_port)
9041 ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
9042 true);
9043 else
9044 ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
9045
9046 if (ret) {
9047 dev_dbg(&pf->pdev->dev,
9048 "Failed to rebuild cloud filter, err %s aq_err %s\n",
9049 i40e_stat_str(&pf->hw, ret),
9050 i40e_aq_str(&pf->hw,
9051 pf->hw.aq.asq_last_status));
9052 return ret;
9053 }
9054 }
9055 return 0;
9056 }
9057
9058 /**
9059 * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
9060 * @vsi: PF main vsi
9061 *
9062 * Rebuilds channel VSIs if they existed before reset
9063 **/
9064 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
9065 {
9066 struct i40e_channel *ch, *ch_tmp;
9067 i40e_status ret;
9068
9069 if (list_empty(&vsi->ch_list))
9070 return 0;
9071
9072 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
9073 if (!ch->initialized)
9074 break;
9075 /* Proceed with creation of channel (VMDq2) VSI */
9076 ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
9077 if (ret) {
9078 dev_info(&vsi->back->pdev->dev,
9079 "failed to rebuild channels using uplink_seid %u\n",
9080 vsi->uplink_seid);
9081 return ret;
9082 }
9083 if (ch->max_tx_rate) {
9084 u64 credits = ch->max_tx_rate;
9085
9086 if (i40e_set_bw_limit(vsi, ch->seid,
9087 ch->max_tx_rate))
9088 return -EINVAL;
9089
9090 do_div(credits, I40E_BW_CREDIT_DIVISOR);
9091 dev_dbg(&vsi->back->pdev->dev,
9092 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
9093 ch->max_tx_rate,
9094 credits,
9095 ch->seid);
9096 }
9097 ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
9098 if (ret) {
9099 dev_dbg(&vsi->back->pdev->dev,
9100 "Failed to rebuild cloud filters for channel VSI %u\n",
9101 ch->seid);
9102 return ret;
9103 }
9104 }
9105 return 0;
9106 }
9107
9108 /**
9109 * i40e_prep_for_reset - prep for the core to reset
9110 * @pf: board private structure
9111 * @lock_acquired: indicates whether or not the lock has been acquired
9112 * before this function was called.
9113 *
9114 * Close up the VFs and other things in prep for PF Reset.
9115 **/
9116 static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
9117 {
9118 struct i40e_hw *hw = &pf->hw;
9119 i40e_status ret = 0;
9120 u32 v;
9121
9122 clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
9123 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
9124 return;
9125 if (i40e_check_asq_alive(&pf->hw))
9126 i40e_vc_notify_reset(pf);
9127
9128 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
9129
9130 /* quiesce the VSIs and their queues that are not already DOWN */
9131 /* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
9132 if (!lock_acquired)
9133 rtnl_lock();
9134 i40e_pf_quiesce_all_vsi(pf);
9135 if (!lock_acquired)
9136 rtnl_unlock();
9137
9138 for (v = 0; v < pf->num_alloc_vsi; v++) {
9139 if (pf->vsi[v])
9140 pf->vsi[v]->seid = 0;
9141 }
9142
9143 i40e_shutdown_adminq(&pf->hw);
9144
9145 /* call shutdown HMC */
9146 if (hw->hmc.hmc_obj) {
9147 ret = i40e_shutdown_lan_hmc(hw);
9148 if (ret)
9149 dev_warn(&pf->pdev->dev,
9150 "shutdown_lan_hmc failed: %d\n", ret);
9151 }
9152 }
9153
9154 /**
9155 * i40e_send_version - update firmware with driver version
9156 * @pf: PF struct
9157 */
9158 static void i40e_send_version(struct i40e_pf *pf)
9159 {
9160 struct i40e_driver_version dv;
9161
9162 dv.major_version = DRV_VERSION_MAJOR;
9163 dv.minor_version = DRV_VERSION_MINOR;
9164 dv.build_version = DRV_VERSION_BUILD;
9165 dv.subbuild_version = 0;
9166 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
9167 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
9168 }
9169
9170 /**
9171 * i40e_get_oem_version - get OEM specific version information
9172 * @hw: pointer to the hardware structure
9173 **/
9174 static void i40e_get_oem_version(struct i40e_hw *hw)
9175 {
9176 u16 block_offset = 0xffff;
9177 u16 block_length = 0;
9178 u16 capabilities = 0;
9179 u16 gen_snap = 0;
9180 u16 release = 0;
9181
9182 #define I40E_SR_NVM_OEM_VERSION_PTR 0x1B
9183 #define I40E_NVM_OEM_LENGTH_OFFSET 0x00
9184 #define I40E_NVM_OEM_CAPABILITIES_OFFSET 0x01
9185 #define I40E_NVM_OEM_GEN_OFFSET 0x02
9186 #define I40E_NVM_OEM_RELEASE_OFFSET 0x03
9187 #define I40E_NVM_OEM_CAPABILITIES_MASK 0x000F
9188 #define I40E_NVM_OEM_LENGTH 3
9189
9190 /* Check if pointer to OEM version block is valid. */
9191 i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
9192 if (block_offset == 0xffff)
9193 return;
9194
9195 /* Check if OEM version block has correct length. */
9196 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
9197 &block_length);
9198 if (block_length < I40E_NVM_OEM_LENGTH)
9199 return;
9200
9201 /* Check if OEM version format is as expected. */
9202 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
9203 &capabilities);
9204 if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
9205 return;
9206
9207 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
9208 &gen_snap);
9209 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
9210 &release);
9211 hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
9212 hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
9213 }
9214
9215 /**
9216 * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
9217 * @pf: board private structure
9218 **/
9219 static int i40e_reset(struct i40e_pf *pf)
9220 {
9221 struct i40e_hw *hw = &pf->hw;
9222 i40e_status ret;
9223
9224 ret = i40e_pf_reset(hw);
9225 if (ret) {
9226 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
9227 set_bit(__I40E_RESET_FAILED, pf->state);
9228 clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
9229 } else {
9230 pf->pfr_count++;
9231 }
9232 return ret;
9233 }
9234
9235 /**
9236 * i40e_rebuild - rebuild using a saved config
9237 * @pf: board private structure
9238 * @reinit: if the Main VSI needs to re-initialized.
9239 * @lock_acquired: indicates whether or not the lock has been acquired
9240 * before this function was called.
9241 **/
9242 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
9243 {
9244 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9245 struct i40e_hw *hw = &pf->hw;
9246 u8 set_fc_aq_fail = 0;
9247 i40e_status ret;
9248 u32 val;
9249 int v;
9250
9251 if (test_bit(__I40E_DOWN, pf->state))
9252 goto clear_recovery;
9253 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
9254
9255 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
9256 ret = i40e_init_adminq(&pf->hw);
9257 if (ret) {
9258 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
9259 i40e_stat_str(&pf->hw, ret),
9260 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9261 goto clear_recovery;
9262 }
9263 i40e_get_oem_version(&pf->hw);
9264
9265 if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
9266 ((hw->aq.fw_maj_ver == 4 && hw->aq.fw_min_ver <= 33) ||
9267 hw->aq.fw_maj_ver < 4) && hw->mac.type == I40E_MAC_XL710) {
9268 /* The following delay is necessary for 4.33 firmware and older
9269 * to recover after EMP reset. 200 ms should suffice but we
9270 * put here 300 ms to be sure that FW is ready to operate
9271 * after reset.
9272 */
9273 mdelay(300);
9274 }
9275
9276 /* re-verify the eeprom if we just had an EMP reset */
9277 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
9278 i40e_verify_eeprom(pf);
9279
9280 i40e_clear_pxe_mode(hw);
9281 ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
9282 if (ret)
9283 goto end_core_reset;
9284
9285 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
9286 hw->func_caps.num_rx_qp, 0, 0);
9287 if (ret) {
9288 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
9289 goto end_core_reset;
9290 }
9291 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
9292 if (ret) {
9293 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
9294 goto end_core_reset;
9295 }
9296
9297 #ifdef CONFIG_I40E_DCB
9298 ret = i40e_init_pf_dcb(pf);
9299 if (ret) {
9300 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
9301 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9302 /* Continue without DCB enabled */
9303 }
9304 #endif /* CONFIG_I40E_DCB */
9305 /* do basic switch setup */
9306 if (!lock_acquired)
9307 rtnl_lock();
9308 ret = i40e_setup_pf_switch(pf, reinit);
9309 if (ret)
9310 goto end_unlock;
9311
9312 /* The driver only wants link up/down and module qualification
9313 * reports from firmware. Note the negative logic.
9314 */
9315 ret = i40e_aq_set_phy_int_mask(&pf->hw,
9316 ~(I40E_AQ_EVENT_LINK_UPDOWN |
9317 I40E_AQ_EVENT_MEDIA_NA |
9318 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
9319 if (ret)
9320 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
9321 i40e_stat_str(&pf->hw, ret),
9322 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9323
9324 /* make sure our flow control settings are restored */
9325 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
9326 if (ret)
9327 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
9328 i40e_stat_str(&pf->hw, ret),
9329 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9330
9331 /* Rebuild the VSIs and VEBs that existed before reset.
9332 * They are still in our local switch element arrays, so only
9333 * need to rebuild the switch model in the HW.
9334 *
9335 * If there were VEBs but the reconstitution failed, we'll try
9336 * try to recover minimal use by getting the basic PF VSI working.
9337 */
9338 if (vsi->uplink_seid != pf->mac_seid) {
9339 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
9340 /* find the one VEB connected to the MAC, and find orphans */
9341 for (v = 0; v < I40E_MAX_VEB; v++) {
9342 if (!pf->veb[v])
9343 continue;
9344
9345 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
9346 pf->veb[v]->uplink_seid == 0) {
9347 ret = i40e_reconstitute_veb(pf->veb[v]);
9348
9349 if (!ret)
9350 continue;
9351
9352 /* If Main VEB failed, we're in deep doodoo,
9353 * so give up rebuilding the switch and set up
9354 * for minimal rebuild of PF VSI.
9355 * If orphan failed, we'll report the error
9356 * but try to keep going.
9357 */
9358 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
9359 dev_info(&pf->pdev->dev,
9360 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
9361 ret);
9362 vsi->uplink_seid = pf->mac_seid;
9363 break;
9364 } else if (pf->veb[v]->uplink_seid == 0) {
9365 dev_info(&pf->pdev->dev,
9366 "rebuild of orphan VEB failed: %d\n",
9367 ret);
9368 }
9369 }
9370 }
9371 }
9372
9373 if (vsi->uplink_seid == pf->mac_seid) {
9374 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
9375 /* no VEB, so rebuild only the Main VSI */
9376 ret = i40e_add_vsi(vsi);
9377 if (ret) {
9378 dev_info(&pf->pdev->dev,
9379 "rebuild of Main VSI failed: %d\n", ret);
9380 goto end_unlock;
9381 }
9382 }
9383
9384 if (vsi->mqprio_qopt.max_rate[0]) {
9385 u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
9386 u64 credits = 0;
9387
9388 do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
9389 ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
9390 if (ret)
9391 goto end_unlock;
9392
9393 credits = max_tx_rate;
9394 do_div(credits, I40E_BW_CREDIT_DIVISOR);
9395 dev_dbg(&vsi->back->pdev->dev,
9396 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
9397 max_tx_rate,
9398 credits,
9399 vsi->seid);
9400 }
9401
9402 ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
9403 if (ret)
9404 goto end_unlock;
9405
9406 /* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
9407 * for this main VSI if they exist
9408 */
9409 ret = i40e_rebuild_channels(vsi);
9410 if (ret)
9411 goto end_unlock;
9412
9413 /* Reconfigure hardware for allowing smaller MSS in the case
9414 * of TSO, so that we avoid the MDD being fired and causing
9415 * a reset in the case of small MSS+TSO.
9416 */
9417 #define I40E_REG_MSS 0x000E64DC
9418 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
9419 #define I40E_64BYTE_MSS 0x400000
9420 val = rd32(hw, I40E_REG_MSS);
9421 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
9422 val &= ~I40E_REG_MSS_MIN_MASK;
9423 val |= I40E_64BYTE_MSS;
9424 wr32(hw, I40E_REG_MSS, val);
9425 }
9426
9427 if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
9428 msleep(75);
9429 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
9430 if (ret)
9431 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
9432 i40e_stat_str(&pf->hw, ret),
9433 i40e_aq_str(&pf->hw,
9434 pf->hw.aq.asq_last_status));
9435 }
9436 /* reinit the misc interrupt */
9437 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
9438 ret = i40e_setup_misc_vector(pf);
9439
9440 /* Add a filter to drop all Flow control frames from any VSI from being
9441 * transmitted. By doing so we stop a malicious VF from sending out
9442 * PAUSE or PFC frames and potentially controlling traffic for other
9443 * PF/VF VSIs.
9444 * The FW can still send Flow control frames if enabled.
9445 */
9446 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
9447 pf->main_vsi_seid);
9448
9449 /* restart the VSIs that were rebuilt and running before the reset */
9450 i40e_pf_unquiesce_all_vsi(pf);
9451
9452 /* Release the RTNL lock before we start resetting VFs */
9453 if (!lock_acquired)
9454 rtnl_unlock();
9455
9456 /* Restore promiscuous settings */
9457 ret = i40e_set_promiscuous(pf, pf->cur_promisc);
9458 if (ret)
9459 dev_warn(&pf->pdev->dev,
9460 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
9461 pf->cur_promisc ? "on" : "off",
9462 i40e_stat_str(&pf->hw, ret),
9463 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9464
9465 i40e_reset_all_vfs(pf, true);
9466
9467 /* tell the firmware that we're starting */
9468 i40e_send_version(pf);
9469
9470 /* We've already released the lock, so don't do it again */
9471 goto end_core_reset;
9472
9473 end_unlock:
9474 if (!lock_acquired)
9475 rtnl_unlock();
9476 end_core_reset:
9477 clear_bit(__I40E_RESET_FAILED, pf->state);
9478 clear_recovery:
9479 clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
9480 }
9481
9482 /**
9483 * i40e_reset_and_rebuild - reset and rebuild using a saved config
9484 * @pf: board private structure
9485 * @reinit: if the Main VSI needs to re-initialized.
9486 * @lock_acquired: indicates whether or not the lock has been acquired
9487 * before this function was called.
9488 **/
9489 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
9490 bool lock_acquired)
9491 {
9492 int ret;
9493 /* Now we wait for GRST to settle out.
9494 * We don't have to delete the VEBs or VSIs from the hw switch
9495 * because the reset will make them disappear.
9496 */
9497 ret = i40e_reset(pf);
9498 if (!ret)
9499 i40e_rebuild(pf, reinit, lock_acquired);
9500 }
9501
9502 /**
9503 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
9504 * @pf: board private structure
9505 *
9506 * Close up the VFs and other things in prep for a Core Reset,
9507 * then get ready to rebuild the world.
9508 * @lock_acquired: indicates whether or not the lock has been acquired
9509 * before this function was called.
9510 **/
9511 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
9512 {
9513 i40e_prep_for_reset(pf, lock_acquired);
9514 i40e_reset_and_rebuild(pf, false, lock_acquired);
9515 }
9516
9517 /**
9518 * i40e_handle_mdd_event
9519 * @pf: pointer to the PF structure
9520 *
9521 * Called from the MDD irq handler to identify possibly malicious vfs
9522 **/
9523 static void i40e_handle_mdd_event(struct i40e_pf *pf)
9524 {
9525 struct i40e_hw *hw = &pf->hw;
9526 bool mdd_detected = false;
9527 bool pf_mdd_detected = false;
9528 struct i40e_vf *vf;
9529 u32 reg;
9530 int i;
9531
9532 if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
9533 return;
9534
9535 /* find what triggered the MDD event */
9536 reg = rd32(hw, I40E_GL_MDET_TX);
9537 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
9538 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
9539 I40E_GL_MDET_TX_PF_NUM_SHIFT;
9540 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
9541 I40E_GL_MDET_TX_VF_NUM_SHIFT;
9542 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
9543 I40E_GL_MDET_TX_EVENT_SHIFT;
9544 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
9545 I40E_GL_MDET_TX_QUEUE_SHIFT) -
9546 pf->hw.func_caps.base_queue;
9547 if (netif_msg_tx_err(pf))
9548 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
9549 event, queue, pf_num, vf_num);
9550 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
9551 mdd_detected = true;
9552 }
9553 reg = rd32(hw, I40E_GL_MDET_RX);
9554 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
9555 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
9556 I40E_GL_MDET_RX_FUNCTION_SHIFT;
9557 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
9558 I40E_GL_MDET_RX_EVENT_SHIFT;
9559 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
9560 I40E_GL_MDET_RX_QUEUE_SHIFT) -
9561 pf->hw.func_caps.base_queue;
9562 if (netif_msg_rx_err(pf))
9563 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
9564 event, queue, func);
9565 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
9566 mdd_detected = true;
9567 }
9568
9569 if (mdd_detected) {
9570 reg = rd32(hw, I40E_PF_MDET_TX);
9571 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
9572 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
9573 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
9574 pf_mdd_detected = true;
9575 }
9576 reg = rd32(hw, I40E_PF_MDET_RX);
9577 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
9578 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
9579 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
9580 pf_mdd_detected = true;
9581 }
9582 /* Queue belongs to the PF, initiate a reset */
9583 if (pf_mdd_detected) {
9584 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9585 i40e_service_event_schedule(pf);
9586 }
9587 }
9588
9589 /* see if one of the VFs needs its hand slapped */
9590 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
9591 vf = &(pf->vf[i]);
9592 reg = rd32(hw, I40E_VP_MDET_TX(i));
9593 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
9594 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
9595 vf->num_mdd_events++;
9596 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
9597 i);
9598 }
9599
9600 reg = rd32(hw, I40E_VP_MDET_RX(i));
9601 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
9602 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
9603 vf->num_mdd_events++;
9604 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
9605 i);
9606 }
9607
9608 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
9609 dev_info(&pf->pdev->dev,
9610 "Too many MDD events on VF %d, disabled\n", i);
9611 dev_info(&pf->pdev->dev,
9612 "Use PF Control I/F to re-enable the VF\n");
9613 set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
9614 }
9615 }
9616
9617 /* re-enable mdd interrupt cause */
9618 clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
9619 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
9620 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
9621 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
9622 i40e_flush(hw);
9623 }
9624
9625 static const char *i40e_tunnel_name(struct i40e_udp_port_config *port)
9626 {
9627 switch (port->type) {
9628 case UDP_TUNNEL_TYPE_VXLAN:
9629 return "vxlan";
9630 case UDP_TUNNEL_TYPE_GENEVE:
9631 return "geneve";
9632 default:
9633 return "unknown";
9634 }
9635 }
9636
9637 /**
9638 * i40e_sync_udp_filters - Trigger a sync event for existing UDP filters
9639 * @pf: board private structure
9640 **/
9641 static void i40e_sync_udp_filters(struct i40e_pf *pf)
9642 {
9643 int i;
9644
9645 /* loop through and set pending bit for all active UDP filters */
9646 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
9647 if (pf->udp_ports[i].port)
9648 pf->pending_udp_bitmap |= BIT_ULL(i);
9649 }
9650
9651 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
9652 }
9653
9654 /**
9655 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
9656 * @pf: board private structure
9657 **/
9658 static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
9659 {
9660 struct i40e_hw *hw = &pf->hw;
9661 i40e_status ret;
9662 u16 port;
9663 int i;
9664
9665 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
9666 return;
9667
9668 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
9669
9670 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
9671 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
9672 pf->pending_udp_bitmap &= ~BIT_ULL(i);
9673 port = pf->udp_ports[i].port;
9674 if (port)
9675 ret = i40e_aq_add_udp_tunnel(hw, port,
9676 pf->udp_ports[i].type,
9677 NULL, NULL);
9678 else
9679 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
9680
9681 if (ret) {
9682 dev_info(&pf->pdev->dev,
9683 "%s %s port %d, index %d failed, err %s aq_err %s\n",
9684 i40e_tunnel_name(&pf->udp_ports[i]),
9685 port ? "add" : "delete",
9686 port, i,
9687 i40e_stat_str(&pf->hw, ret),
9688 i40e_aq_str(&pf->hw,
9689 pf->hw.aq.asq_last_status));
9690 pf->udp_ports[i].port = 0;
9691 }
9692 }
9693 }
9694 }
9695
9696 /**
9697 * i40e_service_task - Run the driver's async subtasks
9698 * @work: pointer to work_struct containing our data
9699 **/
9700 static void i40e_service_task(struct work_struct *work)
9701 {
9702 struct i40e_pf *pf = container_of(work,
9703 struct i40e_pf,
9704 service_task);
9705 unsigned long start_time = jiffies;
9706
9707 /* don't bother with service tasks if a reset is in progress */
9708 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
9709 return;
9710
9711 if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
9712 return;
9713
9714 i40e_detect_recover_hung(pf);
9715 i40e_sync_filters_subtask(pf);
9716 i40e_reset_subtask(pf);
9717 i40e_handle_mdd_event(pf);
9718 i40e_vc_process_vflr_event(pf);
9719 i40e_watchdog_subtask(pf);
9720 i40e_fdir_reinit_subtask(pf);
9721 if (pf->flags & I40E_FLAG_CLIENT_RESET) {
9722 /* Client subtask will reopen next time through. */
9723 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
9724 pf->flags &= ~I40E_FLAG_CLIENT_RESET;
9725 } else {
9726 i40e_client_subtask(pf);
9727 if (pf->flags & I40E_FLAG_CLIENT_L2_CHANGE) {
9728 i40e_notify_client_of_l2_param_changes(
9729 pf->vsi[pf->lan_vsi]);
9730 pf->flags &= ~I40E_FLAG_CLIENT_L2_CHANGE;
9731 }
9732 }
9733 i40e_sync_filters_subtask(pf);
9734 i40e_sync_udp_filters_subtask(pf);
9735 i40e_clean_adminq_subtask(pf);
9736
9737 /* flush memory to make sure state is correct before next watchdog */
9738 smp_mb__before_atomic();
9739 clear_bit(__I40E_SERVICE_SCHED, pf->state);
9740
9741 /* If the tasks have taken longer than one timer cycle or there
9742 * is more work to be done, reschedule the service task now
9743 * rather than wait for the timer to tick again.
9744 */
9745 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
9746 test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state) ||
9747 test_bit(__I40E_MDD_EVENT_PENDING, pf->state) ||
9748 test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
9749 i40e_service_event_schedule(pf);
9750 }
9751
9752 /**
9753 * i40e_service_timer - timer callback
9754 * @data: pointer to PF struct
9755 **/
9756 static void i40e_service_timer(struct timer_list *t)
9757 {
9758 struct i40e_pf *pf = from_timer(pf, t, service_timer);
9759
9760 mod_timer(&pf->service_timer,
9761 round_jiffies(jiffies + pf->service_timer_period));
9762 i40e_service_event_schedule(pf);
9763 }
9764
9765 /**
9766 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
9767 * @vsi: the VSI being configured
9768 **/
9769 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
9770 {
9771 struct i40e_pf *pf = vsi->back;
9772
9773 switch (vsi->type) {
9774 case I40E_VSI_MAIN:
9775 vsi->alloc_queue_pairs = pf->num_lan_qps;
9776 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
9777 I40E_REQ_DESCRIPTOR_MULTIPLE);
9778 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
9779 vsi->num_q_vectors = pf->num_lan_msix;
9780 else
9781 vsi->num_q_vectors = 1;
9782
9783 break;
9784
9785 case I40E_VSI_FDIR:
9786 vsi->alloc_queue_pairs = 1;
9787 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
9788 I40E_REQ_DESCRIPTOR_MULTIPLE);
9789 vsi->num_q_vectors = pf->num_fdsb_msix;
9790 break;
9791
9792 case I40E_VSI_VMDQ2:
9793 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
9794 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
9795 I40E_REQ_DESCRIPTOR_MULTIPLE);
9796 vsi->num_q_vectors = pf->num_vmdq_msix;
9797 break;
9798
9799 case I40E_VSI_SRIOV:
9800 vsi->alloc_queue_pairs = pf->num_vf_qps;
9801 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
9802 I40E_REQ_DESCRIPTOR_MULTIPLE);
9803 break;
9804
9805 default:
9806 WARN_ON(1);
9807 return -ENODATA;
9808 }
9809
9810 return 0;
9811 }
9812
9813 /**
9814 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
9815 * @vsi: VSI pointer
9816 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
9817 *
9818 * On error: returns error code (negative)
9819 * On success: returns 0
9820 **/
9821 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
9822 {
9823 struct i40e_ring **next_rings;
9824 int size;
9825 int ret = 0;
9826
9827 /* allocate memory for both Tx, XDP Tx and Rx ring pointers */
9828 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
9829 (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
9830 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
9831 if (!vsi->tx_rings)
9832 return -ENOMEM;
9833 next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
9834 if (i40e_enabled_xdp_vsi(vsi)) {
9835 vsi->xdp_rings = next_rings;
9836 next_rings += vsi->alloc_queue_pairs;
9837 }
9838 vsi->rx_rings = next_rings;
9839
9840 if (alloc_qvectors) {
9841 /* allocate memory for q_vector pointers */
9842 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
9843 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
9844 if (!vsi->q_vectors) {
9845 ret = -ENOMEM;
9846 goto err_vectors;
9847 }
9848 }
9849 return ret;
9850
9851 err_vectors:
9852 kfree(vsi->tx_rings);
9853 return ret;
9854 }
9855
9856 /**
9857 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
9858 * @pf: board private structure
9859 * @type: type of VSI
9860 *
9861 * On error: returns error code (negative)
9862 * On success: returns vsi index in PF (positive)
9863 **/
9864 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
9865 {
9866 int ret = -ENODEV;
9867 struct i40e_vsi *vsi;
9868 int vsi_idx;
9869 int i;
9870
9871 /* Need to protect the allocation of the VSIs at the PF level */
9872 mutex_lock(&pf->switch_mutex);
9873
9874 /* VSI list may be fragmented if VSI creation/destruction has
9875 * been happening. We can afford to do a quick scan to look
9876 * for any free VSIs in the list.
9877 *
9878 * find next empty vsi slot, looping back around if necessary
9879 */
9880 i = pf->next_vsi;
9881 while (i < pf->num_alloc_vsi && pf->vsi[i])
9882 i++;
9883 if (i >= pf->num_alloc_vsi) {
9884 i = 0;
9885 while (i < pf->next_vsi && pf->vsi[i])
9886 i++;
9887 }
9888
9889 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
9890 vsi_idx = i; /* Found one! */
9891 } else {
9892 ret = -ENODEV;
9893 goto unlock_pf; /* out of VSI slots! */
9894 }
9895 pf->next_vsi = ++i;
9896
9897 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
9898 if (!vsi) {
9899 ret = -ENOMEM;
9900 goto unlock_pf;
9901 }
9902 vsi->type = type;
9903 vsi->back = pf;
9904 set_bit(__I40E_VSI_DOWN, vsi->state);
9905 vsi->flags = 0;
9906 vsi->idx = vsi_idx;
9907 vsi->int_rate_limit = 0;
9908 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
9909 pf->rss_table_size : 64;
9910 vsi->netdev_registered = false;
9911 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
9912 hash_init(vsi->mac_filter_hash);
9913 vsi->irqs_ready = false;
9914
9915 ret = i40e_set_num_rings_in_vsi(vsi);
9916 if (ret)
9917 goto err_rings;
9918
9919 ret = i40e_vsi_alloc_arrays(vsi, true);
9920 if (ret)
9921 goto err_rings;
9922
9923 /* Setup default MSIX irq handler for VSI */
9924 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
9925
9926 /* Initialize VSI lock */
9927 spin_lock_init(&vsi->mac_filter_hash_lock);
9928 pf->vsi[vsi_idx] = vsi;
9929 ret = vsi_idx;
9930 goto unlock_pf;
9931
9932 err_rings:
9933 pf->next_vsi = i - 1;
9934 kfree(vsi);
9935 unlock_pf:
9936 mutex_unlock(&pf->switch_mutex);
9937 return ret;
9938 }
9939
9940 /**
9941 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
9942 * @type: VSI pointer
9943 * @free_qvectors: a bool to specify if q_vectors need to be freed.
9944 *
9945 * On error: returns error code (negative)
9946 * On success: returns 0
9947 **/
9948 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
9949 {
9950 /* free the ring and vector containers */
9951 if (free_qvectors) {
9952 kfree(vsi->q_vectors);
9953 vsi->q_vectors = NULL;
9954 }
9955 kfree(vsi->tx_rings);
9956 vsi->tx_rings = NULL;
9957 vsi->rx_rings = NULL;
9958 vsi->xdp_rings = NULL;
9959 }
9960
9961 /**
9962 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
9963 * and lookup table
9964 * @vsi: Pointer to VSI structure
9965 */
9966 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
9967 {
9968 if (!vsi)
9969 return;
9970
9971 kfree(vsi->rss_hkey_user);
9972 vsi->rss_hkey_user = NULL;
9973
9974 kfree(vsi->rss_lut_user);
9975 vsi->rss_lut_user = NULL;
9976 }
9977
9978 /**
9979 * i40e_vsi_clear - Deallocate the VSI provided
9980 * @vsi: the VSI being un-configured
9981 **/
9982 static int i40e_vsi_clear(struct i40e_vsi *vsi)
9983 {
9984 struct i40e_pf *pf;
9985
9986 if (!vsi)
9987 return 0;
9988
9989 if (!vsi->back)
9990 goto free_vsi;
9991 pf = vsi->back;
9992
9993 mutex_lock(&pf->switch_mutex);
9994 if (!pf->vsi[vsi->idx]) {
9995 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
9996 vsi->idx, vsi->idx, vsi, vsi->type);
9997 goto unlock_vsi;
9998 }
9999
10000 if (pf->vsi[vsi->idx] != vsi) {
10001 dev_err(&pf->pdev->dev,
10002 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
10003 pf->vsi[vsi->idx]->idx,
10004 pf->vsi[vsi->idx],
10005 pf->vsi[vsi->idx]->type,
10006 vsi->idx, vsi, vsi->type);
10007 goto unlock_vsi;
10008 }
10009
10010 /* updates the PF for this cleared vsi */
10011 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
10012 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
10013
10014 i40e_vsi_free_arrays(vsi, true);
10015 i40e_clear_rss_config_user(vsi);
10016
10017 pf->vsi[vsi->idx] = NULL;
10018 if (vsi->idx < pf->next_vsi)
10019 pf->next_vsi = vsi->idx;
10020
10021 unlock_vsi:
10022 mutex_unlock(&pf->switch_mutex);
10023 free_vsi:
10024 kfree(vsi);
10025
10026 return 0;
10027 }
10028
10029 /**
10030 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
10031 * @vsi: the VSI being cleaned
10032 **/
10033 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
10034 {
10035 int i;
10036
10037 if (vsi->tx_rings && vsi->tx_rings[0]) {
10038 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
10039 kfree_rcu(vsi->tx_rings[i], rcu);
10040 vsi->tx_rings[i] = NULL;
10041 vsi->rx_rings[i] = NULL;
10042 if (vsi->xdp_rings)
10043 vsi->xdp_rings[i] = NULL;
10044 }
10045 }
10046 }
10047
10048 /**
10049 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
10050 * @vsi: the VSI being configured
10051 **/
10052 static int i40e_alloc_rings(struct i40e_vsi *vsi)
10053 {
10054 int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
10055 struct i40e_pf *pf = vsi->back;
10056 struct i40e_ring *ring;
10057
10058 /* Set basic values in the rings to be used later during open() */
10059 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
10060 /* allocate space for both Tx and Rx in one shot */
10061 ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
10062 if (!ring)
10063 goto err_out;
10064
10065 ring->queue_index = i;
10066 ring->reg_idx = vsi->base_queue + i;
10067 ring->ring_active = false;
10068 ring->vsi = vsi;
10069 ring->netdev = vsi->netdev;
10070 ring->dev = &pf->pdev->dev;
10071 ring->count = vsi->num_desc;
10072 ring->size = 0;
10073 ring->dcb_tc = 0;
10074 if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
10075 ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
10076 ring->tx_itr_setting = pf->tx_itr_default;
10077 vsi->tx_rings[i] = ring++;
10078
10079 if (!i40e_enabled_xdp_vsi(vsi))
10080 goto setup_rx;
10081
10082 ring->queue_index = vsi->alloc_queue_pairs + i;
10083 ring->reg_idx = vsi->base_queue + ring->queue_index;
10084 ring->ring_active = false;
10085 ring->vsi = vsi;
10086 ring->netdev = NULL;
10087 ring->dev = &pf->pdev->dev;
10088 ring->count = vsi->num_desc;
10089 ring->size = 0;
10090 ring->dcb_tc = 0;
10091 if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
10092 ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
10093 set_ring_xdp(ring);
10094 ring->tx_itr_setting = pf->tx_itr_default;
10095 vsi->xdp_rings[i] = ring++;
10096
10097 setup_rx:
10098 ring->queue_index = i;
10099 ring->reg_idx = vsi->base_queue + i;
10100 ring->ring_active = false;
10101 ring->vsi = vsi;
10102 ring->netdev = vsi->netdev;
10103 ring->dev = &pf->pdev->dev;
10104 ring->count = vsi->num_desc;
10105 ring->size = 0;
10106 ring->dcb_tc = 0;
10107 ring->rx_itr_setting = pf->rx_itr_default;
10108 vsi->rx_rings[i] = ring;
10109 }
10110
10111 return 0;
10112
10113 err_out:
10114 i40e_vsi_clear_rings(vsi);
10115 return -ENOMEM;
10116 }
10117
10118 /**
10119 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
10120 * @pf: board private structure
10121 * @vectors: the number of MSI-X vectors to request
10122 *
10123 * Returns the number of vectors reserved, or error
10124 **/
10125 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
10126 {
10127 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
10128 I40E_MIN_MSIX, vectors);
10129 if (vectors < 0) {
10130 dev_info(&pf->pdev->dev,
10131 "MSI-X vector reservation failed: %d\n", vectors);
10132 vectors = 0;
10133 }
10134
10135 return vectors;
10136 }
10137
10138 /**
10139 * i40e_init_msix - Setup the MSIX capability
10140 * @pf: board private structure
10141 *
10142 * Work with the OS to set up the MSIX vectors needed.
10143 *
10144 * Returns the number of vectors reserved or negative on failure
10145 **/
10146 static int i40e_init_msix(struct i40e_pf *pf)
10147 {
10148 struct i40e_hw *hw = &pf->hw;
10149 int cpus, extra_vectors;
10150 int vectors_left;
10151 int v_budget, i;
10152 int v_actual;
10153 int iwarp_requested = 0;
10154
10155 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
10156 return -ENODEV;
10157
10158 /* The number of vectors we'll request will be comprised of:
10159 * - Add 1 for "other" cause for Admin Queue events, etc.
10160 * - The number of LAN queue pairs
10161 * - Queues being used for RSS.
10162 * We don't need as many as max_rss_size vectors.
10163 * use rss_size instead in the calculation since that
10164 * is governed by number of cpus in the system.
10165 * - assumes symmetric Tx/Rx pairing
10166 * - The number of VMDq pairs
10167 * - The CPU count within the NUMA node if iWARP is enabled
10168 * Once we count this up, try the request.
10169 *
10170 * If we can't get what we want, we'll simplify to nearly nothing
10171 * and try again. If that still fails, we punt.
10172 */
10173 vectors_left = hw->func_caps.num_msix_vectors;
10174 v_budget = 0;
10175
10176 /* reserve one vector for miscellaneous handler */
10177 if (vectors_left) {
10178 v_budget++;
10179 vectors_left--;
10180 }
10181
10182 /* reserve some vectors for the main PF traffic queues. Initially we
10183 * only reserve at most 50% of the available vectors, in the case that
10184 * the number of online CPUs is large. This ensures that we can enable
10185 * extra features as well. Once we've enabled the other features, we
10186 * will use any remaining vectors to reach as close as we can to the
10187 * number of online CPUs.
10188 */
10189 cpus = num_online_cpus();
10190 pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
10191 vectors_left -= pf->num_lan_msix;
10192
10193 /* reserve one vector for sideband flow director */
10194 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10195 if (vectors_left) {
10196 pf->num_fdsb_msix = 1;
10197 v_budget++;
10198 vectors_left--;
10199 } else {
10200 pf->num_fdsb_msix = 0;
10201 }
10202 }
10203
10204 /* can we reserve enough for iWARP? */
10205 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
10206 iwarp_requested = pf->num_iwarp_msix;
10207
10208 if (!vectors_left)
10209 pf->num_iwarp_msix = 0;
10210 else if (vectors_left < pf->num_iwarp_msix)
10211 pf->num_iwarp_msix = 1;
10212 v_budget += pf->num_iwarp_msix;
10213 vectors_left -= pf->num_iwarp_msix;
10214 }
10215
10216 /* any vectors left over go for VMDq support */
10217 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
10218 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
10219 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
10220
10221 if (!vectors_left) {
10222 pf->num_vmdq_msix = 0;
10223 pf->num_vmdq_qps = 0;
10224 } else {
10225 /* if we're short on vectors for what's desired, we limit
10226 * the queues per vmdq. If this is still more than are
10227 * available, the user will need to change the number of
10228 * queues/vectors used by the PF later with the ethtool
10229 * channels command
10230 */
10231 if (vmdq_vecs < vmdq_vecs_wanted)
10232 pf->num_vmdq_qps = 1;
10233 pf->num_vmdq_msix = pf->num_vmdq_qps;
10234
10235 v_budget += vmdq_vecs;
10236 vectors_left -= vmdq_vecs;
10237 }
10238 }
10239
10240 /* On systems with a large number of SMP cores, we previously limited
10241 * the number of vectors for num_lan_msix to be at most 50% of the
10242 * available vectors, to allow for other features. Now, we add back
10243 * the remaining vectors. However, we ensure that the total
10244 * num_lan_msix will not exceed num_online_cpus(). To do this, we
10245 * calculate the number of vectors we can add without going over the
10246 * cap of CPUs. For systems with a small number of CPUs this will be
10247 * zero.
10248 */
10249 extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
10250 pf->num_lan_msix += extra_vectors;
10251 vectors_left -= extra_vectors;
10252
10253 WARN(vectors_left < 0,
10254 "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
10255
10256 v_budget += pf->num_lan_msix;
10257 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
10258 GFP_KERNEL);
10259 if (!pf->msix_entries)
10260 return -ENOMEM;
10261
10262 for (i = 0; i < v_budget; i++)
10263 pf->msix_entries[i].entry = i;
10264 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
10265
10266 if (v_actual < I40E_MIN_MSIX) {
10267 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
10268 kfree(pf->msix_entries);
10269 pf->msix_entries = NULL;
10270 pci_disable_msix(pf->pdev);
10271 return -ENODEV;
10272
10273 } else if (v_actual == I40E_MIN_MSIX) {
10274 /* Adjust for minimal MSIX use */
10275 pf->num_vmdq_vsis = 0;
10276 pf->num_vmdq_qps = 0;
10277 pf->num_lan_qps = 1;
10278 pf->num_lan_msix = 1;
10279
10280 } else if (v_actual != v_budget) {
10281 /* If we have limited resources, we will start with no vectors
10282 * for the special features and then allocate vectors to some
10283 * of these features based on the policy and at the end disable
10284 * the features that did not get any vectors.
10285 */
10286 int vec;
10287
10288 dev_info(&pf->pdev->dev,
10289 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
10290 v_actual, v_budget);
10291 /* reserve the misc vector */
10292 vec = v_actual - 1;
10293
10294 /* Scale vector usage down */
10295 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
10296 pf->num_vmdq_vsis = 1;
10297 pf->num_vmdq_qps = 1;
10298
10299 /* partition out the remaining vectors */
10300 switch (vec) {
10301 case 2:
10302 pf->num_lan_msix = 1;
10303 break;
10304 case 3:
10305 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
10306 pf->num_lan_msix = 1;
10307 pf->num_iwarp_msix = 1;
10308 } else {
10309 pf->num_lan_msix = 2;
10310 }
10311 break;
10312 default:
10313 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
10314 pf->num_iwarp_msix = min_t(int, (vec / 3),
10315 iwarp_requested);
10316 pf->num_vmdq_vsis = min_t(int, (vec / 3),
10317 I40E_DEFAULT_NUM_VMDQ_VSI);
10318 } else {
10319 pf->num_vmdq_vsis = min_t(int, (vec / 2),
10320 I40E_DEFAULT_NUM_VMDQ_VSI);
10321 }
10322 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10323 pf->num_fdsb_msix = 1;
10324 vec--;
10325 }
10326 pf->num_lan_msix = min_t(int,
10327 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
10328 pf->num_lan_msix);
10329 pf->num_lan_qps = pf->num_lan_msix;
10330 break;
10331 }
10332 }
10333
10334 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
10335 (pf->num_fdsb_msix == 0)) {
10336 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
10337 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10338 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10339 }
10340 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10341 (pf->num_vmdq_msix == 0)) {
10342 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
10343 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
10344 }
10345
10346 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
10347 (pf->num_iwarp_msix == 0)) {
10348 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
10349 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
10350 }
10351 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
10352 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
10353 pf->num_lan_msix,
10354 pf->num_vmdq_msix * pf->num_vmdq_vsis,
10355 pf->num_fdsb_msix,
10356 pf->num_iwarp_msix);
10357
10358 return v_actual;
10359 }
10360
10361 /**
10362 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
10363 * @vsi: the VSI being configured
10364 * @v_idx: index of the vector in the vsi struct
10365 * @cpu: cpu to be used on affinity_mask
10366 *
10367 * We allocate one q_vector. If allocation fails we return -ENOMEM.
10368 **/
10369 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
10370 {
10371 struct i40e_q_vector *q_vector;
10372
10373 /* allocate q_vector */
10374 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
10375 if (!q_vector)
10376 return -ENOMEM;
10377
10378 q_vector->vsi = vsi;
10379 q_vector->v_idx = v_idx;
10380 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
10381
10382 if (vsi->netdev)
10383 netif_napi_add(vsi->netdev, &q_vector->napi,
10384 i40e_napi_poll, NAPI_POLL_WEIGHT);
10385
10386 q_vector->rx.latency_range = I40E_LOW_LATENCY;
10387 q_vector->tx.latency_range = I40E_LOW_LATENCY;
10388
10389 /* tie q_vector and vsi together */
10390 vsi->q_vectors[v_idx] = q_vector;
10391
10392 return 0;
10393 }
10394
10395 /**
10396 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
10397 * @vsi: the VSI being configured
10398 *
10399 * We allocate one q_vector per queue interrupt. If allocation fails we
10400 * return -ENOMEM.
10401 **/
10402 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
10403 {
10404 struct i40e_pf *pf = vsi->back;
10405 int err, v_idx, num_q_vectors, current_cpu;
10406
10407 /* if not MSIX, give the one vector only to the LAN VSI */
10408 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10409 num_q_vectors = vsi->num_q_vectors;
10410 else if (vsi == pf->vsi[pf->lan_vsi])
10411 num_q_vectors = 1;
10412 else
10413 return -EINVAL;
10414
10415 current_cpu = cpumask_first(cpu_online_mask);
10416
10417 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
10418 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
10419 if (err)
10420 goto err_out;
10421 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
10422 if (unlikely(current_cpu >= nr_cpu_ids))
10423 current_cpu = cpumask_first(cpu_online_mask);
10424 }
10425
10426 return 0;
10427
10428 err_out:
10429 while (v_idx--)
10430 i40e_free_q_vector(vsi, v_idx);
10431
10432 return err;
10433 }
10434
10435 /**
10436 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
10437 * @pf: board private structure to initialize
10438 **/
10439 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
10440 {
10441 int vectors = 0;
10442 ssize_t size;
10443
10444 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
10445 vectors = i40e_init_msix(pf);
10446 if (vectors < 0) {
10447 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
10448 I40E_FLAG_IWARP_ENABLED |
10449 I40E_FLAG_RSS_ENABLED |
10450 I40E_FLAG_DCB_CAPABLE |
10451 I40E_FLAG_DCB_ENABLED |
10452 I40E_FLAG_SRIOV_ENABLED |
10453 I40E_FLAG_FD_SB_ENABLED |
10454 I40E_FLAG_FD_ATR_ENABLED |
10455 I40E_FLAG_VMDQ_ENABLED);
10456 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10457
10458 /* rework the queue expectations without MSIX */
10459 i40e_determine_queue_usage(pf);
10460 }
10461 }
10462
10463 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
10464 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
10465 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
10466 vectors = pci_enable_msi(pf->pdev);
10467 if (vectors < 0) {
10468 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
10469 vectors);
10470 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
10471 }
10472 vectors = 1; /* one MSI or Legacy vector */
10473 }
10474
10475 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
10476 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
10477
10478 /* set up vector assignment tracking */
10479 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
10480 pf->irq_pile = kzalloc(size, GFP_KERNEL);
10481 if (!pf->irq_pile) {
10482 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
10483 return -ENOMEM;
10484 }
10485 pf->irq_pile->num_entries = vectors;
10486 pf->irq_pile->search_hint = 0;
10487
10488 /* track first vector for misc interrupts, ignore return */
10489 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
10490
10491 return 0;
10492 }
10493
10494 /**
10495 * i40e_restore_interrupt_scheme - Restore the interrupt scheme
10496 * @pf: private board data structure
10497 *
10498 * Restore the interrupt scheme that was cleared when we suspended the
10499 * device. This should be called during resume to re-allocate the q_vectors
10500 * and reacquire IRQs.
10501 */
10502 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
10503 {
10504 int err, i;
10505
10506 /* We cleared the MSI and MSI-X flags when disabling the old interrupt
10507 * scheme. We need to re-enabled them here in order to attempt to
10508 * re-acquire the MSI or MSI-X vectors
10509 */
10510 pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
10511
10512 err = i40e_init_interrupt_scheme(pf);
10513 if (err)
10514 return err;
10515
10516 /* Now that we've re-acquired IRQs, we need to remap the vectors and
10517 * rings together again.
10518 */
10519 for (i = 0; i < pf->num_alloc_vsi; i++) {
10520 if (pf->vsi[i]) {
10521 err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
10522 if (err)
10523 goto err_unwind;
10524 i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
10525 }
10526 }
10527
10528 err = i40e_setup_misc_vector(pf);
10529 if (err)
10530 goto err_unwind;
10531
10532 return 0;
10533
10534 err_unwind:
10535 while (i--) {
10536 if (pf->vsi[i])
10537 i40e_vsi_free_q_vectors(pf->vsi[i]);
10538 }
10539
10540 return err;
10541 }
10542
10543 /**
10544 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
10545 * @pf: board private structure
10546 *
10547 * This sets up the handler for MSIX 0, which is used to manage the
10548 * non-queue interrupts, e.g. AdminQ and errors. This is not used
10549 * when in MSI or Legacy interrupt mode.
10550 **/
10551 static int i40e_setup_misc_vector(struct i40e_pf *pf)
10552 {
10553 struct i40e_hw *hw = &pf->hw;
10554 int err = 0;
10555
10556 /* Only request the IRQ once, the first time through. */
10557 if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
10558 err = request_irq(pf->msix_entries[0].vector,
10559 i40e_intr, 0, pf->int_name, pf);
10560 if (err) {
10561 clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
10562 dev_info(&pf->pdev->dev,
10563 "request_irq for %s failed: %d\n",
10564 pf->int_name, err);
10565 return -EFAULT;
10566 }
10567 }
10568
10569 i40e_enable_misc_int_causes(pf);
10570
10571 /* associate no queues to the misc vector */
10572 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
10573 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
10574
10575 i40e_flush(hw);
10576
10577 i40e_irq_dynamic_enable_icr0(pf);
10578
10579 return err;
10580 }
10581
10582 /**
10583 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
10584 * @vsi: Pointer to vsi structure
10585 * @seed: Buffter to store the hash keys
10586 * @lut: Buffer to store the lookup table entries
10587 * @lut_size: Size of buffer to store the lookup table entries
10588 *
10589 * Return 0 on success, negative on failure
10590 */
10591 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
10592 u8 *lut, u16 lut_size)
10593 {
10594 struct i40e_pf *pf = vsi->back;
10595 struct i40e_hw *hw = &pf->hw;
10596 int ret = 0;
10597
10598 if (seed) {
10599 ret = i40e_aq_get_rss_key(hw, vsi->id,
10600 (struct i40e_aqc_get_set_rss_key_data *)seed);
10601 if (ret) {
10602 dev_info(&pf->pdev->dev,
10603 "Cannot get RSS key, err %s aq_err %s\n",
10604 i40e_stat_str(&pf->hw, ret),
10605 i40e_aq_str(&pf->hw,
10606 pf->hw.aq.asq_last_status));
10607 return ret;
10608 }
10609 }
10610
10611 if (lut) {
10612 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
10613
10614 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
10615 if (ret) {
10616 dev_info(&pf->pdev->dev,
10617 "Cannot get RSS lut, err %s aq_err %s\n",
10618 i40e_stat_str(&pf->hw, ret),
10619 i40e_aq_str(&pf->hw,
10620 pf->hw.aq.asq_last_status));
10621 return ret;
10622 }
10623 }
10624
10625 return ret;
10626 }
10627
10628 /**
10629 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
10630 * @vsi: Pointer to vsi structure
10631 * @seed: RSS hash seed
10632 * @lut: Lookup table
10633 * @lut_size: Lookup table size
10634 *
10635 * Returns 0 on success, negative on failure
10636 **/
10637 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
10638 const u8 *lut, u16 lut_size)
10639 {
10640 struct i40e_pf *pf = vsi->back;
10641 struct i40e_hw *hw = &pf->hw;
10642 u16 vf_id = vsi->vf_id;
10643 u8 i;
10644
10645 /* Fill out hash function seed */
10646 if (seed) {
10647 u32 *seed_dw = (u32 *)seed;
10648
10649 if (vsi->type == I40E_VSI_MAIN) {
10650 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
10651 wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
10652 } else if (vsi->type == I40E_VSI_SRIOV) {
10653 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
10654 wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
10655 } else {
10656 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
10657 }
10658 }
10659
10660 if (lut) {
10661 u32 *lut_dw = (u32 *)lut;
10662
10663 if (vsi->type == I40E_VSI_MAIN) {
10664 if (lut_size != I40E_HLUT_ARRAY_SIZE)
10665 return -EINVAL;
10666 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
10667 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
10668 } else if (vsi->type == I40E_VSI_SRIOV) {
10669 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
10670 return -EINVAL;
10671 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
10672 wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
10673 } else {
10674 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
10675 }
10676 }
10677 i40e_flush(hw);
10678
10679 return 0;
10680 }
10681
10682 /**
10683 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
10684 * @vsi: Pointer to VSI structure
10685 * @seed: Buffer to store the keys
10686 * @lut: Buffer to store the lookup table entries
10687 * @lut_size: Size of buffer to store the lookup table entries
10688 *
10689 * Returns 0 on success, negative on failure
10690 */
10691 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
10692 u8 *lut, u16 lut_size)
10693 {
10694 struct i40e_pf *pf = vsi->back;
10695 struct i40e_hw *hw = &pf->hw;
10696 u16 i;
10697
10698 if (seed) {
10699 u32 *seed_dw = (u32 *)seed;
10700
10701 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
10702 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
10703 }
10704 if (lut) {
10705 u32 *lut_dw = (u32 *)lut;
10706
10707 if (lut_size != I40E_HLUT_ARRAY_SIZE)
10708 return -EINVAL;
10709 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
10710 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
10711 }
10712
10713 return 0;
10714 }
10715
10716 /**
10717 * i40e_config_rss - Configure RSS keys and lut
10718 * @vsi: Pointer to VSI structure
10719 * @seed: RSS hash seed
10720 * @lut: Lookup table
10721 * @lut_size: Lookup table size
10722 *
10723 * Returns 0 on success, negative on failure
10724 */
10725 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
10726 {
10727 struct i40e_pf *pf = vsi->back;
10728
10729 if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
10730 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
10731 else
10732 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
10733 }
10734
10735 /**
10736 * i40e_get_rss - Get RSS keys and lut
10737 * @vsi: Pointer to VSI structure
10738 * @seed: Buffer to store the keys
10739 * @lut: Buffer to store the lookup table entries
10740 * lut_size: Size of buffer to store the lookup table entries
10741 *
10742 * Returns 0 on success, negative on failure
10743 */
10744 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
10745 {
10746 struct i40e_pf *pf = vsi->back;
10747
10748 if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
10749 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
10750 else
10751 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
10752 }
10753
10754 /**
10755 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
10756 * @pf: Pointer to board private structure
10757 * @lut: Lookup table
10758 * @rss_table_size: Lookup table size
10759 * @rss_size: Range of queue number for hashing
10760 */
10761 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
10762 u16 rss_table_size, u16 rss_size)
10763 {
10764 u16 i;
10765
10766 for (i = 0; i < rss_table_size; i++)
10767 lut[i] = i % rss_size;
10768 }
10769
10770 /**
10771 * i40e_pf_config_rss - Prepare for RSS if used
10772 * @pf: board private structure
10773 **/
10774 static int i40e_pf_config_rss(struct i40e_pf *pf)
10775 {
10776 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10777 u8 seed[I40E_HKEY_ARRAY_SIZE];
10778 u8 *lut;
10779 struct i40e_hw *hw = &pf->hw;
10780 u32 reg_val;
10781 u64 hena;
10782 int ret;
10783
10784 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
10785 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
10786 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
10787 hena |= i40e_pf_get_default_rss_hena(pf);
10788
10789 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
10790 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
10791
10792 /* Determine the RSS table size based on the hardware capabilities */
10793 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
10794 reg_val = (pf->rss_table_size == 512) ?
10795 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
10796 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
10797 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
10798
10799 /* Determine the RSS size of the VSI */
10800 if (!vsi->rss_size) {
10801 u16 qcount;
10802
10803 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
10804 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
10805 }
10806 if (!vsi->rss_size)
10807 return -EINVAL;
10808
10809 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
10810 if (!lut)
10811 return -ENOMEM;
10812
10813 /* Use user configured lut if there is one, otherwise use default */
10814 if (vsi->rss_lut_user)
10815 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
10816 else
10817 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
10818
10819 /* Use user configured hash key if there is one, otherwise
10820 * use default.
10821 */
10822 if (vsi->rss_hkey_user)
10823 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
10824 else
10825 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
10826 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
10827 kfree(lut);
10828
10829 return ret;
10830 }
10831
10832 /**
10833 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
10834 * @pf: board private structure
10835 * @queue_count: the requested queue count for rss.
10836 *
10837 * returns 0 if rss is not enabled, if enabled returns the final rss queue
10838 * count which may be different from the requested queue count.
10839 * Note: expects to be called while under rtnl_lock()
10840 **/
10841 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
10842 {
10843 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10844 int new_rss_size;
10845
10846 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
10847 return 0;
10848
10849 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
10850
10851 if (queue_count != vsi->num_queue_pairs) {
10852 u16 qcount;
10853
10854 vsi->req_queue_pairs = queue_count;
10855 i40e_prep_for_reset(pf, true);
10856
10857 pf->alloc_rss_size = new_rss_size;
10858
10859 i40e_reset_and_rebuild(pf, true, true);
10860
10861 /* Discard the user configured hash keys and lut, if less
10862 * queues are enabled.
10863 */
10864 if (queue_count < vsi->rss_size) {
10865 i40e_clear_rss_config_user(vsi);
10866 dev_dbg(&pf->pdev->dev,
10867 "discard user configured hash keys and lut\n");
10868 }
10869
10870 /* Reset vsi->rss_size, as number of enabled queues changed */
10871 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
10872 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
10873
10874 i40e_pf_config_rss(pf);
10875 }
10876 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
10877 vsi->req_queue_pairs, pf->rss_size_max);
10878 return pf->alloc_rss_size;
10879 }
10880
10881 /**
10882 * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
10883 * @pf: board private structure
10884 **/
10885 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
10886 {
10887 i40e_status status;
10888 bool min_valid, max_valid;
10889 u32 max_bw, min_bw;
10890
10891 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
10892 &min_valid, &max_valid);
10893
10894 if (!status) {
10895 if (min_valid)
10896 pf->min_bw = min_bw;
10897 if (max_valid)
10898 pf->max_bw = max_bw;
10899 }
10900
10901 return status;
10902 }
10903
10904 /**
10905 * i40e_set_partition_bw_setting - Set BW settings for this PF partition
10906 * @pf: board private structure
10907 **/
10908 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
10909 {
10910 struct i40e_aqc_configure_partition_bw_data bw_data;
10911 i40e_status status;
10912
10913 /* Set the valid bit for this PF */
10914 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
10915 bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
10916 bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
10917
10918 /* Set the new bandwidths */
10919 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
10920
10921 return status;
10922 }
10923
10924 /**
10925 * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
10926 * @pf: board private structure
10927 **/
10928 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
10929 {
10930 /* Commit temporary BW setting to permanent NVM image */
10931 enum i40e_admin_queue_err last_aq_status;
10932 i40e_status ret;
10933 u16 nvm_word;
10934
10935 if (pf->hw.partition_id != 1) {
10936 dev_info(&pf->pdev->dev,
10937 "Commit BW only works on partition 1! This is partition %d",
10938 pf->hw.partition_id);
10939 ret = I40E_NOT_SUPPORTED;
10940 goto bw_commit_out;
10941 }
10942
10943 /* Acquire NVM for read access */
10944 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
10945 last_aq_status = pf->hw.aq.asq_last_status;
10946 if (ret) {
10947 dev_info(&pf->pdev->dev,
10948 "Cannot acquire NVM for read access, err %s aq_err %s\n",
10949 i40e_stat_str(&pf->hw, ret),
10950 i40e_aq_str(&pf->hw, last_aq_status));
10951 goto bw_commit_out;
10952 }
10953
10954 /* Read word 0x10 of NVM - SW compatibility word 1 */
10955 ret = i40e_aq_read_nvm(&pf->hw,
10956 I40E_SR_NVM_CONTROL_WORD,
10957 0x10, sizeof(nvm_word), &nvm_word,
10958 false, NULL);
10959 /* Save off last admin queue command status before releasing
10960 * the NVM
10961 */
10962 last_aq_status = pf->hw.aq.asq_last_status;
10963 i40e_release_nvm(&pf->hw);
10964 if (ret) {
10965 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
10966 i40e_stat_str(&pf->hw, ret),
10967 i40e_aq_str(&pf->hw, last_aq_status));
10968 goto bw_commit_out;
10969 }
10970
10971 /* Wait a bit for NVM release to complete */
10972 msleep(50);
10973
10974 /* Acquire NVM for write access */
10975 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
10976 last_aq_status = pf->hw.aq.asq_last_status;
10977 if (ret) {
10978 dev_info(&pf->pdev->dev,
10979 "Cannot acquire NVM for write access, err %s aq_err %s\n",
10980 i40e_stat_str(&pf->hw, ret),
10981 i40e_aq_str(&pf->hw, last_aq_status));
10982 goto bw_commit_out;
10983 }
10984 /* Write it back out unchanged to initiate update NVM,
10985 * which will force a write of the shadow (alt) RAM to
10986 * the NVM - thus storing the bandwidth values permanently.
10987 */
10988 ret = i40e_aq_update_nvm(&pf->hw,
10989 I40E_SR_NVM_CONTROL_WORD,
10990 0x10, sizeof(nvm_word),
10991 &nvm_word, true, NULL);
10992 /* Save off last admin queue command status before releasing
10993 * the NVM
10994 */
10995 last_aq_status = pf->hw.aq.asq_last_status;
10996 i40e_release_nvm(&pf->hw);
10997 if (ret)
10998 dev_info(&pf->pdev->dev,
10999 "BW settings NOT SAVED, err %s aq_err %s\n",
11000 i40e_stat_str(&pf->hw, ret),
11001 i40e_aq_str(&pf->hw, last_aq_status));
11002 bw_commit_out:
11003
11004 return ret;
11005 }
11006
11007 /**
11008 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
11009 * @pf: board private structure to initialize
11010 *
11011 * i40e_sw_init initializes the Adapter private data structure.
11012 * Fields are initialized based on PCI device information and
11013 * OS network device settings (MTU size).
11014 **/
11015 static int i40e_sw_init(struct i40e_pf *pf)
11016 {
11017 int err = 0;
11018 int size;
11019
11020 /* Set default capability flags */
11021 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
11022 I40E_FLAG_MSI_ENABLED |
11023 I40E_FLAG_MSIX_ENABLED;
11024
11025 /* Set default ITR */
11026 pf->rx_itr_default = I40E_ITR_RX_DEF;
11027 pf->tx_itr_default = I40E_ITR_TX_DEF;
11028
11029 /* Depending on PF configurations, it is possible that the RSS
11030 * maximum might end up larger than the available queues
11031 */
11032 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
11033 pf->alloc_rss_size = 1;
11034 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
11035 pf->rss_size_max = min_t(int, pf->rss_size_max,
11036 pf->hw.func_caps.num_tx_qp);
11037 if (pf->hw.func_caps.rss) {
11038 pf->flags |= I40E_FLAG_RSS_ENABLED;
11039 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
11040 num_online_cpus());
11041 }
11042
11043 /* MFP mode enabled */
11044 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
11045 pf->flags |= I40E_FLAG_MFP_ENABLED;
11046 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
11047 if (i40e_get_partition_bw_setting(pf)) {
11048 dev_warn(&pf->pdev->dev,
11049 "Could not get partition bw settings\n");
11050 } else {
11051 dev_info(&pf->pdev->dev,
11052 "Partition BW Min = %8.8x, Max = %8.8x\n",
11053 pf->min_bw, pf->max_bw);
11054
11055 /* nudge the Tx scheduler */
11056 i40e_set_partition_bw_setting(pf);
11057 }
11058 }
11059
11060 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
11061 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
11062 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
11063 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
11064 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
11065 pf->hw.num_partitions > 1)
11066 dev_info(&pf->pdev->dev,
11067 "Flow Director Sideband mode Disabled in MFP mode\n");
11068 else
11069 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
11070 pf->fdir_pf_filter_count =
11071 pf->hw.func_caps.fd_filters_guaranteed;
11072 pf->hw.fdir_shared_filter_count =
11073 pf->hw.func_caps.fd_filters_best_effort;
11074 }
11075
11076 if (pf->hw.mac.type == I40E_MAC_X722) {
11077 pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
11078 I40E_HW_128_QP_RSS_CAPABLE |
11079 I40E_HW_ATR_EVICT_CAPABLE |
11080 I40E_HW_WB_ON_ITR_CAPABLE |
11081 I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
11082 I40E_HW_NO_PCI_LINK_CHECK |
11083 I40E_HW_USE_SET_LLDP_MIB |
11084 I40E_HW_GENEVE_OFFLOAD_CAPABLE |
11085 I40E_HW_PTP_L4_CAPABLE |
11086 I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
11087 I40E_HW_OUTER_UDP_CSUM_CAPABLE);
11088
11089 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
11090 if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
11091 I40E_FDEVICT_PCTYPE_DEFAULT) {
11092 dev_warn(&pf->pdev->dev,
11093 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
11094 pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
11095 }
11096 } else if ((pf->hw.aq.api_maj_ver > 1) ||
11097 ((pf->hw.aq.api_maj_ver == 1) &&
11098 (pf->hw.aq.api_min_ver > 4))) {
11099 /* Supported in FW API version higher than 1.4 */
11100 pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
11101 }
11102
11103 /* Enable HW ATR eviction if possible */
11104 if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
11105 pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
11106
11107 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11108 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
11109 (pf->hw.aq.fw_maj_ver < 4))) {
11110 pf->hw_features |= I40E_HW_RESTART_AUTONEG;
11111 /* No DCB support for FW < v4.33 */
11112 pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
11113 }
11114
11115 /* Disable FW LLDP if FW < v4.3 */
11116 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11117 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
11118 (pf->hw.aq.fw_maj_ver < 4)))
11119 pf->hw_features |= I40E_HW_STOP_FW_LLDP;
11120
11121 /* Use the FW Set LLDP MIB API if FW > v4.40 */
11122 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
11123 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
11124 (pf->hw.aq.fw_maj_ver >= 5)))
11125 pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
11126
11127 /* Enable PTP L4 if FW > v6.0 */
11128 if (pf->hw.mac.type == I40E_MAC_XL710 &&
11129 pf->hw.aq.fw_maj_ver >= 6)
11130 pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
11131
11132 if (pf->hw.func_caps.vmdq) {
11133 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
11134 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
11135 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
11136 }
11137
11138 if (pf->hw.func_caps.iwarp) {
11139 pf->flags |= I40E_FLAG_IWARP_ENABLED;
11140 /* IWARP needs one extra vector for CQP just like MISC.*/
11141 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
11142 }
11143
11144 #ifdef CONFIG_PCI_IOV
11145 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
11146 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
11147 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
11148 pf->num_req_vfs = min_t(int,
11149 pf->hw.func_caps.num_vfs,
11150 I40E_MAX_VF_COUNT);
11151 }
11152 #endif /* CONFIG_PCI_IOV */
11153 pf->eeprom_version = 0xDEAD;
11154 pf->lan_veb = I40E_NO_VEB;
11155 pf->lan_vsi = I40E_NO_VSI;
11156
11157 /* By default FW has this off for performance reasons */
11158 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
11159
11160 /* set up queue assignment tracking */
11161 size = sizeof(struct i40e_lump_tracking)
11162 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
11163 pf->qp_pile = kzalloc(size, GFP_KERNEL);
11164 if (!pf->qp_pile) {
11165 err = -ENOMEM;
11166 goto sw_init_done;
11167 }
11168 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
11169 pf->qp_pile->search_hint = 0;
11170
11171 pf->tx_timeout_recovery_level = 1;
11172
11173 mutex_init(&pf->switch_mutex);
11174
11175 sw_init_done:
11176 return err;
11177 }
11178
11179 /**
11180 * i40e_set_ntuple - set the ntuple feature flag and take action
11181 * @pf: board private structure to initialize
11182 * @features: the feature set that the stack is suggesting
11183 *
11184 * returns a bool to indicate if reset needs to happen
11185 **/
11186 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
11187 {
11188 bool need_reset = false;
11189
11190 /* Check if Flow Director n-tuple support was enabled or disabled. If
11191 * the state changed, we need to reset.
11192 */
11193 if (features & NETIF_F_NTUPLE) {
11194 /* Enable filters and mark for reset */
11195 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
11196 need_reset = true;
11197 /* enable FD_SB only if there is MSI-X vector and no cloud
11198 * filters exist
11199 */
11200 if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
11201 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
11202 pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
11203 }
11204 } else {
11205 /* turn off filters, mark for reset and clear SW filter list */
11206 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11207 need_reset = true;
11208 i40e_fdir_filter_exit(pf);
11209 }
11210 pf->flags &= ~(I40E_FLAG_FD_SB_ENABLED |
11211 I40E_FLAG_FD_SB_AUTO_DISABLED);
11212 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11213
11214 /* reset fd counters */
11215 pf->fd_add_err = 0;
11216 pf->fd_atr_cnt = 0;
11217 /* if ATR was auto disabled it can be re-enabled. */
11218 if (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED) {
11219 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
11220 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
11221 (I40E_DEBUG_FD & pf->hw.debug_mask))
11222 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
11223 }
11224 }
11225 return need_reset;
11226 }
11227
11228 /**
11229 * i40e_clear_rss_lut - clear the rx hash lookup table
11230 * @vsi: the VSI being configured
11231 **/
11232 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
11233 {
11234 struct i40e_pf *pf = vsi->back;
11235 struct i40e_hw *hw = &pf->hw;
11236 u16 vf_id = vsi->vf_id;
11237 u8 i;
11238
11239 if (vsi->type == I40E_VSI_MAIN) {
11240 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
11241 wr32(hw, I40E_PFQF_HLUT(i), 0);
11242 } else if (vsi->type == I40E_VSI_SRIOV) {
11243 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
11244 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
11245 } else {
11246 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
11247 }
11248 }
11249
11250 /**
11251 * i40e_set_features - set the netdev feature flags
11252 * @netdev: ptr to the netdev being adjusted
11253 * @features: the feature set that the stack is suggesting
11254 * Note: expects to be called while under rtnl_lock()
11255 **/
11256 static int i40e_set_features(struct net_device *netdev,
11257 netdev_features_t features)
11258 {
11259 struct i40e_netdev_priv *np = netdev_priv(netdev);
11260 struct i40e_vsi *vsi = np->vsi;
11261 struct i40e_pf *pf = vsi->back;
11262 bool need_reset;
11263
11264 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
11265 i40e_pf_config_rss(pf);
11266 else if (!(features & NETIF_F_RXHASH) &&
11267 netdev->features & NETIF_F_RXHASH)
11268 i40e_clear_rss_lut(vsi);
11269
11270 if (features & NETIF_F_HW_VLAN_CTAG_RX)
11271 i40e_vlan_stripping_enable(vsi);
11272 else
11273 i40e_vlan_stripping_disable(vsi);
11274
11275 if (!(features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
11276 dev_err(&pf->pdev->dev,
11277 "Offloaded tc filters active, can't turn hw_tc_offload off");
11278 return -EINVAL;
11279 }
11280
11281 need_reset = i40e_set_ntuple(pf, features);
11282
11283 if (need_reset)
11284 i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
11285
11286 return 0;
11287 }
11288
11289 /**
11290 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
11291 * @pf: board private structure
11292 * @port: The UDP port to look up
11293 *
11294 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
11295 **/
11296 static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
11297 {
11298 u8 i;
11299
11300 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
11301 if (pf->udp_ports[i].port == port)
11302 return i;
11303 }
11304
11305 return i;
11306 }
11307
11308 /**
11309 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
11310 * @netdev: This physical port's netdev
11311 * @ti: Tunnel endpoint information
11312 **/
11313 static void i40e_udp_tunnel_add(struct net_device *netdev,
11314 struct udp_tunnel_info *ti)
11315 {
11316 struct i40e_netdev_priv *np = netdev_priv(netdev);
11317 struct i40e_vsi *vsi = np->vsi;
11318 struct i40e_pf *pf = vsi->back;
11319 u16 port = ntohs(ti->port);
11320 u8 next_idx;
11321 u8 idx;
11322
11323 idx = i40e_get_udp_port_idx(pf, port);
11324
11325 /* Check if port already exists */
11326 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
11327 netdev_info(netdev, "port %d already offloaded\n", port);
11328 return;
11329 }
11330
11331 /* Now check if there is space to add the new port */
11332 next_idx = i40e_get_udp_port_idx(pf, 0);
11333
11334 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
11335 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
11336 port);
11337 return;
11338 }
11339
11340 switch (ti->type) {
11341 case UDP_TUNNEL_TYPE_VXLAN:
11342 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
11343 break;
11344 case UDP_TUNNEL_TYPE_GENEVE:
11345 if (!(pf->hw_features & I40E_HW_GENEVE_OFFLOAD_CAPABLE))
11346 return;
11347 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
11348 break;
11349 default:
11350 return;
11351 }
11352
11353 /* New port: add it and mark its index in the bitmap */
11354 pf->udp_ports[next_idx].port = port;
11355 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
11356 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
11357 }
11358
11359 /**
11360 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
11361 * @netdev: This physical port's netdev
11362 * @ti: Tunnel endpoint information
11363 **/
11364 static void i40e_udp_tunnel_del(struct net_device *netdev,
11365 struct udp_tunnel_info *ti)
11366 {
11367 struct i40e_netdev_priv *np = netdev_priv(netdev);
11368 struct i40e_vsi *vsi = np->vsi;
11369 struct i40e_pf *pf = vsi->back;
11370 u16 port = ntohs(ti->port);
11371 u8 idx;
11372
11373 idx = i40e_get_udp_port_idx(pf, port);
11374
11375 /* Check if port already exists */
11376 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
11377 goto not_found;
11378
11379 switch (ti->type) {
11380 case UDP_TUNNEL_TYPE_VXLAN:
11381 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
11382 goto not_found;
11383 break;
11384 case UDP_TUNNEL_TYPE_GENEVE:
11385 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
11386 goto not_found;
11387 break;
11388 default:
11389 goto not_found;
11390 }
11391
11392 /* if port exists, set it to 0 (mark for deletion)
11393 * and make it pending
11394 */
11395 pf->udp_ports[idx].port = 0;
11396 pf->pending_udp_bitmap |= BIT_ULL(idx);
11397 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
11398
11399 return;
11400 not_found:
11401 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
11402 port);
11403 }
11404
11405 static int i40e_get_phys_port_id(struct net_device *netdev,
11406 struct netdev_phys_item_id *ppid)
11407 {
11408 struct i40e_netdev_priv *np = netdev_priv(netdev);
11409 struct i40e_pf *pf = np->vsi->back;
11410 struct i40e_hw *hw = &pf->hw;
11411
11412 if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
11413 return -EOPNOTSUPP;
11414
11415 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
11416 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
11417
11418 return 0;
11419 }
11420
11421 /**
11422 * i40e_ndo_fdb_add - add an entry to the hardware database
11423 * @ndm: the input from the stack
11424 * @tb: pointer to array of nladdr (unused)
11425 * @dev: the net device pointer
11426 * @addr: the MAC address entry being added
11427 * @flags: instructions from stack about fdb operation
11428 */
11429 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
11430 struct net_device *dev,
11431 const unsigned char *addr, u16 vid,
11432 u16 flags)
11433 {
11434 struct i40e_netdev_priv *np = netdev_priv(dev);
11435 struct i40e_pf *pf = np->vsi->back;
11436 int err = 0;
11437
11438 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
11439 return -EOPNOTSUPP;
11440
11441 if (vid) {
11442 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
11443 return -EINVAL;
11444 }
11445
11446 /* Hardware does not support aging addresses so if a
11447 * ndm_state is given only allow permanent addresses
11448 */
11449 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
11450 netdev_info(dev, "FDB only supports static addresses\n");
11451 return -EINVAL;
11452 }
11453
11454 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
11455 err = dev_uc_add_excl(dev, addr);
11456 else if (is_multicast_ether_addr(addr))
11457 err = dev_mc_add_excl(dev, addr);
11458 else
11459 err = -EINVAL;
11460
11461 /* Only return duplicate errors if NLM_F_EXCL is set */
11462 if (err == -EEXIST && !(flags & NLM_F_EXCL))
11463 err = 0;
11464
11465 return err;
11466 }
11467
11468 /**
11469 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
11470 * @dev: the netdev being configured
11471 * @nlh: RTNL message
11472 *
11473 * Inserts a new hardware bridge if not already created and
11474 * enables the bridging mode requested (VEB or VEPA). If the
11475 * hardware bridge has already been inserted and the request
11476 * is to change the mode then that requires a PF reset to
11477 * allow rebuild of the components with required hardware
11478 * bridge mode enabled.
11479 *
11480 * Note: expects to be called while under rtnl_lock()
11481 **/
11482 static int i40e_ndo_bridge_setlink(struct net_device *dev,
11483 struct nlmsghdr *nlh,
11484 u16 flags)
11485 {
11486 struct i40e_netdev_priv *np = netdev_priv(dev);
11487 struct i40e_vsi *vsi = np->vsi;
11488 struct i40e_pf *pf = vsi->back;
11489 struct i40e_veb *veb = NULL;
11490 struct nlattr *attr, *br_spec;
11491 int i, rem;
11492
11493 /* Only for PF VSI for now */
11494 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
11495 return -EOPNOTSUPP;
11496
11497 /* Find the HW bridge for PF VSI */
11498 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
11499 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
11500 veb = pf->veb[i];
11501 }
11502
11503 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
11504
11505 nla_for_each_nested(attr, br_spec, rem) {
11506 __u16 mode;
11507
11508 if (nla_type(attr) != IFLA_BRIDGE_MODE)
11509 continue;
11510
11511 mode = nla_get_u16(attr);
11512 if ((mode != BRIDGE_MODE_VEPA) &&
11513 (mode != BRIDGE_MODE_VEB))
11514 return -EINVAL;
11515
11516 /* Insert a new HW bridge */
11517 if (!veb) {
11518 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
11519 vsi->tc_config.enabled_tc);
11520 if (veb) {
11521 veb->bridge_mode = mode;
11522 i40e_config_bridge_mode(veb);
11523 } else {
11524 /* No Bridge HW offload available */
11525 return -ENOENT;
11526 }
11527 break;
11528 } else if (mode != veb->bridge_mode) {
11529 /* Existing HW bridge but different mode needs reset */
11530 veb->bridge_mode = mode;
11531 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
11532 if (mode == BRIDGE_MODE_VEB)
11533 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11534 else
11535 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
11536 i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
11537 break;
11538 }
11539 }
11540
11541 return 0;
11542 }
11543
11544 /**
11545 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
11546 * @skb: skb buff
11547 * @pid: process id
11548 * @seq: RTNL message seq #
11549 * @dev: the netdev being configured
11550 * @filter_mask: unused
11551 * @nlflags: netlink flags passed in
11552 *
11553 * Return the mode in which the hardware bridge is operating in
11554 * i.e VEB or VEPA.
11555 **/
11556 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
11557 struct net_device *dev,
11558 u32 __always_unused filter_mask,
11559 int nlflags)
11560 {
11561 struct i40e_netdev_priv *np = netdev_priv(dev);
11562 struct i40e_vsi *vsi = np->vsi;
11563 struct i40e_pf *pf = vsi->back;
11564 struct i40e_veb *veb = NULL;
11565 int i;
11566
11567 /* Only for PF VSI for now */
11568 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
11569 return -EOPNOTSUPP;
11570
11571 /* Find the HW bridge for the PF VSI */
11572 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
11573 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
11574 veb = pf->veb[i];
11575 }
11576
11577 if (!veb)
11578 return 0;
11579
11580 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
11581 0, 0, nlflags, filter_mask, NULL);
11582 }
11583
11584 /**
11585 * i40e_features_check - Validate encapsulated packet conforms to limits
11586 * @skb: skb buff
11587 * @dev: This physical port's netdev
11588 * @features: Offload features that the stack believes apply
11589 **/
11590 static netdev_features_t i40e_features_check(struct sk_buff *skb,
11591 struct net_device *dev,
11592 netdev_features_t features)
11593 {
11594 size_t len;
11595
11596 /* No point in doing any of this if neither checksum nor GSO are
11597 * being requested for this frame. We can rule out both by just
11598 * checking for CHECKSUM_PARTIAL
11599 */
11600 if (skb->ip_summed != CHECKSUM_PARTIAL)
11601 return features;
11602
11603 /* We cannot support GSO if the MSS is going to be less than
11604 * 64 bytes. If it is then we need to drop support for GSO.
11605 */
11606 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
11607 features &= ~NETIF_F_GSO_MASK;
11608
11609 /* MACLEN can support at most 63 words */
11610 len = skb_network_header(skb) - skb->data;
11611 if (len & ~(63 * 2))
11612 goto out_err;
11613
11614 /* IPLEN and EIPLEN can support at most 127 dwords */
11615 len = skb_transport_header(skb) - skb_network_header(skb);
11616 if (len & ~(127 * 4))
11617 goto out_err;
11618
11619 if (skb->encapsulation) {
11620 /* L4TUNLEN can support 127 words */
11621 len = skb_inner_network_header(skb) - skb_transport_header(skb);
11622 if (len & ~(127 * 2))
11623 goto out_err;
11624
11625 /* IPLEN can support at most 127 dwords */
11626 len = skb_inner_transport_header(skb) -
11627 skb_inner_network_header(skb);
11628 if (len & ~(127 * 4))
11629 goto out_err;
11630 }
11631
11632 /* No need to validate L4LEN as TCP is the only protocol with a
11633 * a flexible value and we support all possible values supported
11634 * by TCP, which is at most 15 dwords
11635 */
11636
11637 return features;
11638 out_err:
11639 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
11640 }
11641
11642 /**
11643 * i40e_xdp_setup - add/remove an XDP program
11644 * @vsi: VSI to changed
11645 * @prog: XDP program
11646 **/
11647 static int i40e_xdp_setup(struct i40e_vsi *vsi,
11648 struct bpf_prog *prog)
11649 {
11650 int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
11651 struct i40e_pf *pf = vsi->back;
11652 struct bpf_prog *old_prog;
11653 bool need_reset;
11654 int i;
11655
11656 /* Don't allow frames that span over multiple buffers */
11657 if (frame_size > vsi->rx_buf_len)
11658 return -EINVAL;
11659
11660 if (!i40e_enabled_xdp_vsi(vsi) && !prog)
11661 return 0;
11662
11663 /* When turning XDP on->off/off->on we reset and rebuild the rings. */
11664 need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
11665
11666 if (need_reset)
11667 i40e_prep_for_reset(pf, true);
11668
11669 old_prog = xchg(&vsi->xdp_prog, prog);
11670
11671 if (need_reset)
11672 i40e_reset_and_rebuild(pf, true, true);
11673
11674 for (i = 0; i < vsi->num_queue_pairs; i++)
11675 WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
11676
11677 if (old_prog)
11678 bpf_prog_put(old_prog);
11679
11680 return 0;
11681 }
11682
11683 /**
11684 * i40e_xdp - implements ndo_bpf for i40e
11685 * @dev: netdevice
11686 * @xdp: XDP command
11687 **/
11688 static int i40e_xdp(struct net_device *dev,
11689 struct netdev_bpf *xdp)
11690 {
11691 struct i40e_netdev_priv *np = netdev_priv(dev);
11692 struct i40e_vsi *vsi = np->vsi;
11693
11694 if (vsi->type != I40E_VSI_MAIN)
11695 return -EINVAL;
11696
11697 switch (xdp->command) {
11698 case XDP_SETUP_PROG:
11699 return i40e_xdp_setup(vsi, xdp->prog);
11700 case XDP_QUERY_PROG:
11701 xdp->prog_attached = i40e_enabled_xdp_vsi(vsi);
11702 xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0;
11703 return 0;
11704 default:
11705 return -EINVAL;
11706 }
11707 }
11708
11709 static const struct net_device_ops i40e_netdev_ops = {
11710 .ndo_open = i40e_open,
11711 .ndo_stop = i40e_close,
11712 .ndo_start_xmit = i40e_lan_xmit_frame,
11713 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
11714 .ndo_set_rx_mode = i40e_set_rx_mode,
11715 .ndo_validate_addr = eth_validate_addr,
11716 .ndo_set_mac_address = i40e_set_mac,
11717 .ndo_change_mtu = i40e_change_mtu,
11718 .ndo_do_ioctl = i40e_ioctl,
11719 .ndo_tx_timeout = i40e_tx_timeout,
11720 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
11721 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
11722 #ifdef CONFIG_NET_POLL_CONTROLLER
11723 .ndo_poll_controller = i40e_netpoll,
11724 #endif
11725 .ndo_setup_tc = __i40e_setup_tc,
11726 .ndo_set_features = i40e_set_features,
11727 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
11728 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
11729 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
11730 .ndo_get_vf_config = i40e_ndo_get_vf_config,
11731 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
11732 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
11733 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
11734 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
11735 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
11736 .ndo_get_phys_port_id = i40e_get_phys_port_id,
11737 .ndo_fdb_add = i40e_ndo_fdb_add,
11738 .ndo_features_check = i40e_features_check,
11739 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
11740 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
11741 .ndo_bpf = i40e_xdp,
11742 };
11743
11744 /**
11745 * i40e_config_netdev - Setup the netdev flags
11746 * @vsi: the VSI being configured
11747 *
11748 * Returns 0 on success, negative value on failure
11749 **/
11750 static int i40e_config_netdev(struct i40e_vsi *vsi)
11751 {
11752 struct i40e_pf *pf = vsi->back;
11753 struct i40e_hw *hw = &pf->hw;
11754 struct i40e_netdev_priv *np;
11755 struct net_device *netdev;
11756 u8 broadcast[ETH_ALEN];
11757 u8 mac_addr[ETH_ALEN];
11758 int etherdev_size;
11759 netdev_features_t hw_enc_features;
11760 netdev_features_t hw_features;
11761
11762 etherdev_size = sizeof(struct i40e_netdev_priv);
11763 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
11764 if (!netdev)
11765 return -ENOMEM;
11766
11767 vsi->netdev = netdev;
11768 np = netdev_priv(netdev);
11769 np->vsi = vsi;
11770
11771 hw_enc_features = NETIF_F_SG |
11772 NETIF_F_IP_CSUM |
11773 NETIF_F_IPV6_CSUM |
11774 NETIF_F_HIGHDMA |
11775 NETIF_F_SOFT_FEATURES |
11776 NETIF_F_TSO |
11777 NETIF_F_TSO_ECN |
11778 NETIF_F_TSO6 |
11779 NETIF_F_GSO_GRE |
11780 NETIF_F_GSO_GRE_CSUM |
11781 NETIF_F_GSO_PARTIAL |
11782 NETIF_F_GSO_UDP_TUNNEL |
11783 NETIF_F_GSO_UDP_TUNNEL_CSUM |
11784 NETIF_F_SCTP_CRC |
11785 NETIF_F_RXHASH |
11786 NETIF_F_RXCSUM |
11787 0;
11788
11789 if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
11790 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
11791
11792 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
11793
11794 netdev->hw_enc_features |= hw_enc_features;
11795
11796 /* record features VLANs can make use of */
11797 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
11798
11799 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
11800 netdev->hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
11801
11802 hw_features = hw_enc_features |
11803 NETIF_F_HW_VLAN_CTAG_TX |
11804 NETIF_F_HW_VLAN_CTAG_RX;
11805
11806 netdev->hw_features |= hw_features;
11807
11808 netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
11809 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
11810
11811 if (vsi->type == I40E_VSI_MAIN) {
11812 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
11813 ether_addr_copy(mac_addr, hw->mac.perm_addr);
11814 /* The following steps are necessary for two reasons. First,
11815 * some older NVM configurations load a default MAC-VLAN
11816 * filter that will accept any tagged packet, and we want to
11817 * replace this with a normal filter. Additionally, it is
11818 * possible our MAC address was provided by the platform using
11819 * Open Firmware or similar.
11820 *
11821 * Thus, we need to remove the default filter and install one
11822 * specific to the MAC address.
11823 */
11824 i40e_rm_default_mac_filter(vsi, mac_addr);
11825 spin_lock_bh(&vsi->mac_filter_hash_lock);
11826 i40e_add_mac_filter(vsi, mac_addr);
11827 spin_unlock_bh(&vsi->mac_filter_hash_lock);
11828 } else {
11829 /* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
11830 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
11831 * the end, which is 4 bytes long, so force truncation of the
11832 * original name by IFNAMSIZ - 4
11833 */
11834 snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
11835 IFNAMSIZ - 4,
11836 pf->vsi[pf->lan_vsi]->netdev->name);
11837 random_ether_addr(mac_addr);
11838
11839 spin_lock_bh(&vsi->mac_filter_hash_lock);
11840 i40e_add_mac_filter(vsi, mac_addr);
11841 spin_unlock_bh(&vsi->mac_filter_hash_lock);
11842 }
11843
11844 /* Add the broadcast filter so that we initially will receive
11845 * broadcast packets. Note that when a new VLAN is first added the
11846 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
11847 * specific filters as part of transitioning into "vlan" operation.
11848 * When more VLANs are added, the driver will copy each existing MAC
11849 * filter and add it for the new VLAN.
11850 *
11851 * Broadcast filters are handled specially by
11852 * i40e_sync_filters_subtask, as the driver must to set the broadcast
11853 * promiscuous bit instead of adding this directly as a MAC/VLAN
11854 * filter. The subtask will update the correct broadcast promiscuous
11855 * bits as VLANs become active or inactive.
11856 */
11857 eth_broadcast_addr(broadcast);
11858 spin_lock_bh(&vsi->mac_filter_hash_lock);
11859 i40e_add_mac_filter(vsi, broadcast);
11860 spin_unlock_bh(&vsi->mac_filter_hash_lock);
11861
11862 ether_addr_copy(netdev->dev_addr, mac_addr);
11863 ether_addr_copy(netdev->perm_addr, mac_addr);
11864
11865 netdev->priv_flags |= IFF_UNICAST_FLT;
11866 netdev->priv_flags |= IFF_SUPP_NOFCS;
11867 /* Setup netdev TC information */
11868 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
11869
11870 netdev->netdev_ops = &i40e_netdev_ops;
11871 netdev->watchdog_timeo = 5 * HZ;
11872 i40e_set_ethtool_ops(netdev);
11873
11874 /* MTU range: 68 - 9706 */
11875 netdev->min_mtu = ETH_MIN_MTU;
11876 netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
11877
11878 return 0;
11879 }
11880
11881 /**
11882 * i40e_vsi_delete - Delete a VSI from the switch
11883 * @vsi: the VSI being removed
11884 *
11885 * Returns 0 on success, negative value on failure
11886 **/
11887 static void i40e_vsi_delete(struct i40e_vsi *vsi)
11888 {
11889 /* remove default VSI is not allowed */
11890 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
11891 return;
11892
11893 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
11894 }
11895
11896 /**
11897 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
11898 * @vsi: the VSI being queried
11899 *
11900 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
11901 **/
11902 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
11903 {
11904 struct i40e_veb *veb;
11905 struct i40e_pf *pf = vsi->back;
11906
11907 /* Uplink is not a bridge so default to VEB */
11908 if (vsi->veb_idx == I40E_NO_VEB)
11909 return 1;
11910
11911 veb = pf->veb[vsi->veb_idx];
11912 if (!veb) {
11913 dev_info(&pf->pdev->dev,
11914 "There is no veb associated with the bridge\n");
11915 return -ENOENT;
11916 }
11917
11918 /* Uplink is a bridge in VEPA mode */
11919 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
11920 return 0;
11921 } else {
11922 /* Uplink is a bridge in VEB mode */
11923 return 1;
11924 }
11925
11926 /* VEPA is now default bridge, so return 0 */
11927 return 0;
11928 }
11929
11930 /**
11931 * i40e_add_vsi - Add a VSI to the switch
11932 * @vsi: the VSI being configured
11933 *
11934 * This initializes a VSI context depending on the VSI type to be added and
11935 * passes it down to the add_vsi aq command.
11936 **/
11937 static int i40e_add_vsi(struct i40e_vsi *vsi)
11938 {
11939 int ret = -ENODEV;
11940 struct i40e_pf *pf = vsi->back;
11941 struct i40e_hw *hw = &pf->hw;
11942 struct i40e_vsi_context ctxt;
11943 struct i40e_mac_filter *f;
11944 struct hlist_node *h;
11945 int bkt;
11946
11947 u8 enabled_tc = 0x1; /* TC0 enabled */
11948 int f_count = 0;
11949
11950 memset(&ctxt, 0, sizeof(ctxt));
11951 switch (vsi->type) {
11952 case I40E_VSI_MAIN:
11953 /* The PF's main VSI is already setup as part of the
11954 * device initialization, so we'll not bother with
11955 * the add_vsi call, but we will retrieve the current
11956 * VSI context.
11957 */
11958 ctxt.seid = pf->main_vsi_seid;
11959 ctxt.pf_num = pf->hw.pf_id;
11960 ctxt.vf_num = 0;
11961 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
11962 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
11963 if (ret) {
11964 dev_info(&pf->pdev->dev,
11965 "couldn't get PF vsi config, err %s aq_err %s\n",
11966 i40e_stat_str(&pf->hw, ret),
11967 i40e_aq_str(&pf->hw,
11968 pf->hw.aq.asq_last_status));
11969 return -ENOENT;
11970 }
11971 vsi->info = ctxt.info;
11972 vsi->info.valid_sections = 0;
11973
11974 vsi->seid = ctxt.seid;
11975 vsi->id = ctxt.vsi_number;
11976
11977 enabled_tc = i40e_pf_get_tc_map(pf);
11978
11979 /* Source pruning is enabled by default, so the flag is
11980 * negative logic - if it's set, we need to fiddle with
11981 * the VSI to disable source pruning.
11982 */
11983 if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
11984 memset(&ctxt, 0, sizeof(ctxt));
11985 ctxt.seid = pf->main_vsi_seid;
11986 ctxt.pf_num = pf->hw.pf_id;
11987 ctxt.vf_num = 0;
11988 ctxt.info.valid_sections |=
11989 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
11990 ctxt.info.switch_id =
11991 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
11992 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
11993 if (ret) {
11994 dev_info(&pf->pdev->dev,
11995 "update vsi failed, err %s aq_err %s\n",
11996 i40e_stat_str(&pf->hw, ret),
11997 i40e_aq_str(&pf->hw,
11998 pf->hw.aq.asq_last_status));
11999 ret = -ENOENT;
12000 goto err;
12001 }
12002 }
12003
12004 /* MFP mode setup queue map and update VSI */
12005 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
12006 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
12007 memset(&ctxt, 0, sizeof(ctxt));
12008 ctxt.seid = pf->main_vsi_seid;
12009 ctxt.pf_num = pf->hw.pf_id;
12010 ctxt.vf_num = 0;
12011 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
12012 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
12013 if (ret) {
12014 dev_info(&pf->pdev->dev,
12015 "update vsi failed, err %s aq_err %s\n",
12016 i40e_stat_str(&pf->hw, ret),
12017 i40e_aq_str(&pf->hw,
12018 pf->hw.aq.asq_last_status));
12019 ret = -ENOENT;
12020 goto err;
12021 }
12022 /* update the local VSI info queue map */
12023 i40e_vsi_update_queue_map(vsi, &ctxt);
12024 vsi->info.valid_sections = 0;
12025 } else {
12026 /* Default/Main VSI is only enabled for TC0
12027 * reconfigure it to enable all TCs that are
12028 * available on the port in SFP mode.
12029 * For MFP case the iSCSI PF would use this
12030 * flow to enable LAN+iSCSI TC.
12031 */
12032 ret = i40e_vsi_config_tc(vsi, enabled_tc);
12033 if (ret) {
12034 /* Single TC condition is not fatal,
12035 * message and continue
12036 */
12037 dev_info(&pf->pdev->dev,
12038 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
12039 enabled_tc,
12040 i40e_stat_str(&pf->hw, ret),
12041 i40e_aq_str(&pf->hw,
12042 pf->hw.aq.asq_last_status));
12043 }
12044 }
12045 break;
12046
12047 case I40E_VSI_FDIR:
12048 ctxt.pf_num = hw->pf_id;
12049 ctxt.vf_num = 0;
12050 ctxt.uplink_seid = vsi->uplink_seid;
12051 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
12052 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
12053 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
12054 (i40e_is_vsi_uplink_mode_veb(vsi))) {
12055 ctxt.info.valid_sections |=
12056 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
12057 ctxt.info.switch_id =
12058 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
12059 }
12060 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
12061 break;
12062
12063 case I40E_VSI_VMDQ2:
12064 ctxt.pf_num = hw->pf_id;
12065 ctxt.vf_num = 0;
12066 ctxt.uplink_seid = vsi->uplink_seid;
12067 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
12068 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
12069
12070 /* This VSI is connected to VEB so the switch_id
12071 * should be set to zero by default.
12072 */
12073 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
12074 ctxt.info.valid_sections |=
12075 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
12076 ctxt.info.switch_id =
12077 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
12078 }
12079
12080 /* Setup the VSI tx/rx queue map for TC0 only for now */
12081 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
12082 break;
12083
12084 case I40E_VSI_SRIOV:
12085 ctxt.pf_num = hw->pf_id;
12086 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
12087 ctxt.uplink_seid = vsi->uplink_seid;
12088 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
12089 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
12090
12091 /* This VSI is connected to VEB so the switch_id
12092 * should be set to zero by default.
12093 */
12094 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
12095 ctxt.info.valid_sections |=
12096 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
12097 ctxt.info.switch_id =
12098 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
12099 }
12100
12101 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
12102 ctxt.info.valid_sections |=
12103 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
12104 ctxt.info.queueing_opt_flags |=
12105 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
12106 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
12107 }
12108
12109 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
12110 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
12111 if (pf->vf[vsi->vf_id].spoofchk) {
12112 ctxt.info.valid_sections |=
12113 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
12114 ctxt.info.sec_flags |=
12115 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
12116 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
12117 }
12118 /* Setup the VSI tx/rx queue map for TC0 only for now */
12119 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
12120 break;
12121
12122 case I40E_VSI_IWARP:
12123 /* send down message to iWARP */
12124 break;
12125
12126 default:
12127 return -ENODEV;
12128 }
12129
12130 if (vsi->type != I40E_VSI_MAIN) {
12131 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
12132 if (ret) {
12133 dev_info(&vsi->back->pdev->dev,
12134 "add vsi failed, err %s aq_err %s\n",
12135 i40e_stat_str(&pf->hw, ret),
12136 i40e_aq_str(&pf->hw,
12137 pf->hw.aq.asq_last_status));
12138 ret = -ENOENT;
12139 goto err;
12140 }
12141 vsi->info = ctxt.info;
12142 vsi->info.valid_sections = 0;
12143 vsi->seid = ctxt.seid;
12144 vsi->id = ctxt.vsi_number;
12145 }
12146
12147 vsi->active_filters = 0;
12148 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
12149 spin_lock_bh(&vsi->mac_filter_hash_lock);
12150 /* If macvlan filters already exist, force them to get loaded */
12151 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
12152 f->state = I40E_FILTER_NEW;
12153 f_count++;
12154 }
12155 spin_unlock_bh(&vsi->mac_filter_hash_lock);
12156
12157 if (f_count) {
12158 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
12159 pf->flags |= I40E_FLAG_FILTER_SYNC;
12160 }
12161
12162 /* Update VSI BW information */
12163 ret = i40e_vsi_get_bw_info(vsi);
12164 if (ret) {
12165 dev_info(&pf->pdev->dev,
12166 "couldn't get vsi bw info, err %s aq_err %s\n",
12167 i40e_stat_str(&pf->hw, ret),
12168 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
12169 /* VSI is already added so not tearing that up */
12170 ret = 0;
12171 }
12172
12173 err:
12174 return ret;
12175 }
12176
12177 /**
12178 * i40e_vsi_release - Delete a VSI and free its resources
12179 * @vsi: the VSI being removed
12180 *
12181 * Returns 0 on success or < 0 on error
12182 **/
12183 int i40e_vsi_release(struct i40e_vsi *vsi)
12184 {
12185 struct i40e_mac_filter *f;
12186 struct hlist_node *h;
12187 struct i40e_veb *veb = NULL;
12188 struct i40e_pf *pf;
12189 u16 uplink_seid;
12190 int i, n, bkt;
12191
12192 pf = vsi->back;
12193
12194 /* release of a VEB-owner or last VSI is not allowed */
12195 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
12196 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
12197 vsi->seid, vsi->uplink_seid);
12198 return -ENODEV;
12199 }
12200 if (vsi == pf->vsi[pf->lan_vsi] &&
12201 !test_bit(__I40E_DOWN, pf->state)) {
12202 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
12203 return -ENODEV;
12204 }
12205
12206 uplink_seid = vsi->uplink_seid;
12207 if (vsi->type != I40E_VSI_SRIOV) {
12208 if (vsi->netdev_registered) {
12209 vsi->netdev_registered = false;
12210 if (vsi->netdev) {
12211 /* results in a call to i40e_close() */
12212 unregister_netdev(vsi->netdev);
12213 }
12214 } else {
12215 i40e_vsi_close(vsi);
12216 }
12217 i40e_vsi_disable_irq(vsi);
12218 }
12219
12220 spin_lock_bh(&vsi->mac_filter_hash_lock);
12221
12222 /* clear the sync flag on all filters */
12223 if (vsi->netdev) {
12224 __dev_uc_unsync(vsi->netdev, NULL);
12225 __dev_mc_unsync(vsi->netdev, NULL);
12226 }
12227
12228 /* make sure any remaining filters are marked for deletion */
12229 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
12230 __i40e_del_filter(vsi, f);
12231
12232 spin_unlock_bh(&vsi->mac_filter_hash_lock);
12233
12234 i40e_sync_vsi_filters(vsi);
12235
12236 i40e_vsi_delete(vsi);
12237 i40e_vsi_free_q_vectors(vsi);
12238 if (vsi->netdev) {
12239 free_netdev(vsi->netdev);
12240 vsi->netdev = NULL;
12241 }
12242 i40e_vsi_clear_rings(vsi);
12243 i40e_vsi_clear(vsi);
12244
12245 /* If this was the last thing on the VEB, except for the
12246 * controlling VSI, remove the VEB, which puts the controlling
12247 * VSI onto the next level down in the switch.
12248 *
12249 * Well, okay, there's one more exception here: don't remove
12250 * the orphan VEBs yet. We'll wait for an explicit remove request
12251 * from up the network stack.
12252 */
12253 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
12254 if (pf->vsi[i] &&
12255 pf->vsi[i]->uplink_seid == uplink_seid &&
12256 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
12257 n++; /* count the VSIs */
12258 }
12259 }
12260 for (i = 0; i < I40E_MAX_VEB; i++) {
12261 if (!pf->veb[i])
12262 continue;
12263 if (pf->veb[i]->uplink_seid == uplink_seid)
12264 n++; /* count the VEBs */
12265 if (pf->veb[i]->seid == uplink_seid)
12266 veb = pf->veb[i];
12267 }
12268 if (n == 0 && veb && veb->uplink_seid != 0)
12269 i40e_veb_release(veb);
12270
12271 return 0;
12272 }
12273
12274 /**
12275 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
12276 * @vsi: ptr to the VSI
12277 *
12278 * This should only be called after i40e_vsi_mem_alloc() which allocates the
12279 * corresponding SW VSI structure and initializes num_queue_pairs for the
12280 * newly allocated VSI.
12281 *
12282 * Returns 0 on success or negative on failure
12283 **/
12284 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
12285 {
12286 int ret = -ENOENT;
12287 struct i40e_pf *pf = vsi->back;
12288
12289 if (vsi->q_vectors[0]) {
12290 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
12291 vsi->seid);
12292 return -EEXIST;
12293 }
12294
12295 if (vsi->base_vector) {
12296 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
12297 vsi->seid, vsi->base_vector);
12298 return -EEXIST;
12299 }
12300
12301 ret = i40e_vsi_alloc_q_vectors(vsi);
12302 if (ret) {
12303 dev_info(&pf->pdev->dev,
12304 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
12305 vsi->num_q_vectors, vsi->seid, ret);
12306 vsi->num_q_vectors = 0;
12307 goto vector_setup_out;
12308 }
12309
12310 /* In Legacy mode, we do not have to get any other vector since we
12311 * piggyback on the misc/ICR0 for queue interrupts.
12312 */
12313 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
12314 return ret;
12315 if (vsi->num_q_vectors)
12316 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
12317 vsi->num_q_vectors, vsi->idx);
12318 if (vsi->base_vector < 0) {
12319 dev_info(&pf->pdev->dev,
12320 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
12321 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
12322 i40e_vsi_free_q_vectors(vsi);
12323 ret = -ENOENT;
12324 goto vector_setup_out;
12325 }
12326
12327 vector_setup_out:
12328 return ret;
12329 }
12330
12331 /**
12332 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
12333 * @vsi: pointer to the vsi.
12334 *
12335 * This re-allocates a vsi's queue resources.
12336 *
12337 * Returns pointer to the successfully allocated and configured VSI sw struct
12338 * on success, otherwise returns NULL on failure.
12339 **/
12340 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
12341 {
12342 u16 alloc_queue_pairs;
12343 struct i40e_pf *pf;
12344 u8 enabled_tc;
12345 int ret;
12346
12347 if (!vsi)
12348 return NULL;
12349
12350 pf = vsi->back;
12351
12352 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
12353 i40e_vsi_clear_rings(vsi);
12354
12355 i40e_vsi_free_arrays(vsi, false);
12356 i40e_set_num_rings_in_vsi(vsi);
12357 ret = i40e_vsi_alloc_arrays(vsi, false);
12358 if (ret)
12359 goto err_vsi;
12360
12361 alloc_queue_pairs = vsi->alloc_queue_pairs *
12362 (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
12363
12364 ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
12365 if (ret < 0) {
12366 dev_info(&pf->pdev->dev,
12367 "failed to get tracking for %d queues for VSI %d err %d\n",
12368 alloc_queue_pairs, vsi->seid, ret);
12369 goto err_vsi;
12370 }
12371 vsi->base_queue = ret;
12372
12373 /* Update the FW view of the VSI. Force a reset of TC and queue
12374 * layout configurations.
12375 */
12376 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
12377 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
12378 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
12379 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
12380 if (vsi->type == I40E_VSI_MAIN)
12381 i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
12382
12383 /* assign it some queues */
12384 ret = i40e_alloc_rings(vsi);
12385 if (ret)
12386 goto err_rings;
12387
12388 /* map all of the rings to the q_vectors */
12389 i40e_vsi_map_rings_to_vectors(vsi);
12390 return vsi;
12391
12392 err_rings:
12393 i40e_vsi_free_q_vectors(vsi);
12394 if (vsi->netdev_registered) {
12395 vsi->netdev_registered = false;
12396 unregister_netdev(vsi->netdev);
12397 free_netdev(vsi->netdev);
12398 vsi->netdev = NULL;
12399 }
12400 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
12401 err_vsi:
12402 i40e_vsi_clear(vsi);
12403 return NULL;
12404 }
12405
12406 /**
12407 * i40e_vsi_setup - Set up a VSI by a given type
12408 * @pf: board private structure
12409 * @type: VSI type
12410 * @uplink_seid: the switch element to link to
12411 * @param1: usage depends upon VSI type. For VF types, indicates VF id
12412 *
12413 * This allocates the sw VSI structure and its queue resources, then add a VSI
12414 * to the identified VEB.
12415 *
12416 * Returns pointer to the successfully allocated and configure VSI sw struct on
12417 * success, otherwise returns NULL on failure.
12418 **/
12419 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
12420 u16 uplink_seid, u32 param1)
12421 {
12422 struct i40e_vsi *vsi = NULL;
12423 struct i40e_veb *veb = NULL;
12424 u16 alloc_queue_pairs;
12425 int ret, i;
12426 int v_idx;
12427
12428 /* The requested uplink_seid must be either
12429 * - the PF's port seid
12430 * no VEB is needed because this is the PF
12431 * or this is a Flow Director special case VSI
12432 * - seid of an existing VEB
12433 * - seid of a VSI that owns an existing VEB
12434 * - seid of a VSI that doesn't own a VEB
12435 * a new VEB is created and the VSI becomes the owner
12436 * - seid of the PF VSI, which is what creates the first VEB
12437 * this is a special case of the previous
12438 *
12439 * Find which uplink_seid we were given and create a new VEB if needed
12440 */
12441 for (i = 0; i < I40E_MAX_VEB; i++) {
12442 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
12443 veb = pf->veb[i];
12444 break;
12445 }
12446 }
12447
12448 if (!veb && uplink_seid != pf->mac_seid) {
12449
12450 for (i = 0; i < pf->num_alloc_vsi; i++) {
12451 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
12452 vsi = pf->vsi[i];
12453 break;
12454 }
12455 }
12456 if (!vsi) {
12457 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
12458 uplink_seid);
12459 return NULL;
12460 }
12461
12462 if (vsi->uplink_seid == pf->mac_seid)
12463 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
12464 vsi->tc_config.enabled_tc);
12465 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
12466 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
12467 vsi->tc_config.enabled_tc);
12468 if (veb) {
12469 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
12470 dev_info(&vsi->back->pdev->dev,
12471 "New VSI creation error, uplink seid of LAN VSI expected.\n");
12472 return NULL;
12473 }
12474 /* We come up by default in VEPA mode if SRIOV is not
12475 * already enabled, in which case we can't force VEPA
12476 * mode.
12477 */
12478 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
12479 veb->bridge_mode = BRIDGE_MODE_VEPA;
12480 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
12481 }
12482 i40e_config_bridge_mode(veb);
12483 }
12484 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12485 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12486 veb = pf->veb[i];
12487 }
12488 if (!veb) {
12489 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
12490 return NULL;
12491 }
12492
12493 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
12494 uplink_seid = veb->seid;
12495 }
12496
12497 /* get vsi sw struct */
12498 v_idx = i40e_vsi_mem_alloc(pf, type);
12499 if (v_idx < 0)
12500 goto err_alloc;
12501 vsi = pf->vsi[v_idx];
12502 if (!vsi)
12503 goto err_alloc;
12504 vsi->type = type;
12505 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
12506
12507 if (type == I40E_VSI_MAIN)
12508 pf->lan_vsi = v_idx;
12509 else if (type == I40E_VSI_SRIOV)
12510 vsi->vf_id = param1;
12511 /* assign it some queues */
12512 alloc_queue_pairs = vsi->alloc_queue_pairs *
12513 (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
12514
12515 ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
12516 if (ret < 0) {
12517 dev_info(&pf->pdev->dev,
12518 "failed to get tracking for %d queues for VSI %d err=%d\n",
12519 alloc_queue_pairs, vsi->seid, ret);
12520 goto err_vsi;
12521 }
12522 vsi->base_queue = ret;
12523
12524 /* get a VSI from the hardware */
12525 vsi->uplink_seid = uplink_seid;
12526 ret = i40e_add_vsi(vsi);
12527 if (ret)
12528 goto err_vsi;
12529
12530 switch (vsi->type) {
12531 /* setup the netdev if needed */
12532 case I40E_VSI_MAIN:
12533 case I40E_VSI_VMDQ2:
12534 ret = i40e_config_netdev(vsi);
12535 if (ret)
12536 goto err_netdev;
12537 ret = register_netdev(vsi->netdev);
12538 if (ret)
12539 goto err_netdev;
12540 vsi->netdev_registered = true;
12541 netif_carrier_off(vsi->netdev);
12542 #ifdef CONFIG_I40E_DCB
12543 /* Setup DCB netlink interface */
12544 i40e_dcbnl_setup(vsi);
12545 #endif /* CONFIG_I40E_DCB */
12546 /* fall through */
12547
12548 case I40E_VSI_FDIR:
12549 /* set up vectors and rings if needed */
12550 ret = i40e_vsi_setup_vectors(vsi);
12551 if (ret)
12552 goto err_msix;
12553
12554 ret = i40e_alloc_rings(vsi);
12555 if (ret)
12556 goto err_rings;
12557
12558 /* map all of the rings to the q_vectors */
12559 i40e_vsi_map_rings_to_vectors(vsi);
12560
12561 i40e_vsi_reset_stats(vsi);
12562 break;
12563
12564 default:
12565 /* no netdev or rings for the other VSI types */
12566 break;
12567 }
12568
12569 if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
12570 (vsi->type == I40E_VSI_VMDQ2)) {
12571 ret = i40e_vsi_config_rss(vsi);
12572 }
12573 return vsi;
12574
12575 err_rings:
12576 i40e_vsi_free_q_vectors(vsi);
12577 err_msix:
12578 if (vsi->netdev_registered) {
12579 vsi->netdev_registered = false;
12580 unregister_netdev(vsi->netdev);
12581 free_netdev(vsi->netdev);
12582 vsi->netdev = NULL;
12583 }
12584 err_netdev:
12585 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
12586 err_vsi:
12587 i40e_vsi_clear(vsi);
12588 err_alloc:
12589 return NULL;
12590 }
12591
12592 /**
12593 * i40e_veb_get_bw_info - Query VEB BW information
12594 * @veb: the veb to query
12595 *
12596 * Query the Tx scheduler BW configuration data for given VEB
12597 **/
12598 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
12599 {
12600 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
12601 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
12602 struct i40e_pf *pf = veb->pf;
12603 struct i40e_hw *hw = &pf->hw;
12604 u32 tc_bw_max;
12605 int ret = 0;
12606 int i;
12607
12608 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
12609 &bw_data, NULL);
12610 if (ret) {
12611 dev_info(&pf->pdev->dev,
12612 "query veb bw config failed, err %s aq_err %s\n",
12613 i40e_stat_str(&pf->hw, ret),
12614 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
12615 goto out;
12616 }
12617
12618 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
12619 &ets_data, NULL);
12620 if (ret) {
12621 dev_info(&pf->pdev->dev,
12622 "query veb bw ets config failed, err %s aq_err %s\n",
12623 i40e_stat_str(&pf->hw, ret),
12624 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
12625 goto out;
12626 }
12627
12628 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
12629 veb->bw_max_quanta = ets_data.tc_bw_max;
12630 veb->is_abs_credits = bw_data.absolute_credits_enable;
12631 veb->enabled_tc = ets_data.tc_valid_bits;
12632 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
12633 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
12634 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
12635 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
12636 veb->bw_tc_limit_credits[i] =
12637 le16_to_cpu(bw_data.tc_bw_limits[i]);
12638 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
12639 }
12640
12641 out:
12642 return ret;
12643 }
12644
12645 /**
12646 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
12647 * @pf: board private structure
12648 *
12649 * On error: returns error code (negative)
12650 * On success: returns vsi index in PF (positive)
12651 **/
12652 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
12653 {
12654 int ret = -ENOENT;
12655 struct i40e_veb *veb;
12656 int i;
12657
12658 /* Need to protect the allocation of switch elements at the PF level */
12659 mutex_lock(&pf->switch_mutex);
12660
12661 /* VEB list may be fragmented if VEB creation/destruction has
12662 * been happening. We can afford to do a quick scan to look
12663 * for any free slots in the list.
12664 *
12665 * find next empty veb slot, looping back around if necessary
12666 */
12667 i = 0;
12668 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
12669 i++;
12670 if (i >= I40E_MAX_VEB) {
12671 ret = -ENOMEM;
12672 goto err_alloc_veb; /* out of VEB slots! */
12673 }
12674
12675 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
12676 if (!veb) {
12677 ret = -ENOMEM;
12678 goto err_alloc_veb;
12679 }
12680 veb->pf = pf;
12681 veb->idx = i;
12682 veb->enabled_tc = 1;
12683
12684 pf->veb[i] = veb;
12685 ret = i;
12686 err_alloc_veb:
12687 mutex_unlock(&pf->switch_mutex);
12688 return ret;
12689 }
12690
12691 /**
12692 * i40e_switch_branch_release - Delete a branch of the switch tree
12693 * @branch: where to start deleting
12694 *
12695 * This uses recursion to find the tips of the branch to be
12696 * removed, deleting until we get back to and can delete this VEB.
12697 **/
12698 static void i40e_switch_branch_release(struct i40e_veb *branch)
12699 {
12700 struct i40e_pf *pf = branch->pf;
12701 u16 branch_seid = branch->seid;
12702 u16 veb_idx = branch->idx;
12703 int i;
12704
12705 /* release any VEBs on this VEB - RECURSION */
12706 for (i = 0; i < I40E_MAX_VEB; i++) {
12707 if (!pf->veb[i])
12708 continue;
12709 if (pf->veb[i]->uplink_seid == branch->seid)
12710 i40e_switch_branch_release(pf->veb[i]);
12711 }
12712
12713 /* Release the VSIs on this VEB, but not the owner VSI.
12714 *
12715 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
12716 * the VEB itself, so don't use (*branch) after this loop.
12717 */
12718 for (i = 0; i < pf->num_alloc_vsi; i++) {
12719 if (!pf->vsi[i])
12720 continue;
12721 if (pf->vsi[i]->uplink_seid == branch_seid &&
12722 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
12723 i40e_vsi_release(pf->vsi[i]);
12724 }
12725 }
12726
12727 /* There's one corner case where the VEB might not have been
12728 * removed, so double check it here and remove it if needed.
12729 * This case happens if the veb was created from the debugfs
12730 * commands and no VSIs were added to it.
12731 */
12732 if (pf->veb[veb_idx])
12733 i40e_veb_release(pf->veb[veb_idx]);
12734 }
12735
12736 /**
12737 * i40e_veb_clear - remove veb struct
12738 * @veb: the veb to remove
12739 **/
12740 static void i40e_veb_clear(struct i40e_veb *veb)
12741 {
12742 if (!veb)
12743 return;
12744
12745 if (veb->pf) {
12746 struct i40e_pf *pf = veb->pf;
12747
12748 mutex_lock(&pf->switch_mutex);
12749 if (pf->veb[veb->idx] == veb)
12750 pf->veb[veb->idx] = NULL;
12751 mutex_unlock(&pf->switch_mutex);
12752 }
12753
12754 kfree(veb);
12755 }
12756
12757 /**
12758 * i40e_veb_release - Delete a VEB and free its resources
12759 * @veb: the VEB being removed
12760 **/
12761 void i40e_veb_release(struct i40e_veb *veb)
12762 {
12763 struct i40e_vsi *vsi = NULL;
12764 struct i40e_pf *pf;
12765 int i, n = 0;
12766
12767 pf = veb->pf;
12768
12769 /* find the remaining VSI and check for extras */
12770 for (i = 0; i < pf->num_alloc_vsi; i++) {
12771 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
12772 n++;
12773 vsi = pf->vsi[i];
12774 }
12775 }
12776 if (n != 1) {
12777 dev_info(&pf->pdev->dev,
12778 "can't remove VEB %d with %d VSIs left\n",
12779 veb->seid, n);
12780 return;
12781 }
12782
12783 /* move the remaining VSI to uplink veb */
12784 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
12785 if (veb->uplink_seid) {
12786 vsi->uplink_seid = veb->uplink_seid;
12787 if (veb->uplink_seid == pf->mac_seid)
12788 vsi->veb_idx = I40E_NO_VEB;
12789 else
12790 vsi->veb_idx = veb->veb_idx;
12791 } else {
12792 /* floating VEB */
12793 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
12794 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
12795 }
12796
12797 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
12798 i40e_veb_clear(veb);
12799 }
12800
12801 /**
12802 * i40e_add_veb - create the VEB in the switch
12803 * @veb: the VEB to be instantiated
12804 * @vsi: the controlling VSI
12805 **/
12806 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
12807 {
12808 struct i40e_pf *pf = veb->pf;
12809 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
12810 int ret;
12811
12812 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
12813 veb->enabled_tc, false,
12814 &veb->seid, enable_stats, NULL);
12815
12816 /* get a VEB from the hardware */
12817 if (ret) {
12818 dev_info(&pf->pdev->dev,
12819 "couldn't add VEB, err %s aq_err %s\n",
12820 i40e_stat_str(&pf->hw, ret),
12821 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
12822 return -EPERM;
12823 }
12824
12825 /* get statistics counter */
12826 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
12827 &veb->stats_idx, NULL, NULL, NULL);
12828 if (ret) {
12829 dev_info(&pf->pdev->dev,
12830 "couldn't get VEB statistics idx, err %s aq_err %s\n",
12831 i40e_stat_str(&pf->hw, ret),
12832 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
12833 return -EPERM;
12834 }
12835 ret = i40e_veb_get_bw_info(veb);
12836 if (ret) {
12837 dev_info(&pf->pdev->dev,
12838 "couldn't get VEB bw info, err %s aq_err %s\n",
12839 i40e_stat_str(&pf->hw, ret),
12840 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
12841 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
12842 return -ENOENT;
12843 }
12844
12845 vsi->uplink_seid = veb->seid;
12846 vsi->veb_idx = veb->idx;
12847 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
12848
12849 return 0;
12850 }
12851
12852 /**
12853 * i40e_veb_setup - Set up a VEB
12854 * @pf: board private structure
12855 * @flags: VEB setup flags
12856 * @uplink_seid: the switch element to link to
12857 * @vsi_seid: the initial VSI seid
12858 * @enabled_tc: Enabled TC bit-map
12859 *
12860 * This allocates the sw VEB structure and links it into the switch
12861 * It is possible and legal for this to be a duplicate of an already
12862 * existing VEB. It is also possible for both uplink and vsi seids
12863 * to be zero, in order to create a floating VEB.
12864 *
12865 * Returns pointer to the successfully allocated VEB sw struct on
12866 * success, otherwise returns NULL on failure.
12867 **/
12868 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
12869 u16 uplink_seid, u16 vsi_seid,
12870 u8 enabled_tc)
12871 {
12872 struct i40e_veb *veb, *uplink_veb = NULL;
12873 int vsi_idx, veb_idx;
12874 int ret;
12875
12876 /* if one seid is 0, the other must be 0 to create a floating relay */
12877 if ((uplink_seid == 0 || vsi_seid == 0) &&
12878 (uplink_seid + vsi_seid != 0)) {
12879 dev_info(&pf->pdev->dev,
12880 "one, not both seid's are 0: uplink=%d vsi=%d\n",
12881 uplink_seid, vsi_seid);
12882 return NULL;
12883 }
12884
12885 /* make sure there is such a vsi and uplink */
12886 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
12887 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
12888 break;
12889 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
12890 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
12891 vsi_seid);
12892 return NULL;
12893 }
12894
12895 if (uplink_seid && uplink_seid != pf->mac_seid) {
12896 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
12897 if (pf->veb[veb_idx] &&
12898 pf->veb[veb_idx]->seid == uplink_seid) {
12899 uplink_veb = pf->veb[veb_idx];
12900 break;
12901 }
12902 }
12903 if (!uplink_veb) {
12904 dev_info(&pf->pdev->dev,
12905 "uplink seid %d not found\n", uplink_seid);
12906 return NULL;
12907 }
12908 }
12909
12910 /* get veb sw struct */
12911 veb_idx = i40e_veb_mem_alloc(pf);
12912 if (veb_idx < 0)
12913 goto err_alloc;
12914 veb = pf->veb[veb_idx];
12915 veb->flags = flags;
12916 veb->uplink_seid = uplink_seid;
12917 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
12918 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
12919
12920 /* create the VEB in the switch */
12921 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
12922 if (ret)
12923 goto err_veb;
12924 if (vsi_idx == pf->lan_vsi)
12925 pf->lan_veb = veb->idx;
12926
12927 return veb;
12928
12929 err_veb:
12930 i40e_veb_clear(veb);
12931 err_alloc:
12932 return NULL;
12933 }
12934
12935 /**
12936 * i40e_setup_pf_switch_element - set PF vars based on switch type
12937 * @pf: board private structure
12938 * @ele: element we are building info from
12939 * @num_reported: total number of elements
12940 * @printconfig: should we print the contents
12941 *
12942 * helper function to assist in extracting a few useful SEID values.
12943 **/
12944 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
12945 struct i40e_aqc_switch_config_element_resp *ele,
12946 u16 num_reported, bool printconfig)
12947 {
12948 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
12949 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
12950 u8 element_type = ele->element_type;
12951 u16 seid = le16_to_cpu(ele->seid);
12952
12953 if (printconfig)
12954 dev_info(&pf->pdev->dev,
12955 "type=%d seid=%d uplink=%d downlink=%d\n",
12956 element_type, seid, uplink_seid, downlink_seid);
12957
12958 switch (element_type) {
12959 case I40E_SWITCH_ELEMENT_TYPE_MAC:
12960 pf->mac_seid = seid;
12961 break;
12962 case I40E_SWITCH_ELEMENT_TYPE_VEB:
12963 /* Main VEB? */
12964 if (uplink_seid != pf->mac_seid)
12965 break;
12966 if (pf->lan_veb == I40E_NO_VEB) {
12967 int v;
12968
12969 /* find existing or else empty VEB */
12970 for (v = 0; v < I40E_MAX_VEB; v++) {
12971 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
12972 pf->lan_veb = v;
12973 break;
12974 }
12975 }
12976 if (pf->lan_veb == I40E_NO_VEB) {
12977 v = i40e_veb_mem_alloc(pf);
12978 if (v < 0)
12979 break;
12980 pf->lan_veb = v;
12981 }
12982 }
12983
12984 pf->veb[pf->lan_veb]->seid = seid;
12985 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
12986 pf->veb[pf->lan_veb]->pf = pf;
12987 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
12988 break;
12989 case I40E_SWITCH_ELEMENT_TYPE_VSI:
12990 if (num_reported != 1)
12991 break;
12992 /* This is immediately after a reset so we can assume this is
12993 * the PF's VSI
12994 */
12995 pf->mac_seid = uplink_seid;
12996 pf->pf_seid = downlink_seid;
12997 pf->main_vsi_seid = seid;
12998 if (printconfig)
12999 dev_info(&pf->pdev->dev,
13000 "pf_seid=%d main_vsi_seid=%d\n",
13001 pf->pf_seid, pf->main_vsi_seid);
13002 break;
13003 case I40E_SWITCH_ELEMENT_TYPE_PF:
13004 case I40E_SWITCH_ELEMENT_TYPE_VF:
13005 case I40E_SWITCH_ELEMENT_TYPE_EMP:
13006 case I40E_SWITCH_ELEMENT_TYPE_BMC:
13007 case I40E_SWITCH_ELEMENT_TYPE_PE:
13008 case I40E_SWITCH_ELEMENT_TYPE_PA:
13009 /* ignore these for now */
13010 break;
13011 default:
13012 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
13013 element_type, seid);
13014 break;
13015 }
13016 }
13017
13018 /**
13019 * i40e_fetch_switch_configuration - Get switch config from firmware
13020 * @pf: board private structure
13021 * @printconfig: should we print the contents
13022 *
13023 * Get the current switch configuration from the device and
13024 * extract a few useful SEID values.
13025 **/
13026 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
13027 {
13028 struct i40e_aqc_get_switch_config_resp *sw_config;
13029 u16 next_seid = 0;
13030 int ret = 0;
13031 u8 *aq_buf;
13032 int i;
13033
13034 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
13035 if (!aq_buf)
13036 return -ENOMEM;
13037
13038 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
13039 do {
13040 u16 num_reported, num_total;
13041
13042 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
13043 I40E_AQ_LARGE_BUF,
13044 &next_seid, NULL);
13045 if (ret) {
13046 dev_info(&pf->pdev->dev,
13047 "get switch config failed err %s aq_err %s\n",
13048 i40e_stat_str(&pf->hw, ret),
13049 i40e_aq_str(&pf->hw,
13050 pf->hw.aq.asq_last_status));
13051 kfree(aq_buf);
13052 return -ENOENT;
13053 }
13054
13055 num_reported = le16_to_cpu(sw_config->header.num_reported);
13056 num_total = le16_to_cpu(sw_config->header.num_total);
13057
13058 if (printconfig)
13059 dev_info(&pf->pdev->dev,
13060 "header: %d reported %d total\n",
13061 num_reported, num_total);
13062
13063 for (i = 0; i < num_reported; i++) {
13064 struct i40e_aqc_switch_config_element_resp *ele =
13065 &sw_config->element[i];
13066
13067 i40e_setup_pf_switch_element(pf, ele, num_reported,
13068 printconfig);
13069 }
13070 } while (next_seid != 0);
13071
13072 kfree(aq_buf);
13073 return ret;
13074 }
13075
13076 /**
13077 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
13078 * @pf: board private structure
13079 * @reinit: if the Main VSI needs to re-initialized.
13080 *
13081 * Returns 0 on success, negative value on failure
13082 **/
13083 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
13084 {
13085 u16 flags = 0;
13086 int ret;
13087
13088 /* find out what's out there already */
13089 ret = i40e_fetch_switch_configuration(pf, false);
13090 if (ret) {
13091 dev_info(&pf->pdev->dev,
13092 "couldn't fetch switch config, err %s aq_err %s\n",
13093 i40e_stat_str(&pf->hw, ret),
13094 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13095 return ret;
13096 }
13097 i40e_pf_reset_stats(pf);
13098
13099 /* set the switch config bit for the whole device to
13100 * support limited promisc or true promisc
13101 * when user requests promisc. The default is limited
13102 * promisc.
13103 */
13104
13105 if ((pf->hw.pf_id == 0) &&
13106 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
13107 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
13108 pf->last_sw_conf_flags = flags;
13109 }
13110
13111 if (pf->hw.pf_id == 0) {
13112 u16 valid_flags;
13113
13114 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
13115 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
13116 NULL);
13117 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
13118 dev_info(&pf->pdev->dev,
13119 "couldn't set switch config bits, err %s aq_err %s\n",
13120 i40e_stat_str(&pf->hw, ret),
13121 i40e_aq_str(&pf->hw,
13122 pf->hw.aq.asq_last_status));
13123 /* not a fatal problem, just keep going */
13124 }
13125 pf->last_sw_conf_valid_flags = valid_flags;
13126 }
13127
13128 /* first time setup */
13129 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
13130 struct i40e_vsi *vsi = NULL;
13131 u16 uplink_seid;
13132
13133 /* Set up the PF VSI associated with the PF's main VSI
13134 * that is already in the HW switch
13135 */
13136 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
13137 uplink_seid = pf->veb[pf->lan_veb]->seid;
13138 else
13139 uplink_seid = pf->mac_seid;
13140 if (pf->lan_vsi == I40E_NO_VSI)
13141 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
13142 else if (reinit)
13143 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
13144 if (!vsi) {
13145 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
13146 i40e_cloud_filter_exit(pf);
13147 i40e_fdir_teardown(pf);
13148 return -EAGAIN;
13149 }
13150 } else {
13151 /* force a reset of TC and queue layout configurations */
13152 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
13153
13154 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
13155 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
13156 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
13157 }
13158 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
13159
13160 i40e_fdir_sb_setup(pf);
13161
13162 /* Setup static PF queue filter control settings */
13163 ret = i40e_setup_pf_filter_control(pf);
13164 if (ret) {
13165 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
13166 ret);
13167 /* Failure here should not stop continuing other steps */
13168 }
13169
13170 /* enable RSS in the HW, even for only one queue, as the stack can use
13171 * the hash
13172 */
13173 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
13174 i40e_pf_config_rss(pf);
13175
13176 /* fill in link information and enable LSE reporting */
13177 i40e_link_event(pf);
13178
13179 /* Initialize user-specific link properties */
13180 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
13181 I40E_AQ_AN_COMPLETED) ? true : false);
13182
13183 i40e_ptp_init(pf);
13184
13185 /* repopulate tunnel port filters */
13186 i40e_sync_udp_filters(pf);
13187
13188 return ret;
13189 }
13190
13191 /**
13192 * i40e_determine_queue_usage - Work out queue distribution
13193 * @pf: board private structure
13194 **/
13195 static void i40e_determine_queue_usage(struct i40e_pf *pf)
13196 {
13197 int queues_left;
13198 int q_max;
13199
13200 pf->num_lan_qps = 0;
13201
13202 /* Find the max queues to be put into basic use. We'll always be
13203 * using TC0, whether or not DCB is running, and TC0 will get the
13204 * big RSS set.
13205 */
13206 queues_left = pf->hw.func_caps.num_tx_qp;
13207
13208 if ((queues_left == 1) ||
13209 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
13210 /* one qp for PF, no queues for anything else */
13211 queues_left = 0;
13212 pf->alloc_rss_size = pf->num_lan_qps = 1;
13213
13214 /* make sure all the fancies are disabled */
13215 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
13216 I40E_FLAG_IWARP_ENABLED |
13217 I40E_FLAG_FD_SB_ENABLED |
13218 I40E_FLAG_FD_ATR_ENABLED |
13219 I40E_FLAG_DCB_CAPABLE |
13220 I40E_FLAG_DCB_ENABLED |
13221 I40E_FLAG_SRIOV_ENABLED |
13222 I40E_FLAG_VMDQ_ENABLED);
13223 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
13224 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
13225 I40E_FLAG_FD_SB_ENABLED |
13226 I40E_FLAG_FD_ATR_ENABLED |
13227 I40E_FLAG_DCB_CAPABLE))) {
13228 /* one qp for PF */
13229 pf->alloc_rss_size = pf->num_lan_qps = 1;
13230 queues_left -= pf->num_lan_qps;
13231
13232 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
13233 I40E_FLAG_IWARP_ENABLED |
13234 I40E_FLAG_FD_SB_ENABLED |
13235 I40E_FLAG_FD_ATR_ENABLED |
13236 I40E_FLAG_DCB_ENABLED |
13237 I40E_FLAG_VMDQ_ENABLED);
13238 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
13239 } else {
13240 /* Not enough queues for all TCs */
13241 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
13242 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
13243 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
13244 I40E_FLAG_DCB_ENABLED);
13245 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
13246 }
13247
13248 /* limit lan qps to the smaller of qps, cpus or msix */
13249 q_max = max_t(int, pf->rss_size_max, num_online_cpus());
13250 q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
13251 q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
13252 pf->num_lan_qps = q_max;
13253
13254 queues_left -= pf->num_lan_qps;
13255 }
13256
13257 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
13258 if (queues_left > 1) {
13259 queues_left -= 1; /* save 1 queue for FD */
13260 } else {
13261 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
13262 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
13263 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
13264 }
13265 }
13266
13267 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
13268 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
13269 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
13270 (queues_left / pf->num_vf_qps));
13271 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
13272 }
13273
13274 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
13275 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
13276 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
13277 (queues_left / pf->num_vmdq_qps));
13278 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
13279 }
13280
13281 pf->queues_left = queues_left;
13282 dev_dbg(&pf->pdev->dev,
13283 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
13284 pf->hw.func_caps.num_tx_qp,
13285 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
13286 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
13287 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
13288 queues_left);
13289 }
13290
13291 /**
13292 * i40e_setup_pf_filter_control - Setup PF static filter control
13293 * @pf: PF to be setup
13294 *
13295 * i40e_setup_pf_filter_control sets up a PF's initial filter control
13296 * settings. If PE/FCoE are enabled then it will also set the per PF
13297 * based filter sizes required for them. It also enables Flow director,
13298 * ethertype and macvlan type filter settings for the pf.
13299 *
13300 * Returns 0 on success, negative on failure
13301 **/
13302 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
13303 {
13304 struct i40e_filter_control_settings *settings = &pf->filter_settings;
13305
13306 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
13307
13308 /* Flow Director is enabled */
13309 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
13310 settings->enable_fdir = true;
13311
13312 /* Ethtype and MACVLAN filters enabled for PF */
13313 settings->enable_ethtype = true;
13314 settings->enable_macvlan = true;
13315
13316 if (i40e_set_filter_control(&pf->hw, settings))
13317 return -ENOENT;
13318
13319 return 0;
13320 }
13321
13322 #define INFO_STRING_LEN 255
13323 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
13324 static void i40e_print_features(struct i40e_pf *pf)
13325 {
13326 struct i40e_hw *hw = &pf->hw;
13327 char *buf;
13328 int i;
13329
13330 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
13331 if (!buf)
13332 return;
13333
13334 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
13335 #ifdef CONFIG_PCI_IOV
13336 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
13337 #endif
13338 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
13339 pf->hw.func_caps.num_vsis,
13340 pf->vsi[pf->lan_vsi]->num_queue_pairs);
13341 if (pf->flags & I40E_FLAG_RSS_ENABLED)
13342 i += snprintf(&buf[i], REMAIN(i), " RSS");
13343 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
13344 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
13345 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
13346 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
13347 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
13348 }
13349 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
13350 i += snprintf(&buf[i], REMAIN(i), " DCB");
13351 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
13352 i += snprintf(&buf[i], REMAIN(i), " Geneve");
13353 if (pf->flags & I40E_FLAG_PTP)
13354 i += snprintf(&buf[i], REMAIN(i), " PTP");
13355 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
13356 i += snprintf(&buf[i], REMAIN(i), " VEB");
13357 else
13358 i += snprintf(&buf[i], REMAIN(i), " VEPA");
13359
13360 dev_info(&pf->pdev->dev, "%s\n", buf);
13361 kfree(buf);
13362 WARN_ON(i > INFO_STRING_LEN);
13363 }
13364
13365 /**
13366 * i40e_get_platform_mac_addr - get platform-specific MAC address
13367 * @pdev: PCI device information struct
13368 * @pf: board private structure
13369 *
13370 * Look up the MAC address for the device. First we'll try
13371 * eth_platform_get_mac_address, which will check Open Firmware, or arch
13372 * specific fallback. Otherwise, we'll default to the stored value in
13373 * firmware.
13374 **/
13375 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
13376 {
13377 if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
13378 i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
13379 }
13380
13381 /**
13382 * i40e_probe - Device initialization routine
13383 * @pdev: PCI device information struct
13384 * @ent: entry in i40e_pci_tbl
13385 *
13386 * i40e_probe initializes a PF identified by a pci_dev structure.
13387 * The OS initialization, configuring of the PF private structure,
13388 * and a hardware reset occur.
13389 *
13390 * Returns 0 on success, negative on failure
13391 **/
13392 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
13393 {
13394 struct i40e_aq_get_phy_abilities_resp abilities;
13395 struct i40e_pf *pf;
13396 struct i40e_hw *hw;
13397 static u16 pfs_found;
13398 u16 wol_nvm_bits;
13399 u16 link_status;
13400 int err;
13401 u32 val;
13402 u32 i;
13403 u8 set_fc_aq_fail;
13404
13405 err = pci_enable_device_mem(pdev);
13406 if (err)
13407 return err;
13408
13409 /* set up for high or low dma */
13410 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
13411 if (err) {
13412 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
13413 if (err) {
13414 dev_err(&pdev->dev,
13415 "DMA configuration failed: 0x%x\n", err);
13416 goto err_dma;
13417 }
13418 }
13419
13420 /* set up pci connections */
13421 err = pci_request_mem_regions(pdev, i40e_driver_name);
13422 if (err) {
13423 dev_info(&pdev->dev,
13424 "pci_request_selected_regions failed %d\n", err);
13425 goto err_pci_reg;
13426 }
13427
13428 pci_enable_pcie_error_reporting(pdev);
13429 pci_set_master(pdev);
13430
13431 /* Now that we have a PCI connection, we need to do the
13432 * low level device setup. This is primarily setting up
13433 * the Admin Queue structures and then querying for the
13434 * device's current profile information.
13435 */
13436 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
13437 if (!pf) {
13438 err = -ENOMEM;
13439 goto err_pf_alloc;
13440 }
13441 pf->next_vsi = 0;
13442 pf->pdev = pdev;
13443 set_bit(__I40E_DOWN, pf->state);
13444
13445 hw = &pf->hw;
13446 hw->back = pf;
13447
13448 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
13449 I40E_MAX_CSR_SPACE);
13450
13451 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
13452 if (!hw->hw_addr) {
13453 err = -EIO;
13454 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
13455 (unsigned int)pci_resource_start(pdev, 0),
13456 pf->ioremap_len, err);
13457 goto err_ioremap;
13458 }
13459 hw->vendor_id = pdev->vendor;
13460 hw->device_id = pdev->device;
13461 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
13462 hw->subsystem_vendor_id = pdev->subsystem_vendor;
13463 hw->subsystem_device_id = pdev->subsystem_device;
13464 hw->bus.device = PCI_SLOT(pdev->devfn);
13465 hw->bus.func = PCI_FUNC(pdev->devfn);
13466 hw->bus.bus_id = pdev->bus->number;
13467 pf->instance = pfs_found;
13468
13469 /* Select something other than the 802.1ad ethertype for the
13470 * switch to use internally and drop on ingress.
13471 */
13472 hw->switch_tag = 0xffff;
13473 hw->first_tag = ETH_P_8021AD;
13474 hw->second_tag = ETH_P_8021Q;
13475
13476 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
13477 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
13478
13479 /* set up the locks for the AQ, do this only once in probe
13480 * and destroy them only once in remove
13481 */
13482 mutex_init(&hw->aq.asq_mutex);
13483 mutex_init(&hw->aq.arq_mutex);
13484
13485 pf->msg_enable = netif_msg_init(debug,
13486 NETIF_MSG_DRV |
13487 NETIF_MSG_PROBE |
13488 NETIF_MSG_LINK);
13489 if (debug < -1)
13490 pf->hw.debug_mask = debug;
13491
13492 /* do a special CORER for clearing PXE mode once at init */
13493 if (hw->revision_id == 0 &&
13494 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
13495 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
13496 i40e_flush(hw);
13497 msleep(200);
13498 pf->corer_count++;
13499
13500 i40e_clear_pxe_mode(hw);
13501 }
13502
13503 /* Reset here to make sure all is clean and to define PF 'n' */
13504 i40e_clear_hw(hw);
13505 err = i40e_pf_reset(hw);
13506 if (err) {
13507 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
13508 goto err_pf_reset;
13509 }
13510 pf->pfr_count++;
13511
13512 hw->aq.num_arq_entries = I40E_AQ_LEN;
13513 hw->aq.num_asq_entries = I40E_AQ_LEN;
13514 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
13515 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
13516 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
13517
13518 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
13519 "%s-%s:misc",
13520 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
13521
13522 err = i40e_init_shared_code(hw);
13523 if (err) {
13524 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
13525 err);
13526 goto err_pf_reset;
13527 }
13528
13529 /* set up a default setting for link flow control */
13530 pf->hw.fc.requested_mode = I40E_FC_NONE;
13531
13532 err = i40e_init_adminq(hw);
13533 if (err) {
13534 if (err == I40E_ERR_FIRMWARE_API_VERSION)
13535 dev_info(&pdev->dev,
13536 "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");
13537 else
13538 dev_info(&pdev->dev,
13539 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
13540
13541 goto err_pf_reset;
13542 }
13543 i40e_get_oem_version(hw);
13544
13545 /* provide nvm, fw, api versions */
13546 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
13547 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
13548 hw->aq.api_maj_ver, hw->aq.api_min_ver,
13549 i40e_nvm_version_str(hw));
13550
13551 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
13552 hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
13553 dev_info(&pdev->dev,
13554 "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");
13555 else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
13556 dev_info(&pdev->dev,
13557 "The driver for the device detected an older version of the NVM image than expected. Please update the NVM image.\n");
13558
13559 i40e_verify_eeprom(pf);
13560
13561 /* Rev 0 hardware was never productized */
13562 if (hw->revision_id < 1)
13563 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");
13564
13565 i40e_clear_pxe_mode(hw);
13566 err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
13567 if (err)
13568 goto err_adminq_setup;
13569
13570 err = i40e_sw_init(pf);
13571 if (err) {
13572 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
13573 goto err_sw_init;
13574 }
13575
13576 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
13577 hw->func_caps.num_rx_qp, 0, 0);
13578 if (err) {
13579 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
13580 goto err_init_lan_hmc;
13581 }
13582
13583 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
13584 if (err) {
13585 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
13586 err = -ENOENT;
13587 goto err_configure_lan_hmc;
13588 }
13589
13590 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
13591 * Ignore error return codes because if it was already disabled via
13592 * hardware settings this will fail
13593 */
13594 if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
13595 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
13596 i40e_aq_stop_lldp(hw, true, NULL);
13597 }
13598
13599 /* allow a platform config to override the HW addr */
13600 i40e_get_platform_mac_addr(pdev, pf);
13601
13602 if (!is_valid_ether_addr(hw->mac.addr)) {
13603 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
13604 err = -EIO;
13605 goto err_mac_addr;
13606 }
13607 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
13608 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
13609 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
13610 if (is_valid_ether_addr(hw->mac.port_addr))
13611 pf->hw_features |= I40E_HW_PORT_ID_VALID;
13612
13613 pci_set_drvdata(pdev, pf);
13614 pci_save_state(pdev);
13615 #ifdef CONFIG_I40E_DCB
13616 err = i40e_init_pf_dcb(pf);
13617 if (err) {
13618 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
13619 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
13620 /* Continue without DCB enabled */
13621 }
13622 #endif /* CONFIG_I40E_DCB */
13623
13624 /* set up periodic task facility */
13625 timer_setup(&pf->service_timer, i40e_service_timer, 0);
13626 pf->service_timer_period = HZ;
13627
13628 INIT_WORK(&pf->service_task, i40e_service_task);
13629 clear_bit(__I40E_SERVICE_SCHED, pf->state);
13630
13631 /* NVM bit on means WoL disabled for the port */
13632 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
13633 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
13634 pf->wol_en = false;
13635 else
13636 pf->wol_en = true;
13637 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
13638
13639 /* set up the main switch operations */
13640 i40e_determine_queue_usage(pf);
13641 err = i40e_init_interrupt_scheme(pf);
13642 if (err)
13643 goto err_switch_setup;
13644
13645 /* The number of VSIs reported by the FW is the minimum guaranteed
13646 * to us; HW supports far more and we share the remaining pool with
13647 * the other PFs. We allocate space for more than the guarantee with
13648 * the understanding that we might not get them all later.
13649 */
13650 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
13651 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
13652 else
13653 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
13654
13655 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
13656 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
13657 GFP_KERNEL);
13658 if (!pf->vsi) {
13659 err = -ENOMEM;
13660 goto err_switch_setup;
13661 }
13662
13663 #ifdef CONFIG_PCI_IOV
13664 /* prep for VF support */
13665 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
13666 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
13667 !test_bit(__I40E_BAD_EEPROM, pf->state)) {
13668 if (pci_num_vf(pdev))
13669 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
13670 }
13671 #endif
13672 err = i40e_setup_pf_switch(pf, false);
13673 if (err) {
13674 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
13675 goto err_vsis;
13676 }
13677 INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
13678
13679 /* Make sure flow control is set according to current settings */
13680 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
13681 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
13682 dev_dbg(&pf->pdev->dev,
13683 "Set fc with err %s aq_err %s on get_phy_cap\n",
13684 i40e_stat_str(hw, err),
13685 i40e_aq_str(hw, hw->aq.asq_last_status));
13686 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
13687 dev_dbg(&pf->pdev->dev,
13688 "Set fc with err %s aq_err %s on set_phy_config\n",
13689 i40e_stat_str(hw, err),
13690 i40e_aq_str(hw, hw->aq.asq_last_status));
13691 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
13692 dev_dbg(&pf->pdev->dev,
13693 "Set fc with err %s aq_err %s on get_link_info\n",
13694 i40e_stat_str(hw, err),
13695 i40e_aq_str(hw, hw->aq.asq_last_status));
13696
13697 /* if FDIR VSI was set up, start it now */
13698 for (i = 0; i < pf->num_alloc_vsi; i++) {
13699 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
13700 i40e_vsi_open(pf->vsi[i]);
13701 break;
13702 }
13703 }
13704
13705 /* The driver only wants link up/down and module qualification
13706 * reports from firmware. Note the negative logic.
13707 */
13708 err = i40e_aq_set_phy_int_mask(&pf->hw,
13709 ~(I40E_AQ_EVENT_LINK_UPDOWN |
13710 I40E_AQ_EVENT_MEDIA_NA |
13711 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
13712 if (err)
13713 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
13714 i40e_stat_str(&pf->hw, err),
13715 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13716
13717 /* Reconfigure hardware for allowing smaller MSS in the case
13718 * of TSO, so that we avoid the MDD being fired and causing
13719 * a reset in the case of small MSS+TSO.
13720 */
13721 val = rd32(hw, I40E_REG_MSS);
13722 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
13723 val &= ~I40E_REG_MSS_MIN_MASK;
13724 val |= I40E_64BYTE_MSS;
13725 wr32(hw, I40E_REG_MSS, val);
13726 }
13727
13728 if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
13729 msleep(75);
13730 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
13731 if (err)
13732 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
13733 i40e_stat_str(&pf->hw, err),
13734 i40e_aq_str(&pf->hw,
13735 pf->hw.aq.asq_last_status));
13736 }
13737 /* The main driver is (mostly) up and happy. We need to set this state
13738 * before setting up the misc vector or we get a race and the vector
13739 * ends up disabled forever.
13740 */
13741 clear_bit(__I40E_DOWN, pf->state);
13742
13743 /* In case of MSIX we are going to setup the misc vector right here
13744 * to handle admin queue events etc. In case of legacy and MSI
13745 * the misc functionality and queue processing is combined in
13746 * the same vector and that gets setup at open.
13747 */
13748 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13749 err = i40e_setup_misc_vector(pf);
13750 if (err) {
13751 dev_info(&pdev->dev,
13752 "setup of misc vector failed: %d\n", err);
13753 goto err_vsis;
13754 }
13755 }
13756
13757 #ifdef CONFIG_PCI_IOV
13758 /* prep for VF support */
13759 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
13760 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
13761 !test_bit(__I40E_BAD_EEPROM, pf->state)) {
13762 /* disable link interrupts for VFs */
13763 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
13764 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
13765 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
13766 i40e_flush(hw);
13767
13768 if (pci_num_vf(pdev)) {
13769 dev_info(&pdev->dev,
13770 "Active VFs found, allocating resources.\n");
13771 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
13772 if (err)
13773 dev_info(&pdev->dev,
13774 "Error %d allocating resources for existing VFs\n",
13775 err);
13776 }
13777 }
13778 #endif /* CONFIG_PCI_IOV */
13779
13780 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
13781 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
13782 pf->num_iwarp_msix,
13783 I40E_IWARP_IRQ_PILE_ID);
13784 if (pf->iwarp_base_vector < 0) {
13785 dev_info(&pdev->dev,
13786 "failed to get tracking for %d vectors for IWARP err=%d\n",
13787 pf->num_iwarp_msix, pf->iwarp_base_vector);
13788 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
13789 }
13790 }
13791
13792 i40e_dbg_pf_init(pf);
13793
13794 /* tell the firmware that we're starting */
13795 i40e_send_version(pf);
13796
13797 /* since everything's happy, start the service_task timer */
13798 mod_timer(&pf->service_timer,
13799 round_jiffies(jiffies + pf->service_timer_period));
13800
13801 /* add this PF to client device list and launch a client service task */
13802 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
13803 err = i40e_lan_add_device(pf);
13804 if (err)
13805 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
13806 err);
13807 }
13808
13809 #define PCI_SPEED_SIZE 8
13810 #define PCI_WIDTH_SIZE 8
13811 /* Devices on the IOSF bus do not have this information
13812 * and will report PCI Gen 1 x 1 by default so don't bother
13813 * checking them.
13814 */
13815 if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
13816 char speed[PCI_SPEED_SIZE] = "Unknown";
13817 char width[PCI_WIDTH_SIZE] = "Unknown";
13818
13819 /* Get the negotiated link width and speed from PCI config
13820 * space
13821 */
13822 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
13823 &link_status);
13824
13825 i40e_set_pci_config_data(hw, link_status);
13826
13827 switch (hw->bus.speed) {
13828 case i40e_bus_speed_8000:
13829 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
13830 case i40e_bus_speed_5000:
13831 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
13832 case i40e_bus_speed_2500:
13833 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
13834 default:
13835 break;
13836 }
13837 switch (hw->bus.width) {
13838 case i40e_bus_width_pcie_x8:
13839 strncpy(width, "8", PCI_WIDTH_SIZE); break;
13840 case i40e_bus_width_pcie_x4:
13841 strncpy(width, "4", PCI_WIDTH_SIZE); break;
13842 case i40e_bus_width_pcie_x2:
13843 strncpy(width, "2", PCI_WIDTH_SIZE); break;
13844 case i40e_bus_width_pcie_x1:
13845 strncpy(width, "1", PCI_WIDTH_SIZE); break;
13846 default:
13847 break;
13848 }
13849
13850 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
13851 speed, width);
13852
13853 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
13854 hw->bus.speed < i40e_bus_speed_8000) {
13855 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
13856 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
13857 }
13858 }
13859
13860 /* get the requested speeds from the fw */
13861 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
13862 if (err)
13863 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
13864 i40e_stat_str(&pf->hw, err),
13865 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13866 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
13867
13868 /* get the supported phy types from the fw */
13869 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
13870 if (err)
13871 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
13872 i40e_stat_str(&pf->hw, err),
13873 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13874
13875 /* Add a filter to drop all Flow control frames from any VSI from being
13876 * transmitted. By doing so we stop a malicious VF from sending out
13877 * PAUSE or PFC frames and potentially controlling traffic for other
13878 * PF/VF VSIs.
13879 * The FW can still send Flow control frames if enabled.
13880 */
13881 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
13882 pf->main_vsi_seid);
13883
13884 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
13885 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
13886 pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
13887 if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
13888 pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
13889 /* print a string summarizing features */
13890 i40e_print_features(pf);
13891
13892 return 0;
13893
13894 /* Unwind what we've done if something failed in the setup */
13895 err_vsis:
13896 set_bit(__I40E_DOWN, pf->state);
13897 i40e_clear_interrupt_scheme(pf);
13898 kfree(pf->vsi);
13899 err_switch_setup:
13900 i40e_reset_interrupt_capability(pf);
13901 del_timer_sync(&pf->service_timer);
13902 err_mac_addr:
13903 err_configure_lan_hmc:
13904 (void)i40e_shutdown_lan_hmc(hw);
13905 err_init_lan_hmc:
13906 kfree(pf->qp_pile);
13907 err_sw_init:
13908 err_adminq_setup:
13909 err_pf_reset:
13910 iounmap(hw->hw_addr);
13911 err_ioremap:
13912 kfree(pf);
13913 err_pf_alloc:
13914 pci_disable_pcie_error_reporting(pdev);
13915 pci_release_mem_regions(pdev);
13916 err_pci_reg:
13917 err_dma:
13918 pci_disable_device(pdev);
13919 return err;
13920 }
13921
13922 /**
13923 * i40e_remove - Device removal routine
13924 * @pdev: PCI device information struct
13925 *
13926 * i40e_remove is called by the PCI subsystem to alert the driver
13927 * that is should release a PCI device. This could be caused by a
13928 * Hot-Plug event, or because the driver is going to be removed from
13929 * memory.
13930 **/
13931 static void i40e_remove(struct pci_dev *pdev)
13932 {
13933 struct i40e_pf *pf = pci_get_drvdata(pdev);
13934 struct i40e_hw *hw = &pf->hw;
13935 i40e_status ret_code;
13936 int i;
13937
13938 i40e_dbg_pf_exit(pf);
13939
13940 i40e_ptp_stop(pf);
13941
13942 /* Disable RSS in hw */
13943 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
13944 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
13945
13946 /* no more scheduling of any task */
13947 set_bit(__I40E_SUSPENDED, pf->state);
13948 set_bit(__I40E_DOWN, pf->state);
13949 if (pf->service_timer.function)
13950 del_timer_sync(&pf->service_timer);
13951 if (pf->service_task.func)
13952 cancel_work_sync(&pf->service_task);
13953
13954 /* Client close must be called explicitly here because the timer
13955 * has been stopped.
13956 */
13957 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
13958
13959 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
13960 i40e_free_vfs(pf);
13961 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
13962 }
13963
13964 i40e_fdir_teardown(pf);
13965
13966 /* If there is a switch structure or any orphans, remove them.
13967 * This will leave only the PF's VSI remaining.
13968 */
13969 for (i = 0; i < I40E_MAX_VEB; i++) {
13970 if (!pf->veb[i])
13971 continue;
13972
13973 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
13974 pf->veb[i]->uplink_seid == 0)
13975 i40e_switch_branch_release(pf->veb[i]);
13976 }
13977
13978 /* Now we can shutdown the PF's VSI, just before we kill
13979 * adminq and hmc.
13980 */
13981 if (pf->vsi[pf->lan_vsi])
13982 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
13983
13984 i40e_cloud_filter_exit(pf);
13985
13986 /* remove attached clients */
13987 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
13988 ret_code = i40e_lan_del_device(pf);
13989 if (ret_code)
13990 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
13991 ret_code);
13992 }
13993
13994 /* shutdown and destroy the HMC */
13995 if (hw->hmc.hmc_obj) {
13996 ret_code = i40e_shutdown_lan_hmc(hw);
13997 if (ret_code)
13998 dev_warn(&pdev->dev,
13999 "Failed to destroy the HMC resources: %d\n",
14000 ret_code);
14001 }
14002
14003 /* shutdown the adminq */
14004 i40e_shutdown_adminq(hw);
14005
14006 /* destroy the locks only once, here */
14007 mutex_destroy(&hw->aq.arq_mutex);
14008 mutex_destroy(&hw->aq.asq_mutex);
14009
14010 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
14011 i40e_clear_interrupt_scheme(pf);
14012 for (i = 0; i < pf->num_alloc_vsi; i++) {
14013 if (pf->vsi[i]) {
14014 i40e_vsi_clear_rings(pf->vsi[i]);
14015 i40e_vsi_clear(pf->vsi[i]);
14016 pf->vsi[i] = NULL;
14017 }
14018 }
14019
14020 for (i = 0; i < I40E_MAX_VEB; i++) {
14021 kfree(pf->veb[i]);
14022 pf->veb[i] = NULL;
14023 }
14024
14025 kfree(pf->qp_pile);
14026 kfree(pf->vsi);
14027
14028 iounmap(hw->hw_addr);
14029 kfree(pf);
14030 pci_release_mem_regions(pdev);
14031
14032 pci_disable_pcie_error_reporting(pdev);
14033 pci_disable_device(pdev);
14034 }
14035
14036 /**
14037 * i40e_pci_error_detected - warning that something funky happened in PCI land
14038 * @pdev: PCI device information struct
14039 *
14040 * Called to warn that something happened and the error handling steps
14041 * are in progress. Allows the driver to quiesce things, be ready for
14042 * remediation.
14043 **/
14044 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
14045 enum pci_channel_state error)
14046 {
14047 struct i40e_pf *pf = pci_get_drvdata(pdev);
14048
14049 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
14050
14051 if (!pf) {
14052 dev_info(&pdev->dev,
14053 "Cannot recover - error happened during device probe\n");
14054 return PCI_ERS_RESULT_DISCONNECT;
14055 }
14056
14057 /* shutdown all operations */
14058 if (!test_bit(__I40E_SUSPENDED, pf->state))
14059 i40e_prep_for_reset(pf, false);
14060
14061 /* Request a slot reset */
14062 return PCI_ERS_RESULT_NEED_RESET;
14063 }
14064
14065 /**
14066 * i40e_pci_error_slot_reset - a PCI slot reset just happened
14067 * @pdev: PCI device information struct
14068 *
14069 * Called to find if the driver can work with the device now that
14070 * the pci slot has been reset. If a basic connection seems good
14071 * (registers are readable and have sane content) then return a
14072 * happy little PCI_ERS_RESULT_xxx.
14073 **/
14074 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
14075 {
14076 struct i40e_pf *pf = pci_get_drvdata(pdev);
14077 pci_ers_result_t result;
14078 int err;
14079 u32 reg;
14080
14081 dev_dbg(&pdev->dev, "%s\n", __func__);
14082 if (pci_enable_device_mem(pdev)) {
14083 dev_info(&pdev->dev,
14084 "Cannot re-enable PCI device after reset.\n");
14085 result = PCI_ERS_RESULT_DISCONNECT;
14086 } else {
14087 pci_set_master(pdev);
14088 pci_restore_state(pdev);
14089 pci_save_state(pdev);
14090 pci_wake_from_d3(pdev, false);
14091
14092 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
14093 if (reg == 0)
14094 result = PCI_ERS_RESULT_RECOVERED;
14095 else
14096 result = PCI_ERS_RESULT_DISCONNECT;
14097 }
14098
14099 err = pci_cleanup_aer_uncorrect_error_status(pdev);
14100 if (err) {
14101 dev_info(&pdev->dev,
14102 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
14103 err);
14104 /* non-fatal, continue */
14105 }
14106
14107 return result;
14108 }
14109
14110 /**
14111 * i40e_pci_error_reset_prepare - prepare device driver for pci reset
14112 * @pdev: PCI device information struct
14113 */
14114 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
14115 {
14116 struct i40e_pf *pf = pci_get_drvdata(pdev);
14117
14118 i40e_prep_for_reset(pf, false);
14119 }
14120
14121 /**
14122 * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
14123 * @pdev: PCI device information struct
14124 */
14125 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
14126 {
14127 struct i40e_pf *pf = pci_get_drvdata(pdev);
14128
14129 i40e_reset_and_rebuild(pf, false, false);
14130 }
14131
14132 /**
14133 * i40e_pci_error_resume - restart operations after PCI error recovery
14134 * @pdev: PCI device information struct
14135 *
14136 * Called to allow the driver to bring things back up after PCI error
14137 * and/or reset recovery has finished.
14138 **/
14139 static void i40e_pci_error_resume(struct pci_dev *pdev)
14140 {
14141 struct i40e_pf *pf = pci_get_drvdata(pdev);
14142
14143 dev_dbg(&pdev->dev, "%s\n", __func__);
14144 if (test_bit(__I40E_SUSPENDED, pf->state))
14145 return;
14146
14147 i40e_handle_reset_warning(pf, false);
14148 }
14149
14150 /**
14151 * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
14152 * using the mac_address_write admin q function
14153 * @pf: pointer to i40e_pf struct
14154 **/
14155 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
14156 {
14157 struct i40e_hw *hw = &pf->hw;
14158 i40e_status ret;
14159 u8 mac_addr[6];
14160 u16 flags = 0;
14161
14162 /* Get current MAC address in case it's an LAA */
14163 if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
14164 ether_addr_copy(mac_addr,
14165 pf->vsi[pf->lan_vsi]->netdev->dev_addr);
14166 } else {
14167 dev_err(&pf->pdev->dev,
14168 "Failed to retrieve MAC address; using default\n");
14169 ether_addr_copy(mac_addr, hw->mac.addr);
14170 }
14171
14172 /* The FW expects the mac address write cmd to first be called with
14173 * one of these flags before calling it again with the multicast
14174 * enable flags.
14175 */
14176 flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
14177
14178 if (hw->func_caps.flex10_enable && hw->partition_id != 1)
14179 flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
14180
14181 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
14182 if (ret) {
14183 dev_err(&pf->pdev->dev,
14184 "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
14185 return;
14186 }
14187
14188 flags = I40E_AQC_MC_MAG_EN
14189 | I40E_AQC_WOL_PRESERVE_ON_PFR
14190 | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
14191 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
14192 if (ret)
14193 dev_err(&pf->pdev->dev,
14194 "Failed to enable Multicast Magic Packet wake up\n");
14195 }
14196
14197 /**
14198 * i40e_shutdown - PCI callback for shutting down
14199 * @pdev: PCI device information struct
14200 **/
14201 static void i40e_shutdown(struct pci_dev *pdev)
14202 {
14203 struct i40e_pf *pf = pci_get_drvdata(pdev);
14204 struct i40e_hw *hw = &pf->hw;
14205
14206 set_bit(__I40E_SUSPENDED, pf->state);
14207 set_bit(__I40E_DOWN, pf->state);
14208 rtnl_lock();
14209 i40e_prep_for_reset(pf, true);
14210 rtnl_unlock();
14211
14212 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
14213 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
14214
14215 del_timer_sync(&pf->service_timer);
14216 cancel_work_sync(&pf->service_task);
14217 i40e_cloud_filter_exit(pf);
14218 i40e_fdir_teardown(pf);
14219
14220 /* Client close must be called explicitly here because the timer
14221 * has been stopped.
14222 */
14223 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
14224
14225 if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
14226 i40e_enable_mc_magic_wake(pf);
14227
14228 i40e_prep_for_reset(pf, false);
14229
14230 wr32(hw, I40E_PFPM_APM,
14231 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
14232 wr32(hw, I40E_PFPM_WUFC,
14233 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
14234
14235 i40e_clear_interrupt_scheme(pf);
14236
14237 if (system_state == SYSTEM_POWER_OFF) {
14238 pci_wake_from_d3(pdev, pf->wol_en);
14239 pci_set_power_state(pdev, PCI_D3hot);
14240 }
14241 }
14242
14243 /**
14244 * i40e_suspend - PM callback for moving to D3
14245 * @dev: generic device information structure
14246 **/
14247 static int __maybe_unused i40e_suspend(struct device *dev)
14248 {
14249 struct pci_dev *pdev = to_pci_dev(dev);
14250 struct i40e_pf *pf = pci_get_drvdata(pdev);
14251 struct i40e_hw *hw = &pf->hw;
14252
14253 /* If we're already suspended, then there is nothing to do */
14254 if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
14255 return 0;
14256
14257 set_bit(__I40E_DOWN, pf->state);
14258
14259 /* Ensure service task will not be running */
14260 del_timer_sync(&pf->service_timer);
14261 cancel_work_sync(&pf->service_task);
14262
14263 if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
14264 i40e_enable_mc_magic_wake(pf);
14265
14266 /* Since we're going to destroy queues during the
14267 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
14268 * whole section
14269 */
14270 rtnl_lock();
14271
14272 i40e_prep_for_reset(pf, true);
14273
14274 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
14275 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
14276
14277 /* Clear the interrupt scheme and release our IRQs so that the system
14278 * can safely hibernate even when there are a large number of CPUs.
14279 * Otherwise hibernation might fail when mapping all the vectors back
14280 * to CPU0.
14281 */
14282 i40e_clear_interrupt_scheme(pf);
14283
14284 rtnl_unlock();
14285
14286 return 0;
14287 }
14288
14289 /**
14290 * i40e_resume - PM callback for waking up from D3
14291 * @dev: generic device information structure
14292 **/
14293 static int __maybe_unused i40e_resume(struct device *dev)
14294 {
14295 struct pci_dev *pdev = to_pci_dev(dev);
14296 struct i40e_pf *pf = pci_get_drvdata(pdev);
14297 int err;
14298
14299 /* If we're not suspended, then there is nothing to do */
14300 if (!test_bit(__I40E_SUSPENDED, pf->state))
14301 return 0;
14302
14303 /* We need to hold the RTNL lock prior to restoring interrupt schemes,
14304 * since we're going to be restoring queues
14305 */
14306 rtnl_lock();
14307
14308 /* We cleared the interrupt scheme when we suspended, so we need to
14309 * restore it now to resume device functionality.
14310 */
14311 err = i40e_restore_interrupt_scheme(pf);
14312 if (err) {
14313 dev_err(&pdev->dev, "Cannot restore interrupt scheme: %d\n",
14314 err);
14315 }
14316
14317 clear_bit(__I40E_DOWN, pf->state);
14318 i40e_reset_and_rebuild(pf, false, true);
14319
14320 rtnl_unlock();
14321
14322 /* Clear suspended state last after everything is recovered */
14323 clear_bit(__I40E_SUSPENDED, pf->state);
14324
14325 /* Restart the service task */
14326 mod_timer(&pf->service_timer,
14327 round_jiffies(jiffies + pf->service_timer_period));
14328
14329 return 0;
14330 }
14331
14332 static const struct pci_error_handlers i40e_err_handler = {
14333 .error_detected = i40e_pci_error_detected,
14334 .slot_reset = i40e_pci_error_slot_reset,
14335 .reset_prepare = i40e_pci_error_reset_prepare,
14336 .reset_done = i40e_pci_error_reset_done,
14337 .resume = i40e_pci_error_resume,
14338 };
14339
14340 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
14341
14342 static struct pci_driver i40e_driver = {
14343 .name = i40e_driver_name,
14344 .id_table = i40e_pci_tbl,
14345 .probe = i40e_probe,
14346 .remove = i40e_remove,
14347 .driver = {
14348 .pm = &i40e_pm_ops,
14349 },
14350 .shutdown = i40e_shutdown,
14351 .err_handler = &i40e_err_handler,
14352 .sriov_configure = i40e_pci_sriov_configure,
14353 };
14354
14355 /**
14356 * i40e_init_module - Driver registration routine
14357 *
14358 * i40e_init_module is the first routine called when the driver is
14359 * loaded. All it does is register with the PCI subsystem.
14360 **/
14361 static int __init i40e_init_module(void)
14362 {
14363 pr_info("%s: %s - version %s\n", i40e_driver_name,
14364 i40e_driver_string, i40e_driver_version_str);
14365 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
14366
14367 /* There is no need to throttle the number of active tasks because
14368 * each device limits its own task using a state bit for scheduling
14369 * the service task, and the device tasks do not interfere with each
14370 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
14371 * since we need to be able to guarantee forward progress even under
14372 * memory pressure.
14373 */
14374 i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
14375 if (!i40e_wq) {
14376 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
14377 return -ENOMEM;
14378 }
14379
14380 i40e_dbg_init();
14381 return pci_register_driver(&i40e_driver);
14382 }
14383 module_init(i40e_init_module);
14384
14385 /**
14386 * i40e_exit_module - Driver exit cleanup routine
14387 *
14388 * i40e_exit_module is called just before the driver is removed
14389 * from memory.
14390 **/
14391 static void __exit i40e_exit_module(void)
14392 {
14393 pci_unregister_driver(&i40e_driver);
14394 destroy_workqueue(i40e_wq);
14395 i40e_dbg_exit();
14396 }
14397 module_exit(i40e_exit_module);