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