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