blob: d1b5bae15ea8d528007d4d5379495c062d4ebc6e [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 Duycka114d0a2013-09-28 06:00:43 +0000379 memset(&vsi->rx_rings[i].stats, 0 ,
380 sizeof(vsi->rx_rings[i].stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000381 memset(&vsi->rx_rings[i].rx_stats, 0 ,
382 sizeof(vsi->rx_rings[i].rx_stats));
Alexander Duycka114d0a2013-09-28 06:00:43 +0000383 memset(&vsi->tx_rings[i].stats, 0 ,
384 sizeof(vsi->tx_rings[i].stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000385 memset(&vsi->tx_rings[i].tx_stats, 0,
386 sizeof(vsi->tx_rings[i].tx_stats));
387 }
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++) {
605 struct i40e_ring *ring = &vsi->tx_rings[i];
606 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++) {
659 struct i40e_ring *ring = &vsi->tx_rings[i];
660
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
714 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
720 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++)
1995 err = i40e_setup_tx_descriptors(&vsi->tx_rings[i]);
1996
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++)
2011 if (vsi->tx_rings[i].desc)
2012 i40e_free_tx_resources(&vsi->tx_rings[i]);
2013}
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++)
2030 err = i40e_setup_rx_descriptors(&vsi->rx_rings[i]);
2031 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++)
2045 if (vsi->rx_rings[i].desc)
2046 i40e_free_rx_resources(&vsi->rx_rings[i]);
2047}
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
2230 for (i = 0; (i < vsi->num_queue_pairs) && (!err); i++)
2231 err = i40e_configure_tx_ring(&vsi->tx_rings[i]);
2232
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++)
2281 err = i40e_configure_rx_ring(&vsi->rx_rings[i]);
2282
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++) {
2305 struct i40e_ring *rx_ring = &vsi->rx_rings[i];
2306 struct i40e_ring *tx_ring = &vsi->tx_rings[i];
2307 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;
2361 q_vector = vsi->q_vectors;
2362 for (i = 0; i < vsi->num_q_vectors; i++, q_vector++, vector++) {
2363 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{
2442 struct i40e_q_vector *q_vector = vsi->q_vectors;
2443 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
2519 if (!q_vector->tx.ring[0] && !q_vector->rx.ring[0])
2520 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
2536 if (!q_vector->tx.ring[0] && !q_vector->rx.ring[0])
2537 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++) {
2561 struct i40e_q_vector *q_vector = &(vsi->q_vectors[vector]);
2562
2563 if (q_vector->tx.ring[0] && q_vector->rx.ring[0]) {
2564 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2565 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
2566 tx_int_idx++;
2567 } else if (q_vector->rx.ring[0]) {
2568 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2569 "%s-%s-%d", basename, "rx", rx_int_idx++);
2570 } else if (q_vector->tx.ring[0]) {
2571 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++) {
2618 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);
2620 }
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))
2712 napi_schedule(&pf->vsi[pf->lan_vsi]->q_vectors[0].napi);
2713 }
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/**
2781 * i40e_map_vector_to_rxq - Assigns the Rx queue to the vector
2782 * @vsi: the VSI being configured
2783 * @v_idx: vector index
2784 * @r_idx: rx queue index
2785 **/
2786static void map_vector_to_rxq(struct i40e_vsi *vsi, int v_idx, int r_idx)
2787{
2788 struct i40e_q_vector *q_vector = &(vsi->q_vectors[v_idx]);
2789 struct i40e_ring *rx_ring = &(vsi->rx_rings[r_idx]);
2790
2791 rx_ring->q_vector = q_vector;
2792 q_vector->rx.ring[q_vector->rx.count] = rx_ring;
2793 q_vector->rx.count++;
2794 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2795 q_vector->vsi = vsi;
2796}
2797
2798/**
2799 * i40e_map_vector_to_txq - Assigns the Tx queue to the vector
2800 * @vsi: the VSI being configured
2801 * @v_idx: vector index
2802 * @t_idx: tx queue index
2803 **/
2804static void map_vector_to_txq(struct i40e_vsi *vsi, int v_idx, int t_idx)
2805{
2806 struct i40e_q_vector *q_vector = &(vsi->q_vectors[v_idx]);
2807 struct i40e_ring *tx_ring = &(vsi->tx_rings[t_idx]);
2808
2809 tx_ring->q_vector = q_vector;
2810 q_vector->tx.ring[q_vector->tx.count] = tx_ring;
2811 q_vector->tx.count++;
2812 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2813 q_vector->num_ringpairs++;
2814 q_vector->vsi = vsi;
2815}
2816
2817/**
2818 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
2819 * @vsi: the VSI being configured
2820 *
2821 * This function maps descriptor rings to the queue-specific vectors
2822 * we were allotted through the MSI-X enabling code. Ideally, we'd have
2823 * one vector per queue pair, but on a constrained vector budget, we
2824 * group the queue pairs as "efficiently" as possible.
2825 **/
2826static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
2827{
2828 int qp_remaining = vsi->num_queue_pairs;
2829 int q_vectors = vsi->num_q_vectors;
2830 int qp_per_vector;
2831 int v_start = 0;
2832 int qp_idx = 0;
2833
2834 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
2835 * group them so there are multiple queues per vector.
2836 */
2837 for (; v_start < q_vectors && qp_remaining; v_start++) {
2838 qp_per_vector = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
2839 for (; qp_per_vector;
2840 qp_per_vector--, qp_idx++, qp_remaining--) {
2841 map_vector_to_rxq(vsi, v_start, qp_idx);
2842 map_vector_to_txq(vsi, v_start, qp_idx);
2843 }
2844 }
2845}
2846
2847/**
2848 * i40e_vsi_request_irq - Request IRQ from the OS
2849 * @vsi: the VSI being configured
2850 * @basename: name for the vector
2851 **/
2852static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
2853{
2854 struct i40e_pf *pf = vsi->back;
2855 int err;
2856
2857 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
2858 err = i40e_vsi_request_irq_msix(vsi, basename);
2859 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
2860 err = request_irq(pf->pdev->irq, i40e_intr, 0,
2861 pf->misc_int_name, pf);
2862 else
2863 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
2864 pf->misc_int_name, pf);
2865
2866 if (err)
2867 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
2868
2869 return err;
2870}
2871
2872#ifdef CONFIG_NET_POLL_CONTROLLER
2873/**
2874 * i40e_netpoll - A Polling 'interrupt'handler
2875 * @netdev: network interface device structure
2876 *
2877 * This is used by netconsole to send skbs without having to re-enable
2878 * interrupts. It's not called while the normal interrupt routine is executing.
2879 **/
2880static void i40e_netpoll(struct net_device *netdev)
2881{
2882 struct i40e_netdev_priv *np = netdev_priv(netdev);
2883 struct i40e_vsi *vsi = np->vsi;
2884 struct i40e_pf *pf = vsi->back;
2885 int i;
2886
2887 /* if interface is down do nothing */
2888 if (test_bit(__I40E_DOWN, &vsi->state))
2889 return;
2890
2891 pf->flags |= I40E_FLAG_IN_NETPOLL;
2892 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2893 for (i = 0; i < vsi->num_q_vectors; i++)
2894 i40e_msix_clean_rings(0, &vsi->q_vectors[i]);
2895 } else {
2896 i40e_intr(pf->pdev->irq, netdev);
2897 }
2898 pf->flags &= ~I40E_FLAG_IN_NETPOLL;
2899}
2900#endif
2901
2902/**
2903 * i40e_vsi_control_tx - Start or stop a VSI's rings
2904 * @vsi: the VSI being configured
2905 * @enable: start or stop the rings
2906 **/
2907static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
2908{
2909 struct i40e_pf *pf = vsi->back;
2910 struct i40e_hw *hw = &pf->hw;
2911 int i, j, pf_q;
2912 u32 tx_reg;
2913
2914 pf_q = vsi->base_queue;
2915 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
2916 j = 1000;
2917 do {
2918 usleep_range(1000, 2000);
2919 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
2920 } while (j-- && ((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT)
2921 ^ (tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT)) & 1);
2922
2923 if (enable) {
2924 /* is STAT set ? */
2925 if ((tx_reg & I40E_QTX_ENA_QENA_STAT_MASK)) {
2926 dev_info(&pf->pdev->dev,
2927 "Tx %d already enabled\n", i);
2928 continue;
2929 }
2930 } else {
2931 /* is !STAT set ? */
2932 if (!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK)) {
2933 dev_info(&pf->pdev->dev,
2934 "Tx %d already disabled\n", i);
2935 continue;
2936 }
2937 }
2938
2939 /* turn on/off the queue */
2940 if (enable)
2941 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK |
2942 I40E_QTX_ENA_QENA_STAT_MASK;
2943 else
2944 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
2945
2946 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
2947
2948 /* wait for the change to finish */
2949 for (j = 0; j < 10; j++) {
2950 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
2951 if (enable) {
2952 if ((tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
2953 break;
2954 } else {
2955 if (!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
2956 break;
2957 }
2958
2959 udelay(10);
2960 }
2961 if (j >= 10) {
2962 dev_info(&pf->pdev->dev, "Tx ring %d %sable timeout\n",
2963 pf_q, (enable ? "en" : "dis"));
2964 return -ETIMEDOUT;
2965 }
2966 }
2967
2968 return 0;
2969}
2970
2971/**
2972 * i40e_vsi_control_rx - Start or stop a VSI's rings
2973 * @vsi: the VSI being configured
2974 * @enable: start or stop the rings
2975 **/
2976static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
2977{
2978 struct i40e_pf *pf = vsi->back;
2979 struct i40e_hw *hw = &pf->hw;
2980 int i, j, pf_q;
2981 u32 rx_reg;
2982
2983 pf_q = vsi->base_queue;
2984 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
2985 j = 1000;
2986 do {
2987 usleep_range(1000, 2000);
2988 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
2989 } while (j-- && ((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT)
2990 ^ (rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT)) & 1);
2991
2992 if (enable) {
2993 /* is STAT set ? */
2994 if ((rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
2995 continue;
2996 } else {
2997 /* is !STAT set ? */
2998 if (!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
2999 continue;
3000 }
3001
3002 /* turn on/off the queue */
3003 if (enable)
3004 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK |
3005 I40E_QRX_ENA_QENA_STAT_MASK;
3006 else
3007 rx_reg &= ~(I40E_QRX_ENA_QENA_REQ_MASK |
3008 I40E_QRX_ENA_QENA_STAT_MASK);
3009 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
3010
3011 /* wait for the change to finish */
3012 for (j = 0; j < 10; j++) {
3013 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
3014
3015 if (enable) {
3016 if ((rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3017 break;
3018 } else {
3019 if (!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3020 break;
3021 }
3022
3023 udelay(10);
3024 }
3025 if (j >= 10) {
3026 dev_info(&pf->pdev->dev, "Rx ring %d %sable timeout\n",
3027 pf_q, (enable ? "en" : "dis"));
3028 return -ETIMEDOUT;
3029 }
3030 }
3031
3032 return 0;
3033}
3034
3035/**
3036 * i40e_vsi_control_rings - Start or stop a VSI's rings
3037 * @vsi: the VSI being configured
3038 * @enable: start or stop the rings
3039 **/
3040static int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
3041{
3042 int ret;
3043
3044 /* do rx first for enable and last for disable */
3045 if (request) {
3046 ret = i40e_vsi_control_rx(vsi, request);
3047 if (ret)
3048 return ret;
3049 ret = i40e_vsi_control_tx(vsi, request);
3050 } else {
3051 ret = i40e_vsi_control_tx(vsi, request);
3052 if (ret)
3053 return ret;
3054 ret = i40e_vsi_control_rx(vsi, request);
3055 }
3056
3057 return ret;
3058}
3059
3060/**
3061 * i40e_vsi_free_irq - Free the irq association with the OS
3062 * @vsi: the VSI being configured
3063 **/
3064static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
3065{
3066 struct i40e_pf *pf = vsi->back;
3067 struct i40e_hw *hw = &pf->hw;
3068 int base = vsi->base_vector;
3069 u32 val, qp;
3070 int i;
3071
3072 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3073 if (!vsi->q_vectors)
3074 return;
3075
3076 for (i = 0; i < vsi->num_q_vectors; i++) {
3077 u16 vector = i + base;
3078
3079 /* free only the irqs that were actually requested */
3080 if (vsi->q_vectors[i].num_ringpairs == 0)
3081 continue;
3082
3083 /* clear the affinity_mask in the IRQ descriptor */
3084 irq_set_affinity_hint(pf->msix_entries[vector].vector,
3085 NULL);
3086 free_irq(pf->msix_entries[vector].vector,
3087 &vsi->q_vectors[i]);
3088
3089 /* Tear down the interrupt queue link list
3090 *
3091 * We know that they come in pairs and always
3092 * the Rx first, then the Tx. To clear the
3093 * link list, stick the EOL value into the
3094 * next_q field of the registers.
3095 */
3096 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
3097 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3098 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3099 val |= I40E_QUEUE_END_OF_LIST
3100 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3101 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
3102
3103 while (qp != I40E_QUEUE_END_OF_LIST) {
3104 u32 next;
3105
3106 val = rd32(hw, I40E_QINT_RQCTL(qp));
3107
3108 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3109 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3110 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3111 I40E_QINT_RQCTL_INTEVENT_MASK);
3112
3113 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3114 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3115
3116 wr32(hw, I40E_QINT_RQCTL(qp), val);
3117
3118 val = rd32(hw, I40E_QINT_TQCTL(qp));
3119
3120 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
3121 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
3122
3123 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3124 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3125 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3126 I40E_QINT_TQCTL_INTEVENT_MASK);
3127
3128 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3129 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3130
3131 wr32(hw, I40E_QINT_TQCTL(qp), val);
3132 qp = next;
3133 }
3134 }
3135 } else {
3136 free_irq(pf->pdev->irq, pf);
3137
3138 val = rd32(hw, I40E_PFINT_LNKLST0);
3139 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3140 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3141 val |= I40E_QUEUE_END_OF_LIST
3142 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
3143 wr32(hw, I40E_PFINT_LNKLST0, val);
3144
3145 val = rd32(hw, I40E_QINT_RQCTL(qp));
3146 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3147 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3148 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3149 I40E_QINT_RQCTL_INTEVENT_MASK);
3150
3151 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3152 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3153
3154 wr32(hw, I40E_QINT_RQCTL(qp), val);
3155
3156 val = rd32(hw, I40E_QINT_TQCTL(qp));
3157
3158 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3159 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3160 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3161 I40E_QINT_TQCTL_INTEVENT_MASK);
3162
3163 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3164 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3165
3166 wr32(hw, I40E_QINT_TQCTL(qp), val);
3167 }
3168}
3169
3170/**
3171 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
3172 * @vsi: the VSI being un-configured
3173 *
3174 * This frees the memory allocated to the q_vectors and
3175 * deletes references to the NAPI struct.
3176 **/
3177static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
3178{
3179 int v_idx;
3180
3181 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++) {
3182 struct i40e_q_vector *q_vector = &vsi->q_vectors[v_idx];
3183 int r_idx;
3184
3185 if (!q_vector)
3186 continue;
3187
3188 /* disassociate q_vector from rings */
3189 for (r_idx = 0; r_idx < q_vector->tx.count; r_idx++)
3190 q_vector->tx.ring[r_idx]->q_vector = NULL;
3191 for (r_idx = 0; r_idx < q_vector->rx.count; r_idx++)
3192 q_vector->rx.ring[r_idx]->q_vector = NULL;
3193
3194 /* only VSI w/ an associated netdev is set up w/ NAPI */
3195 if (vsi->netdev)
3196 netif_napi_del(&q_vector->napi);
3197 }
3198 kfree(vsi->q_vectors);
3199}
3200
3201/**
3202 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
3203 * @pf: board private structure
3204 **/
3205static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
3206{
3207 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
3208 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3209 pci_disable_msix(pf->pdev);
3210 kfree(pf->msix_entries);
3211 pf->msix_entries = NULL;
3212 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
3213 pci_disable_msi(pf->pdev);
3214 }
3215 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
3216}
3217
3218/**
3219 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
3220 * @pf: board private structure
3221 *
3222 * We go through and clear interrupt specific resources and reset the structure
3223 * to pre-load conditions
3224 **/
3225static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
3226{
3227 int i;
3228
3229 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
3230 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
3231 if (pf->vsi[i])
3232 i40e_vsi_free_q_vectors(pf->vsi[i]);
3233 i40e_reset_interrupt_capability(pf);
3234}
3235
3236/**
3237 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
3238 * @vsi: the VSI being configured
3239 **/
3240static void i40e_napi_enable_all(struct i40e_vsi *vsi)
3241{
3242 int q_idx;
3243
3244 if (!vsi->netdev)
3245 return;
3246
3247 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
3248 napi_enable(&vsi->q_vectors[q_idx].napi);
3249}
3250
3251/**
3252 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
3253 * @vsi: the VSI being configured
3254 **/
3255static void i40e_napi_disable_all(struct i40e_vsi *vsi)
3256{
3257 int q_idx;
3258
3259 if (!vsi->netdev)
3260 return;
3261
3262 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
3263 napi_disable(&vsi->q_vectors[q_idx].napi);
3264}
3265
3266/**
3267 * i40e_quiesce_vsi - Pause a given VSI
3268 * @vsi: the VSI being paused
3269 **/
3270static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
3271{
3272 if (test_bit(__I40E_DOWN, &vsi->state))
3273 return;
3274
3275 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
3276 if (vsi->netdev && netif_running(vsi->netdev)) {
3277 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
3278 } else {
3279 set_bit(__I40E_DOWN, &vsi->state);
3280 i40e_down(vsi);
3281 }
3282}
3283
3284/**
3285 * i40e_unquiesce_vsi - Resume a given VSI
3286 * @vsi: the VSI being resumed
3287 **/
3288static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
3289{
3290 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
3291 return;
3292
3293 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
3294 if (vsi->netdev && netif_running(vsi->netdev))
3295 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
3296 else
3297 i40e_up(vsi); /* this clears the DOWN bit */
3298}
3299
3300/**
3301 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
3302 * @pf: the PF
3303 **/
3304static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
3305{
3306 int v;
3307
3308 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
3309 if (pf->vsi[v])
3310 i40e_quiesce_vsi(pf->vsi[v]);
3311 }
3312}
3313
3314/**
3315 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
3316 * @pf: the PF
3317 **/
3318static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
3319{
3320 int v;
3321
3322 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
3323 if (pf->vsi[v])
3324 i40e_unquiesce_vsi(pf->vsi[v]);
3325 }
3326}
3327
3328/**
3329 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
3330 * @dcbcfg: the corresponding DCBx configuration structure
3331 *
3332 * Return the number of TCs from given DCBx configuration
3333 **/
3334static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
3335{
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003336 u8 num_tc = 0;
3337 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003338
3339 /* Scan the ETS Config Priority Table to find
3340 * traffic class enabled for a given priority
3341 * and use the traffic class index to get the
3342 * number of traffic classes enabled
3343 */
3344 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3345 if (dcbcfg->etscfg.prioritytable[i] > num_tc)
3346 num_tc = dcbcfg->etscfg.prioritytable[i];
3347 }
3348
3349 /* Traffic class index starts from zero so
3350 * increment to return the actual count
3351 */
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003352 return num_tc + 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003353}
3354
3355/**
3356 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
3357 * @dcbcfg: the corresponding DCBx configuration structure
3358 *
3359 * Query the current DCB configuration and return the number of
3360 * traffic classes enabled from the given DCBX config
3361 **/
3362static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
3363{
3364 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
3365 u8 enabled_tc = 1;
3366 u8 i;
3367
3368 for (i = 0; i < num_tc; i++)
3369 enabled_tc |= 1 << i;
3370
3371 return enabled_tc;
3372}
3373
3374/**
3375 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
3376 * @pf: PF being queried
3377 *
3378 * Return number of traffic classes enabled for the given PF
3379 **/
3380static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
3381{
3382 struct i40e_hw *hw = &pf->hw;
3383 u8 i, enabled_tc;
3384 u8 num_tc = 0;
3385 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3386
3387 /* If DCB is not enabled then always in single TC */
3388 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3389 return 1;
3390
3391 /* MFP mode return count of enabled TCs for this PF */
3392 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
3393 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3394 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3395 if (enabled_tc & (1 << i))
3396 num_tc++;
3397 }
3398 return num_tc;
3399 }
3400
3401 /* SFP mode will be enabled for all TCs on port */
3402 return i40e_dcb_get_num_tc(dcbcfg);
3403}
3404
3405/**
3406 * i40e_pf_get_default_tc - Get bitmap for first enabled TC
3407 * @pf: PF being queried
3408 *
3409 * Return a bitmap for first enabled traffic class for this PF.
3410 **/
3411static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
3412{
3413 u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3414 u8 i = 0;
3415
3416 if (!enabled_tc)
3417 return 0x1; /* TC0 */
3418
3419 /* Find the first enabled TC */
3420 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3421 if (enabled_tc & (1 << i))
3422 break;
3423 }
3424
3425 return 1 << i;
3426}
3427
3428/**
3429 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
3430 * @pf: PF being queried
3431 *
3432 * Return a bitmap for enabled traffic classes for this PF.
3433 **/
3434static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
3435{
3436 /* If DCB is not enabled for this PF then just return default TC */
3437 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3438 return i40e_pf_get_default_tc(pf);
3439
3440 /* MFP mode will have enabled TCs set by FW */
3441 if (pf->flags & I40E_FLAG_MFP_ENABLED)
3442 return pf->hw.func_caps.enabled_tcmap;
3443
3444 /* SFP mode we want PF to be enabled for all TCs */
3445 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
3446}
3447
3448/**
3449 * i40e_vsi_get_bw_info - Query VSI BW Information
3450 * @vsi: the VSI being queried
3451 *
3452 * Returns 0 on success, negative value on failure
3453 **/
3454static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
3455{
3456 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
3457 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
3458 struct i40e_pf *pf = vsi->back;
3459 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003460 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003461 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003462 int i;
3463
3464 /* Get the VSI level BW configuration */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003465 aq_ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
3466 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003467 dev_info(&pf->pdev->dev,
3468 "couldn't get pf vsi bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003469 aq_ret, pf->hw.aq.asq_last_status);
3470 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003471 }
3472
3473 /* Get the VSI level BW configuration per TC */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003474 aq_ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
3475 NULL);
3476 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003477 dev_info(&pf->pdev->dev,
3478 "couldn't get pf vsi ets bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003479 aq_ret, pf->hw.aq.asq_last_status);
3480 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003481 }
3482
3483 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
3484 dev_info(&pf->pdev->dev,
3485 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
3486 bw_config.tc_valid_bits,
3487 bw_ets_config.tc_valid_bits);
3488 /* Still continuing */
3489 }
3490
3491 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
3492 vsi->bw_max_quanta = bw_config.max_bw;
3493 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
3494 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
3495 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3496 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
3497 vsi->bw_ets_limit_credits[i] =
3498 le16_to_cpu(bw_ets_config.credits[i]);
3499 /* 3 bits out of 4 for each TC */
3500 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
3501 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003502
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003503 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003504}
3505
3506/**
3507 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
3508 * @vsi: the VSI being configured
3509 * @enabled_tc: TC bitmap
3510 * @bw_credits: BW shared credits per TC
3511 *
3512 * Returns 0 on success, negative value on failure
3513 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003514static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003515 u8 *bw_share)
3516{
3517 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003518 i40e_status aq_ret;
3519 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003520
3521 bw_data.tc_valid_bits = enabled_tc;
3522 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3523 bw_data.tc_bw_credits[i] = bw_share[i];
3524
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003525 aq_ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
3526 NULL);
3527 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003528 dev_info(&vsi->back->pdev->dev,
3529 "%s: AQ command Config VSI BW allocation per TC failed = %d\n",
3530 __func__, vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003531 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003532 }
3533
3534 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3535 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
3536
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003537 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003538}
3539
3540/**
3541 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
3542 * @vsi: the VSI being configured
3543 * @enabled_tc: TC map to be enabled
3544 *
3545 **/
3546static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3547{
3548 struct net_device *netdev = vsi->netdev;
3549 struct i40e_pf *pf = vsi->back;
3550 struct i40e_hw *hw = &pf->hw;
3551 u8 netdev_tc = 0;
3552 int i;
3553 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3554
3555 if (!netdev)
3556 return;
3557
3558 if (!enabled_tc) {
3559 netdev_reset_tc(netdev);
3560 return;
3561 }
3562
3563 /* Set up actual enabled TCs on the VSI */
3564 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
3565 return;
3566
3567 /* set per TC queues for the VSI */
3568 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3569 /* Only set TC queues for enabled tcs
3570 *
3571 * e.g. For a VSI that has TC0 and TC3 enabled the
3572 * enabled_tc bitmap would be 0x00001001; the driver
3573 * will set the numtc for netdev as 2 that will be
3574 * referenced by the netdev layer as TC 0 and 1.
3575 */
3576 if (vsi->tc_config.enabled_tc & (1 << i))
3577 netdev_set_tc_queue(netdev,
3578 vsi->tc_config.tc_info[i].netdev_tc,
3579 vsi->tc_config.tc_info[i].qcount,
3580 vsi->tc_config.tc_info[i].qoffset);
3581 }
3582
3583 /* Assign UP2TC map for the VSI */
3584 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3585 /* Get the actual TC# for the UP */
3586 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
3587 /* Get the mapped netdev TC# for the UP */
3588 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
3589 netdev_set_prio_tc_map(netdev, i, netdev_tc);
3590 }
3591}
3592
3593/**
3594 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
3595 * @vsi: the VSI being configured
3596 * @ctxt: the ctxt buffer returned from AQ VSI update param command
3597 **/
3598static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
3599 struct i40e_vsi_context *ctxt)
3600{
3601 /* copy just the sections touched not the entire info
3602 * since not all sections are valid as returned by
3603 * update vsi params
3604 */
3605 vsi->info.mapping_flags = ctxt->info.mapping_flags;
3606 memcpy(&vsi->info.queue_mapping,
3607 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
3608 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
3609 sizeof(vsi->info.tc_mapping));
3610}
3611
3612/**
3613 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
3614 * @vsi: VSI to be configured
3615 * @enabled_tc: TC bitmap
3616 *
3617 * This configures a particular VSI for TCs that are mapped to the
3618 * given TC bitmap. It uses default bandwidth share for TCs across
3619 * VSIs to configure TC for a particular VSI.
3620 *
3621 * NOTE:
3622 * It is expected that the VSI queues have been quisced before calling
3623 * this function.
3624 **/
3625static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3626{
3627 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
3628 struct i40e_vsi_context ctxt;
3629 int ret = 0;
3630 int i;
3631
3632 /* Check if enabled_tc is same as existing or new TCs */
3633 if (vsi->tc_config.enabled_tc == enabled_tc)
3634 return ret;
3635
3636 /* Enable ETS TCs with equal BW Share for now across all VSIs */
3637 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3638 if (enabled_tc & (1 << i))
3639 bw_share[i] = 1;
3640 }
3641
3642 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
3643 if (ret) {
3644 dev_info(&vsi->back->pdev->dev,
3645 "Failed configuring TC map %d for VSI %d\n",
3646 enabled_tc, vsi->seid);
3647 goto out;
3648 }
3649
3650 /* Update Queue Pairs Mapping for currently enabled UPs */
3651 ctxt.seid = vsi->seid;
3652 ctxt.pf_num = vsi->back->hw.pf_id;
3653 ctxt.vf_num = 0;
3654 ctxt.uplink_seid = vsi->uplink_seid;
3655 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
3656 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
3657
3658 /* Update the VSI after updating the VSI queue-mapping information */
3659 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3660 if (ret) {
3661 dev_info(&vsi->back->pdev->dev,
3662 "update vsi failed, aq_err=%d\n",
3663 vsi->back->hw.aq.asq_last_status);
3664 goto out;
3665 }
3666 /* update the local VSI info with updated queue map */
3667 i40e_vsi_update_queue_map(vsi, &ctxt);
3668 vsi->info.valid_sections = 0;
3669
3670 /* Update current VSI BW information */
3671 ret = i40e_vsi_get_bw_info(vsi);
3672 if (ret) {
3673 dev_info(&vsi->back->pdev->dev,
3674 "Failed updating vsi bw info, aq_err=%d\n",
3675 vsi->back->hw.aq.asq_last_status);
3676 goto out;
3677 }
3678
3679 /* Update the netdev TC setup */
3680 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
3681out:
3682 return ret;
3683}
3684
3685/**
3686 * i40e_up_complete - Finish the last steps of bringing up a connection
3687 * @vsi: the VSI being configured
3688 **/
3689static int i40e_up_complete(struct i40e_vsi *vsi)
3690{
3691 struct i40e_pf *pf = vsi->back;
3692 int err;
3693
3694 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3695 i40e_vsi_configure_msix(vsi);
3696 else
3697 i40e_configure_msi_and_legacy(vsi);
3698
3699 /* start rings */
3700 err = i40e_vsi_control_rings(vsi, true);
3701 if (err)
3702 return err;
3703
3704 clear_bit(__I40E_DOWN, &vsi->state);
3705 i40e_napi_enable_all(vsi);
3706 i40e_vsi_enable_irq(vsi);
3707
3708 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
3709 (vsi->netdev)) {
Anjali Singhai6d779b412013-09-28 06:00:02 +00003710 netdev_info(vsi->netdev, "NIC Link is Up\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003711 netif_tx_start_all_queues(vsi->netdev);
3712 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b412013-09-28 06:00:02 +00003713 } else if (vsi->netdev) {
3714 netdev_info(vsi->netdev, "NIC Link is Down\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003715 }
3716 i40e_service_event_schedule(pf);
3717
3718 return 0;
3719}
3720
3721/**
3722 * i40e_vsi_reinit_locked - Reset the VSI
3723 * @vsi: the VSI being configured
3724 *
3725 * Rebuild the ring structs after some configuration
3726 * has changed, e.g. MTU size.
3727 **/
3728static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
3729{
3730 struct i40e_pf *pf = vsi->back;
3731
3732 WARN_ON(in_interrupt());
3733 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
3734 usleep_range(1000, 2000);
3735 i40e_down(vsi);
3736
3737 /* Give a VF some time to respond to the reset. The
3738 * two second wait is based upon the watchdog cycle in
3739 * the VF driver.
3740 */
3741 if (vsi->type == I40E_VSI_SRIOV)
3742 msleep(2000);
3743 i40e_up(vsi);
3744 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
3745}
3746
3747/**
3748 * i40e_up - Bring the connection back up after being down
3749 * @vsi: the VSI being configured
3750 **/
3751int i40e_up(struct i40e_vsi *vsi)
3752{
3753 int err;
3754
3755 err = i40e_vsi_configure(vsi);
3756 if (!err)
3757 err = i40e_up_complete(vsi);
3758
3759 return err;
3760}
3761
3762/**
3763 * i40e_down - Shutdown the connection processing
3764 * @vsi: the VSI being stopped
3765 **/
3766void i40e_down(struct i40e_vsi *vsi)
3767{
3768 int i;
3769
3770 /* It is assumed that the caller of this function
3771 * sets the vsi->state __I40E_DOWN bit.
3772 */
3773 if (vsi->netdev) {
3774 netif_carrier_off(vsi->netdev);
3775 netif_tx_disable(vsi->netdev);
3776 }
3777 i40e_vsi_disable_irq(vsi);
3778 i40e_vsi_control_rings(vsi, false);
3779 i40e_napi_disable_all(vsi);
3780
3781 for (i = 0; i < vsi->num_queue_pairs; i++) {
3782 i40e_clean_tx_ring(&vsi->tx_rings[i]);
3783 i40e_clean_rx_ring(&vsi->rx_rings[i]);
3784 }
3785}
3786
3787/**
3788 * i40e_setup_tc - configure multiple traffic classes
3789 * @netdev: net device to configure
3790 * @tc: number of traffic classes to enable
3791 **/
3792static int i40e_setup_tc(struct net_device *netdev, u8 tc)
3793{
3794 struct i40e_netdev_priv *np = netdev_priv(netdev);
3795 struct i40e_vsi *vsi = np->vsi;
3796 struct i40e_pf *pf = vsi->back;
3797 u8 enabled_tc = 0;
3798 int ret = -EINVAL;
3799 int i;
3800
3801 /* Check if DCB enabled to continue */
3802 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
3803 netdev_info(netdev, "DCB is not enabled for adapter\n");
3804 goto exit;
3805 }
3806
3807 /* Check if MFP enabled */
3808 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
3809 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
3810 goto exit;
3811 }
3812
3813 /* Check whether tc count is within enabled limit */
3814 if (tc > i40e_pf_get_num_tc(pf)) {
3815 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
3816 goto exit;
3817 }
3818
3819 /* Generate TC map for number of tc requested */
3820 for (i = 0; i < tc; i++)
3821 enabled_tc |= (1 << i);
3822
3823 /* Requesting same TC configuration as already enabled */
3824 if (enabled_tc == vsi->tc_config.enabled_tc)
3825 return 0;
3826
3827 /* Quiesce VSI queues */
3828 i40e_quiesce_vsi(vsi);
3829
3830 /* Configure VSI for enabled TCs */
3831 ret = i40e_vsi_config_tc(vsi, enabled_tc);
3832 if (ret) {
3833 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
3834 vsi->seid);
3835 goto exit;
3836 }
3837
3838 /* Unquiesce VSI */
3839 i40e_unquiesce_vsi(vsi);
3840
3841exit:
3842 return ret;
3843}
3844
3845/**
3846 * i40e_open - Called when a network interface is made active
3847 * @netdev: network interface device structure
3848 *
3849 * The open entry point is called when a network interface is made
3850 * active by the system (IFF_UP). At this point all resources needed
3851 * for transmit and receive operations are allocated, the interrupt
3852 * handler is registered with the OS, the netdev watchdog subtask is
3853 * enabled, and the stack is notified that the interface is ready.
3854 *
3855 * Returns 0 on success, negative value on failure
3856 **/
3857static int i40e_open(struct net_device *netdev)
3858{
3859 struct i40e_netdev_priv *np = netdev_priv(netdev);
3860 struct i40e_vsi *vsi = np->vsi;
3861 struct i40e_pf *pf = vsi->back;
3862 char int_name[IFNAMSIZ];
3863 int err;
3864
3865 /* disallow open during test */
3866 if (test_bit(__I40E_TESTING, &pf->state))
3867 return -EBUSY;
3868
3869 netif_carrier_off(netdev);
3870
3871 /* allocate descriptors */
3872 err = i40e_vsi_setup_tx_resources(vsi);
3873 if (err)
3874 goto err_setup_tx;
3875 err = i40e_vsi_setup_rx_resources(vsi);
3876 if (err)
3877 goto err_setup_rx;
3878
3879 err = i40e_vsi_configure(vsi);
3880 if (err)
3881 goto err_setup_rx;
3882
3883 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
3884 dev_driver_string(&pf->pdev->dev), netdev->name);
3885 err = i40e_vsi_request_irq(vsi, int_name);
3886 if (err)
3887 goto err_setup_rx;
3888
3889 err = i40e_up_complete(vsi);
3890 if (err)
3891 goto err_up_complete;
3892
3893 if ((vsi->type == I40E_VSI_MAIN) || (vsi->type == I40E_VSI_VMDQ2)) {
3894 err = i40e_aq_set_vsi_broadcast(&pf->hw, vsi->seid, true, NULL);
3895 if (err)
3896 netdev_info(netdev,
3897 "couldn't set broadcast err %d aq_err %d\n",
3898 err, pf->hw.aq.asq_last_status);
3899 }
3900
3901 return 0;
3902
3903err_up_complete:
3904 i40e_down(vsi);
3905 i40e_vsi_free_irq(vsi);
3906err_setup_rx:
3907 i40e_vsi_free_rx_resources(vsi);
3908err_setup_tx:
3909 i40e_vsi_free_tx_resources(vsi);
3910 if (vsi == pf->vsi[pf->lan_vsi])
3911 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
3912
3913 return err;
3914}
3915
3916/**
3917 * i40e_close - Disables a network interface
3918 * @netdev: network interface device structure
3919 *
3920 * The close entry point is called when an interface is de-activated
3921 * by the OS. The hardware is still under the driver's control, but
3922 * this netdev interface is disabled.
3923 *
3924 * Returns 0, this is not allowed to fail
3925 **/
3926static int i40e_close(struct net_device *netdev)
3927{
3928 struct i40e_netdev_priv *np = netdev_priv(netdev);
3929 struct i40e_vsi *vsi = np->vsi;
3930
3931 if (test_and_set_bit(__I40E_DOWN, &vsi->state))
3932 return 0;
3933
3934 i40e_down(vsi);
3935 i40e_vsi_free_irq(vsi);
3936
3937 i40e_vsi_free_tx_resources(vsi);
3938 i40e_vsi_free_rx_resources(vsi);
3939
3940 return 0;
3941}
3942
3943/**
3944 * i40e_do_reset - Start a PF or Core Reset sequence
3945 * @pf: board private structure
3946 * @reset_flags: which reset is requested
3947 *
3948 * The essential difference in resets is that the PF Reset
3949 * doesn't clear the packet buffers, doesn't reset the PE
3950 * firmware, and doesn't bother the other PFs on the chip.
3951 **/
3952void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
3953{
3954 u32 val;
3955
3956 WARN_ON(in_interrupt());
3957
3958 /* do the biggest reset indicated */
3959 if (reset_flags & (1 << __I40E_GLOBAL_RESET_REQUESTED)) {
3960
3961 /* Request a Global Reset
3962 *
3963 * This will start the chip's countdown to the actual full
3964 * chip reset event, and a warning interrupt to be sent
3965 * to all PFs, including the requestor. Our handler
3966 * for the warning interrupt will deal with the shutdown
3967 * and recovery of the switch setup.
3968 */
3969 dev_info(&pf->pdev->dev, "GlobalR requested\n");
3970 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
3971 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
3972 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
3973
3974 } else if (reset_flags & (1 << __I40E_CORE_RESET_REQUESTED)) {
3975
3976 /* Request a Core Reset
3977 *
3978 * Same as Global Reset, except does *not* include the MAC/PHY
3979 */
3980 dev_info(&pf->pdev->dev, "CoreR requested\n");
3981 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
3982 val |= I40E_GLGEN_RTRIG_CORER_MASK;
3983 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
3984 i40e_flush(&pf->hw);
3985
3986 } else if (reset_flags & (1 << __I40E_PF_RESET_REQUESTED)) {
3987
3988 /* Request a PF Reset
3989 *
3990 * Resets only the PF-specific registers
3991 *
3992 * This goes directly to the tear-down and rebuild of
3993 * the switch, since we need to do all the recovery as
3994 * for the Core Reset.
3995 */
3996 dev_info(&pf->pdev->dev, "PFR requested\n");
3997 i40e_handle_reset_warning(pf);
3998
3999 } else if (reset_flags & (1 << __I40E_REINIT_REQUESTED)) {
4000 int v;
4001
4002 /* Find the VSI(s) that requested a re-init */
4003 dev_info(&pf->pdev->dev,
4004 "VSI reinit requested\n");
4005 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4006 struct i40e_vsi *vsi = pf->vsi[v];
4007 if (vsi != NULL &&
4008 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
4009 i40e_vsi_reinit_locked(pf->vsi[v]);
4010 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
4011 }
4012 }
4013
4014 /* no further action needed, so return now */
4015 return;
4016 } else {
4017 dev_info(&pf->pdev->dev,
4018 "bad reset request 0x%08x\n", reset_flags);
4019 return;
4020 }
4021}
4022
4023/**
4024 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
4025 * @pf: board private structure
4026 * @e: event info posted on ARQ
4027 *
4028 * Handler for LAN Queue Overflow Event generated by the firmware for PF
4029 * and VF queues
4030 **/
4031static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
4032 struct i40e_arq_event_info *e)
4033{
4034 struct i40e_aqc_lan_overflow *data =
4035 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
4036 u32 queue = le32_to_cpu(data->prtdcb_rupto);
4037 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
4038 struct i40e_hw *hw = &pf->hw;
4039 struct i40e_vf *vf;
4040 u16 vf_id;
4041
4042 dev_info(&pf->pdev->dev, "%s: Rx Queue Number = %d QTX_CTL=0x%08x\n",
4043 __func__, queue, qtx_ctl);
4044
4045 /* Queue belongs to VF, find the VF and issue VF reset */
4046 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
4047 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
4048 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
4049 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
4050 vf_id -= hw->func_caps.vf_base_id;
4051 vf = &pf->vf[vf_id];
4052 i40e_vc_notify_vf_reset(vf);
4053 /* Allow VF to process pending reset notification */
4054 msleep(20);
4055 i40e_reset_vf(vf, false);
4056 }
4057}
4058
4059/**
4060 * i40e_service_event_complete - Finish up the service event
4061 * @pf: board private structure
4062 **/
4063static void i40e_service_event_complete(struct i40e_pf *pf)
4064{
4065 BUG_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
4066
4067 /* flush memory to make sure state is correct before next watchog */
4068 smp_mb__before_clear_bit();
4069 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
4070}
4071
4072/**
4073 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
4074 * @pf: board private structure
4075 **/
4076static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
4077{
4078 if (!(pf->flags & I40E_FLAG_FDIR_REQUIRES_REINIT))
4079 return;
4080
4081 pf->flags &= ~I40E_FLAG_FDIR_REQUIRES_REINIT;
4082
4083 /* if interface is down do nothing */
4084 if (test_bit(__I40E_DOWN, &pf->state))
4085 return;
4086}
4087
4088/**
4089 * i40e_vsi_link_event - notify VSI of a link event
4090 * @vsi: vsi to be notified
4091 * @link_up: link up or down
4092 **/
4093static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
4094{
4095 if (!vsi)
4096 return;
4097
4098 switch (vsi->type) {
4099 case I40E_VSI_MAIN:
4100 if (!vsi->netdev || !vsi->netdev_registered)
4101 break;
4102
4103 if (link_up) {
4104 netif_carrier_on(vsi->netdev);
4105 netif_tx_wake_all_queues(vsi->netdev);
4106 } else {
4107 netif_carrier_off(vsi->netdev);
4108 netif_tx_stop_all_queues(vsi->netdev);
4109 }
4110 break;
4111
4112 case I40E_VSI_SRIOV:
4113 break;
4114
4115 case I40E_VSI_VMDQ2:
4116 case I40E_VSI_CTRL:
4117 case I40E_VSI_MIRROR:
4118 default:
4119 /* there is no notification for other VSIs */
4120 break;
4121 }
4122}
4123
4124/**
4125 * i40e_veb_link_event - notify elements on the veb of a link event
4126 * @veb: veb to be notified
4127 * @link_up: link up or down
4128 **/
4129static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
4130{
4131 struct i40e_pf *pf;
4132 int i;
4133
4134 if (!veb || !veb->pf)
4135 return;
4136 pf = veb->pf;
4137
4138 /* depth first... */
4139 for (i = 0; i < I40E_MAX_VEB; i++)
4140 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
4141 i40e_veb_link_event(pf->veb[i], link_up);
4142
4143 /* ... now the local VSIs */
4144 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
4145 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
4146 i40e_vsi_link_event(pf->vsi[i], link_up);
4147}
4148
4149/**
4150 * i40e_link_event - Update netif_carrier status
4151 * @pf: board private structure
4152 **/
4153static void i40e_link_event(struct i40e_pf *pf)
4154{
4155 bool new_link, old_link;
4156
4157 new_link = (pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP);
4158 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
4159
4160 if (new_link == old_link)
4161 return;
4162
Anjali Singhai6d779b412013-09-28 06:00:02 +00004163 if (!test_bit(__I40E_DOWN, &pf->vsi[pf->lan_vsi]->state))
4164 netdev_info(pf->vsi[pf->lan_vsi]->netdev,
4165 "NIC Link is %s\n", (new_link ? "Up" : "Down"));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004166
4167 /* Notify the base of the switch tree connected to
4168 * the link. Floating VEBs are not notified.
4169 */
4170 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
4171 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
4172 else
4173 i40e_vsi_link_event(pf->vsi[pf->lan_vsi], new_link);
4174
4175 if (pf->vf)
4176 i40e_vc_notify_link_state(pf);
4177}
4178
4179/**
4180 * i40e_check_hang_subtask - Check for hung queues and dropped interrupts
4181 * @pf: board private structure
4182 *
4183 * Set the per-queue flags to request a check for stuck queues in the irq
4184 * clean functions, then force interrupts to be sure the irq clean is called.
4185 **/
4186static void i40e_check_hang_subtask(struct i40e_pf *pf)
4187{
4188 int i, v;
4189
4190 /* If we're down or resetting, just bail */
4191 if (test_bit(__I40E_CONFIG_BUSY, &pf->state))
4192 return;
4193
4194 /* for each VSI/netdev
4195 * for each Tx queue
4196 * set the check flag
4197 * for each q_vector
4198 * force an interrupt
4199 */
4200 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4201 struct i40e_vsi *vsi = pf->vsi[v];
4202 int armed = 0;
4203
4204 if (!pf->vsi[v] ||
4205 test_bit(__I40E_DOWN, &vsi->state) ||
4206 (vsi->netdev && !netif_carrier_ok(vsi->netdev)))
4207 continue;
4208
4209 for (i = 0; i < vsi->num_queue_pairs; i++) {
4210 set_check_for_tx_hang(&vsi->tx_rings[i]);
4211 if (test_bit(__I40E_HANG_CHECK_ARMED,
4212 &vsi->tx_rings[i].state))
4213 armed++;
4214 }
4215
4216 if (armed) {
4217 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
4218 wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0,
4219 (I40E_PFINT_DYN_CTL0_INTENA_MASK |
4220 I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK));
4221 } else {
4222 u16 vec = vsi->base_vector - 1;
4223 u32 val = (I40E_PFINT_DYN_CTLN_INTENA_MASK |
4224 I40E_PFINT_DYN_CTLN_SWINT_TRIG_MASK);
4225 for (i = 0; i < vsi->num_q_vectors; i++, vec++)
4226 wr32(&vsi->back->hw,
4227 I40E_PFINT_DYN_CTLN(vec), val);
4228 }
4229 i40e_flush(&vsi->back->hw);
4230 }
4231 }
4232}
4233
4234/**
4235 * i40e_watchdog_subtask - Check and bring link up
4236 * @pf: board private structure
4237 **/
4238static void i40e_watchdog_subtask(struct i40e_pf *pf)
4239{
4240 int i;
4241
4242 /* if interface is down do nothing */
4243 if (test_bit(__I40E_DOWN, &pf->state) ||
4244 test_bit(__I40E_CONFIG_BUSY, &pf->state))
4245 return;
4246
4247 /* Update the stats for active netdevs so the network stack
4248 * can look at updated numbers whenever it cares to
4249 */
4250 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
4251 if (pf->vsi[i] && pf->vsi[i]->netdev)
4252 i40e_update_stats(pf->vsi[i]);
4253
4254 /* Update the stats for the active switching components */
4255 for (i = 0; i < I40E_MAX_VEB; i++)
4256 if (pf->veb[i])
4257 i40e_update_veb_stats(pf->veb[i]);
4258}
4259
4260/**
4261 * i40e_reset_subtask - Set up for resetting the device and driver
4262 * @pf: board private structure
4263 **/
4264static void i40e_reset_subtask(struct i40e_pf *pf)
4265{
4266 u32 reset_flags = 0;
4267
4268 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
4269 reset_flags |= (1 << __I40E_REINIT_REQUESTED);
4270 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
4271 }
4272 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
4273 reset_flags |= (1 << __I40E_PF_RESET_REQUESTED);
4274 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
4275 }
4276 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
4277 reset_flags |= (1 << __I40E_CORE_RESET_REQUESTED);
4278 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
4279 }
4280 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
4281 reset_flags |= (1 << __I40E_GLOBAL_RESET_REQUESTED);
4282 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
4283 }
4284
4285 /* If there's a recovery already waiting, it takes
4286 * precedence before starting a new reset sequence.
4287 */
4288 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
4289 i40e_handle_reset_warning(pf);
4290 return;
4291 }
4292
4293 /* If we're already down or resetting, just bail */
4294 if (reset_flags &&
4295 !test_bit(__I40E_DOWN, &pf->state) &&
4296 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
4297 i40e_do_reset(pf, reset_flags);
4298}
4299
4300/**
4301 * i40e_handle_link_event - Handle link event
4302 * @pf: board private structure
4303 * @e: event info posted on ARQ
4304 **/
4305static void i40e_handle_link_event(struct i40e_pf *pf,
4306 struct i40e_arq_event_info *e)
4307{
4308 struct i40e_hw *hw = &pf->hw;
4309 struct i40e_aqc_get_link_status *status =
4310 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
4311 struct i40e_link_status *hw_link_info = &hw->phy.link_info;
4312
4313 /* save off old link status information */
4314 memcpy(&pf->hw.phy.link_info_old, hw_link_info,
4315 sizeof(pf->hw.phy.link_info_old));
4316
4317 /* update link status */
4318 hw_link_info->phy_type = (enum i40e_aq_phy_type)status->phy_type;
4319 hw_link_info->link_speed = (enum i40e_aq_link_speed)status->link_speed;
4320 hw_link_info->link_info = status->link_info;
4321 hw_link_info->an_info = status->an_info;
4322 hw_link_info->ext_info = status->ext_info;
4323 hw_link_info->lse_enable =
4324 le16_to_cpu(status->command_flags) &
4325 I40E_AQ_LSE_ENABLE;
4326
4327 /* process the event */
4328 i40e_link_event(pf);
4329
4330 /* Do a new status request to re-enable LSE reporting
4331 * and load new status information into the hw struct,
4332 * then see if the status changed while processing the
4333 * initial event.
4334 */
4335 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
4336 i40e_link_event(pf);
4337}
4338
4339/**
4340 * i40e_clean_adminq_subtask - Clean the AdminQ rings
4341 * @pf: board private structure
4342 **/
4343static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
4344{
4345 struct i40e_arq_event_info event;
4346 struct i40e_hw *hw = &pf->hw;
4347 u16 pending, i = 0;
4348 i40e_status ret;
4349 u16 opcode;
4350 u32 val;
4351
4352 if (!test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state))
4353 return;
4354
4355 event.msg_size = I40E_MAX_AQ_BUF_SIZE;
4356 event.msg_buf = kzalloc(event.msg_size, GFP_KERNEL);
4357 if (!event.msg_buf)
4358 return;
4359
4360 do {
4361 ret = i40e_clean_arq_element(hw, &event, &pending);
4362 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
4363 dev_info(&pf->pdev->dev, "No ARQ event found\n");
4364 break;
4365 } else if (ret) {
4366 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
4367 break;
4368 }
4369
4370 opcode = le16_to_cpu(event.desc.opcode);
4371 switch (opcode) {
4372
4373 case i40e_aqc_opc_get_link_status:
4374 i40e_handle_link_event(pf, &event);
4375 break;
4376 case i40e_aqc_opc_send_msg_to_pf:
4377 ret = i40e_vc_process_vf_msg(pf,
4378 le16_to_cpu(event.desc.retval),
4379 le32_to_cpu(event.desc.cookie_high),
4380 le32_to_cpu(event.desc.cookie_low),
4381 event.msg_buf,
4382 event.msg_size);
4383 break;
4384 case i40e_aqc_opc_lldp_update_mib:
4385 dev_info(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
4386 break;
4387 case i40e_aqc_opc_event_lan_overflow:
4388 dev_info(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
4389 i40e_handle_lan_overflow_event(pf, &event);
4390 break;
4391 default:
4392 dev_info(&pf->pdev->dev,
4393 "ARQ Error: Unknown event %d received\n",
4394 event.desc.opcode);
4395 break;
4396 }
4397 } while (pending && (i++ < pf->adminq_work_limit));
4398
4399 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
4400 /* re-enable Admin queue interrupt cause */
4401 val = rd32(hw, I40E_PFINT_ICR0_ENA);
4402 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4403 wr32(hw, I40E_PFINT_ICR0_ENA, val);
4404 i40e_flush(hw);
4405
4406 kfree(event.msg_buf);
4407}
4408
4409/**
4410 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
4411 * @veb: pointer to the VEB instance
4412 *
4413 * This is a recursive function that first builds the attached VSIs then
4414 * recurses in to build the next layer of VEB. We track the connections
4415 * through our own index numbers because the seid's from the HW could
4416 * change across the reset.
4417 **/
4418static int i40e_reconstitute_veb(struct i40e_veb *veb)
4419{
4420 struct i40e_vsi *ctl_vsi = NULL;
4421 struct i40e_pf *pf = veb->pf;
4422 int v, veb_idx;
4423 int ret;
4424
4425 /* build VSI that owns this VEB, temporarily attached to base VEB */
4426 for (v = 0; v < pf->hw.func_caps.num_vsis && !ctl_vsi; v++) {
4427 if (pf->vsi[v] &&
4428 pf->vsi[v]->veb_idx == veb->idx &&
4429 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
4430 ctl_vsi = pf->vsi[v];
4431 break;
4432 }
4433 }
4434 if (!ctl_vsi) {
4435 dev_info(&pf->pdev->dev,
4436 "missing owner VSI for veb_idx %d\n", veb->idx);
4437 ret = -ENOENT;
4438 goto end_reconstitute;
4439 }
4440 if (ctl_vsi != pf->vsi[pf->lan_vsi])
4441 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
4442 ret = i40e_add_vsi(ctl_vsi);
4443 if (ret) {
4444 dev_info(&pf->pdev->dev,
4445 "rebuild of owner VSI failed: %d\n", ret);
4446 goto end_reconstitute;
4447 }
4448 i40e_vsi_reset_stats(ctl_vsi);
4449
4450 /* create the VEB in the switch and move the VSI onto the VEB */
4451 ret = i40e_add_veb(veb, ctl_vsi);
4452 if (ret)
4453 goto end_reconstitute;
4454
4455 /* create the remaining VSIs attached to this VEB */
4456 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4457 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
4458 continue;
4459
4460 if (pf->vsi[v]->veb_idx == veb->idx) {
4461 struct i40e_vsi *vsi = pf->vsi[v];
4462 vsi->uplink_seid = veb->seid;
4463 ret = i40e_add_vsi(vsi);
4464 if (ret) {
4465 dev_info(&pf->pdev->dev,
4466 "rebuild of vsi_idx %d failed: %d\n",
4467 v, ret);
4468 goto end_reconstitute;
4469 }
4470 i40e_vsi_reset_stats(vsi);
4471 }
4472 }
4473
4474 /* create any VEBs attached to this VEB - RECURSION */
4475 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
4476 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
4477 pf->veb[veb_idx]->uplink_seid = veb->seid;
4478 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
4479 if (ret)
4480 break;
4481 }
4482 }
4483
4484end_reconstitute:
4485 return ret;
4486}
4487
4488/**
4489 * i40e_get_capabilities - get info about the HW
4490 * @pf: the PF struct
4491 **/
4492static int i40e_get_capabilities(struct i40e_pf *pf)
4493{
4494 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
4495 u16 data_size;
4496 int buf_len;
4497 int err;
4498
4499 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
4500 do {
4501 cap_buf = kzalloc(buf_len, GFP_KERNEL);
4502 if (!cap_buf)
4503 return -ENOMEM;
4504
4505 /* this loads the data into the hw struct for us */
4506 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
4507 &data_size,
4508 i40e_aqc_opc_list_func_capabilities,
4509 NULL);
4510 /* data loaded, buffer no longer needed */
4511 kfree(cap_buf);
4512
4513 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
4514 /* retry with a larger buffer */
4515 buf_len = data_size;
4516 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
4517 dev_info(&pf->pdev->dev,
4518 "capability discovery failed: aq=%d\n",
4519 pf->hw.aq.asq_last_status);
4520 return -ENODEV;
4521 }
4522 } while (err);
4523
4524 if (pf->hw.debug_mask & I40E_DEBUG_USER)
4525 dev_info(&pf->pdev->dev,
4526 "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",
4527 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
4528 pf->hw.func_caps.num_msix_vectors,
4529 pf->hw.func_caps.num_msix_vectors_vf,
4530 pf->hw.func_caps.fd_filters_guaranteed,
4531 pf->hw.func_caps.fd_filters_best_effort,
4532 pf->hw.func_caps.num_tx_qp,
4533 pf->hw.func_caps.num_vsis);
4534
4535 return 0;
4536}
4537
4538/**
4539 * i40e_fdir_setup - initialize the Flow Director resources
4540 * @pf: board private structure
4541 **/
4542static void i40e_fdir_setup(struct i40e_pf *pf)
4543{
4544 struct i40e_vsi *vsi;
4545 bool new_vsi = false;
4546 int err, i;
4547
4548 if (!(pf->flags & (I40E_FLAG_FDIR_ENABLED|I40E_FLAG_FDIR_ATR_ENABLED)))
4549 return;
4550
4551 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
4552
4553 /* find existing or make new FDIR VSI */
4554 vsi = NULL;
4555 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
4556 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR)
4557 vsi = pf->vsi[i];
4558 if (!vsi) {
4559 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR, pf->mac_seid, 0);
4560 if (!vsi) {
4561 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
4562 pf->flags &= ~I40E_FLAG_FDIR_ENABLED;
4563 return;
4564 }
4565 new_vsi = true;
4566 }
4567 WARN_ON(vsi->base_queue != I40E_FDIR_RING);
4568 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_rings);
4569
4570 err = i40e_vsi_setup_tx_resources(vsi);
4571 if (!err)
4572 err = i40e_vsi_setup_rx_resources(vsi);
4573 if (!err)
4574 err = i40e_vsi_configure(vsi);
4575 if (!err && new_vsi) {
4576 char int_name[IFNAMSIZ + 9];
4577 snprintf(int_name, sizeof(int_name) - 1, "%s-fdir",
4578 dev_driver_string(&pf->pdev->dev));
4579 err = i40e_vsi_request_irq(vsi, int_name);
4580 }
4581 if (!err)
4582 err = i40e_up_complete(vsi);
4583
4584 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
4585}
4586
4587/**
4588 * i40e_fdir_teardown - release the Flow Director resources
4589 * @pf: board private structure
4590 **/
4591static void i40e_fdir_teardown(struct i40e_pf *pf)
4592{
4593 int i;
4594
4595 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
4596 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
4597 i40e_vsi_release(pf->vsi[i]);
4598 break;
4599 }
4600 }
4601}
4602
4603/**
4604 * i40e_handle_reset_warning - prep for the core to reset
4605 * @pf: board private structure
4606 *
4607 * Close up the VFs and other things in prep for a Core Reset,
4608 * then get ready to rebuild the world.
4609 **/
4610static void i40e_handle_reset_warning(struct i40e_pf *pf)
4611{
4612 struct i40e_driver_version dv;
4613 struct i40e_hw *hw = &pf->hw;
4614 i40e_status ret;
4615 u32 v;
4616
4617 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
4618 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
4619 return;
4620
4621 dev_info(&pf->pdev->dev, "Tearing down internal switch for reset\n");
4622
4623 i40e_vc_notify_reset(pf);
4624
4625 /* quiesce the VSIs and their queues that are not already DOWN */
4626 i40e_pf_quiesce_all_vsi(pf);
4627
4628 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4629 if (pf->vsi[v])
4630 pf->vsi[v]->seid = 0;
4631 }
4632
4633 i40e_shutdown_adminq(&pf->hw);
4634
4635 /* Now we wait for GRST to settle out.
4636 * We don't have to delete the VEBs or VSIs from the hw switch
4637 * because the reset will make them disappear.
4638 */
4639 ret = i40e_pf_reset(hw);
4640 if (ret)
4641 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
4642 pf->pfr_count++;
4643
4644 if (test_bit(__I40E_DOWN, &pf->state))
4645 goto end_core_reset;
4646 dev_info(&pf->pdev->dev, "Rebuilding internal switch\n");
4647
4648 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
4649 ret = i40e_init_adminq(&pf->hw);
4650 if (ret) {
4651 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, %d\n", ret);
4652 goto end_core_reset;
4653 }
4654
4655 ret = i40e_get_capabilities(pf);
4656 if (ret) {
4657 dev_info(&pf->pdev->dev, "i40e_get_capabilities failed, %d\n",
4658 ret);
4659 goto end_core_reset;
4660 }
4661
4662 /* call shutdown HMC */
4663 ret = i40e_shutdown_lan_hmc(hw);
4664 if (ret) {
4665 dev_info(&pf->pdev->dev, "shutdown_lan_hmc failed: %d\n", ret);
4666 goto end_core_reset;
4667 }
4668
4669 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
4670 hw->func_caps.num_rx_qp,
4671 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
4672 if (ret) {
4673 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
4674 goto end_core_reset;
4675 }
4676 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
4677 if (ret) {
4678 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
4679 goto end_core_reset;
4680 }
4681
4682 /* do basic switch setup */
4683 ret = i40e_setup_pf_switch(pf);
4684 if (ret)
4685 goto end_core_reset;
4686
4687 /* Rebuild the VSIs and VEBs that existed before reset.
4688 * They are still in our local switch element arrays, so only
4689 * need to rebuild the switch model in the HW.
4690 *
4691 * If there were VEBs but the reconstitution failed, we'll try
4692 * try to recover minimal use by getting the basic PF VSI working.
4693 */
4694 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
4695 dev_info(&pf->pdev->dev, "attempting to rebuild switch\n");
4696 /* find the one VEB connected to the MAC, and find orphans */
4697 for (v = 0; v < I40E_MAX_VEB; v++) {
4698 if (!pf->veb[v])
4699 continue;
4700
4701 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
4702 pf->veb[v]->uplink_seid == 0) {
4703 ret = i40e_reconstitute_veb(pf->veb[v]);
4704
4705 if (!ret)
4706 continue;
4707
4708 /* If Main VEB failed, we're in deep doodoo,
4709 * so give up rebuilding the switch and set up
4710 * for minimal rebuild of PF VSI.
4711 * If orphan failed, we'll report the error
4712 * but try to keep going.
4713 */
4714 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
4715 dev_info(&pf->pdev->dev,
4716 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
4717 ret);
4718 pf->vsi[pf->lan_vsi]->uplink_seid
4719 = pf->mac_seid;
4720 break;
4721 } else if (pf->veb[v]->uplink_seid == 0) {
4722 dev_info(&pf->pdev->dev,
4723 "rebuild of orphan VEB failed: %d\n",
4724 ret);
4725 }
4726 }
4727 }
4728 }
4729
4730 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
4731 dev_info(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
4732 /* no VEB, so rebuild only the Main VSI */
4733 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
4734 if (ret) {
4735 dev_info(&pf->pdev->dev,
4736 "rebuild of Main VSI failed: %d\n", ret);
4737 goto end_core_reset;
4738 }
4739 }
4740
4741 /* reinit the misc interrupt */
4742 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4743 ret = i40e_setup_misc_vector(pf);
4744
4745 /* restart the VSIs that were rebuilt and running before the reset */
4746 i40e_pf_unquiesce_all_vsi(pf);
4747
4748 /* tell the firmware that we're starting */
4749 dv.major_version = DRV_VERSION_MAJOR;
4750 dv.minor_version = DRV_VERSION_MINOR;
4751 dv.build_version = DRV_VERSION_BUILD;
4752 dv.subbuild_version = 0;
4753 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
4754
4755 dev_info(&pf->pdev->dev, "PF reset done\n");
4756
4757end_core_reset:
4758 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
4759}
4760
4761/**
4762 * i40e_handle_mdd_event
4763 * @pf: pointer to the pf structure
4764 *
4765 * Called from the MDD irq handler to identify possibly malicious vfs
4766 **/
4767static void i40e_handle_mdd_event(struct i40e_pf *pf)
4768{
4769 struct i40e_hw *hw = &pf->hw;
4770 bool mdd_detected = false;
4771 struct i40e_vf *vf;
4772 u32 reg;
4773 int i;
4774
4775 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
4776 return;
4777
4778 /* find what triggered the MDD event */
4779 reg = rd32(hw, I40E_GL_MDET_TX);
4780 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
4781 u8 func = (reg & I40E_GL_MDET_TX_FUNCTION_MASK)
4782 >> I40E_GL_MDET_TX_FUNCTION_SHIFT;
4783 u8 event = (reg & I40E_GL_MDET_TX_EVENT_SHIFT)
4784 >> I40E_GL_MDET_TX_EVENT_SHIFT;
4785 u8 queue = (reg & I40E_GL_MDET_TX_QUEUE_MASK)
4786 >> I40E_GL_MDET_TX_QUEUE_SHIFT;
4787 dev_info(&pf->pdev->dev,
4788 "Malicious Driver Detection TX event 0x%02x on q %d of function 0x%02x\n",
4789 event, queue, func);
4790 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
4791 mdd_detected = true;
4792 }
4793 reg = rd32(hw, I40E_GL_MDET_RX);
4794 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
4795 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK)
4796 >> I40E_GL_MDET_RX_FUNCTION_SHIFT;
4797 u8 event = (reg & I40E_GL_MDET_RX_EVENT_SHIFT)
4798 >> I40E_GL_MDET_RX_EVENT_SHIFT;
4799 u8 queue = (reg & I40E_GL_MDET_RX_QUEUE_MASK)
4800 >> I40E_GL_MDET_RX_QUEUE_SHIFT;
4801 dev_info(&pf->pdev->dev,
4802 "Malicious Driver Detection RX event 0x%02x on q %d of function 0x%02x\n",
4803 event, queue, func);
4804 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
4805 mdd_detected = true;
4806 }
4807
4808 /* see if one of the VFs needs its hand slapped */
4809 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
4810 vf = &(pf->vf[i]);
4811 reg = rd32(hw, I40E_VP_MDET_TX(i));
4812 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
4813 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
4814 vf->num_mdd_events++;
4815 dev_info(&pf->pdev->dev, "MDD TX event on VF %d\n", i);
4816 }
4817
4818 reg = rd32(hw, I40E_VP_MDET_RX(i));
4819 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
4820 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
4821 vf->num_mdd_events++;
4822 dev_info(&pf->pdev->dev, "MDD RX event on VF %d\n", i);
4823 }
4824
4825 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
4826 dev_info(&pf->pdev->dev,
4827 "Too many MDD events on VF %d, disabled\n", i);
4828 dev_info(&pf->pdev->dev,
4829 "Use PF Control I/F to re-enable the VF\n");
4830 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
4831 }
4832 }
4833
4834 /* re-enable mdd interrupt cause */
4835 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
4836 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4837 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4838 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4839 i40e_flush(hw);
4840}
4841
4842/**
4843 * i40e_service_task - Run the driver's async subtasks
4844 * @work: pointer to work_struct containing our data
4845 **/
4846static void i40e_service_task(struct work_struct *work)
4847{
4848 struct i40e_pf *pf = container_of(work,
4849 struct i40e_pf,
4850 service_task);
4851 unsigned long start_time = jiffies;
4852
4853 i40e_reset_subtask(pf);
4854 i40e_handle_mdd_event(pf);
4855 i40e_vc_process_vflr_event(pf);
4856 i40e_watchdog_subtask(pf);
4857 i40e_fdir_reinit_subtask(pf);
4858 i40e_check_hang_subtask(pf);
4859 i40e_sync_filters_subtask(pf);
4860 i40e_clean_adminq_subtask(pf);
4861
4862 i40e_service_event_complete(pf);
4863
4864 /* If the tasks have taken longer than one timer cycle or there
4865 * is more work to be done, reschedule the service task now
4866 * rather than wait for the timer to tick again.
4867 */
4868 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
4869 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
4870 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
4871 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
4872 i40e_service_event_schedule(pf);
4873}
4874
4875/**
4876 * i40e_service_timer - timer callback
4877 * @data: pointer to PF struct
4878 **/
4879static void i40e_service_timer(unsigned long data)
4880{
4881 struct i40e_pf *pf = (struct i40e_pf *)data;
4882
4883 mod_timer(&pf->service_timer,
4884 round_jiffies(jiffies + pf->service_timer_period));
4885 i40e_service_event_schedule(pf);
4886}
4887
4888/**
4889 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
4890 * @vsi: the VSI being configured
4891 **/
4892static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
4893{
4894 struct i40e_pf *pf = vsi->back;
4895
4896 switch (vsi->type) {
4897 case I40E_VSI_MAIN:
4898 vsi->alloc_queue_pairs = pf->num_lan_qps;
4899 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
4900 I40E_REQ_DESCRIPTOR_MULTIPLE);
4901 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4902 vsi->num_q_vectors = pf->num_lan_msix;
4903 else
4904 vsi->num_q_vectors = 1;
4905
4906 break;
4907
4908 case I40E_VSI_FDIR:
4909 vsi->alloc_queue_pairs = 1;
4910 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
4911 I40E_REQ_DESCRIPTOR_MULTIPLE);
4912 vsi->num_q_vectors = 1;
4913 break;
4914
4915 case I40E_VSI_VMDQ2:
4916 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
4917 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
4918 I40E_REQ_DESCRIPTOR_MULTIPLE);
4919 vsi->num_q_vectors = pf->num_vmdq_msix;
4920 break;
4921
4922 case I40E_VSI_SRIOV:
4923 vsi->alloc_queue_pairs = pf->num_vf_qps;
4924 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
4925 I40E_REQ_DESCRIPTOR_MULTIPLE);
4926 break;
4927
4928 default:
4929 WARN_ON(1);
4930 return -ENODATA;
4931 }
4932
4933 return 0;
4934}
4935
4936/**
4937 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
4938 * @pf: board private structure
4939 * @type: type of VSI
4940 *
4941 * On error: returns error code (negative)
4942 * On success: returns vsi index in PF (positive)
4943 **/
4944static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
4945{
4946 int ret = -ENODEV;
4947 struct i40e_vsi *vsi;
4948 int vsi_idx;
4949 int i;
4950
4951 /* Need to protect the allocation of the VSIs at the PF level */
4952 mutex_lock(&pf->switch_mutex);
4953
4954 /* VSI list may be fragmented if VSI creation/destruction has
4955 * been happening. We can afford to do a quick scan to look
4956 * for any free VSIs in the list.
4957 *
4958 * find next empty vsi slot, looping back around if necessary
4959 */
4960 i = pf->next_vsi;
4961 while (i < pf->hw.func_caps.num_vsis && pf->vsi[i])
4962 i++;
4963 if (i >= pf->hw.func_caps.num_vsis) {
4964 i = 0;
4965 while (i < pf->next_vsi && pf->vsi[i])
4966 i++;
4967 }
4968
4969 if (i < pf->hw.func_caps.num_vsis && !pf->vsi[i]) {
4970 vsi_idx = i; /* Found one! */
4971 } else {
4972 ret = -ENODEV;
4973 goto err_alloc_vsi; /* out of VSI slots! */
4974 }
4975 pf->next_vsi = ++i;
4976
4977 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
4978 if (!vsi) {
4979 ret = -ENOMEM;
4980 goto err_alloc_vsi;
4981 }
4982 vsi->type = type;
4983 vsi->back = pf;
4984 set_bit(__I40E_DOWN, &vsi->state);
4985 vsi->flags = 0;
4986 vsi->idx = vsi_idx;
4987 vsi->rx_itr_setting = pf->rx_itr_default;
4988 vsi->tx_itr_setting = pf->tx_itr_default;
4989 vsi->netdev_registered = false;
4990 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
4991 INIT_LIST_HEAD(&vsi->mac_filter_list);
4992
4993 i40e_set_num_rings_in_vsi(vsi);
4994
4995 /* Setup default MSIX irq handler for VSI */
4996 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
4997
4998 pf->vsi[vsi_idx] = vsi;
4999 ret = vsi_idx;
5000err_alloc_vsi:
5001 mutex_unlock(&pf->switch_mutex);
5002 return ret;
5003}
5004
5005/**
5006 * i40e_vsi_clear - Deallocate the VSI provided
5007 * @vsi: the VSI being un-configured
5008 **/
5009static int i40e_vsi_clear(struct i40e_vsi *vsi)
5010{
5011 struct i40e_pf *pf;
5012
5013 if (!vsi)
5014 return 0;
5015
5016 if (!vsi->back)
5017 goto free_vsi;
5018 pf = vsi->back;
5019
5020 mutex_lock(&pf->switch_mutex);
5021 if (!pf->vsi[vsi->idx]) {
5022 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
5023 vsi->idx, vsi->idx, vsi, vsi->type);
5024 goto unlock_vsi;
5025 }
5026
5027 if (pf->vsi[vsi->idx] != vsi) {
5028 dev_err(&pf->pdev->dev,
5029 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
5030 pf->vsi[vsi->idx]->idx,
5031 pf->vsi[vsi->idx],
5032 pf->vsi[vsi->idx]->type,
5033 vsi->idx, vsi, vsi->type);
5034 goto unlock_vsi;
5035 }
5036
5037 /* updates the pf for this cleared vsi */
5038 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
5039 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
5040
5041 pf->vsi[vsi->idx] = NULL;
5042 if (vsi->idx < pf->next_vsi)
5043 pf->next_vsi = vsi->idx;
5044
5045unlock_vsi:
5046 mutex_unlock(&pf->switch_mutex);
5047free_vsi:
5048 kfree(vsi);
5049
5050 return 0;
5051}
5052
5053/**
5054 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
5055 * @vsi: the VSI being configured
5056 **/
5057static int i40e_alloc_rings(struct i40e_vsi *vsi)
5058{
5059 struct i40e_pf *pf = vsi->back;
5060 int ret = 0;
5061 int i;
5062
5063 vsi->rx_rings = kcalloc(vsi->alloc_queue_pairs,
5064 sizeof(struct i40e_ring), GFP_KERNEL);
5065 if (!vsi->rx_rings) {
5066 ret = -ENOMEM;
5067 goto err_alloc_rings;
5068 }
5069
5070 vsi->tx_rings = kcalloc(vsi->alloc_queue_pairs,
5071 sizeof(struct i40e_ring), GFP_KERNEL);
5072 if (!vsi->tx_rings) {
5073 ret = -ENOMEM;
5074 kfree(vsi->rx_rings);
5075 goto err_alloc_rings;
5076 }
5077
5078 /* Set basic values in the rings to be used later during open() */
5079 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
5080 struct i40e_ring *rx_ring = &vsi->rx_rings[i];
5081 struct i40e_ring *tx_ring = &vsi->tx_rings[i];
5082
5083 tx_ring->queue_index = i;
5084 tx_ring->reg_idx = vsi->base_queue + i;
5085 tx_ring->ring_active = false;
5086 tx_ring->vsi = vsi;
5087 tx_ring->netdev = vsi->netdev;
5088 tx_ring->dev = &pf->pdev->dev;
5089 tx_ring->count = vsi->num_desc;
5090 tx_ring->size = 0;
5091 tx_ring->dcb_tc = 0;
5092
5093 rx_ring->queue_index = i;
5094 rx_ring->reg_idx = vsi->base_queue + i;
5095 rx_ring->ring_active = false;
5096 rx_ring->vsi = vsi;
5097 rx_ring->netdev = vsi->netdev;
5098 rx_ring->dev = &pf->pdev->dev;
5099 rx_ring->count = vsi->num_desc;
5100 rx_ring->size = 0;
5101 rx_ring->dcb_tc = 0;
5102 if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
5103 set_ring_16byte_desc_enabled(rx_ring);
5104 else
5105 clear_ring_16byte_desc_enabled(rx_ring);
5106 }
5107
5108err_alloc_rings:
5109 return ret;
5110}
5111
5112/**
5113 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
5114 * @vsi: the VSI being cleaned
5115 **/
5116static int i40e_vsi_clear_rings(struct i40e_vsi *vsi)
5117{
5118 if (vsi) {
5119 kfree(vsi->rx_rings);
5120 kfree(vsi->tx_rings);
5121 }
5122
5123 return 0;
5124}
5125
5126/**
5127 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
5128 * @pf: board private structure
5129 * @vectors: the number of MSI-X vectors to request
5130 *
5131 * Returns the number of vectors reserved, or error
5132 **/
5133static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
5134{
5135 int err = 0;
5136
5137 pf->num_msix_entries = 0;
5138 while (vectors >= I40E_MIN_MSIX) {
5139 err = pci_enable_msix(pf->pdev, pf->msix_entries, vectors);
5140 if (err == 0) {
5141 /* good to go */
5142 pf->num_msix_entries = vectors;
5143 break;
5144 } else if (err < 0) {
5145 /* total failure */
5146 dev_info(&pf->pdev->dev,
5147 "MSI-X vector reservation failed: %d\n", err);
5148 vectors = 0;
5149 break;
5150 } else {
5151 /* err > 0 is the hint for retry */
5152 dev_info(&pf->pdev->dev,
5153 "MSI-X vectors wanted %d, retrying with %d\n",
5154 vectors, err);
5155 vectors = err;
5156 }
5157 }
5158
5159 if (vectors > 0 && vectors < I40E_MIN_MSIX) {
5160 dev_info(&pf->pdev->dev,
5161 "Couldn't get enough vectors, only %d available\n",
5162 vectors);
5163 vectors = 0;
5164 }
5165
5166 return vectors;
5167}
5168
5169/**
5170 * i40e_init_msix - Setup the MSIX capability
5171 * @pf: board private structure
5172 *
5173 * Work with the OS to set up the MSIX vectors needed.
5174 *
5175 * Returns 0 on success, negative on failure
5176 **/
5177static int i40e_init_msix(struct i40e_pf *pf)
5178{
5179 i40e_status err = 0;
5180 struct i40e_hw *hw = &pf->hw;
5181 int v_budget, i;
5182 int vec;
5183
5184 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
5185 return -ENODEV;
5186
5187 /* The number of vectors we'll request will be comprised of:
5188 * - Add 1 for "other" cause for Admin Queue events, etc.
5189 * - The number of LAN queue pairs
5190 * already adjusted for the NUMA node
5191 * assumes symmetric Tx/Rx pairing
5192 * - The number of VMDq pairs
5193 * Once we count this up, try the request.
5194 *
5195 * If we can't get what we want, we'll simplify to nearly nothing
5196 * and try again. If that still fails, we punt.
5197 */
5198 pf->num_lan_msix = pf->num_lan_qps;
5199 pf->num_vmdq_msix = pf->num_vmdq_qps;
5200 v_budget = 1 + pf->num_lan_msix;
5201 v_budget += (pf->num_vmdq_vsis * pf->num_vmdq_msix);
5202 if (pf->flags & I40E_FLAG_FDIR_ENABLED)
5203 v_budget++;
5204
5205 /* Scale down if necessary, and the rings will share vectors */
5206 v_budget = min_t(int, v_budget, hw->func_caps.num_msix_vectors);
5207
5208 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
5209 GFP_KERNEL);
5210 if (!pf->msix_entries)
5211 return -ENOMEM;
5212
5213 for (i = 0; i < v_budget; i++)
5214 pf->msix_entries[i].entry = i;
5215 vec = i40e_reserve_msix_vectors(pf, v_budget);
5216 if (vec < I40E_MIN_MSIX) {
5217 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
5218 kfree(pf->msix_entries);
5219 pf->msix_entries = NULL;
5220 return -ENODEV;
5221
5222 } else if (vec == I40E_MIN_MSIX) {
5223 /* Adjust for minimal MSIX use */
5224 dev_info(&pf->pdev->dev, "Features disabled, not enough MSIX vectors\n");
5225 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
5226 pf->num_vmdq_vsis = 0;
5227 pf->num_vmdq_qps = 0;
5228 pf->num_vmdq_msix = 0;
5229 pf->num_lan_qps = 1;
5230 pf->num_lan_msix = 1;
5231
5232 } else if (vec != v_budget) {
5233 /* Scale vector usage down */
5234 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
5235 vec--; /* reserve the misc vector */
5236
5237 /* partition out the remaining vectors */
5238 switch (vec) {
5239 case 2:
5240 pf->num_vmdq_vsis = 1;
5241 pf->num_lan_msix = 1;
5242 break;
5243 case 3:
5244 pf->num_vmdq_vsis = 1;
5245 pf->num_lan_msix = 2;
5246 break;
5247 default:
5248 pf->num_lan_msix = min_t(int, (vec / 2),
5249 pf->num_lan_qps);
5250 pf->num_vmdq_vsis = min_t(int, (vec - pf->num_lan_msix),
5251 I40E_DEFAULT_NUM_VMDQ_VSI);
5252 break;
5253 }
5254 }
5255
5256 return err;
5257}
5258
5259/**
5260 * i40e_alloc_q_vectors - Allocate memory for interrupt vectors
5261 * @vsi: the VSI being configured
5262 *
5263 * We allocate one q_vector per queue interrupt. If allocation fails we
5264 * return -ENOMEM.
5265 **/
5266static int i40e_alloc_q_vectors(struct i40e_vsi *vsi)
5267{
5268 struct i40e_pf *pf = vsi->back;
5269 int v_idx, num_q_vectors;
5270
5271 /* if not MSIX, give the one vector only to the LAN VSI */
5272 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5273 num_q_vectors = vsi->num_q_vectors;
5274 else if (vsi == pf->vsi[pf->lan_vsi])
5275 num_q_vectors = 1;
5276 else
5277 return -EINVAL;
5278
5279 vsi->q_vectors = kcalloc(num_q_vectors,
5280 sizeof(struct i40e_q_vector),
5281 GFP_KERNEL);
5282 if (!vsi->q_vectors)
5283 return -ENOMEM;
5284
5285 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
5286 vsi->q_vectors[v_idx].vsi = vsi;
5287 vsi->q_vectors[v_idx].v_idx = v_idx;
5288 cpumask_set_cpu(v_idx, &vsi->q_vectors[v_idx].affinity_mask);
5289 if (vsi->netdev)
5290 netif_napi_add(vsi->netdev, &vsi->q_vectors[v_idx].napi,
5291 i40e_napi_poll, vsi->work_limit);
5292 }
5293
5294 return 0;
5295}
5296
5297/**
5298 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
5299 * @pf: board private structure to initialize
5300 **/
5301static void i40e_init_interrupt_scheme(struct i40e_pf *pf)
5302{
5303 int err = 0;
5304
5305 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
5306 err = i40e_init_msix(pf);
5307 if (err) {
5308 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
5309 I40E_FLAG_MQ_ENABLED |
5310 I40E_FLAG_DCB_ENABLED |
5311 I40E_FLAG_SRIOV_ENABLED |
5312 I40E_FLAG_FDIR_ENABLED |
5313 I40E_FLAG_FDIR_ATR_ENABLED |
5314 I40E_FLAG_VMDQ_ENABLED);
5315
5316 /* rework the queue expectations without MSIX */
5317 i40e_determine_queue_usage(pf);
5318 }
5319 }
5320
5321 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
5322 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
5323 err = pci_enable_msi(pf->pdev);
5324 if (err) {
5325 dev_info(&pf->pdev->dev,
5326 "MSI init failed (%d), trying legacy.\n", err);
5327 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
5328 }
5329 }
5330
5331 /* track first vector for misc interrupts */
5332 err = i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT-1);
5333}
5334
5335/**
5336 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
5337 * @pf: board private structure
5338 *
5339 * This sets up the handler for MSIX 0, which is used to manage the
5340 * non-queue interrupts, e.g. AdminQ and errors. This is not used
5341 * when in MSI or Legacy interrupt mode.
5342 **/
5343static int i40e_setup_misc_vector(struct i40e_pf *pf)
5344{
5345 struct i40e_hw *hw = &pf->hw;
5346 int err = 0;
5347
5348 /* Only request the irq if this is the first time through, and
5349 * not when we're rebuilding after a Reset
5350 */
5351 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
5352 err = request_irq(pf->msix_entries[0].vector,
5353 i40e_intr, 0, pf->misc_int_name, pf);
5354 if (err) {
5355 dev_info(&pf->pdev->dev,
5356 "request_irq for msix_misc failed: %d\n", err);
5357 return -EFAULT;
5358 }
5359 }
5360
5361 i40e_enable_misc_int_causes(hw);
5362
5363 /* associate no queues to the misc vector */
5364 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
5365 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
5366
5367 i40e_flush(hw);
5368
5369 i40e_irq_dynamic_enable_icr0(pf);
5370
5371 return err;
5372}
5373
5374/**
5375 * i40e_config_rss - Prepare for RSS if used
5376 * @pf: board private structure
5377 **/
5378static int i40e_config_rss(struct i40e_pf *pf)
5379{
5380 struct i40e_hw *hw = &pf->hw;
5381 u32 lut = 0;
5382 int i, j;
5383 u64 hena;
5384 /* Set of random keys generated using kernel random number generator */
5385 static const u32 seed[I40E_PFQF_HKEY_MAX_INDEX + 1] = {0x41b01687,
5386 0x183cfd8c, 0xce880440, 0x580cbc3c, 0x35897377,
5387 0x328b25e1, 0x4fa98922, 0xb7d90c14, 0xd5bad70d,
5388 0xcd15a2c1, 0xe8580225, 0x4a1e9d11, 0xfe5731be};
5389
5390 /* Fill out hash function seed */
5391 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
5392 wr32(hw, I40E_PFQF_HKEY(i), seed[i]);
5393
5394 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
5395 hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
5396 ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
5397 hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_UDP) |
5398 ((u64)1 << I40E_FILTER_PCTYPE_NONF_UNICAST_IPV4_UDP) |
5399 ((u64)1 << I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV4_UDP) |
5400 ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_TCP) |
5401 ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_TCP) |
5402 ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_UDP) |
5403 ((u64)1 << I40E_FILTER_PCTYPE_NONF_UNICAST_IPV6_UDP) |
5404 ((u64)1 << I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV6_UDP) |
5405 ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV4)|
5406 ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV6);
5407 wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
5408 wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
5409
5410 /* Populate the LUT with max no. of queues in round robin fashion */
5411 for (i = 0, j = 0; i < pf->hw.func_caps.rss_table_size; i++, j++) {
5412
5413 /* The assumption is that lan qp count will be the highest
5414 * qp count for any PF VSI that needs RSS.
5415 * If multiple VSIs need RSS support, all the qp counts
5416 * for those VSIs should be a power of 2 for RSS to work.
5417 * If LAN VSI is the only consumer for RSS then this requirement
5418 * is not necessary.
5419 */
5420 if (j == pf->rss_size)
5421 j = 0;
5422 /* lut = 4-byte sliding window of 4 lut entries */
5423 lut = (lut << 8) | (j &
5424 ((0x1 << pf->hw.func_caps.rss_table_entry_width) - 1));
5425 /* On i = 3, we have 4 entries in lut; write to the register */
5426 if ((i & 3) == 3)
5427 wr32(hw, I40E_PFQF_HLUT(i >> 2), lut);
5428 }
5429 i40e_flush(hw);
5430
5431 return 0;
5432}
5433
5434/**
5435 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
5436 * @pf: board private structure to initialize
5437 *
5438 * i40e_sw_init initializes the Adapter private data structure.
5439 * Fields are initialized based on PCI device information and
5440 * OS network device settings (MTU size).
5441 **/
5442static int i40e_sw_init(struct i40e_pf *pf)
5443{
5444 int err = 0;
5445 int size;
5446
5447 pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
5448 (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
5449 if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
5450 if (I40E_DEBUG_USER & debug)
5451 pf->hw.debug_mask = debug;
5452 pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
5453 I40E_DEFAULT_MSG_ENABLE);
5454 }
5455
5456 /* Set default capability flags */
5457 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
5458 I40E_FLAG_MSI_ENABLED |
5459 I40E_FLAG_MSIX_ENABLED |
5460 I40E_FLAG_RX_PS_ENABLED |
5461 I40E_FLAG_MQ_ENABLED |
5462 I40E_FLAG_RX_1BUF_ENABLED;
5463
5464 pf->rss_size_max = 0x1 << pf->hw.func_caps.rss_table_entry_width;
5465 if (pf->hw.func_caps.rss) {
5466 pf->flags |= I40E_FLAG_RSS_ENABLED;
5467 pf->rss_size = min_t(int, pf->rss_size_max,
5468 nr_cpus_node(numa_node_id()));
5469 } else {
5470 pf->rss_size = 1;
5471 }
5472
5473 if (pf->hw.func_caps.dcb)
5474 pf->num_tc_qps = I40E_DEFAULT_QUEUES_PER_TC;
5475 else
5476 pf->num_tc_qps = 0;
5477
5478 if (pf->hw.func_caps.fd) {
5479 /* FW/NVM is not yet fixed in this regard */
5480 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
5481 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
5482 pf->flags |= I40E_FLAG_FDIR_ATR_ENABLED;
5483 dev_info(&pf->pdev->dev,
5484 "Flow Director ATR mode Enabled\n");
5485 pf->flags |= I40E_FLAG_FDIR_ENABLED;
5486 dev_info(&pf->pdev->dev,
5487 "Flow Director Side Band mode Enabled\n");
5488 pf->fdir_pf_filter_count =
5489 pf->hw.func_caps.fd_filters_guaranteed;
5490 }
5491 } else {
5492 pf->fdir_pf_filter_count = 0;
5493 }
5494
5495 if (pf->hw.func_caps.vmdq) {
5496 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
5497 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
5498 pf->num_vmdq_qps = I40E_DEFAULT_QUEUES_PER_VMDQ;
5499 }
5500
5501 /* MFP mode enabled */
5502 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.mfp_mode_1) {
5503 pf->flags |= I40E_FLAG_MFP_ENABLED;
5504 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
5505 }
5506
5507#ifdef CONFIG_PCI_IOV
5508 if (pf->hw.func_caps.num_vfs) {
5509 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
5510 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
5511 pf->num_req_vfs = min_t(int,
5512 pf->hw.func_caps.num_vfs,
5513 I40E_MAX_VF_COUNT);
5514 }
5515#endif /* CONFIG_PCI_IOV */
5516 pf->eeprom_version = 0xDEAD;
5517 pf->lan_veb = I40E_NO_VEB;
5518 pf->lan_vsi = I40E_NO_VSI;
5519
5520 /* set up queue assignment tracking */
5521 size = sizeof(struct i40e_lump_tracking)
5522 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
5523 pf->qp_pile = kzalloc(size, GFP_KERNEL);
5524 if (!pf->qp_pile) {
5525 err = -ENOMEM;
5526 goto sw_init_done;
5527 }
5528 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
5529 pf->qp_pile->search_hint = 0;
5530
5531 /* set up vector assignment tracking */
5532 size = sizeof(struct i40e_lump_tracking)
5533 + (sizeof(u16) * pf->hw.func_caps.num_msix_vectors);
5534 pf->irq_pile = kzalloc(size, GFP_KERNEL);
5535 if (!pf->irq_pile) {
5536 kfree(pf->qp_pile);
5537 err = -ENOMEM;
5538 goto sw_init_done;
5539 }
5540 pf->irq_pile->num_entries = pf->hw.func_caps.num_msix_vectors;
5541 pf->irq_pile->search_hint = 0;
5542
5543 mutex_init(&pf->switch_mutex);
5544
5545sw_init_done:
5546 return err;
5547}
5548
5549/**
5550 * i40e_set_features - set the netdev feature flags
5551 * @netdev: ptr to the netdev being adjusted
5552 * @features: the feature set that the stack is suggesting
5553 **/
5554static int i40e_set_features(struct net_device *netdev,
5555 netdev_features_t features)
5556{
5557 struct i40e_netdev_priv *np = netdev_priv(netdev);
5558 struct i40e_vsi *vsi = np->vsi;
5559
5560 if (features & NETIF_F_HW_VLAN_CTAG_RX)
5561 i40e_vlan_stripping_enable(vsi);
5562 else
5563 i40e_vlan_stripping_disable(vsi);
5564
5565 return 0;
5566}
5567
5568static const struct net_device_ops i40e_netdev_ops = {
5569 .ndo_open = i40e_open,
5570 .ndo_stop = i40e_close,
5571 .ndo_start_xmit = i40e_lan_xmit_frame,
5572 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
5573 .ndo_set_rx_mode = i40e_set_rx_mode,
5574 .ndo_validate_addr = eth_validate_addr,
5575 .ndo_set_mac_address = i40e_set_mac,
5576 .ndo_change_mtu = i40e_change_mtu,
5577 .ndo_tx_timeout = i40e_tx_timeout,
5578 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
5579 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
5580#ifdef CONFIG_NET_POLL_CONTROLLER
5581 .ndo_poll_controller = i40e_netpoll,
5582#endif
5583 .ndo_setup_tc = i40e_setup_tc,
5584 .ndo_set_features = i40e_set_features,
5585 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
5586 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
5587 .ndo_set_vf_tx_rate = i40e_ndo_set_vf_bw,
5588 .ndo_get_vf_config = i40e_ndo_get_vf_config,
5589};
5590
5591/**
5592 * i40e_config_netdev - Setup the netdev flags
5593 * @vsi: the VSI being configured
5594 *
5595 * Returns 0 on success, negative value on failure
5596 **/
5597static int i40e_config_netdev(struct i40e_vsi *vsi)
5598{
5599 struct i40e_pf *pf = vsi->back;
5600 struct i40e_hw *hw = &pf->hw;
5601 struct i40e_netdev_priv *np;
5602 struct net_device *netdev;
5603 u8 mac_addr[ETH_ALEN];
5604 int etherdev_size;
5605
5606 etherdev_size = sizeof(struct i40e_netdev_priv);
5607 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
5608 if (!netdev)
5609 return -ENOMEM;
5610
5611 vsi->netdev = netdev;
5612 np = netdev_priv(netdev);
5613 np->vsi = vsi;
5614
5615 netdev->hw_enc_features = NETIF_F_IP_CSUM |
5616 NETIF_F_GSO_UDP_TUNNEL |
5617 NETIF_F_TSO |
5618 NETIF_F_SG;
5619
5620 netdev->features = NETIF_F_SG |
5621 NETIF_F_IP_CSUM |
5622 NETIF_F_SCTP_CSUM |
5623 NETIF_F_HIGHDMA |
5624 NETIF_F_GSO_UDP_TUNNEL |
5625 NETIF_F_HW_VLAN_CTAG_TX |
5626 NETIF_F_HW_VLAN_CTAG_RX |
5627 NETIF_F_HW_VLAN_CTAG_FILTER |
5628 NETIF_F_IPV6_CSUM |
5629 NETIF_F_TSO |
5630 NETIF_F_TSO6 |
5631 NETIF_F_RXCSUM |
5632 NETIF_F_RXHASH |
5633 0;
5634
5635 /* copy netdev features into list of user selectable features */
5636 netdev->hw_features |= netdev->features;
5637
5638 if (vsi->type == I40E_VSI_MAIN) {
5639 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
5640 memcpy(mac_addr, hw->mac.perm_addr, ETH_ALEN);
5641 } else {
5642 /* relate the VSI_VMDQ name to the VSI_MAIN name */
5643 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
5644 pf->vsi[pf->lan_vsi]->netdev->name);
5645 random_ether_addr(mac_addr);
5646 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
5647 }
5648
5649 memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
5650 memcpy(netdev->perm_addr, mac_addr, ETH_ALEN);
5651 /* vlan gets same features (except vlan offload)
5652 * after any tweaks for specific VSI types
5653 */
5654 netdev->vlan_features = netdev->features & ~(NETIF_F_HW_VLAN_CTAG_TX |
5655 NETIF_F_HW_VLAN_CTAG_RX |
5656 NETIF_F_HW_VLAN_CTAG_FILTER);
5657 netdev->priv_flags |= IFF_UNICAST_FLT;
5658 netdev->priv_flags |= IFF_SUPP_NOFCS;
5659 /* Setup netdev TC information */
5660 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
5661
5662 netdev->netdev_ops = &i40e_netdev_ops;
5663 netdev->watchdog_timeo = 5 * HZ;
5664 i40e_set_ethtool_ops(netdev);
5665
5666 return 0;
5667}
5668
5669/**
5670 * i40e_vsi_delete - Delete a VSI from the switch
5671 * @vsi: the VSI being removed
5672 *
5673 * Returns 0 on success, negative value on failure
5674 **/
5675static void i40e_vsi_delete(struct i40e_vsi *vsi)
5676{
5677 /* remove default VSI is not allowed */
5678 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
5679 return;
5680
5681 /* there is no HW VSI for FDIR */
5682 if (vsi->type == I40E_VSI_FDIR)
5683 return;
5684
5685 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
5686 return;
5687}
5688
5689/**
5690 * i40e_add_vsi - Add a VSI to the switch
5691 * @vsi: the VSI being configured
5692 *
5693 * This initializes a VSI context depending on the VSI type to be added and
5694 * passes it down to the add_vsi aq command.
5695 **/
5696static int i40e_add_vsi(struct i40e_vsi *vsi)
5697{
5698 int ret = -ENODEV;
5699 struct i40e_mac_filter *f, *ftmp;
5700 struct i40e_pf *pf = vsi->back;
5701 struct i40e_hw *hw = &pf->hw;
5702 struct i40e_vsi_context ctxt;
5703 u8 enabled_tc = 0x1; /* TC0 enabled */
5704 int f_count = 0;
5705
5706 memset(&ctxt, 0, sizeof(ctxt));
5707 switch (vsi->type) {
5708 case I40E_VSI_MAIN:
5709 /* The PF's main VSI is already setup as part of the
5710 * device initialization, so we'll not bother with
5711 * the add_vsi call, but we will retrieve the current
5712 * VSI context.
5713 */
5714 ctxt.seid = pf->main_vsi_seid;
5715 ctxt.pf_num = pf->hw.pf_id;
5716 ctxt.vf_num = 0;
5717 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
5718 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
5719 if (ret) {
5720 dev_info(&pf->pdev->dev,
5721 "couldn't get pf vsi config, err %d, aq_err %d\n",
5722 ret, pf->hw.aq.asq_last_status);
5723 return -ENOENT;
5724 }
5725 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
5726 vsi->info.valid_sections = 0;
5727
5728 vsi->seid = ctxt.seid;
5729 vsi->id = ctxt.vsi_number;
5730
5731 enabled_tc = i40e_pf_get_tc_map(pf);
5732
5733 /* MFP mode setup queue map and update VSI */
5734 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5735 memset(&ctxt, 0, sizeof(ctxt));
5736 ctxt.seid = pf->main_vsi_seid;
5737 ctxt.pf_num = pf->hw.pf_id;
5738 ctxt.vf_num = 0;
5739 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5740 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5741 if (ret) {
5742 dev_info(&pf->pdev->dev,
5743 "update vsi failed, aq_err=%d\n",
5744 pf->hw.aq.asq_last_status);
5745 ret = -ENOENT;
5746 goto err;
5747 }
5748 /* update the local VSI info queue map */
5749 i40e_vsi_update_queue_map(vsi, &ctxt);
5750 vsi->info.valid_sections = 0;
5751 } else {
5752 /* Default/Main VSI is only enabled for TC0
5753 * reconfigure it to enable all TCs that are
5754 * available on the port in SFP mode.
5755 */
5756 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5757 if (ret) {
5758 dev_info(&pf->pdev->dev,
5759 "failed to configure TCs for main VSI tc_map 0x%08x, err %d, aq_err %d\n",
5760 enabled_tc, ret,
5761 pf->hw.aq.asq_last_status);
5762 ret = -ENOENT;
5763 }
5764 }
5765 break;
5766
5767 case I40E_VSI_FDIR:
5768 /* no queue mapping or actual HW VSI needed */
5769 vsi->info.valid_sections = 0;
5770 vsi->seid = 0;
5771 vsi->id = 0;
5772 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
5773 return 0;
5774 break;
5775
5776 case I40E_VSI_VMDQ2:
5777 ctxt.pf_num = hw->pf_id;
5778 ctxt.vf_num = 0;
5779 ctxt.uplink_seid = vsi->uplink_seid;
5780 ctxt.connection_type = 0x1; /* regular data port */
5781 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
5782
5783 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
5784
5785 /* This VSI is connected to VEB so the switch_id
5786 * should be set to zero by default.
5787 */
5788 ctxt.info.switch_id = 0;
5789 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
5790 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
5791
5792 /* Setup the VSI tx/rx queue map for TC0 only for now */
5793 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
5794 break;
5795
5796 case I40E_VSI_SRIOV:
5797 ctxt.pf_num = hw->pf_id;
5798 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
5799 ctxt.uplink_seid = vsi->uplink_seid;
5800 ctxt.connection_type = 0x1; /* regular data port */
5801 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5802
5803 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
5804
5805 /* This VSI is connected to VEB so the switch_id
5806 * should be set to zero by default.
5807 */
5808 ctxt.info.switch_id = cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
5809
5810 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
5811 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
5812 /* Setup the VSI tx/rx queue map for TC0 only for now */
5813 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
5814 break;
5815
5816 default:
5817 return -ENODEV;
5818 }
5819
5820 if (vsi->type != I40E_VSI_MAIN) {
5821 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
5822 if (ret) {
5823 dev_info(&vsi->back->pdev->dev,
5824 "add vsi failed, aq_err=%d\n",
5825 vsi->back->hw.aq.asq_last_status);
5826 ret = -ENOENT;
5827 goto err;
5828 }
5829 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
5830 vsi->info.valid_sections = 0;
5831 vsi->seid = ctxt.seid;
5832 vsi->id = ctxt.vsi_number;
5833 }
5834
5835 /* If macvlan filters already exist, force them to get loaded */
5836 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
5837 f->changed = true;
5838 f_count++;
5839 }
5840 if (f_count) {
5841 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
5842 pf->flags |= I40E_FLAG_FILTER_SYNC;
5843 }
5844
5845 /* Update VSI BW information */
5846 ret = i40e_vsi_get_bw_info(vsi);
5847 if (ret) {
5848 dev_info(&pf->pdev->dev,
5849 "couldn't get vsi bw info, err %d, aq_err %d\n",
5850 ret, pf->hw.aq.asq_last_status);
5851 /* VSI is already added so not tearing that up */
5852 ret = 0;
5853 }
5854
5855err:
5856 return ret;
5857}
5858
5859/**
5860 * i40e_vsi_release - Delete a VSI and free its resources
5861 * @vsi: the VSI being removed
5862 *
5863 * Returns 0 on success or < 0 on error
5864 **/
5865int i40e_vsi_release(struct i40e_vsi *vsi)
5866{
5867 struct i40e_mac_filter *f, *ftmp;
5868 struct i40e_veb *veb = NULL;
5869 struct i40e_pf *pf;
5870 u16 uplink_seid;
5871 int i, n;
5872
5873 pf = vsi->back;
5874
5875 /* release of a VEB-owner or last VSI is not allowed */
5876 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
5877 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
5878 vsi->seid, vsi->uplink_seid);
5879 return -ENODEV;
5880 }
5881 if (vsi == pf->vsi[pf->lan_vsi] &&
5882 !test_bit(__I40E_DOWN, &pf->state)) {
5883 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
5884 return -ENODEV;
5885 }
5886
5887 uplink_seid = vsi->uplink_seid;
5888 if (vsi->type != I40E_VSI_SRIOV) {
5889 if (vsi->netdev_registered) {
5890 vsi->netdev_registered = false;
5891 if (vsi->netdev) {
5892 /* results in a call to i40e_close() */
5893 unregister_netdev(vsi->netdev);
5894 free_netdev(vsi->netdev);
5895 vsi->netdev = NULL;
5896 }
5897 } else {
5898 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
5899 i40e_down(vsi);
5900 i40e_vsi_free_irq(vsi);
5901 i40e_vsi_free_tx_resources(vsi);
5902 i40e_vsi_free_rx_resources(vsi);
5903 }
5904 i40e_vsi_disable_irq(vsi);
5905 }
5906
5907 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
5908 i40e_del_filter(vsi, f->macaddr, f->vlan,
5909 f->is_vf, f->is_netdev);
5910 i40e_sync_vsi_filters(vsi);
5911
5912 i40e_vsi_delete(vsi);
5913 i40e_vsi_free_q_vectors(vsi);
5914 i40e_vsi_clear_rings(vsi);
5915 i40e_vsi_clear(vsi);
5916
5917 /* If this was the last thing on the VEB, except for the
5918 * controlling VSI, remove the VEB, which puts the controlling
5919 * VSI onto the next level down in the switch.
5920 *
5921 * Well, okay, there's one more exception here: don't remove
5922 * the orphan VEBs yet. We'll wait for an explicit remove request
5923 * from up the network stack.
5924 */
5925 for (n = 0, i = 0; i < pf->hw.func_caps.num_vsis; i++) {
5926 if (pf->vsi[i] &&
5927 pf->vsi[i]->uplink_seid == uplink_seid &&
5928 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
5929 n++; /* count the VSIs */
5930 }
5931 }
5932 for (i = 0; i < I40E_MAX_VEB; i++) {
5933 if (!pf->veb[i])
5934 continue;
5935 if (pf->veb[i]->uplink_seid == uplink_seid)
5936 n++; /* count the VEBs */
5937 if (pf->veb[i]->seid == uplink_seid)
5938 veb = pf->veb[i];
5939 }
5940 if (n == 0 && veb && veb->uplink_seid != 0)
5941 i40e_veb_release(veb);
5942
5943 return 0;
5944}
5945
5946/**
5947 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
5948 * @vsi: ptr to the VSI
5949 *
5950 * This should only be called after i40e_vsi_mem_alloc() which allocates the
5951 * corresponding SW VSI structure and initializes num_queue_pairs for the
5952 * newly allocated VSI.
5953 *
5954 * Returns 0 on success or negative on failure
5955 **/
5956static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
5957{
5958 int ret = -ENOENT;
5959 struct i40e_pf *pf = vsi->back;
5960
5961 if (vsi->q_vectors) {
5962 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
5963 vsi->seid);
5964 return -EEXIST;
5965 }
5966
5967 if (vsi->base_vector) {
5968 dev_info(&pf->pdev->dev,
5969 "VSI %d has non-zero base vector %d\n",
5970 vsi->seid, vsi->base_vector);
5971 return -EEXIST;
5972 }
5973
5974 ret = i40e_alloc_q_vectors(vsi);
5975 if (ret) {
5976 dev_info(&pf->pdev->dev,
5977 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
5978 vsi->num_q_vectors, vsi->seid, ret);
5979 vsi->num_q_vectors = 0;
5980 goto vector_setup_out;
5981 }
5982
5983 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
5984 vsi->num_q_vectors, vsi->idx);
5985 if (vsi->base_vector < 0) {
5986 dev_info(&pf->pdev->dev,
5987 "failed to get q tracking for VSI %d, err=%d\n",
5988 vsi->seid, vsi->base_vector);
5989 i40e_vsi_free_q_vectors(vsi);
5990 ret = -ENOENT;
5991 goto vector_setup_out;
5992 }
5993
5994vector_setup_out:
5995 return ret;
5996}
5997
5998/**
5999 * i40e_vsi_setup - Set up a VSI by a given type
6000 * @pf: board private structure
6001 * @type: VSI type
6002 * @uplink_seid: the switch element to link to
6003 * @param1: usage depends upon VSI type. For VF types, indicates VF id
6004 *
6005 * This allocates the sw VSI structure and its queue resources, then add a VSI
6006 * to the identified VEB.
6007 *
6008 * Returns pointer to the successfully allocated and configure VSI sw struct on
6009 * success, otherwise returns NULL on failure.
6010 **/
6011struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
6012 u16 uplink_seid, u32 param1)
6013{
6014 struct i40e_vsi *vsi = NULL;
6015 struct i40e_veb *veb = NULL;
6016 int ret, i;
6017 int v_idx;
6018
6019 /* The requested uplink_seid must be either
6020 * - the PF's port seid
6021 * no VEB is needed because this is the PF
6022 * or this is a Flow Director special case VSI
6023 * - seid of an existing VEB
6024 * - seid of a VSI that owns an existing VEB
6025 * - seid of a VSI that doesn't own a VEB
6026 * a new VEB is created and the VSI becomes the owner
6027 * - seid of the PF VSI, which is what creates the first VEB
6028 * this is a special case of the previous
6029 *
6030 * Find which uplink_seid we were given and create a new VEB if needed
6031 */
6032 for (i = 0; i < I40E_MAX_VEB; i++) {
6033 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
6034 veb = pf->veb[i];
6035 break;
6036 }
6037 }
6038
6039 if (!veb && uplink_seid != pf->mac_seid) {
6040
6041 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
6042 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
6043 vsi = pf->vsi[i];
6044 break;
6045 }
6046 }
6047 if (!vsi) {
6048 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
6049 uplink_seid);
6050 return NULL;
6051 }
6052
6053 if (vsi->uplink_seid == pf->mac_seid)
6054 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
6055 vsi->tc_config.enabled_tc);
6056 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
6057 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
6058 vsi->tc_config.enabled_tc);
6059
6060 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
6061 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
6062 veb = pf->veb[i];
6063 }
6064 if (!veb) {
6065 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
6066 return NULL;
6067 }
6068
6069 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
6070 uplink_seid = veb->seid;
6071 }
6072
6073 /* get vsi sw struct */
6074 v_idx = i40e_vsi_mem_alloc(pf, type);
6075 if (v_idx < 0)
6076 goto err_alloc;
6077 vsi = pf->vsi[v_idx];
6078 vsi->type = type;
6079 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
6080
6081 if (type == I40E_VSI_MAIN)
6082 pf->lan_vsi = v_idx;
6083 else if (type == I40E_VSI_SRIOV)
6084 vsi->vf_id = param1;
6085 /* assign it some queues */
6086 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
6087 if (ret < 0) {
6088 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
6089 vsi->seid, ret);
6090 goto err_vsi;
6091 }
6092 vsi->base_queue = ret;
6093
6094 /* get a VSI from the hardware */
6095 vsi->uplink_seid = uplink_seid;
6096 ret = i40e_add_vsi(vsi);
6097 if (ret)
6098 goto err_vsi;
6099
6100 switch (vsi->type) {
6101 /* setup the netdev if needed */
6102 case I40E_VSI_MAIN:
6103 case I40E_VSI_VMDQ2:
6104 ret = i40e_config_netdev(vsi);
6105 if (ret)
6106 goto err_netdev;
6107 ret = register_netdev(vsi->netdev);
6108 if (ret)
6109 goto err_netdev;
6110 vsi->netdev_registered = true;
6111 netif_carrier_off(vsi->netdev);
6112 /* fall through */
6113
6114 case I40E_VSI_FDIR:
6115 /* set up vectors and rings if needed */
6116 ret = i40e_vsi_setup_vectors(vsi);
6117 if (ret)
6118 goto err_msix;
6119
6120 ret = i40e_alloc_rings(vsi);
6121 if (ret)
6122 goto err_rings;
6123
6124 /* map all of the rings to the q_vectors */
6125 i40e_vsi_map_rings_to_vectors(vsi);
6126
6127 i40e_vsi_reset_stats(vsi);
6128 break;
6129
6130 default:
6131 /* no netdev or rings for the other VSI types */
6132 break;
6133 }
6134
6135 return vsi;
6136
6137err_rings:
6138 i40e_vsi_free_q_vectors(vsi);
6139err_msix:
6140 if (vsi->netdev_registered) {
6141 vsi->netdev_registered = false;
6142 unregister_netdev(vsi->netdev);
6143 free_netdev(vsi->netdev);
6144 vsi->netdev = NULL;
6145 }
6146err_netdev:
6147 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
6148err_vsi:
6149 i40e_vsi_clear(vsi);
6150err_alloc:
6151 return NULL;
6152}
6153
6154/**
6155 * i40e_veb_get_bw_info - Query VEB BW information
6156 * @veb: the veb to query
6157 *
6158 * Query the Tx scheduler BW configuration data for given VEB
6159 **/
6160static int i40e_veb_get_bw_info(struct i40e_veb *veb)
6161{
6162 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
6163 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
6164 struct i40e_pf *pf = veb->pf;
6165 struct i40e_hw *hw = &pf->hw;
6166 u32 tc_bw_max;
6167 int ret = 0;
6168 int i;
6169
6170 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
6171 &bw_data, NULL);
6172 if (ret) {
6173 dev_info(&pf->pdev->dev,
6174 "query veb bw config failed, aq_err=%d\n",
6175 hw->aq.asq_last_status);
6176 goto out;
6177 }
6178
6179 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
6180 &ets_data, NULL);
6181 if (ret) {
6182 dev_info(&pf->pdev->dev,
6183 "query veb bw ets config failed, aq_err=%d\n",
6184 hw->aq.asq_last_status);
6185 goto out;
6186 }
6187
6188 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
6189 veb->bw_max_quanta = ets_data.tc_bw_max;
6190 veb->is_abs_credits = bw_data.absolute_credits_enable;
6191 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
6192 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
6193 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6194 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
6195 veb->bw_tc_limit_credits[i] =
6196 le16_to_cpu(bw_data.tc_bw_limits[i]);
6197 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
6198 }
6199
6200out:
6201 return ret;
6202}
6203
6204/**
6205 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
6206 * @pf: board private structure
6207 *
6208 * On error: returns error code (negative)
6209 * On success: returns vsi index in PF (positive)
6210 **/
6211static int i40e_veb_mem_alloc(struct i40e_pf *pf)
6212{
6213 int ret = -ENOENT;
6214 struct i40e_veb *veb;
6215 int i;
6216
6217 /* Need to protect the allocation of switch elements at the PF level */
6218 mutex_lock(&pf->switch_mutex);
6219
6220 /* VEB list may be fragmented if VEB creation/destruction has
6221 * been happening. We can afford to do a quick scan to look
6222 * for any free slots in the list.
6223 *
6224 * find next empty veb slot, looping back around if necessary
6225 */
6226 i = 0;
6227 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
6228 i++;
6229 if (i >= I40E_MAX_VEB) {
6230 ret = -ENOMEM;
6231 goto err_alloc_veb; /* out of VEB slots! */
6232 }
6233
6234 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
6235 if (!veb) {
6236 ret = -ENOMEM;
6237 goto err_alloc_veb;
6238 }
6239 veb->pf = pf;
6240 veb->idx = i;
6241 veb->enabled_tc = 1;
6242
6243 pf->veb[i] = veb;
6244 ret = i;
6245err_alloc_veb:
6246 mutex_unlock(&pf->switch_mutex);
6247 return ret;
6248}
6249
6250/**
6251 * i40e_switch_branch_release - Delete a branch of the switch tree
6252 * @branch: where to start deleting
6253 *
6254 * This uses recursion to find the tips of the branch to be
6255 * removed, deleting until we get back to and can delete this VEB.
6256 **/
6257static void i40e_switch_branch_release(struct i40e_veb *branch)
6258{
6259 struct i40e_pf *pf = branch->pf;
6260 u16 branch_seid = branch->seid;
6261 u16 veb_idx = branch->idx;
6262 int i;
6263
6264 /* release any VEBs on this VEB - RECURSION */
6265 for (i = 0; i < I40E_MAX_VEB; i++) {
6266 if (!pf->veb[i])
6267 continue;
6268 if (pf->veb[i]->uplink_seid == branch->seid)
6269 i40e_switch_branch_release(pf->veb[i]);
6270 }
6271
6272 /* Release the VSIs on this VEB, but not the owner VSI.
6273 *
6274 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
6275 * the VEB itself, so don't use (*branch) after this loop.
6276 */
6277 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
6278 if (!pf->vsi[i])
6279 continue;
6280 if (pf->vsi[i]->uplink_seid == branch_seid &&
6281 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
6282 i40e_vsi_release(pf->vsi[i]);
6283 }
6284 }
6285
6286 /* There's one corner case where the VEB might not have been
6287 * removed, so double check it here and remove it if needed.
6288 * This case happens if the veb was created from the debugfs
6289 * commands and no VSIs were added to it.
6290 */
6291 if (pf->veb[veb_idx])
6292 i40e_veb_release(pf->veb[veb_idx]);
6293}
6294
6295/**
6296 * i40e_veb_clear - remove veb struct
6297 * @veb: the veb to remove
6298 **/
6299static void i40e_veb_clear(struct i40e_veb *veb)
6300{
6301 if (!veb)
6302 return;
6303
6304 if (veb->pf) {
6305 struct i40e_pf *pf = veb->pf;
6306
6307 mutex_lock(&pf->switch_mutex);
6308 if (pf->veb[veb->idx] == veb)
6309 pf->veb[veb->idx] = NULL;
6310 mutex_unlock(&pf->switch_mutex);
6311 }
6312
6313 kfree(veb);
6314}
6315
6316/**
6317 * i40e_veb_release - Delete a VEB and free its resources
6318 * @veb: the VEB being removed
6319 **/
6320void i40e_veb_release(struct i40e_veb *veb)
6321{
6322 struct i40e_vsi *vsi = NULL;
6323 struct i40e_pf *pf;
6324 int i, n = 0;
6325
6326 pf = veb->pf;
6327
6328 /* find the remaining VSI and check for extras */
6329 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
6330 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
6331 n++;
6332 vsi = pf->vsi[i];
6333 }
6334 }
6335 if (n != 1) {
6336 dev_info(&pf->pdev->dev,
6337 "can't remove VEB %d with %d VSIs left\n",
6338 veb->seid, n);
6339 return;
6340 }
6341
6342 /* move the remaining VSI to uplink veb */
6343 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
6344 if (veb->uplink_seid) {
6345 vsi->uplink_seid = veb->uplink_seid;
6346 if (veb->uplink_seid == pf->mac_seid)
6347 vsi->veb_idx = I40E_NO_VEB;
6348 else
6349 vsi->veb_idx = veb->veb_idx;
6350 } else {
6351 /* floating VEB */
6352 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6353 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
6354 }
6355
6356 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
6357 i40e_veb_clear(veb);
6358
6359 return;
6360}
6361
6362/**
6363 * i40e_add_veb - create the VEB in the switch
6364 * @veb: the VEB to be instantiated
6365 * @vsi: the controlling VSI
6366 **/
6367static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
6368{
6369 bool is_default = (vsi->idx == vsi->back->lan_vsi);
6370 int ret;
6371
6372 /* get a VEB from the hardware */
6373 ret = i40e_aq_add_veb(&veb->pf->hw, veb->uplink_seid, vsi->seid,
6374 veb->enabled_tc, is_default, &veb->seid, NULL);
6375 if (ret) {
6376 dev_info(&veb->pf->pdev->dev,
6377 "couldn't add VEB, err %d, aq_err %d\n",
6378 ret, veb->pf->hw.aq.asq_last_status);
6379 return -EPERM;
6380 }
6381
6382 /* get statistics counter */
6383 ret = i40e_aq_get_veb_parameters(&veb->pf->hw, veb->seid, NULL, NULL,
6384 &veb->stats_idx, NULL, NULL, NULL);
6385 if (ret) {
6386 dev_info(&veb->pf->pdev->dev,
6387 "couldn't get VEB statistics idx, err %d, aq_err %d\n",
6388 ret, veb->pf->hw.aq.asq_last_status);
6389 return -EPERM;
6390 }
6391 ret = i40e_veb_get_bw_info(veb);
6392 if (ret) {
6393 dev_info(&veb->pf->pdev->dev,
6394 "couldn't get VEB bw info, err %d, aq_err %d\n",
6395 ret, veb->pf->hw.aq.asq_last_status);
6396 i40e_aq_delete_element(&veb->pf->hw, veb->seid, NULL);
6397 return -ENOENT;
6398 }
6399
6400 vsi->uplink_seid = veb->seid;
6401 vsi->veb_idx = veb->idx;
6402 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
6403
6404 return 0;
6405}
6406
6407/**
6408 * i40e_veb_setup - Set up a VEB
6409 * @pf: board private structure
6410 * @flags: VEB setup flags
6411 * @uplink_seid: the switch element to link to
6412 * @vsi_seid: the initial VSI seid
6413 * @enabled_tc: Enabled TC bit-map
6414 *
6415 * This allocates the sw VEB structure and links it into the switch
6416 * It is possible and legal for this to be a duplicate of an already
6417 * existing VEB. It is also possible for both uplink and vsi seids
6418 * to be zero, in order to create a floating VEB.
6419 *
6420 * Returns pointer to the successfully allocated VEB sw struct on
6421 * success, otherwise returns NULL on failure.
6422 **/
6423struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
6424 u16 uplink_seid, u16 vsi_seid,
6425 u8 enabled_tc)
6426{
6427 struct i40e_veb *veb, *uplink_veb = NULL;
6428 int vsi_idx, veb_idx;
6429 int ret;
6430
6431 /* if one seid is 0, the other must be 0 to create a floating relay */
6432 if ((uplink_seid == 0 || vsi_seid == 0) &&
6433 (uplink_seid + vsi_seid != 0)) {
6434 dev_info(&pf->pdev->dev,
6435 "one, not both seid's are 0: uplink=%d vsi=%d\n",
6436 uplink_seid, vsi_seid);
6437 return NULL;
6438 }
6439
6440 /* make sure there is such a vsi and uplink */
6441 for (vsi_idx = 0; vsi_idx < pf->hw.func_caps.num_vsis; vsi_idx++)
6442 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
6443 break;
6444 if (vsi_idx >= pf->hw.func_caps.num_vsis && vsi_seid != 0) {
6445 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
6446 vsi_seid);
6447 return NULL;
6448 }
6449
6450 if (uplink_seid && uplink_seid != pf->mac_seid) {
6451 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6452 if (pf->veb[veb_idx] &&
6453 pf->veb[veb_idx]->seid == uplink_seid) {
6454 uplink_veb = pf->veb[veb_idx];
6455 break;
6456 }
6457 }
6458 if (!uplink_veb) {
6459 dev_info(&pf->pdev->dev,
6460 "uplink seid %d not found\n", uplink_seid);
6461 return NULL;
6462 }
6463 }
6464
6465 /* get veb sw struct */
6466 veb_idx = i40e_veb_mem_alloc(pf);
6467 if (veb_idx < 0)
6468 goto err_alloc;
6469 veb = pf->veb[veb_idx];
6470 veb->flags = flags;
6471 veb->uplink_seid = uplink_seid;
6472 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
6473 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
6474
6475 /* create the VEB in the switch */
6476 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
6477 if (ret)
6478 goto err_veb;
6479
6480 return veb;
6481
6482err_veb:
6483 i40e_veb_clear(veb);
6484err_alloc:
6485 return NULL;
6486}
6487
6488/**
6489 * i40e_setup_pf_switch_element - set pf vars based on switch type
6490 * @pf: board private structure
6491 * @ele: element we are building info from
6492 * @num_reported: total number of elements
6493 * @printconfig: should we print the contents
6494 *
6495 * helper function to assist in extracting a few useful SEID values.
6496 **/
6497static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
6498 struct i40e_aqc_switch_config_element_resp *ele,
6499 u16 num_reported, bool printconfig)
6500{
6501 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
6502 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
6503 u8 element_type = ele->element_type;
6504 u16 seid = le16_to_cpu(ele->seid);
6505
6506 if (printconfig)
6507 dev_info(&pf->pdev->dev,
6508 "type=%d seid=%d uplink=%d downlink=%d\n",
6509 element_type, seid, uplink_seid, downlink_seid);
6510
6511 switch (element_type) {
6512 case I40E_SWITCH_ELEMENT_TYPE_MAC:
6513 pf->mac_seid = seid;
6514 break;
6515 case I40E_SWITCH_ELEMENT_TYPE_VEB:
6516 /* Main VEB? */
6517 if (uplink_seid != pf->mac_seid)
6518 break;
6519 if (pf->lan_veb == I40E_NO_VEB) {
6520 int v;
6521
6522 /* find existing or else empty VEB */
6523 for (v = 0; v < I40E_MAX_VEB; v++) {
6524 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
6525 pf->lan_veb = v;
6526 break;
6527 }
6528 }
6529 if (pf->lan_veb == I40E_NO_VEB) {
6530 v = i40e_veb_mem_alloc(pf);
6531 if (v < 0)
6532 break;
6533 pf->lan_veb = v;
6534 }
6535 }
6536
6537 pf->veb[pf->lan_veb]->seid = seid;
6538 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
6539 pf->veb[pf->lan_veb]->pf = pf;
6540 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
6541 break;
6542 case I40E_SWITCH_ELEMENT_TYPE_VSI:
6543 if (num_reported != 1)
6544 break;
6545 /* This is immediately after a reset so we can assume this is
6546 * the PF's VSI
6547 */
6548 pf->mac_seid = uplink_seid;
6549 pf->pf_seid = downlink_seid;
6550 pf->main_vsi_seid = seid;
6551 if (printconfig)
6552 dev_info(&pf->pdev->dev,
6553 "pf_seid=%d main_vsi_seid=%d\n",
6554 pf->pf_seid, pf->main_vsi_seid);
6555 break;
6556 case I40E_SWITCH_ELEMENT_TYPE_PF:
6557 case I40E_SWITCH_ELEMENT_TYPE_VF:
6558 case I40E_SWITCH_ELEMENT_TYPE_EMP:
6559 case I40E_SWITCH_ELEMENT_TYPE_BMC:
6560 case I40E_SWITCH_ELEMENT_TYPE_PE:
6561 case I40E_SWITCH_ELEMENT_TYPE_PA:
6562 /* ignore these for now */
6563 break;
6564 default:
6565 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
6566 element_type, seid);
6567 break;
6568 }
6569}
6570
6571/**
6572 * i40e_fetch_switch_configuration - Get switch config from firmware
6573 * @pf: board private structure
6574 * @printconfig: should we print the contents
6575 *
6576 * Get the current switch configuration from the device and
6577 * extract a few useful SEID values.
6578 **/
6579int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
6580{
6581 struct i40e_aqc_get_switch_config_resp *sw_config;
6582 u16 next_seid = 0;
6583 int ret = 0;
6584 u8 *aq_buf;
6585 int i;
6586
6587 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
6588 if (!aq_buf)
6589 return -ENOMEM;
6590
6591 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
6592 do {
6593 u16 num_reported, num_total;
6594
6595 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
6596 I40E_AQ_LARGE_BUF,
6597 &next_seid, NULL);
6598 if (ret) {
6599 dev_info(&pf->pdev->dev,
6600 "get switch config failed %d aq_err=%x\n",
6601 ret, pf->hw.aq.asq_last_status);
6602 kfree(aq_buf);
6603 return -ENOENT;
6604 }
6605
6606 num_reported = le16_to_cpu(sw_config->header.num_reported);
6607 num_total = le16_to_cpu(sw_config->header.num_total);
6608
6609 if (printconfig)
6610 dev_info(&pf->pdev->dev,
6611 "header: %d reported %d total\n",
6612 num_reported, num_total);
6613
6614 if (num_reported) {
6615 int sz = sizeof(*sw_config) * num_reported;
6616
6617 kfree(pf->sw_config);
6618 pf->sw_config = kzalloc(sz, GFP_KERNEL);
6619 if (pf->sw_config)
6620 memcpy(pf->sw_config, sw_config, sz);
6621 }
6622
6623 for (i = 0; i < num_reported; i++) {
6624 struct i40e_aqc_switch_config_element_resp *ele =
6625 &sw_config->element[i];
6626
6627 i40e_setup_pf_switch_element(pf, ele, num_reported,
6628 printconfig);
6629 }
6630 } while (next_seid != 0);
6631
6632 kfree(aq_buf);
6633 return ret;
6634}
6635
6636/**
6637 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
6638 * @pf: board private structure
6639 *
6640 * Returns 0 on success, negative value on failure
6641 **/
6642static int i40e_setup_pf_switch(struct i40e_pf *pf)
6643{
6644 int ret;
6645
6646 /* find out what's out there already */
6647 ret = i40e_fetch_switch_configuration(pf, false);
6648 if (ret) {
6649 dev_info(&pf->pdev->dev,
6650 "couldn't fetch switch config, err %d, aq_err %d\n",
6651 ret, pf->hw.aq.asq_last_status);
6652 return ret;
6653 }
6654 i40e_pf_reset_stats(pf);
6655
6656 /* fdir VSI must happen first to be sure it gets queue 0, but only
6657 * if there is enough room for the fdir VSI
6658 */
6659 if (pf->num_lan_qps > 1)
6660 i40e_fdir_setup(pf);
6661
6662 /* first time setup */
6663 if (pf->lan_vsi == I40E_NO_VSI) {
6664 struct i40e_vsi *vsi = NULL;
6665 u16 uplink_seid;
6666
6667 /* Set up the PF VSI associated with the PF's main VSI
6668 * that is already in the HW switch
6669 */
6670 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6671 uplink_seid = pf->veb[pf->lan_veb]->seid;
6672 else
6673 uplink_seid = pf->mac_seid;
6674
6675 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
6676 if (!vsi) {
6677 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
6678 i40e_fdir_teardown(pf);
6679 return -EAGAIN;
6680 }
6681 /* accommodate kcompat by copying the main VSI queue count
6682 * into the pf, since this newer code pushes the pf queue
6683 * info down a level into a VSI
6684 */
6685 pf->num_rx_queues = vsi->alloc_queue_pairs;
6686 pf->num_tx_queues = vsi->alloc_queue_pairs;
6687 } else {
6688 /* force a reset of TC and queue layout configurations */
6689 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
6690 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
6691 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
6692 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
6693 }
6694 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
6695
6696 /* Setup static PF queue filter control settings */
6697 ret = i40e_setup_pf_filter_control(pf);
6698 if (ret) {
6699 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
6700 ret);
6701 /* Failure here should not stop continuing other steps */
6702 }
6703
6704 /* enable RSS in the HW, even for only one queue, as the stack can use
6705 * the hash
6706 */
6707 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
6708 i40e_config_rss(pf);
6709
6710 /* fill in link information and enable LSE reporting */
6711 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
6712 i40e_link_event(pf);
6713
6714 /* Initialize user-specifics link properties */
6715 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
6716 I40E_AQ_AN_COMPLETED) ? true : false);
6717 pf->hw.fc.requested_mode = I40E_FC_DEFAULT;
6718 if (pf->hw.phy.link_info.an_info &
6719 (I40E_AQ_LINK_PAUSE_TX | I40E_AQ_LINK_PAUSE_RX))
6720 pf->hw.fc.current_mode = I40E_FC_FULL;
6721 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX)
6722 pf->hw.fc.current_mode = I40E_FC_TX_PAUSE;
6723 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX)
6724 pf->hw.fc.current_mode = I40E_FC_RX_PAUSE;
6725 else
6726 pf->hw.fc.current_mode = I40E_FC_DEFAULT;
6727
6728 return ret;
6729}
6730
6731/**
6732 * i40e_set_rss_size - helper to set rss_size
6733 * @pf: board private structure
6734 * @queues_left: how many queues
6735 */
6736static u16 i40e_set_rss_size(struct i40e_pf *pf, int queues_left)
6737{
6738 int num_tc0;
6739
6740 num_tc0 = min_t(int, queues_left, pf->rss_size_max);
6741 num_tc0 = min_t(int, num_tc0, nr_cpus_node(numa_node_id()));
6742 num_tc0 = rounddown_pow_of_two(num_tc0);
6743
6744 return num_tc0;
6745}
6746
6747/**
6748 * i40e_determine_queue_usage - Work out queue distribution
6749 * @pf: board private structure
6750 **/
6751static void i40e_determine_queue_usage(struct i40e_pf *pf)
6752{
6753 int accum_tc_size;
6754 int queues_left;
6755
6756 pf->num_lan_qps = 0;
6757 pf->num_tc_qps = rounddown_pow_of_two(pf->num_tc_qps);
6758 accum_tc_size = (I40E_MAX_TRAFFIC_CLASS - 1) * pf->num_tc_qps;
6759
6760 /* Find the max queues to be put into basic use. We'll always be
6761 * using TC0, whether or not DCB is running, and TC0 will get the
6762 * big RSS set.
6763 */
6764 queues_left = pf->hw.func_caps.num_tx_qp;
6765
6766 if (!((pf->flags & I40E_FLAG_MSIX_ENABLED) &&
6767 (pf->flags & I40E_FLAG_MQ_ENABLED)) ||
6768 !(pf->flags & (I40E_FLAG_RSS_ENABLED |
6769 I40E_FLAG_FDIR_ENABLED | I40E_FLAG_DCB_ENABLED)) ||
6770 (queues_left == 1)) {
6771
6772 /* one qp for PF, no queues for anything else */
6773 queues_left = 0;
6774 pf->rss_size = pf->num_lan_qps = 1;
6775
6776 /* make sure all the fancies are disabled */
6777 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
6778 I40E_FLAG_MQ_ENABLED |
6779 I40E_FLAG_FDIR_ENABLED |
6780 I40E_FLAG_FDIR_ATR_ENABLED |
6781 I40E_FLAG_DCB_ENABLED |
6782 I40E_FLAG_SRIOV_ENABLED |
6783 I40E_FLAG_VMDQ_ENABLED);
6784
6785 } else if (pf->flags & I40E_FLAG_RSS_ENABLED &&
6786 !(pf->flags & I40E_FLAG_FDIR_ENABLED) &&
6787 !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
6788
6789 pf->rss_size = i40e_set_rss_size(pf, queues_left);
6790
6791 queues_left -= pf->rss_size;
6792 pf->num_lan_qps = pf->rss_size;
6793
6794 } else if (pf->flags & I40E_FLAG_RSS_ENABLED &&
6795 !(pf->flags & I40E_FLAG_FDIR_ENABLED) &&
6796 (pf->flags & I40E_FLAG_DCB_ENABLED)) {
6797
6798 /* save num_tc_qps queues for TCs 1 thru 7 and the rest
6799 * are set up for RSS in TC0
6800 */
6801 queues_left -= accum_tc_size;
6802
6803 pf->rss_size = i40e_set_rss_size(pf, queues_left);
6804
6805 queues_left -= pf->rss_size;
6806 if (queues_left < 0) {
6807 dev_info(&pf->pdev->dev, "not enough queues for DCB\n");
6808 return;
6809 }
6810
6811 pf->num_lan_qps = pf->rss_size + accum_tc_size;
6812
6813 } else if (pf->flags & I40E_FLAG_RSS_ENABLED &&
6814 (pf->flags & I40E_FLAG_FDIR_ENABLED) &&
6815 !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
6816
6817 queues_left -= 1; /* save 1 queue for FD */
6818
6819 pf->rss_size = i40e_set_rss_size(pf, queues_left);
6820
6821 queues_left -= pf->rss_size;
6822 if (queues_left < 0) {
6823 dev_info(&pf->pdev->dev, "not enough queues for Flow Director\n");
6824 return;
6825 }
6826
6827 pf->num_lan_qps = pf->rss_size;
6828
6829 } else if (pf->flags & I40E_FLAG_RSS_ENABLED &&
6830 (pf->flags & I40E_FLAG_FDIR_ENABLED) &&
6831 (pf->flags & I40E_FLAG_DCB_ENABLED)) {
6832
6833 /* save 1 queue for TCs 1 thru 7,
6834 * 1 queue for flow director,
6835 * and the rest are set up for RSS in TC0
6836 */
6837 queues_left -= 1;
6838 queues_left -= accum_tc_size;
6839
6840 pf->rss_size = i40e_set_rss_size(pf, queues_left);
6841 queues_left -= pf->rss_size;
6842 if (queues_left < 0) {
6843 dev_info(&pf->pdev->dev, "not enough queues for DCB and Flow Director\n");
6844 return;
6845 }
6846
6847 pf->num_lan_qps = pf->rss_size + accum_tc_size;
6848
6849 } else {
6850 dev_info(&pf->pdev->dev,
6851 "Invalid configuration, flags=0x%08llx\n", pf->flags);
6852 return;
6853 }
6854
6855 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
6856 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
6857 pf->num_req_vfs = min_t(int, pf->num_req_vfs, (queues_left /
6858 pf->num_vf_qps));
6859 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
6860 }
6861
6862 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
6863 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
6864 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
6865 (queues_left / pf->num_vmdq_qps));
6866 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
6867 }
6868
6869 return;
6870}
6871
6872/**
6873 * i40e_setup_pf_filter_control - Setup PF static filter control
6874 * @pf: PF to be setup
6875 *
6876 * i40e_setup_pf_filter_control sets up a pf's initial filter control
6877 * settings. If PE/FCoE are enabled then it will also set the per PF
6878 * based filter sizes required for them. It also enables Flow director,
6879 * ethertype and macvlan type filter settings for the pf.
6880 *
6881 * Returns 0 on success, negative on failure
6882 **/
6883static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
6884{
6885 struct i40e_filter_control_settings *settings = &pf->filter_settings;
6886
6887 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
6888
6889 /* Flow Director is enabled */
6890 if (pf->flags & (I40E_FLAG_FDIR_ENABLED | I40E_FLAG_FDIR_ATR_ENABLED))
6891 settings->enable_fdir = true;
6892
6893 /* Ethtype and MACVLAN filters enabled for PF */
6894 settings->enable_ethtype = true;
6895 settings->enable_macvlan = true;
6896
6897 if (i40e_set_filter_control(&pf->hw, settings))
6898 return -ENOENT;
6899
6900 return 0;
6901}
6902
6903/**
6904 * i40e_probe - Device initialization routine
6905 * @pdev: PCI device information struct
6906 * @ent: entry in i40e_pci_tbl
6907 *
6908 * i40e_probe initializes a pf identified by a pci_dev structure.
6909 * The OS initialization, configuring of the pf private structure,
6910 * and a hardware reset occur.
6911 *
6912 * Returns 0 on success, negative on failure
6913 **/
6914static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
6915{
6916 struct i40e_driver_version dv;
6917 struct i40e_pf *pf;
6918 struct i40e_hw *hw;
6919 int err = 0;
6920 u32 len;
6921
6922 err = pci_enable_device_mem(pdev);
6923 if (err)
6924 return err;
6925
6926 /* set up for high or low dma */
6927 if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64))) {
6928 /* coherent mask for the same size will always succeed if
6929 * dma_set_mask does
6930 */
6931 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
6932 } else if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
6933 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
6934 } else {
6935 dev_err(&pdev->dev, "DMA configuration failed: %d\n", err);
6936 err = -EIO;
6937 goto err_dma;
6938 }
6939
6940 /* set up pci connections */
6941 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
6942 IORESOURCE_MEM), i40e_driver_name);
6943 if (err) {
6944 dev_info(&pdev->dev,
6945 "pci_request_selected_regions failed %d\n", err);
6946 goto err_pci_reg;
6947 }
6948
6949 pci_enable_pcie_error_reporting(pdev);
6950 pci_set_master(pdev);
6951
6952 /* Now that we have a PCI connection, we need to do the
6953 * low level device setup. This is primarily setting up
6954 * the Admin Queue structures and then querying for the
6955 * device's current profile information.
6956 */
6957 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
6958 if (!pf) {
6959 err = -ENOMEM;
6960 goto err_pf_alloc;
6961 }
6962 pf->next_vsi = 0;
6963 pf->pdev = pdev;
6964 set_bit(__I40E_DOWN, &pf->state);
6965
6966 hw = &pf->hw;
6967 hw->back = pf;
6968 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
6969 pci_resource_len(pdev, 0));
6970 if (!hw->hw_addr) {
6971 err = -EIO;
6972 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
6973 (unsigned int)pci_resource_start(pdev, 0),
6974 (unsigned int)pci_resource_len(pdev, 0), err);
6975 goto err_ioremap;
6976 }
6977 hw->vendor_id = pdev->vendor;
6978 hw->device_id = pdev->device;
6979 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
6980 hw->subsystem_vendor_id = pdev->subsystem_vendor;
6981 hw->subsystem_device_id = pdev->subsystem_device;
6982 hw->bus.device = PCI_SLOT(pdev->devfn);
6983 hw->bus.func = PCI_FUNC(pdev->devfn);
6984
6985 /* Reset here to make sure all is clean and to define PF 'n' */
6986 err = i40e_pf_reset(hw);
6987 if (err) {
6988 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
6989 goto err_pf_reset;
6990 }
6991 pf->pfr_count++;
6992
6993 hw->aq.num_arq_entries = I40E_AQ_LEN;
6994 hw->aq.num_asq_entries = I40E_AQ_LEN;
6995 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
6996 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
6997 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
6998 snprintf(pf->misc_int_name, sizeof(pf->misc_int_name) - 1,
6999 "%s-pf%d:misc",
7000 dev_driver_string(&pf->pdev->dev), pf->hw.pf_id);
7001
7002 err = i40e_init_shared_code(hw);
7003 if (err) {
7004 dev_info(&pdev->dev, "init_shared_code failed: %d\n", err);
7005 goto err_pf_reset;
7006 }
7007
7008 err = i40e_init_adminq(hw);
7009 dev_info(&pdev->dev, "%s\n", i40e_fw_version_str(hw));
7010 if (err) {
7011 dev_info(&pdev->dev,
7012 "init_adminq failed: %d expecting API %02x.%02x\n",
7013 err,
7014 I40E_FW_API_VERSION_MAJOR, I40E_FW_API_VERSION_MINOR);
7015 goto err_pf_reset;
7016 }
7017
7018 err = i40e_get_capabilities(pf);
7019 if (err)
7020 goto err_adminq_setup;
7021
7022 err = i40e_sw_init(pf);
7023 if (err) {
7024 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
7025 goto err_sw_init;
7026 }
7027
7028 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
7029 hw->func_caps.num_rx_qp,
7030 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
7031 if (err) {
7032 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
7033 goto err_init_lan_hmc;
7034 }
7035
7036 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
7037 if (err) {
7038 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
7039 err = -ENOENT;
7040 goto err_configure_lan_hmc;
7041 }
7042
7043 i40e_get_mac_addr(hw, hw->mac.addr);
7044 if (i40e_validate_mac_addr(hw->mac.addr)) {
7045 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
7046 err = -EIO;
7047 goto err_mac_addr;
7048 }
7049 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
7050 memcpy(hw->mac.perm_addr, hw->mac.addr, ETH_ALEN);
7051
7052 pci_set_drvdata(pdev, pf);
7053 pci_save_state(pdev);
7054
7055 /* set up periodic task facility */
7056 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
7057 pf->service_timer_period = HZ;
7058
7059 INIT_WORK(&pf->service_task, i40e_service_task);
7060 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
7061 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
7062 pf->link_check_timeout = jiffies;
7063
7064 /* set up the main switch operations */
7065 i40e_determine_queue_usage(pf);
7066 i40e_init_interrupt_scheme(pf);
7067
7068 /* Set up the *vsi struct based on the number of VSIs in the HW,
7069 * and set up our local tracking of the MAIN PF vsi.
7070 */
7071 len = sizeof(struct i40e_vsi *) * pf->hw.func_caps.num_vsis;
7072 pf->vsi = kzalloc(len, GFP_KERNEL);
7073 if (!pf->vsi)
7074 goto err_switch_setup;
7075
7076 err = i40e_setup_pf_switch(pf);
7077 if (err) {
7078 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
7079 goto err_vsis;
7080 }
7081
7082 /* The main driver is (mostly) up and happy. We need to set this state
7083 * before setting up the misc vector or we get a race and the vector
7084 * ends up disabled forever.
7085 */
7086 clear_bit(__I40E_DOWN, &pf->state);
7087
7088 /* In case of MSIX we are going to setup the misc vector right here
7089 * to handle admin queue events etc. In case of legacy and MSI
7090 * the misc functionality and queue processing is combined in
7091 * the same vector and that gets setup at open.
7092 */
7093 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
7094 err = i40e_setup_misc_vector(pf);
7095 if (err) {
7096 dev_info(&pdev->dev,
7097 "setup of misc vector failed: %d\n", err);
7098 goto err_vsis;
7099 }
7100 }
7101
7102 /* prep for VF support */
7103 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
7104 (pf->flags & I40E_FLAG_MSIX_ENABLED)) {
7105 u32 val;
7106
7107 /* disable link interrupts for VFs */
7108 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
7109 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
7110 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
7111 i40e_flush(hw);
7112 }
7113
7114 i40e_dbg_pf_init(pf);
7115
7116 /* tell the firmware that we're starting */
7117 dv.major_version = DRV_VERSION_MAJOR;
7118 dv.minor_version = DRV_VERSION_MINOR;
7119 dv.build_version = DRV_VERSION_BUILD;
7120 dv.subbuild_version = 0;
7121 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
7122
7123 /* since everything's happy, start the service_task timer */
7124 mod_timer(&pf->service_timer,
7125 round_jiffies(jiffies + pf->service_timer_period));
7126
7127 return 0;
7128
7129 /* Unwind what we've done if something failed in the setup */
7130err_vsis:
7131 set_bit(__I40E_DOWN, &pf->state);
7132err_switch_setup:
7133 i40e_clear_interrupt_scheme(pf);
7134 kfree(pf->vsi);
7135 del_timer_sync(&pf->service_timer);
7136err_mac_addr:
7137err_configure_lan_hmc:
7138 (void)i40e_shutdown_lan_hmc(hw);
7139err_init_lan_hmc:
7140 kfree(pf->qp_pile);
7141 kfree(pf->irq_pile);
7142err_sw_init:
7143err_adminq_setup:
7144 (void)i40e_shutdown_adminq(hw);
7145err_pf_reset:
7146 iounmap(hw->hw_addr);
7147err_ioremap:
7148 kfree(pf);
7149err_pf_alloc:
7150 pci_disable_pcie_error_reporting(pdev);
7151 pci_release_selected_regions(pdev,
7152 pci_select_bars(pdev, IORESOURCE_MEM));
7153err_pci_reg:
7154err_dma:
7155 pci_disable_device(pdev);
7156 return err;
7157}
7158
7159/**
7160 * i40e_remove - Device removal routine
7161 * @pdev: PCI device information struct
7162 *
7163 * i40e_remove is called by the PCI subsystem to alert the driver
7164 * that is should release a PCI device. This could be caused by a
7165 * Hot-Plug event, or because the driver is going to be removed from
7166 * memory.
7167 **/
7168static void i40e_remove(struct pci_dev *pdev)
7169{
7170 struct i40e_pf *pf = pci_get_drvdata(pdev);
7171 i40e_status ret_code;
7172 u32 reg;
7173 int i;
7174
7175 i40e_dbg_pf_exit(pf);
7176
7177 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
7178 i40e_free_vfs(pf);
7179 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
7180 }
7181
7182 /* no more scheduling of any task */
7183 set_bit(__I40E_DOWN, &pf->state);
7184 del_timer_sync(&pf->service_timer);
7185 cancel_work_sync(&pf->service_task);
7186
7187 i40e_fdir_teardown(pf);
7188
7189 /* If there is a switch structure or any orphans, remove them.
7190 * This will leave only the PF's VSI remaining.
7191 */
7192 for (i = 0; i < I40E_MAX_VEB; i++) {
7193 if (!pf->veb[i])
7194 continue;
7195
7196 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
7197 pf->veb[i]->uplink_seid == 0)
7198 i40e_switch_branch_release(pf->veb[i]);
7199 }
7200
7201 /* Now we can shutdown the PF's VSI, just before we kill
7202 * adminq and hmc.
7203 */
7204 if (pf->vsi[pf->lan_vsi])
7205 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
7206
7207 i40e_stop_misc_vector(pf);
7208 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
7209 synchronize_irq(pf->msix_entries[0].vector);
7210 free_irq(pf->msix_entries[0].vector, pf);
7211 }
7212
7213 /* shutdown and destroy the HMC */
7214 ret_code = i40e_shutdown_lan_hmc(&pf->hw);
7215 if (ret_code)
7216 dev_warn(&pdev->dev,
7217 "Failed to destroy the HMC resources: %d\n", ret_code);
7218
7219 /* shutdown the adminq */
7220 i40e_aq_queue_shutdown(&pf->hw, true);
7221 ret_code = i40e_shutdown_adminq(&pf->hw);
7222 if (ret_code)
7223 dev_warn(&pdev->dev,
7224 "Failed to destroy the Admin Queue resources: %d\n",
7225 ret_code);
7226
7227 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
7228 i40e_clear_interrupt_scheme(pf);
7229 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
7230 if (pf->vsi[i]) {
7231 i40e_vsi_clear_rings(pf->vsi[i]);
7232 i40e_vsi_clear(pf->vsi[i]);
7233 pf->vsi[i] = NULL;
7234 }
7235 }
7236
7237 for (i = 0; i < I40E_MAX_VEB; i++) {
7238 kfree(pf->veb[i]);
7239 pf->veb[i] = NULL;
7240 }
7241
7242 kfree(pf->qp_pile);
7243 kfree(pf->irq_pile);
7244 kfree(pf->sw_config);
7245 kfree(pf->vsi);
7246
7247 /* force a PF reset to clean anything leftover */
7248 reg = rd32(&pf->hw, I40E_PFGEN_CTRL);
7249 wr32(&pf->hw, I40E_PFGEN_CTRL, (reg | I40E_PFGEN_CTRL_PFSWR_MASK));
7250 i40e_flush(&pf->hw);
7251
7252 iounmap(pf->hw.hw_addr);
7253 kfree(pf);
7254 pci_release_selected_regions(pdev,
7255 pci_select_bars(pdev, IORESOURCE_MEM));
7256
7257 pci_disable_pcie_error_reporting(pdev);
7258 pci_disable_device(pdev);
7259}
7260
7261/**
7262 * i40e_pci_error_detected - warning that something funky happened in PCI land
7263 * @pdev: PCI device information struct
7264 *
7265 * Called to warn that something happened and the error handling steps
7266 * are in progress. Allows the driver to quiesce things, be ready for
7267 * remediation.
7268 **/
7269static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
7270 enum pci_channel_state error)
7271{
7272 struct i40e_pf *pf = pci_get_drvdata(pdev);
7273
7274 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
7275
7276 /* shutdown all operations */
7277 i40e_pf_quiesce_all_vsi(pf);
7278
7279 /* Request a slot reset */
7280 return PCI_ERS_RESULT_NEED_RESET;
7281}
7282
7283/**
7284 * i40e_pci_error_slot_reset - a PCI slot reset just happened
7285 * @pdev: PCI device information struct
7286 *
7287 * Called to find if the driver can work with the device now that
7288 * the pci slot has been reset. If a basic connection seems good
7289 * (registers are readable and have sane content) then return a
7290 * happy little PCI_ERS_RESULT_xxx.
7291 **/
7292static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
7293{
7294 struct i40e_pf *pf = pci_get_drvdata(pdev);
7295 pci_ers_result_t result;
7296 int err;
7297 u32 reg;
7298
7299 dev_info(&pdev->dev, "%s\n", __func__);
7300 if (pci_enable_device_mem(pdev)) {
7301 dev_info(&pdev->dev,
7302 "Cannot re-enable PCI device after reset.\n");
7303 result = PCI_ERS_RESULT_DISCONNECT;
7304 } else {
7305 pci_set_master(pdev);
7306 pci_restore_state(pdev);
7307 pci_save_state(pdev);
7308 pci_wake_from_d3(pdev, false);
7309
7310 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
7311 if (reg == 0)
7312 result = PCI_ERS_RESULT_RECOVERED;
7313 else
7314 result = PCI_ERS_RESULT_DISCONNECT;
7315 }
7316
7317 err = pci_cleanup_aer_uncorrect_error_status(pdev);
7318 if (err) {
7319 dev_info(&pdev->dev,
7320 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
7321 err);
7322 /* non-fatal, continue */
7323 }
7324
7325 return result;
7326}
7327
7328/**
7329 * i40e_pci_error_resume - restart operations after PCI error recovery
7330 * @pdev: PCI device information struct
7331 *
7332 * Called to allow the driver to bring things back up after PCI error
7333 * and/or reset recovery has finished.
7334 **/
7335static void i40e_pci_error_resume(struct pci_dev *pdev)
7336{
7337 struct i40e_pf *pf = pci_get_drvdata(pdev);
7338
7339 dev_info(&pdev->dev, "%s\n", __func__);
7340 i40e_handle_reset_warning(pf);
7341}
7342
7343static const struct pci_error_handlers i40e_err_handler = {
7344 .error_detected = i40e_pci_error_detected,
7345 .slot_reset = i40e_pci_error_slot_reset,
7346 .resume = i40e_pci_error_resume,
7347};
7348
7349static struct pci_driver i40e_driver = {
7350 .name = i40e_driver_name,
7351 .id_table = i40e_pci_tbl,
7352 .probe = i40e_probe,
7353 .remove = i40e_remove,
7354 .err_handler = &i40e_err_handler,
7355 .sriov_configure = i40e_pci_sriov_configure,
7356};
7357
7358/**
7359 * i40e_init_module - Driver registration routine
7360 *
7361 * i40e_init_module is the first routine called when the driver is
7362 * loaded. All it does is register with the PCI subsystem.
7363 **/
7364static int __init i40e_init_module(void)
7365{
7366 pr_info("%s: %s - version %s\n", i40e_driver_name,
7367 i40e_driver_string, i40e_driver_version_str);
7368 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
7369 i40e_dbg_init();
7370 return pci_register_driver(&i40e_driver);
7371}
7372module_init(i40e_init_module);
7373
7374/**
7375 * i40e_exit_module - Driver exit cleanup routine
7376 *
7377 * i40e_exit_module is called just before the driver is removed
7378 * from memory.
7379 **/
7380static void __exit i40e_exit_module(void)
7381{
7382 pci_unregister_driver(&i40e_driver);
7383 i40e_dbg_exit();
7384}
7385module_exit(i40e_exit_module);