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