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