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