Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[linux-drm-fsl-dcu.git] / drivers / net / ethernet / intel / i40e / i40e_main.c
1 /*******************************************************************************
2  *
3  * Intel Ethernet Controller XL710 Family Linux Driver
4  * Copyright(c) 2013 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 with
16  * this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * The full GNU General Public License is included in this distribution in
20  * the file called "COPYING".
21  *
22  * Contact Information:
23  * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25  *
26  ******************************************************************************/
27
28 /* Local includes */
29 #include "i40e.h"
30
31 const char i40e_driver_name[] = "i40e";
32 static const char i40e_driver_string[] =
33                         "Intel(R) Ethernet Connection XL710 Network Driver";
34
35 #define DRV_KERN "-k"
36
37 #define DRV_VERSION_MAJOR 0
38 #define DRV_VERSION_MINOR 3
39 #define DRV_VERSION_BUILD 13
40 #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
41              __stringify(DRV_VERSION_MINOR) "." \
42              __stringify(DRV_VERSION_BUILD)    DRV_KERN
43 const char i40e_driver_version_str[] = DRV_VERSION;
44 static const char i40e_copyright[] = "Copyright (c) 2013 Intel Corporation.";
45
46 /* a bit of forward declarations */
47 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
48 static void i40e_handle_reset_warning(struct i40e_pf *pf);
49 static int i40e_add_vsi(struct i40e_vsi *vsi);
50 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
51 static int i40e_setup_pf_switch(struct i40e_pf *pf);
52 static int i40e_setup_misc_vector(struct i40e_pf *pf);
53 static void i40e_determine_queue_usage(struct i40e_pf *pf);
54 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
55
56 /* i40e_pci_tbl - PCI Device ID Table
57  *
58  * Last entry must be all 0s
59  *
60  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
61  *   Class, Class Mask, private data (not used) }
62  */
63 static DEFINE_PCI_DEVICE_TABLE(i40e_pci_tbl) = {
64         {PCI_VDEVICE(INTEL, I40E_SFP_XL710_DEVICE_ID), 0},
65         {PCI_VDEVICE(INTEL, I40E_SFP_X710_DEVICE_ID), 0},
66         {PCI_VDEVICE(INTEL, I40E_QEMU_DEVICE_ID), 0},
67         {PCI_VDEVICE(INTEL, I40E_KX_A_DEVICE_ID), 0},
68         {PCI_VDEVICE(INTEL, I40E_KX_B_DEVICE_ID), 0},
69         {PCI_VDEVICE(INTEL, I40E_KX_C_DEVICE_ID), 0},
70         {PCI_VDEVICE(INTEL, I40E_KX_D_DEVICE_ID), 0},
71         {PCI_VDEVICE(INTEL, I40E_QSFP_A_DEVICE_ID), 0},
72         {PCI_VDEVICE(INTEL, I40E_QSFP_B_DEVICE_ID), 0},
73         {PCI_VDEVICE(INTEL, I40E_QSFP_C_DEVICE_ID), 0},
74         /* required last entry */
75         {0, }
76 };
77 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
78
79 #define I40E_MAX_VF_COUNT 128
80 static int debug = -1;
81 module_param(debug, int, 0);
82 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
83
84 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
85 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
86 MODULE_LICENSE("GPL");
87 MODULE_VERSION(DRV_VERSION);
88
89 /**
90  * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
91  * @hw:   pointer to the HW structure
92  * @mem:  ptr to mem struct to fill out
93  * @size: size of memory requested
94  * @alignment: what to align the allocation to
95  **/
96 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
97                             u64 size, u32 alignment)
98 {
99         struct i40e_pf *pf = (struct i40e_pf *)hw->back;
100
101         mem->size = ALIGN(size, alignment);
102         mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
103                                       &mem->pa, GFP_KERNEL);
104         if (!mem->va)
105                 return -ENOMEM;
106
107         return 0;
108 }
109
110 /**
111  * i40e_free_dma_mem_d - OS specific memory free for shared code
112  * @hw:   pointer to the HW structure
113  * @mem:  ptr to mem struct to free
114  **/
115 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
116 {
117         struct i40e_pf *pf = (struct i40e_pf *)hw->back;
118
119         dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
120         mem->va = NULL;
121         mem->pa = 0;
122         mem->size = 0;
123
124         return 0;
125 }
126
127 /**
128  * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
129  * @hw:   pointer to the HW structure
130  * @mem:  ptr to mem struct to fill out
131  * @size: size of memory requested
132  **/
133 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
134                              u32 size)
135 {
136         mem->size = size;
137         mem->va = kzalloc(size, GFP_KERNEL);
138
139         if (!mem->va)
140                 return -ENOMEM;
141
142         return 0;
143 }
144
145 /**
146  * i40e_free_virt_mem_d - OS specific memory free for shared code
147  * @hw:   pointer to the HW structure
148  * @mem:  ptr to mem struct to free
149  **/
150 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
151 {
152         /* it's ok to kfree a NULL pointer */
153         kfree(mem->va);
154         mem->va = NULL;
155         mem->size = 0;
156
157         return 0;
158 }
159
160 /**
161  * i40e_get_lump - find a lump of free generic resource
162  * @pf: board private structure
163  * @pile: the pile of resource to search
164  * @needed: the number of items needed
165  * @id: an owner id to stick on the items assigned
166  *
167  * Returns the base item index of the lump, or negative for error
168  *
169  * The search_hint trick and lack of advanced fit-finding only work
170  * because we're highly likely to have all the same size lump requests.
171  * Linear search time and any fragmentation should be minimal.
172  **/
173 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
174                          u16 needed, u16 id)
175 {
176         int ret = -ENOMEM;
177         int i, j;
178
179         if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
180                 dev_info(&pf->pdev->dev,
181                          "param err: pile=%p needed=%d id=0x%04x\n",
182                          pile, needed, id);
183                 return -EINVAL;
184         }
185
186         /* start the linear search with an imperfect hint */
187         i = pile->search_hint;
188         while (i < pile->num_entries) {
189                 /* skip already allocated entries */
190                 if (pile->list[i] & I40E_PILE_VALID_BIT) {
191                         i++;
192                         continue;
193                 }
194
195                 /* do we have enough in this lump? */
196                 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
197                         if (pile->list[i+j] & I40E_PILE_VALID_BIT)
198                                 break;
199                 }
200
201                 if (j == needed) {
202                         /* there was enough, so assign it to the requestor */
203                         for (j = 0; j < needed; j++)
204                                 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
205                         ret = i;
206                         pile->search_hint = i + j;
207                         break;
208                 } else {
209                         /* not enough, so skip over it and continue looking */
210                         i += j;
211                 }
212         }
213
214         return ret;
215 }
216
217 /**
218  * i40e_put_lump - return a lump of generic resource
219  * @pile: the pile of resource to search
220  * @index: the base item index
221  * @id: the owner id of the items assigned
222  *
223  * Returns the count of items in the lump
224  **/
225 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
226 {
227         int valid_id = (id | I40E_PILE_VALID_BIT);
228         int count = 0;
229         int i;
230
231         if (!pile || index >= pile->num_entries)
232                 return -EINVAL;
233
234         for (i = index;
235              i < pile->num_entries && pile->list[i] == valid_id;
236              i++) {
237                 pile->list[i] = 0;
238                 count++;
239         }
240
241         if (count && index < pile->search_hint)
242                 pile->search_hint = index;
243
244         return count;
245 }
246
247 /**
248  * i40e_service_event_schedule - Schedule the service task to wake up
249  * @pf: board private structure
250  *
251  * If not already scheduled, this puts the task into the work queue
252  **/
253 static void i40e_service_event_schedule(struct i40e_pf *pf)
254 {
255         if (!test_bit(__I40E_DOWN, &pf->state) &&
256             !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) &&
257             !test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
258                 schedule_work(&pf->service_task);
259 }
260
261 /**
262  * i40e_tx_timeout - Respond to a Tx Hang
263  * @netdev: network interface device structure
264  *
265  * If any port has noticed a Tx timeout, it is likely that the whole
266  * device is munged, not just the one netdev port, so go for the full
267  * reset.
268  **/
269 static void i40e_tx_timeout(struct net_device *netdev)
270 {
271         struct i40e_netdev_priv *np = netdev_priv(netdev);
272         struct i40e_vsi *vsi = np->vsi;
273         struct i40e_pf *pf = vsi->back;
274
275         pf->tx_timeout_count++;
276
277         if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
278                 pf->tx_timeout_recovery_level = 0;
279         pf->tx_timeout_last_recovery = jiffies;
280         netdev_info(netdev, "tx_timeout recovery level %d\n",
281                     pf->tx_timeout_recovery_level);
282
283         switch (pf->tx_timeout_recovery_level) {
284         case 0:
285                 /* disable and re-enable queues for the VSI */
286                 if (in_interrupt()) {
287                         set_bit(__I40E_REINIT_REQUESTED, &pf->state);
288                         set_bit(__I40E_REINIT_REQUESTED, &vsi->state);
289                 } else {
290                         i40e_vsi_reinit_locked(vsi);
291                 }
292                 break;
293         case 1:
294                 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
295                 break;
296         case 2:
297                 set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
298                 break;
299         case 3:
300                 set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
301                 break;
302         default:
303                 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
304                 i40e_down(vsi);
305                 break;
306         }
307         i40e_service_event_schedule(pf);
308         pf->tx_timeout_recovery_level++;
309 }
310
311 /**
312  * i40e_release_rx_desc - Store the new tail and head values
313  * @rx_ring: ring to bump
314  * @val: new head index
315  **/
316 static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
317 {
318         rx_ring->next_to_use = val;
319
320         /* Force memory writes to complete before letting h/w
321          * know there are new descriptors to fetch.  (Only
322          * applicable for weak-ordered memory model archs,
323          * such as IA-64).
324          */
325         wmb();
326         writel(val, rx_ring->tail);
327 }
328
329 /**
330  * i40e_get_vsi_stats_struct - Get System Network Statistics
331  * @vsi: the VSI we care about
332  *
333  * Returns the address of the device statistics structure.
334  * The statistics are actually updated from the service task.
335  **/
336 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
337 {
338         return &vsi->net_stats;
339 }
340
341 /**
342  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
343  * @netdev: network interface device structure
344  *
345  * Returns the address of the device statistics structure.
346  * The statistics are actually updated from the service task.
347  **/
348 static struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
349                                              struct net_device *netdev,
350                                              struct rtnl_link_stats64 *stats)
351 {
352         struct i40e_netdev_priv *np = netdev_priv(netdev);
353         struct i40e_vsi *vsi = np->vsi;
354         struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
355         int i;
356
357         rcu_read_lock();
358         for (i = 0; i < vsi->num_queue_pairs; i++) {
359                 struct i40e_ring *tx_ring, *rx_ring;
360                 u64 bytes, packets;
361                 unsigned int start;
362
363                 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
364                 if (!tx_ring)
365                         continue;
366
367                 do {
368                         start = u64_stats_fetch_begin_bh(&tx_ring->syncp);
369                         packets = tx_ring->stats.packets;
370                         bytes   = tx_ring->stats.bytes;
371                 } while (u64_stats_fetch_retry_bh(&tx_ring->syncp, start));
372
373                 stats->tx_packets += packets;
374                 stats->tx_bytes   += bytes;
375                 rx_ring = &tx_ring[1];
376
377                 do {
378                         start = u64_stats_fetch_begin_bh(&rx_ring->syncp);
379                         packets = rx_ring->stats.packets;
380                         bytes   = rx_ring->stats.bytes;
381                 } while (u64_stats_fetch_retry_bh(&rx_ring->syncp, start));
382
383                 stats->rx_packets += packets;
384                 stats->rx_bytes   += bytes;
385         }
386         rcu_read_unlock();
387
388         /* following stats updated by ixgbe_watchdog_task() */
389         stats->multicast        = vsi_stats->multicast;
390         stats->tx_errors        = vsi_stats->tx_errors;
391         stats->tx_dropped       = vsi_stats->tx_dropped;
392         stats->rx_errors        = vsi_stats->rx_errors;
393         stats->rx_crc_errors    = vsi_stats->rx_crc_errors;
394         stats->rx_length_errors = vsi_stats->rx_length_errors;
395
396         return stats;
397 }
398
399 /**
400  * i40e_vsi_reset_stats - Resets all stats of the given vsi
401  * @vsi: the VSI to have its stats reset
402  **/
403 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
404 {
405         struct rtnl_link_stats64 *ns;
406         int i;
407
408         if (!vsi)
409                 return;
410
411         ns = i40e_get_vsi_stats_struct(vsi);
412         memset(ns, 0, sizeof(*ns));
413         memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
414         memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
415         memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
416         if (vsi->rx_rings)
417                 for (i = 0; i < vsi->num_queue_pairs; i++) {
418                         memset(&vsi->rx_rings[i]->stats, 0 ,
419                                sizeof(vsi->rx_rings[i]->stats));
420                         memset(&vsi->rx_rings[i]->rx_stats, 0 ,
421                                sizeof(vsi->rx_rings[i]->rx_stats));
422                         memset(&vsi->tx_rings[i]->stats, 0 ,
423                                sizeof(vsi->tx_rings[i]->stats));
424                         memset(&vsi->tx_rings[i]->tx_stats, 0,
425                                sizeof(vsi->tx_rings[i]->tx_stats));
426                 }
427         vsi->stat_offsets_loaded = false;
428 }
429
430 /**
431  * i40e_pf_reset_stats - Reset all of the stats for the given pf
432  * @pf: the PF to be reset
433  **/
434 void i40e_pf_reset_stats(struct i40e_pf *pf)
435 {
436         memset(&pf->stats, 0, sizeof(pf->stats));
437         memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
438         pf->stat_offsets_loaded = false;
439 }
440
441 /**
442  * i40e_stat_update48 - read and update a 48 bit stat from the chip
443  * @hw: ptr to the hardware info
444  * @hireg: the high 32 bit reg to read
445  * @loreg: the low 32 bit reg to read
446  * @offset_loaded: has the initial offset been loaded yet
447  * @offset: ptr to current offset value
448  * @stat: ptr to the stat
449  *
450  * Since the device stats are not reset at PFReset, they likely will not
451  * be zeroed when the driver starts.  We'll save the first values read
452  * and use them as offsets to be subtracted from the raw values in order
453  * to report stats that count from zero.  In the process, we also manage
454  * the potential roll-over.
455  **/
456 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
457                                bool offset_loaded, u64 *offset, u64 *stat)
458 {
459         u64 new_data;
460
461         if (hw->device_id == I40E_QEMU_DEVICE_ID) {
462                 new_data = rd32(hw, loreg);
463                 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
464         } else {
465                 new_data = rd64(hw, loreg);
466         }
467         if (!offset_loaded)
468                 *offset = new_data;
469         if (likely(new_data >= *offset))
470                 *stat = new_data - *offset;
471         else
472                 *stat = (new_data + ((u64)1 << 48)) - *offset;
473         *stat &= 0xFFFFFFFFFFFFULL;
474 }
475
476 /**
477  * i40e_stat_update32 - read and update a 32 bit stat from the chip
478  * @hw: ptr to the hardware info
479  * @reg: the hw reg to read
480  * @offset_loaded: has the initial offset been loaded yet
481  * @offset: ptr to current offset value
482  * @stat: ptr to the stat
483  **/
484 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
485                                bool offset_loaded, u64 *offset, u64 *stat)
486 {
487         u32 new_data;
488
489         new_data = rd32(hw, reg);
490         if (!offset_loaded)
491                 *offset = new_data;
492         if (likely(new_data >= *offset))
493                 *stat = (u32)(new_data - *offset);
494         else
495                 *stat = (u32)((new_data + ((u64)1 << 32)) - *offset);
496 }
497
498 /**
499  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
500  * @vsi: the VSI to be updated
501  **/
502 void i40e_update_eth_stats(struct i40e_vsi *vsi)
503 {
504         int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
505         struct i40e_pf *pf = vsi->back;
506         struct i40e_hw *hw = &pf->hw;
507         struct i40e_eth_stats *oes;
508         struct i40e_eth_stats *es;     /* device's eth stats */
509
510         es = &vsi->eth_stats;
511         oes = &vsi->eth_stats_offsets;
512
513         /* Gather up the stats that the hw collects */
514         i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
515                            vsi->stat_offsets_loaded,
516                            &oes->tx_errors, &es->tx_errors);
517         i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
518                            vsi->stat_offsets_loaded,
519                            &oes->rx_discards, &es->rx_discards);
520
521         i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
522                            I40E_GLV_GORCL(stat_idx),
523                            vsi->stat_offsets_loaded,
524                            &oes->rx_bytes, &es->rx_bytes);
525         i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
526                            I40E_GLV_UPRCL(stat_idx),
527                            vsi->stat_offsets_loaded,
528                            &oes->rx_unicast, &es->rx_unicast);
529         i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
530                            I40E_GLV_MPRCL(stat_idx),
531                            vsi->stat_offsets_loaded,
532                            &oes->rx_multicast, &es->rx_multicast);
533         i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
534                            I40E_GLV_BPRCL(stat_idx),
535                            vsi->stat_offsets_loaded,
536                            &oes->rx_broadcast, &es->rx_broadcast);
537
538         i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
539                            I40E_GLV_GOTCL(stat_idx),
540                            vsi->stat_offsets_loaded,
541                            &oes->tx_bytes, &es->tx_bytes);
542         i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
543                            I40E_GLV_UPTCL(stat_idx),
544                            vsi->stat_offsets_loaded,
545                            &oes->tx_unicast, &es->tx_unicast);
546         i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
547                            I40E_GLV_MPTCL(stat_idx),
548                            vsi->stat_offsets_loaded,
549                            &oes->tx_multicast, &es->tx_multicast);
550         i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
551                            I40E_GLV_BPTCL(stat_idx),
552                            vsi->stat_offsets_loaded,
553                            &oes->tx_broadcast, &es->tx_broadcast);
554         vsi->stat_offsets_loaded = true;
555 }
556
557 /**
558  * i40e_update_veb_stats - Update Switch component statistics
559  * @veb: the VEB being updated
560  **/
561 static void i40e_update_veb_stats(struct i40e_veb *veb)
562 {
563         struct i40e_pf *pf = veb->pf;
564         struct i40e_hw *hw = &pf->hw;
565         struct i40e_eth_stats *oes;
566         struct i40e_eth_stats *es;     /* device's eth stats */
567         int idx = 0;
568
569         idx = veb->stats_idx;
570         es = &veb->stats;
571         oes = &veb->stats_offsets;
572
573         /* Gather up the stats that the hw collects */
574         i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
575                            veb->stat_offsets_loaded,
576                            &oes->tx_discards, &es->tx_discards);
577         if (hw->revision_id > 0)
578                 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
579                                    veb->stat_offsets_loaded,
580                                    &oes->rx_unknown_protocol,
581                                    &es->rx_unknown_protocol);
582         i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
583                            veb->stat_offsets_loaded,
584                            &oes->rx_bytes, &es->rx_bytes);
585         i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
586                            veb->stat_offsets_loaded,
587                            &oes->rx_unicast, &es->rx_unicast);
588         i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
589                            veb->stat_offsets_loaded,
590                            &oes->rx_multicast, &es->rx_multicast);
591         i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
592                            veb->stat_offsets_loaded,
593                            &oes->rx_broadcast, &es->rx_broadcast);
594
595         i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
596                            veb->stat_offsets_loaded,
597                            &oes->tx_bytes, &es->tx_bytes);
598         i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
599                            veb->stat_offsets_loaded,
600                            &oes->tx_unicast, &es->tx_unicast);
601         i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
602                            veb->stat_offsets_loaded,
603                            &oes->tx_multicast, &es->tx_multicast);
604         i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
605                            veb->stat_offsets_loaded,
606                            &oes->tx_broadcast, &es->tx_broadcast);
607         veb->stat_offsets_loaded = true;
608 }
609
610 /**
611  * i40e_update_link_xoff_rx - Update XOFF received in link flow control mode
612  * @pf: the corresponding PF
613  *
614  * Update the Rx XOFF counter (PAUSE frames) in link flow control mode
615  **/
616 static void i40e_update_link_xoff_rx(struct i40e_pf *pf)
617 {
618         struct i40e_hw_port_stats *osd = &pf->stats_offsets;
619         struct i40e_hw_port_stats *nsd = &pf->stats;
620         struct i40e_hw *hw = &pf->hw;
621         u64 xoff = 0;
622         u16 i, v;
623
624         if ((hw->fc.current_mode != I40E_FC_FULL) &&
625             (hw->fc.current_mode != I40E_FC_RX_PAUSE))
626                 return;
627
628         xoff = nsd->link_xoff_rx;
629         i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
630                            pf->stat_offsets_loaded,
631                            &osd->link_xoff_rx, &nsd->link_xoff_rx);
632
633         /* No new LFC xoff rx */
634         if (!(nsd->link_xoff_rx - xoff))
635                 return;
636
637         /* Clear the __I40E_HANG_CHECK_ARMED bit for all Tx rings */
638         for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
639                 struct i40e_vsi *vsi = pf->vsi[v];
640
641                 if (!vsi)
642                         continue;
643
644                 for (i = 0; i < vsi->num_queue_pairs; i++) {
645                         struct i40e_ring *ring = vsi->tx_rings[i];
646                         clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
647                 }
648         }
649 }
650
651 /**
652  * i40e_update_prio_xoff_rx - Update XOFF received in PFC mode
653  * @pf: the corresponding PF
654  *
655  * Update the Rx XOFF counter (PAUSE frames) in PFC mode
656  **/
657 static void i40e_update_prio_xoff_rx(struct i40e_pf *pf)
658 {
659         struct i40e_hw_port_stats *osd = &pf->stats_offsets;
660         struct i40e_hw_port_stats *nsd = &pf->stats;
661         bool xoff[I40E_MAX_TRAFFIC_CLASS] = {false};
662         struct i40e_dcbx_config *dcb_cfg;
663         struct i40e_hw *hw = &pf->hw;
664         u16 i, v;
665         u8 tc;
666
667         dcb_cfg = &hw->local_dcbx_config;
668
669         /* See if DCB enabled with PFC TC */
670         if (!(pf->flags & I40E_FLAG_DCB_ENABLED) ||
671             !(dcb_cfg->pfc.pfcenable)) {
672                 i40e_update_link_xoff_rx(pf);
673                 return;
674         }
675
676         for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
677                 u64 prio_xoff = nsd->priority_xoff_rx[i];
678                 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
679                                    pf->stat_offsets_loaded,
680                                    &osd->priority_xoff_rx[i],
681                                    &nsd->priority_xoff_rx[i]);
682
683                 /* No new PFC xoff rx */
684                 if (!(nsd->priority_xoff_rx[i] - prio_xoff))
685                         continue;
686                 /* Get the TC for given priority */
687                 tc = dcb_cfg->etscfg.prioritytable[i];
688                 xoff[tc] = true;
689         }
690
691         /* Clear the __I40E_HANG_CHECK_ARMED bit for Tx rings */
692         for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
693                 struct i40e_vsi *vsi = pf->vsi[v];
694
695                 if (!vsi)
696                         continue;
697
698                 for (i = 0; i < vsi->num_queue_pairs; i++) {
699                         struct i40e_ring *ring = vsi->tx_rings[i];
700
701                         tc = ring->dcb_tc;
702                         if (xoff[tc])
703                                 clear_bit(__I40E_HANG_CHECK_ARMED,
704                                           &ring->state);
705                 }
706         }
707 }
708
709 /**
710  * i40e_update_stats - Update the board statistics counters.
711  * @vsi: the VSI to be updated
712  *
713  * There are a few instances where we store the same stat in a
714  * couple of different structs.  This is partly because we have
715  * the netdev stats that need to be filled out, which is slightly
716  * different from the "eth_stats" defined by the chip and used in
717  * VF communications.  We sort it all out here in a central place.
718  **/
719 void i40e_update_stats(struct i40e_vsi *vsi)
720 {
721         struct i40e_pf *pf = vsi->back;
722         struct i40e_hw *hw = &pf->hw;
723         struct rtnl_link_stats64 *ons;
724         struct rtnl_link_stats64 *ns;   /* netdev stats */
725         struct i40e_eth_stats *oes;
726         struct i40e_eth_stats *es;     /* device's eth stats */
727         u32 tx_restart, tx_busy;
728         u32 rx_page, rx_buf;
729         u64 rx_p, rx_b;
730         u64 tx_p, tx_b;
731         int i;
732         u16 q;
733
734         if (test_bit(__I40E_DOWN, &vsi->state) ||
735             test_bit(__I40E_CONFIG_BUSY, &pf->state))
736                 return;
737
738         ns = i40e_get_vsi_stats_struct(vsi);
739         ons = &vsi->net_stats_offsets;
740         es = &vsi->eth_stats;
741         oes = &vsi->eth_stats_offsets;
742
743         /* Gather up the netdev and vsi stats that the driver collects
744          * on the fly during packet processing
745          */
746         rx_b = rx_p = 0;
747         tx_b = tx_p = 0;
748         tx_restart = tx_busy = 0;
749         rx_page = 0;
750         rx_buf = 0;
751         rcu_read_lock();
752         for (q = 0; q < vsi->num_queue_pairs; q++) {
753                 struct i40e_ring *p;
754                 u64 bytes, packets;
755                 unsigned int start;
756
757                 /* locate Tx ring */
758                 p = ACCESS_ONCE(vsi->tx_rings[q]);
759
760                 do {
761                         start = u64_stats_fetch_begin_bh(&p->syncp);
762                         packets = p->stats.packets;
763                         bytes = p->stats.bytes;
764                 } while (u64_stats_fetch_retry_bh(&p->syncp, start));
765                 tx_b += bytes;
766                 tx_p += packets;
767                 tx_restart += p->tx_stats.restart_queue;
768                 tx_busy += p->tx_stats.tx_busy;
769
770                 /* Rx queue is part of the same block as Tx queue */
771                 p = &p[1];
772                 do {
773                         start = u64_stats_fetch_begin_bh(&p->syncp);
774                         packets = p->stats.packets;
775                         bytes = p->stats.bytes;
776                 } while (u64_stats_fetch_retry_bh(&p->syncp, start));
777                 rx_b += bytes;
778                 rx_p += packets;
779                 rx_buf += p->rx_stats.alloc_rx_buff_failed;
780                 rx_page += p->rx_stats.alloc_rx_page_failed;
781         }
782         rcu_read_unlock();
783         vsi->tx_restart = tx_restart;
784         vsi->tx_busy = tx_busy;
785         vsi->rx_page_failed = rx_page;
786         vsi->rx_buf_failed = rx_buf;
787
788         ns->rx_packets = rx_p;
789         ns->rx_bytes = rx_b;
790         ns->tx_packets = tx_p;
791         ns->tx_bytes = tx_b;
792
793         i40e_update_eth_stats(vsi);
794         /* update netdev stats from eth stats */
795         ons->rx_errors = oes->rx_errors;
796         ns->rx_errors = es->rx_errors;
797         ons->tx_errors = oes->tx_errors;
798         ns->tx_errors = es->tx_errors;
799         ons->multicast = oes->rx_multicast;
800         ns->multicast = es->rx_multicast;
801         ons->tx_dropped = oes->tx_discards;
802         ns->tx_dropped = es->tx_discards;
803
804         /* Get the port data only if this is the main PF VSI */
805         if (vsi == pf->vsi[pf->lan_vsi]) {
806                 struct i40e_hw_port_stats *nsd = &pf->stats;
807                 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
808
809                 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
810                                    I40E_GLPRT_GORCL(hw->port),
811                                    pf->stat_offsets_loaded,
812                                    &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
813                 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
814                                    I40E_GLPRT_GOTCL(hw->port),
815                                    pf->stat_offsets_loaded,
816                                    &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
817                 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
818                                    pf->stat_offsets_loaded,
819                                    &osd->eth.rx_discards,
820                                    &nsd->eth.rx_discards);
821                 i40e_stat_update32(hw, I40E_GLPRT_TDPC(hw->port),
822                                    pf->stat_offsets_loaded,
823                                    &osd->eth.tx_discards,
824                                    &nsd->eth.tx_discards);
825                 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
826                                    I40E_GLPRT_MPRCL(hw->port),
827                                    pf->stat_offsets_loaded,
828                                    &osd->eth.rx_multicast,
829                                    &nsd->eth.rx_multicast);
830
831                 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
832                                    pf->stat_offsets_loaded,
833                                    &osd->tx_dropped_link_down,
834                                    &nsd->tx_dropped_link_down);
835
836                 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
837                                    pf->stat_offsets_loaded,
838                                    &osd->crc_errors, &nsd->crc_errors);
839                 ns->rx_crc_errors = nsd->crc_errors;
840
841                 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
842                                    pf->stat_offsets_loaded,
843                                    &osd->illegal_bytes, &nsd->illegal_bytes);
844                 ns->rx_errors = nsd->crc_errors
845                                 + nsd->illegal_bytes;
846
847                 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
848                                    pf->stat_offsets_loaded,
849                                    &osd->mac_local_faults,
850                                    &nsd->mac_local_faults);
851                 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
852                                    pf->stat_offsets_loaded,
853                                    &osd->mac_remote_faults,
854                                    &nsd->mac_remote_faults);
855
856                 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
857                                    pf->stat_offsets_loaded,
858                                    &osd->rx_length_errors,
859                                    &nsd->rx_length_errors);
860                 ns->rx_length_errors = nsd->rx_length_errors;
861
862                 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
863                                    pf->stat_offsets_loaded,
864                                    &osd->link_xon_rx, &nsd->link_xon_rx);
865                 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
866                                    pf->stat_offsets_loaded,
867                                    &osd->link_xon_tx, &nsd->link_xon_tx);
868                 i40e_update_prio_xoff_rx(pf);  /* handles I40E_GLPRT_LXOFFRXC */
869                 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
870                                    pf->stat_offsets_loaded,
871                                    &osd->link_xoff_tx, &nsd->link_xoff_tx);
872
873                 for (i = 0; i < 8; i++) {
874                         i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
875                                            pf->stat_offsets_loaded,
876                                            &osd->priority_xon_rx[i],
877                                            &nsd->priority_xon_rx[i]);
878                         i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
879                                            pf->stat_offsets_loaded,
880                                            &osd->priority_xon_tx[i],
881                                            &nsd->priority_xon_tx[i]);
882                         i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
883                                            pf->stat_offsets_loaded,
884                                            &osd->priority_xoff_tx[i],
885                                            &nsd->priority_xoff_tx[i]);
886                         i40e_stat_update32(hw,
887                                            I40E_GLPRT_RXON2OFFCNT(hw->port, i),
888                                            pf->stat_offsets_loaded,
889                                            &osd->priority_xon_2_xoff[i],
890                                            &nsd->priority_xon_2_xoff[i]);
891                 }
892
893                 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
894                                    I40E_GLPRT_PRC64L(hw->port),
895                                    pf->stat_offsets_loaded,
896                                    &osd->rx_size_64, &nsd->rx_size_64);
897                 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
898                                    I40E_GLPRT_PRC127L(hw->port),
899                                    pf->stat_offsets_loaded,
900                                    &osd->rx_size_127, &nsd->rx_size_127);
901                 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
902                                    I40E_GLPRT_PRC255L(hw->port),
903                                    pf->stat_offsets_loaded,
904                                    &osd->rx_size_255, &nsd->rx_size_255);
905                 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
906                                    I40E_GLPRT_PRC511L(hw->port),
907                                    pf->stat_offsets_loaded,
908                                    &osd->rx_size_511, &nsd->rx_size_511);
909                 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
910                                    I40E_GLPRT_PRC1023L(hw->port),
911                                    pf->stat_offsets_loaded,
912                                    &osd->rx_size_1023, &nsd->rx_size_1023);
913                 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
914                                    I40E_GLPRT_PRC1522L(hw->port),
915                                    pf->stat_offsets_loaded,
916                                    &osd->rx_size_1522, &nsd->rx_size_1522);
917                 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
918                                    I40E_GLPRT_PRC9522L(hw->port),
919                                    pf->stat_offsets_loaded,
920                                    &osd->rx_size_big, &nsd->rx_size_big);
921
922                 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
923                                    I40E_GLPRT_PTC64L(hw->port),
924                                    pf->stat_offsets_loaded,
925                                    &osd->tx_size_64, &nsd->tx_size_64);
926                 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
927                                    I40E_GLPRT_PTC127L(hw->port),
928                                    pf->stat_offsets_loaded,
929                                    &osd->tx_size_127, &nsd->tx_size_127);
930                 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
931                                    I40E_GLPRT_PTC255L(hw->port),
932                                    pf->stat_offsets_loaded,
933                                    &osd->tx_size_255, &nsd->tx_size_255);
934                 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
935                                    I40E_GLPRT_PTC511L(hw->port),
936                                    pf->stat_offsets_loaded,
937                                    &osd->tx_size_511, &nsd->tx_size_511);
938                 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
939                                    I40E_GLPRT_PTC1023L(hw->port),
940                                    pf->stat_offsets_loaded,
941                                    &osd->tx_size_1023, &nsd->tx_size_1023);
942                 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
943                                    I40E_GLPRT_PTC1522L(hw->port),
944                                    pf->stat_offsets_loaded,
945                                    &osd->tx_size_1522, &nsd->tx_size_1522);
946                 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
947                                    I40E_GLPRT_PTC9522L(hw->port),
948                                    pf->stat_offsets_loaded,
949                                    &osd->tx_size_big, &nsd->tx_size_big);
950
951                 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
952                                    pf->stat_offsets_loaded,
953                                    &osd->rx_undersize, &nsd->rx_undersize);
954                 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
955                                    pf->stat_offsets_loaded,
956                                    &osd->rx_fragments, &nsd->rx_fragments);
957                 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
958                                    pf->stat_offsets_loaded,
959                                    &osd->rx_oversize, &nsd->rx_oversize);
960                 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
961                                    pf->stat_offsets_loaded,
962                                    &osd->rx_jabber, &nsd->rx_jabber);
963         }
964
965         pf->stat_offsets_loaded = true;
966 }
967
968 /**
969  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
970  * @vsi: the VSI to be searched
971  * @macaddr: the MAC address
972  * @vlan: the vlan
973  * @is_vf: make sure its a vf filter, else doesn't matter
974  * @is_netdev: make sure its a netdev filter, else doesn't matter
975  *
976  * Returns ptr to the filter object or NULL
977  **/
978 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
979                                                 u8 *macaddr, s16 vlan,
980                                                 bool is_vf, bool is_netdev)
981 {
982         struct i40e_mac_filter *f;
983
984         if (!vsi || !macaddr)
985                 return NULL;
986
987         list_for_each_entry(f, &vsi->mac_filter_list, list) {
988                 if ((ether_addr_equal(macaddr, f->macaddr)) &&
989                     (vlan == f->vlan)    &&
990                     (!is_vf || f->is_vf) &&
991                     (!is_netdev || f->is_netdev))
992                         return f;
993         }
994         return NULL;
995 }
996
997 /**
998  * i40e_find_mac - Find a mac addr in the macvlan filters list
999  * @vsi: the VSI to be searched
1000  * @macaddr: the MAC address we are searching for
1001  * @is_vf: make sure its a vf filter, else doesn't matter
1002  * @is_netdev: make sure its a netdev filter, else doesn't matter
1003  *
1004  * Returns the first filter with the provided MAC address or NULL if
1005  * MAC address was not found
1006  **/
1007 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, u8 *macaddr,
1008                                       bool is_vf, bool is_netdev)
1009 {
1010         struct i40e_mac_filter *f;
1011
1012         if (!vsi || !macaddr)
1013                 return NULL;
1014
1015         list_for_each_entry(f, &vsi->mac_filter_list, list) {
1016                 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1017                     (!is_vf || f->is_vf) &&
1018                     (!is_netdev || f->is_netdev))
1019                         return f;
1020         }
1021         return NULL;
1022 }
1023
1024 /**
1025  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1026  * @vsi: the VSI to be searched
1027  *
1028  * Returns true if VSI is in vlan mode or false otherwise
1029  **/
1030 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1031 {
1032         struct i40e_mac_filter *f;
1033
1034         /* Only -1 for all the filters denotes not in vlan mode
1035          * so we have to go through all the list in order to make sure
1036          */
1037         list_for_each_entry(f, &vsi->mac_filter_list, list) {
1038                 if (f->vlan >= 0)
1039                         return true;
1040         }
1041
1042         return false;
1043 }
1044
1045 /**
1046  * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
1047  * @vsi: the VSI to be searched
1048  * @macaddr: the mac address to be filtered
1049  * @is_vf: true if it is a vf
1050  * @is_netdev: true if it is a netdev
1051  *
1052  * Goes through all the macvlan filters and adds a
1053  * macvlan filter for each unique vlan that already exists
1054  *
1055  * Returns first filter found on success, else NULL
1056  **/
1057 struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi, u8 *macaddr,
1058                                              bool is_vf, bool is_netdev)
1059 {
1060         struct i40e_mac_filter *f;
1061
1062         list_for_each_entry(f, &vsi->mac_filter_list, list) {
1063                 if (!i40e_find_filter(vsi, macaddr, f->vlan,
1064                                       is_vf, is_netdev)) {
1065                         if (!i40e_add_filter(vsi, macaddr, f->vlan,
1066                                                 is_vf, is_netdev))
1067                                 return NULL;
1068                 }
1069         }
1070
1071         return list_first_entry_or_null(&vsi->mac_filter_list,
1072                                         struct i40e_mac_filter, list);
1073 }
1074
1075 /**
1076  * i40e_add_filter - Add a mac/vlan filter to the VSI
1077  * @vsi: the VSI to be searched
1078  * @macaddr: the MAC address
1079  * @vlan: the vlan
1080  * @is_vf: make sure its a vf filter, else doesn't matter
1081  * @is_netdev: make sure its a netdev filter, else doesn't matter
1082  *
1083  * Returns ptr to the filter object or NULL when no memory available.
1084  **/
1085 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1086                                         u8 *macaddr, s16 vlan,
1087                                         bool is_vf, bool is_netdev)
1088 {
1089         struct i40e_mac_filter *f;
1090
1091         if (!vsi || !macaddr)
1092                 return NULL;
1093
1094         f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1095         if (!f) {
1096                 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1097                 if (!f)
1098                         goto add_filter_out;
1099
1100                 memcpy(f->macaddr, macaddr, ETH_ALEN);
1101                 f->vlan = vlan;
1102                 f->changed = true;
1103
1104                 INIT_LIST_HEAD(&f->list);
1105                 list_add(&f->list, &vsi->mac_filter_list);
1106         }
1107
1108         /* increment counter and add a new flag if needed */
1109         if (is_vf) {
1110                 if (!f->is_vf) {
1111                         f->is_vf = true;
1112                         f->counter++;
1113                 }
1114         } else if (is_netdev) {
1115                 if (!f->is_netdev) {
1116                         f->is_netdev = true;
1117                         f->counter++;
1118                 }
1119         } else {
1120                 f->counter++;
1121         }
1122
1123         /* changed tells sync_filters_subtask to
1124          * push the filter down to the firmware
1125          */
1126         if (f->changed) {
1127                 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1128                 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1129         }
1130
1131 add_filter_out:
1132         return f;
1133 }
1134
1135 /**
1136  * i40e_del_filter - Remove a mac/vlan filter from the VSI
1137  * @vsi: the VSI to be searched
1138  * @macaddr: the MAC address
1139  * @vlan: the vlan
1140  * @is_vf: make sure it's a vf filter, else doesn't matter
1141  * @is_netdev: make sure it's a netdev filter, else doesn't matter
1142  **/
1143 void i40e_del_filter(struct i40e_vsi *vsi,
1144                      u8 *macaddr, s16 vlan,
1145                      bool is_vf, bool is_netdev)
1146 {
1147         struct i40e_mac_filter *f;
1148
1149         if (!vsi || !macaddr)
1150                 return;
1151
1152         f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1153         if (!f || f->counter == 0)
1154                 return;
1155
1156         if (is_vf) {
1157                 if (f->is_vf) {
1158                         f->is_vf = false;
1159                         f->counter--;
1160                 }
1161         } else if (is_netdev) {
1162                 if (f->is_netdev) {
1163                         f->is_netdev = false;
1164                         f->counter--;
1165                 }
1166         } else {
1167                 /* make sure we don't remove a filter in use by vf or netdev */
1168                 int min_f = 0;
1169                 min_f += (f->is_vf ? 1 : 0);
1170                 min_f += (f->is_netdev ? 1 : 0);
1171
1172                 if (f->counter > min_f)
1173                         f->counter--;
1174         }
1175
1176         /* counter == 0 tells sync_filters_subtask to
1177          * remove the filter from the firmware's list
1178          */
1179         if (f->counter == 0) {
1180                 f->changed = true;
1181                 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1182                 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1183         }
1184 }
1185
1186 /**
1187  * i40e_set_mac - NDO callback to set mac address
1188  * @netdev: network interface device structure
1189  * @p: pointer to an address structure
1190  *
1191  * Returns 0 on success, negative on failure
1192  **/
1193 static int i40e_set_mac(struct net_device *netdev, void *p)
1194 {
1195         struct i40e_netdev_priv *np = netdev_priv(netdev);
1196         struct i40e_vsi *vsi = np->vsi;
1197         struct sockaddr *addr = p;
1198         struct i40e_mac_filter *f;
1199
1200         if (!is_valid_ether_addr(addr->sa_data))
1201                 return -EADDRNOTAVAIL;
1202
1203         netdev_info(netdev, "set mac address=%pM\n", addr->sa_data);
1204
1205         if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
1206                 return 0;
1207
1208         if (vsi->type == I40E_VSI_MAIN) {
1209                 i40e_status ret;
1210                 ret = i40e_aq_mac_address_write(&vsi->back->hw,
1211                                                 I40E_AQC_WRITE_TYPE_LAA_ONLY,
1212                                                 addr->sa_data, NULL);
1213                 if (ret) {
1214                         netdev_info(netdev,
1215                                     "Addr change for Main VSI failed: %d\n",
1216                                     ret);
1217                         return -EADDRNOTAVAIL;
1218                 }
1219
1220                 memcpy(vsi->back->hw.mac.addr, addr->sa_data, netdev->addr_len);
1221         }
1222
1223         /* In order to be sure to not drop any packets, add the new address
1224          * then delete the old one.
1225          */
1226         f = i40e_add_filter(vsi, addr->sa_data, I40E_VLAN_ANY, false, false);
1227         if (!f)
1228                 return -ENOMEM;
1229
1230         i40e_sync_vsi_filters(vsi);
1231         i40e_del_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY, false, false);
1232         i40e_sync_vsi_filters(vsi);
1233
1234         memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1235
1236         return 0;
1237 }
1238
1239 /**
1240  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1241  * @vsi: the VSI being setup
1242  * @ctxt: VSI context structure
1243  * @enabled_tc: Enabled TCs bitmap
1244  * @is_add: True if called before Add VSI
1245  *
1246  * Setup VSI queue mapping for enabled traffic classes.
1247  **/
1248 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1249                                      struct i40e_vsi_context *ctxt,
1250                                      u8 enabled_tc,
1251                                      bool is_add)
1252 {
1253         struct i40e_pf *pf = vsi->back;
1254         u16 sections = 0;
1255         u8 netdev_tc = 0;
1256         u16 numtc = 0;
1257         u16 qcount;
1258         u8 offset;
1259         u16 qmap;
1260         int i;
1261
1262         sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1263         offset = 0;
1264
1265         if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1266                 /* Find numtc from enabled TC bitmap */
1267                 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1268                         if (enabled_tc & (1 << i)) /* TC is enabled */
1269                                 numtc++;
1270                 }
1271                 if (!numtc) {
1272                         dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1273                         numtc = 1;
1274                 }
1275         } else {
1276                 /* At least TC0 is enabled in case of non-DCB case */
1277                 numtc = 1;
1278         }
1279
1280         vsi->tc_config.numtc = numtc;
1281         vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1282
1283         /* Setup queue offset/count for all TCs for given VSI */
1284         for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1285                 /* See if the given TC is enabled for the given VSI */
1286                 if (vsi->tc_config.enabled_tc & (1 << i)) { /* TC is enabled */
1287                         int pow, num_qps;
1288
1289                         vsi->tc_config.tc_info[i].qoffset = offset;
1290                         switch (vsi->type) {
1291                         case I40E_VSI_MAIN:
1292                                 if (i == 0)
1293                                         qcount = pf->rss_size;
1294                                 else
1295                                         qcount = pf->num_tc_qps;
1296                                 vsi->tc_config.tc_info[i].qcount = qcount;
1297                                 break;
1298                         case I40E_VSI_FDIR:
1299                         case I40E_VSI_SRIOV:
1300                         case I40E_VSI_VMDQ2:
1301                         default:
1302                                 qcount = vsi->alloc_queue_pairs;
1303                                 vsi->tc_config.tc_info[i].qcount = qcount;
1304                                 WARN_ON(i != 0);
1305                                 break;
1306                         }
1307
1308                         /* find the power-of-2 of the number of queue pairs */
1309                         num_qps = vsi->tc_config.tc_info[i].qcount;
1310                         pow = 0;
1311                         while (num_qps &&
1312                               ((1 << pow) < vsi->tc_config.tc_info[i].qcount)) {
1313                                 pow++;
1314                                 num_qps >>= 1;
1315                         }
1316
1317                         vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1318                         qmap =
1319                             (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1320                             (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1321
1322                         offset += vsi->tc_config.tc_info[i].qcount;
1323                 } else {
1324                         /* TC is not enabled so set the offset to
1325                          * default queue and allocate one queue
1326                          * for the given TC.
1327                          */
1328                         vsi->tc_config.tc_info[i].qoffset = 0;
1329                         vsi->tc_config.tc_info[i].qcount = 1;
1330                         vsi->tc_config.tc_info[i].netdev_tc = 0;
1331
1332                         qmap = 0;
1333                 }
1334                 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1335         }
1336
1337         /* Set actual Tx/Rx queue pairs */
1338         vsi->num_queue_pairs = offset;
1339
1340         /* Scheduler section valid can only be set for ADD VSI */
1341         if (is_add) {
1342                 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1343
1344                 ctxt->info.up_enable_bits = enabled_tc;
1345         }
1346         if (vsi->type == I40E_VSI_SRIOV) {
1347                 ctxt->info.mapping_flags |=
1348                                      cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1349                 for (i = 0; i < vsi->num_queue_pairs; i++)
1350                         ctxt->info.queue_mapping[i] =
1351                                                cpu_to_le16(vsi->base_queue + i);
1352         } else {
1353                 ctxt->info.mapping_flags |=
1354                                         cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1355                 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1356         }
1357         ctxt->info.valid_sections |= cpu_to_le16(sections);
1358 }
1359
1360 /**
1361  * i40e_set_rx_mode - NDO callback to set the netdev filters
1362  * @netdev: network interface device structure
1363  **/
1364 static void i40e_set_rx_mode(struct net_device *netdev)
1365 {
1366         struct i40e_netdev_priv *np = netdev_priv(netdev);
1367         struct i40e_mac_filter *f, *ftmp;
1368         struct i40e_vsi *vsi = np->vsi;
1369         struct netdev_hw_addr *uca;
1370         struct netdev_hw_addr *mca;
1371         struct netdev_hw_addr *ha;
1372
1373         /* add addr if not already in the filter list */
1374         netdev_for_each_uc_addr(uca, netdev) {
1375                 if (!i40e_find_mac(vsi, uca->addr, false, true)) {
1376                         if (i40e_is_vsi_in_vlan(vsi))
1377                                 i40e_put_mac_in_vlan(vsi, uca->addr,
1378                                                      false, true);
1379                         else
1380                                 i40e_add_filter(vsi, uca->addr, I40E_VLAN_ANY,
1381                                                 false, true);
1382                 }
1383         }
1384
1385         netdev_for_each_mc_addr(mca, netdev) {
1386                 if (!i40e_find_mac(vsi, mca->addr, false, true)) {
1387                         if (i40e_is_vsi_in_vlan(vsi))
1388                                 i40e_put_mac_in_vlan(vsi, mca->addr,
1389                                                      false, true);
1390                         else
1391                                 i40e_add_filter(vsi, mca->addr, I40E_VLAN_ANY,
1392                                                 false, true);
1393                 }
1394         }
1395
1396         /* remove filter if not in netdev list */
1397         list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1398                 bool found = false;
1399
1400                 if (!f->is_netdev)
1401                         continue;
1402
1403                 if (is_multicast_ether_addr(f->macaddr)) {
1404                         netdev_for_each_mc_addr(mca, netdev) {
1405                                 if (ether_addr_equal(mca->addr, f->macaddr)) {
1406                                         found = true;
1407                                         break;
1408                                 }
1409                         }
1410                 } else {
1411                         netdev_for_each_uc_addr(uca, netdev) {
1412                                 if (ether_addr_equal(uca->addr, f->macaddr)) {
1413                                         found = true;
1414                                         break;
1415                                 }
1416                         }
1417
1418                         for_each_dev_addr(netdev, ha) {
1419                                 if (ether_addr_equal(ha->addr, f->macaddr)) {
1420                                         found = true;
1421                                         break;
1422                                 }
1423                         }
1424                 }
1425                 if (!found)
1426                         i40e_del_filter(
1427                            vsi, f->macaddr, I40E_VLAN_ANY, false, true);
1428         }
1429
1430         /* check for other flag changes */
1431         if (vsi->current_netdev_flags != vsi->netdev->flags) {
1432                 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1433                 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1434         }
1435 }
1436
1437 /**
1438  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1439  * @vsi: ptr to the VSI
1440  *
1441  * Push any outstanding VSI filter changes through the AdminQ.
1442  *
1443  * Returns 0 or error value
1444  **/
1445 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
1446 {
1447         struct i40e_mac_filter *f, *ftmp;
1448         bool promisc_forced_on = false;
1449         bool add_happened = false;
1450         int filter_list_len = 0;
1451         u32 changed_flags = 0;
1452         i40e_status aq_ret = 0;
1453         struct i40e_pf *pf;
1454         int num_add = 0;
1455         int num_del = 0;
1456         u16 cmd_flags;
1457
1458         /* empty array typed pointers, kcalloc later */
1459         struct i40e_aqc_add_macvlan_element_data *add_list;
1460         struct i40e_aqc_remove_macvlan_element_data *del_list;
1461
1462         while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1463                 usleep_range(1000, 2000);
1464         pf = vsi->back;
1465
1466         if (vsi->netdev) {
1467                 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1468                 vsi->current_netdev_flags = vsi->netdev->flags;
1469         }
1470
1471         if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1472                 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1473
1474                 filter_list_len = pf->hw.aq.asq_buf_size /
1475                             sizeof(struct i40e_aqc_remove_macvlan_element_data);
1476                 del_list = kcalloc(filter_list_len,
1477                             sizeof(struct i40e_aqc_remove_macvlan_element_data),
1478                             GFP_KERNEL);
1479                 if (!del_list)
1480                         return -ENOMEM;
1481
1482                 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1483                         if (!f->changed)
1484                                 continue;
1485
1486                         if (f->counter != 0)
1487                                 continue;
1488                         f->changed = false;
1489                         cmd_flags = 0;
1490
1491                         /* add to delete list */
1492                         memcpy(del_list[num_del].mac_addr,
1493                                f->macaddr, ETH_ALEN);
1494                         del_list[num_del].vlan_tag =
1495                                 cpu_to_le16((u16)(f->vlan ==
1496                                             I40E_VLAN_ANY ? 0 : f->vlan));
1497
1498                         /* vlan0 as wild card to allow packets from all vlans */
1499                         if (f->vlan == I40E_VLAN_ANY ||
1500                             (vsi->netdev && !(vsi->netdev->features &
1501                                               NETIF_F_HW_VLAN_CTAG_FILTER)))
1502                                 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1503                         cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1504                         del_list[num_del].flags = cmd_flags;
1505                         num_del++;
1506
1507                         /* unlink from filter list */
1508                         list_del(&f->list);
1509                         kfree(f);
1510
1511                         /* flush a full buffer */
1512                         if (num_del == filter_list_len) {
1513                                 aq_ret = i40e_aq_remove_macvlan(&pf->hw,
1514                                             vsi->seid, del_list, num_del,
1515                                             NULL);
1516                                 num_del = 0;
1517                                 memset(del_list, 0, sizeof(*del_list));
1518
1519                                 if (aq_ret)
1520                                         dev_info(&pf->pdev->dev,
1521                                                  "ignoring delete macvlan error, err %d, aq_err %d while flushing a full buffer\n",
1522                                                  aq_ret,
1523                                                  pf->hw.aq.asq_last_status);
1524                         }
1525                 }
1526                 if (num_del) {
1527                         aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid,
1528                                                      del_list, num_del, NULL);
1529                         num_del = 0;
1530
1531                         if (aq_ret)
1532                                 dev_info(&pf->pdev->dev,
1533                                          "ignoring delete macvlan error, err %d, aq_err %d\n",
1534                                          aq_ret, pf->hw.aq.asq_last_status);
1535                 }
1536
1537                 kfree(del_list);
1538                 del_list = NULL;
1539
1540                 /* do all the adds now */
1541                 filter_list_len = pf->hw.aq.asq_buf_size /
1542                                sizeof(struct i40e_aqc_add_macvlan_element_data),
1543                 add_list = kcalloc(filter_list_len,
1544                                sizeof(struct i40e_aqc_add_macvlan_element_data),
1545                                GFP_KERNEL);
1546                 if (!add_list)
1547                         return -ENOMEM;
1548
1549                 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1550                         if (!f->changed)
1551                                 continue;
1552
1553                         if (f->counter == 0)
1554                                 continue;
1555                         f->changed = false;
1556                         add_happened = true;
1557                         cmd_flags = 0;
1558
1559                         /* add to add array */
1560                         memcpy(add_list[num_add].mac_addr,
1561                                f->macaddr, ETH_ALEN);
1562                         add_list[num_add].vlan_tag =
1563                                 cpu_to_le16(
1564                                  (u16)(f->vlan == I40E_VLAN_ANY ? 0 : f->vlan));
1565                         add_list[num_add].queue_number = 0;
1566
1567                         cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
1568
1569                         /* vlan0 as wild card to allow packets from all vlans */
1570                         if (f->vlan == I40E_VLAN_ANY || (vsi->netdev &&
1571                             !(vsi->netdev->features &
1572                                                  NETIF_F_HW_VLAN_CTAG_FILTER)))
1573                                 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
1574                         add_list[num_add].flags = cpu_to_le16(cmd_flags);
1575                         num_add++;
1576
1577                         /* flush a full buffer */
1578                         if (num_add == filter_list_len) {
1579                                 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1580                                                              add_list, num_add,
1581                                                              NULL);
1582                                 num_add = 0;
1583
1584                                 if (aq_ret)
1585                                         break;
1586                                 memset(add_list, 0, sizeof(*add_list));
1587                         }
1588                 }
1589                 if (num_add) {
1590                         aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1591                                                      add_list, num_add, NULL);
1592                         num_add = 0;
1593                 }
1594                 kfree(add_list);
1595                 add_list = NULL;
1596
1597                 if (add_happened && (!aq_ret)) {
1598                         /* do nothing */;
1599                 } else if (add_happened && (aq_ret)) {
1600                         dev_info(&pf->pdev->dev,
1601                                  "add filter failed, err %d, aq_err %d\n",
1602                                  aq_ret, pf->hw.aq.asq_last_status);
1603                         if ((pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOSPC) &&
1604                             !test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1605                                       &vsi->state)) {
1606                                 promisc_forced_on = true;
1607                                 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1608                                         &vsi->state);
1609                                 dev_info(&pf->pdev->dev, "promiscuous mode forced on\n");
1610                         }
1611                 }
1612         }
1613
1614         /* check for changes in promiscuous modes */
1615         if (changed_flags & IFF_ALLMULTI) {
1616                 bool cur_multipromisc;
1617                 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
1618                 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
1619                                                                vsi->seid,
1620                                                                cur_multipromisc,
1621                                                                NULL);
1622                 if (aq_ret)
1623                         dev_info(&pf->pdev->dev,
1624                                  "set multi promisc failed, err %d, aq_err %d\n",
1625                                  aq_ret, pf->hw.aq.asq_last_status);
1626         }
1627         if ((changed_flags & IFF_PROMISC) || promisc_forced_on) {
1628                 bool cur_promisc;
1629                 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
1630                                test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1631                                         &vsi->state));
1632                 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(&vsi->back->hw,
1633                                                              vsi->seid,
1634                                                              cur_promisc, NULL);
1635                 if (aq_ret)
1636                         dev_info(&pf->pdev->dev,
1637                                  "set uni promisc failed, err %d, aq_err %d\n",
1638                                  aq_ret, pf->hw.aq.asq_last_status);
1639         }
1640
1641         clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
1642         return 0;
1643 }
1644
1645 /**
1646  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
1647  * @pf: board private structure
1648  **/
1649 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
1650 {
1651         int v;
1652
1653         if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
1654                 return;
1655         pf->flags &= ~I40E_FLAG_FILTER_SYNC;
1656
1657         for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
1658                 if (pf->vsi[v] &&
1659                     (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED))
1660                         i40e_sync_vsi_filters(pf->vsi[v]);
1661         }
1662 }
1663
1664 /**
1665  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
1666  * @netdev: network interface device structure
1667  * @new_mtu: new value for maximum frame size
1668  *
1669  * Returns 0 on success, negative on failure
1670  **/
1671 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
1672 {
1673         struct i40e_netdev_priv *np = netdev_priv(netdev);
1674         int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
1675         struct i40e_vsi *vsi = np->vsi;
1676
1677         /* MTU < 68 is an error and causes problems on some kernels */
1678         if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
1679                 return -EINVAL;
1680
1681         netdev_info(netdev, "changing MTU from %d to %d\n",
1682                     netdev->mtu, new_mtu);
1683         netdev->mtu = new_mtu;
1684         if (netif_running(netdev))
1685                 i40e_vsi_reinit_locked(vsi);
1686
1687         return 0;
1688 }
1689
1690 /**
1691  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
1692  * @vsi: the vsi being adjusted
1693  **/
1694 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
1695 {
1696         struct i40e_vsi_context ctxt;
1697         i40e_status ret;
1698
1699         if ((vsi->info.valid_sections &
1700              cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1701             ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
1702                 return;  /* already enabled */
1703
1704         vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1705         vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1706                                     I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
1707
1708         ctxt.seid = vsi->seid;
1709         memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1710         ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1711         if (ret) {
1712                 dev_info(&vsi->back->pdev->dev,
1713                          "%s: update vsi failed, aq_err=%d\n",
1714                          __func__, vsi->back->hw.aq.asq_last_status);
1715         }
1716 }
1717
1718 /**
1719  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
1720  * @vsi: the vsi being adjusted
1721  **/
1722 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
1723 {
1724         struct i40e_vsi_context ctxt;
1725         i40e_status ret;
1726
1727         if ((vsi->info.valid_sections &
1728              cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1729             ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
1730              I40E_AQ_VSI_PVLAN_EMOD_MASK))
1731                 return;  /* already disabled */
1732
1733         vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1734         vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1735                                     I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
1736
1737         ctxt.seid = vsi->seid;
1738         memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1739         ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1740         if (ret) {
1741                 dev_info(&vsi->back->pdev->dev,
1742                          "%s: update vsi failed, aq_err=%d\n",
1743                          __func__, vsi->back->hw.aq.asq_last_status);
1744         }
1745 }
1746
1747 /**
1748  * i40e_vlan_rx_register - Setup or shutdown vlan offload
1749  * @netdev: network interface to be adjusted
1750  * @features: netdev features to test if VLAN offload is enabled or not
1751  **/
1752 static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
1753 {
1754         struct i40e_netdev_priv *np = netdev_priv(netdev);
1755         struct i40e_vsi *vsi = np->vsi;
1756
1757         if (features & NETIF_F_HW_VLAN_CTAG_RX)
1758                 i40e_vlan_stripping_enable(vsi);
1759         else
1760                 i40e_vlan_stripping_disable(vsi);
1761 }
1762
1763 /**
1764  * i40e_vsi_add_vlan - Add vsi membership for given vlan
1765  * @vsi: the vsi being configured
1766  * @vid: vlan id to be added (0 = untagged only , -1 = any)
1767  **/
1768 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
1769 {
1770         struct i40e_mac_filter *f, *add_f;
1771         bool is_netdev, is_vf;
1772         int ret;
1773
1774         is_vf = (vsi->type == I40E_VSI_SRIOV);
1775         is_netdev = !!(vsi->netdev);
1776
1777         if (is_netdev) {
1778                 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid,
1779                                         is_vf, is_netdev);
1780                 if (!add_f) {
1781                         dev_info(&vsi->back->pdev->dev,
1782                                  "Could not add vlan filter %d for %pM\n",
1783                                  vid, vsi->netdev->dev_addr);
1784                         return -ENOMEM;
1785                 }
1786         }
1787
1788         list_for_each_entry(f, &vsi->mac_filter_list, list) {
1789                 add_f = i40e_add_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
1790                 if (!add_f) {
1791                         dev_info(&vsi->back->pdev->dev,
1792                                  "Could not add vlan filter %d for %pM\n",
1793                                  vid, f->macaddr);
1794                         return -ENOMEM;
1795                 }
1796         }
1797
1798         ret = i40e_sync_vsi_filters(vsi);
1799         if (ret) {
1800                 dev_info(&vsi->back->pdev->dev,
1801                          "Could not sync filters for vid %d\n", vid);
1802                 return ret;
1803         }
1804
1805         /* Now if we add a vlan tag, make sure to check if it is the first
1806          * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
1807          * with 0, so we now accept untagged and specified tagged traffic
1808          * (and not any taged and untagged)
1809          */
1810         if (vid > 0) {
1811                 if (is_netdev && i40e_find_filter(vsi, vsi->netdev->dev_addr,
1812                                                   I40E_VLAN_ANY,
1813                                                   is_vf, is_netdev)) {
1814                         i40e_del_filter(vsi, vsi->netdev->dev_addr,
1815                                         I40E_VLAN_ANY, is_vf, is_netdev);
1816                         add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0,
1817                                                 is_vf, is_netdev);
1818                         if (!add_f) {
1819                                 dev_info(&vsi->back->pdev->dev,
1820                                          "Could not add filter 0 for %pM\n",
1821                                          vsi->netdev->dev_addr);
1822                                 return -ENOMEM;
1823                         }
1824                 }
1825
1826                 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1827                         if (i40e_find_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1828                                              is_vf, is_netdev)) {
1829                                 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1830                                                 is_vf, is_netdev);
1831                                 add_f = i40e_add_filter(vsi, f->macaddr,
1832                                                         0, is_vf, is_netdev);
1833                                 if (!add_f) {
1834                                         dev_info(&vsi->back->pdev->dev,
1835                                                  "Could not add filter 0 for %pM\n",
1836                                                  f->macaddr);
1837                                         return -ENOMEM;
1838                                 }
1839                         }
1840                 }
1841                 ret = i40e_sync_vsi_filters(vsi);
1842         }
1843
1844         return ret;
1845 }
1846
1847 /**
1848  * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
1849  * @vsi: the vsi being configured
1850  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
1851  *
1852  * Return: 0 on success or negative otherwise
1853  **/
1854 int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
1855 {
1856         struct net_device *netdev = vsi->netdev;
1857         struct i40e_mac_filter *f, *add_f;
1858         bool is_vf, is_netdev;
1859         int filter_count = 0;
1860         int ret;
1861
1862         is_vf = (vsi->type == I40E_VSI_SRIOV);
1863         is_netdev = !!(netdev);
1864
1865         if (is_netdev)
1866                 i40e_del_filter(vsi, netdev->dev_addr, vid, is_vf, is_netdev);
1867
1868         list_for_each_entry(f, &vsi->mac_filter_list, list)
1869                 i40e_del_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
1870
1871         ret = i40e_sync_vsi_filters(vsi);
1872         if (ret) {
1873                 dev_info(&vsi->back->pdev->dev, "Could not sync filters\n");
1874                 return ret;
1875         }
1876
1877         /* go through all the filters for this VSI and if there is only
1878          * vid == 0 it means there are no other filters, so vid 0 must
1879          * be replaced with -1. This signifies that we should from now
1880          * on accept any traffic (with any tag present, or untagged)
1881          */
1882         list_for_each_entry(f, &vsi->mac_filter_list, list) {
1883                 if (is_netdev) {
1884                         if (f->vlan &&
1885                             ether_addr_equal(netdev->dev_addr, f->macaddr))
1886                                 filter_count++;
1887                 }
1888
1889                 if (f->vlan)
1890                         filter_count++;
1891         }
1892
1893         if (!filter_count && is_netdev) {
1894                 i40e_del_filter(vsi, netdev->dev_addr, 0, is_vf, is_netdev);
1895                 f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
1896                                     is_vf, is_netdev);
1897                 if (!f) {
1898                         dev_info(&vsi->back->pdev->dev,
1899                                  "Could not add filter %d for %pM\n",
1900                                  I40E_VLAN_ANY, netdev->dev_addr);
1901                         return -ENOMEM;
1902                 }
1903         }
1904
1905         if (!filter_count) {
1906                 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1907                         i40e_del_filter(vsi, f->macaddr, 0, is_vf, is_netdev);
1908                         add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1909                                             is_vf, is_netdev);
1910                         if (!add_f) {
1911                                 dev_info(&vsi->back->pdev->dev,
1912                                          "Could not add filter %d for %pM\n",
1913                                          I40E_VLAN_ANY, f->macaddr);
1914                                 return -ENOMEM;
1915                         }
1916                 }
1917         }
1918
1919         return i40e_sync_vsi_filters(vsi);
1920 }
1921
1922 /**
1923  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
1924  * @netdev: network interface to be adjusted
1925  * @vid: vlan id to be added
1926  *
1927  * net_device_ops implementation for adding vlan ids
1928  **/
1929 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
1930                                 __always_unused __be16 proto, u16 vid)
1931 {
1932         struct i40e_netdev_priv *np = netdev_priv(netdev);
1933         struct i40e_vsi *vsi = np->vsi;
1934         int ret = 0;
1935
1936         if (vid > 4095)
1937                 return -EINVAL;
1938
1939         netdev_info(netdev, "adding %pM vid=%d\n", netdev->dev_addr, vid);
1940
1941         /* If the network stack called us with vid = 0, we should
1942          * indicate to i40e_vsi_add_vlan() that we want to receive
1943          * any traffic (i.e. with any vlan tag, or untagged)
1944          */
1945         ret = i40e_vsi_add_vlan(vsi, vid ? vid : I40E_VLAN_ANY);
1946
1947         if (!ret && (vid < VLAN_N_VID))
1948                 set_bit(vid, vsi->active_vlans);
1949
1950         return ret;
1951 }
1952
1953 /**
1954  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
1955  * @netdev: network interface to be adjusted
1956  * @vid: vlan id to be removed
1957  *
1958  * net_device_ops implementation for adding vlan ids
1959  **/
1960 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
1961                                  __always_unused __be16 proto, u16 vid)
1962 {
1963         struct i40e_netdev_priv *np = netdev_priv(netdev);
1964         struct i40e_vsi *vsi = np->vsi;
1965
1966         netdev_info(netdev, "removing %pM vid=%d\n", netdev->dev_addr, vid);
1967
1968         /* return code is ignored as there is nothing a user
1969          * can do about failure to remove and a log message was
1970          * already printed from the other function
1971          */
1972         i40e_vsi_kill_vlan(vsi, vid);
1973
1974         clear_bit(vid, vsi->active_vlans);
1975
1976         return 0;
1977 }
1978
1979 /**
1980  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
1981  * @vsi: the vsi being brought back up
1982  **/
1983 static void i40e_restore_vlan(struct i40e_vsi *vsi)
1984 {
1985         u16 vid;
1986
1987         if (!vsi->netdev)
1988                 return;
1989
1990         i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
1991
1992         for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
1993                 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
1994                                      vid);
1995 }
1996
1997 /**
1998  * i40e_vsi_add_pvid - Add pvid for the VSI
1999  * @vsi: the vsi being adjusted
2000  * @vid: the vlan id to set as a PVID
2001  **/
2002 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
2003 {
2004         struct i40e_vsi_context ctxt;
2005         i40e_status aq_ret;
2006
2007         vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2008         vsi->info.pvid = cpu_to_le16(vid);
2009         vsi->info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_INSERT_PVID;
2010         vsi->info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_UNTAGGED;
2011
2012         ctxt.seid = vsi->seid;
2013         memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
2014         aq_ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2015         if (aq_ret) {
2016                 dev_info(&vsi->back->pdev->dev,
2017                          "%s: update vsi failed, aq_err=%d\n",
2018                          __func__, vsi->back->hw.aq.asq_last_status);
2019                 return -ENOENT;
2020         }
2021
2022         return 0;
2023 }
2024
2025 /**
2026  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2027  * @vsi: the vsi being adjusted
2028  *
2029  * Just use the vlan_rx_register() service to put it back to normal
2030  **/
2031 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2032 {
2033         vsi->info.pvid = 0;
2034         i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2035 }
2036
2037 /**
2038  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2039  * @vsi: ptr to the VSI
2040  *
2041  * If this function returns with an error, then it's possible one or
2042  * more of the rings is populated (while the rest are not).  It is the
2043  * callers duty to clean those orphaned rings.
2044  *
2045  * Return 0 on success, negative on failure
2046  **/
2047 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2048 {
2049         int i, err = 0;
2050
2051         for (i = 0; i < vsi->num_queue_pairs && !err; i++)
2052                 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
2053
2054         return err;
2055 }
2056
2057 /**
2058  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2059  * @vsi: ptr to the VSI
2060  *
2061  * Free VSI's transmit software resources
2062  **/
2063 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2064 {
2065         int i;
2066
2067         for (i = 0; i < vsi->num_queue_pairs; i++)
2068                 if (vsi->tx_rings[i]->desc)
2069                         i40e_free_tx_resources(vsi->tx_rings[i]);
2070 }
2071
2072 /**
2073  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2074  * @vsi: ptr to the VSI
2075  *
2076  * If this function returns with an error, then it's possible one or
2077  * more of the rings is populated (while the rest are not).  It is the
2078  * callers duty to clean those orphaned rings.
2079  *
2080  * Return 0 on success, negative on failure
2081  **/
2082 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2083 {
2084         int i, err = 0;
2085
2086         for (i = 0; i < vsi->num_queue_pairs && !err; i++)
2087                 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
2088         return err;
2089 }
2090
2091 /**
2092  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2093  * @vsi: ptr to the VSI
2094  *
2095  * Free all receive software resources
2096  **/
2097 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2098 {
2099         int i;
2100
2101         for (i = 0; i < vsi->num_queue_pairs; i++)
2102                 if (vsi->rx_rings[i]->desc)
2103                         i40e_free_rx_resources(vsi->rx_rings[i]);
2104 }
2105
2106 /**
2107  * i40e_configure_tx_ring - Configure a transmit ring context and rest
2108  * @ring: The Tx ring to configure
2109  *
2110  * Configure the Tx descriptor ring in the HMC context.
2111  **/
2112 static int i40e_configure_tx_ring(struct i40e_ring *ring)
2113 {
2114         struct i40e_vsi *vsi = ring->vsi;
2115         u16 pf_q = vsi->base_queue + ring->queue_index;
2116         struct i40e_hw *hw = &vsi->back->hw;
2117         struct i40e_hmc_obj_txq tx_ctx;
2118         i40e_status err = 0;
2119         u32 qtx_ctl = 0;
2120
2121         /* some ATR related tx ring init */
2122         if (vsi->back->flags & I40E_FLAG_FDIR_ATR_ENABLED) {
2123                 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2124                 ring->atr_count = 0;
2125         } else {
2126                 ring->atr_sample_rate = 0;
2127         }
2128
2129         /* initialize XPS */
2130         if (ring->q_vector && ring->netdev &&
2131             !test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
2132                 netif_set_xps_queue(ring->netdev,
2133                                     &ring->q_vector->affinity_mask,
2134                                     ring->queue_index);
2135
2136         /* clear the context structure first */
2137         memset(&tx_ctx, 0, sizeof(tx_ctx));
2138
2139         tx_ctx.new_context = 1;
2140         tx_ctx.base = (ring->dma / 128);
2141         tx_ctx.qlen = ring->count;
2142         tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FDIR_ENABLED |
2143                         I40E_FLAG_FDIR_ATR_ENABLED));
2144
2145         /* As part of VSI creation/update, FW allocates certain
2146          * Tx arbitration queue sets for each TC enabled for
2147          * the VSI. The FW returns the handles to these queue
2148          * sets as part of the response buffer to Add VSI,
2149          * Update VSI, etc. AQ commands. It is expected that
2150          * these queue set handles be associated with the Tx
2151          * queues by the driver as part of the TX queue context
2152          * initialization. This has to be done regardless of
2153          * DCB as by default everything is mapped to TC0.
2154          */
2155         tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2156         tx_ctx.rdylist_act = 0;
2157
2158         /* clear the context in the HMC */
2159         err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2160         if (err) {
2161                 dev_info(&vsi->back->pdev->dev,
2162                          "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2163                          ring->queue_index, pf_q, err);
2164                 return -ENOMEM;
2165         }
2166
2167         /* set the context in the HMC */
2168         err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2169         if (err) {
2170                 dev_info(&vsi->back->pdev->dev,
2171                          "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2172                          ring->queue_index, pf_q, err);
2173                 return -ENOMEM;
2174         }
2175
2176         /* Now associate this queue with this PCI function */
2177         qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
2178         qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2179                     I40E_QTX_CTL_PF_INDX_MASK);
2180         wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2181         i40e_flush(hw);
2182
2183         clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
2184
2185         /* cache tail off for easier writes later */
2186         ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2187
2188         return 0;
2189 }
2190
2191 /**
2192  * i40e_configure_rx_ring - Configure a receive ring context
2193  * @ring: The Rx ring to configure
2194  *
2195  * Configure the Rx descriptor ring in the HMC context.
2196  **/
2197 static int i40e_configure_rx_ring(struct i40e_ring *ring)
2198 {
2199         struct i40e_vsi *vsi = ring->vsi;
2200         u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2201         u16 pf_q = vsi->base_queue + ring->queue_index;
2202         struct i40e_hw *hw = &vsi->back->hw;
2203         struct i40e_hmc_obj_rxq rx_ctx;
2204         i40e_status err = 0;
2205
2206         ring->state = 0;
2207
2208         /* clear the context structure first */
2209         memset(&rx_ctx, 0, sizeof(rx_ctx));
2210
2211         ring->rx_buf_len = vsi->rx_buf_len;
2212         ring->rx_hdr_len = vsi->rx_hdr_len;
2213
2214         rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
2215         rx_ctx.hbuff = ring->rx_hdr_len >> I40E_RXQ_CTX_HBUFF_SHIFT;
2216
2217         rx_ctx.base = (ring->dma / 128);
2218         rx_ctx.qlen = ring->count;
2219
2220         if (vsi->back->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED) {
2221                 set_ring_16byte_desc_enabled(ring);
2222                 rx_ctx.dsize = 0;
2223         } else {
2224                 rx_ctx.dsize = 1;
2225         }
2226
2227         rx_ctx.dtype = vsi->dtype;
2228         if (vsi->dtype) {
2229                 set_ring_ps_enabled(ring);
2230                 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2      |
2231                                   I40E_RX_SPLIT_IP      |
2232                                   I40E_RX_SPLIT_TCP_UDP |
2233                                   I40E_RX_SPLIT_SCTP;
2234         } else {
2235                 rx_ctx.hsplit_0 = 0;
2236         }
2237
2238         rx_ctx.rxmax = min_t(u16, vsi->max_frame,
2239                                   (chain_len * ring->rx_buf_len));
2240         rx_ctx.tphrdesc_ena = 1;
2241         rx_ctx.tphwdesc_ena = 1;
2242         rx_ctx.tphdata_ena = 1;
2243         rx_ctx.tphhead_ena = 1;
2244         if (hw->revision_id == 0)
2245                 rx_ctx.lrxqthresh = 0;
2246         else
2247                 rx_ctx.lrxqthresh = 2;
2248         rx_ctx.crcstrip = 1;
2249         rx_ctx.l2tsel = 1;
2250         rx_ctx.showiv = 1;
2251
2252         /* clear the context in the HMC */
2253         err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2254         if (err) {
2255                 dev_info(&vsi->back->pdev->dev,
2256                          "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2257                          ring->queue_index, pf_q, err);
2258                 return -ENOMEM;
2259         }
2260
2261         /* set the context in the HMC */
2262         err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2263         if (err) {
2264                 dev_info(&vsi->back->pdev->dev,
2265                          "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2266                          ring->queue_index, pf_q, err);
2267                 return -ENOMEM;
2268         }
2269
2270         /* cache tail for quicker writes, and clear the reg before use */
2271         ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2272         writel(0, ring->tail);
2273
2274         i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
2275
2276         return 0;
2277 }
2278
2279 /**
2280  * i40e_vsi_configure_tx - Configure the VSI for Tx
2281  * @vsi: VSI structure describing this set of rings and resources
2282  *
2283  * Configure the Tx VSI for operation.
2284  **/
2285 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2286 {
2287         int err = 0;
2288         u16 i;
2289
2290         for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2291                 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
2292
2293         return err;
2294 }
2295
2296 /**
2297  * i40e_vsi_configure_rx - Configure the VSI for Rx
2298  * @vsi: the VSI being configured
2299  *
2300  * Configure the Rx VSI for operation.
2301  **/
2302 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
2303 {
2304         int err = 0;
2305         u16 i;
2306
2307         if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
2308                 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
2309                                + ETH_FCS_LEN + VLAN_HLEN;
2310         else
2311                 vsi->max_frame = I40E_RXBUFFER_2048;
2312
2313         /* figure out correct receive buffer length */
2314         switch (vsi->back->flags & (I40E_FLAG_RX_1BUF_ENABLED |
2315                                     I40E_FLAG_RX_PS_ENABLED)) {
2316         case I40E_FLAG_RX_1BUF_ENABLED:
2317                 vsi->rx_hdr_len = 0;
2318                 vsi->rx_buf_len = vsi->max_frame;
2319                 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2320                 break;
2321         case I40E_FLAG_RX_PS_ENABLED:
2322                 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2323                 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2324                 vsi->dtype = I40E_RX_DTYPE_HEADER_SPLIT;
2325                 break;
2326         default:
2327                 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2328                 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2329                 vsi->dtype = I40E_RX_DTYPE_SPLIT_ALWAYS;
2330                 break;
2331         }
2332
2333         /* round up for the chip's needs */
2334         vsi->rx_hdr_len = ALIGN(vsi->rx_hdr_len,
2335                                 (1 << I40E_RXQ_CTX_HBUFF_SHIFT));
2336         vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
2337                                 (1 << I40E_RXQ_CTX_DBUFF_SHIFT));
2338
2339         /* set up individual rings */
2340         for (i = 0; i < vsi->num_queue_pairs && !err; i++)
2341                 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
2342
2343         return err;
2344 }
2345
2346 /**
2347  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
2348  * @vsi: ptr to the VSI
2349  **/
2350 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
2351 {
2352         u16 qoffset, qcount;
2353         int i, n;
2354
2355         if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED))
2356                 return;
2357
2358         for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
2359                 if (!(vsi->tc_config.enabled_tc & (1 << n)))
2360                         continue;
2361
2362                 qoffset = vsi->tc_config.tc_info[n].qoffset;
2363                 qcount = vsi->tc_config.tc_info[n].qcount;
2364                 for (i = qoffset; i < (qoffset + qcount); i++) {
2365                         struct i40e_ring *rx_ring = vsi->rx_rings[i];
2366                         struct i40e_ring *tx_ring = vsi->tx_rings[i];
2367                         rx_ring->dcb_tc = n;
2368                         tx_ring->dcb_tc = n;
2369                 }
2370         }
2371 }
2372
2373 /**
2374  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
2375  * @vsi: ptr to the VSI
2376  **/
2377 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
2378 {
2379         if (vsi->netdev)
2380                 i40e_set_rx_mode(vsi->netdev);
2381 }
2382
2383 /**
2384  * i40e_vsi_configure - Set up the VSI for action
2385  * @vsi: the VSI being configured
2386  **/
2387 static int i40e_vsi_configure(struct i40e_vsi *vsi)
2388 {
2389         int err;
2390
2391         i40e_set_vsi_rx_mode(vsi);
2392         i40e_restore_vlan(vsi);
2393         i40e_vsi_config_dcb_rings(vsi);
2394         err = i40e_vsi_configure_tx(vsi);
2395         if (!err)
2396                 err = i40e_vsi_configure_rx(vsi);
2397
2398         return err;
2399 }
2400
2401 /**
2402  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
2403  * @vsi: the VSI being configured
2404  **/
2405 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
2406 {
2407         struct i40e_pf *pf = vsi->back;
2408         struct i40e_q_vector *q_vector;
2409         struct i40e_hw *hw = &pf->hw;
2410         u16 vector;
2411         int i, q;
2412         u32 val;
2413         u32 qp;
2414
2415         /* The interrupt indexing is offset by 1 in the PFINT_ITRn
2416          * and PFINT_LNKLSTn registers, e.g.:
2417          *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
2418          */
2419         qp = vsi->base_queue;
2420         vector = vsi->base_vector;
2421         for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
2422                 q_vector = vsi->q_vectors[i];
2423                 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2424                 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2425                 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
2426                      q_vector->rx.itr);
2427                 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2428                 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2429                 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
2430                      q_vector->tx.itr);
2431
2432                 /* Linked list for the queuepairs assigned to this vector */
2433                 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
2434                 for (q = 0; q < q_vector->num_ringpairs; q++) {
2435                         val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2436                               (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT)  |
2437                               (vector      << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
2438                               (qp          << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
2439                               (I40E_QUEUE_TYPE_TX
2440                                       << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
2441
2442                         wr32(hw, I40E_QINT_RQCTL(qp), val);
2443
2444                         val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2445                               (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)  |
2446                               (vector      << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
2447                               ((qp+1)      << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
2448                               (I40E_QUEUE_TYPE_RX
2449                                       << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2450
2451                         /* Terminate the linked list */
2452                         if (q == (q_vector->num_ringpairs - 1))
2453                                 val |= (I40E_QUEUE_END_OF_LIST
2454                                            << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2455
2456                         wr32(hw, I40E_QINT_TQCTL(qp), val);
2457                         qp++;
2458                 }
2459         }
2460
2461         i40e_flush(hw);
2462 }
2463
2464 /**
2465  * i40e_enable_misc_int_causes - enable the non-queue interrupts
2466  * @hw: ptr to the hardware info
2467  **/
2468 static void i40e_enable_misc_int_causes(struct i40e_hw *hw)
2469 {
2470         u32 val;
2471
2472         /* clear things first */
2473         wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
2474         rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
2475
2476         val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
2477               I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
2478               I40E_PFINT_ICR0_ENA_GRST_MASK          |
2479               I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
2480               I40E_PFINT_ICR0_ENA_GPIO_MASK          |
2481               I40E_PFINT_ICR0_ENA_STORM_DETECT_MASK  |
2482               I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
2483               I40E_PFINT_ICR0_ENA_VFLR_MASK          |
2484               I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2485
2486         wr32(hw, I40E_PFINT_ICR0_ENA, val);
2487
2488         /* SW_ITR_IDX = 0, but don't change INTENA */
2489         wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTLN_SW_ITR_INDX_MASK |
2490                                         I40E_PFINT_DYN_CTLN_INTENA_MSK_MASK);
2491
2492         /* OTHER_ITR_IDX = 0 */
2493         wr32(hw, I40E_PFINT_STAT_CTL0, 0);
2494 }
2495
2496 /**
2497  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
2498  * @vsi: the VSI being configured
2499  **/
2500 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
2501 {
2502         struct i40e_q_vector *q_vector = vsi->q_vectors[0];
2503         struct i40e_pf *pf = vsi->back;
2504         struct i40e_hw *hw = &pf->hw;
2505         u32 val;
2506
2507         /* set the ITR configuration */
2508         q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2509         q_vector->rx.latency_range = I40E_LOW_LATENCY;
2510         wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
2511         q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2512         q_vector->tx.latency_range = I40E_LOW_LATENCY;
2513         wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
2514
2515         i40e_enable_misc_int_causes(hw);
2516
2517         /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
2518         wr32(hw, I40E_PFINT_LNKLST0, 0);
2519
2520         /* Associate the queue pair to the vector and enable the q int */
2521         val = I40E_QINT_RQCTL_CAUSE_ENA_MASK                  |
2522               (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2523               (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2524
2525         wr32(hw, I40E_QINT_RQCTL(0), val);
2526
2527         val = I40E_QINT_TQCTL_CAUSE_ENA_MASK                  |
2528               (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2529               (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2530
2531         wr32(hw, I40E_QINT_TQCTL(0), val);
2532         i40e_flush(hw);
2533 }
2534
2535 /**
2536  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
2537  * @pf: board private structure
2538  **/
2539 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
2540 {
2541         struct i40e_hw *hw = &pf->hw;
2542         u32 val;
2543
2544         val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
2545               I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
2546               (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
2547
2548         wr32(hw, I40E_PFINT_DYN_CTL0, val);
2549         i40e_flush(hw);
2550 }
2551
2552 /**
2553  * i40e_irq_dynamic_enable - Enable default interrupt generation settings
2554  * @vsi: pointer to a vsi
2555  * @vector: enable a particular Hw Interrupt vector
2556  **/
2557 void i40e_irq_dynamic_enable(struct i40e_vsi *vsi, int vector)
2558 {
2559         struct i40e_pf *pf = vsi->back;
2560         struct i40e_hw *hw = &pf->hw;
2561         u32 val;
2562
2563         val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
2564               I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
2565               (I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2566         wr32(hw, I40E_PFINT_DYN_CTLN(vector - 1), val);
2567         /* skip the flush */
2568 }
2569
2570 /**
2571  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
2572  * @irq: interrupt number
2573  * @data: pointer to a q_vector
2574  **/
2575 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
2576 {
2577         struct i40e_q_vector *q_vector = data;
2578
2579         if (!q_vector->tx.ring && !q_vector->rx.ring)
2580                 return IRQ_HANDLED;
2581
2582         napi_schedule(&q_vector->napi);
2583
2584         return IRQ_HANDLED;
2585 }
2586
2587 /**
2588  * i40e_fdir_clean_rings - Interrupt Handler for FDIR rings
2589  * @irq: interrupt number
2590  * @data: pointer to a q_vector
2591  **/
2592 static irqreturn_t i40e_fdir_clean_rings(int irq, void *data)
2593 {
2594         struct i40e_q_vector *q_vector = data;
2595
2596         if (!q_vector->tx.ring && !q_vector->rx.ring)
2597                 return IRQ_HANDLED;
2598
2599         pr_info("fdir ring cleaning needed\n");
2600
2601         return IRQ_HANDLED;
2602 }
2603
2604 /**
2605  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
2606  * @vsi: the VSI being configured
2607  * @basename: name for the vector
2608  *
2609  * Allocates MSI-X vectors and requests interrupts from the kernel.
2610  **/
2611 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
2612 {
2613         int q_vectors = vsi->num_q_vectors;
2614         struct i40e_pf *pf = vsi->back;
2615         int base = vsi->base_vector;
2616         int rx_int_idx = 0;
2617         int tx_int_idx = 0;
2618         int vector, err;
2619
2620         for (vector = 0; vector < q_vectors; vector++) {
2621                 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
2622
2623                 if (q_vector->tx.ring && q_vector->rx.ring) {
2624                         snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2625                                  "%s-%s-%d", basename, "TxRx", rx_int_idx++);
2626                         tx_int_idx++;
2627                 } else if (q_vector->rx.ring) {
2628                         snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2629                                  "%s-%s-%d", basename, "rx", rx_int_idx++);
2630                 } else if (q_vector->tx.ring) {
2631                         snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2632                                  "%s-%s-%d", basename, "tx", tx_int_idx++);
2633                 } else {
2634                         /* skip this unused q_vector */
2635                         continue;
2636                 }
2637                 err = request_irq(pf->msix_entries[base + vector].vector,
2638                                   vsi->irq_handler,
2639                                   0,
2640                                   q_vector->name,
2641                                   q_vector);
2642                 if (err) {
2643                         dev_info(&pf->pdev->dev,
2644                                  "%s: request_irq failed, error: %d\n",
2645                                  __func__, err);
2646                         goto free_queue_irqs;
2647                 }
2648                 /* assign the mask for this irq */
2649                 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2650                                       &q_vector->affinity_mask);
2651         }
2652
2653         return 0;
2654
2655 free_queue_irqs:
2656         while (vector) {
2657                 vector--;
2658                 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2659                                       NULL);
2660                 free_irq(pf->msix_entries[base + vector].vector,
2661                          &(vsi->q_vectors[vector]));
2662         }
2663         return err;
2664 }
2665
2666 /**
2667  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
2668  * @vsi: the VSI being un-configured
2669  **/
2670 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
2671 {
2672         struct i40e_pf *pf = vsi->back;
2673         struct i40e_hw *hw = &pf->hw;
2674         int base = vsi->base_vector;
2675         int i;
2676
2677         for (i = 0; i < vsi->num_queue_pairs; i++) {
2678                 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
2679                 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
2680         }
2681
2682         if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2683                 for (i = vsi->base_vector;
2684                      i < (vsi->num_q_vectors + vsi->base_vector); i++)
2685                         wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
2686
2687                 i40e_flush(hw);
2688                 for (i = 0; i < vsi->num_q_vectors; i++)
2689                         synchronize_irq(pf->msix_entries[i + base].vector);
2690         } else {
2691                 /* Legacy and MSI mode - this stops all interrupt handling */
2692                 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
2693                 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
2694                 i40e_flush(hw);
2695                 synchronize_irq(pf->pdev->irq);
2696         }
2697 }
2698
2699 /**
2700  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
2701  * @vsi: the VSI being configured
2702  **/
2703 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
2704 {
2705         struct i40e_pf *pf = vsi->back;
2706         int i;
2707
2708         if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2709                 for (i = vsi->base_vector;
2710                      i < (vsi->num_q_vectors + vsi->base_vector); i++)
2711                         i40e_irq_dynamic_enable(vsi, i);
2712         } else {
2713                 i40e_irq_dynamic_enable_icr0(pf);
2714         }
2715
2716         i40e_flush(&pf->hw);
2717         return 0;
2718 }
2719
2720 /**
2721  * i40e_stop_misc_vector - Stop the vector that handles non-queue events
2722  * @pf: board private structure
2723  **/
2724 static void i40e_stop_misc_vector(struct i40e_pf *pf)
2725 {
2726         /* Disable ICR 0 */
2727         wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
2728         i40e_flush(&pf->hw);
2729 }
2730
2731 /**
2732  * i40e_intr - MSI/Legacy and non-queue interrupt handler
2733  * @irq: interrupt number
2734  * @data: pointer to a q_vector
2735  *
2736  * This is the handler used for all MSI/Legacy interrupts, and deals
2737  * with both queue and non-queue interrupts.  This is also used in
2738  * MSIX mode to handle the non-queue interrupts.
2739  **/
2740 static irqreturn_t i40e_intr(int irq, void *data)
2741 {
2742         struct i40e_pf *pf = (struct i40e_pf *)data;
2743         struct i40e_hw *hw = &pf->hw;
2744         u32 icr0, icr0_remaining;
2745         u32 val, ena_mask;
2746
2747         icr0 = rd32(hw, I40E_PFINT_ICR0);
2748
2749         val = rd32(hw, I40E_PFINT_DYN_CTL0);
2750         val = val | I40E_PFINT_DYN_CTL0_CLEARPBA_MASK;
2751         wr32(hw, I40E_PFINT_DYN_CTL0, val);
2752
2753         /* if sharing a legacy IRQ, we might get called w/o an intr pending */
2754         if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
2755                 return IRQ_NONE;
2756
2757         ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
2758
2759         /* if interrupt but no bits showing, must be SWINT */
2760         if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
2761             (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
2762                 pf->sw_int_count++;
2763
2764         /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
2765         if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
2766
2767                 /* temporarily disable queue cause for NAPI processing */
2768                 u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
2769                 qval &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
2770                 wr32(hw, I40E_QINT_RQCTL(0), qval);
2771
2772                 qval = rd32(hw, I40E_QINT_TQCTL(0));
2773                 qval &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
2774                 wr32(hw, I40E_QINT_TQCTL(0), qval);
2775
2776                 if (!test_bit(__I40E_DOWN, &pf->state))
2777                         napi_schedule(&pf->vsi[pf->lan_vsi]->q_vectors[0]->napi);
2778         }
2779
2780         if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
2781                 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2782                 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
2783         }
2784
2785         if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
2786                 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
2787                 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
2788         }
2789
2790         if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
2791                 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
2792                 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
2793         }
2794
2795         if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
2796                 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
2797                         set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
2798                 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
2799                 val = rd32(hw, I40E_GLGEN_RSTAT);
2800                 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
2801                        >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
2802                 if (val == I40E_RESET_CORER)
2803                         pf->corer_count++;
2804                 else if (val == I40E_RESET_GLOBR)
2805                         pf->globr_count++;
2806                 else if (val == I40E_RESET_EMPR)
2807                         pf->empr_count++;
2808         }
2809
2810         /* If a critical error is pending we have no choice but to reset the
2811          * device.
2812          * Report and mask out any remaining unexpected interrupts.
2813          */
2814         icr0_remaining = icr0 & ena_mask;
2815         if (icr0_remaining) {
2816                 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
2817                          icr0_remaining);
2818                 if ((icr0_remaining & I40E_PFINT_ICR0_HMC_ERR_MASK) ||
2819                     (icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
2820                     (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
2821                     (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK) ||
2822                     (icr0_remaining & I40E_PFINT_ICR0_MAL_DETECT_MASK)) {
2823                         if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
2824                                 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
2825                         } else {
2826                                 dev_info(&pf->pdev->dev, "device will be reset\n");
2827                                 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
2828                                 i40e_service_event_schedule(pf);
2829                         }
2830                 }
2831                 ena_mask &= ~icr0_remaining;
2832         }
2833
2834         /* re-enable interrupt causes */
2835         wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
2836         if (!test_bit(__I40E_DOWN, &pf->state)) {
2837                 i40e_service_event_schedule(pf);
2838                 i40e_irq_dynamic_enable_icr0(pf);
2839         }
2840
2841         return IRQ_HANDLED;
2842 }
2843
2844 /**
2845  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
2846  * @vsi: the VSI being configured
2847  * @v_idx: vector index
2848  * @qp_idx: queue pair index
2849  **/
2850 static void map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
2851 {
2852         struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
2853         struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
2854         struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
2855
2856         tx_ring->q_vector = q_vector;
2857         tx_ring->next = q_vector->tx.ring;
2858         q_vector->tx.ring = tx_ring;
2859         q_vector->tx.count++;
2860
2861         rx_ring->q_vector = q_vector;
2862         rx_ring->next = q_vector->rx.ring;
2863         q_vector->rx.ring = rx_ring;
2864         q_vector->rx.count++;
2865 }
2866
2867 /**
2868  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
2869  * @vsi: the VSI being configured
2870  *
2871  * This function maps descriptor rings to the queue-specific vectors
2872  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
2873  * one vector per queue pair, but on a constrained vector budget, we
2874  * group the queue pairs as "efficiently" as possible.
2875  **/
2876 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
2877 {
2878         int qp_remaining = vsi->num_queue_pairs;
2879         int q_vectors = vsi->num_q_vectors;
2880         int num_ringpairs;
2881         int v_start = 0;
2882         int qp_idx = 0;
2883
2884         /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
2885          * group them so there are multiple queues per vector.
2886          */
2887         for (; v_start < q_vectors && qp_remaining; v_start++) {
2888                 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
2889
2890                 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
2891
2892                 q_vector->num_ringpairs = num_ringpairs;
2893
2894                 q_vector->rx.count = 0;
2895                 q_vector->tx.count = 0;
2896                 q_vector->rx.ring = NULL;
2897                 q_vector->tx.ring = NULL;
2898
2899                 while (num_ringpairs--) {
2900                         map_vector_to_qp(vsi, v_start, qp_idx);
2901                         qp_idx++;
2902                         qp_remaining--;
2903                 }
2904         }
2905 }
2906
2907 /**
2908  * i40e_vsi_request_irq - Request IRQ from the OS
2909  * @vsi: the VSI being configured
2910  * @basename: name for the vector
2911  **/
2912 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
2913 {
2914         struct i40e_pf *pf = vsi->back;
2915         int err;
2916
2917         if (pf->flags & I40E_FLAG_MSIX_ENABLED)
2918                 err = i40e_vsi_request_irq_msix(vsi, basename);
2919         else if (pf->flags & I40E_FLAG_MSI_ENABLED)
2920                 err = request_irq(pf->pdev->irq, i40e_intr, 0,
2921                                   pf->misc_int_name, pf);
2922         else
2923                 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
2924                                   pf->misc_int_name, pf);
2925
2926         if (err)
2927                 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
2928
2929         return err;
2930 }
2931
2932 #ifdef CONFIG_NET_POLL_CONTROLLER
2933 /**
2934  * i40e_netpoll - A Polling 'interrupt'handler
2935  * @netdev: network interface device structure
2936  *
2937  * This is used by netconsole to send skbs without having to re-enable
2938  * interrupts.  It's not called while the normal interrupt routine is executing.
2939  **/
2940 static void i40e_netpoll(struct net_device *netdev)
2941 {
2942         struct i40e_netdev_priv *np = netdev_priv(netdev);
2943         struct i40e_vsi *vsi = np->vsi;
2944         struct i40e_pf *pf = vsi->back;
2945         int i;
2946
2947         /* if interface is down do nothing */
2948         if (test_bit(__I40E_DOWN, &vsi->state))
2949                 return;
2950
2951         pf->flags |= I40E_FLAG_IN_NETPOLL;
2952         if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2953                 for (i = 0; i < vsi->num_q_vectors; i++)
2954                         i40e_msix_clean_rings(0, vsi->q_vectors[i]);
2955         } else {
2956                 i40e_intr(pf->pdev->irq, netdev);
2957         }
2958         pf->flags &= ~I40E_FLAG_IN_NETPOLL;
2959 }
2960 #endif
2961
2962 /**
2963  * i40e_vsi_control_tx - Start or stop a VSI's rings
2964  * @vsi: the VSI being configured
2965  * @enable: start or stop the rings
2966  **/
2967 static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
2968 {
2969         struct i40e_pf *pf = vsi->back;
2970         struct i40e_hw *hw = &pf->hw;
2971         int i, j, pf_q;
2972         u32 tx_reg;
2973
2974         pf_q = vsi->base_queue;
2975         for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
2976                 j = 1000;
2977                 do {
2978                         usleep_range(1000, 2000);
2979                         tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
2980                 } while (j-- && ((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT)
2981                                ^ (tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT)) & 1);
2982
2983                 if (enable) {
2984                         /* is STAT set ? */
2985                         if ((tx_reg & I40E_QTX_ENA_QENA_STAT_MASK)) {
2986                                 dev_info(&pf->pdev->dev,
2987                                          "Tx %d already enabled\n", i);
2988                                 continue;
2989                         }
2990                 } else {
2991                         /* is !STAT set ? */
2992                         if (!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK)) {
2993                                 dev_info(&pf->pdev->dev,
2994                                          "Tx %d already disabled\n", i);
2995                                 continue;
2996                         }
2997                 }
2998
2999                 /* turn on/off the queue */
3000                 if (enable)
3001                         tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK |
3002                                   I40E_QTX_ENA_QENA_STAT_MASK;
3003                 else
3004                         tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
3005
3006                 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
3007
3008                 /* wait for the change to finish */
3009                 for (j = 0; j < 10; j++) {
3010                         tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
3011                         if (enable) {
3012                                 if ((tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3013                                         break;
3014                         } else {
3015                                 if (!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3016                                         break;
3017                         }
3018
3019                         udelay(10);
3020                 }
3021                 if (j >= 10) {
3022                         dev_info(&pf->pdev->dev, "Tx ring %d %sable timeout\n",
3023                                  pf_q, (enable ? "en" : "dis"));
3024                         return -ETIMEDOUT;
3025                 }
3026         }
3027
3028         if (hw->revision_id == 0)
3029                 mdelay(50);
3030
3031         return 0;
3032 }
3033
3034 /**
3035  * i40e_vsi_control_rx - Start or stop a VSI's rings
3036  * @vsi: the VSI being configured
3037  * @enable: start or stop the rings
3038  **/
3039 static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3040 {
3041         struct i40e_pf *pf = vsi->back;
3042         struct i40e_hw *hw = &pf->hw;
3043         int i, j, pf_q;
3044         u32 rx_reg;
3045
3046         pf_q = vsi->base_queue;
3047         for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
3048                 j = 1000;
3049                 do {
3050                         usleep_range(1000, 2000);
3051                         rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
3052                 } while (j-- && ((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT)
3053                                ^ (rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT)) & 1);
3054
3055                 if (enable) {
3056                         /* is STAT set ? */
3057                         if ((rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3058                                 continue;
3059                 } else {
3060                         /* is !STAT set ? */
3061                         if (!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3062                                 continue;
3063                 }
3064
3065                 /* turn on/off the queue */
3066                 if (enable)
3067                         rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK |
3068                                   I40E_QRX_ENA_QENA_STAT_MASK;
3069                 else
3070                         rx_reg &= ~(I40E_QRX_ENA_QENA_REQ_MASK |
3071                                   I40E_QRX_ENA_QENA_STAT_MASK);
3072                 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
3073
3074                 /* wait for the change to finish */
3075                 for (j = 0; j < 10; j++) {
3076                         rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
3077
3078                         if (enable) {
3079                                 if ((rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3080                                         break;
3081                         } else {
3082                                 if (!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3083                                         break;
3084                         }
3085
3086                         udelay(10);
3087                 }
3088                 if (j >= 10) {
3089                         dev_info(&pf->pdev->dev, "Rx ring %d %sable timeout\n",
3090                                  pf_q, (enable ? "en" : "dis"));
3091                         return -ETIMEDOUT;
3092                 }
3093         }
3094
3095         return 0;
3096 }
3097
3098 /**
3099  * i40e_vsi_control_rings - Start or stop a VSI's rings
3100  * @vsi: the VSI being configured
3101  * @enable: start or stop the rings
3102  **/
3103 static int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
3104 {
3105         int ret;
3106
3107         /* do rx first for enable and last for disable */
3108         if (request) {
3109                 ret = i40e_vsi_control_rx(vsi, request);
3110                 if (ret)
3111                         return ret;
3112                 ret = i40e_vsi_control_tx(vsi, request);
3113         } else {
3114                 ret = i40e_vsi_control_tx(vsi, request);
3115                 if (ret)
3116                         return ret;
3117                 ret = i40e_vsi_control_rx(vsi, request);
3118         }
3119
3120         return ret;
3121 }
3122
3123 /**
3124  * i40e_vsi_free_irq - Free the irq association with the OS
3125  * @vsi: the VSI being configured
3126  **/
3127 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
3128 {
3129         struct i40e_pf *pf = vsi->back;
3130         struct i40e_hw *hw = &pf->hw;
3131         int base = vsi->base_vector;
3132         u32 val, qp;
3133         int i;
3134
3135         if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3136                 if (!vsi->q_vectors)
3137                         return;
3138
3139                 for (i = 0; i < vsi->num_q_vectors; i++) {
3140                         u16 vector = i + base;
3141
3142                         /* free only the irqs that were actually requested */
3143                         if (vsi->q_vectors[i]->num_ringpairs == 0)
3144                                 continue;
3145
3146                         /* clear the affinity_mask in the IRQ descriptor */
3147                         irq_set_affinity_hint(pf->msix_entries[vector].vector,
3148                                               NULL);
3149                         free_irq(pf->msix_entries[vector].vector,
3150                                  vsi->q_vectors[i]);
3151
3152                         /* Tear down the interrupt queue link list
3153                          *
3154                          * We know that they come in pairs and always
3155                          * the Rx first, then the Tx.  To clear the
3156                          * link list, stick the EOL value into the
3157                          * next_q field of the registers.
3158                          */
3159                         val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
3160                         qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3161                                 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3162                         val |= I40E_QUEUE_END_OF_LIST
3163                                 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3164                         wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
3165
3166                         while (qp != I40E_QUEUE_END_OF_LIST) {
3167                                 u32 next;
3168
3169                                 val = rd32(hw, I40E_QINT_RQCTL(qp));
3170
3171                                 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
3172                                          I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3173                                          I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
3174                                          I40E_QINT_RQCTL_INTEVENT_MASK);
3175
3176                                 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3177                                          I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3178
3179                                 wr32(hw, I40E_QINT_RQCTL(qp), val);
3180
3181                                 val = rd32(hw, I40E_QINT_TQCTL(qp));
3182
3183                                 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
3184                                         >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
3185
3186                                 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
3187                                          I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3188                                          I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
3189                                          I40E_QINT_TQCTL_INTEVENT_MASK);
3190
3191                                 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3192                                          I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3193
3194                                 wr32(hw, I40E_QINT_TQCTL(qp), val);
3195                                 qp = next;
3196                         }
3197                 }
3198         } else {
3199                 free_irq(pf->pdev->irq, pf);
3200
3201                 val = rd32(hw, I40E_PFINT_LNKLST0);
3202                 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3203                         >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3204                 val |= I40E_QUEUE_END_OF_LIST
3205                         << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
3206                 wr32(hw, I40E_PFINT_LNKLST0, val);
3207
3208                 val = rd32(hw, I40E_QINT_RQCTL(qp));
3209                 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
3210                          I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3211                          I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
3212                          I40E_QINT_RQCTL_INTEVENT_MASK);
3213
3214                 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3215                         I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3216
3217                 wr32(hw, I40E_QINT_RQCTL(qp), val);
3218
3219                 val = rd32(hw, I40E_QINT_TQCTL(qp));
3220
3221                 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
3222                          I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3223                          I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
3224                          I40E_QINT_TQCTL_INTEVENT_MASK);
3225
3226                 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3227                         I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3228
3229                 wr32(hw, I40E_QINT_TQCTL(qp), val);
3230         }
3231 }
3232
3233 /**
3234  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
3235  * @vsi: the VSI being configured
3236  * @v_idx: Index of vector to be freed
3237  *
3238  * This function frees the memory allocated to the q_vector.  In addition if
3239  * NAPI is enabled it will delete any references to the NAPI struct prior
3240  * to freeing the q_vector.
3241  **/
3242 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
3243 {
3244         struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
3245         struct i40e_ring *ring;
3246
3247         if (!q_vector)
3248                 return;
3249
3250         /* disassociate q_vector from rings */
3251         i40e_for_each_ring(ring, q_vector->tx)
3252                 ring->q_vector = NULL;
3253
3254         i40e_for_each_ring(ring, q_vector->rx)
3255                 ring->q_vector = NULL;
3256
3257         /* only VSI w/ an associated netdev is set up w/ NAPI */
3258         if (vsi->netdev)
3259                 netif_napi_del(&q_vector->napi);
3260
3261         vsi->q_vectors[v_idx] = NULL;
3262
3263         kfree_rcu(q_vector, rcu);
3264 }
3265
3266 /**
3267  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
3268  * @vsi: the VSI being un-configured
3269  *
3270  * This frees the memory allocated to the q_vectors and
3271  * deletes references to the NAPI struct.
3272  **/
3273 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
3274 {
3275         int v_idx;
3276
3277         for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
3278                 i40e_free_q_vector(vsi, v_idx);
3279 }
3280
3281 /**
3282  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
3283  * @pf: board private structure
3284  **/
3285 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
3286 {
3287         /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
3288         if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3289                 pci_disable_msix(pf->pdev);
3290                 kfree(pf->msix_entries);
3291                 pf->msix_entries = NULL;
3292         } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
3293                 pci_disable_msi(pf->pdev);
3294         }
3295         pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
3296 }
3297
3298 /**
3299  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
3300  * @pf: board private structure
3301  *
3302  * We go through and clear interrupt specific resources and reset the structure
3303  * to pre-load conditions
3304  **/
3305 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
3306 {
3307         int i;
3308
3309         i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
3310         for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
3311                 if (pf->vsi[i])
3312                         i40e_vsi_free_q_vectors(pf->vsi[i]);
3313         i40e_reset_interrupt_capability(pf);
3314 }
3315
3316 /**
3317  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
3318  * @vsi: the VSI being configured
3319  **/
3320 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
3321 {
3322         int q_idx;
3323
3324         if (!vsi->netdev)
3325                 return;
3326
3327         for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
3328                 napi_enable(&vsi->q_vectors[q_idx]->napi);
3329 }
3330
3331 /**
3332  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
3333  * @vsi: the VSI being configured
3334  **/
3335 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
3336 {
3337         int q_idx;
3338
3339         if (!vsi->netdev)
3340                 return;
3341
3342         for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
3343                 napi_disable(&vsi->q_vectors[q_idx]->napi);
3344 }
3345
3346 /**
3347  * i40e_quiesce_vsi - Pause a given VSI
3348  * @vsi: the VSI being paused
3349  **/
3350 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
3351 {
3352         if (test_bit(__I40E_DOWN, &vsi->state))
3353                 return;
3354
3355         set_bit(__I40E_NEEDS_RESTART, &vsi->state);
3356         if (vsi->netdev && netif_running(vsi->netdev)) {
3357                 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
3358         } else {
3359                 set_bit(__I40E_DOWN, &vsi->state);
3360                 i40e_down(vsi);
3361         }
3362 }
3363
3364 /**
3365  * i40e_unquiesce_vsi - Resume a given VSI
3366  * @vsi: the VSI being resumed
3367  **/
3368 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
3369 {
3370         if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
3371                 return;
3372
3373         clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
3374         if (vsi->netdev && netif_running(vsi->netdev))
3375                 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
3376         else
3377                 i40e_up(vsi);   /* this clears the DOWN bit */
3378 }
3379
3380 /**
3381  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
3382  * @pf: the PF
3383  **/
3384 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
3385 {
3386         int v;
3387
3388         for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
3389                 if (pf->vsi[v])
3390                         i40e_quiesce_vsi(pf->vsi[v]);
3391         }
3392 }
3393
3394 /**
3395  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
3396  * @pf: the PF
3397  **/
3398 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
3399 {
3400         int v;
3401
3402         for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
3403                 if (pf->vsi[v])
3404                         i40e_unquiesce_vsi(pf->vsi[v]);
3405         }
3406 }
3407
3408 /**
3409  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
3410  * @dcbcfg: the corresponding DCBx configuration structure
3411  *
3412  * Return the number of TCs from given DCBx configuration
3413  **/
3414 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
3415 {
3416         u8 num_tc = 0;
3417         int i;
3418
3419         /* Scan the ETS Config Priority Table to find
3420          * traffic class enabled for a given priority
3421          * and use the traffic class index to get the
3422          * number of traffic classes enabled
3423          */
3424         for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3425                 if (dcbcfg->etscfg.prioritytable[i] > num_tc)
3426                         num_tc = dcbcfg->etscfg.prioritytable[i];
3427         }
3428
3429         /* Traffic class index starts from zero so
3430          * increment to return the actual count
3431          */
3432         return num_tc + 1;
3433 }
3434
3435 /**
3436  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
3437  * @dcbcfg: the corresponding DCBx configuration structure
3438  *
3439  * Query the current DCB configuration and return the number of
3440  * traffic classes enabled from the given DCBX config
3441  **/
3442 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
3443 {
3444         u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
3445         u8 enabled_tc = 1;
3446         u8 i;
3447
3448         for (i = 0; i < num_tc; i++)
3449                 enabled_tc |= 1 << i;
3450
3451         return enabled_tc;
3452 }
3453
3454 /**
3455  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
3456  * @pf: PF being queried
3457  *
3458  * Return number of traffic classes enabled for the given PF
3459  **/
3460 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
3461 {
3462         struct i40e_hw *hw = &pf->hw;
3463         u8 i, enabled_tc;
3464         u8 num_tc = 0;
3465         struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3466
3467         /* If DCB is not enabled then always in single TC */
3468         if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3469                 return 1;
3470
3471         /* MFP mode return count of enabled TCs for this PF */
3472         if (pf->flags & I40E_FLAG_MFP_ENABLED) {
3473                 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3474                 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3475                         if (enabled_tc & (1 << i))
3476                                 num_tc++;
3477                 }
3478                 return num_tc;
3479         }
3480
3481         /* SFP mode will be enabled for all TCs on port */
3482         return i40e_dcb_get_num_tc(dcbcfg);
3483 }
3484
3485 /**
3486  * i40e_pf_get_default_tc - Get bitmap for first enabled TC
3487  * @pf: PF being queried
3488  *
3489  * Return a bitmap for first enabled traffic class for this PF.
3490  **/
3491 static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
3492 {
3493         u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3494         u8 i = 0;
3495
3496         if (!enabled_tc)
3497                 return 0x1; /* TC0 */
3498
3499         /* Find the first enabled TC */
3500         for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3501                 if (enabled_tc & (1 << i))
3502                         break;
3503         }
3504
3505         return 1 << i;
3506 }
3507
3508 /**
3509  * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
3510  * @pf: PF being queried
3511  *
3512  * Return a bitmap for enabled traffic classes for this PF.
3513  **/
3514 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
3515 {
3516         /* If DCB is not enabled for this PF then just return default TC */
3517         if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3518                 return i40e_pf_get_default_tc(pf);
3519
3520         /* MFP mode will have enabled TCs set by FW */
3521         if (pf->flags & I40E_FLAG_MFP_ENABLED)
3522                 return pf->hw.func_caps.enabled_tcmap;
3523
3524         /* SFP mode we want PF to be enabled for all TCs */
3525         return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
3526 }
3527
3528 /**
3529  * i40e_vsi_get_bw_info - Query VSI BW Information
3530  * @vsi: the VSI being queried
3531  *
3532  * Returns 0 on success, negative value on failure
3533  **/
3534 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
3535 {
3536         struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
3537         struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
3538         struct i40e_pf *pf = vsi->back;
3539         struct i40e_hw *hw = &pf->hw;
3540         i40e_status aq_ret;
3541         u32 tc_bw_max;
3542         int i;
3543
3544         /* Get the VSI level BW configuration */
3545         aq_ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
3546         if (aq_ret) {
3547                 dev_info(&pf->pdev->dev,
3548                          "couldn't get pf vsi bw config, err %d, aq_err %d\n",
3549                          aq_ret, pf->hw.aq.asq_last_status);
3550                 return -EINVAL;
3551         }
3552
3553         /* Get the VSI level BW configuration per TC */
3554         aq_ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
3555                                                   NULL);
3556         if (aq_ret) {
3557                 dev_info(&pf->pdev->dev,
3558                          "couldn't get pf vsi ets bw config, err %d, aq_err %d\n",
3559                          aq_ret, pf->hw.aq.asq_last_status);
3560                 return -EINVAL;
3561         }
3562
3563         if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
3564                 dev_info(&pf->pdev->dev,
3565                          "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
3566                          bw_config.tc_valid_bits,
3567                          bw_ets_config.tc_valid_bits);
3568                 /* Still continuing */
3569         }
3570
3571         vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
3572         vsi->bw_max_quanta = bw_config.max_bw;
3573         tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
3574                     (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
3575         for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3576                 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
3577                 vsi->bw_ets_limit_credits[i] =
3578                                         le16_to_cpu(bw_ets_config.credits[i]);
3579                 /* 3 bits out of 4 for each TC */
3580                 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
3581         }
3582
3583         return 0;
3584 }
3585
3586 /**
3587  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
3588  * @vsi: the VSI being configured
3589  * @enabled_tc: TC bitmap
3590  * @bw_credits: BW shared credits per TC
3591  *
3592  * Returns 0 on success, negative value on failure
3593  **/
3594 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
3595                                        u8 *bw_share)
3596 {
3597         struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
3598         i40e_status aq_ret;
3599         int i;
3600
3601         bw_data.tc_valid_bits = enabled_tc;
3602         for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3603                 bw_data.tc_bw_credits[i] = bw_share[i];
3604
3605         aq_ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
3606                                           NULL);
3607         if (aq_ret) {
3608                 dev_info(&vsi->back->pdev->dev,
3609                          "%s: AQ command Config VSI BW allocation per TC failed = %d\n",
3610                          __func__, vsi->back->hw.aq.asq_last_status);
3611                 return -EINVAL;
3612         }
3613
3614         for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3615                 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
3616
3617         return 0;
3618 }
3619
3620 /**
3621  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
3622  * @vsi: the VSI being configured
3623  * @enabled_tc: TC map to be enabled
3624  *
3625  **/
3626 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3627 {
3628         struct net_device *netdev = vsi->netdev;
3629         struct i40e_pf *pf = vsi->back;
3630         struct i40e_hw *hw = &pf->hw;
3631         u8 netdev_tc = 0;
3632         int i;
3633         struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3634
3635         if (!netdev)
3636                 return;
3637
3638         if (!enabled_tc) {
3639                 netdev_reset_tc(netdev);
3640                 return;
3641         }
3642
3643         /* Set up actual enabled TCs on the VSI */
3644         if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
3645                 return;
3646
3647         /* set per TC queues for the VSI */
3648         for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3649                 /* Only set TC queues for enabled tcs
3650                  *
3651                  * e.g. For a VSI that has TC0 and TC3 enabled the
3652                  * enabled_tc bitmap would be 0x00001001; the driver
3653                  * will set the numtc for netdev as 2 that will be
3654                  * referenced by the netdev layer as TC 0 and 1.
3655                  */
3656                 if (vsi->tc_config.enabled_tc & (1 << i))
3657                         netdev_set_tc_queue(netdev,
3658                                         vsi->tc_config.tc_info[i].netdev_tc,
3659                                         vsi->tc_config.tc_info[i].qcount,
3660                                         vsi->tc_config.tc_info[i].qoffset);
3661         }
3662
3663         /* Assign UP2TC map for the VSI */
3664         for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3665                 /* Get the actual TC# for the UP */
3666                 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
3667                 /* Get the mapped netdev TC# for the UP */
3668                 netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
3669                 netdev_set_prio_tc_map(netdev, i, netdev_tc);
3670         }
3671 }
3672
3673 /**
3674  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
3675  * @vsi: the VSI being configured
3676  * @ctxt: the ctxt buffer returned from AQ VSI update param command
3677  **/
3678 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
3679                                       struct i40e_vsi_context *ctxt)
3680 {
3681         /* copy just the sections touched not the entire info
3682          * since not all sections are valid as returned by
3683          * update vsi params
3684          */
3685         vsi->info.mapping_flags = ctxt->info.mapping_flags;
3686         memcpy(&vsi->info.queue_mapping,
3687                &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
3688         memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
3689                sizeof(vsi->info.tc_mapping));
3690 }
3691
3692 /**
3693  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
3694  * @vsi: VSI to be configured
3695  * @enabled_tc: TC bitmap
3696  *
3697  * This configures a particular VSI for TCs that are mapped to the
3698  * given TC bitmap. It uses default bandwidth share for TCs across
3699  * VSIs to configure TC for a particular VSI.
3700  *
3701  * NOTE:
3702  * It is expected that the VSI queues have been quisced before calling
3703  * this function.
3704  **/
3705 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3706 {
3707         u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
3708         struct i40e_vsi_context ctxt;
3709         int ret = 0;
3710         int i;
3711
3712         /* Check if enabled_tc is same as existing or new TCs */
3713         if (vsi->tc_config.enabled_tc == enabled_tc)
3714                 return ret;
3715
3716         /* Enable ETS TCs with equal BW Share for now across all VSIs */
3717         for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3718                 if (enabled_tc & (1 << i))
3719                         bw_share[i] = 1;
3720         }
3721
3722         ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
3723         if (ret) {
3724                 dev_info(&vsi->back->pdev->dev,
3725                          "Failed configuring TC map %d for VSI %d\n",
3726                          enabled_tc, vsi->seid);
3727                 goto out;
3728         }
3729
3730         /* Update Queue Pairs Mapping for currently enabled UPs */
3731         ctxt.seid = vsi->seid;
3732         ctxt.pf_num = vsi->back->hw.pf_id;
3733         ctxt.vf_num = 0;
3734         ctxt.uplink_seid = vsi->uplink_seid;
3735         memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
3736         i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
3737
3738         /* Update the VSI after updating the VSI queue-mapping information */
3739         ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3740         if (ret) {
3741                 dev_info(&vsi->back->pdev->dev,
3742                          "update vsi failed, aq_err=%d\n",
3743                          vsi->back->hw.aq.asq_last_status);
3744                 goto out;
3745         }
3746         /* update the local VSI info with updated queue map */
3747         i40e_vsi_update_queue_map(vsi, &ctxt);
3748         vsi->info.valid_sections = 0;
3749
3750         /* Update current VSI BW information */
3751         ret = i40e_vsi_get_bw_info(vsi);
3752         if (ret) {
3753                 dev_info(&vsi->back->pdev->dev,
3754                          "Failed updating vsi bw info, aq_err=%d\n",
3755                          vsi->back->hw.aq.asq_last_status);
3756                 goto out;
3757         }
3758
3759         /* Update the netdev TC setup */
3760         i40e_vsi_config_netdev_tc(vsi, enabled_tc);
3761 out:
3762         return ret;
3763 }
3764
3765 /**
3766  * i40e_up_complete - Finish the last steps of bringing up a connection
3767  * @vsi: the VSI being configured
3768  **/
3769 static int i40e_up_complete(struct i40e_vsi *vsi)
3770 {
3771         struct i40e_pf *pf = vsi->back;
3772         int err;
3773
3774         if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3775                 i40e_vsi_configure_msix(vsi);
3776         else
3777                 i40e_configure_msi_and_legacy(vsi);
3778
3779         /* start rings */
3780         err = i40e_vsi_control_rings(vsi, true);
3781         if (err)
3782                 return err;
3783
3784         clear_bit(__I40E_DOWN, &vsi->state);
3785         i40e_napi_enable_all(vsi);
3786         i40e_vsi_enable_irq(vsi);
3787
3788         if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
3789             (vsi->netdev)) {
3790                 netdev_info(vsi->netdev, "NIC Link is Up\n");
3791                 netif_tx_start_all_queues(vsi->netdev);
3792                 netif_carrier_on(vsi->netdev);
3793         } else if (vsi->netdev) {
3794                 netdev_info(vsi->netdev, "NIC Link is Down\n");
3795         }
3796         i40e_service_event_schedule(pf);
3797
3798         return 0;
3799 }
3800
3801 /**
3802  * i40e_vsi_reinit_locked - Reset the VSI
3803  * @vsi: the VSI being configured
3804  *
3805  * Rebuild the ring structs after some configuration
3806  * has changed, e.g. MTU size.
3807  **/
3808 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
3809 {
3810         struct i40e_pf *pf = vsi->back;
3811
3812         WARN_ON(in_interrupt());
3813         while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
3814                 usleep_range(1000, 2000);
3815         i40e_down(vsi);
3816
3817         /* Give a VF some time to respond to the reset.  The
3818          * two second wait is based upon the watchdog cycle in
3819          * the VF driver.
3820          */
3821         if (vsi->type == I40E_VSI_SRIOV)
3822                 msleep(2000);
3823         i40e_up(vsi);
3824         clear_bit(__I40E_CONFIG_BUSY, &pf->state);
3825 }
3826
3827 /**
3828  * i40e_up - Bring the connection back up after being down
3829  * @vsi: the VSI being configured
3830  **/
3831 int i40e_up(struct i40e_vsi *vsi)
3832 {
3833         int err;
3834
3835         err = i40e_vsi_configure(vsi);
3836         if (!err)
3837                 err = i40e_up_complete(vsi);
3838
3839         return err;
3840 }
3841
3842 /**
3843  * i40e_down - Shutdown the connection processing
3844  * @vsi: the VSI being stopped
3845  **/
3846 void i40e_down(struct i40e_vsi *vsi)
3847 {
3848         int i;
3849
3850         /* It is assumed that the caller of this function
3851          * sets the vsi->state __I40E_DOWN bit.
3852          */
3853         if (vsi->netdev) {
3854                 netif_carrier_off(vsi->netdev);
3855                 netif_tx_disable(vsi->netdev);
3856         }
3857         i40e_vsi_disable_irq(vsi);
3858         i40e_vsi_control_rings(vsi, false);
3859         i40e_napi_disable_all(vsi);
3860
3861         for (i = 0; i < vsi->num_queue_pairs; i++) {
3862                 i40e_clean_tx_ring(vsi->tx_rings[i]);
3863                 i40e_clean_rx_ring(vsi->rx_rings[i]);
3864         }
3865 }
3866
3867 /**
3868  * i40e_setup_tc - configure multiple traffic classes
3869  * @netdev: net device to configure
3870  * @tc: number of traffic classes to enable
3871  **/
3872 static int i40e_setup_tc(struct net_device *netdev, u8 tc)
3873 {
3874         struct i40e_netdev_priv *np = netdev_priv(netdev);
3875         struct i40e_vsi *vsi = np->vsi;
3876         struct i40e_pf *pf = vsi->back;
3877         u8 enabled_tc = 0;
3878         int ret = -EINVAL;
3879         int i;
3880
3881         /* Check if DCB enabled to continue */
3882         if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
3883                 netdev_info(netdev, "DCB is not enabled for adapter\n");
3884                 goto exit;
3885         }
3886
3887         /* Check if MFP enabled */
3888         if (pf->flags & I40E_FLAG_MFP_ENABLED) {
3889                 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
3890                 goto exit;
3891         }
3892
3893         /* Check whether tc count is within enabled limit */
3894         if (tc > i40e_pf_get_num_tc(pf)) {
3895                 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
3896                 goto exit;
3897         }
3898
3899         /* Generate TC map for number of tc requested */
3900         for (i = 0; i < tc; i++)
3901                 enabled_tc |= (1 << i);
3902
3903         /* Requesting same TC configuration as already enabled */
3904         if (enabled_tc == vsi->tc_config.enabled_tc)
3905                 return 0;
3906
3907         /* Quiesce VSI queues */
3908         i40e_quiesce_vsi(vsi);
3909
3910         /* Configure VSI for enabled TCs */
3911         ret = i40e_vsi_config_tc(vsi, enabled_tc);
3912         if (ret) {
3913                 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
3914                             vsi->seid);
3915                 goto exit;
3916         }
3917
3918         /* Unquiesce VSI */
3919         i40e_unquiesce_vsi(vsi);
3920
3921 exit:
3922         return ret;
3923 }
3924
3925 /**
3926  * i40e_open - Called when a network interface is made active
3927  * @netdev: network interface device structure
3928  *
3929  * The open entry point is called when a network interface is made
3930  * active by the system (IFF_UP).  At this point all resources needed
3931  * for transmit and receive operations are allocated, the interrupt
3932  * handler is registered with the OS, the netdev watchdog subtask is
3933  * enabled, and the stack is notified that the interface is ready.
3934  *
3935  * Returns 0 on success, negative value on failure
3936  **/
3937 static int i40e_open(struct net_device *netdev)
3938 {
3939         struct i40e_netdev_priv *np = netdev_priv(netdev);
3940         struct i40e_vsi *vsi = np->vsi;
3941         struct i40e_pf *pf = vsi->back;
3942         char int_name[IFNAMSIZ];
3943         int err;
3944
3945         /* disallow open during test */
3946         if (test_bit(__I40E_TESTING, &pf->state))
3947                 return -EBUSY;
3948
3949         netif_carrier_off(netdev);
3950
3951         /* allocate descriptors */
3952         err = i40e_vsi_setup_tx_resources(vsi);
3953         if (err)
3954                 goto err_setup_tx;
3955         err = i40e_vsi_setup_rx_resources(vsi);
3956         if (err)
3957                 goto err_setup_rx;
3958
3959         err = i40e_vsi_configure(vsi);
3960         if (err)
3961                 goto err_setup_rx;
3962
3963         snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
3964                  dev_driver_string(&pf->pdev->dev), netdev->name);
3965         err = i40e_vsi_request_irq(vsi, int_name);
3966         if (err)
3967                 goto err_setup_rx;
3968
3969         err = i40e_up_complete(vsi);
3970         if (err)
3971                 goto err_up_complete;
3972
3973         if ((vsi->type == I40E_VSI_MAIN) || (vsi->type == I40E_VSI_VMDQ2)) {
3974                 err = i40e_aq_set_vsi_broadcast(&pf->hw, vsi->seid, true, NULL);
3975                 if (err)
3976                         netdev_info(netdev,
3977                                     "couldn't set broadcast err %d aq_err %d\n",
3978                                     err, pf->hw.aq.asq_last_status);
3979         }
3980
3981         return 0;
3982
3983 err_up_complete:
3984         i40e_down(vsi);
3985         i40e_vsi_free_irq(vsi);
3986 err_setup_rx:
3987         i40e_vsi_free_rx_resources(vsi);
3988 err_setup_tx:
3989         i40e_vsi_free_tx_resources(vsi);
3990         if (vsi == pf->vsi[pf->lan_vsi])
3991                 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
3992
3993         return err;
3994 }
3995
3996 /**
3997  * i40e_close - Disables a network interface
3998  * @netdev: network interface device structure
3999  *
4000  * The close entry point is called when an interface is de-activated
4001  * by the OS.  The hardware is still under the driver's control, but
4002  * this netdev interface is disabled.
4003  *
4004  * Returns 0, this is not allowed to fail
4005  **/
4006 static int i40e_close(struct net_device *netdev)
4007 {
4008         struct i40e_netdev_priv *np = netdev_priv(netdev);
4009         struct i40e_vsi *vsi = np->vsi;
4010
4011         if (test_and_set_bit(__I40E_DOWN, &vsi->state))
4012                 return 0;
4013
4014         i40e_down(vsi);
4015         i40e_vsi_free_irq(vsi);
4016
4017         i40e_vsi_free_tx_resources(vsi);
4018         i40e_vsi_free_rx_resources(vsi);
4019
4020         return 0;
4021 }
4022
4023 /**
4024  * i40e_do_reset - Start a PF or Core Reset sequence
4025  * @pf: board private structure
4026  * @reset_flags: which reset is requested
4027  *
4028  * The essential difference in resets is that the PF Reset
4029  * doesn't clear the packet buffers, doesn't reset the PE
4030  * firmware, and doesn't bother the other PFs on the chip.
4031  **/
4032 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
4033 {
4034         u32 val;
4035
4036         WARN_ON(in_interrupt());
4037
4038         /* do the biggest reset indicated */
4039         if (reset_flags & (1 << __I40E_GLOBAL_RESET_REQUESTED)) {
4040
4041                 /* Request a Global Reset
4042                  *
4043                  * This will start the chip's countdown to the actual full
4044                  * chip reset event, and a warning interrupt to be sent
4045                  * to all PFs, including the requestor.  Our handler
4046                  * for the warning interrupt will deal with the shutdown
4047                  * and recovery of the switch setup.
4048                  */
4049                 dev_info(&pf->pdev->dev, "GlobalR requested\n");
4050                 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4051                 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
4052                 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4053
4054         } else if (reset_flags & (1 << __I40E_CORE_RESET_REQUESTED)) {
4055
4056                 /* Request a Core Reset
4057                  *
4058                  * Same as Global Reset, except does *not* include the MAC/PHY
4059                  */
4060                 dev_info(&pf->pdev->dev, "CoreR requested\n");
4061                 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4062                 val |= I40E_GLGEN_RTRIG_CORER_MASK;
4063                 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4064                 i40e_flush(&pf->hw);
4065
4066         } else if (reset_flags & (1 << __I40E_EMP_RESET_REQUESTED)) {
4067
4068                 /* Request a Firmware Reset
4069                  *
4070                  * Same as Global reset, plus restarting the
4071                  * embedded firmware engine.
4072                  */
4073                 /* enable EMP Reset */
4074                 val = rd32(&pf->hw, I40E_GLGEN_RSTENA_EMP);
4075                 val |= I40E_GLGEN_RSTENA_EMP_EMP_RST_ENA_MASK;
4076                 wr32(&pf->hw, I40E_GLGEN_RSTENA_EMP, val);
4077
4078                 /* force the reset */
4079                 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4080                 val |= I40E_GLGEN_RTRIG_EMPFWR_MASK;
4081                 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4082                 i40e_flush(&pf->hw);
4083
4084         } else if (reset_flags & (1 << __I40E_PF_RESET_REQUESTED)) {
4085
4086                 /* Request a PF Reset
4087                  *
4088                  * Resets only the PF-specific registers
4089                  *
4090                  * This goes directly to the tear-down and rebuild of
4091                  * the switch, since we need to do all the recovery as
4092                  * for the Core Reset.
4093                  */
4094                 dev_info(&pf->pdev->dev, "PFR requested\n");
4095                 i40e_handle_reset_warning(pf);
4096
4097         } else if (reset_flags & (1 << __I40E_REINIT_REQUESTED)) {
4098                 int v;
4099
4100                 /* Find the VSI(s) that requested a re-init */
4101                 dev_info(&pf->pdev->dev,
4102                          "VSI reinit requested\n");
4103                 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4104                         struct i40e_vsi *vsi = pf->vsi[v];
4105                         if (vsi != NULL &&
4106                             test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
4107                                 i40e_vsi_reinit_locked(pf->vsi[v]);
4108                                 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
4109                         }
4110                 }
4111
4112                 /* no further action needed, so return now */
4113                 return;
4114         } else {
4115                 dev_info(&pf->pdev->dev,
4116                          "bad reset request 0x%08x\n", reset_flags);
4117                 return;
4118         }
4119 }
4120
4121 /**
4122  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
4123  * @pf: board private structure
4124  * @e: event info posted on ARQ
4125  *
4126  * Handler for LAN Queue Overflow Event generated by the firmware for PF
4127  * and VF queues
4128  **/
4129 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
4130                                            struct i40e_arq_event_info *e)
4131 {
4132         struct i40e_aqc_lan_overflow *data =
4133                 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
4134         u32 queue = le32_to_cpu(data->prtdcb_rupto);
4135         u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
4136         struct i40e_hw *hw = &pf->hw;
4137         struct i40e_vf *vf;
4138         u16 vf_id;
4139
4140         dev_info(&pf->pdev->dev, "%s: Rx Queue Number = %d QTX_CTL=0x%08x\n",
4141                  __func__, queue, qtx_ctl);
4142
4143         /* Queue belongs to VF, find the VF and issue VF reset */
4144         if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
4145             >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
4146                 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
4147                          >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
4148                 vf_id -= hw->func_caps.vf_base_id;
4149                 vf = &pf->vf[vf_id];
4150                 i40e_vc_notify_vf_reset(vf);
4151                 /* Allow VF to process pending reset notification */
4152                 msleep(20);
4153                 i40e_reset_vf(vf, false);
4154         }
4155 }
4156
4157 /**
4158  * i40e_service_event_complete - Finish up the service event
4159  * @pf: board private structure
4160  **/
4161 static void i40e_service_event_complete(struct i40e_pf *pf)
4162 {
4163         BUG_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
4164
4165         /* flush memory to make sure state is correct before next watchog */
4166         smp_mb__before_clear_bit();
4167         clear_bit(__I40E_SERVICE_SCHED, &pf->state);
4168 }
4169
4170 /**
4171  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
4172  * @pf: board private structure
4173  **/
4174 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
4175 {
4176         if (!(pf->flags & I40E_FLAG_FDIR_REQUIRES_REINIT))
4177                 return;
4178
4179         pf->flags &= ~I40E_FLAG_FDIR_REQUIRES_REINIT;
4180
4181         /* if interface is down do nothing */
4182         if (test_bit(__I40E_DOWN, &pf->state))
4183                 return;
4184 }
4185
4186 /**
4187  * i40e_vsi_link_event - notify VSI of a link event
4188  * @vsi: vsi to be notified
4189  * @link_up: link up or down
4190  **/
4191 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
4192 {
4193         if (!vsi)
4194                 return;
4195
4196         switch (vsi->type) {
4197         case I40E_VSI_MAIN:
4198                 if (!vsi->netdev || !vsi->netdev_registered)
4199                         break;
4200
4201                 if (link_up) {
4202                         netif_carrier_on(vsi->netdev);
4203                         netif_tx_wake_all_queues(vsi->netdev);
4204                 } else {
4205                         netif_carrier_off(vsi->netdev);
4206                         netif_tx_stop_all_queues(vsi->netdev);
4207                 }
4208                 break;
4209
4210         case I40E_VSI_SRIOV:
4211                 break;
4212
4213         case I40E_VSI_VMDQ2:
4214         case I40E_VSI_CTRL:
4215         case I40E_VSI_MIRROR:
4216         default:
4217                 /* there is no notification for other VSIs */
4218                 break;
4219         }
4220 }
4221
4222 /**
4223  * i40e_veb_link_event - notify elements on the veb of a link event
4224  * @veb: veb to be notified
4225  * @link_up: link up or down
4226  **/
4227 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
4228 {
4229         struct i40e_pf *pf;
4230         int i;
4231
4232         if (!veb || !veb->pf)
4233                 return;
4234         pf = veb->pf;
4235
4236         /* depth first... */
4237         for (i = 0; i < I40E_MAX_VEB; i++)
4238                 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
4239                         i40e_veb_link_event(pf->veb[i], link_up);
4240
4241         /* ... now the local VSIs */
4242         for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
4243                 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
4244                         i40e_vsi_link_event(pf->vsi[i], link_up);
4245 }
4246
4247 /**
4248  * i40e_link_event - Update netif_carrier status
4249  * @pf: board private structure
4250  **/
4251 static void i40e_link_event(struct i40e_pf *pf)
4252 {
4253         bool new_link, old_link;
4254
4255         new_link = (pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP);
4256         old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
4257
4258         if (new_link == old_link)
4259                 return;
4260
4261         if (!test_bit(__I40E_DOWN, &pf->vsi[pf->lan_vsi]->state))
4262                 netdev_info(pf->vsi[pf->lan_vsi]->netdev,
4263                             "NIC Link is %s\n", (new_link ? "Up" : "Down"));
4264
4265         /* Notify the base of the switch tree connected to
4266          * the link.  Floating VEBs are not notified.
4267          */
4268         if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
4269                 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
4270         else
4271                 i40e_vsi_link_event(pf->vsi[pf->lan_vsi], new_link);
4272
4273         if (pf->vf)
4274                 i40e_vc_notify_link_state(pf);
4275 }
4276
4277 /**
4278  * i40e_check_hang_subtask - Check for hung queues and dropped interrupts
4279  * @pf: board private structure
4280  *
4281  * Set the per-queue flags to request a check for stuck queues in the irq
4282  * clean functions, then force interrupts to be sure the irq clean is called.
4283  **/
4284 static void i40e_check_hang_subtask(struct i40e_pf *pf)
4285 {
4286         int i, v;
4287
4288         /* If we're down or resetting, just bail */
4289         if (test_bit(__I40E_CONFIG_BUSY, &pf->state))
4290                 return;
4291
4292         /* for each VSI/netdev
4293          *     for each Tx queue
4294          *         set the check flag
4295          *     for each q_vector
4296          *         force an interrupt
4297          */
4298         for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4299                 struct i40e_vsi *vsi = pf->vsi[v];
4300                 int armed = 0;
4301
4302                 if (!pf->vsi[v] ||
4303                     test_bit(__I40E_DOWN, &vsi->state) ||
4304                     (vsi->netdev && !netif_carrier_ok(vsi->netdev)))
4305                         continue;
4306
4307                 for (i = 0; i < vsi->num_queue_pairs; i++) {
4308                         set_check_for_tx_hang(vsi->tx_rings[i]);
4309                         if (test_bit(__I40E_HANG_CHECK_ARMED,
4310                                      &vsi->tx_rings[i]->state))
4311                                 armed++;
4312                 }
4313
4314                 if (armed) {
4315                         if (!(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
4316                                 wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0,
4317                                      (I40E_PFINT_DYN_CTL0_INTENA_MASK |
4318                                       I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK));
4319                         } else {
4320                                 u16 vec = vsi->base_vector - 1;
4321                                 u32 val = (I40E_PFINT_DYN_CTLN_INTENA_MASK |
4322                                            I40E_PFINT_DYN_CTLN_SWINT_TRIG_MASK);
4323                                 for (i = 0; i < vsi->num_q_vectors; i++, vec++)
4324                                         wr32(&vsi->back->hw,
4325                                              I40E_PFINT_DYN_CTLN(vec), val);
4326                         }
4327                         i40e_flush(&vsi->back->hw);
4328                 }
4329         }
4330 }
4331
4332 /**
4333  * i40e_watchdog_subtask - Check and bring link up
4334  * @pf: board private structure
4335  **/
4336 static void i40e_watchdog_subtask(struct i40e_pf *pf)
4337 {
4338         int i;
4339
4340         /* if interface is down do nothing */
4341         if (test_bit(__I40E_DOWN, &pf->state) ||
4342             test_bit(__I40E_CONFIG_BUSY, &pf->state))
4343                 return;
4344
4345         /* Update the stats for active netdevs so the network stack
4346          * can look at updated numbers whenever it cares to
4347          */
4348         for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
4349                 if (pf->vsi[i] && pf->vsi[i]->netdev)
4350                         i40e_update_stats(pf->vsi[i]);
4351
4352         /* Update the stats for the active switching components */
4353         for (i = 0; i < I40E_MAX_VEB; i++)
4354                 if (pf->veb[i])
4355                         i40e_update_veb_stats(pf->veb[i]);
4356 }
4357
4358 /**
4359  * i40e_reset_subtask - Set up for resetting the device and driver
4360  * @pf: board private structure
4361  **/
4362 static void i40e_reset_subtask(struct i40e_pf *pf)
4363 {
4364         u32 reset_flags = 0;
4365
4366         if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
4367                 reset_flags |= (1 << __I40E_REINIT_REQUESTED);
4368                 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
4369         }
4370         if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
4371                 reset_flags |= (1 << __I40E_PF_RESET_REQUESTED);
4372                 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
4373         }
4374         if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
4375                 reset_flags |= (1 << __I40E_CORE_RESET_REQUESTED);
4376                 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
4377         }
4378         if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
4379                 reset_flags |= (1 << __I40E_GLOBAL_RESET_REQUESTED);
4380                 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
4381         }
4382
4383         /* If there's a recovery already waiting, it takes
4384          * precedence before starting a new reset sequence.
4385          */
4386         if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
4387                 i40e_handle_reset_warning(pf);
4388                 return;
4389         }
4390
4391         /* If we're already down or resetting, just bail */
4392         if (reset_flags &&
4393             !test_bit(__I40E_DOWN, &pf->state) &&
4394             !test_bit(__I40E_CONFIG_BUSY, &pf->state))
4395                 i40e_do_reset(pf, reset_flags);
4396 }
4397
4398 /**
4399  * i40e_handle_link_event - Handle link event
4400  * @pf: board private structure
4401  * @e: event info posted on ARQ
4402  **/
4403 static void i40e_handle_link_event(struct i40e_pf *pf,
4404                                    struct i40e_arq_event_info *e)
4405 {
4406         struct i40e_hw *hw = &pf->hw;
4407         struct i40e_aqc_get_link_status *status =
4408                 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
4409         struct i40e_link_status *hw_link_info = &hw->phy.link_info;
4410
4411         /* save off old link status information */
4412         memcpy(&pf->hw.phy.link_info_old, hw_link_info,
4413                sizeof(pf->hw.phy.link_info_old));
4414
4415         /* update link status */
4416         hw_link_info->phy_type = (enum i40e_aq_phy_type)status->phy_type;
4417         hw_link_info->link_speed = (enum i40e_aq_link_speed)status->link_speed;
4418         hw_link_info->link_info = status->link_info;
4419         hw_link_info->an_info = status->an_info;
4420         hw_link_info->ext_info = status->ext_info;
4421         hw_link_info->lse_enable =
4422                 le16_to_cpu(status->command_flags) &
4423                             I40E_AQ_LSE_ENABLE;
4424
4425         /* process the event */
4426         i40e_link_event(pf);
4427
4428         /* Do a new status request to re-enable LSE reporting
4429          * and load new status information into the hw struct,
4430          * then see if the status changed while processing the
4431          * initial event.
4432          */
4433         i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
4434         i40e_link_event(pf);
4435 }
4436
4437 /**
4438  * i40e_clean_adminq_subtask - Clean the AdminQ rings
4439  * @pf: board private structure
4440  **/
4441 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
4442 {
4443         struct i40e_arq_event_info event;
4444         struct i40e_hw *hw = &pf->hw;
4445         u16 pending, i = 0;
4446         i40e_status ret;
4447         u16 opcode;
4448         u32 val;
4449
4450         if (!test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state))
4451                 return;
4452
4453         event.msg_size = I40E_MAX_AQ_BUF_SIZE;
4454         event.msg_buf = kzalloc(event.msg_size, GFP_KERNEL);
4455         if (!event.msg_buf)
4456                 return;
4457
4458         do {
4459                 ret = i40e_clean_arq_element(hw, &event, &pending);
4460                 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
4461                         dev_info(&pf->pdev->dev, "No ARQ event found\n");
4462                         break;
4463                 } else if (ret) {
4464                         dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
4465                         break;
4466                 }
4467
4468                 opcode = le16_to_cpu(event.desc.opcode);
4469                 switch (opcode) {
4470
4471                 case i40e_aqc_opc_get_link_status:
4472                         i40e_handle_link_event(pf, &event);
4473                         break;
4474                 case i40e_aqc_opc_send_msg_to_pf:
4475                         ret = i40e_vc_process_vf_msg(pf,
4476                                         le16_to_cpu(event.desc.retval),
4477                                         le32_to_cpu(event.desc.cookie_high),
4478                                         le32_to_cpu(event.desc.cookie_low),
4479                                         event.msg_buf,
4480                                         event.msg_size);
4481                         break;
4482                 case i40e_aqc_opc_lldp_update_mib:
4483                         dev_info(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
4484                         break;
4485                 case i40e_aqc_opc_event_lan_overflow:
4486                         dev_info(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
4487                         i40e_handle_lan_overflow_event(pf, &event);
4488                         break;
4489                 default:
4490                         dev_info(&pf->pdev->dev,
4491                                  "ARQ Error: Unknown event %d received\n",
4492                                  event.desc.opcode);
4493                         break;
4494                 }
4495         } while (pending && (i++ < pf->adminq_work_limit));
4496
4497         clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
4498         /* re-enable Admin queue interrupt cause */
4499         val = rd32(hw, I40E_PFINT_ICR0_ENA);
4500         val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4501         wr32(hw, I40E_PFINT_ICR0_ENA, val);
4502         i40e_flush(hw);
4503
4504         kfree(event.msg_buf);
4505 }
4506
4507 /**
4508  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
4509  * @veb: pointer to the VEB instance
4510  *
4511  * This is a recursive function that first builds the attached VSIs then
4512  * recurses in to build the next layer of VEB.  We track the connections
4513  * through our own index numbers because the seid's from the HW could
4514  * change across the reset.
4515  **/
4516 static int i40e_reconstitute_veb(struct i40e_veb *veb)
4517 {
4518         struct i40e_vsi *ctl_vsi = NULL;
4519         struct i40e_pf *pf = veb->pf;
4520         int v, veb_idx;
4521         int ret;
4522
4523         /* build VSI that owns this VEB, temporarily attached to base VEB */
4524         for (v = 0; v < pf->hw.func_caps.num_vsis && !ctl_vsi; v++) {
4525                 if (pf->vsi[v] &&
4526                     pf->vsi[v]->veb_idx == veb->idx &&
4527                     pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
4528                         ctl_vsi = pf->vsi[v];
4529                         break;
4530                 }
4531         }
4532         if (!ctl_vsi) {
4533                 dev_info(&pf->pdev->dev,
4534                          "missing owner VSI for veb_idx %d\n", veb->idx);
4535                 ret = -ENOENT;
4536                 goto end_reconstitute;
4537         }
4538         if (ctl_vsi != pf->vsi[pf->lan_vsi])
4539                 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
4540         ret = i40e_add_vsi(ctl_vsi);
4541         if (ret) {
4542                 dev_info(&pf->pdev->dev,
4543                          "rebuild of owner VSI failed: %d\n", ret);
4544                 goto end_reconstitute;
4545         }
4546         i40e_vsi_reset_stats(ctl_vsi);
4547
4548         /* create the VEB in the switch and move the VSI onto the VEB */
4549         ret = i40e_add_veb(veb, ctl_vsi);
4550         if (ret)
4551                 goto end_reconstitute;
4552
4553         /* create the remaining VSIs attached to this VEB */
4554         for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4555                 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
4556                         continue;
4557
4558                 if (pf->vsi[v]->veb_idx == veb->idx) {
4559                         struct i40e_vsi *vsi = pf->vsi[v];
4560                         vsi->uplink_seid = veb->seid;
4561                         ret = i40e_add_vsi(vsi);
4562                         if (ret) {
4563                                 dev_info(&pf->pdev->dev,
4564                                          "rebuild of vsi_idx %d failed: %d\n",
4565                                          v, ret);
4566                                 goto end_reconstitute;
4567                         }
4568                         i40e_vsi_reset_stats(vsi);
4569                 }
4570         }
4571
4572         /* create any VEBs attached to this VEB - RECURSION */
4573         for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
4574                 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
4575                         pf->veb[veb_idx]->uplink_seid = veb->seid;
4576                         ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
4577                         if (ret)
4578                                 break;
4579                 }
4580         }
4581
4582 end_reconstitute:
4583         return ret;
4584 }
4585
4586 /**
4587  * i40e_get_capabilities - get info about the HW
4588  * @pf: the PF struct
4589  **/
4590 static int i40e_get_capabilities(struct i40e_pf *pf)
4591 {
4592         struct i40e_aqc_list_capabilities_element_resp *cap_buf;
4593         u16 data_size;
4594         int buf_len;
4595         int err;
4596
4597         buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
4598         do {
4599                 cap_buf = kzalloc(buf_len, GFP_KERNEL);
4600                 if (!cap_buf)
4601                         return -ENOMEM;
4602
4603                 /* this loads the data into the hw struct for us */
4604                 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
4605                                             &data_size,
4606                                             i40e_aqc_opc_list_func_capabilities,
4607                                             NULL);
4608                 /* data loaded, buffer no longer needed */
4609                 kfree(cap_buf);
4610
4611                 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
4612                         /* retry with a larger buffer */
4613                         buf_len = data_size;
4614                 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
4615                         dev_info(&pf->pdev->dev,
4616                                  "capability discovery failed: aq=%d\n",
4617                                  pf->hw.aq.asq_last_status);
4618                         return -ENODEV;
4619                 }
4620         } while (err);
4621
4622         if (pf->hw.revision_id == 0 && pf->hw.func_caps.npar_enable) {
4623                 pf->hw.func_caps.num_msix_vectors += 1;
4624                 pf->hw.func_caps.num_tx_qp =
4625                         min_t(int, pf->hw.func_caps.num_tx_qp,
4626                               I40E_MAX_NPAR_QPS);
4627         }
4628
4629         if (pf->hw.debug_mask & I40E_DEBUG_USER)
4630                 dev_info(&pf->pdev->dev,
4631                          "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",
4632                          pf->hw.pf_id, pf->hw.func_caps.num_vfs,
4633                          pf->hw.func_caps.num_msix_vectors,
4634                          pf->hw.func_caps.num_msix_vectors_vf,
4635                          pf->hw.func_caps.fd_filters_guaranteed,
4636                          pf->hw.func_caps.fd_filters_best_effort,
4637                          pf->hw.func_caps.num_tx_qp,
4638                          pf->hw.func_caps.num_vsis);
4639
4640 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
4641                        + pf->hw.func_caps.num_vfs)
4642         if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
4643                 dev_info(&pf->pdev->dev,
4644                          "got num_vsis %d, setting num_vsis to %d\n",
4645                          pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
4646                 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
4647         }
4648
4649         return 0;
4650 }
4651
4652 /**
4653  * i40e_fdir_setup - initialize the Flow Director resources
4654  * @pf: board private structure
4655  **/
4656 static void i40e_fdir_setup(struct i40e_pf *pf)
4657 {
4658         struct i40e_vsi *vsi;
4659         bool new_vsi = false;
4660         int err, i;
4661
4662         if (!(pf->flags & (I40E_FLAG_FDIR_ENABLED |
4663                            I40E_FLAG_FDIR_ATR_ENABLED)))
4664                 return;
4665
4666         pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
4667
4668         /* find existing or make new FDIR VSI */
4669         vsi = NULL;
4670         for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
4671                 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR)
4672                         vsi = pf->vsi[i];
4673         if (!vsi) {
4674                 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR, pf->mac_seid, 0);
4675                 if (!vsi) {
4676                         dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
4677                         pf->flags &= ~I40E_FLAG_FDIR_ENABLED;
4678                         return;
4679                 }
4680                 new_vsi = true;
4681         }
4682         WARN_ON(vsi->base_queue != I40E_FDIR_RING);
4683         i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_rings);
4684
4685         err = i40e_vsi_setup_tx_resources(vsi);
4686         if (!err)
4687                 err = i40e_vsi_setup_rx_resources(vsi);
4688         if (!err)
4689                 err = i40e_vsi_configure(vsi);
4690         if (!err && new_vsi) {
4691                 char int_name[IFNAMSIZ + 9];
4692                 snprintf(int_name, sizeof(int_name) - 1, "%s-fdir",
4693                          dev_driver_string(&pf->pdev->dev));
4694                 err = i40e_vsi_request_irq(vsi, int_name);
4695         }
4696         if (!err)
4697                 err = i40e_up_complete(vsi);
4698
4699         clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
4700 }
4701
4702 /**
4703  * i40e_fdir_teardown - release the Flow Director resources
4704  * @pf: board private structure
4705  **/
4706 static void i40e_fdir_teardown(struct i40e_pf *pf)
4707 {
4708         int i;
4709
4710         for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
4711                 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
4712                         i40e_vsi_release(pf->vsi[i]);
4713                         break;
4714                 }
4715         }
4716 }
4717
4718 /**
4719  * i40e_prep_for_reset - prep for the core to reset
4720  * @pf: board private structure
4721  *
4722  * Close up the VFs and other things in prep for pf Reset.
4723   **/
4724 static int i40e_prep_for_reset(struct i40e_pf *pf)
4725 {
4726         struct i40e_hw *hw = &pf->hw;
4727         i40e_status ret;
4728         u32 v;
4729
4730         clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
4731         if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
4732                 return 0;
4733
4734         dev_info(&pf->pdev->dev, "Tearing down internal switch for reset\n");
4735
4736         i40e_vc_notify_reset(pf);
4737
4738         /* quiesce the VSIs and their queues that are not already DOWN */
4739         i40e_pf_quiesce_all_vsi(pf);
4740
4741         for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4742                 if (pf->vsi[v])
4743                         pf->vsi[v]->seid = 0;
4744         }
4745
4746         i40e_shutdown_adminq(&pf->hw);
4747
4748         /* call shutdown HMC */
4749         ret = i40e_shutdown_lan_hmc(hw);
4750         if (ret) {
4751                 dev_info(&pf->pdev->dev, "shutdown_lan_hmc failed: %d\n", ret);
4752                 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
4753         }
4754         return ret;
4755 }
4756
4757 /**
4758  * i40e_reset_and_rebuild - reset and rebuid using a saved config
4759  * @pf: board private structure
4760  **/
4761 static void i40e_reset_and_rebuild(struct i40e_pf *pf)
4762 {
4763         struct i40e_driver_version dv;
4764         struct i40e_hw *hw = &pf->hw;
4765         i40e_status ret;
4766         u32 v;
4767
4768         /* Now we wait for GRST to settle out.
4769          * We don't have to delete the VEBs or VSIs from the hw switch
4770          * because the reset will make them disappear.
4771          */
4772         ret = i40e_pf_reset(hw);
4773         if (ret)
4774                 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
4775         pf->pfr_count++;
4776
4777         if (test_bit(__I40E_DOWN, &pf->state))
4778                 goto end_core_reset;
4779         dev_info(&pf->pdev->dev, "Rebuilding internal switch\n");
4780
4781         /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
4782         ret = i40e_init_adminq(&pf->hw);
4783         if (ret) {
4784                 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, %d\n", ret);
4785                 goto end_core_reset;
4786         }
4787
4788         ret = i40e_get_capabilities(pf);
4789         if (ret) {
4790                 dev_info(&pf->pdev->dev, "i40e_get_capabilities failed, %d\n",
4791                          ret);
4792                 goto end_core_reset;
4793         }
4794
4795         ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
4796                                 hw->func_caps.num_rx_qp,
4797                                 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
4798         if (ret) {
4799                 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
4800                 goto end_core_reset;
4801         }
4802         ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
4803         if (ret) {
4804                 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
4805                 goto end_core_reset;
4806         }
4807
4808         /* do basic switch setup */
4809         ret = i40e_setup_pf_switch(pf);
4810         if (ret)
4811                 goto end_core_reset;
4812
4813         /* Rebuild the VSIs and VEBs that existed before reset.
4814          * They are still in our local switch element arrays, so only
4815          * need to rebuild the switch model in the HW.
4816          *
4817          * If there were VEBs but the reconstitution failed, we'll try
4818          * try to recover minimal use by getting the basic PF VSI working.
4819          */
4820         if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
4821                 dev_info(&pf->pdev->dev, "attempting to rebuild switch\n");
4822                 /* find the one VEB connected to the MAC, and find orphans */
4823                 for (v = 0; v < I40E_MAX_VEB; v++) {
4824                         if (!pf->veb[v])
4825                                 continue;
4826
4827                         if (pf->veb[v]->uplink_seid == pf->mac_seid ||
4828                             pf->veb[v]->uplink_seid == 0) {
4829                                 ret = i40e_reconstitute_veb(pf->veb[v]);
4830
4831                                 if (!ret)
4832                                         continue;
4833
4834                                 /* If Main VEB failed, we're in deep doodoo,
4835                                  * so give up rebuilding the switch and set up
4836                                  * for minimal rebuild of PF VSI.
4837                                  * If orphan failed, we'll report the error
4838                                  * but try to keep going.
4839                                  */
4840                                 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
4841                                         dev_info(&pf->pdev->dev,
4842                                                  "rebuild of switch failed: %d, will try to set up simple PF connection\n",
4843                                                  ret);
4844                                         pf->vsi[pf->lan_vsi]->uplink_seid
4845                                                                 = pf->mac_seid;
4846                                         break;
4847                                 } else if (pf->veb[v]->uplink_seid == 0) {
4848                                         dev_info(&pf->pdev->dev,
4849                                                  "rebuild of orphan VEB failed: %d\n",
4850                                                  ret);
4851                                 }
4852                         }
4853                 }
4854         }
4855
4856         if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
4857                 dev_info(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
4858                 /* no VEB, so rebuild only the Main VSI */
4859                 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
4860                 if (ret) {
4861                         dev_info(&pf->pdev->dev,
4862                                  "rebuild of Main VSI failed: %d\n", ret);
4863                         goto end_core_reset;
4864                 }
4865         }
4866
4867         /* reinit the misc interrupt */
4868         if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4869                 ret = i40e_setup_misc_vector(pf);
4870
4871         /* restart the VSIs that were rebuilt and running before the reset */
4872         i40e_pf_unquiesce_all_vsi(pf);
4873
4874         /* tell the firmware that we're starting */
4875         dv.major_version = DRV_VERSION_MAJOR;
4876         dv.minor_version = DRV_VERSION_MINOR;
4877         dv.build_version = DRV_VERSION_BUILD;
4878         dv.subbuild_version = 0;
4879         i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
4880
4881         dev_info(&pf->pdev->dev, "PF reset done\n");
4882
4883 end_core_reset:
4884         clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
4885 }
4886
4887 /**
4888  * i40e_handle_reset_warning - prep for the pf to reset, reset and rebuild
4889  * @pf: board private structure
4890  *
4891  * Close up the VFs and other things in prep for a Core Reset,
4892  * then get ready to rebuild the world.
4893  **/
4894 static void i40e_handle_reset_warning(struct i40e_pf *pf)
4895 {
4896         i40e_status ret;
4897
4898         ret = i40e_prep_for_reset(pf);
4899         if (!ret)
4900                 i40e_reset_and_rebuild(pf);
4901 }
4902
4903 /**
4904  * i40e_handle_mdd_event
4905  * @pf: pointer to the pf structure
4906  *
4907  * Called from the MDD irq handler to identify possibly malicious vfs
4908  **/
4909 static void i40e_handle_mdd_event(struct i40e_pf *pf)
4910 {
4911         struct i40e_hw *hw = &pf->hw;
4912         bool mdd_detected = false;
4913         struct i40e_vf *vf;
4914         u32 reg;
4915         int i;
4916
4917         if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
4918                 return;
4919
4920         /* find what triggered the MDD event */
4921         reg = rd32(hw, I40E_GL_MDET_TX);
4922         if (reg & I40E_GL_MDET_TX_VALID_MASK) {
4923                 u8 func = (reg & I40E_GL_MDET_TX_FUNCTION_MASK)
4924                                 >> I40E_GL_MDET_TX_FUNCTION_SHIFT;
4925                 u8 event = (reg & I40E_GL_MDET_TX_EVENT_SHIFT)
4926                                 >> I40E_GL_MDET_TX_EVENT_SHIFT;
4927                 u8 queue = (reg & I40E_GL_MDET_TX_QUEUE_MASK)
4928                                 >> I40E_GL_MDET_TX_QUEUE_SHIFT;
4929                 dev_info(&pf->pdev->dev,
4930                          "Malicious Driver Detection TX event 0x%02x on q %d of function 0x%02x\n",
4931                          event, queue, func);
4932                 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
4933                 mdd_detected = true;
4934         }
4935         reg = rd32(hw, I40E_GL_MDET_RX);
4936         if (reg & I40E_GL_MDET_RX_VALID_MASK) {
4937                 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK)
4938                                 >> I40E_GL_MDET_RX_FUNCTION_SHIFT;
4939                 u8 event = (reg & I40E_GL_MDET_RX_EVENT_SHIFT)
4940                                 >> I40E_GL_MDET_RX_EVENT_SHIFT;
4941                 u8 queue = (reg & I40E_GL_MDET_RX_QUEUE_MASK)
4942                                 >> I40E_GL_MDET_RX_QUEUE_SHIFT;
4943                 dev_info(&pf->pdev->dev,
4944                          "Malicious Driver Detection RX event 0x%02x on q %d of function 0x%02x\n",
4945                          event, queue, func);
4946                 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
4947                 mdd_detected = true;
4948         }
4949
4950         /* see if one of the VFs needs its hand slapped */
4951         for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
4952                 vf = &(pf->vf[i]);
4953                 reg = rd32(hw, I40E_VP_MDET_TX(i));
4954                 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
4955                         wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
4956                         vf->num_mdd_events++;
4957                         dev_info(&pf->pdev->dev, "MDD TX event on VF %d\n", i);
4958                 }
4959
4960                 reg = rd32(hw, I40E_VP_MDET_RX(i));
4961                 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
4962                         wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
4963                         vf->num_mdd_events++;
4964                         dev_info(&pf->pdev->dev, "MDD RX event on VF %d\n", i);
4965                 }
4966
4967                 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
4968                         dev_info(&pf->pdev->dev,
4969                                  "Too many MDD events on VF %d, disabled\n", i);
4970                         dev_info(&pf->pdev->dev,
4971                                  "Use PF Control I/F to re-enable the VF\n");
4972                         set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
4973                 }
4974         }
4975
4976         /* re-enable mdd interrupt cause */
4977         clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
4978         reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4979         reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4980         wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4981         i40e_flush(hw);
4982 }
4983
4984 /**
4985  * i40e_service_task - Run the driver's async subtasks
4986  * @work: pointer to work_struct containing our data
4987  **/
4988 static void i40e_service_task(struct work_struct *work)
4989 {
4990         struct i40e_pf *pf = container_of(work,
4991                                           struct i40e_pf,
4992                                           service_task);
4993         unsigned long start_time = jiffies;
4994
4995         i40e_reset_subtask(pf);
4996         i40e_handle_mdd_event(pf);
4997         i40e_vc_process_vflr_event(pf);
4998         i40e_watchdog_subtask(pf);
4999         i40e_fdir_reinit_subtask(pf);
5000         i40e_check_hang_subtask(pf);
5001         i40e_sync_filters_subtask(pf);
5002         i40e_clean_adminq_subtask(pf);
5003
5004         i40e_service_event_complete(pf);
5005
5006         /* If the tasks have taken longer than one timer cycle or there
5007          * is more work to be done, reschedule the service task now
5008          * rather than wait for the timer to tick again.
5009          */
5010         if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
5011             test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state)            ||
5012             test_bit(__I40E_MDD_EVENT_PENDING, &pf->state)               ||
5013             test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
5014                 i40e_service_event_schedule(pf);
5015 }
5016
5017 /**
5018  * i40e_service_timer - timer callback
5019  * @data: pointer to PF struct
5020  **/
5021 static void i40e_service_timer(unsigned long data)
5022 {
5023         struct i40e_pf *pf = (struct i40e_pf *)data;
5024
5025         mod_timer(&pf->service_timer,
5026                   round_jiffies(jiffies + pf->service_timer_period));
5027         i40e_service_event_schedule(pf);
5028 }
5029
5030 /**
5031  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
5032  * @vsi: the VSI being configured
5033  **/
5034 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
5035 {
5036         struct i40e_pf *pf = vsi->back;
5037
5038         switch (vsi->type) {
5039         case I40E_VSI_MAIN:
5040                 vsi->alloc_queue_pairs = pf->num_lan_qps;
5041                 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5042                                       I40E_REQ_DESCRIPTOR_MULTIPLE);
5043                 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5044                         vsi->num_q_vectors = pf->num_lan_msix;
5045                 else
5046                         vsi->num_q_vectors = 1;
5047
5048                 break;
5049
5050         case I40E_VSI_FDIR:
5051                 vsi->alloc_queue_pairs = 1;
5052                 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
5053                                       I40E_REQ_DESCRIPTOR_MULTIPLE);
5054                 vsi->num_q_vectors = 1;
5055                 break;
5056
5057         case I40E_VSI_VMDQ2:
5058                 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
5059                 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5060                                       I40E_REQ_DESCRIPTOR_MULTIPLE);
5061                 vsi->num_q_vectors = pf->num_vmdq_msix;
5062                 break;
5063
5064         case I40E_VSI_SRIOV:
5065                 vsi->alloc_queue_pairs = pf->num_vf_qps;
5066                 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5067                                       I40E_REQ_DESCRIPTOR_MULTIPLE);
5068                 break;
5069
5070         default:
5071                 WARN_ON(1);
5072                 return -ENODATA;
5073         }
5074
5075         return 0;
5076 }
5077
5078 /**
5079  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
5080  * @type: VSI pointer
5081  *
5082  * On error: returns error code (negative)
5083  * On success: returns 0
5084  **/
5085 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi)
5086 {
5087         int size;
5088         int ret = 0;
5089
5090         /* allocate memory for both Tx and Rx ring pointers */
5091         size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
5092         vsi->tx_rings = kzalloc(size, GFP_KERNEL);
5093         if (!vsi->tx_rings)
5094                 return -ENOMEM;
5095         vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
5096
5097         /* allocate memory for q_vector pointers */
5098         size = sizeof(struct i40e_q_vectors *) * vsi->num_q_vectors;
5099         vsi->q_vectors = kzalloc(size, GFP_KERNEL);
5100         if (!vsi->q_vectors) {
5101                 ret = -ENOMEM;
5102                 goto err_vectors;
5103         }
5104         return ret;
5105
5106 err_vectors:
5107         kfree(vsi->tx_rings);
5108         return ret;
5109 }
5110
5111 /**
5112  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
5113  * @pf: board private structure
5114  * @type: type of VSI
5115  *
5116  * On error: returns error code (negative)
5117  * On success: returns vsi index in PF (positive)
5118  **/
5119 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
5120 {
5121         int ret = -ENODEV;
5122         struct i40e_vsi *vsi;
5123         int vsi_idx;
5124         int i;
5125
5126         /* Need to protect the allocation of the VSIs at the PF level */
5127         mutex_lock(&pf->switch_mutex);
5128
5129         /* VSI list may be fragmented if VSI creation/destruction has
5130          * been happening.  We can afford to do a quick scan to look
5131          * for any free VSIs in the list.
5132          *
5133          * find next empty vsi slot, looping back around if necessary
5134          */
5135         i = pf->next_vsi;
5136         while (i < pf->hw.func_caps.num_vsis && pf->vsi[i])
5137                 i++;
5138         if (i >= pf->hw.func_caps.num_vsis) {
5139                 i = 0;
5140                 while (i < pf->next_vsi && pf->vsi[i])
5141                         i++;
5142         }
5143
5144         if (i < pf->hw.func_caps.num_vsis && !pf->vsi[i]) {
5145                 vsi_idx = i;             /* Found one! */
5146         } else {
5147                 ret = -ENODEV;
5148                 goto unlock_pf;  /* out of VSI slots! */
5149         }
5150         pf->next_vsi = ++i;
5151
5152         vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
5153         if (!vsi) {
5154                 ret = -ENOMEM;
5155                 goto unlock_pf;
5156         }
5157         vsi->type = type;
5158         vsi->back = pf;
5159         set_bit(__I40E_DOWN, &vsi->state);
5160         vsi->flags = 0;
5161         vsi->idx = vsi_idx;
5162         vsi->rx_itr_setting = pf->rx_itr_default;
5163         vsi->tx_itr_setting = pf->tx_itr_default;
5164         vsi->netdev_registered = false;
5165         vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
5166         INIT_LIST_HEAD(&vsi->mac_filter_list);
5167
5168         ret = i40e_set_num_rings_in_vsi(vsi);
5169         if (ret)
5170                 goto err_rings;
5171
5172         ret = i40e_vsi_alloc_arrays(vsi);
5173         if (ret)
5174                 goto err_rings;
5175
5176         /* Setup default MSIX irq handler for VSI */
5177         i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
5178
5179         pf->vsi[vsi_idx] = vsi;
5180         ret = vsi_idx;
5181         goto unlock_pf;
5182
5183 err_rings:
5184         pf->next_vsi = i - 1;
5185         kfree(vsi);
5186 unlock_pf:
5187         mutex_unlock(&pf->switch_mutex);
5188         return ret;
5189 }
5190
5191 /**
5192  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
5193  * @type: VSI pointer
5194  *
5195  * On error: returns error code (negative)
5196  * On success: returns 0
5197  **/
5198 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi)
5199 {
5200         /* free the ring and vector containers */
5201         kfree(vsi->q_vectors);
5202         vsi->q_vectors = NULL;
5203         kfree(vsi->tx_rings);
5204         vsi->tx_rings = NULL;
5205         vsi->rx_rings = NULL;
5206 }
5207
5208 /**
5209  * i40e_vsi_clear - Deallocate the VSI provided
5210  * @vsi: the VSI being un-configured
5211  **/
5212 static int i40e_vsi_clear(struct i40e_vsi *vsi)
5213 {
5214         struct i40e_pf *pf;
5215
5216         if (!vsi)
5217                 return 0;
5218
5219         if (!vsi->back)
5220                 goto free_vsi;
5221         pf = vsi->back;
5222
5223         mutex_lock(&pf->switch_mutex);
5224         if (!pf->vsi[vsi->idx]) {
5225                 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
5226                         vsi->idx, vsi->idx, vsi, vsi->type);
5227                 goto unlock_vsi;
5228         }
5229
5230         if (pf->vsi[vsi->idx] != vsi) {
5231                 dev_err(&pf->pdev->dev,
5232                         "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
5233                         pf->vsi[vsi->idx]->idx,
5234                         pf->vsi[vsi->idx],
5235                         pf->vsi[vsi->idx]->type,
5236                         vsi->idx, vsi, vsi->type);
5237                 goto unlock_vsi;
5238         }
5239
5240         /* updates the pf for this cleared vsi */
5241         i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
5242         i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
5243
5244         i40e_vsi_free_arrays(vsi);
5245
5246         pf->vsi[vsi->idx] = NULL;
5247         if (vsi->idx < pf->next_vsi)
5248                 pf->next_vsi = vsi->idx;
5249
5250 unlock_vsi:
5251         mutex_unlock(&pf->switch_mutex);
5252 free_vsi:
5253         kfree(vsi);
5254
5255         return 0;
5256 }
5257
5258 /**
5259  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
5260  * @vsi: the VSI being cleaned
5261  **/
5262 static s32 i40e_vsi_clear_rings(struct i40e_vsi *vsi)
5263 {
5264         int i;
5265
5266         if (vsi->tx_rings[0])
5267                 for (i = 0; i < vsi->num_queue_pairs; i++) {
5268                         kfree_rcu(vsi->tx_rings[i], rcu);
5269                         vsi->tx_rings[i] = NULL;
5270                         vsi->rx_rings[i] = NULL;
5271                 }
5272
5273         return 0;
5274 }
5275
5276 /**
5277  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
5278  * @vsi: the VSI being configured
5279  **/
5280 static int i40e_alloc_rings(struct i40e_vsi *vsi)
5281 {
5282         struct i40e_pf *pf = vsi->back;
5283         int i;
5284
5285         /* Set basic values in the rings to be used later during open() */
5286         for (i = 0; i < vsi->num_queue_pairs; i++) {
5287                 struct i40e_ring *tx_ring;
5288                 struct i40e_ring *rx_ring;
5289
5290                 /* allocate space for both Tx and Rx in one shot */
5291                 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
5292                 if (!tx_ring)
5293                         goto err_out;
5294
5295                 tx_ring->queue_index = i;
5296                 tx_ring->reg_idx = vsi->base_queue + i;
5297                 tx_ring->ring_active = false;
5298                 tx_ring->vsi = vsi;
5299                 tx_ring->netdev = vsi->netdev;
5300                 tx_ring->dev = &pf->pdev->dev;
5301                 tx_ring->count = vsi->num_desc;
5302                 tx_ring->size = 0;
5303                 tx_ring->dcb_tc = 0;
5304                 vsi->tx_rings[i] = tx_ring;
5305
5306                 rx_ring = &tx_ring[1];
5307                 rx_ring->queue_index = i;
5308                 rx_ring->reg_idx = vsi->base_queue + i;
5309                 rx_ring->ring_active = false;
5310                 rx_ring->vsi = vsi;
5311                 rx_ring->netdev = vsi->netdev;
5312                 rx_ring->dev = &pf->pdev->dev;
5313                 rx_ring->count = vsi->num_desc;
5314                 rx_ring->size = 0;
5315                 rx_ring->dcb_tc = 0;
5316                 if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
5317                         set_ring_16byte_desc_enabled(rx_ring);
5318                 else
5319                         clear_ring_16byte_desc_enabled(rx_ring);
5320                 vsi->rx_rings[i] = rx_ring;
5321         }
5322
5323         return 0;
5324
5325 err_out:
5326         i40e_vsi_clear_rings(vsi);
5327         return -ENOMEM;
5328 }
5329
5330 /**
5331  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
5332  * @pf: board private structure
5333  * @vectors: the number of MSI-X vectors to request
5334  *
5335  * Returns the number of vectors reserved, or error
5336  **/
5337 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
5338 {
5339         int err = 0;
5340
5341         pf->num_msix_entries = 0;
5342         while (vectors >= I40E_MIN_MSIX) {
5343                 err = pci_enable_msix(pf->pdev, pf->msix_entries, vectors);
5344                 if (err == 0) {
5345                         /* good to go */
5346                         pf->num_msix_entries = vectors;
5347                         break;
5348                 } else if (err < 0) {
5349                         /* total failure */
5350                         dev_info(&pf->pdev->dev,
5351                                  "MSI-X vector reservation failed: %d\n", err);
5352                         vectors = 0;
5353                         break;
5354                 } else {
5355                         /* err > 0 is the hint for retry */
5356                         dev_info(&pf->pdev->dev,
5357                                  "MSI-X vectors wanted %d, retrying with %d\n",
5358                                  vectors, err);
5359                         vectors = err;
5360                 }
5361         }
5362
5363         if (vectors > 0 && vectors < I40E_MIN_MSIX) {
5364                 dev_info(&pf->pdev->dev,
5365                          "Couldn't get enough vectors, only %d available\n",
5366                          vectors);
5367                 vectors = 0;
5368         }
5369
5370         return vectors;
5371 }
5372
5373 /**
5374  * i40e_init_msix - Setup the MSIX capability
5375  * @pf: board private structure
5376  *
5377  * Work with the OS to set up the MSIX vectors needed.
5378  *
5379  * Returns 0 on success, negative on failure
5380  **/
5381 static int i40e_init_msix(struct i40e_pf *pf)
5382 {
5383         i40e_status err = 0;
5384         struct i40e_hw *hw = &pf->hw;
5385         int v_budget, i;
5386         int vec;
5387
5388         if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
5389                 return -ENODEV;
5390
5391         /* The number of vectors we'll request will be comprised of:
5392          *   - Add 1 for "other" cause for Admin Queue events, etc.
5393          *   - The number of LAN queue pairs
5394          *        already adjusted for the NUMA node
5395          *        assumes symmetric Tx/Rx pairing
5396          *   - The number of VMDq pairs
5397          * Once we count this up, try the request.
5398          *
5399          * If we can't get what we want, we'll simplify to nearly nothing
5400          * and try again.  If that still fails, we punt.
5401          */
5402         pf->num_lan_msix = pf->num_lan_qps;
5403         pf->num_vmdq_msix = pf->num_vmdq_qps;
5404         v_budget = 1 + pf->num_lan_msix;
5405         v_budget += (pf->num_vmdq_vsis * pf->num_vmdq_msix);
5406         if (pf->flags & I40E_FLAG_FDIR_ENABLED)
5407                 v_budget++;
5408
5409         /* Scale down if necessary, and the rings will share vectors */
5410         v_budget = min_t(int, v_budget, hw->func_caps.num_msix_vectors);
5411
5412         pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
5413                                    GFP_KERNEL);
5414         if (!pf->msix_entries)
5415                 return -ENOMEM;
5416
5417         for (i = 0; i < v_budget; i++)
5418                 pf->msix_entries[i].entry = i;
5419         vec = i40e_reserve_msix_vectors(pf, v_budget);
5420         if (vec < I40E_MIN_MSIX) {
5421                 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
5422                 kfree(pf->msix_entries);
5423                 pf->msix_entries = NULL;
5424                 return -ENODEV;
5425
5426         } else if (vec == I40E_MIN_MSIX) {
5427                 /* Adjust for minimal MSIX use */
5428                 dev_info(&pf->pdev->dev, "Features disabled, not enough MSIX vectors\n");
5429                 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
5430                 pf->num_vmdq_vsis = 0;
5431                 pf->num_vmdq_qps = 0;
5432                 pf->num_vmdq_msix = 0;
5433                 pf->num_lan_qps = 1;
5434                 pf->num_lan_msix = 1;
5435
5436         } else if (vec != v_budget) {
5437                 /* Scale vector usage down */
5438                 pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
5439                 vec--;                    /* reserve the misc vector */
5440
5441                 /* partition out the remaining vectors */
5442                 switch (vec) {
5443                 case 2:
5444                         pf->num_vmdq_vsis = 1;
5445                         pf->num_lan_msix = 1;
5446                         break;
5447                 case 3:
5448                         pf->num_vmdq_vsis = 1;
5449                         pf->num_lan_msix = 2;
5450                         break;
5451                 default:
5452                         pf->num_lan_msix = min_t(int, (vec / 2),
5453                                                  pf->num_lan_qps);
5454                         pf->num_vmdq_vsis = min_t(int, (vec - pf->num_lan_msix),
5455                                                   I40E_DEFAULT_NUM_VMDQ_VSI);
5456                         break;
5457                 }
5458         }
5459
5460         return err;
5461 }
5462
5463 /**
5464  * i40e_alloc_q_vector - Allocate memory for a single interrupt vector
5465  * @vsi: the VSI being configured
5466  * @v_idx: index of the vector in the vsi struct
5467  *
5468  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
5469  **/
5470 static int i40e_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
5471 {
5472         struct i40e_q_vector *q_vector;
5473
5474         /* allocate q_vector */
5475         q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
5476         if (!q_vector)
5477                 return -ENOMEM;
5478
5479         q_vector->vsi = vsi;
5480         q_vector->v_idx = v_idx;
5481         cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
5482         if (vsi->netdev)
5483                 netif_napi_add(vsi->netdev, &q_vector->napi,
5484                                i40e_napi_poll, vsi->work_limit);
5485
5486         q_vector->rx.latency_range = I40E_LOW_LATENCY;
5487         q_vector->tx.latency_range = I40E_LOW_LATENCY;
5488
5489         /* tie q_vector and vsi together */
5490         vsi->q_vectors[v_idx] = q_vector;
5491
5492         return 0;
5493 }
5494
5495 /**
5496  * i40e_alloc_q_vectors - Allocate memory for interrupt vectors
5497  * @vsi: the VSI being configured
5498  *
5499  * We allocate one q_vector per queue interrupt.  If allocation fails we
5500  * return -ENOMEM.
5501  **/
5502 static int i40e_alloc_q_vectors(struct i40e_vsi *vsi)
5503 {
5504         struct i40e_pf *pf = vsi->back;
5505         int v_idx, num_q_vectors;
5506         int err;
5507
5508         /* if not MSIX, give the one vector only to the LAN VSI */
5509         if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5510                 num_q_vectors = vsi->num_q_vectors;
5511         else if (vsi == pf->vsi[pf->lan_vsi])
5512                 num_q_vectors = 1;
5513         else
5514                 return -EINVAL;
5515
5516         for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
5517                 err = i40e_alloc_q_vector(vsi, v_idx);
5518                 if (err)
5519                         goto err_out;
5520         }
5521
5522         return 0;
5523
5524 err_out:
5525         while (v_idx--)
5526                 i40e_free_q_vector(vsi, v_idx);
5527
5528         return err;
5529 }
5530
5531 /**
5532  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
5533  * @pf: board private structure to initialize
5534  **/
5535 static void i40e_init_interrupt_scheme(struct i40e_pf *pf)
5536 {
5537         int err = 0;
5538
5539         if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
5540                 err = i40e_init_msix(pf);
5541                 if (err) {
5542                         pf->flags &= ~(I40E_FLAG_MSIX_ENABLED      |
5543                                         I40E_FLAG_RSS_ENABLED      |
5544                                         I40E_FLAG_MQ_ENABLED       |
5545                                         I40E_FLAG_DCB_ENABLED      |
5546                                         I40E_FLAG_SRIOV_ENABLED    |
5547                                         I40E_FLAG_FDIR_ENABLED     |
5548                                         I40E_FLAG_FDIR_ATR_ENABLED |
5549                                         I40E_FLAG_VMDQ_ENABLED);
5550
5551                         /* rework the queue expectations without MSIX */
5552                         i40e_determine_queue_usage(pf);
5553                 }
5554         }
5555
5556         if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
5557             (pf->flags & I40E_FLAG_MSI_ENABLED)) {
5558                 dev_info(&pf->pdev->dev, "MSIX not available, trying MSI\n");
5559                 err = pci_enable_msi(pf->pdev);
5560                 if (err) {
5561                         dev_info(&pf->pdev->dev, "MSI init failed - %d\n", err);
5562                         pf->flags &= ~I40E_FLAG_MSI_ENABLED;
5563                 }
5564         }
5565
5566         if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
5567                 dev_info(&pf->pdev->dev, "MSIX and MSI not available, falling back to Legacy IRQ\n");
5568
5569         /* track first vector for misc interrupts */
5570         err = i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT-1);
5571 }
5572
5573 /**
5574  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
5575  * @pf: board private structure
5576  *
5577  * This sets up the handler for MSIX 0, which is used to manage the
5578  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
5579  * when in MSI or Legacy interrupt mode.
5580  **/
5581 static int i40e_setup_misc_vector(struct i40e_pf *pf)
5582 {
5583         struct i40e_hw *hw = &pf->hw;
5584         int err = 0;
5585
5586         /* Only request the irq if this is the first time through, and
5587          * not when we're rebuilding after a Reset
5588          */
5589         if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
5590                 err = request_irq(pf->msix_entries[0].vector,
5591                                   i40e_intr, 0, pf->misc_int_name, pf);
5592                 if (err) {
5593                         dev_info(&pf->pdev->dev,
5594                                  "request_irq for msix_misc failed: %d\n", err);
5595                         return -EFAULT;
5596                 }
5597         }
5598
5599         i40e_enable_misc_int_causes(hw);
5600
5601         /* associate no queues to the misc vector */
5602         wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
5603         wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
5604
5605         i40e_flush(hw);
5606
5607         i40e_irq_dynamic_enable_icr0(pf);
5608
5609         return err;
5610 }
5611
5612 /**
5613  * i40e_config_rss - Prepare for RSS if used
5614  * @pf: board private structure
5615  **/
5616 static int i40e_config_rss(struct i40e_pf *pf)
5617 {
5618         const u64 default_hena =
5619                         ((u64)1 << I40E_FILTER_PCTYPE_NONF_UNICAST_IPV4_UDP) |
5620                         ((u64)1 << I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV4_UDP) |
5621                         ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_UDP) |
5622                         ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_SCTP) |
5623                         ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_TCP_SYN) |
5624                         ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_TCP) |
5625                         ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_OTHER) |
5626                         ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV4) |
5627                         ((u64)1 << I40E_FILTER_PCTYPE_NONF_UNICAST_IPV6_UDP) |
5628                         ((u64)1 << I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV6_UDP) |
5629                         ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_UDP) |
5630                         ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_TCP_SYN) |
5631                         ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_TCP) |
5632                         ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_SCTP) |
5633                         ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_OTHER) |
5634                         ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV6) |
5635                         ((u64)1 << I40E_FILTER_PCTYPE_L2_PAYLOAD);
5636
5637         /* Set of random keys generated using kernel random number generator */
5638         static const u32 seed[I40E_PFQF_HKEY_MAX_INDEX + 1] = {0x41b01687,
5639                                 0x183cfd8c, 0xce880440, 0x580cbc3c, 0x35897377,
5640                                 0x328b25e1, 0x4fa98922, 0xb7d90c14, 0xd5bad70d,
5641                                 0xcd15a2c1, 0xe8580225, 0x4a1e9d11, 0xfe5731be};
5642         struct i40e_hw *hw = &pf->hw;
5643         u32 lut = 0;
5644         int i, j;
5645         u64 hena;
5646
5647         /* Fill out hash function seed */
5648         for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
5649                 wr32(hw, I40E_PFQF_HKEY(i), seed[i]);
5650
5651         /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
5652         hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
5653                 ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
5654         hena |= default_hena;
5655         wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
5656         wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
5657
5658         /* Populate the LUT with max no. of queues in round robin fashion */
5659         for (i = 0, j = 0; i < pf->hw.func_caps.rss_table_size; i++, j++) {
5660
5661                 /* The assumption is that lan qp count will be the highest
5662                  * qp count for any PF VSI that needs RSS.
5663                  * If multiple VSIs need RSS support, all the qp counts
5664                  * for those VSIs should be a power of 2 for RSS to work.
5665                  * If LAN VSI is the only consumer for RSS then this requirement
5666                  * is not necessary.
5667                  */
5668                 if (j == pf->rss_size)
5669                         j = 0;
5670                 /* lut = 4-byte sliding window of 4 lut entries */
5671                 lut = (lut << 8) | (j &
5672                          ((0x1 << pf->hw.func_caps.rss_table_entry_width) - 1));
5673                 /* On i = 3, we have 4 entries in lut; write to the register */
5674                 if ((i & 3) == 3)
5675                         wr32(hw, I40E_PFQF_HLUT(i >> 2), lut);
5676         }
5677         i40e_flush(hw);
5678
5679         return 0;
5680 }
5681
5682 /**
5683  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
5684  * @pf: board private structure to initialize
5685  *
5686  * i40e_sw_init initializes the Adapter private data structure.
5687  * Fields are initialized based on PCI device information and
5688  * OS network device settings (MTU size).
5689  **/
5690 static int i40e_sw_init(struct i40e_pf *pf)
5691 {
5692         int err = 0;
5693         int size;
5694
5695         pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
5696                                 (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
5697         pf->hw.debug_mask = pf->msg_enable | I40E_DEBUG_DIAG;
5698         if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
5699                 if (I40E_DEBUG_USER & debug)
5700                         pf->hw.debug_mask = debug;
5701                 pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
5702                                                 I40E_DEFAULT_MSG_ENABLE);
5703         }
5704
5705         /* Set default capability flags */
5706         pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
5707                     I40E_FLAG_MSI_ENABLED     |
5708                     I40E_FLAG_MSIX_ENABLED    |
5709                     I40E_FLAG_RX_PS_ENABLED   |
5710                     I40E_FLAG_MQ_ENABLED      |
5711                     I40E_FLAG_RX_1BUF_ENABLED;
5712
5713         /* Depending on PF configurations, it is possible that the RSS
5714          * maximum might end up larger than the available queues
5715          */
5716         pf->rss_size_max = 0x1 << pf->hw.func_caps.rss_table_entry_width;
5717         pf->rss_size_max = min_t(int, pf->rss_size_max,
5718                                  pf->hw.func_caps.num_tx_qp);
5719         if (pf->hw.func_caps.rss) {
5720                 pf->flags |= I40E_FLAG_RSS_ENABLED;
5721                 pf->rss_size = min_t(int, pf->rss_size_max,
5722                                      nr_cpus_node(numa_node_id()));
5723         } else {
5724                 pf->rss_size = 1;
5725         }
5726
5727         if (pf->hw.func_caps.dcb)
5728                 pf->num_tc_qps = I40E_DEFAULT_QUEUES_PER_TC;
5729         else
5730                 pf->num_tc_qps = 0;
5731
5732         if (pf->hw.func_caps.fd) {
5733                 /* FW/NVM is not yet fixed in this regard */
5734                 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
5735                     (pf->hw.func_caps.fd_filters_best_effort > 0)) {
5736                         pf->flags |= I40E_FLAG_FDIR_ATR_ENABLED;
5737                         dev_info(&pf->pdev->dev,
5738                                  "Flow Director ATR mode Enabled\n");
5739                         pf->flags |= I40E_FLAG_FDIR_ENABLED;
5740                         dev_info(&pf->pdev->dev,
5741                                  "Flow Director Side Band mode Enabled\n");
5742                         pf->fdir_pf_filter_count =
5743                                          pf->hw.func_caps.fd_filters_guaranteed;
5744                 }
5745         } else {
5746                 pf->fdir_pf_filter_count = 0;
5747         }
5748
5749         if (pf->hw.func_caps.vmdq) {
5750                 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
5751                 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
5752                 pf->num_vmdq_qps = I40E_DEFAULT_QUEUES_PER_VMDQ;
5753         }
5754
5755         /* MFP mode enabled */
5756         if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.mfp_mode_1) {
5757                 pf->flags |= I40E_FLAG_MFP_ENABLED;
5758                 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
5759         }
5760
5761 #ifdef CONFIG_PCI_IOV
5762         if (pf->hw.func_caps.num_vfs) {
5763                 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
5764                 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
5765                 pf->num_req_vfs = min_t(int,
5766                                         pf->hw.func_caps.num_vfs,
5767                                         I40E_MAX_VF_COUNT);
5768                 dev_info(&pf->pdev->dev,
5769                          "Number of VFs being requested for PF[%d] = %d\n",
5770                          pf->hw.pf_id, pf->num_req_vfs);
5771         }
5772 #endif /* CONFIG_PCI_IOV */
5773         pf->eeprom_version = 0xDEAD;
5774         pf->lan_veb = I40E_NO_VEB;
5775         pf->lan_vsi = I40E_NO_VSI;
5776
5777         /* set up queue assignment tracking */
5778         size = sizeof(struct i40e_lump_tracking)
5779                 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
5780         pf->qp_pile = kzalloc(size, GFP_KERNEL);
5781         if (!pf->qp_pile) {
5782                 err = -ENOMEM;
5783                 goto sw_init_done;
5784         }
5785         pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
5786         pf->qp_pile->search_hint = 0;
5787
5788         /* set up vector assignment tracking */
5789         size = sizeof(struct i40e_lump_tracking)
5790                 + (sizeof(u16) * pf->hw.func_caps.num_msix_vectors);
5791         pf->irq_pile = kzalloc(size, GFP_KERNEL);
5792         if (!pf->irq_pile) {
5793                 kfree(pf->qp_pile);
5794                 err = -ENOMEM;
5795                 goto sw_init_done;
5796         }
5797         pf->irq_pile->num_entries = pf->hw.func_caps.num_msix_vectors;
5798         pf->irq_pile->search_hint = 0;
5799
5800         mutex_init(&pf->switch_mutex);
5801
5802 sw_init_done:
5803         return err;
5804 }
5805
5806 /**
5807  * i40e_set_features - set the netdev feature flags
5808  * @netdev: ptr to the netdev being adjusted
5809  * @features: the feature set that the stack is suggesting
5810  **/
5811 static int i40e_set_features(struct net_device *netdev,
5812                              netdev_features_t features)
5813 {
5814         struct i40e_netdev_priv *np = netdev_priv(netdev);
5815         struct i40e_vsi *vsi = np->vsi;
5816
5817         if (features & NETIF_F_HW_VLAN_CTAG_RX)
5818                 i40e_vlan_stripping_enable(vsi);
5819         else
5820                 i40e_vlan_stripping_disable(vsi);
5821
5822         return 0;
5823 }
5824
5825 static const struct net_device_ops i40e_netdev_ops = {
5826         .ndo_open               = i40e_open,
5827         .ndo_stop               = i40e_close,
5828         .ndo_start_xmit         = i40e_lan_xmit_frame,
5829         .ndo_get_stats64        = i40e_get_netdev_stats_struct,
5830         .ndo_set_rx_mode        = i40e_set_rx_mode,
5831         .ndo_validate_addr      = eth_validate_addr,
5832         .ndo_set_mac_address    = i40e_set_mac,
5833         .ndo_change_mtu         = i40e_change_mtu,
5834         .ndo_tx_timeout         = i40e_tx_timeout,
5835         .ndo_vlan_rx_add_vid    = i40e_vlan_rx_add_vid,
5836         .ndo_vlan_rx_kill_vid   = i40e_vlan_rx_kill_vid,
5837 #ifdef CONFIG_NET_POLL_CONTROLLER
5838         .ndo_poll_controller    = i40e_netpoll,
5839 #endif
5840         .ndo_setup_tc           = i40e_setup_tc,
5841         .ndo_set_features       = i40e_set_features,
5842         .ndo_set_vf_mac         = i40e_ndo_set_vf_mac,
5843         .ndo_set_vf_vlan        = i40e_ndo_set_vf_port_vlan,
5844         .ndo_set_vf_tx_rate     = i40e_ndo_set_vf_bw,
5845         .ndo_get_vf_config      = i40e_ndo_get_vf_config,
5846 };
5847
5848 /**
5849  * i40e_config_netdev - Setup the netdev flags
5850  * @vsi: the VSI being configured
5851  *
5852  * Returns 0 on success, negative value on failure
5853  **/
5854 static int i40e_config_netdev(struct i40e_vsi *vsi)
5855 {
5856         struct i40e_pf *pf = vsi->back;
5857         struct i40e_hw *hw = &pf->hw;
5858         struct i40e_netdev_priv *np;
5859         struct net_device *netdev;
5860         u8 mac_addr[ETH_ALEN];
5861         int etherdev_size;
5862
5863         etherdev_size = sizeof(struct i40e_netdev_priv);
5864         netdev = alloc_etherdev_mq(etherdev_size, vsi->num_queue_pairs);
5865         if (!netdev)
5866                 return -ENOMEM;
5867
5868         vsi->netdev = netdev;
5869         np = netdev_priv(netdev);
5870         np->vsi = vsi;
5871
5872         netdev->hw_enc_features = NETIF_F_IP_CSUM        |
5873                                   NETIF_F_GSO_UDP_TUNNEL |
5874                                   NETIF_F_TSO            |
5875                                   NETIF_F_SG;
5876
5877         netdev->features = NETIF_F_SG                  |
5878                            NETIF_F_IP_CSUM             |
5879                            NETIF_F_SCTP_CSUM           |
5880                            NETIF_F_HIGHDMA             |
5881                            NETIF_F_GSO_UDP_TUNNEL      |
5882                            NETIF_F_HW_VLAN_CTAG_TX     |
5883                            NETIF_F_HW_VLAN_CTAG_RX     |
5884                            NETIF_F_HW_VLAN_CTAG_FILTER |
5885                            NETIF_F_IPV6_CSUM           |
5886                            NETIF_F_TSO                 |
5887                            NETIF_F_TSO6                |
5888                            NETIF_F_RXCSUM              |
5889                            NETIF_F_RXHASH              |
5890                            0;
5891
5892         /* copy netdev features into list of user selectable features */
5893         netdev->hw_features |= netdev->features;
5894
5895         if (vsi->type == I40E_VSI_MAIN) {
5896                 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
5897                 memcpy(mac_addr, hw->mac.perm_addr, ETH_ALEN);
5898         } else {
5899                 /* relate the VSI_VMDQ name to the VSI_MAIN name */
5900                 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
5901                          pf->vsi[pf->lan_vsi]->netdev->name);
5902                 random_ether_addr(mac_addr);
5903                 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
5904         }
5905
5906         memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
5907         memcpy(netdev->perm_addr, mac_addr, ETH_ALEN);
5908         /* vlan gets same features (except vlan offload)
5909          * after any tweaks for specific VSI types
5910          */
5911         netdev->vlan_features = netdev->features & ~(NETIF_F_HW_VLAN_CTAG_TX |
5912                                                      NETIF_F_HW_VLAN_CTAG_RX |
5913                                                    NETIF_F_HW_VLAN_CTAG_FILTER);
5914         netdev->priv_flags |= IFF_UNICAST_FLT;
5915         netdev->priv_flags |= IFF_SUPP_NOFCS;
5916         /* Setup netdev TC information */
5917         i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
5918
5919         netdev->netdev_ops = &i40e_netdev_ops;
5920         netdev->watchdog_timeo = 5 * HZ;
5921         i40e_set_ethtool_ops(netdev);
5922
5923         return 0;
5924 }
5925
5926 /**
5927  * i40e_vsi_delete - Delete a VSI from the switch
5928  * @vsi: the VSI being removed
5929  *
5930  * Returns 0 on success, negative value on failure
5931  **/
5932 static void i40e_vsi_delete(struct i40e_vsi *vsi)
5933 {
5934         /* remove default VSI is not allowed */
5935         if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
5936                 return;
5937
5938         /* there is no HW VSI for FDIR */
5939         if (vsi->type == I40E_VSI_FDIR)
5940                 return;
5941
5942         i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
5943         return;
5944 }
5945
5946 /**
5947  * i40e_add_vsi - Add a VSI to the switch
5948  * @vsi: the VSI being configured
5949  *
5950  * This initializes a VSI context depending on the VSI type to be added and
5951  * passes it down to the add_vsi aq command.
5952  **/
5953 static int i40e_add_vsi(struct i40e_vsi *vsi)
5954 {
5955         int ret = -ENODEV;
5956         struct i40e_mac_filter *f, *ftmp;
5957         struct i40e_pf *pf = vsi->back;
5958         struct i40e_hw *hw = &pf->hw;
5959         struct i40e_vsi_context ctxt;
5960         u8 enabled_tc = 0x1; /* TC0 enabled */
5961         int f_count = 0;
5962
5963         memset(&ctxt, 0, sizeof(ctxt));
5964         switch (vsi->type) {
5965         case I40E_VSI_MAIN:
5966                 /* The PF's main VSI is already setup as part of the
5967                  * device initialization, so we'll not bother with
5968                  * the add_vsi call, but we will retrieve the current
5969                  * VSI context.
5970                  */
5971                 ctxt.seid = pf->main_vsi_seid;
5972                 ctxt.pf_num = pf->hw.pf_id;
5973                 ctxt.vf_num = 0;
5974                 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
5975                 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
5976                 if (ret) {
5977                         dev_info(&pf->pdev->dev,
5978                                  "couldn't get pf vsi config, err %d, aq_err %d\n",
5979                                  ret, pf->hw.aq.asq_last_status);
5980                         return -ENOENT;
5981                 }
5982                 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
5983                 vsi->info.valid_sections = 0;
5984
5985                 vsi->seid = ctxt.seid;
5986                 vsi->id = ctxt.vsi_number;
5987
5988                 enabled_tc = i40e_pf_get_tc_map(pf);
5989
5990                 /* MFP mode setup queue map and update VSI */
5991                 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5992                         memset(&ctxt, 0, sizeof(ctxt));
5993                         ctxt.seid = pf->main_vsi_seid;
5994                         ctxt.pf_num = pf->hw.pf_id;
5995                         ctxt.vf_num = 0;
5996                         i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5997                         ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5998                         if (ret) {
5999                                 dev_info(&pf->pdev->dev,
6000                                          "update vsi failed, aq_err=%d\n",
6001                                          pf->hw.aq.asq_last_status);
6002                                 ret = -ENOENT;
6003                                 goto err;
6004                         }
6005                         /* update the local VSI info queue map */
6006                         i40e_vsi_update_queue_map(vsi, &ctxt);
6007                         vsi->info.valid_sections = 0;
6008                 } else {
6009                         /* Default/Main VSI is only enabled for TC0
6010                          * reconfigure it to enable all TCs that are
6011                          * available on the port in SFP mode.
6012                          */
6013                         ret = i40e_vsi_config_tc(vsi, enabled_tc);
6014                         if (ret) {
6015                                 dev_info(&pf->pdev->dev,
6016                                          "failed to configure TCs for main VSI tc_map 0x%08x, err %d, aq_err %d\n",
6017                                          enabled_tc, ret,
6018                                          pf->hw.aq.asq_last_status);
6019                                 ret = -ENOENT;
6020                         }
6021                 }
6022                 break;
6023
6024         case I40E_VSI_FDIR:
6025                 /* no queue mapping or actual HW VSI needed */
6026                 vsi->info.valid_sections = 0;
6027                 vsi->seid = 0;
6028                 vsi->id = 0;
6029                 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
6030                 return 0;
6031                 break;
6032
6033         case I40E_VSI_VMDQ2:
6034                 ctxt.pf_num = hw->pf_id;
6035                 ctxt.vf_num = 0;
6036                 ctxt.uplink_seid = vsi->uplink_seid;
6037                 ctxt.connection_type = 0x1;     /* regular data port */
6038                 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6039
6040                 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6041
6042                 /* This VSI is connected to VEB so the switch_id
6043                  * should be set to zero by default.
6044                  */
6045                 ctxt.info.switch_id = 0;
6046                 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
6047                 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6048
6049                 /* Setup the VSI tx/rx queue map for TC0 only for now */
6050                 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
6051                 break;
6052
6053         case I40E_VSI_SRIOV:
6054                 ctxt.pf_num = hw->pf_id;
6055                 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
6056                 ctxt.uplink_seid = vsi->uplink_seid;
6057                 ctxt.connection_type = 0x1;     /* regular data port */
6058                 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
6059
6060                 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6061
6062                 /* This VSI is connected to VEB so the switch_id
6063                  * should be set to zero by default.
6064                  */
6065                 ctxt.info.switch_id = cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6066
6067                 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
6068                 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
6069                 /* Setup the VSI tx/rx queue map for TC0 only for now */
6070                 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
6071                 break;
6072
6073         default:
6074                 return -ENODEV;
6075         }
6076
6077         if (vsi->type != I40E_VSI_MAIN) {
6078                 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6079                 if (ret) {
6080                         dev_info(&vsi->back->pdev->dev,
6081                                  "add vsi failed, aq_err=%d\n",
6082                                  vsi->back->hw.aq.asq_last_status);
6083                         ret = -ENOENT;
6084                         goto err;
6085                 }
6086                 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
6087                 vsi->info.valid_sections = 0;
6088                 vsi->seid = ctxt.seid;
6089                 vsi->id = ctxt.vsi_number;
6090         }
6091
6092         /* If macvlan filters already exist, force them to get loaded */
6093         list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
6094                 f->changed = true;
6095                 f_count++;
6096         }
6097         if (f_count) {
6098                 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
6099                 pf->flags |= I40E_FLAG_FILTER_SYNC;
6100         }
6101
6102         /* Update VSI BW information */
6103         ret = i40e_vsi_get_bw_info(vsi);
6104         if (ret) {
6105                 dev_info(&pf->pdev->dev,
6106                          "couldn't get vsi bw info, err %d, aq_err %d\n",
6107                          ret, pf->hw.aq.asq_last_status);
6108                 /* VSI is already added so not tearing that up */
6109                 ret = 0;
6110         }
6111
6112 err:
6113         return ret;
6114 }
6115
6116 /**
6117  * i40e_vsi_release - Delete a VSI and free its resources
6118  * @vsi: the VSI being removed
6119  *
6120  * Returns 0 on success or < 0 on error
6121  **/
6122 int i40e_vsi_release(struct i40e_vsi *vsi)
6123 {
6124         struct i40e_mac_filter *f, *ftmp;
6125         struct i40e_veb *veb = NULL;
6126         struct i40e_pf *pf;
6127         u16 uplink_seid;
6128         int i, n;
6129
6130         pf = vsi->back;
6131
6132         /* release of a VEB-owner or last VSI is not allowed */
6133         if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
6134                 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
6135                          vsi->seid, vsi->uplink_seid);
6136                 return -ENODEV;
6137         }
6138         if (vsi == pf->vsi[pf->lan_vsi] &&
6139             !test_bit(__I40E_DOWN, &pf->state)) {
6140                 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
6141                 return -ENODEV;
6142         }
6143
6144         uplink_seid = vsi->uplink_seid;
6145         if (vsi->type != I40E_VSI_SRIOV) {
6146                 if (vsi->netdev_registered) {
6147                         vsi->netdev_registered = false;
6148                         if (vsi->netdev) {
6149                                 /* results in a call to i40e_close() */
6150                                 unregister_netdev(vsi->netdev);
6151                                 free_netdev(vsi->netdev);
6152                                 vsi->netdev = NULL;
6153                         }
6154                 } else {
6155                         if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
6156                                 i40e_down(vsi);
6157                         i40e_vsi_free_irq(vsi);
6158                         i40e_vsi_free_tx_resources(vsi);
6159                         i40e_vsi_free_rx_resources(vsi);
6160                 }
6161                 i40e_vsi_disable_irq(vsi);
6162         }
6163
6164         list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
6165                 i40e_del_filter(vsi, f->macaddr, f->vlan,
6166                                 f->is_vf, f->is_netdev);
6167         i40e_sync_vsi_filters(vsi);
6168
6169         i40e_vsi_delete(vsi);
6170         i40e_vsi_free_q_vectors(vsi);
6171         i40e_vsi_clear_rings(vsi);
6172         i40e_vsi_clear(vsi);
6173
6174         /* If this was the last thing on the VEB, except for the
6175          * controlling VSI, remove the VEB, which puts the controlling
6176          * VSI onto the next level down in the switch.
6177          *
6178          * Well, okay, there's one more exception here: don't remove
6179          * the orphan VEBs yet.  We'll wait for an explicit remove request
6180          * from up the network stack.
6181          */
6182         for (n = 0, i = 0; i < pf->hw.func_caps.num_vsis; i++) {
6183                 if (pf->vsi[i] &&
6184                     pf->vsi[i]->uplink_seid == uplink_seid &&
6185                     (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
6186                         n++;      /* count the VSIs */
6187                 }
6188         }
6189         for (i = 0; i < I40E_MAX_VEB; i++) {
6190                 if (!pf->veb[i])
6191                         continue;
6192                 if (pf->veb[i]->uplink_seid == uplink_seid)
6193                         n++;     /* count the VEBs */
6194                 if (pf->veb[i]->seid == uplink_seid)
6195                         veb = pf->veb[i];
6196         }
6197         if (n == 0 && veb && veb->uplink_seid != 0)
6198                 i40e_veb_release(veb);
6199
6200         return 0;
6201 }
6202
6203 /**
6204  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
6205  * @vsi: ptr to the VSI
6206  *
6207  * This should only be called after i40e_vsi_mem_alloc() which allocates the
6208  * corresponding SW VSI structure and initializes num_queue_pairs for the
6209  * newly allocated VSI.
6210  *
6211  * Returns 0 on success or negative on failure
6212  **/
6213 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
6214 {
6215         int ret = -ENOENT;
6216         struct i40e_pf *pf = vsi->back;
6217
6218         if (vsi->q_vectors[0]) {
6219                 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
6220                          vsi->seid);
6221                 return -EEXIST;
6222         }
6223
6224         if (vsi->base_vector) {
6225                 dev_info(&pf->pdev->dev,
6226                          "VSI %d has non-zero base vector %d\n",
6227                          vsi->seid, vsi->base_vector);
6228                 return -EEXIST;
6229         }
6230
6231         ret = i40e_alloc_q_vectors(vsi);
6232         if (ret) {
6233                 dev_info(&pf->pdev->dev,
6234                          "failed to allocate %d q_vector for VSI %d, ret=%d\n",
6235                          vsi->num_q_vectors, vsi->seid, ret);
6236                 vsi->num_q_vectors = 0;
6237                 goto vector_setup_out;
6238         }
6239
6240         if (vsi->num_q_vectors)
6241                 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
6242                                                  vsi->num_q_vectors, vsi->idx);
6243         if (vsi->base_vector < 0) {
6244                 dev_info(&pf->pdev->dev,
6245                          "failed to get q tracking for VSI %d, err=%d\n",
6246                          vsi->seid, vsi->base_vector);
6247                 i40e_vsi_free_q_vectors(vsi);
6248                 ret = -ENOENT;
6249                 goto vector_setup_out;
6250         }
6251
6252 vector_setup_out:
6253         return ret;
6254 }
6255
6256 /**
6257  * i40e_vsi_setup - Set up a VSI by a given type
6258  * @pf: board private structure
6259  * @type: VSI type
6260  * @uplink_seid: the switch element to link to
6261  * @param1: usage depends upon VSI type. For VF types, indicates VF id
6262  *
6263  * This allocates the sw VSI structure and its queue resources, then add a VSI
6264  * to the identified VEB.
6265  *
6266  * Returns pointer to the successfully allocated and configure VSI sw struct on
6267  * success, otherwise returns NULL on failure.
6268  **/
6269 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
6270                                 u16 uplink_seid, u32 param1)
6271 {
6272         struct i40e_vsi *vsi = NULL;
6273         struct i40e_veb *veb = NULL;
6274         int ret, i;
6275         int v_idx;
6276
6277         /* The requested uplink_seid must be either
6278          *     - the PF's port seid
6279          *              no VEB is needed because this is the PF
6280          *              or this is a Flow Director special case VSI
6281          *     - seid of an existing VEB
6282          *     - seid of a VSI that owns an existing VEB
6283          *     - seid of a VSI that doesn't own a VEB
6284          *              a new VEB is created and the VSI becomes the owner
6285          *     - seid of the PF VSI, which is what creates the first VEB
6286          *              this is a special case of the previous
6287          *
6288          * Find which uplink_seid we were given and create a new VEB if needed
6289          */
6290         for (i = 0; i < I40E_MAX_VEB; i++) {
6291                 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
6292                         veb = pf->veb[i];
6293                         break;
6294                 }
6295         }
6296
6297         if (!veb && uplink_seid != pf->mac_seid) {
6298
6299                 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
6300                         if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
6301                                 vsi = pf->vsi[i];
6302                                 break;
6303                         }
6304                 }
6305                 if (!vsi) {
6306                         dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
6307                                  uplink_seid);
6308                         return NULL;
6309                 }
6310
6311                 if (vsi->uplink_seid == pf->mac_seid)
6312                         veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
6313                                              vsi->tc_config.enabled_tc);
6314                 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
6315                         veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
6316                                              vsi->tc_config.enabled_tc);
6317
6318                 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
6319                         if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
6320                                 veb = pf->veb[i];
6321                 }
6322                 if (!veb) {
6323                         dev_info(&pf->pdev->dev, "couldn't add VEB\n");
6324                         return NULL;
6325                 }
6326
6327                 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
6328                 uplink_seid = veb->seid;
6329         }
6330
6331         /* get vsi sw struct */
6332         v_idx = i40e_vsi_mem_alloc(pf, type);
6333         if (v_idx < 0)
6334                 goto err_alloc;
6335         vsi = pf->vsi[v_idx];
6336         vsi->type = type;
6337         vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
6338
6339         if (type == I40E_VSI_MAIN)
6340                 pf->lan_vsi = v_idx;
6341         else if (type == I40E_VSI_SRIOV)
6342                 vsi->vf_id = param1;
6343         /* assign it some queues */
6344         ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
6345         if (ret < 0) {
6346                 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
6347                          vsi->seid, ret);
6348                 goto err_vsi;
6349         }
6350         vsi->base_queue = ret;
6351
6352         /* get a VSI from the hardware */
6353         vsi->uplink_seid = uplink_seid;
6354         ret = i40e_add_vsi(vsi);
6355         if (ret)
6356                 goto err_vsi;
6357
6358         switch (vsi->type) {
6359         /* setup the netdev if needed */
6360         case I40E_VSI_MAIN:
6361         case I40E_VSI_VMDQ2:
6362                 ret = i40e_config_netdev(vsi);
6363                 if (ret)
6364                         goto err_netdev;
6365                 ret = register_netdev(vsi->netdev);
6366                 if (ret)
6367                         goto err_netdev;
6368                 vsi->netdev_registered = true;
6369                 netif_carrier_off(vsi->netdev);
6370                 /* fall through */
6371
6372         case I40E_VSI_FDIR:
6373                 /* set up vectors and rings if needed */
6374                 ret = i40e_vsi_setup_vectors(vsi);
6375                 if (ret)
6376                         goto err_msix;
6377
6378                 ret = i40e_alloc_rings(vsi);
6379                 if (ret)
6380                         goto err_rings;
6381
6382                 /* map all of the rings to the q_vectors */
6383                 i40e_vsi_map_rings_to_vectors(vsi);
6384
6385                 i40e_vsi_reset_stats(vsi);
6386                 break;
6387
6388         default:
6389                 /* no netdev or rings for the other VSI types */
6390                 break;
6391         }
6392
6393         return vsi;
6394
6395 err_rings:
6396         i40e_vsi_free_q_vectors(vsi);
6397 err_msix:
6398         if (vsi->netdev_registered) {
6399                 vsi->netdev_registered = false;
6400                 unregister_netdev(vsi->netdev);
6401                 free_netdev(vsi->netdev);
6402                 vsi->netdev = NULL;
6403         }
6404 err_netdev:
6405         i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
6406 err_vsi:
6407         i40e_vsi_clear(vsi);
6408 err_alloc:
6409         return NULL;
6410 }
6411
6412 /**
6413  * i40e_veb_get_bw_info - Query VEB BW information
6414  * @veb: the veb to query
6415  *
6416  * Query the Tx scheduler BW configuration data for given VEB
6417  **/
6418 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
6419 {
6420         struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
6421         struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
6422         struct i40e_pf *pf = veb->pf;
6423         struct i40e_hw *hw = &pf->hw;
6424         u32 tc_bw_max;
6425         int ret = 0;
6426         int i;
6427
6428         ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
6429                                                   &bw_data, NULL);
6430         if (ret) {
6431                 dev_info(&pf->pdev->dev,
6432                          "query veb bw config failed, aq_err=%d\n",
6433                          hw->aq.asq_last_status);
6434                 goto out;
6435         }
6436
6437         ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
6438                                                    &ets_data, NULL);
6439         if (ret) {
6440                 dev_info(&pf->pdev->dev,
6441                          "query veb bw ets config failed, aq_err=%d\n",
6442                          hw->aq.asq_last_status);
6443                 goto out;
6444         }
6445
6446         veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
6447         veb->bw_max_quanta = ets_data.tc_bw_max;
6448         veb->is_abs_credits = bw_data.absolute_credits_enable;
6449         tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
6450                     (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
6451         for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6452                 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
6453                 veb->bw_tc_limit_credits[i] =
6454                                         le16_to_cpu(bw_data.tc_bw_limits[i]);
6455                 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
6456         }
6457
6458 out:
6459         return ret;
6460 }
6461
6462 /**
6463  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
6464  * @pf: board private structure
6465  *
6466  * On error: returns error code (negative)
6467  * On success: returns vsi index in PF (positive)
6468  **/
6469 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
6470 {
6471         int ret = -ENOENT;
6472         struct i40e_veb *veb;
6473         int i;
6474
6475         /* Need to protect the allocation of switch elements at the PF level */
6476         mutex_lock(&pf->switch_mutex);
6477
6478         /* VEB list may be fragmented if VEB creation/destruction has
6479          * been happening.  We can afford to do a quick scan to look
6480          * for any free slots in the list.
6481          *
6482          * find next empty veb slot, looping back around if necessary
6483          */
6484         i = 0;
6485         while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
6486                 i++;
6487         if (i >= I40E_MAX_VEB) {
6488                 ret = -ENOMEM;
6489                 goto err_alloc_veb;  /* out of VEB slots! */
6490         }
6491
6492         veb = kzalloc(sizeof(*veb), GFP_KERNEL);
6493         if (!veb) {
6494                 ret = -ENOMEM;
6495                 goto err_alloc_veb;
6496         }
6497         veb->pf = pf;
6498         veb->idx = i;
6499         veb->enabled_tc = 1;
6500
6501         pf->veb[i] = veb;
6502         ret = i;
6503 err_alloc_veb:
6504         mutex_unlock(&pf->switch_mutex);
6505         return ret;
6506 }
6507
6508 /**
6509  * i40e_switch_branch_release - Delete a branch of the switch tree
6510  * @branch: where to start deleting
6511  *
6512  * This uses recursion to find the tips of the branch to be
6513  * removed, deleting until we get back to and can delete this VEB.
6514  **/
6515 static void i40e_switch_branch_release(struct i40e_veb *branch)
6516 {
6517         struct i40e_pf *pf = branch->pf;
6518         u16 branch_seid = branch->seid;
6519         u16 veb_idx = branch->idx;
6520         int i;
6521
6522         /* release any VEBs on this VEB - RECURSION */
6523         for (i = 0; i < I40E_MAX_VEB; i++) {
6524                 if (!pf->veb[i])
6525                         continue;
6526                 if (pf->veb[i]->uplink_seid == branch->seid)
6527                         i40e_switch_branch_release(pf->veb[i]);
6528         }
6529
6530         /* Release the VSIs on this VEB, but not the owner VSI.
6531          *
6532          * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
6533          *       the VEB itself, so don't use (*branch) after this loop.
6534          */
6535         for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
6536                 if (!pf->vsi[i])
6537                         continue;
6538                 if (pf->vsi[i]->uplink_seid == branch_seid &&
6539                    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
6540                         i40e_vsi_release(pf->vsi[i]);
6541                 }
6542         }
6543
6544         /* There's one corner case where the VEB might not have been
6545          * removed, so double check it here and remove it if needed.
6546          * This case happens if the veb was created from the debugfs
6547          * commands and no VSIs were added to it.
6548          */
6549         if (pf->veb[veb_idx])
6550                 i40e_veb_release(pf->veb[veb_idx]);
6551 }
6552
6553 /**
6554  * i40e_veb_clear - remove veb struct
6555  * @veb: the veb to remove
6556  **/
6557 static void i40e_veb_clear(struct i40e_veb *veb)
6558 {
6559         if (!veb)
6560                 return;
6561
6562         if (veb->pf) {
6563                 struct i40e_pf *pf = veb->pf;
6564
6565                 mutex_lock(&pf->switch_mutex);
6566                 if (pf->veb[veb->idx] == veb)
6567                         pf->veb[veb->idx] = NULL;
6568                 mutex_unlock(&pf->switch_mutex);
6569         }
6570
6571         kfree(veb);
6572 }
6573
6574 /**
6575  * i40e_veb_release - Delete a VEB and free its resources
6576  * @veb: the VEB being removed
6577  **/
6578 void i40e_veb_release(struct i40e_veb *veb)
6579 {
6580         struct i40e_vsi *vsi = NULL;
6581         struct i40e_pf *pf;
6582         int i, n = 0;
6583
6584         pf = veb->pf;
6585
6586         /* find the remaining VSI and check for extras */
6587         for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
6588                 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
6589                         n++;
6590                         vsi = pf->vsi[i];
6591                 }
6592         }
6593         if (n != 1) {
6594                 dev_info(&pf->pdev->dev,
6595                          "can't remove VEB %d with %d VSIs left\n",
6596                          veb->seid, n);
6597                 return;
6598         }
6599
6600         /* move the remaining VSI to uplink veb */
6601         vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
6602         if (veb->uplink_seid) {
6603                 vsi->uplink_seid = veb->uplink_seid;
6604                 if (veb->uplink_seid == pf->mac_seid)
6605                         vsi->veb_idx = I40E_NO_VEB;
6606                 else
6607                         vsi->veb_idx = veb->veb_idx;
6608         } else {
6609                 /* floating VEB */
6610                 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6611                 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
6612         }
6613
6614         i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
6615         i40e_veb_clear(veb);
6616
6617         return;
6618 }
6619
6620 /**
6621  * i40e_add_veb - create the VEB in the switch
6622  * @veb: the VEB to be instantiated
6623  * @vsi: the controlling VSI
6624  **/
6625 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
6626 {
6627         bool is_default = (vsi->idx == vsi->back->lan_vsi);
6628         bool is_cloud = false;
6629         int ret;
6630
6631         /* get a VEB from the hardware */
6632         ret = i40e_aq_add_veb(&veb->pf->hw, veb->uplink_seid, vsi->seid,
6633                               veb->enabled_tc, is_default,
6634                               is_cloud, &veb->seid, NULL);
6635         if (ret) {
6636                 dev_info(&veb->pf->pdev->dev,
6637                          "couldn't add VEB, err %d, aq_err %d\n",
6638                          ret, veb->pf->hw.aq.asq_last_status);
6639                 return -EPERM;
6640         }
6641
6642         /* get statistics counter */
6643         ret = i40e_aq_get_veb_parameters(&veb->pf->hw, veb->seid, NULL, NULL,
6644                                          &veb->stats_idx, NULL, NULL, NULL);
6645         if (ret) {
6646                 dev_info(&veb->pf->pdev->dev,
6647                          "couldn't get VEB statistics idx, err %d, aq_err %d\n",
6648                          ret, veb->pf->hw.aq.asq_last_status);
6649                 return -EPERM;
6650         }
6651         ret = i40e_veb_get_bw_info(veb);
6652         if (ret) {
6653                 dev_info(&veb->pf->pdev->dev,
6654                          "couldn't get VEB bw info, err %d, aq_err %d\n",
6655                          ret, veb->pf->hw.aq.asq_last_status);
6656                 i40e_aq_delete_element(&veb->pf->hw, veb->seid, NULL);
6657                 return -ENOENT;
6658         }
6659
6660         vsi->uplink_seid = veb->seid;
6661         vsi->veb_idx = veb->idx;
6662         vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
6663
6664         return 0;
6665 }
6666
6667 /**
6668  * i40e_veb_setup - Set up a VEB
6669  * @pf: board private structure
6670  * @flags: VEB setup flags
6671  * @uplink_seid: the switch element to link to
6672  * @vsi_seid: the initial VSI seid
6673  * @enabled_tc: Enabled TC bit-map
6674  *
6675  * This allocates the sw VEB structure and links it into the switch
6676  * It is possible and legal for this to be a duplicate of an already
6677  * existing VEB.  It is also possible for both uplink and vsi seids
6678  * to be zero, in order to create a floating VEB.
6679  *
6680  * Returns pointer to the successfully allocated VEB sw struct on
6681  * success, otherwise returns NULL on failure.
6682  **/
6683 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
6684                                 u16 uplink_seid, u16 vsi_seid,
6685                                 u8 enabled_tc)
6686 {
6687         struct i40e_veb *veb, *uplink_veb = NULL;
6688         int vsi_idx, veb_idx;
6689         int ret;
6690
6691         /* if one seid is 0, the other must be 0 to create a floating relay */
6692         if ((uplink_seid == 0 || vsi_seid == 0) &&
6693             (uplink_seid + vsi_seid != 0)) {
6694                 dev_info(&pf->pdev->dev,
6695                          "one, not both seid's are 0: uplink=%d vsi=%d\n",
6696                          uplink_seid, vsi_seid);
6697                 return NULL;
6698         }
6699
6700         /* make sure there is such a vsi and uplink */
6701         for (vsi_idx = 0; vsi_idx < pf->hw.func_caps.num_vsis; vsi_idx++)
6702                 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
6703                         break;
6704         if (vsi_idx >= pf->hw.func_caps.num_vsis && vsi_seid != 0) {
6705                 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
6706                          vsi_seid);
6707                 return NULL;
6708         }
6709
6710         if (uplink_seid && uplink_seid != pf->mac_seid) {
6711                 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6712                         if (pf->veb[veb_idx] &&
6713                             pf->veb[veb_idx]->seid == uplink_seid) {
6714                                 uplink_veb = pf->veb[veb_idx];
6715                                 break;
6716                         }
6717                 }
6718                 if (!uplink_veb) {
6719                         dev_info(&pf->pdev->dev,
6720                                  "uplink seid %d not found\n", uplink_seid);
6721                         return NULL;
6722                 }
6723         }
6724
6725         /* get veb sw struct */
6726         veb_idx = i40e_veb_mem_alloc(pf);
6727         if (veb_idx < 0)
6728                 goto err_alloc;
6729         veb = pf->veb[veb_idx];
6730         veb->flags = flags;
6731         veb->uplink_seid = uplink_seid;
6732         veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
6733         veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
6734
6735         /* create the VEB in the switch */
6736         ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
6737         if (ret)
6738                 goto err_veb;
6739
6740         return veb;
6741
6742 err_veb:
6743         i40e_veb_clear(veb);
6744 err_alloc:
6745         return NULL;
6746 }
6747
6748 /**
6749  * i40e_setup_pf_switch_element - set pf vars based on switch type
6750  * @pf: board private structure
6751  * @ele: element we are building info from
6752  * @num_reported: total number of elements
6753  * @printconfig: should we print the contents
6754  *
6755  * helper function to assist in extracting a few useful SEID values.
6756  **/
6757 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
6758                                 struct i40e_aqc_switch_config_element_resp *ele,
6759                                 u16 num_reported, bool printconfig)
6760 {
6761         u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
6762         u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
6763         u8 element_type = ele->element_type;
6764         u16 seid = le16_to_cpu(ele->seid);
6765
6766         if (printconfig)
6767                 dev_info(&pf->pdev->dev,
6768                          "type=%d seid=%d uplink=%d downlink=%d\n",
6769                          element_type, seid, uplink_seid, downlink_seid);
6770
6771         switch (element_type) {
6772         case I40E_SWITCH_ELEMENT_TYPE_MAC:
6773                 pf->mac_seid = seid;
6774                 break;
6775         case I40E_SWITCH_ELEMENT_TYPE_VEB:
6776                 /* Main VEB? */
6777                 if (uplink_seid != pf->mac_seid)
6778                         break;
6779                 if (pf->lan_veb == I40E_NO_VEB) {
6780                         int v;
6781
6782                         /* find existing or else empty VEB */
6783                         for (v = 0; v < I40E_MAX_VEB; v++) {
6784                                 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
6785                                         pf->lan_veb = v;
6786                                         break;
6787                                 }
6788                         }
6789                         if (pf->lan_veb == I40E_NO_VEB) {
6790                                 v = i40e_veb_mem_alloc(pf);
6791                                 if (v < 0)
6792                                         break;
6793                                 pf->lan_veb = v;
6794                         }
6795                 }
6796
6797                 pf->veb[pf->lan_veb]->seid = seid;
6798                 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
6799                 pf->veb[pf->lan_veb]->pf = pf;
6800                 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
6801                 break;
6802         case I40E_SWITCH_ELEMENT_TYPE_VSI:
6803                 if (num_reported != 1)
6804                         break;
6805                 /* This is immediately after a reset so we can assume this is
6806                  * the PF's VSI
6807                  */
6808                 pf->mac_seid = uplink_seid;
6809                 pf->pf_seid = downlink_seid;
6810                 pf->main_vsi_seid = seid;
6811                 if (printconfig)
6812                         dev_info(&pf->pdev->dev,
6813                                  "pf_seid=%d main_vsi_seid=%d\n",
6814                                  pf->pf_seid, pf->main_vsi_seid);
6815                 break;
6816         case I40E_SWITCH_ELEMENT_TYPE_PF:
6817         case I40E_SWITCH_ELEMENT_TYPE_VF:
6818         case I40E_SWITCH_ELEMENT_TYPE_EMP:
6819         case I40E_SWITCH_ELEMENT_TYPE_BMC:
6820         case I40E_SWITCH_ELEMENT_TYPE_PE:
6821         case I40E_SWITCH_ELEMENT_TYPE_PA:
6822                 /* ignore these for now */
6823                 break;
6824         default:
6825                 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
6826                          element_type, seid);
6827                 break;
6828         }
6829 }
6830
6831 /**
6832  * i40e_fetch_switch_configuration - Get switch config from firmware
6833  * @pf: board private structure
6834  * @printconfig: should we print the contents
6835  *
6836  * Get the current switch configuration from the device and
6837  * extract a few useful SEID values.
6838  **/
6839 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
6840 {
6841         struct i40e_aqc_get_switch_config_resp *sw_config;
6842         u16 next_seid = 0;
6843         int ret = 0;
6844         u8 *aq_buf;
6845         int i;
6846
6847         aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
6848         if (!aq_buf)
6849                 return -ENOMEM;
6850
6851         sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
6852         do {
6853                 u16 num_reported, num_total;
6854
6855                 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
6856                                                 I40E_AQ_LARGE_BUF,
6857                                                 &next_seid, NULL);
6858                 if (ret) {
6859                         dev_info(&pf->pdev->dev,
6860                                  "get switch config failed %d aq_err=%x\n",
6861                                  ret, pf->hw.aq.asq_last_status);
6862                         kfree(aq_buf);
6863                         return -ENOENT;
6864                 }
6865
6866                 num_reported = le16_to_cpu(sw_config->header.num_reported);
6867                 num_total = le16_to_cpu(sw_config->header.num_total);
6868
6869                 if (printconfig)
6870                         dev_info(&pf->pdev->dev,
6871                                  "header: %d reported %d total\n",
6872                                  num_reported, num_total);
6873
6874                 if (num_reported) {
6875                         int sz = sizeof(*sw_config) * num_reported;
6876
6877                         kfree(pf->sw_config);
6878                         pf->sw_config = kzalloc(sz, GFP_KERNEL);
6879                         if (pf->sw_config)
6880                                 memcpy(pf->sw_config, sw_config, sz);
6881                 }
6882
6883                 for (i = 0; i < num_reported; i++) {
6884                         struct i40e_aqc_switch_config_element_resp *ele =
6885                                 &sw_config->element[i];
6886
6887                         i40e_setup_pf_switch_element(pf, ele, num_reported,
6888                                                      printconfig);
6889                 }
6890         } while (next_seid != 0);
6891
6892         kfree(aq_buf);
6893         return ret;
6894 }
6895
6896 /**
6897  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
6898  * @pf: board private structure
6899  *
6900  * Returns 0 on success, negative value on failure
6901  **/
6902 static int i40e_setup_pf_switch(struct i40e_pf *pf)
6903 {
6904         int ret;
6905
6906         /* find out what's out there already */
6907         ret = i40e_fetch_switch_configuration(pf, false);
6908         if (ret) {
6909                 dev_info(&pf->pdev->dev,
6910                          "couldn't fetch switch config, err %d, aq_err %d\n",
6911                          ret, pf->hw.aq.asq_last_status);
6912                 return ret;
6913         }
6914         i40e_pf_reset_stats(pf);
6915
6916         /* fdir VSI must happen first to be sure it gets queue 0, but only
6917          * if there is enough room for the fdir VSI
6918          */
6919         if (pf->num_lan_qps > 1)
6920                 i40e_fdir_setup(pf);
6921
6922         /* first time setup */
6923         if (pf->lan_vsi == I40E_NO_VSI) {
6924                 struct i40e_vsi *vsi = NULL;
6925                 u16 uplink_seid;
6926
6927                 /* Set up the PF VSI associated with the PF's main VSI
6928                  * that is already in the HW switch
6929                  */
6930                 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6931                         uplink_seid = pf->veb[pf->lan_veb]->seid;
6932                 else
6933                         uplink_seid = pf->mac_seid;
6934
6935                 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
6936                 if (!vsi) {
6937                         dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
6938                         i40e_fdir_teardown(pf);
6939                         return -EAGAIN;
6940                 }
6941                 /* accommodate kcompat by copying the main VSI queue count
6942                  * into the pf, since this newer code pushes the pf queue
6943                  * info down a level into a VSI
6944                  */
6945                 pf->num_rx_queues = vsi->num_queue_pairs;
6946                 pf->num_tx_queues = vsi->num_queue_pairs;
6947         } else {
6948                 /* force a reset of TC and queue layout configurations */
6949                 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
6950                 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
6951                 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
6952                 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
6953         }
6954         i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
6955
6956         /* Setup static PF queue filter control settings */
6957         ret = i40e_setup_pf_filter_control(pf);
6958         if (ret) {
6959                 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
6960                          ret);
6961                 /* Failure here should not stop continuing other steps */
6962         }
6963
6964         /* enable RSS in the HW, even for only one queue, as the stack can use
6965          * the hash
6966          */
6967         if ((pf->flags & I40E_FLAG_RSS_ENABLED))
6968                 i40e_config_rss(pf);
6969
6970         /* fill in link information and enable LSE reporting */
6971         i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
6972         i40e_link_event(pf);
6973
6974         /* Initialize user-specifics link properties */
6975         pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
6976                                   I40E_AQ_AN_COMPLETED) ? true : false);
6977         pf->hw.fc.requested_mode = I40E_FC_DEFAULT;
6978         if (pf->hw.phy.link_info.an_info &
6979            (I40E_AQ_LINK_PAUSE_TX | I40E_AQ_LINK_PAUSE_RX))
6980                 pf->hw.fc.current_mode = I40E_FC_FULL;
6981         else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX)
6982                 pf->hw.fc.current_mode = I40E_FC_TX_PAUSE;
6983         else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX)
6984                 pf->hw.fc.current_mode = I40E_FC_RX_PAUSE;
6985         else
6986                 pf->hw.fc.current_mode = I40E_FC_DEFAULT;
6987
6988         return ret;
6989 }
6990
6991 /**
6992  * i40e_set_rss_size - helper to set rss_size
6993  * @pf: board private structure
6994  * @queues_left: how many queues
6995  */
6996 static u16 i40e_set_rss_size(struct i40e_pf *pf, int queues_left)
6997 {
6998         int num_tc0;
6999
7000         num_tc0 = min_t(int, queues_left, pf->rss_size_max);
7001         num_tc0 = min_t(int, num_tc0, nr_cpus_node(numa_node_id()));
7002         num_tc0 = rounddown_pow_of_two(num_tc0);
7003
7004         return num_tc0;
7005 }
7006
7007 /**
7008  * i40e_determine_queue_usage - Work out queue distribution
7009  * @pf: board private structure
7010  **/
7011 static void i40e_determine_queue_usage(struct i40e_pf *pf)
7012 {
7013         int accum_tc_size;
7014         int queues_left;
7015
7016         pf->num_lan_qps = 0;
7017         pf->num_tc_qps = rounddown_pow_of_two(pf->num_tc_qps);
7018         accum_tc_size = (I40E_MAX_TRAFFIC_CLASS - 1) * pf->num_tc_qps;
7019
7020         /* Find the max queues to be put into basic use.  We'll always be
7021          * using TC0, whether or not DCB is running, and TC0 will get the
7022          * big RSS set.
7023          */
7024         queues_left = pf->hw.func_caps.num_tx_qp;
7025
7026         if   (!((pf->flags & I40E_FLAG_MSIX_ENABLED)             &&
7027                 (pf->flags & I40E_FLAG_MQ_ENABLED))              ||
7028                 !(pf->flags & (I40E_FLAG_RSS_ENABLED |
7029                 I40E_FLAG_FDIR_ENABLED | I40E_FLAG_DCB_ENABLED)) ||
7030                 (queues_left == 1)) {
7031
7032                 /* one qp for PF, no queues for anything else */
7033                 queues_left = 0;
7034                 pf->rss_size = pf->num_lan_qps = 1;
7035
7036                 /* make sure all the fancies are disabled */
7037                 pf->flags &= ~(I40E_FLAG_RSS_ENABLED       |
7038                                 I40E_FLAG_MQ_ENABLED       |
7039                                 I40E_FLAG_FDIR_ENABLED     |
7040                                 I40E_FLAG_FDIR_ATR_ENABLED |
7041                                 I40E_FLAG_DCB_ENABLED      |
7042                                 I40E_FLAG_SRIOV_ENABLED    |
7043                                 I40E_FLAG_VMDQ_ENABLED);
7044
7045         } else if (pf->flags & I40E_FLAG_RSS_ENABLED      &&
7046                    !(pf->flags & I40E_FLAG_FDIR_ENABLED)  &&
7047                    !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
7048
7049                 pf->rss_size = i40e_set_rss_size(pf, queues_left);
7050
7051                 queues_left -= pf->rss_size;
7052                 pf->num_lan_qps = pf->rss_size;
7053
7054         } else if (pf->flags & I40E_FLAG_RSS_ENABLED      &&
7055                    !(pf->flags & I40E_FLAG_FDIR_ENABLED)  &&
7056                    (pf->flags & I40E_FLAG_DCB_ENABLED)) {
7057
7058                 /* save num_tc_qps queues for TCs 1 thru 7 and the rest
7059                  * are set up for RSS in TC0
7060                  */
7061                 queues_left -= accum_tc_size;
7062
7063                 pf->rss_size = i40e_set_rss_size(pf, queues_left);
7064
7065                 queues_left -= pf->rss_size;
7066                 if (queues_left < 0) {
7067                         dev_info(&pf->pdev->dev, "not enough queues for DCB\n");
7068                         return;
7069                 }
7070
7071                 pf->num_lan_qps = pf->rss_size + accum_tc_size;
7072
7073         } else if (pf->flags & I40E_FLAG_RSS_ENABLED   &&
7074                   (pf->flags & I40E_FLAG_FDIR_ENABLED) &&
7075                   !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
7076
7077                 queues_left -= 1; /* save 1 queue for FD */
7078
7079                 pf->rss_size = i40e_set_rss_size(pf, queues_left);
7080
7081                 queues_left -= pf->rss_size;
7082                 if (queues_left < 0) {
7083                         dev_info(&pf->pdev->dev, "not enough queues for Flow Director\n");
7084                         return;
7085                 }
7086
7087                 pf->num_lan_qps = pf->rss_size;
7088
7089         } else if (pf->flags & I40E_FLAG_RSS_ENABLED   &&
7090                   (pf->flags & I40E_FLAG_FDIR_ENABLED) &&
7091                   (pf->flags & I40E_FLAG_DCB_ENABLED)) {
7092
7093                 /* save 1 queue for TCs 1 thru 7,
7094                  * 1 queue for flow director,
7095                  * and the rest are set up for RSS in TC0
7096                  */
7097                 queues_left -= 1;
7098                 queues_left -= accum_tc_size;
7099
7100                 pf->rss_size = i40e_set_rss_size(pf, queues_left);
7101                 queues_left -= pf->rss_size;
7102                 if (queues_left < 0) {
7103                         dev_info(&pf->pdev->dev, "not enough queues for DCB and Flow Director\n");
7104                         return;
7105                 }
7106
7107                 pf->num_lan_qps = pf->rss_size + accum_tc_size;
7108
7109         } else {
7110                 dev_info(&pf->pdev->dev,
7111                          "Invalid configuration, flags=0x%08llx\n", pf->flags);
7112                 return;
7113         }
7114
7115         if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
7116             pf->num_vf_qps && pf->num_req_vfs && queues_left) {
7117                 pf->num_req_vfs = min_t(int, pf->num_req_vfs, (queues_left /
7118                                                                pf->num_vf_qps));
7119                 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
7120         }
7121
7122         if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7123             pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
7124                 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
7125                                           (queues_left / pf->num_vmdq_qps));
7126                 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
7127         }
7128
7129         return;
7130 }
7131
7132 /**
7133  * i40e_setup_pf_filter_control - Setup PF static filter control
7134  * @pf: PF to be setup
7135  *
7136  * i40e_setup_pf_filter_control sets up a pf's initial filter control
7137  * settings. If PE/FCoE are enabled then it will also set the per PF
7138  * based filter sizes required for them. It also enables Flow director,
7139  * ethertype and macvlan type filter settings for the pf.
7140  *
7141  * Returns 0 on success, negative on failure
7142  **/
7143 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
7144 {
7145         struct i40e_filter_control_settings *settings = &pf->filter_settings;
7146
7147         settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
7148
7149         /* Flow Director is enabled */
7150         if (pf->flags & (I40E_FLAG_FDIR_ENABLED | I40E_FLAG_FDIR_ATR_ENABLED))
7151                 settings->enable_fdir = true;
7152
7153         /* Ethtype and MACVLAN filters enabled for PF */
7154         settings->enable_ethtype = true;
7155         settings->enable_macvlan = true;
7156
7157         if (i40e_set_filter_control(&pf->hw, settings))
7158                 return -ENOENT;
7159
7160         return 0;
7161 }
7162
7163 /**
7164  * i40e_probe - Device initialization routine
7165  * @pdev: PCI device information struct
7166  * @ent: entry in i40e_pci_tbl
7167  *
7168  * i40e_probe initializes a pf identified by a pci_dev structure.
7169  * The OS initialization, configuring of the pf private structure,
7170  * and a hardware reset occur.
7171  *
7172  * Returns 0 on success, negative on failure
7173  **/
7174 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
7175 {
7176         struct i40e_driver_version dv;
7177         struct i40e_pf *pf;
7178         struct i40e_hw *hw;
7179         static u16 pfs_found;
7180         int err = 0;
7181         u32 len;
7182
7183         err = pci_enable_device_mem(pdev);
7184         if (err)
7185                 return err;
7186
7187         /* set up for high or low dma */
7188         if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64))) {
7189                 /* coherent mask for the same size will always succeed if
7190                  * dma_set_mask does
7191                  */
7192                 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
7193         } else if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
7194                 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
7195         } else {
7196                 dev_err(&pdev->dev, "DMA configuration failed: %d\n", err);
7197                 err = -EIO;
7198                 goto err_dma;
7199         }
7200
7201         /* set up pci connections */
7202         err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
7203                                            IORESOURCE_MEM), i40e_driver_name);
7204         if (err) {
7205                 dev_info(&pdev->dev,
7206                          "pci_request_selected_regions failed %d\n", err);
7207                 goto err_pci_reg;
7208         }
7209
7210         pci_enable_pcie_error_reporting(pdev);
7211         pci_set_master(pdev);
7212
7213         /* Now that we have a PCI connection, we need to do the
7214          * low level device setup.  This is primarily setting up
7215          * the Admin Queue structures and then querying for the
7216          * device's current profile information.
7217          */
7218         pf = kzalloc(sizeof(*pf), GFP_KERNEL);
7219         if (!pf) {
7220                 err = -ENOMEM;
7221                 goto err_pf_alloc;
7222         }
7223         pf->next_vsi = 0;
7224         pf->pdev = pdev;
7225         set_bit(__I40E_DOWN, &pf->state);
7226
7227         hw = &pf->hw;
7228         hw->back = pf;
7229         hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
7230                               pci_resource_len(pdev, 0));
7231         if (!hw->hw_addr) {
7232                 err = -EIO;
7233                 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
7234                          (unsigned int)pci_resource_start(pdev, 0),
7235                          (unsigned int)pci_resource_len(pdev, 0), err);
7236                 goto err_ioremap;
7237         }
7238         hw->vendor_id = pdev->vendor;
7239         hw->device_id = pdev->device;
7240         pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
7241         hw->subsystem_vendor_id = pdev->subsystem_vendor;
7242         hw->subsystem_device_id = pdev->subsystem_device;
7243         hw->bus.device = PCI_SLOT(pdev->devfn);
7244         hw->bus.func = PCI_FUNC(pdev->devfn);
7245         pf->instance = pfs_found;
7246
7247         /* do a special CORER for clearing PXE mode once at init */
7248         if (hw->revision_id == 0 &&
7249             (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
7250                 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
7251                 i40e_flush(hw);
7252                 msleep(200);
7253                 pf->corer_count++;
7254
7255                 i40e_clear_pxe_mode(hw);
7256         }
7257
7258         /* Reset here to make sure all is clean and to define PF 'n' */
7259         err = i40e_pf_reset(hw);
7260         if (err) {
7261                 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
7262                 goto err_pf_reset;
7263         }
7264         pf->pfr_count++;
7265
7266         hw->aq.num_arq_entries = I40E_AQ_LEN;
7267         hw->aq.num_asq_entries = I40E_AQ_LEN;
7268         hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
7269         hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
7270         pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
7271         snprintf(pf->misc_int_name, sizeof(pf->misc_int_name) - 1,
7272                  "%s-pf%d:misc",
7273                  dev_driver_string(&pf->pdev->dev), pf->hw.pf_id);
7274
7275         err = i40e_init_shared_code(hw);
7276         if (err) {
7277                 dev_info(&pdev->dev, "init_shared_code failed: %d\n", err);
7278                 goto err_pf_reset;
7279         }
7280
7281         err = i40e_init_adminq(hw);
7282         dev_info(&pdev->dev, "%s\n", i40e_fw_version_str(hw));
7283         if (((hw->nvm.version & I40E_NVM_VERSION_HI_MASK)
7284                  >> I40E_NVM_VERSION_HI_SHIFT) != I40E_CURRENT_NVM_VERSION_HI) {
7285                 dev_info(&pdev->dev,
7286                          "warning: NVM version not supported, supported version: %02x.%02x\n",
7287                          I40E_CURRENT_NVM_VERSION_HI,
7288                          I40E_CURRENT_NVM_VERSION_LO);
7289         }
7290         if (err) {
7291                 dev_info(&pdev->dev,
7292                          "init_adminq failed: %d expecting API %02x.%02x\n",
7293                          err,
7294                          I40E_FW_API_VERSION_MAJOR, I40E_FW_API_VERSION_MINOR);
7295                 goto err_pf_reset;
7296         }
7297
7298         err = i40e_get_capabilities(pf);
7299         if (err)
7300                 goto err_adminq_setup;
7301
7302         err = i40e_sw_init(pf);
7303         if (err) {
7304                 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
7305                 goto err_sw_init;
7306         }
7307
7308         err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
7309                                 hw->func_caps.num_rx_qp,
7310                                 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
7311         if (err) {
7312                 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
7313                 goto err_init_lan_hmc;
7314         }
7315
7316         err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
7317         if (err) {
7318                 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
7319                 err = -ENOENT;
7320                 goto err_configure_lan_hmc;
7321         }
7322
7323         i40e_get_mac_addr(hw, hw->mac.addr);
7324         if (i40e_validate_mac_addr(hw->mac.addr)) {
7325                 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
7326                 err = -EIO;
7327                 goto err_mac_addr;
7328         }
7329         dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
7330         memcpy(hw->mac.perm_addr, hw->mac.addr, ETH_ALEN);
7331
7332         pci_set_drvdata(pdev, pf);
7333         pci_save_state(pdev);
7334
7335         /* set up periodic task facility */
7336         setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
7337         pf->service_timer_period = HZ;
7338
7339         INIT_WORK(&pf->service_task, i40e_service_task);
7340         clear_bit(__I40E_SERVICE_SCHED, &pf->state);
7341         pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
7342         pf->link_check_timeout = jiffies;
7343
7344         /* set up the main switch operations */
7345         i40e_determine_queue_usage(pf);
7346         i40e_init_interrupt_scheme(pf);
7347
7348         /* Set up the *vsi struct based on the number of VSIs in the HW,
7349          * and set up our local tracking of the MAIN PF vsi.
7350          */
7351         len = sizeof(struct i40e_vsi *) * pf->hw.func_caps.num_vsis;
7352         pf->vsi = kzalloc(len, GFP_KERNEL);
7353         if (!pf->vsi) {
7354                 err = -ENOMEM;
7355                 goto err_switch_setup;
7356         }
7357
7358         err = i40e_setup_pf_switch(pf);
7359         if (err) {
7360                 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
7361                 goto err_vsis;
7362         }
7363
7364         /* The main driver is (mostly) up and happy. We need to set this state
7365          * before setting up the misc vector or we get a race and the vector
7366          * ends up disabled forever.
7367          */
7368         clear_bit(__I40E_DOWN, &pf->state);
7369
7370         /* In case of MSIX we are going to setup the misc vector right here
7371          * to handle admin queue events etc. In case of legacy and MSI
7372          * the misc functionality and queue processing is combined in
7373          * the same vector and that gets setup at open.
7374          */
7375         if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
7376                 err = i40e_setup_misc_vector(pf);
7377                 if (err) {
7378                         dev_info(&pdev->dev,
7379                                  "setup of misc vector failed: %d\n", err);
7380                         goto err_vsis;
7381                 }
7382         }
7383
7384         /* prep for VF support */
7385         if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
7386             (pf->flags & I40E_FLAG_MSIX_ENABLED)) {
7387                 u32 val;
7388
7389                 /* disable link interrupts for VFs */
7390                 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
7391                 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
7392                 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
7393                 i40e_flush(hw);
7394         }
7395
7396         pfs_found++;
7397
7398         i40e_dbg_pf_init(pf);
7399
7400         /* tell the firmware that we're starting */
7401         dv.major_version = DRV_VERSION_MAJOR;
7402         dv.minor_version = DRV_VERSION_MINOR;
7403         dv.build_version = DRV_VERSION_BUILD;
7404         dv.subbuild_version = 0;
7405         i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
7406
7407         /* since everything's happy, start the service_task timer */
7408         mod_timer(&pf->service_timer,
7409                   round_jiffies(jiffies + pf->service_timer_period));
7410
7411         return 0;
7412
7413         /* Unwind what we've done if something failed in the setup */
7414 err_vsis:
7415         set_bit(__I40E_DOWN, &pf->state);
7416 err_switch_setup:
7417         i40e_clear_interrupt_scheme(pf);
7418         kfree(pf->vsi);
7419         del_timer_sync(&pf->service_timer);
7420 err_mac_addr:
7421 err_configure_lan_hmc:
7422         (void)i40e_shutdown_lan_hmc(hw);
7423 err_init_lan_hmc:
7424         kfree(pf->qp_pile);
7425         kfree(pf->irq_pile);
7426 err_sw_init:
7427 err_adminq_setup:
7428         (void)i40e_shutdown_adminq(hw);
7429 err_pf_reset:
7430         iounmap(hw->hw_addr);
7431 err_ioremap:
7432         kfree(pf);
7433 err_pf_alloc:
7434         pci_disable_pcie_error_reporting(pdev);
7435         pci_release_selected_regions(pdev,
7436                                      pci_select_bars(pdev, IORESOURCE_MEM));
7437 err_pci_reg:
7438 err_dma:
7439         pci_disable_device(pdev);
7440         return err;
7441 }
7442
7443 /**
7444  * i40e_remove - Device removal routine
7445  * @pdev: PCI device information struct
7446  *
7447  * i40e_remove is called by the PCI subsystem to alert the driver
7448  * that is should release a PCI device.  This could be caused by a
7449  * Hot-Plug event, or because the driver is going to be removed from
7450  * memory.
7451  **/
7452 static void i40e_remove(struct pci_dev *pdev)
7453 {
7454         struct i40e_pf *pf = pci_get_drvdata(pdev);
7455         i40e_status ret_code;
7456         u32 reg;
7457         int i;
7458
7459         i40e_dbg_pf_exit(pf);
7460
7461         if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
7462                 i40e_free_vfs(pf);
7463                 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
7464         }
7465
7466         /* no more scheduling of any task */
7467         set_bit(__I40E_DOWN, &pf->state);
7468         del_timer_sync(&pf->service_timer);
7469         cancel_work_sync(&pf->service_task);
7470
7471         i40e_fdir_teardown(pf);
7472
7473         /* If there is a switch structure or any orphans, remove them.
7474          * This will leave only the PF's VSI remaining.
7475          */
7476         for (i = 0; i < I40E_MAX_VEB; i++) {
7477                 if (!pf->veb[i])
7478                         continue;
7479
7480                 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
7481                     pf->veb[i]->uplink_seid == 0)
7482                         i40e_switch_branch_release(pf->veb[i]);
7483         }
7484
7485         /* Now we can shutdown the PF's VSI, just before we kill
7486          * adminq and hmc.
7487          */
7488         if (pf->vsi[pf->lan_vsi])
7489                 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
7490
7491         i40e_stop_misc_vector(pf);
7492         if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
7493                 synchronize_irq(pf->msix_entries[0].vector);
7494                 free_irq(pf->msix_entries[0].vector, pf);
7495         }
7496
7497         /* shutdown and destroy the HMC */
7498         ret_code = i40e_shutdown_lan_hmc(&pf->hw);
7499         if (ret_code)
7500                 dev_warn(&pdev->dev,
7501                          "Failed to destroy the HMC resources: %d\n", ret_code);
7502
7503         /* shutdown the adminq */
7504         i40e_aq_queue_shutdown(&pf->hw, true);
7505         ret_code = i40e_shutdown_adminq(&pf->hw);
7506         if (ret_code)
7507                 dev_warn(&pdev->dev,
7508                          "Failed to destroy the Admin Queue resources: %d\n",
7509                          ret_code);
7510
7511         /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
7512         i40e_clear_interrupt_scheme(pf);
7513         for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
7514                 if (pf->vsi[i]) {
7515                         i40e_vsi_clear_rings(pf->vsi[i]);
7516                         i40e_vsi_clear(pf->vsi[i]);
7517                         pf->vsi[i] = NULL;
7518                 }
7519         }
7520
7521         for (i = 0; i < I40E_MAX_VEB; i++) {
7522                 kfree(pf->veb[i]);
7523                 pf->veb[i] = NULL;
7524         }
7525
7526         kfree(pf->qp_pile);
7527         kfree(pf->irq_pile);
7528         kfree(pf->sw_config);
7529         kfree(pf->vsi);
7530
7531         /* force a PF reset to clean anything leftover */
7532         reg = rd32(&pf->hw, I40E_PFGEN_CTRL);
7533         wr32(&pf->hw, I40E_PFGEN_CTRL, (reg | I40E_PFGEN_CTRL_PFSWR_MASK));
7534         i40e_flush(&pf->hw);
7535
7536         iounmap(pf->hw.hw_addr);
7537         kfree(pf);
7538         pci_release_selected_regions(pdev,
7539                                      pci_select_bars(pdev, IORESOURCE_MEM));
7540
7541         pci_disable_pcie_error_reporting(pdev);
7542         pci_disable_device(pdev);
7543 }
7544
7545 /**
7546  * i40e_pci_error_detected - warning that something funky happened in PCI land
7547  * @pdev: PCI device information struct
7548  *
7549  * Called to warn that something happened and the error handling steps
7550  * are in progress.  Allows the driver to quiesce things, be ready for
7551  * remediation.
7552  **/
7553 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
7554                                                 enum pci_channel_state error)
7555 {
7556         struct i40e_pf *pf = pci_get_drvdata(pdev);
7557
7558         dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
7559
7560         /* shutdown all operations */
7561         i40e_pf_quiesce_all_vsi(pf);
7562
7563         /* Request a slot reset */
7564         return PCI_ERS_RESULT_NEED_RESET;
7565 }
7566
7567 /**
7568  * i40e_pci_error_slot_reset - a PCI slot reset just happened
7569  * @pdev: PCI device information struct
7570  *
7571  * Called to find if the driver can work with the device now that
7572  * the pci slot has been reset.  If a basic connection seems good
7573  * (registers are readable and have sane content) then return a
7574  * happy little PCI_ERS_RESULT_xxx.
7575  **/
7576 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
7577 {
7578         struct i40e_pf *pf = pci_get_drvdata(pdev);
7579         pci_ers_result_t result;
7580         int err;
7581         u32 reg;
7582
7583         dev_info(&pdev->dev, "%s\n", __func__);
7584         if (pci_enable_device_mem(pdev)) {
7585                 dev_info(&pdev->dev,
7586                          "Cannot re-enable PCI device after reset.\n");
7587                 result = PCI_ERS_RESULT_DISCONNECT;
7588         } else {
7589                 pci_set_master(pdev);
7590                 pci_restore_state(pdev);
7591                 pci_save_state(pdev);
7592                 pci_wake_from_d3(pdev, false);
7593
7594                 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
7595                 if (reg == 0)
7596                         result = PCI_ERS_RESULT_RECOVERED;
7597                 else
7598                         result = PCI_ERS_RESULT_DISCONNECT;
7599         }
7600
7601         err = pci_cleanup_aer_uncorrect_error_status(pdev);
7602         if (err) {
7603                 dev_info(&pdev->dev,
7604                          "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
7605                          err);
7606                 /* non-fatal, continue */
7607         }
7608
7609         return result;
7610 }
7611
7612 /**
7613  * i40e_pci_error_resume - restart operations after PCI error recovery
7614  * @pdev: PCI device information struct
7615  *
7616  * Called to allow the driver to bring things back up after PCI error
7617  * and/or reset recovery has finished.
7618  **/
7619 static void i40e_pci_error_resume(struct pci_dev *pdev)
7620 {
7621         struct i40e_pf *pf = pci_get_drvdata(pdev);
7622
7623         dev_info(&pdev->dev, "%s\n", __func__);
7624         i40e_handle_reset_warning(pf);
7625 }
7626
7627 static const struct pci_error_handlers i40e_err_handler = {
7628         .error_detected = i40e_pci_error_detected,
7629         .slot_reset = i40e_pci_error_slot_reset,
7630         .resume = i40e_pci_error_resume,
7631 };
7632
7633 static struct pci_driver i40e_driver = {
7634         .name     = i40e_driver_name,
7635         .id_table = i40e_pci_tbl,
7636         .probe    = i40e_probe,
7637         .remove   = i40e_remove,
7638         .err_handler = &i40e_err_handler,
7639         .sriov_configure = i40e_pci_sriov_configure,
7640 };
7641
7642 /**
7643  * i40e_init_module - Driver registration routine
7644  *
7645  * i40e_init_module is the first routine called when the driver is
7646  * loaded. All it does is register with the PCI subsystem.
7647  **/
7648 static int __init i40e_init_module(void)
7649 {
7650         pr_info("%s: %s - version %s\n", i40e_driver_name,
7651                 i40e_driver_string, i40e_driver_version_str);
7652         pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
7653         i40e_dbg_init();
7654         return pci_register_driver(&i40e_driver);
7655 }
7656 module_init(i40e_init_module);
7657
7658 /**
7659  * i40e_exit_module - Driver exit cleanup routine
7660  *
7661  * i40e_exit_module is called just before the driver is removed
7662  * from memory.
7663  **/
7664 static void __exit i40e_exit_module(void)
7665 {
7666         pci_unregister_driver(&i40e_driver);
7667         i40e_dbg_exit();
7668 }
7669 module_exit(i40e_exit_module);