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