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