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