i40e: Bump version
[deliverable/linux.git] / drivers / net / ethernet / intel / i40e / i40e_main.c
CommitLineData
41c445ff
JB
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
31const char i40e_driver_name[] = "i40e";
32static 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
217ffd41 39#define DRV_VERSION_BUILD 12
41c445ff
JB
40#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
41 __stringify(DRV_VERSION_MINOR) "." \
42 __stringify(DRV_VERSION_BUILD) DRV_KERN
43const char i40e_driver_version_str[] = DRV_VERSION;
44static const char i40e_copyright[] = "Copyright (c) 2013 Intel Corporation.";
45
46/* a bit of forward declarations */
47static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
48static void i40e_handle_reset_warning(struct i40e_pf *pf);
49static int i40e_add_vsi(struct i40e_vsi *vsi);
50static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
51static int i40e_setup_pf_switch(struct i40e_pf *pf);
52static int i40e_setup_misc_vector(struct i40e_pf *pf);
53static void i40e_determine_queue_usage(struct i40e_pf *pf);
54static 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 */
63static 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};
77MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
78
79#define I40E_MAX_VF_COUNT 128
80static int debug = -1;
81module_param(debug, int, 0);
82MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
83
84MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
85MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
86MODULE_LICENSE("GPL");
87MODULE_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 **/
96int 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);
93bc73b8
JB
104 if (!mem->va)
105 return -ENOMEM;
41c445ff 106
93bc73b8 107 return 0;
41c445ff
JB
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 **/
115int 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 **/
133int 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
93bc73b8
JB
139 if (!mem->va)
140 return -ENOMEM;
41c445ff 141
93bc73b8 142 return 0;
41c445ff
JB
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 **/
150int 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 **/
173static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
174 u16 needed, u16 id)
175{
176 int ret = -ENOMEM;
ddf434ac 177 int i, j;
41c445ff
JB
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;
ddf434ac 188 while (i < pile->num_entries) {
41c445ff
JB
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;
ddf434ac 207 break;
41c445ff
JB
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 **/
225static 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 **/
253static 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 **/
269static 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 **/
316static 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 **/
336struct 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 **/
348static struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
349 struct net_device *netdev,
980e9b11 350 struct rtnl_link_stats64 *stats)
41c445ff
JB
351{
352 struct i40e_netdev_priv *np = netdev_priv(netdev);
353 struct i40e_vsi *vsi = np->vsi;
980e9b11
AD
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));
41c445ff 382
980e9b11
AD
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;
41c445ff 395
980e9b11 396 return stats;
41c445ff
JB
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 **/
403void 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++) {
9f65e15b
AD
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));
41c445ff
JB
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 **/
434void 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 **/
456static 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 **/
484static 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 **/
502void 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 **/
561static 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);
7134f9ce
JB
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);
41c445ff
JB
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 **/
616static 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++) {
9f65e15b 645 struct i40e_ring *ring = vsi->tx_rings[i];
41c445ff
JB
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 **/
657static 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++) {
9f65e15b 699 struct i40e_ring *ring = vsi->tx_rings[i];
41c445ff
JB
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 **/
719void 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;
980e9b11 751 rcu_read_lock();
41c445ff
JB
752 for (q = 0; q < vsi->num_queue_pairs; q++) {
753 struct i40e_ring *p;
980e9b11
AD
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;
41c445ff 769
980e9b11
AD
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;
41c445ff
JB
779 rx_buf += p->rx_stats.alloc_rx_buff_failed;
780 rx_page += p->rx_stats.alloc_rx_page_failed;
41c445ff 781 }
980e9b11 782 rcu_read_unlock();
41c445ff
JB
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 **/
978static 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 **/
1007struct 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 **/
1030bool 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 **/
1057struct 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 **/
1085struct 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
1131add_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 **/
1143void 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 **/
1193static 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 **/
1248static 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 **/
1364static 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 **/
1445int 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;
dcae29be 1452 i40e_status aq_ret = 0;
41c445ff
JB
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) {
dcae29be 1513 aq_ret = i40e_aq_remove_macvlan(&pf->hw,
41c445ff
JB
1514 vsi->seid, del_list, num_del,
1515 NULL);
1516 num_del = 0;
1517 memset(del_list, 0, sizeof(*del_list));
1518
dcae29be 1519 if (aq_ret)
41c445ff
JB
1520 dev_info(&pf->pdev->dev,
1521 "ignoring delete macvlan error, err %d, aq_err %d while flushing a full buffer\n",
dcae29be 1522 aq_ret,
41c445ff
JB
1523 pf->hw.aq.asq_last_status);
1524 }
1525 }
1526 if (num_del) {
dcae29be 1527 aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid,
41c445ff
JB
1528 del_list, num_del, NULL);
1529 num_del = 0;
1530
dcae29be 1531 if (aq_ret)
41c445ff
JB
1532 dev_info(&pf->pdev->dev,
1533 "ignoring delete macvlan error, err %d, aq_err %d\n",
dcae29be 1534 aq_ret, pf->hw.aq.asq_last_status);
41c445ff
JB
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) {
dcae29be
JB
1579 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1580 add_list, num_add,
1581 NULL);
41c445ff
JB
1582 num_add = 0;
1583
dcae29be 1584 if (aq_ret)
41c445ff
JB
1585 break;
1586 memset(add_list, 0, sizeof(*add_list));
1587 }
1588 }
1589 if (num_add) {
dcae29be
JB
1590 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1591 add_list, num_add, NULL);
41c445ff
JB
1592 num_add = 0;
1593 }
1594 kfree(add_list);
1595 add_list = NULL;
1596
dcae29be 1597 if (add_happened && (!aq_ret)) {
41c445ff 1598 /* do nothing */;
dcae29be 1599 } else if (add_happened && (aq_ret)) {
41c445ff
JB
1600 dev_info(&pf->pdev->dev,
1601 "add filter failed, err %d, aq_err %d\n",
dcae29be 1602 aq_ret, pf->hw.aq.asq_last_status);
41c445ff
JB
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);
dcae29be
JB
1618 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
1619 vsi->seid,
1620 cur_multipromisc,
1621 NULL);
1622 if (aq_ret)
41c445ff
JB
1623 dev_info(&pf->pdev->dev,
1624 "set multi promisc failed, err %d, aq_err %d\n",
dcae29be 1625 aq_ret, pf->hw.aq.asq_last_status);
41c445ff
JB
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));
dcae29be
JB
1632 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(&vsi->back->hw,
1633 vsi->seid,
1634 cur_promisc, NULL);
1635 if (aq_ret)
41c445ff
JB
1636 dev_info(&pf->pdev->dev,
1637 "set uni promisc failed, err %d, aq_err %d\n",
dcae29be 1638 aq_ret, pf->hw.aq.asq_last_status);
41c445ff
JB
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 **/
1649static 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 **/
1671static 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 **/
1694void 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 **/
1722void 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 **/
1752static 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 **/
1768int 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)
078b5876
JB
1851 *
1852 * Return: 0 on success or negative otherwise
41c445ff
JB
1853 **/
1854int 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
078b5876
JB
1926 *
1927 * net_device_ops implementation for adding vlan ids
41c445ff
JB
1928 **/
1929static 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;
078b5876 1934 int ret = 0;
41c445ff
JB
1935
1936 if (vid > 4095)
078b5876
JB
1937 return -EINVAL;
1938
1939 netdev_info(netdev, "adding %pM vid=%d\n", netdev->dev_addr, vid);
41c445ff 1940
41c445ff
JB
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
078b5876
JB
1947 if (!ret && (vid < VLAN_N_VID))
1948 set_bit(vid, vsi->active_vlans);
41c445ff 1949
078b5876 1950 return ret;
41c445ff
JB
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
078b5876
JB
1957 *
1958 * net_device_ops implementation for adding vlan ids
41c445ff
JB
1959 **/
1960static 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
078b5876
JB
1966 netdev_info(netdev, "removing %pM vid=%d\n", netdev->dev_addr, vid);
1967
41c445ff
JB
1968 /* return code is ignored as there is nothing a user
1969 * can do about failure to remove and a log message was
078b5876 1970 * already printed from the other function
41c445ff
JB
1971 */
1972 i40e_vsi_kill_vlan(vsi, vid);
1973
1974 clear_bit(vid, vsi->active_vlans);
078b5876 1975
41c445ff
JB
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 **/
1983static 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 **/
dcae29be 2002int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
41c445ff
JB
2003{
2004 struct i40e_vsi_context ctxt;
dcae29be 2005 i40e_status aq_ret;
41c445ff
JB
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));
dcae29be
JB
2014 aq_ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2015 if (aq_ret) {
41c445ff
JB
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);
dcae29be 2019 return -ENOENT;
41c445ff
JB
2020 }
2021
dcae29be 2022 return 0;
41c445ff
JB
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 **/
2031void 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 **/
2047static 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++)
9f65e15b 2052 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
41c445ff
JB
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 **/
2063static 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++)
9f65e15b
AD
2068 if (vsi->tx_rings[i]->desc)
2069 i40e_free_tx_resources(vsi->tx_rings[i]);
41c445ff
JB
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 **/
2082static 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++)
9f65e15b 2087 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
41c445ff
JB
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 **/
2097static 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++)
9f65e15b
AD
2102 if (vsi->rx_rings[i]->desc)
2103 i40e_free_rx_resources(vsi->rx_rings[i]);
41c445ff
JB
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 **/
2112static 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;
13fd9774
SN
2178 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2179 I40E_QTX_CTL_PF_INDX_MASK);
41c445ff
JB
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 **/
2197static 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;
7134f9ce
JB
2244 if (hw->revision_id == 0)
2245 rx_ctx.lrxqthresh = 0;
2246 else
2247 rx_ctx.lrxqthresh = 2;
41c445ff
JB
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 **/
2285static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2286{
2287 int err = 0;
2288 u16 i;
2289
9f65e15b
AD
2290 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2291 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
41c445ff
JB
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 **/
2302static 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++)
9f65e15b 2341 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
41c445ff
JB
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 **/
2350static 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++) {
9f65e15b
AD
2365 struct i40e_ring *rx_ring = vsi->rx_rings[i];
2366 struct i40e_ring *tx_ring = vsi->tx_rings[i];
41c445ff
JB
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 **/
2377static 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 **/
2387static 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 **/
2405static 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;
493fb300
AD
2421 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
2422 q_vector = vsi->q_vectors[i];
41c445ff
JB
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 **/
2468static 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 **/
2500static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
2501{
493fb300 2502 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
41c445ff
JB
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 **/
116a57d4 2539void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
41c445ff
JB
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 **/
2557void 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);
1022cb6c 2567 /* skip the flush */
41c445ff
JB
2568}
2569
2570/**
2571 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
2572 * @irq: interrupt number
2573 * @data: pointer to a q_vector
2574 **/
2575static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
2576{
2577 struct i40e_q_vector *q_vector = data;
2578
cd0b6fa6 2579 if (!q_vector->tx.ring && !q_vector->rx.ring)
41c445ff
JB
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 **/
2592static irqreturn_t i40e_fdir_clean_rings(int irq, void *data)
2593{
2594 struct i40e_q_vector *q_vector = data;
2595
cd0b6fa6 2596 if (!q_vector->tx.ring && !q_vector->rx.ring)
41c445ff
JB
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 **/
2611static 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++) {
493fb300 2621 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
41c445ff 2622
cd0b6fa6 2623 if (q_vector->tx.ring && q_vector->rx.ring) {
41c445ff
JB
2624 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2625 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
2626 tx_int_idx++;
cd0b6fa6 2627 } else if (q_vector->rx.ring) {
41c445ff
JB
2628 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2629 "%s-%s-%d", basename, "rx", rx_int_idx++);
cd0b6fa6 2630 } else if (q_vector->tx.ring) {
41c445ff
JB
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
2655free_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 **/
2670static 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++) {
9f65e15b
AD
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);
41c445ff
JB
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 **/
2703static 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
1022cb6c 2716 i40e_flush(&pf->hw);
41c445ff
JB
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 **/
2724static 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 **/
2740static 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
41c445ff
JB
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
116a57d4
SN
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
41c445ff
JB
2757 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
2758
cd92e72f
SN
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
41c445ff
JB
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);
41c445ff
JB
2775
2776 if (!test_bit(__I40E_DOWN, &pf->state))
493fb300 2777 napi_schedule(&pf->vsi[pf->lan_vsi]->q_vectors[0]->napi);
41c445ff
JB
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;
d52cf0a9 2802 if (val == I40E_RESET_CORER)
41c445ff 2803 pf->corer_count++;
d52cf0a9 2804 else if (val == I40E_RESET_GLOBR)
41c445ff 2805 pf->globr_count++;
d52cf0a9 2806 else if (val == I40E_RESET_EMPR)
41c445ff
JB
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);
41c445ff
JB
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/**
cd0b6fa6 2845 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
41c445ff
JB
2846 * @vsi: the VSI being configured
2847 * @v_idx: vector index
cd0b6fa6 2848 * @qp_idx: queue pair index
41c445ff 2849 **/
cd0b6fa6 2850static void map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
41c445ff 2851{
493fb300 2852 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
9f65e15b
AD
2853 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
2854 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
41c445ff
JB
2855
2856 tx_ring->q_vector = q_vector;
cd0b6fa6
AD
2857 tx_ring->next = q_vector->tx.ring;
2858 q_vector->tx.ring = tx_ring;
41c445ff 2859 q_vector->tx.count++;
cd0b6fa6
AD
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++;
41c445ff
JB
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 **/
2876static 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;
cd0b6fa6 2880 int num_ringpairs;
41c445ff
JB
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++) {
cd0b6fa6
AD
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--;
41c445ff
JB
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 **/
2912static 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 **/
2940static 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++)
493fb300 2954 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
41c445ff
JB
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 **/
2967static 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
7134f9ce
JB
3028 if (hw->revision_id == 0)
3029 mdelay(50);
3030
41c445ff
JB
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 **/
3039static 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 **/
3103static 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 **/
3127static 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 */
493fb300 3143 if (vsi->q_vectors[i]->num_ringpairs == 0)
41c445ff
JB
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,
493fb300 3150 vsi->q_vectors[i]);
41c445ff
JB
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
493fb300
AD
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 **/
3242static 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];
cd0b6fa6 3245 struct i40e_ring *ring;
493fb300
AD
3246
3247 if (!q_vector)
3248 return;
3249
3250 /* disassociate q_vector from rings */
cd0b6fa6
AD
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;
493fb300
AD
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
41c445ff
JB
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 **/
3273static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
3274{
3275 int v_idx;
3276
493fb300
AD
3277 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
3278 i40e_free_q_vector(vsi, v_idx);
41c445ff
JB
3279}
3280
3281/**
3282 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
3283 * @pf: board private structure
3284 **/
3285static 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 **/
3305static 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 **/
3320static 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++)
493fb300 3328 napi_enable(&vsi->q_vectors[q_idx]->napi);
41c445ff
JB
3329}
3330
3331/**
3332 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
3333 * @vsi: the VSI being configured
3334 **/
3335static 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++)
493fb300 3343 napi_disable(&vsi->q_vectors[q_idx]->napi);
41c445ff
JB
3344}
3345
3346/**
3347 * i40e_quiesce_vsi - Pause a given VSI
3348 * @vsi: the VSI being paused
3349 **/
3350static 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 **/
3368static 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 **/
3384static 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 **/
3398static 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 **/
3414static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
3415{
078b5876
JB
3416 u8 num_tc = 0;
3417 int i;
41c445ff
JB
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 */
078b5876 3432 return num_tc + 1;
41c445ff
JB
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 **/
3442static 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 **/
3460static 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 **/
3491static 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 **/
3514static 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 **/
3534static 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;
dcae29be 3540 i40e_status aq_ret;
41c445ff 3541 u32 tc_bw_max;
41c445ff
JB
3542 int i;
3543
3544 /* Get the VSI level BW configuration */
dcae29be
JB
3545 aq_ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
3546 if (aq_ret) {
41c445ff
JB
3547 dev_info(&pf->pdev->dev,
3548 "couldn't get pf vsi bw config, err %d, aq_err %d\n",
dcae29be
JB
3549 aq_ret, pf->hw.aq.asq_last_status);
3550 return -EINVAL;
41c445ff
JB
3551 }
3552
3553 /* Get the VSI level BW configuration per TC */
dcae29be
JB
3554 aq_ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
3555 NULL);
3556 if (aq_ret) {
41c445ff
JB
3557 dev_info(&pf->pdev->dev,
3558 "couldn't get pf vsi ets bw config, err %d, aq_err %d\n",
dcae29be
JB
3559 aq_ret, pf->hw.aq.asq_last_status);
3560 return -EINVAL;
41c445ff
JB
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 }
078b5876 3582
dcae29be 3583 return 0;
41c445ff
JB
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 **/
dcae29be 3594static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
41c445ff
JB
3595 u8 *bw_share)
3596{
3597 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
dcae29be
JB
3598 i40e_status aq_ret;
3599 int i;
41c445ff
JB
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
dcae29be
JB
3605 aq_ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
3606 NULL);
3607 if (aq_ret) {
41c445ff
JB
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);
dcae29be 3611 return -EINVAL;
41c445ff
JB
3612 }
3613
3614 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3615 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
3616
dcae29be 3617 return 0;
41c445ff
JB
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 **/
3626static 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 **/
3678static 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 **/
3705static 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);
3761out:
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 **/
3769static 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)) {
6d779b41 3790 netdev_info(vsi->netdev, "NIC Link is Up\n");
41c445ff
JB
3791 netif_tx_start_all_queues(vsi->netdev);
3792 netif_carrier_on(vsi->netdev);
6d779b41
AS
3793 } else if (vsi->netdev) {
3794 netdev_info(vsi->netdev, "NIC Link is Down\n");
41c445ff
JB
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 **/
3808static 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 **/
3831int 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 **/
3846void 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++) {
9f65e15b
AD
3862 i40e_clean_tx_ring(vsi->tx_rings[i]);
3863 i40e_clean_rx_ring(vsi->rx_rings[i]);
41c445ff
JB
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 **/
3872static 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
3921exit:
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 **/
3937static 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
3983err_up_complete:
3984 i40e_down(vsi);
3985 i40e_vsi_free_irq(vsi);
3986err_setup_rx:
3987 i40e_vsi_free_rx_resources(vsi);
3988err_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 **/
4006static 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 **/
4032void 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
7823fe34
SN
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
41c445ff
JB
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 **/
4129static 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 **/
4161static 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 **/
4174static 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 **/
4191static 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 **/
4227static 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 **/
4251static 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
6d779b41
AS
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"));
41c445ff
JB
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 **/
4284static 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++) {
9f65e15b 4308 set_check_for_tx_hang(vsi->tx_rings[i]);
41c445ff 4309 if (test_bit(__I40E_HANG_CHECK_ARMED,
9f65e15b 4310 &vsi->tx_rings[i]->state))
41c445ff
JB
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 **/
4336static 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 **/
4362static 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 **/
4403static 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 **/
4441static 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 **/
4516static 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
4582end_reconstitute:
4583 return ret;
4584}
4585
4586/**
4587 * i40e_get_capabilities - get info about the HW
4588 * @pf: the PF struct
4589 **/
4590static 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
7134f9ce
JB
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
41c445ff
JB
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
7134f9ce
JB
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
41c445ff
JB
4649 return 0;
4650}
4651
4652/**
4653 * i40e_fdir_setup - initialize the Flow Director resources
4654 * @pf: board private structure
4655 **/
4656static void i40e_fdir_setup(struct i40e_pf *pf)
4657{
4658 struct i40e_vsi *vsi;
4659 bool new_vsi = false;
4660 int err, i;
4661
958a3e3b
SN
4662 if (!(pf->flags & (I40E_FLAG_FDIR_ENABLED |
4663 I40E_FLAG_FDIR_ATR_ENABLED)))
41c445ff
JB
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 **/
4706static 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_handle_reset_warning - prep for the core to reset
4720 * @pf: board private structure
4721 *
4722 * Close up the VFs and other things in prep for a Core Reset,
4723 * then get ready to rebuild the world.
4724 **/
4725static void i40e_handle_reset_warning(struct i40e_pf *pf)
4726{
4727 struct i40e_driver_version dv;
4728 struct i40e_hw *hw = &pf->hw;
4729 i40e_status ret;
4730 u32 v;
4731
4732 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
4733 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
4734 return;
4735
4736 dev_info(&pf->pdev->dev, "Tearing down internal switch for reset\n");
4737
4738 i40e_vc_notify_reset(pf);
4739
4740 /* quiesce the VSIs and their queues that are not already DOWN */
4741 i40e_pf_quiesce_all_vsi(pf);
4742
4743 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4744 if (pf->vsi[v])
4745 pf->vsi[v]->seid = 0;
4746 }
4747
4748 i40e_shutdown_adminq(&pf->hw);
4749
4750 /* Now we wait for GRST to settle out.
4751 * We don't have to delete the VEBs or VSIs from the hw switch
4752 * because the reset will make them disappear.
4753 */
4754 ret = i40e_pf_reset(hw);
4755 if (ret)
4756 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
4757 pf->pfr_count++;
4758
4759 if (test_bit(__I40E_DOWN, &pf->state))
4760 goto end_core_reset;
4761 dev_info(&pf->pdev->dev, "Rebuilding internal switch\n");
4762
4763 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
4764 ret = i40e_init_adminq(&pf->hw);
4765 if (ret) {
4766 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, %d\n", ret);
4767 goto end_core_reset;
4768 }
4769
4770 ret = i40e_get_capabilities(pf);
4771 if (ret) {
4772 dev_info(&pf->pdev->dev, "i40e_get_capabilities failed, %d\n",
4773 ret);
4774 goto end_core_reset;
4775 }
4776
4777 /* call shutdown HMC */
4778 ret = i40e_shutdown_lan_hmc(hw);
4779 if (ret) {
4780 dev_info(&pf->pdev->dev, "shutdown_lan_hmc failed: %d\n", ret);
4781 goto end_core_reset;
4782 }
4783
4784 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
4785 hw->func_caps.num_rx_qp,
4786 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
4787 if (ret) {
4788 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
4789 goto end_core_reset;
4790 }
4791 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
4792 if (ret) {
4793 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
4794 goto end_core_reset;
4795 }
4796
4797 /* do basic switch setup */
4798 ret = i40e_setup_pf_switch(pf);
4799 if (ret)
4800 goto end_core_reset;
4801
4802 /* Rebuild the VSIs and VEBs that existed before reset.
4803 * They are still in our local switch element arrays, so only
4804 * need to rebuild the switch model in the HW.
4805 *
4806 * If there were VEBs but the reconstitution failed, we'll try
4807 * try to recover minimal use by getting the basic PF VSI working.
4808 */
4809 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
4810 dev_info(&pf->pdev->dev, "attempting to rebuild switch\n");
4811 /* find the one VEB connected to the MAC, and find orphans */
4812 for (v = 0; v < I40E_MAX_VEB; v++) {
4813 if (!pf->veb[v])
4814 continue;
4815
4816 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
4817 pf->veb[v]->uplink_seid == 0) {
4818 ret = i40e_reconstitute_veb(pf->veb[v]);
4819
4820 if (!ret)
4821 continue;
4822
4823 /* If Main VEB failed, we're in deep doodoo,
4824 * so give up rebuilding the switch and set up
4825 * for minimal rebuild of PF VSI.
4826 * If orphan failed, we'll report the error
4827 * but try to keep going.
4828 */
4829 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
4830 dev_info(&pf->pdev->dev,
4831 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
4832 ret);
4833 pf->vsi[pf->lan_vsi]->uplink_seid
4834 = pf->mac_seid;
4835 break;
4836 } else if (pf->veb[v]->uplink_seid == 0) {
4837 dev_info(&pf->pdev->dev,
4838 "rebuild of orphan VEB failed: %d\n",
4839 ret);
4840 }
4841 }
4842 }
4843 }
4844
4845 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
4846 dev_info(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
4847 /* no VEB, so rebuild only the Main VSI */
4848 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
4849 if (ret) {
4850 dev_info(&pf->pdev->dev,
4851 "rebuild of Main VSI failed: %d\n", ret);
4852 goto end_core_reset;
4853 }
4854 }
4855
4856 /* reinit the misc interrupt */
4857 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4858 ret = i40e_setup_misc_vector(pf);
4859
4860 /* restart the VSIs that were rebuilt and running before the reset */
4861 i40e_pf_unquiesce_all_vsi(pf);
4862
4863 /* tell the firmware that we're starting */
4864 dv.major_version = DRV_VERSION_MAJOR;
4865 dv.minor_version = DRV_VERSION_MINOR;
4866 dv.build_version = DRV_VERSION_BUILD;
4867 dv.subbuild_version = 0;
4868 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
4869
4870 dev_info(&pf->pdev->dev, "PF reset done\n");
4871
4872end_core_reset:
4873 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
4874}
4875
4876/**
4877 * i40e_handle_mdd_event
4878 * @pf: pointer to the pf structure
4879 *
4880 * Called from the MDD irq handler to identify possibly malicious vfs
4881 **/
4882static void i40e_handle_mdd_event(struct i40e_pf *pf)
4883{
4884 struct i40e_hw *hw = &pf->hw;
4885 bool mdd_detected = false;
4886 struct i40e_vf *vf;
4887 u32 reg;
4888 int i;
4889
4890 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
4891 return;
4892
4893 /* find what triggered the MDD event */
4894 reg = rd32(hw, I40E_GL_MDET_TX);
4895 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
4896 u8 func = (reg & I40E_GL_MDET_TX_FUNCTION_MASK)
4897 >> I40E_GL_MDET_TX_FUNCTION_SHIFT;
4898 u8 event = (reg & I40E_GL_MDET_TX_EVENT_SHIFT)
4899 >> I40E_GL_MDET_TX_EVENT_SHIFT;
4900 u8 queue = (reg & I40E_GL_MDET_TX_QUEUE_MASK)
4901 >> I40E_GL_MDET_TX_QUEUE_SHIFT;
4902 dev_info(&pf->pdev->dev,
4903 "Malicious Driver Detection TX event 0x%02x on q %d of function 0x%02x\n",
4904 event, queue, func);
4905 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
4906 mdd_detected = true;
4907 }
4908 reg = rd32(hw, I40E_GL_MDET_RX);
4909 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
4910 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK)
4911 >> I40E_GL_MDET_RX_FUNCTION_SHIFT;
4912 u8 event = (reg & I40E_GL_MDET_RX_EVENT_SHIFT)
4913 >> I40E_GL_MDET_RX_EVENT_SHIFT;
4914 u8 queue = (reg & I40E_GL_MDET_RX_QUEUE_MASK)
4915 >> I40E_GL_MDET_RX_QUEUE_SHIFT;
4916 dev_info(&pf->pdev->dev,
4917 "Malicious Driver Detection RX event 0x%02x on q %d of function 0x%02x\n",
4918 event, queue, func);
4919 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
4920 mdd_detected = true;
4921 }
4922
4923 /* see if one of the VFs needs its hand slapped */
4924 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
4925 vf = &(pf->vf[i]);
4926 reg = rd32(hw, I40E_VP_MDET_TX(i));
4927 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
4928 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
4929 vf->num_mdd_events++;
4930 dev_info(&pf->pdev->dev, "MDD TX event on VF %d\n", i);
4931 }
4932
4933 reg = rd32(hw, I40E_VP_MDET_RX(i));
4934 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
4935 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
4936 vf->num_mdd_events++;
4937 dev_info(&pf->pdev->dev, "MDD RX event on VF %d\n", i);
4938 }
4939
4940 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
4941 dev_info(&pf->pdev->dev,
4942 "Too many MDD events on VF %d, disabled\n", i);
4943 dev_info(&pf->pdev->dev,
4944 "Use PF Control I/F to re-enable the VF\n");
4945 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
4946 }
4947 }
4948
4949 /* re-enable mdd interrupt cause */
4950 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
4951 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4952 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4953 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4954 i40e_flush(hw);
4955}
4956
4957/**
4958 * i40e_service_task - Run the driver's async subtasks
4959 * @work: pointer to work_struct containing our data
4960 **/
4961static void i40e_service_task(struct work_struct *work)
4962{
4963 struct i40e_pf *pf = container_of(work,
4964 struct i40e_pf,
4965 service_task);
4966 unsigned long start_time = jiffies;
4967
4968 i40e_reset_subtask(pf);
4969 i40e_handle_mdd_event(pf);
4970 i40e_vc_process_vflr_event(pf);
4971 i40e_watchdog_subtask(pf);
4972 i40e_fdir_reinit_subtask(pf);
4973 i40e_check_hang_subtask(pf);
4974 i40e_sync_filters_subtask(pf);
4975 i40e_clean_adminq_subtask(pf);
4976
4977 i40e_service_event_complete(pf);
4978
4979 /* If the tasks have taken longer than one timer cycle or there
4980 * is more work to be done, reschedule the service task now
4981 * rather than wait for the timer to tick again.
4982 */
4983 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
4984 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
4985 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
4986 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
4987 i40e_service_event_schedule(pf);
4988}
4989
4990/**
4991 * i40e_service_timer - timer callback
4992 * @data: pointer to PF struct
4993 **/
4994static void i40e_service_timer(unsigned long data)
4995{
4996 struct i40e_pf *pf = (struct i40e_pf *)data;
4997
4998 mod_timer(&pf->service_timer,
4999 round_jiffies(jiffies + pf->service_timer_period));
5000 i40e_service_event_schedule(pf);
5001}
5002
5003/**
5004 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
5005 * @vsi: the VSI being configured
5006 **/
5007static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
5008{
5009 struct i40e_pf *pf = vsi->back;
5010
5011 switch (vsi->type) {
5012 case I40E_VSI_MAIN:
5013 vsi->alloc_queue_pairs = pf->num_lan_qps;
5014 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5015 I40E_REQ_DESCRIPTOR_MULTIPLE);
5016 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5017 vsi->num_q_vectors = pf->num_lan_msix;
5018 else
5019 vsi->num_q_vectors = 1;
5020
5021 break;
5022
5023 case I40E_VSI_FDIR:
5024 vsi->alloc_queue_pairs = 1;
5025 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
5026 I40E_REQ_DESCRIPTOR_MULTIPLE);
5027 vsi->num_q_vectors = 1;
5028 break;
5029
5030 case I40E_VSI_VMDQ2:
5031 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
5032 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5033 I40E_REQ_DESCRIPTOR_MULTIPLE);
5034 vsi->num_q_vectors = pf->num_vmdq_msix;
5035 break;
5036
5037 case I40E_VSI_SRIOV:
5038 vsi->alloc_queue_pairs = pf->num_vf_qps;
5039 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5040 I40E_REQ_DESCRIPTOR_MULTIPLE);
5041 break;
5042
5043 default:
5044 WARN_ON(1);
5045 return -ENODATA;
5046 }
5047
5048 return 0;
5049}
5050
5051/**
5052 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
5053 * @pf: board private structure
5054 * @type: type of VSI
5055 *
5056 * On error: returns error code (negative)
5057 * On success: returns vsi index in PF (positive)
5058 **/
5059static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
5060{
5061 int ret = -ENODEV;
5062 struct i40e_vsi *vsi;
493fb300 5063 int sz_vectors;
9f65e15b 5064 int sz_rings;
41c445ff
JB
5065 int vsi_idx;
5066 int i;
5067
5068 /* Need to protect the allocation of the VSIs at the PF level */
5069 mutex_lock(&pf->switch_mutex);
5070
5071 /* VSI list may be fragmented if VSI creation/destruction has
5072 * been happening. We can afford to do a quick scan to look
5073 * for any free VSIs in the list.
5074 *
5075 * find next empty vsi slot, looping back around if necessary
5076 */
5077 i = pf->next_vsi;
5078 while (i < pf->hw.func_caps.num_vsis && pf->vsi[i])
5079 i++;
5080 if (i >= pf->hw.func_caps.num_vsis) {
5081 i = 0;
5082 while (i < pf->next_vsi && pf->vsi[i])
5083 i++;
5084 }
5085
5086 if (i < pf->hw.func_caps.num_vsis && !pf->vsi[i]) {
5087 vsi_idx = i; /* Found one! */
5088 } else {
5089 ret = -ENODEV;
493fb300 5090 goto unlock_pf; /* out of VSI slots! */
41c445ff
JB
5091 }
5092 pf->next_vsi = ++i;
5093
5094 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
5095 if (!vsi) {
5096 ret = -ENOMEM;
493fb300 5097 goto unlock_pf;
41c445ff
JB
5098 }
5099 vsi->type = type;
5100 vsi->back = pf;
5101 set_bit(__I40E_DOWN, &vsi->state);
5102 vsi->flags = 0;
5103 vsi->idx = vsi_idx;
5104 vsi->rx_itr_setting = pf->rx_itr_default;
5105 vsi->tx_itr_setting = pf->tx_itr_default;
5106 vsi->netdev_registered = false;
5107 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
5108 INIT_LIST_HEAD(&vsi->mac_filter_list);
5109
9f65e15b
AD
5110 ret = i40e_set_num_rings_in_vsi(vsi);
5111 if (ret)
5112 goto err_rings;
5113
5114 /* allocate memory for ring pointers */
5115 sz_rings = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
5116 vsi->tx_rings = kzalloc(sz_rings, GFP_KERNEL);
5117 if (!vsi->tx_rings) {
5118 ret = -ENOMEM;
5119 goto err_rings;
5120 }
5121 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
41c445ff 5122
493fb300
AD
5123 /* allocate memory for q_vector pointers */
5124 sz_vectors = sizeof(struct i40e_q_vectors *) * vsi->num_q_vectors;
5125 vsi->q_vectors = kzalloc(sz_vectors, GFP_KERNEL);
5126 if (!vsi->q_vectors) {
5127 ret = -ENOMEM;
5128 goto err_vectors;
5129 }
5130
41c445ff
JB
5131 /* Setup default MSIX irq handler for VSI */
5132 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
5133
5134 pf->vsi[vsi_idx] = vsi;
5135 ret = vsi_idx;
493fb300
AD
5136 goto unlock_pf;
5137
5138err_vectors:
9f65e15b
AD
5139 kfree(vsi->tx_rings);
5140err_rings:
493fb300
AD
5141 pf->next_vsi = i - 1;
5142 kfree(vsi);
5143unlock_pf:
41c445ff
JB
5144 mutex_unlock(&pf->switch_mutex);
5145 return ret;
5146}
5147
5148/**
5149 * i40e_vsi_clear - Deallocate the VSI provided
5150 * @vsi: the VSI being un-configured
5151 **/
5152static int i40e_vsi_clear(struct i40e_vsi *vsi)
5153{
5154 struct i40e_pf *pf;
5155
5156 if (!vsi)
5157 return 0;
5158
5159 if (!vsi->back)
5160 goto free_vsi;
5161 pf = vsi->back;
5162
5163 mutex_lock(&pf->switch_mutex);
5164 if (!pf->vsi[vsi->idx]) {
5165 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
5166 vsi->idx, vsi->idx, vsi, vsi->type);
5167 goto unlock_vsi;
5168 }
5169
5170 if (pf->vsi[vsi->idx] != vsi) {
5171 dev_err(&pf->pdev->dev,
5172 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
5173 pf->vsi[vsi->idx]->idx,
5174 pf->vsi[vsi->idx],
5175 pf->vsi[vsi->idx]->type,
5176 vsi->idx, vsi, vsi->type);
5177 goto unlock_vsi;
5178 }
5179
5180 /* updates the pf for this cleared vsi */
5181 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
5182 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
5183
493fb300
AD
5184 /* free the ring and vector containers */
5185 kfree(vsi->q_vectors);
9f65e15b 5186 kfree(vsi->tx_rings);
493fb300 5187
41c445ff
JB
5188 pf->vsi[vsi->idx] = NULL;
5189 if (vsi->idx < pf->next_vsi)
5190 pf->next_vsi = vsi->idx;
5191
5192unlock_vsi:
5193 mutex_unlock(&pf->switch_mutex);
5194free_vsi:
5195 kfree(vsi);
5196
5197 return 0;
5198}
5199
9f65e15b
AD
5200/**
5201 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
5202 * @vsi: the VSI being cleaned
5203 **/
5204static s32 i40e_vsi_clear_rings(struct i40e_vsi *vsi)
5205{
5206 int i;
5207
00403f04
MW
5208 if (vsi->tx_rings[0])
5209 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
5210 kfree_rcu(vsi->tx_rings[i], rcu);
5211 vsi->tx_rings[i] = NULL;
5212 vsi->rx_rings[i] = NULL;
5213 }
9f65e15b
AD
5214
5215 return 0;
5216}
5217
41c445ff
JB
5218/**
5219 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
5220 * @vsi: the VSI being configured
5221 **/
5222static int i40e_alloc_rings(struct i40e_vsi *vsi)
5223{
5224 struct i40e_pf *pf = vsi->back;
41c445ff
JB
5225 int i;
5226
41c445ff
JB
5227 /* Set basic values in the rings to be used later during open() */
5228 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
9f65e15b
AD
5229 struct i40e_ring *tx_ring;
5230 struct i40e_ring *rx_ring;
5231
5232 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
5233 if (!tx_ring)
5234 goto err_out;
41c445ff
JB
5235
5236 tx_ring->queue_index = i;
5237 tx_ring->reg_idx = vsi->base_queue + i;
5238 tx_ring->ring_active = false;
5239 tx_ring->vsi = vsi;
5240 tx_ring->netdev = vsi->netdev;
5241 tx_ring->dev = &pf->pdev->dev;
5242 tx_ring->count = vsi->num_desc;
5243 tx_ring->size = 0;
5244 tx_ring->dcb_tc = 0;
9f65e15b 5245 vsi->tx_rings[i] = tx_ring;
41c445ff 5246
9f65e15b 5247 rx_ring = &tx_ring[1];
41c445ff
JB
5248 rx_ring->queue_index = i;
5249 rx_ring->reg_idx = vsi->base_queue + i;
5250 rx_ring->ring_active = false;
5251 rx_ring->vsi = vsi;
5252 rx_ring->netdev = vsi->netdev;
5253 rx_ring->dev = &pf->pdev->dev;
5254 rx_ring->count = vsi->num_desc;
5255 rx_ring->size = 0;
5256 rx_ring->dcb_tc = 0;
5257 if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
5258 set_ring_16byte_desc_enabled(rx_ring);
5259 else
5260 clear_ring_16byte_desc_enabled(rx_ring);
9f65e15b 5261 vsi->rx_rings[i] = rx_ring;
41c445ff
JB
5262 }
5263
5264 return 0;
9f65e15b
AD
5265
5266err_out:
5267 i40e_vsi_clear_rings(vsi);
5268 return -ENOMEM;
41c445ff
JB
5269}
5270
5271/**
5272 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
5273 * @pf: board private structure
5274 * @vectors: the number of MSI-X vectors to request
5275 *
5276 * Returns the number of vectors reserved, or error
5277 **/
5278static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
5279{
5280 int err = 0;
5281
5282 pf->num_msix_entries = 0;
5283 while (vectors >= I40E_MIN_MSIX) {
5284 err = pci_enable_msix(pf->pdev, pf->msix_entries, vectors);
5285 if (err == 0) {
5286 /* good to go */
5287 pf->num_msix_entries = vectors;
5288 break;
5289 } else if (err < 0) {
5290 /* total failure */
5291 dev_info(&pf->pdev->dev,
5292 "MSI-X vector reservation failed: %d\n", err);
5293 vectors = 0;
5294 break;
5295 } else {
5296 /* err > 0 is the hint for retry */
5297 dev_info(&pf->pdev->dev,
5298 "MSI-X vectors wanted %d, retrying with %d\n",
5299 vectors, err);
5300 vectors = err;
5301 }
5302 }
5303
5304 if (vectors > 0 && vectors < I40E_MIN_MSIX) {
5305 dev_info(&pf->pdev->dev,
5306 "Couldn't get enough vectors, only %d available\n",
5307 vectors);
5308 vectors = 0;
5309 }
5310
5311 return vectors;
5312}
5313
5314/**
5315 * i40e_init_msix - Setup the MSIX capability
5316 * @pf: board private structure
5317 *
5318 * Work with the OS to set up the MSIX vectors needed.
5319 *
5320 * Returns 0 on success, negative on failure
5321 **/
5322static int i40e_init_msix(struct i40e_pf *pf)
5323{
5324 i40e_status err = 0;
5325 struct i40e_hw *hw = &pf->hw;
5326 int v_budget, i;
5327 int vec;
5328
5329 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
5330 return -ENODEV;
5331
5332 /* The number of vectors we'll request will be comprised of:
5333 * - Add 1 for "other" cause for Admin Queue events, etc.
5334 * - The number of LAN queue pairs
5335 * already adjusted for the NUMA node
5336 * assumes symmetric Tx/Rx pairing
5337 * - The number of VMDq pairs
5338 * Once we count this up, try the request.
5339 *
5340 * If we can't get what we want, we'll simplify to nearly nothing
5341 * and try again. If that still fails, we punt.
5342 */
5343 pf->num_lan_msix = pf->num_lan_qps;
5344 pf->num_vmdq_msix = pf->num_vmdq_qps;
5345 v_budget = 1 + pf->num_lan_msix;
5346 v_budget += (pf->num_vmdq_vsis * pf->num_vmdq_msix);
5347 if (pf->flags & I40E_FLAG_FDIR_ENABLED)
5348 v_budget++;
5349
5350 /* Scale down if necessary, and the rings will share vectors */
5351 v_budget = min_t(int, v_budget, hw->func_caps.num_msix_vectors);
5352
5353 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
5354 GFP_KERNEL);
5355 if (!pf->msix_entries)
5356 return -ENOMEM;
5357
5358 for (i = 0; i < v_budget; i++)
5359 pf->msix_entries[i].entry = i;
5360 vec = i40e_reserve_msix_vectors(pf, v_budget);
5361 if (vec < I40E_MIN_MSIX) {
5362 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
5363 kfree(pf->msix_entries);
5364 pf->msix_entries = NULL;
5365 return -ENODEV;
5366
5367 } else if (vec == I40E_MIN_MSIX) {
5368 /* Adjust for minimal MSIX use */
5369 dev_info(&pf->pdev->dev, "Features disabled, not enough MSIX vectors\n");
5370 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
5371 pf->num_vmdq_vsis = 0;
5372 pf->num_vmdq_qps = 0;
5373 pf->num_vmdq_msix = 0;
5374 pf->num_lan_qps = 1;
5375 pf->num_lan_msix = 1;
5376
5377 } else if (vec != v_budget) {
5378 /* Scale vector usage down */
5379 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
5380 vec--; /* reserve the misc vector */
5381
5382 /* partition out the remaining vectors */
5383 switch (vec) {
5384 case 2:
5385 pf->num_vmdq_vsis = 1;
5386 pf->num_lan_msix = 1;
5387 break;
5388 case 3:
5389 pf->num_vmdq_vsis = 1;
5390 pf->num_lan_msix = 2;
5391 break;
5392 default:
5393 pf->num_lan_msix = min_t(int, (vec / 2),
5394 pf->num_lan_qps);
5395 pf->num_vmdq_vsis = min_t(int, (vec - pf->num_lan_msix),
5396 I40E_DEFAULT_NUM_VMDQ_VSI);
5397 break;
5398 }
5399 }
5400
5401 return err;
5402}
5403
493fb300
AD
5404/**
5405 * i40e_alloc_q_vector - Allocate memory for a single interrupt vector
5406 * @vsi: the VSI being configured
5407 * @v_idx: index of the vector in the vsi struct
5408 *
5409 * We allocate one q_vector. If allocation fails we return -ENOMEM.
5410 **/
5411static int i40e_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
5412{
5413 struct i40e_q_vector *q_vector;
5414
5415 /* allocate q_vector */
5416 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
5417 if (!q_vector)
5418 return -ENOMEM;
5419
5420 q_vector->vsi = vsi;
5421 q_vector->v_idx = v_idx;
5422 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
5423 if (vsi->netdev)
5424 netif_napi_add(vsi->netdev, &q_vector->napi,
5425 i40e_napi_poll, vsi->work_limit);
5426
cd0b6fa6
AD
5427 q_vector->rx.latency_range = I40E_LOW_LATENCY;
5428 q_vector->tx.latency_range = I40E_LOW_LATENCY;
5429
493fb300
AD
5430 /* tie q_vector and vsi together */
5431 vsi->q_vectors[v_idx] = q_vector;
5432
5433 return 0;
5434}
5435
41c445ff
JB
5436/**
5437 * i40e_alloc_q_vectors - Allocate memory for interrupt vectors
5438 * @vsi: the VSI being configured
5439 *
5440 * We allocate one q_vector per queue interrupt. If allocation fails we
5441 * return -ENOMEM.
5442 **/
5443static int i40e_alloc_q_vectors(struct i40e_vsi *vsi)
5444{
5445 struct i40e_pf *pf = vsi->back;
5446 int v_idx, num_q_vectors;
493fb300 5447 int err;
41c445ff
JB
5448
5449 /* if not MSIX, give the one vector only to the LAN VSI */
5450 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5451 num_q_vectors = vsi->num_q_vectors;
5452 else if (vsi == pf->vsi[pf->lan_vsi])
5453 num_q_vectors = 1;
5454 else
5455 return -EINVAL;
5456
41c445ff 5457 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
493fb300
AD
5458 err = i40e_alloc_q_vector(vsi, v_idx);
5459 if (err)
5460 goto err_out;
41c445ff
JB
5461 }
5462
5463 return 0;
493fb300
AD
5464
5465err_out:
5466 while (v_idx--)
5467 i40e_free_q_vector(vsi, v_idx);
5468
5469 return err;
41c445ff
JB
5470}
5471
5472/**
5473 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
5474 * @pf: board private structure to initialize
5475 **/
5476static void i40e_init_interrupt_scheme(struct i40e_pf *pf)
5477{
5478 int err = 0;
5479
5480 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
5481 err = i40e_init_msix(pf);
5482 if (err) {
958a3e3b
SN
5483 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
5484 I40E_FLAG_RSS_ENABLED |
41c445ff
JB
5485 I40E_FLAG_MQ_ENABLED |
5486 I40E_FLAG_DCB_ENABLED |
5487 I40E_FLAG_SRIOV_ENABLED |
5488 I40E_FLAG_FDIR_ENABLED |
5489 I40E_FLAG_FDIR_ATR_ENABLED |
5490 I40E_FLAG_VMDQ_ENABLED);
5491
5492 /* rework the queue expectations without MSIX */
5493 i40e_determine_queue_usage(pf);
5494 }
5495 }
5496
5497 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
5498 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
958a3e3b 5499 dev_info(&pf->pdev->dev, "MSIX not available, trying MSI\n");
41c445ff
JB
5500 err = pci_enable_msi(pf->pdev);
5501 if (err) {
958a3e3b 5502 dev_info(&pf->pdev->dev, "MSI init failed - %d\n", err);
41c445ff
JB
5503 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
5504 }
5505 }
5506
958a3e3b
SN
5507 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
5508 dev_info(&pf->pdev->dev, "MSIX and MSI not available, falling back to Legacy IRQ\n");
5509
41c445ff
JB
5510 /* track first vector for misc interrupts */
5511 err = i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT-1);
5512}
5513
5514/**
5515 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
5516 * @pf: board private structure
5517 *
5518 * This sets up the handler for MSIX 0, which is used to manage the
5519 * non-queue interrupts, e.g. AdminQ and errors. This is not used
5520 * when in MSI or Legacy interrupt mode.
5521 **/
5522static int i40e_setup_misc_vector(struct i40e_pf *pf)
5523{
5524 struct i40e_hw *hw = &pf->hw;
5525 int err = 0;
5526
5527 /* Only request the irq if this is the first time through, and
5528 * not when we're rebuilding after a Reset
5529 */
5530 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
5531 err = request_irq(pf->msix_entries[0].vector,
5532 i40e_intr, 0, pf->misc_int_name, pf);
5533 if (err) {
5534 dev_info(&pf->pdev->dev,
5535 "request_irq for msix_misc failed: %d\n", err);
5536 return -EFAULT;
5537 }
5538 }
5539
5540 i40e_enable_misc_int_causes(hw);
5541
5542 /* associate no queues to the misc vector */
5543 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
5544 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
5545
5546 i40e_flush(hw);
5547
5548 i40e_irq_dynamic_enable_icr0(pf);
5549
5550 return err;
5551}
5552
5553/**
5554 * i40e_config_rss - Prepare for RSS if used
5555 * @pf: board private structure
5556 **/
5557static int i40e_config_rss(struct i40e_pf *pf)
5558{
5559 struct i40e_hw *hw = &pf->hw;
5560 u32 lut = 0;
5561 int i, j;
5562 u64 hena;
5563 /* Set of random keys generated using kernel random number generator */
5564 static const u32 seed[I40E_PFQF_HKEY_MAX_INDEX + 1] = {0x41b01687,
5565 0x183cfd8c, 0xce880440, 0x580cbc3c, 0x35897377,
5566 0x328b25e1, 0x4fa98922, 0xb7d90c14, 0xd5bad70d,
5567 0xcd15a2c1, 0xe8580225, 0x4a1e9d11, 0xfe5731be};
5568
5569 /* Fill out hash function seed */
5570 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
5571 wr32(hw, I40E_PFQF_HKEY(i), seed[i]);
5572
5573 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
5574 hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
5575 ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
5576 hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_UDP) |
5577 ((u64)1 << I40E_FILTER_PCTYPE_NONF_UNICAST_IPV4_UDP) |
5578 ((u64)1 << I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV4_UDP) |
5579 ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_TCP) |
5580 ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_TCP) |
5581 ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_UDP) |
5582 ((u64)1 << I40E_FILTER_PCTYPE_NONF_UNICAST_IPV6_UDP) |
5583 ((u64)1 << I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV6_UDP) |
5584 ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV4)|
5585 ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV6);
5586 wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
5587 wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
5588
5589 /* Populate the LUT with max no. of queues in round robin fashion */
5590 for (i = 0, j = 0; i < pf->hw.func_caps.rss_table_size; i++, j++) {
5591
5592 /* The assumption is that lan qp count will be the highest
5593 * qp count for any PF VSI that needs RSS.
5594 * If multiple VSIs need RSS support, all the qp counts
5595 * for those VSIs should be a power of 2 for RSS to work.
5596 * If LAN VSI is the only consumer for RSS then this requirement
5597 * is not necessary.
5598 */
5599 if (j == pf->rss_size)
5600 j = 0;
5601 /* lut = 4-byte sliding window of 4 lut entries */
5602 lut = (lut << 8) | (j &
5603 ((0x1 << pf->hw.func_caps.rss_table_entry_width) - 1));
5604 /* On i = 3, we have 4 entries in lut; write to the register */
5605 if ((i & 3) == 3)
5606 wr32(hw, I40E_PFQF_HLUT(i >> 2), lut);
5607 }
5608 i40e_flush(hw);
5609
5610 return 0;
5611}
5612
5613/**
5614 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
5615 * @pf: board private structure to initialize
5616 *
5617 * i40e_sw_init initializes the Adapter private data structure.
5618 * Fields are initialized based on PCI device information and
5619 * OS network device settings (MTU size).
5620 **/
5621static int i40e_sw_init(struct i40e_pf *pf)
5622{
5623 int err = 0;
5624 int size;
5625
5626 pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
5627 (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
2759997b 5628 pf->hw.debug_mask = pf->msg_enable | I40E_DEBUG_DIAG;
41c445ff
JB
5629 if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
5630 if (I40E_DEBUG_USER & debug)
5631 pf->hw.debug_mask = debug;
5632 pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
5633 I40E_DEFAULT_MSG_ENABLE);
5634 }
5635
5636 /* Set default capability flags */
5637 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
5638 I40E_FLAG_MSI_ENABLED |
5639 I40E_FLAG_MSIX_ENABLED |
5640 I40E_FLAG_RX_PS_ENABLED |
5641 I40E_FLAG_MQ_ENABLED |
5642 I40E_FLAG_RX_1BUF_ENABLED;
5643
7134f9ce
JB
5644 /* Depending on PF configurations, it is possible that the RSS
5645 * maximum might end up larger than the available queues
5646 */
41c445ff 5647 pf->rss_size_max = 0x1 << pf->hw.func_caps.rss_table_entry_width;
7134f9ce
JB
5648 pf->rss_size_max = min_t(int, pf->rss_size_max,
5649 pf->hw.func_caps.num_tx_qp);
41c445ff
JB
5650 if (pf->hw.func_caps.rss) {
5651 pf->flags |= I40E_FLAG_RSS_ENABLED;
5652 pf->rss_size = min_t(int, pf->rss_size_max,
5653 nr_cpus_node(numa_node_id()));
5654 } else {
5655 pf->rss_size = 1;
5656 }
5657
5658 if (pf->hw.func_caps.dcb)
5659 pf->num_tc_qps = I40E_DEFAULT_QUEUES_PER_TC;
5660 else
5661 pf->num_tc_qps = 0;
5662
5663 if (pf->hw.func_caps.fd) {
5664 /* FW/NVM is not yet fixed in this regard */
5665 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
5666 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
5667 pf->flags |= I40E_FLAG_FDIR_ATR_ENABLED;
5668 dev_info(&pf->pdev->dev,
5669 "Flow Director ATR mode Enabled\n");
5670 pf->flags |= I40E_FLAG_FDIR_ENABLED;
5671 dev_info(&pf->pdev->dev,
5672 "Flow Director Side Band mode Enabled\n");
5673 pf->fdir_pf_filter_count =
5674 pf->hw.func_caps.fd_filters_guaranteed;
5675 }
5676 } else {
5677 pf->fdir_pf_filter_count = 0;
5678 }
5679
5680 if (pf->hw.func_caps.vmdq) {
5681 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
5682 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
5683 pf->num_vmdq_qps = I40E_DEFAULT_QUEUES_PER_VMDQ;
5684 }
5685
5686 /* MFP mode enabled */
5687 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.mfp_mode_1) {
5688 pf->flags |= I40E_FLAG_MFP_ENABLED;
5689 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
5690 }
5691
5692#ifdef CONFIG_PCI_IOV
5693 if (pf->hw.func_caps.num_vfs) {
5694 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
5695 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
5696 pf->num_req_vfs = min_t(int,
5697 pf->hw.func_caps.num_vfs,
5698 I40E_MAX_VF_COUNT);
5699 }
5700#endif /* CONFIG_PCI_IOV */
5701 pf->eeprom_version = 0xDEAD;
5702 pf->lan_veb = I40E_NO_VEB;
5703 pf->lan_vsi = I40E_NO_VSI;
5704
5705 /* set up queue assignment tracking */
5706 size = sizeof(struct i40e_lump_tracking)
5707 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
5708 pf->qp_pile = kzalloc(size, GFP_KERNEL);
5709 if (!pf->qp_pile) {
5710 err = -ENOMEM;
5711 goto sw_init_done;
5712 }
5713 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
5714 pf->qp_pile->search_hint = 0;
5715
5716 /* set up vector assignment tracking */
5717 size = sizeof(struct i40e_lump_tracking)
5718 + (sizeof(u16) * pf->hw.func_caps.num_msix_vectors);
5719 pf->irq_pile = kzalloc(size, GFP_KERNEL);
5720 if (!pf->irq_pile) {
5721 kfree(pf->qp_pile);
5722 err = -ENOMEM;
5723 goto sw_init_done;
5724 }
5725 pf->irq_pile->num_entries = pf->hw.func_caps.num_msix_vectors;
5726 pf->irq_pile->search_hint = 0;
5727
5728 mutex_init(&pf->switch_mutex);
5729
5730sw_init_done:
5731 return err;
5732}
5733
5734/**
5735 * i40e_set_features - set the netdev feature flags
5736 * @netdev: ptr to the netdev being adjusted
5737 * @features: the feature set that the stack is suggesting
5738 **/
5739static int i40e_set_features(struct net_device *netdev,
5740 netdev_features_t features)
5741{
5742 struct i40e_netdev_priv *np = netdev_priv(netdev);
5743 struct i40e_vsi *vsi = np->vsi;
5744
5745 if (features & NETIF_F_HW_VLAN_CTAG_RX)
5746 i40e_vlan_stripping_enable(vsi);
5747 else
5748 i40e_vlan_stripping_disable(vsi);
5749
5750 return 0;
5751}
5752
5753static const struct net_device_ops i40e_netdev_ops = {
5754 .ndo_open = i40e_open,
5755 .ndo_stop = i40e_close,
5756 .ndo_start_xmit = i40e_lan_xmit_frame,
5757 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
5758 .ndo_set_rx_mode = i40e_set_rx_mode,
5759 .ndo_validate_addr = eth_validate_addr,
5760 .ndo_set_mac_address = i40e_set_mac,
5761 .ndo_change_mtu = i40e_change_mtu,
5762 .ndo_tx_timeout = i40e_tx_timeout,
5763 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
5764 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
5765#ifdef CONFIG_NET_POLL_CONTROLLER
5766 .ndo_poll_controller = i40e_netpoll,
5767#endif
5768 .ndo_setup_tc = i40e_setup_tc,
5769 .ndo_set_features = i40e_set_features,
5770 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
5771 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
5772 .ndo_set_vf_tx_rate = i40e_ndo_set_vf_bw,
5773 .ndo_get_vf_config = i40e_ndo_get_vf_config,
5774};
5775
5776/**
5777 * i40e_config_netdev - Setup the netdev flags
5778 * @vsi: the VSI being configured
5779 *
5780 * Returns 0 on success, negative value on failure
5781 **/
5782static int i40e_config_netdev(struct i40e_vsi *vsi)
5783{
5784 struct i40e_pf *pf = vsi->back;
5785 struct i40e_hw *hw = &pf->hw;
5786 struct i40e_netdev_priv *np;
5787 struct net_device *netdev;
5788 u8 mac_addr[ETH_ALEN];
5789 int etherdev_size;
5790
5791 etherdev_size = sizeof(struct i40e_netdev_priv);
5792 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
5793 if (!netdev)
5794 return -ENOMEM;
5795
5796 vsi->netdev = netdev;
5797 np = netdev_priv(netdev);
5798 np->vsi = vsi;
5799
5800 netdev->hw_enc_features = NETIF_F_IP_CSUM |
5801 NETIF_F_GSO_UDP_TUNNEL |
5802 NETIF_F_TSO |
5803 NETIF_F_SG;
5804
5805 netdev->features = NETIF_F_SG |
5806 NETIF_F_IP_CSUM |
5807 NETIF_F_SCTP_CSUM |
5808 NETIF_F_HIGHDMA |
5809 NETIF_F_GSO_UDP_TUNNEL |
5810 NETIF_F_HW_VLAN_CTAG_TX |
5811 NETIF_F_HW_VLAN_CTAG_RX |
5812 NETIF_F_HW_VLAN_CTAG_FILTER |
5813 NETIF_F_IPV6_CSUM |
5814 NETIF_F_TSO |
5815 NETIF_F_TSO6 |
5816 NETIF_F_RXCSUM |
5817 NETIF_F_RXHASH |
5818 0;
5819
5820 /* copy netdev features into list of user selectable features */
5821 netdev->hw_features |= netdev->features;
5822
5823 if (vsi->type == I40E_VSI_MAIN) {
5824 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
5825 memcpy(mac_addr, hw->mac.perm_addr, ETH_ALEN);
5826 } else {
5827 /* relate the VSI_VMDQ name to the VSI_MAIN name */
5828 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
5829 pf->vsi[pf->lan_vsi]->netdev->name);
5830 random_ether_addr(mac_addr);
5831 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
5832 }
5833
5834 memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
5835 memcpy(netdev->perm_addr, mac_addr, ETH_ALEN);
5836 /* vlan gets same features (except vlan offload)
5837 * after any tweaks for specific VSI types
5838 */
5839 netdev->vlan_features = netdev->features & ~(NETIF_F_HW_VLAN_CTAG_TX |
5840 NETIF_F_HW_VLAN_CTAG_RX |
5841 NETIF_F_HW_VLAN_CTAG_FILTER);
5842 netdev->priv_flags |= IFF_UNICAST_FLT;
5843 netdev->priv_flags |= IFF_SUPP_NOFCS;
5844 /* Setup netdev TC information */
5845 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
5846
5847 netdev->netdev_ops = &i40e_netdev_ops;
5848 netdev->watchdog_timeo = 5 * HZ;
5849 i40e_set_ethtool_ops(netdev);
5850
5851 return 0;
5852}
5853
5854/**
5855 * i40e_vsi_delete - Delete a VSI from the switch
5856 * @vsi: the VSI being removed
5857 *
5858 * Returns 0 on success, negative value on failure
5859 **/
5860static void i40e_vsi_delete(struct i40e_vsi *vsi)
5861{
5862 /* remove default VSI is not allowed */
5863 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
5864 return;
5865
5866 /* there is no HW VSI for FDIR */
5867 if (vsi->type == I40E_VSI_FDIR)
5868 return;
5869
5870 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
5871 return;
5872}
5873
5874/**
5875 * i40e_add_vsi - Add a VSI to the switch
5876 * @vsi: the VSI being configured
5877 *
5878 * This initializes a VSI context depending on the VSI type to be added and
5879 * passes it down to the add_vsi aq command.
5880 **/
5881static int i40e_add_vsi(struct i40e_vsi *vsi)
5882{
5883 int ret = -ENODEV;
5884 struct i40e_mac_filter *f, *ftmp;
5885 struct i40e_pf *pf = vsi->back;
5886 struct i40e_hw *hw = &pf->hw;
5887 struct i40e_vsi_context ctxt;
5888 u8 enabled_tc = 0x1; /* TC0 enabled */
5889 int f_count = 0;
5890
5891 memset(&ctxt, 0, sizeof(ctxt));
5892 switch (vsi->type) {
5893 case I40E_VSI_MAIN:
5894 /* The PF's main VSI is already setup as part of the
5895 * device initialization, so we'll not bother with
5896 * the add_vsi call, but we will retrieve the current
5897 * VSI context.
5898 */
5899 ctxt.seid = pf->main_vsi_seid;
5900 ctxt.pf_num = pf->hw.pf_id;
5901 ctxt.vf_num = 0;
5902 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
5903 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
5904 if (ret) {
5905 dev_info(&pf->pdev->dev,
5906 "couldn't get pf vsi config, err %d, aq_err %d\n",
5907 ret, pf->hw.aq.asq_last_status);
5908 return -ENOENT;
5909 }
5910 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
5911 vsi->info.valid_sections = 0;
5912
5913 vsi->seid = ctxt.seid;
5914 vsi->id = ctxt.vsi_number;
5915
5916 enabled_tc = i40e_pf_get_tc_map(pf);
5917
5918 /* MFP mode setup queue map and update VSI */
5919 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5920 memset(&ctxt, 0, sizeof(ctxt));
5921 ctxt.seid = pf->main_vsi_seid;
5922 ctxt.pf_num = pf->hw.pf_id;
5923 ctxt.vf_num = 0;
5924 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5925 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5926 if (ret) {
5927 dev_info(&pf->pdev->dev,
5928 "update vsi failed, aq_err=%d\n",
5929 pf->hw.aq.asq_last_status);
5930 ret = -ENOENT;
5931 goto err;
5932 }
5933 /* update the local VSI info queue map */
5934 i40e_vsi_update_queue_map(vsi, &ctxt);
5935 vsi->info.valid_sections = 0;
5936 } else {
5937 /* Default/Main VSI is only enabled for TC0
5938 * reconfigure it to enable all TCs that are
5939 * available on the port in SFP mode.
5940 */
5941 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5942 if (ret) {
5943 dev_info(&pf->pdev->dev,
5944 "failed to configure TCs for main VSI tc_map 0x%08x, err %d, aq_err %d\n",
5945 enabled_tc, ret,
5946 pf->hw.aq.asq_last_status);
5947 ret = -ENOENT;
5948 }
5949 }
5950 break;
5951
5952 case I40E_VSI_FDIR:
5953 /* no queue mapping or actual HW VSI needed */
5954 vsi->info.valid_sections = 0;
5955 vsi->seid = 0;
5956 vsi->id = 0;
5957 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
5958 return 0;
5959 break;
5960
5961 case I40E_VSI_VMDQ2:
5962 ctxt.pf_num = hw->pf_id;
5963 ctxt.vf_num = 0;
5964 ctxt.uplink_seid = vsi->uplink_seid;
5965 ctxt.connection_type = 0x1; /* regular data port */
5966 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
5967
5968 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
5969
5970 /* This VSI is connected to VEB so the switch_id
5971 * should be set to zero by default.
5972 */
5973 ctxt.info.switch_id = 0;
5974 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
5975 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
5976
5977 /* Setup the VSI tx/rx queue map for TC0 only for now */
5978 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
5979 break;
5980
5981 case I40E_VSI_SRIOV:
5982 ctxt.pf_num = hw->pf_id;
5983 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
5984 ctxt.uplink_seid = vsi->uplink_seid;
5985 ctxt.connection_type = 0x1; /* regular data port */
5986 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5987
5988 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
5989
5990 /* This VSI is connected to VEB so the switch_id
5991 * should be set to zero by default.
5992 */
5993 ctxt.info.switch_id = cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
5994
5995 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
5996 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
5997 /* Setup the VSI tx/rx queue map for TC0 only for now */
5998 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
5999 break;
6000
6001 default:
6002 return -ENODEV;
6003 }
6004
6005 if (vsi->type != I40E_VSI_MAIN) {
6006 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6007 if (ret) {
6008 dev_info(&vsi->back->pdev->dev,
6009 "add vsi failed, aq_err=%d\n",
6010 vsi->back->hw.aq.asq_last_status);
6011 ret = -ENOENT;
6012 goto err;
6013 }
6014 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
6015 vsi->info.valid_sections = 0;
6016 vsi->seid = ctxt.seid;
6017 vsi->id = ctxt.vsi_number;
6018 }
6019
6020 /* If macvlan filters already exist, force them to get loaded */
6021 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
6022 f->changed = true;
6023 f_count++;
6024 }
6025 if (f_count) {
6026 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
6027 pf->flags |= I40E_FLAG_FILTER_SYNC;
6028 }
6029
6030 /* Update VSI BW information */
6031 ret = i40e_vsi_get_bw_info(vsi);
6032 if (ret) {
6033 dev_info(&pf->pdev->dev,
6034 "couldn't get vsi bw info, err %d, aq_err %d\n",
6035 ret, pf->hw.aq.asq_last_status);
6036 /* VSI is already added so not tearing that up */
6037 ret = 0;
6038 }
6039
6040err:
6041 return ret;
6042}
6043
6044/**
6045 * i40e_vsi_release - Delete a VSI and free its resources
6046 * @vsi: the VSI being removed
6047 *
6048 * Returns 0 on success or < 0 on error
6049 **/
6050int i40e_vsi_release(struct i40e_vsi *vsi)
6051{
6052 struct i40e_mac_filter *f, *ftmp;
6053 struct i40e_veb *veb = NULL;
6054 struct i40e_pf *pf;
6055 u16 uplink_seid;
6056 int i, n;
6057
6058 pf = vsi->back;
6059
6060 /* release of a VEB-owner or last VSI is not allowed */
6061 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
6062 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
6063 vsi->seid, vsi->uplink_seid);
6064 return -ENODEV;
6065 }
6066 if (vsi == pf->vsi[pf->lan_vsi] &&
6067 !test_bit(__I40E_DOWN, &pf->state)) {
6068 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
6069 return -ENODEV;
6070 }
6071
6072 uplink_seid = vsi->uplink_seid;
6073 if (vsi->type != I40E_VSI_SRIOV) {
6074 if (vsi->netdev_registered) {
6075 vsi->netdev_registered = false;
6076 if (vsi->netdev) {
6077 /* results in a call to i40e_close() */
6078 unregister_netdev(vsi->netdev);
6079 free_netdev(vsi->netdev);
6080 vsi->netdev = NULL;
6081 }
6082 } else {
6083 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
6084 i40e_down(vsi);
6085 i40e_vsi_free_irq(vsi);
6086 i40e_vsi_free_tx_resources(vsi);
6087 i40e_vsi_free_rx_resources(vsi);
6088 }
6089 i40e_vsi_disable_irq(vsi);
6090 }
6091
6092 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
6093 i40e_del_filter(vsi, f->macaddr, f->vlan,
6094 f->is_vf, f->is_netdev);
6095 i40e_sync_vsi_filters(vsi);
6096
6097 i40e_vsi_delete(vsi);
6098 i40e_vsi_free_q_vectors(vsi);
6099 i40e_vsi_clear_rings(vsi);
6100 i40e_vsi_clear(vsi);
6101
6102 /* If this was the last thing on the VEB, except for the
6103 * controlling VSI, remove the VEB, which puts the controlling
6104 * VSI onto the next level down in the switch.
6105 *
6106 * Well, okay, there's one more exception here: don't remove
6107 * the orphan VEBs yet. We'll wait for an explicit remove request
6108 * from up the network stack.
6109 */
6110 for (n = 0, i = 0; i < pf->hw.func_caps.num_vsis; i++) {
6111 if (pf->vsi[i] &&
6112 pf->vsi[i]->uplink_seid == uplink_seid &&
6113 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
6114 n++; /* count the VSIs */
6115 }
6116 }
6117 for (i = 0; i < I40E_MAX_VEB; i++) {
6118 if (!pf->veb[i])
6119 continue;
6120 if (pf->veb[i]->uplink_seid == uplink_seid)
6121 n++; /* count the VEBs */
6122 if (pf->veb[i]->seid == uplink_seid)
6123 veb = pf->veb[i];
6124 }
6125 if (n == 0 && veb && veb->uplink_seid != 0)
6126 i40e_veb_release(veb);
6127
6128 return 0;
6129}
6130
6131/**
6132 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
6133 * @vsi: ptr to the VSI
6134 *
6135 * This should only be called after i40e_vsi_mem_alloc() which allocates the
6136 * corresponding SW VSI structure and initializes num_queue_pairs for the
6137 * newly allocated VSI.
6138 *
6139 * Returns 0 on success or negative on failure
6140 **/
6141static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
6142{
6143 int ret = -ENOENT;
6144 struct i40e_pf *pf = vsi->back;
6145
493fb300 6146 if (vsi->q_vectors[0]) {
41c445ff
JB
6147 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
6148 vsi->seid);
6149 return -EEXIST;
6150 }
6151
6152 if (vsi->base_vector) {
6153 dev_info(&pf->pdev->dev,
6154 "VSI %d has non-zero base vector %d\n",
6155 vsi->seid, vsi->base_vector);
6156 return -EEXIST;
6157 }
6158
6159 ret = i40e_alloc_q_vectors(vsi);
6160 if (ret) {
6161 dev_info(&pf->pdev->dev,
6162 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
6163 vsi->num_q_vectors, vsi->seid, ret);
6164 vsi->num_q_vectors = 0;
6165 goto vector_setup_out;
6166 }
6167
958a3e3b
SN
6168 if (vsi->num_q_vectors)
6169 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
6170 vsi->num_q_vectors, vsi->idx);
41c445ff
JB
6171 if (vsi->base_vector < 0) {
6172 dev_info(&pf->pdev->dev,
6173 "failed to get q tracking for VSI %d, err=%d\n",
6174 vsi->seid, vsi->base_vector);
6175 i40e_vsi_free_q_vectors(vsi);
6176 ret = -ENOENT;
6177 goto vector_setup_out;
6178 }
6179
6180vector_setup_out:
6181 return ret;
6182}
6183
6184/**
6185 * i40e_vsi_setup - Set up a VSI by a given type
6186 * @pf: board private structure
6187 * @type: VSI type
6188 * @uplink_seid: the switch element to link to
6189 * @param1: usage depends upon VSI type. For VF types, indicates VF id
6190 *
6191 * This allocates the sw VSI structure and its queue resources, then add a VSI
6192 * to the identified VEB.
6193 *
6194 * Returns pointer to the successfully allocated and configure VSI sw struct on
6195 * success, otherwise returns NULL on failure.
6196 **/
6197struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
6198 u16 uplink_seid, u32 param1)
6199{
6200 struct i40e_vsi *vsi = NULL;
6201 struct i40e_veb *veb = NULL;
6202 int ret, i;
6203 int v_idx;
6204
6205 /* The requested uplink_seid must be either
6206 * - the PF's port seid
6207 * no VEB is needed because this is the PF
6208 * or this is a Flow Director special case VSI
6209 * - seid of an existing VEB
6210 * - seid of a VSI that owns an existing VEB
6211 * - seid of a VSI that doesn't own a VEB
6212 * a new VEB is created and the VSI becomes the owner
6213 * - seid of the PF VSI, which is what creates the first VEB
6214 * this is a special case of the previous
6215 *
6216 * Find which uplink_seid we were given and create a new VEB if needed
6217 */
6218 for (i = 0; i < I40E_MAX_VEB; i++) {
6219 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
6220 veb = pf->veb[i];
6221 break;
6222 }
6223 }
6224
6225 if (!veb && uplink_seid != pf->mac_seid) {
6226
6227 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
6228 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
6229 vsi = pf->vsi[i];
6230 break;
6231 }
6232 }
6233 if (!vsi) {
6234 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
6235 uplink_seid);
6236 return NULL;
6237 }
6238
6239 if (vsi->uplink_seid == pf->mac_seid)
6240 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
6241 vsi->tc_config.enabled_tc);
6242 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
6243 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
6244 vsi->tc_config.enabled_tc);
6245
6246 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
6247 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
6248 veb = pf->veb[i];
6249 }
6250 if (!veb) {
6251 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
6252 return NULL;
6253 }
6254
6255 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
6256 uplink_seid = veb->seid;
6257 }
6258
6259 /* get vsi sw struct */
6260 v_idx = i40e_vsi_mem_alloc(pf, type);
6261 if (v_idx < 0)
6262 goto err_alloc;
6263 vsi = pf->vsi[v_idx];
6264 vsi->type = type;
6265 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
6266
6267 if (type == I40E_VSI_MAIN)
6268 pf->lan_vsi = v_idx;
6269 else if (type == I40E_VSI_SRIOV)
6270 vsi->vf_id = param1;
6271 /* assign it some queues */
6272 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
6273 if (ret < 0) {
6274 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
6275 vsi->seid, ret);
6276 goto err_vsi;
6277 }
6278 vsi->base_queue = ret;
6279
6280 /* get a VSI from the hardware */
6281 vsi->uplink_seid = uplink_seid;
6282 ret = i40e_add_vsi(vsi);
6283 if (ret)
6284 goto err_vsi;
6285
6286 switch (vsi->type) {
6287 /* setup the netdev if needed */
6288 case I40E_VSI_MAIN:
6289 case I40E_VSI_VMDQ2:
6290 ret = i40e_config_netdev(vsi);
6291 if (ret)
6292 goto err_netdev;
6293 ret = register_netdev(vsi->netdev);
6294 if (ret)
6295 goto err_netdev;
6296 vsi->netdev_registered = true;
6297 netif_carrier_off(vsi->netdev);
6298 /* fall through */
6299
6300 case I40E_VSI_FDIR:
6301 /* set up vectors and rings if needed */
6302 ret = i40e_vsi_setup_vectors(vsi);
6303 if (ret)
6304 goto err_msix;
6305
6306 ret = i40e_alloc_rings(vsi);
6307 if (ret)
6308 goto err_rings;
6309
6310 /* map all of the rings to the q_vectors */
6311 i40e_vsi_map_rings_to_vectors(vsi);
6312
6313 i40e_vsi_reset_stats(vsi);
6314 break;
6315
6316 default:
6317 /* no netdev or rings for the other VSI types */
6318 break;
6319 }
6320
6321 return vsi;
6322
6323err_rings:
6324 i40e_vsi_free_q_vectors(vsi);
6325err_msix:
6326 if (vsi->netdev_registered) {
6327 vsi->netdev_registered = false;
6328 unregister_netdev(vsi->netdev);
6329 free_netdev(vsi->netdev);
6330 vsi->netdev = NULL;
6331 }
6332err_netdev:
6333 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
6334err_vsi:
6335 i40e_vsi_clear(vsi);
6336err_alloc:
6337 return NULL;
6338}
6339
6340/**
6341 * i40e_veb_get_bw_info - Query VEB BW information
6342 * @veb: the veb to query
6343 *
6344 * Query the Tx scheduler BW configuration data for given VEB
6345 **/
6346static int i40e_veb_get_bw_info(struct i40e_veb *veb)
6347{
6348 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
6349 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
6350 struct i40e_pf *pf = veb->pf;
6351 struct i40e_hw *hw = &pf->hw;
6352 u32 tc_bw_max;
6353 int ret = 0;
6354 int i;
6355
6356 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
6357 &bw_data, NULL);
6358 if (ret) {
6359 dev_info(&pf->pdev->dev,
6360 "query veb bw config failed, aq_err=%d\n",
6361 hw->aq.asq_last_status);
6362 goto out;
6363 }
6364
6365 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
6366 &ets_data, NULL);
6367 if (ret) {
6368 dev_info(&pf->pdev->dev,
6369 "query veb bw ets config failed, aq_err=%d\n",
6370 hw->aq.asq_last_status);
6371 goto out;
6372 }
6373
6374 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
6375 veb->bw_max_quanta = ets_data.tc_bw_max;
6376 veb->is_abs_credits = bw_data.absolute_credits_enable;
6377 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
6378 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
6379 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6380 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
6381 veb->bw_tc_limit_credits[i] =
6382 le16_to_cpu(bw_data.tc_bw_limits[i]);
6383 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
6384 }
6385
6386out:
6387 return ret;
6388}
6389
6390/**
6391 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
6392 * @pf: board private structure
6393 *
6394 * On error: returns error code (negative)
6395 * On success: returns vsi index in PF (positive)
6396 **/
6397static int i40e_veb_mem_alloc(struct i40e_pf *pf)
6398{
6399 int ret = -ENOENT;
6400 struct i40e_veb *veb;
6401 int i;
6402
6403 /* Need to protect the allocation of switch elements at the PF level */
6404 mutex_lock(&pf->switch_mutex);
6405
6406 /* VEB list may be fragmented if VEB creation/destruction has
6407 * been happening. We can afford to do a quick scan to look
6408 * for any free slots in the list.
6409 *
6410 * find next empty veb slot, looping back around if necessary
6411 */
6412 i = 0;
6413 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
6414 i++;
6415 if (i >= I40E_MAX_VEB) {
6416 ret = -ENOMEM;
6417 goto err_alloc_veb; /* out of VEB slots! */
6418 }
6419
6420 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
6421 if (!veb) {
6422 ret = -ENOMEM;
6423 goto err_alloc_veb;
6424 }
6425 veb->pf = pf;
6426 veb->idx = i;
6427 veb->enabled_tc = 1;
6428
6429 pf->veb[i] = veb;
6430 ret = i;
6431err_alloc_veb:
6432 mutex_unlock(&pf->switch_mutex);
6433 return ret;
6434}
6435
6436/**
6437 * i40e_switch_branch_release - Delete a branch of the switch tree
6438 * @branch: where to start deleting
6439 *
6440 * This uses recursion to find the tips of the branch to be
6441 * removed, deleting until we get back to and can delete this VEB.
6442 **/
6443static void i40e_switch_branch_release(struct i40e_veb *branch)
6444{
6445 struct i40e_pf *pf = branch->pf;
6446 u16 branch_seid = branch->seid;
6447 u16 veb_idx = branch->idx;
6448 int i;
6449
6450 /* release any VEBs on this VEB - RECURSION */
6451 for (i = 0; i < I40E_MAX_VEB; i++) {
6452 if (!pf->veb[i])
6453 continue;
6454 if (pf->veb[i]->uplink_seid == branch->seid)
6455 i40e_switch_branch_release(pf->veb[i]);
6456 }
6457
6458 /* Release the VSIs on this VEB, but not the owner VSI.
6459 *
6460 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
6461 * the VEB itself, so don't use (*branch) after this loop.
6462 */
6463 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
6464 if (!pf->vsi[i])
6465 continue;
6466 if (pf->vsi[i]->uplink_seid == branch_seid &&
6467 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
6468 i40e_vsi_release(pf->vsi[i]);
6469 }
6470 }
6471
6472 /* There's one corner case where the VEB might not have been
6473 * removed, so double check it here and remove it if needed.
6474 * This case happens if the veb was created from the debugfs
6475 * commands and no VSIs were added to it.
6476 */
6477 if (pf->veb[veb_idx])
6478 i40e_veb_release(pf->veb[veb_idx]);
6479}
6480
6481/**
6482 * i40e_veb_clear - remove veb struct
6483 * @veb: the veb to remove
6484 **/
6485static void i40e_veb_clear(struct i40e_veb *veb)
6486{
6487 if (!veb)
6488 return;
6489
6490 if (veb->pf) {
6491 struct i40e_pf *pf = veb->pf;
6492
6493 mutex_lock(&pf->switch_mutex);
6494 if (pf->veb[veb->idx] == veb)
6495 pf->veb[veb->idx] = NULL;
6496 mutex_unlock(&pf->switch_mutex);
6497 }
6498
6499 kfree(veb);
6500}
6501
6502/**
6503 * i40e_veb_release - Delete a VEB and free its resources
6504 * @veb: the VEB being removed
6505 **/
6506void i40e_veb_release(struct i40e_veb *veb)
6507{
6508 struct i40e_vsi *vsi = NULL;
6509 struct i40e_pf *pf;
6510 int i, n = 0;
6511
6512 pf = veb->pf;
6513
6514 /* find the remaining VSI and check for extras */
6515 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
6516 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
6517 n++;
6518 vsi = pf->vsi[i];
6519 }
6520 }
6521 if (n != 1) {
6522 dev_info(&pf->pdev->dev,
6523 "can't remove VEB %d with %d VSIs left\n",
6524 veb->seid, n);
6525 return;
6526 }
6527
6528 /* move the remaining VSI to uplink veb */
6529 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
6530 if (veb->uplink_seid) {
6531 vsi->uplink_seid = veb->uplink_seid;
6532 if (veb->uplink_seid == pf->mac_seid)
6533 vsi->veb_idx = I40E_NO_VEB;
6534 else
6535 vsi->veb_idx = veb->veb_idx;
6536 } else {
6537 /* floating VEB */
6538 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6539 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
6540 }
6541
6542 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
6543 i40e_veb_clear(veb);
6544
6545 return;
6546}
6547
6548/**
6549 * i40e_add_veb - create the VEB in the switch
6550 * @veb: the VEB to be instantiated
6551 * @vsi: the controlling VSI
6552 **/
6553static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
6554{
6555 bool is_default = (vsi->idx == vsi->back->lan_vsi);
e1c51b95 6556 bool is_cloud = false;
41c445ff
JB
6557 int ret;
6558
6559 /* get a VEB from the hardware */
6560 ret = i40e_aq_add_veb(&veb->pf->hw, veb->uplink_seid, vsi->seid,
e1c51b95
KS
6561 veb->enabled_tc, is_default,
6562 is_cloud, &veb->seid, NULL);
41c445ff
JB
6563 if (ret) {
6564 dev_info(&veb->pf->pdev->dev,
6565 "couldn't add VEB, err %d, aq_err %d\n",
6566 ret, veb->pf->hw.aq.asq_last_status);
6567 return -EPERM;
6568 }
6569
6570 /* get statistics counter */
6571 ret = i40e_aq_get_veb_parameters(&veb->pf->hw, veb->seid, NULL, NULL,
6572 &veb->stats_idx, NULL, NULL, NULL);
6573 if (ret) {
6574 dev_info(&veb->pf->pdev->dev,
6575 "couldn't get VEB statistics idx, err %d, aq_err %d\n",
6576 ret, veb->pf->hw.aq.asq_last_status);
6577 return -EPERM;
6578 }
6579 ret = i40e_veb_get_bw_info(veb);
6580 if (ret) {
6581 dev_info(&veb->pf->pdev->dev,
6582 "couldn't get VEB bw info, err %d, aq_err %d\n",
6583 ret, veb->pf->hw.aq.asq_last_status);
6584 i40e_aq_delete_element(&veb->pf->hw, veb->seid, NULL);
6585 return -ENOENT;
6586 }
6587
6588 vsi->uplink_seid = veb->seid;
6589 vsi->veb_idx = veb->idx;
6590 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
6591
6592 return 0;
6593}
6594
6595/**
6596 * i40e_veb_setup - Set up a VEB
6597 * @pf: board private structure
6598 * @flags: VEB setup flags
6599 * @uplink_seid: the switch element to link to
6600 * @vsi_seid: the initial VSI seid
6601 * @enabled_tc: Enabled TC bit-map
6602 *
6603 * This allocates the sw VEB structure and links it into the switch
6604 * It is possible and legal for this to be a duplicate of an already
6605 * existing VEB. It is also possible for both uplink and vsi seids
6606 * to be zero, in order to create a floating VEB.
6607 *
6608 * Returns pointer to the successfully allocated VEB sw struct on
6609 * success, otherwise returns NULL on failure.
6610 **/
6611struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
6612 u16 uplink_seid, u16 vsi_seid,
6613 u8 enabled_tc)
6614{
6615 struct i40e_veb *veb, *uplink_veb = NULL;
6616 int vsi_idx, veb_idx;
6617 int ret;
6618
6619 /* if one seid is 0, the other must be 0 to create a floating relay */
6620 if ((uplink_seid == 0 || vsi_seid == 0) &&
6621 (uplink_seid + vsi_seid != 0)) {
6622 dev_info(&pf->pdev->dev,
6623 "one, not both seid's are 0: uplink=%d vsi=%d\n",
6624 uplink_seid, vsi_seid);
6625 return NULL;
6626 }
6627
6628 /* make sure there is such a vsi and uplink */
6629 for (vsi_idx = 0; vsi_idx < pf->hw.func_caps.num_vsis; vsi_idx++)
6630 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
6631 break;
6632 if (vsi_idx >= pf->hw.func_caps.num_vsis && vsi_seid != 0) {
6633 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
6634 vsi_seid);
6635 return NULL;
6636 }
6637
6638 if (uplink_seid && uplink_seid != pf->mac_seid) {
6639 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6640 if (pf->veb[veb_idx] &&
6641 pf->veb[veb_idx]->seid == uplink_seid) {
6642 uplink_veb = pf->veb[veb_idx];
6643 break;
6644 }
6645 }
6646 if (!uplink_veb) {
6647 dev_info(&pf->pdev->dev,
6648 "uplink seid %d not found\n", uplink_seid);
6649 return NULL;
6650 }
6651 }
6652
6653 /* get veb sw struct */
6654 veb_idx = i40e_veb_mem_alloc(pf);
6655 if (veb_idx < 0)
6656 goto err_alloc;
6657 veb = pf->veb[veb_idx];
6658 veb->flags = flags;
6659 veb->uplink_seid = uplink_seid;
6660 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
6661 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
6662
6663 /* create the VEB in the switch */
6664 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
6665 if (ret)
6666 goto err_veb;
6667
6668 return veb;
6669
6670err_veb:
6671 i40e_veb_clear(veb);
6672err_alloc:
6673 return NULL;
6674}
6675
6676/**
6677 * i40e_setup_pf_switch_element - set pf vars based on switch type
6678 * @pf: board private structure
6679 * @ele: element we are building info from
6680 * @num_reported: total number of elements
6681 * @printconfig: should we print the contents
6682 *
6683 * helper function to assist in extracting a few useful SEID values.
6684 **/
6685static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
6686 struct i40e_aqc_switch_config_element_resp *ele,
6687 u16 num_reported, bool printconfig)
6688{
6689 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
6690 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
6691 u8 element_type = ele->element_type;
6692 u16 seid = le16_to_cpu(ele->seid);
6693
6694 if (printconfig)
6695 dev_info(&pf->pdev->dev,
6696 "type=%d seid=%d uplink=%d downlink=%d\n",
6697 element_type, seid, uplink_seid, downlink_seid);
6698
6699 switch (element_type) {
6700 case I40E_SWITCH_ELEMENT_TYPE_MAC:
6701 pf->mac_seid = seid;
6702 break;
6703 case I40E_SWITCH_ELEMENT_TYPE_VEB:
6704 /* Main VEB? */
6705 if (uplink_seid != pf->mac_seid)
6706 break;
6707 if (pf->lan_veb == I40E_NO_VEB) {
6708 int v;
6709
6710 /* find existing or else empty VEB */
6711 for (v = 0; v < I40E_MAX_VEB; v++) {
6712 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
6713 pf->lan_veb = v;
6714 break;
6715 }
6716 }
6717 if (pf->lan_veb == I40E_NO_VEB) {
6718 v = i40e_veb_mem_alloc(pf);
6719 if (v < 0)
6720 break;
6721 pf->lan_veb = v;
6722 }
6723 }
6724
6725 pf->veb[pf->lan_veb]->seid = seid;
6726 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
6727 pf->veb[pf->lan_veb]->pf = pf;
6728 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
6729 break;
6730 case I40E_SWITCH_ELEMENT_TYPE_VSI:
6731 if (num_reported != 1)
6732 break;
6733 /* This is immediately after a reset so we can assume this is
6734 * the PF's VSI
6735 */
6736 pf->mac_seid = uplink_seid;
6737 pf->pf_seid = downlink_seid;
6738 pf->main_vsi_seid = seid;
6739 if (printconfig)
6740 dev_info(&pf->pdev->dev,
6741 "pf_seid=%d main_vsi_seid=%d\n",
6742 pf->pf_seid, pf->main_vsi_seid);
6743 break;
6744 case I40E_SWITCH_ELEMENT_TYPE_PF:
6745 case I40E_SWITCH_ELEMENT_TYPE_VF:
6746 case I40E_SWITCH_ELEMENT_TYPE_EMP:
6747 case I40E_SWITCH_ELEMENT_TYPE_BMC:
6748 case I40E_SWITCH_ELEMENT_TYPE_PE:
6749 case I40E_SWITCH_ELEMENT_TYPE_PA:
6750 /* ignore these for now */
6751 break;
6752 default:
6753 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
6754 element_type, seid);
6755 break;
6756 }
6757}
6758
6759/**
6760 * i40e_fetch_switch_configuration - Get switch config from firmware
6761 * @pf: board private structure
6762 * @printconfig: should we print the contents
6763 *
6764 * Get the current switch configuration from the device and
6765 * extract a few useful SEID values.
6766 **/
6767int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
6768{
6769 struct i40e_aqc_get_switch_config_resp *sw_config;
6770 u16 next_seid = 0;
6771 int ret = 0;
6772 u8 *aq_buf;
6773 int i;
6774
6775 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
6776 if (!aq_buf)
6777 return -ENOMEM;
6778
6779 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
6780 do {
6781 u16 num_reported, num_total;
6782
6783 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
6784 I40E_AQ_LARGE_BUF,
6785 &next_seid, NULL);
6786 if (ret) {
6787 dev_info(&pf->pdev->dev,
6788 "get switch config failed %d aq_err=%x\n",
6789 ret, pf->hw.aq.asq_last_status);
6790 kfree(aq_buf);
6791 return -ENOENT;
6792 }
6793
6794 num_reported = le16_to_cpu(sw_config->header.num_reported);
6795 num_total = le16_to_cpu(sw_config->header.num_total);
6796
6797 if (printconfig)
6798 dev_info(&pf->pdev->dev,
6799 "header: %d reported %d total\n",
6800 num_reported, num_total);
6801
6802 if (num_reported) {
6803 int sz = sizeof(*sw_config) * num_reported;
6804
6805 kfree(pf->sw_config);
6806 pf->sw_config = kzalloc(sz, GFP_KERNEL);
6807 if (pf->sw_config)
6808 memcpy(pf->sw_config, sw_config, sz);
6809 }
6810
6811 for (i = 0; i < num_reported; i++) {
6812 struct i40e_aqc_switch_config_element_resp *ele =
6813 &sw_config->element[i];
6814
6815 i40e_setup_pf_switch_element(pf, ele, num_reported,
6816 printconfig);
6817 }
6818 } while (next_seid != 0);
6819
6820 kfree(aq_buf);
6821 return ret;
6822}
6823
6824/**
6825 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
6826 * @pf: board private structure
6827 *
6828 * Returns 0 on success, negative value on failure
6829 **/
6830static int i40e_setup_pf_switch(struct i40e_pf *pf)
6831{
6832 int ret;
6833
6834 /* find out what's out there already */
6835 ret = i40e_fetch_switch_configuration(pf, false);
6836 if (ret) {
6837 dev_info(&pf->pdev->dev,
6838 "couldn't fetch switch config, err %d, aq_err %d\n",
6839 ret, pf->hw.aq.asq_last_status);
6840 return ret;
6841 }
6842 i40e_pf_reset_stats(pf);
6843
6844 /* fdir VSI must happen first to be sure it gets queue 0, but only
6845 * if there is enough room for the fdir VSI
6846 */
6847 if (pf->num_lan_qps > 1)
6848 i40e_fdir_setup(pf);
6849
6850 /* first time setup */
6851 if (pf->lan_vsi == I40E_NO_VSI) {
6852 struct i40e_vsi *vsi = NULL;
6853 u16 uplink_seid;
6854
6855 /* Set up the PF VSI associated with the PF's main VSI
6856 * that is already in the HW switch
6857 */
6858 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6859 uplink_seid = pf->veb[pf->lan_veb]->seid;
6860 else
6861 uplink_seid = pf->mac_seid;
6862
6863 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
6864 if (!vsi) {
6865 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
6866 i40e_fdir_teardown(pf);
6867 return -EAGAIN;
6868 }
6869 /* accommodate kcompat by copying the main VSI queue count
6870 * into the pf, since this newer code pushes the pf queue
6871 * info down a level into a VSI
6872 */
6873 pf->num_rx_queues = vsi->alloc_queue_pairs;
6874 pf->num_tx_queues = vsi->alloc_queue_pairs;
6875 } else {
6876 /* force a reset of TC and queue layout configurations */
6877 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
6878 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
6879 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
6880 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
6881 }
6882 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
6883
6884 /* Setup static PF queue filter control settings */
6885 ret = i40e_setup_pf_filter_control(pf);
6886 if (ret) {
6887 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
6888 ret);
6889 /* Failure here should not stop continuing other steps */
6890 }
6891
6892 /* enable RSS in the HW, even for only one queue, as the stack can use
6893 * the hash
6894 */
6895 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
6896 i40e_config_rss(pf);
6897
6898 /* fill in link information and enable LSE reporting */
6899 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
6900 i40e_link_event(pf);
6901
6902 /* Initialize user-specifics link properties */
6903 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
6904 I40E_AQ_AN_COMPLETED) ? true : false);
6905 pf->hw.fc.requested_mode = I40E_FC_DEFAULT;
6906 if (pf->hw.phy.link_info.an_info &
6907 (I40E_AQ_LINK_PAUSE_TX | I40E_AQ_LINK_PAUSE_RX))
6908 pf->hw.fc.current_mode = I40E_FC_FULL;
6909 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX)
6910 pf->hw.fc.current_mode = I40E_FC_TX_PAUSE;
6911 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX)
6912 pf->hw.fc.current_mode = I40E_FC_RX_PAUSE;
6913 else
6914 pf->hw.fc.current_mode = I40E_FC_DEFAULT;
6915
6916 return ret;
6917}
6918
6919/**
6920 * i40e_set_rss_size - helper to set rss_size
6921 * @pf: board private structure
6922 * @queues_left: how many queues
6923 */
6924static u16 i40e_set_rss_size(struct i40e_pf *pf, int queues_left)
6925{
6926 int num_tc0;
6927
6928 num_tc0 = min_t(int, queues_left, pf->rss_size_max);
6929 num_tc0 = min_t(int, num_tc0, nr_cpus_node(numa_node_id()));
6930 num_tc0 = rounddown_pow_of_two(num_tc0);
6931
6932 return num_tc0;
6933}
6934
6935/**
6936 * i40e_determine_queue_usage - Work out queue distribution
6937 * @pf: board private structure
6938 **/
6939static void i40e_determine_queue_usage(struct i40e_pf *pf)
6940{
6941 int accum_tc_size;
6942 int queues_left;
6943
6944 pf->num_lan_qps = 0;
6945 pf->num_tc_qps = rounddown_pow_of_two(pf->num_tc_qps);
6946 accum_tc_size = (I40E_MAX_TRAFFIC_CLASS - 1) * pf->num_tc_qps;
6947
6948 /* Find the max queues to be put into basic use. We'll always be
6949 * using TC0, whether or not DCB is running, and TC0 will get the
6950 * big RSS set.
6951 */
6952 queues_left = pf->hw.func_caps.num_tx_qp;
6953
6954 if (!((pf->flags & I40E_FLAG_MSIX_ENABLED) &&
6955 (pf->flags & I40E_FLAG_MQ_ENABLED)) ||
6956 !(pf->flags & (I40E_FLAG_RSS_ENABLED |
6957 I40E_FLAG_FDIR_ENABLED | I40E_FLAG_DCB_ENABLED)) ||
6958 (queues_left == 1)) {
6959
6960 /* one qp for PF, no queues for anything else */
6961 queues_left = 0;
6962 pf->rss_size = pf->num_lan_qps = 1;
6963
6964 /* make sure all the fancies are disabled */
6965 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
6966 I40E_FLAG_MQ_ENABLED |
6967 I40E_FLAG_FDIR_ENABLED |
6968 I40E_FLAG_FDIR_ATR_ENABLED |
6969 I40E_FLAG_DCB_ENABLED |
6970 I40E_FLAG_SRIOV_ENABLED |
6971 I40E_FLAG_VMDQ_ENABLED);
6972
6973 } else if (pf->flags & I40E_FLAG_RSS_ENABLED &&
6974 !(pf->flags & I40E_FLAG_FDIR_ENABLED) &&
6975 !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
6976
6977 pf->rss_size = i40e_set_rss_size(pf, queues_left);
6978
6979 queues_left -= pf->rss_size;
6980 pf->num_lan_qps = pf->rss_size;
6981
6982 } else if (pf->flags & I40E_FLAG_RSS_ENABLED &&
6983 !(pf->flags & I40E_FLAG_FDIR_ENABLED) &&
6984 (pf->flags & I40E_FLAG_DCB_ENABLED)) {
6985
6986 /* save num_tc_qps queues for TCs 1 thru 7 and the rest
6987 * are set up for RSS in TC0
6988 */
6989 queues_left -= accum_tc_size;
6990
6991 pf->rss_size = i40e_set_rss_size(pf, queues_left);
6992
6993 queues_left -= pf->rss_size;
6994 if (queues_left < 0) {
6995 dev_info(&pf->pdev->dev, "not enough queues for DCB\n");
6996 return;
6997 }
6998
6999 pf->num_lan_qps = pf->rss_size + accum_tc_size;
7000
7001 } else if (pf->flags & I40E_FLAG_RSS_ENABLED &&
7002 (pf->flags & I40E_FLAG_FDIR_ENABLED) &&
7003 !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
7004
7005 queues_left -= 1; /* save 1 queue for FD */
7006
7007 pf->rss_size = i40e_set_rss_size(pf, queues_left);
7008
7009 queues_left -= pf->rss_size;
7010 if (queues_left < 0) {
7011 dev_info(&pf->pdev->dev, "not enough queues for Flow Director\n");
7012 return;
7013 }
7014
7015 pf->num_lan_qps = pf->rss_size;
7016
7017 } else if (pf->flags & I40E_FLAG_RSS_ENABLED &&
7018 (pf->flags & I40E_FLAG_FDIR_ENABLED) &&
7019 (pf->flags & I40E_FLAG_DCB_ENABLED)) {
7020
7021 /* save 1 queue for TCs 1 thru 7,
7022 * 1 queue for flow director,
7023 * and the rest are set up for RSS in TC0
7024 */
7025 queues_left -= 1;
7026 queues_left -= accum_tc_size;
7027
7028 pf->rss_size = i40e_set_rss_size(pf, queues_left);
7029 queues_left -= pf->rss_size;
7030 if (queues_left < 0) {
7031 dev_info(&pf->pdev->dev, "not enough queues for DCB and Flow Director\n");
7032 return;
7033 }
7034
7035 pf->num_lan_qps = pf->rss_size + accum_tc_size;
7036
7037 } else {
7038 dev_info(&pf->pdev->dev,
7039 "Invalid configuration, flags=0x%08llx\n", pf->flags);
7040 return;
7041 }
7042
7043 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
7044 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
7045 pf->num_req_vfs = min_t(int, pf->num_req_vfs, (queues_left /
7046 pf->num_vf_qps));
7047 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
7048 }
7049
7050 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7051 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
7052 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
7053 (queues_left / pf->num_vmdq_qps));
7054 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
7055 }
7056
7057 return;
7058}
7059
7060/**
7061 * i40e_setup_pf_filter_control - Setup PF static filter control
7062 * @pf: PF to be setup
7063 *
7064 * i40e_setup_pf_filter_control sets up a pf's initial filter control
7065 * settings. If PE/FCoE are enabled then it will also set the per PF
7066 * based filter sizes required for them. It also enables Flow director,
7067 * ethertype and macvlan type filter settings for the pf.
7068 *
7069 * Returns 0 on success, negative on failure
7070 **/
7071static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
7072{
7073 struct i40e_filter_control_settings *settings = &pf->filter_settings;
7074
7075 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
7076
7077 /* Flow Director is enabled */
7078 if (pf->flags & (I40E_FLAG_FDIR_ENABLED | I40E_FLAG_FDIR_ATR_ENABLED))
7079 settings->enable_fdir = true;
7080
7081 /* Ethtype and MACVLAN filters enabled for PF */
7082 settings->enable_ethtype = true;
7083 settings->enable_macvlan = true;
7084
7085 if (i40e_set_filter_control(&pf->hw, settings))
7086 return -ENOENT;
7087
7088 return 0;
7089}
7090
7091/**
7092 * i40e_probe - Device initialization routine
7093 * @pdev: PCI device information struct
7094 * @ent: entry in i40e_pci_tbl
7095 *
7096 * i40e_probe initializes a pf identified by a pci_dev structure.
7097 * The OS initialization, configuring of the pf private structure,
7098 * and a hardware reset occur.
7099 *
7100 * Returns 0 on success, negative on failure
7101 **/
7102static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
7103{
7104 struct i40e_driver_version dv;
7105 struct i40e_pf *pf;
7106 struct i40e_hw *hw;
7107 int err = 0;
7108 u32 len;
7109
7110 err = pci_enable_device_mem(pdev);
7111 if (err)
7112 return err;
7113
7114 /* set up for high or low dma */
7115 if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64))) {
7116 /* coherent mask for the same size will always succeed if
7117 * dma_set_mask does
7118 */
7119 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
7120 } else if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
7121 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
7122 } else {
7123 dev_err(&pdev->dev, "DMA configuration failed: %d\n", err);
7124 err = -EIO;
7125 goto err_dma;
7126 }
7127
7128 /* set up pci connections */
7129 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
7130 IORESOURCE_MEM), i40e_driver_name);
7131 if (err) {
7132 dev_info(&pdev->dev,
7133 "pci_request_selected_regions failed %d\n", err);
7134 goto err_pci_reg;
7135 }
7136
7137 pci_enable_pcie_error_reporting(pdev);
7138 pci_set_master(pdev);
7139
7140 /* Now that we have a PCI connection, we need to do the
7141 * low level device setup. This is primarily setting up
7142 * the Admin Queue structures and then querying for the
7143 * device's current profile information.
7144 */
7145 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
7146 if (!pf) {
7147 err = -ENOMEM;
7148 goto err_pf_alloc;
7149 }
7150 pf->next_vsi = 0;
7151 pf->pdev = pdev;
7152 set_bit(__I40E_DOWN, &pf->state);
7153
7154 hw = &pf->hw;
7155 hw->back = pf;
7156 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
7157 pci_resource_len(pdev, 0));
7158 if (!hw->hw_addr) {
7159 err = -EIO;
7160 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
7161 (unsigned int)pci_resource_start(pdev, 0),
7162 (unsigned int)pci_resource_len(pdev, 0), err);
7163 goto err_ioremap;
7164 }
7165 hw->vendor_id = pdev->vendor;
7166 hw->device_id = pdev->device;
7167 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
7168 hw->subsystem_vendor_id = pdev->subsystem_vendor;
7169 hw->subsystem_device_id = pdev->subsystem_device;
7170 hw->bus.device = PCI_SLOT(pdev->devfn);
7171 hw->bus.func = PCI_FUNC(pdev->devfn);
7172
7134f9ce
JB
7173 /* do a special CORER for clearing PXE mode once at init */
7174 if (hw->revision_id == 0 &&
7175 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
7176 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
7177 i40e_flush(hw);
7178 msleep(200);
7179 pf->corer_count++;
7180
7181 i40e_clear_pxe_mode(hw);
7182 }
7183
41c445ff
JB
7184 /* Reset here to make sure all is clean and to define PF 'n' */
7185 err = i40e_pf_reset(hw);
7186 if (err) {
7187 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
7188 goto err_pf_reset;
7189 }
7190 pf->pfr_count++;
7191
7192 hw->aq.num_arq_entries = I40E_AQ_LEN;
7193 hw->aq.num_asq_entries = I40E_AQ_LEN;
7194 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
7195 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
7196 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
7197 snprintf(pf->misc_int_name, sizeof(pf->misc_int_name) - 1,
7198 "%s-pf%d:misc",
7199 dev_driver_string(&pf->pdev->dev), pf->hw.pf_id);
7200
7201 err = i40e_init_shared_code(hw);
7202 if (err) {
7203 dev_info(&pdev->dev, "init_shared_code failed: %d\n", err);
7204 goto err_pf_reset;
7205 }
7206
7207 err = i40e_init_adminq(hw);
7208 dev_info(&pdev->dev, "%s\n", i40e_fw_version_str(hw));
fe310704
AS
7209 if (((hw->nvm.version & I40E_NVM_VERSION_HI_MASK)
7210 >> I40E_NVM_VERSION_HI_SHIFT) != I40E_CURRENT_NVM_VERSION_HI) {
7211 dev_info(&pdev->dev,
7212 "warning: NVM version not supported, supported version: %02x.%02x\n",
7213 I40E_CURRENT_NVM_VERSION_HI,
7214 I40E_CURRENT_NVM_VERSION_LO);
7215 }
41c445ff
JB
7216 if (err) {
7217 dev_info(&pdev->dev,
7218 "init_adminq failed: %d expecting API %02x.%02x\n",
7219 err,
7220 I40E_FW_API_VERSION_MAJOR, I40E_FW_API_VERSION_MINOR);
7221 goto err_pf_reset;
7222 }
7223
7224 err = i40e_get_capabilities(pf);
7225 if (err)
7226 goto err_adminq_setup;
7227
7228 err = i40e_sw_init(pf);
7229 if (err) {
7230 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
7231 goto err_sw_init;
7232 }
7233
7234 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
7235 hw->func_caps.num_rx_qp,
7236 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
7237 if (err) {
7238 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
7239 goto err_init_lan_hmc;
7240 }
7241
7242 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
7243 if (err) {
7244 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
7245 err = -ENOENT;
7246 goto err_configure_lan_hmc;
7247 }
7248
7249 i40e_get_mac_addr(hw, hw->mac.addr);
7250 if (i40e_validate_mac_addr(hw->mac.addr)) {
7251 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
7252 err = -EIO;
7253 goto err_mac_addr;
7254 }
7255 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
7256 memcpy(hw->mac.perm_addr, hw->mac.addr, ETH_ALEN);
7257
7258 pci_set_drvdata(pdev, pf);
7259 pci_save_state(pdev);
7260
7261 /* set up periodic task facility */
7262 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
7263 pf->service_timer_period = HZ;
7264
7265 INIT_WORK(&pf->service_task, i40e_service_task);
7266 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
7267 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
7268 pf->link_check_timeout = jiffies;
7269
7270 /* set up the main switch operations */
7271 i40e_determine_queue_usage(pf);
7272 i40e_init_interrupt_scheme(pf);
7273
7274 /* Set up the *vsi struct based on the number of VSIs in the HW,
7275 * and set up our local tracking of the MAIN PF vsi.
7276 */
7277 len = sizeof(struct i40e_vsi *) * pf->hw.func_caps.num_vsis;
7278 pf->vsi = kzalloc(len, GFP_KERNEL);
ed87ac09
WY
7279 if (!pf->vsi) {
7280 err = -ENOMEM;
41c445ff 7281 goto err_switch_setup;
ed87ac09 7282 }
41c445ff
JB
7283
7284 err = i40e_setup_pf_switch(pf);
7285 if (err) {
7286 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
7287 goto err_vsis;
7288 }
7289
7290 /* The main driver is (mostly) up and happy. We need to set this state
7291 * before setting up the misc vector or we get a race and the vector
7292 * ends up disabled forever.
7293 */
7294 clear_bit(__I40E_DOWN, &pf->state);
7295
7296 /* In case of MSIX we are going to setup the misc vector right here
7297 * to handle admin queue events etc. In case of legacy and MSI
7298 * the misc functionality and queue processing is combined in
7299 * the same vector and that gets setup at open.
7300 */
7301 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
7302 err = i40e_setup_misc_vector(pf);
7303 if (err) {
7304 dev_info(&pdev->dev,
7305 "setup of misc vector failed: %d\n", err);
7306 goto err_vsis;
7307 }
7308 }
7309
7310 /* prep for VF support */
7311 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
7312 (pf->flags & I40E_FLAG_MSIX_ENABLED)) {
7313 u32 val;
7314
7315 /* disable link interrupts for VFs */
7316 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
7317 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
7318 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
7319 i40e_flush(hw);
7320 }
7321
7322 i40e_dbg_pf_init(pf);
7323
7324 /* tell the firmware that we're starting */
7325 dv.major_version = DRV_VERSION_MAJOR;
7326 dv.minor_version = DRV_VERSION_MINOR;
7327 dv.build_version = DRV_VERSION_BUILD;
7328 dv.subbuild_version = 0;
7329 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
7330
7331 /* since everything's happy, start the service_task timer */
7332 mod_timer(&pf->service_timer,
7333 round_jiffies(jiffies + pf->service_timer_period));
7334
7335 return 0;
7336
7337 /* Unwind what we've done if something failed in the setup */
7338err_vsis:
7339 set_bit(__I40E_DOWN, &pf->state);
7340err_switch_setup:
7341 i40e_clear_interrupt_scheme(pf);
7342 kfree(pf->vsi);
7343 del_timer_sync(&pf->service_timer);
7344err_mac_addr:
7345err_configure_lan_hmc:
7346 (void)i40e_shutdown_lan_hmc(hw);
7347err_init_lan_hmc:
7348 kfree(pf->qp_pile);
7349 kfree(pf->irq_pile);
7350err_sw_init:
7351err_adminq_setup:
7352 (void)i40e_shutdown_adminq(hw);
7353err_pf_reset:
7354 iounmap(hw->hw_addr);
7355err_ioremap:
7356 kfree(pf);
7357err_pf_alloc:
7358 pci_disable_pcie_error_reporting(pdev);
7359 pci_release_selected_regions(pdev,
7360 pci_select_bars(pdev, IORESOURCE_MEM));
7361err_pci_reg:
7362err_dma:
7363 pci_disable_device(pdev);
7364 return err;
7365}
7366
7367/**
7368 * i40e_remove - Device removal routine
7369 * @pdev: PCI device information struct
7370 *
7371 * i40e_remove is called by the PCI subsystem to alert the driver
7372 * that is should release a PCI device. This could be caused by a
7373 * Hot-Plug event, or because the driver is going to be removed from
7374 * memory.
7375 **/
7376static void i40e_remove(struct pci_dev *pdev)
7377{
7378 struct i40e_pf *pf = pci_get_drvdata(pdev);
7379 i40e_status ret_code;
7380 u32 reg;
7381 int i;
7382
7383 i40e_dbg_pf_exit(pf);
7384
7385 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
7386 i40e_free_vfs(pf);
7387 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
7388 }
7389
7390 /* no more scheduling of any task */
7391 set_bit(__I40E_DOWN, &pf->state);
7392 del_timer_sync(&pf->service_timer);
7393 cancel_work_sync(&pf->service_task);
7394
7395 i40e_fdir_teardown(pf);
7396
7397 /* If there is a switch structure or any orphans, remove them.
7398 * This will leave only the PF's VSI remaining.
7399 */
7400 for (i = 0; i < I40E_MAX_VEB; i++) {
7401 if (!pf->veb[i])
7402 continue;
7403
7404 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
7405 pf->veb[i]->uplink_seid == 0)
7406 i40e_switch_branch_release(pf->veb[i]);
7407 }
7408
7409 /* Now we can shutdown the PF's VSI, just before we kill
7410 * adminq and hmc.
7411 */
7412 if (pf->vsi[pf->lan_vsi])
7413 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
7414
7415 i40e_stop_misc_vector(pf);
7416 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
7417 synchronize_irq(pf->msix_entries[0].vector);
7418 free_irq(pf->msix_entries[0].vector, pf);
7419 }
7420
7421 /* shutdown and destroy the HMC */
7422 ret_code = i40e_shutdown_lan_hmc(&pf->hw);
7423 if (ret_code)
7424 dev_warn(&pdev->dev,
7425 "Failed to destroy the HMC resources: %d\n", ret_code);
7426
7427 /* shutdown the adminq */
7428 i40e_aq_queue_shutdown(&pf->hw, true);
7429 ret_code = i40e_shutdown_adminq(&pf->hw);
7430 if (ret_code)
7431 dev_warn(&pdev->dev,
7432 "Failed to destroy the Admin Queue resources: %d\n",
7433 ret_code);
7434
7435 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
7436 i40e_clear_interrupt_scheme(pf);
7437 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
7438 if (pf->vsi[i]) {
7439 i40e_vsi_clear_rings(pf->vsi[i]);
7440 i40e_vsi_clear(pf->vsi[i]);
7441 pf->vsi[i] = NULL;
7442 }
7443 }
7444
7445 for (i = 0; i < I40E_MAX_VEB; i++) {
7446 kfree(pf->veb[i]);
7447 pf->veb[i] = NULL;
7448 }
7449
7450 kfree(pf->qp_pile);
7451 kfree(pf->irq_pile);
7452 kfree(pf->sw_config);
7453 kfree(pf->vsi);
7454
7455 /* force a PF reset to clean anything leftover */
7456 reg = rd32(&pf->hw, I40E_PFGEN_CTRL);
7457 wr32(&pf->hw, I40E_PFGEN_CTRL, (reg | I40E_PFGEN_CTRL_PFSWR_MASK));
7458 i40e_flush(&pf->hw);
7459
7460 iounmap(pf->hw.hw_addr);
7461 kfree(pf);
7462 pci_release_selected_regions(pdev,
7463 pci_select_bars(pdev, IORESOURCE_MEM));
7464
7465 pci_disable_pcie_error_reporting(pdev);
7466 pci_disable_device(pdev);
7467}
7468
7469/**
7470 * i40e_pci_error_detected - warning that something funky happened in PCI land
7471 * @pdev: PCI device information struct
7472 *
7473 * Called to warn that something happened and the error handling steps
7474 * are in progress. Allows the driver to quiesce things, be ready for
7475 * remediation.
7476 **/
7477static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
7478 enum pci_channel_state error)
7479{
7480 struct i40e_pf *pf = pci_get_drvdata(pdev);
7481
7482 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
7483
7484 /* shutdown all operations */
7485 i40e_pf_quiesce_all_vsi(pf);
7486
7487 /* Request a slot reset */
7488 return PCI_ERS_RESULT_NEED_RESET;
7489}
7490
7491/**
7492 * i40e_pci_error_slot_reset - a PCI slot reset just happened
7493 * @pdev: PCI device information struct
7494 *
7495 * Called to find if the driver can work with the device now that
7496 * the pci slot has been reset. If a basic connection seems good
7497 * (registers are readable and have sane content) then return a
7498 * happy little PCI_ERS_RESULT_xxx.
7499 **/
7500static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
7501{
7502 struct i40e_pf *pf = pci_get_drvdata(pdev);
7503 pci_ers_result_t result;
7504 int err;
7505 u32 reg;
7506
7507 dev_info(&pdev->dev, "%s\n", __func__);
7508 if (pci_enable_device_mem(pdev)) {
7509 dev_info(&pdev->dev,
7510 "Cannot re-enable PCI device after reset.\n");
7511 result = PCI_ERS_RESULT_DISCONNECT;
7512 } else {
7513 pci_set_master(pdev);
7514 pci_restore_state(pdev);
7515 pci_save_state(pdev);
7516 pci_wake_from_d3(pdev, false);
7517
7518 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
7519 if (reg == 0)
7520 result = PCI_ERS_RESULT_RECOVERED;
7521 else
7522 result = PCI_ERS_RESULT_DISCONNECT;
7523 }
7524
7525 err = pci_cleanup_aer_uncorrect_error_status(pdev);
7526 if (err) {
7527 dev_info(&pdev->dev,
7528 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
7529 err);
7530 /* non-fatal, continue */
7531 }
7532
7533 return result;
7534}
7535
7536/**
7537 * i40e_pci_error_resume - restart operations after PCI error recovery
7538 * @pdev: PCI device information struct
7539 *
7540 * Called to allow the driver to bring things back up after PCI error
7541 * and/or reset recovery has finished.
7542 **/
7543static void i40e_pci_error_resume(struct pci_dev *pdev)
7544{
7545 struct i40e_pf *pf = pci_get_drvdata(pdev);
7546
7547 dev_info(&pdev->dev, "%s\n", __func__);
7548 i40e_handle_reset_warning(pf);
7549}
7550
7551static const struct pci_error_handlers i40e_err_handler = {
7552 .error_detected = i40e_pci_error_detected,
7553 .slot_reset = i40e_pci_error_slot_reset,
7554 .resume = i40e_pci_error_resume,
7555};
7556
7557static struct pci_driver i40e_driver = {
7558 .name = i40e_driver_name,
7559 .id_table = i40e_pci_tbl,
7560 .probe = i40e_probe,
7561 .remove = i40e_remove,
7562 .err_handler = &i40e_err_handler,
7563 .sriov_configure = i40e_pci_sriov_configure,
7564};
7565
7566/**
7567 * i40e_init_module - Driver registration routine
7568 *
7569 * i40e_init_module is the first routine called when the driver is
7570 * loaded. All it does is register with the PCI subsystem.
7571 **/
7572static int __init i40e_init_module(void)
7573{
7574 pr_info("%s: %s - version %s\n", i40e_driver_name,
7575 i40e_driver_string, i40e_driver_version_str);
7576 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
7577 i40e_dbg_init();
7578 return pci_register_driver(&i40e_driver);
7579}
7580module_init(i40e_init_module);
7581
7582/**
7583 * i40e_exit_module - Driver exit cleanup routine
7584 *
7585 * i40e_exit_module is called just before the driver is removed
7586 * from memory.
7587 **/
7588static void __exit i40e_exit_module(void)
7589{
7590 pci_unregister_driver(&i40e_driver);
7591 i40e_dbg_exit();
7592}
7593module_exit(i40e_exit_module);
This page took 0.37216 seconds and 5 git commands to generate.