blob: 97add1f2a1d6885b840ecec78cac90088c694dc4 [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"
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000030#ifdef CONFIG_I40E_VXLAN
31#include <net/vxlan.h>
32#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000033
34const char i40e_driver_name[] = "i40e";
35static const char i40e_driver_string[] =
36 "Intel(R) Ethernet Connection XL710 Network Driver";
37
38#define DRV_KERN "-k"
39
40#define DRV_VERSION_MAJOR 0
41#define DRV_VERSION_MINOR 3
Catherine Sullivan582a74e2013-11-26 10:49:33 +000042#define DRV_VERSION_BUILD 14
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000043#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
44 __stringify(DRV_VERSION_MINOR) "." \
45 __stringify(DRV_VERSION_BUILD) DRV_KERN
46const char i40e_driver_version_str[] = DRV_VERSION;
47static const char i40e_copyright[] = "Copyright (c) 2013 Intel Corporation.";
48
49/* a bit of forward declarations */
50static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
51static void i40e_handle_reset_warning(struct i40e_pf *pf);
52static int i40e_add_vsi(struct i40e_vsi *vsi);
53static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000054static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000055static int i40e_setup_misc_vector(struct i40e_pf *pf);
56static void i40e_determine_queue_usage(struct i40e_pf *pf);
57static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
58
59/* i40e_pci_tbl - PCI Device ID Table
60 *
61 * Last entry must be all 0s
62 *
63 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
64 * Class, Class Mask, private data (not used) }
65 */
66static DEFINE_PCI_DEVICE_TABLE(i40e_pci_tbl) = {
67 {PCI_VDEVICE(INTEL, I40E_SFP_XL710_DEVICE_ID), 0},
68 {PCI_VDEVICE(INTEL, I40E_SFP_X710_DEVICE_ID), 0},
69 {PCI_VDEVICE(INTEL, I40E_QEMU_DEVICE_ID), 0},
70 {PCI_VDEVICE(INTEL, I40E_KX_A_DEVICE_ID), 0},
71 {PCI_VDEVICE(INTEL, I40E_KX_B_DEVICE_ID), 0},
72 {PCI_VDEVICE(INTEL, I40E_KX_C_DEVICE_ID), 0},
73 {PCI_VDEVICE(INTEL, I40E_KX_D_DEVICE_ID), 0},
74 {PCI_VDEVICE(INTEL, I40E_QSFP_A_DEVICE_ID), 0},
75 {PCI_VDEVICE(INTEL, I40E_QSFP_B_DEVICE_ID), 0},
76 {PCI_VDEVICE(INTEL, I40E_QSFP_C_DEVICE_ID), 0},
77 /* required last entry */
78 {0, }
79};
80MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
81
82#define I40E_MAX_VF_COUNT 128
83static int debug = -1;
84module_param(debug, int, 0);
85MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
86
87MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
88MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
89MODULE_LICENSE("GPL");
90MODULE_VERSION(DRV_VERSION);
91
92/**
93 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
94 * @hw: pointer to the HW structure
95 * @mem: ptr to mem struct to fill out
96 * @size: size of memory requested
97 * @alignment: what to align the allocation to
98 **/
99int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
100 u64 size, u32 alignment)
101{
102 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
103
104 mem->size = ALIGN(size, alignment);
105 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
106 &mem->pa, GFP_KERNEL);
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000107 if (!mem->va)
108 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000109
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000110 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000111}
112
113/**
114 * i40e_free_dma_mem_d - OS specific memory free for shared code
115 * @hw: pointer to the HW structure
116 * @mem: ptr to mem struct to free
117 **/
118int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
119{
120 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
121
122 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
123 mem->va = NULL;
124 mem->pa = 0;
125 mem->size = 0;
126
127 return 0;
128}
129
130/**
131 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
132 * @hw: pointer to the HW structure
133 * @mem: ptr to mem struct to fill out
134 * @size: size of memory requested
135 **/
136int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
137 u32 size)
138{
139 mem->size = size;
140 mem->va = kzalloc(size, GFP_KERNEL);
141
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000142 if (!mem->va)
143 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000144
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000145 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000146}
147
148/**
149 * i40e_free_virt_mem_d - OS specific memory free for shared code
150 * @hw: pointer to the HW structure
151 * @mem: ptr to mem struct to free
152 **/
153int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
154{
155 /* it's ok to kfree a NULL pointer */
156 kfree(mem->va);
157 mem->va = NULL;
158 mem->size = 0;
159
160 return 0;
161}
162
163/**
164 * i40e_get_lump - find a lump of free generic resource
165 * @pf: board private structure
166 * @pile: the pile of resource to search
167 * @needed: the number of items needed
168 * @id: an owner id to stick on the items assigned
169 *
170 * Returns the base item index of the lump, or negative for error
171 *
172 * The search_hint trick and lack of advanced fit-finding only work
173 * because we're highly likely to have all the same size lump requests.
174 * Linear search time and any fragmentation should be minimal.
175 **/
176static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
177 u16 needed, u16 id)
178{
179 int ret = -ENOMEM;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000180 int i, j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000181
182 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
183 dev_info(&pf->pdev->dev,
184 "param err: pile=%p needed=%d id=0x%04x\n",
185 pile, needed, id);
186 return -EINVAL;
187 }
188
189 /* start the linear search with an imperfect hint */
190 i = pile->search_hint;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000191 while (i < pile->num_entries) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000192 /* skip already allocated entries */
193 if (pile->list[i] & I40E_PILE_VALID_BIT) {
194 i++;
195 continue;
196 }
197
198 /* do we have enough in this lump? */
199 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
200 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
201 break;
202 }
203
204 if (j == needed) {
205 /* there was enough, so assign it to the requestor */
206 for (j = 0; j < needed; j++)
207 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
208 ret = i;
209 pile->search_hint = i + j;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000210 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000211 } else {
212 /* not enough, so skip over it and continue looking */
213 i += j;
214 }
215 }
216
217 return ret;
218}
219
220/**
221 * i40e_put_lump - return a lump of generic resource
222 * @pile: the pile of resource to search
223 * @index: the base item index
224 * @id: the owner id of the items assigned
225 *
226 * Returns the count of items in the lump
227 **/
228static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
229{
230 int valid_id = (id | I40E_PILE_VALID_BIT);
231 int count = 0;
232 int i;
233
234 if (!pile || index >= pile->num_entries)
235 return -EINVAL;
236
237 for (i = index;
238 i < pile->num_entries && pile->list[i] == valid_id;
239 i++) {
240 pile->list[i] = 0;
241 count++;
242 }
243
244 if (count && index < pile->search_hint)
245 pile->search_hint = index;
246
247 return count;
248}
249
250/**
251 * i40e_service_event_schedule - Schedule the service task to wake up
252 * @pf: board private structure
253 *
254 * If not already scheduled, this puts the task into the work queue
255 **/
256static void i40e_service_event_schedule(struct i40e_pf *pf)
257{
258 if (!test_bit(__I40E_DOWN, &pf->state) &&
259 !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) &&
260 !test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
261 schedule_work(&pf->service_task);
262}
263
264/**
265 * i40e_tx_timeout - Respond to a Tx Hang
266 * @netdev: network interface device structure
267 *
268 * If any port has noticed a Tx timeout, it is likely that the whole
269 * device is munged, not just the one netdev port, so go for the full
270 * reset.
271 **/
272static void i40e_tx_timeout(struct net_device *netdev)
273{
274 struct i40e_netdev_priv *np = netdev_priv(netdev);
275 struct i40e_vsi *vsi = np->vsi;
276 struct i40e_pf *pf = vsi->back;
277
278 pf->tx_timeout_count++;
279
280 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
281 pf->tx_timeout_recovery_level = 0;
282 pf->tx_timeout_last_recovery = jiffies;
283 netdev_info(netdev, "tx_timeout recovery level %d\n",
284 pf->tx_timeout_recovery_level);
285
286 switch (pf->tx_timeout_recovery_level) {
287 case 0:
288 /* disable and re-enable queues for the VSI */
289 if (in_interrupt()) {
290 set_bit(__I40E_REINIT_REQUESTED, &pf->state);
291 set_bit(__I40E_REINIT_REQUESTED, &vsi->state);
292 } else {
293 i40e_vsi_reinit_locked(vsi);
294 }
295 break;
296 case 1:
297 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
298 break;
299 case 2:
300 set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
301 break;
302 case 3:
303 set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
304 break;
305 default:
306 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
307 i40e_down(vsi);
308 break;
309 }
310 i40e_service_event_schedule(pf);
311 pf->tx_timeout_recovery_level++;
312}
313
314/**
315 * i40e_release_rx_desc - Store the new tail and head values
316 * @rx_ring: ring to bump
317 * @val: new head index
318 **/
319static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
320{
321 rx_ring->next_to_use = val;
322
323 /* Force memory writes to complete before letting h/w
324 * know there are new descriptors to fetch. (Only
325 * applicable for weak-ordered memory model archs,
326 * such as IA-64).
327 */
328 wmb();
329 writel(val, rx_ring->tail);
330}
331
332/**
333 * i40e_get_vsi_stats_struct - Get System Network Statistics
334 * @vsi: the VSI we care about
335 *
336 * Returns the address of the device statistics structure.
337 * The statistics are actually updated from the service task.
338 **/
339struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
340{
341 return &vsi->net_stats;
342}
343
344/**
345 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
346 * @netdev: network interface device structure
347 *
348 * Returns the address of the device statistics structure.
349 * The statistics are actually updated from the service task.
350 **/
351static struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
352 struct net_device *netdev,
Alexander Duyck980e9b12013-09-28 06:01:03 +0000353 struct rtnl_link_stats64 *stats)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000354{
355 struct i40e_netdev_priv *np = netdev_priv(netdev);
356 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000357 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
358 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000359
David S. Miller143c9052013-12-18 16:42:06 -0500360
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000361 if (test_bit(__I40E_DOWN, &vsi->state))
362 return stats;
363
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800364 if (!vsi->tx_rings)
365 return stats;
366
Alexander Duyck980e9b12013-09-28 06:01:03 +0000367 rcu_read_lock();
368 for (i = 0; i < vsi->num_queue_pairs; i++) {
369 struct i40e_ring *tx_ring, *rx_ring;
370 u64 bytes, packets;
371 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000372
Alexander Duyck980e9b12013-09-28 06:01:03 +0000373 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
374 if (!tx_ring)
375 continue;
376
377 do {
378 start = u64_stats_fetch_begin_bh(&tx_ring->syncp);
379 packets = tx_ring->stats.packets;
380 bytes = tx_ring->stats.bytes;
381 } while (u64_stats_fetch_retry_bh(&tx_ring->syncp, start));
382
383 stats->tx_packets += packets;
384 stats->tx_bytes += bytes;
385 rx_ring = &tx_ring[1];
386
387 do {
388 start = u64_stats_fetch_begin_bh(&rx_ring->syncp);
389 packets = rx_ring->stats.packets;
390 bytes = rx_ring->stats.bytes;
391 } while (u64_stats_fetch_retry_bh(&rx_ring->syncp, start));
392
393 stats->rx_packets += packets;
394 stats->rx_bytes += bytes;
395 }
396 rcu_read_unlock();
397
398 /* following stats updated by ixgbe_watchdog_task() */
399 stats->multicast = vsi_stats->multicast;
400 stats->tx_errors = vsi_stats->tx_errors;
401 stats->tx_dropped = vsi_stats->tx_dropped;
402 stats->rx_errors = vsi_stats->rx_errors;
403 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
404 stats->rx_length_errors = vsi_stats->rx_length_errors;
405
406 return stats;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000407}
408
409/**
410 * i40e_vsi_reset_stats - Resets all stats of the given vsi
411 * @vsi: the VSI to have its stats reset
412 **/
413void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
414{
415 struct rtnl_link_stats64 *ns;
416 int i;
417
418 if (!vsi)
419 return;
420
421 ns = i40e_get_vsi_stats_struct(vsi);
422 memset(ns, 0, sizeof(*ns));
423 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
424 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
425 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
426 if (vsi->rx_rings)
427 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000428 memset(&vsi->rx_rings[i]->stats, 0 ,
429 sizeof(vsi->rx_rings[i]->stats));
430 memset(&vsi->rx_rings[i]->rx_stats, 0 ,
431 sizeof(vsi->rx_rings[i]->rx_stats));
432 memset(&vsi->tx_rings[i]->stats, 0 ,
433 sizeof(vsi->tx_rings[i]->stats));
434 memset(&vsi->tx_rings[i]->tx_stats, 0,
435 sizeof(vsi->tx_rings[i]->tx_stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000436 }
437 vsi->stat_offsets_loaded = false;
438}
439
440/**
441 * i40e_pf_reset_stats - Reset all of the stats for the given pf
442 * @pf: the PF to be reset
443 **/
444void i40e_pf_reset_stats(struct i40e_pf *pf)
445{
446 memset(&pf->stats, 0, sizeof(pf->stats));
447 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
448 pf->stat_offsets_loaded = false;
449}
450
451/**
452 * i40e_stat_update48 - read and update a 48 bit stat from the chip
453 * @hw: ptr to the hardware info
454 * @hireg: the high 32 bit reg to read
455 * @loreg: the low 32 bit reg to read
456 * @offset_loaded: has the initial offset been loaded yet
457 * @offset: ptr to current offset value
458 * @stat: ptr to the stat
459 *
460 * Since the device stats are not reset at PFReset, they likely will not
461 * be zeroed when the driver starts. We'll save the first values read
462 * and use them as offsets to be subtracted from the raw values in order
463 * to report stats that count from zero. In the process, we also manage
464 * the potential roll-over.
465 **/
466static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
467 bool offset_loaded, u64 *offset, u64 *stat)
468{
469 u64 new_data;
470
471 if (hw->device_id == I40E_QEMU_DEVICE_ID) {
472 new_data = rd32(hw, loreg);
473 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
474 } else {
475 new_data = rd64(hw, loreg);
476 }
477 if (!offset_loaded)
478 *offset = new_data;
479 if (likely(new_data >= *offset))
480 *stat = new_data - *offset;
481 else
482 *stat = (new_data + ((u64)1 << 48)) - *offset;
483 *stat &= 0xFFFFFFFFFFFFULL;
484}
485
486/**
487 * i40e_stat_update32 - read and update a 32 bit stat from the chip
488 * @hw: ptr to the hardware info
489 * @reg: the hw reg to read
490 * @offset_loaded: has the initial offset been loaded yet
491 * @offset: ptr to current offset value
492 * @stat: ptr to the stat
493 **/
494static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
495 bool offset_loaded, u64 *offset, u64 *stat)
496{
497 u32 new_data;
498
499 new_data = rd32(hw, reg);
500 if (!offset_loaded)
501 *offset = new_data;
502 if (likely(new_data >= *offset))
503 *stat = (u32)(new_data - *offset);
504 else
505 *stat = (u32)((new_data + ((u64)1 << 32)) - *offset);
506}
507
508/**
509 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
510 * @vsi: the VSI to be updated
511 **/
512void i40e_update_eth_stats(struct i40e_vsi *vsi)
513{
514 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
515 struct i40e_pf *pf = vsi->back;
516 struct i40e_hw *hw = &pf->hw;
517 struct i40e_eth_stats *oes;
518 struct i40e_eth_stats *es; /* device's eth stats */
519
520 es = &vsi->eth_stats;
521 oes = &vsi->eth_stats_offsets;
522
523 /* Gather up the stats that the hw collects */
524 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
525 vsi->stat_offsets_loaded,
526 &oes->tx_errors, &es->tx_errors);
527 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
528 vsi->stat_offsets_loaded,
529 &oes->rx_discards, &es->rx_discards);
530
531 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
532 I40E_GLV_GORCL(stat_idx),
533 vsi->stat_offsets_loaded,
534 &oes->rx_bytes, &es->rx_bytes);
535 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
536 I40E_GLV_UPRCL(stat_idx),
537 vsi->stat_offsets_loaded,
538 &oes->rx_unicast, &es->rx_unicast);
539 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
540 I40E_GLV_MPRCL(stat_idx),
541 vsi->stat_offsets_loaded,
542 &oes->rx_multicast, &es->rx_multicast);
543 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
544 I40E_GLV_BPRCL(stat_idx),
545 vsi->stat_offsets_loaded,
546 &oes->rx_broadcast, &es->rx_broadcast);
547
548 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
549 I40E_GLV_GOTCL(stat_idx),
550 vsi->stat_offsets_loaded,
551 &oes->tx_bytes, &es->tx_bytes);
552 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
553 I40E_GLV_UPTCL(stat_idx),
554 vsi->stat_offsets_loaded,
555 &oes->tx_unicast, &es->tx_unicast);
556 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
557 I40E_GLV_MPTCL(stat_idx),
558 vsi->stat_offsets_loaded,
559 &oes->tx_multicast, &es->tx_multicast);
560 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
561 I40E_GLV_BPTCL(stat_idx),
562 vsi->stat_offsets_loaded,
563 &oes->tx_broadcast, &es->tx_broadcast);
564 vsi->stat_offsets_loaded = true;
565}
566
567/**
568 * i40e_update_veb_stats - Update Switch component statistics
569 * @veb: the VEB being updated
570 **/
571static void i40e_update_veb_stats(struct i40e_veb *veb)
572{
573 struct i40e_pf *pf = veb->pf;
574 struct i40e_hw *hw = &pf->hw;
575 struct i40e_eth_stats *oes;
576 struct i40e_eth_stats *es; /* device's eth stats */
577 int idx = 0;
578
579 idx = veb->stats_idx;
580 es = &veb->stats;
581 oes = &veb->stats_offsets;
582
583 /* Gather up the stats that the hw collects */
584 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
585 veb->stat_offsets_loaded,
586 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000587 if (hw->revision_id > 0)
588 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
589 veb->stat_offsets_loaded,
590 &oes->rx_unknown_protocol,
591 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000592 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
593 veb->stat_offsets_loaded,
594 &oes->rx_bytes, &es->rx_bytes);
595 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
596 veb->stat_offsets_loaded,
597 &oes->rx_unicast, &es->rx_unicast);
598 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
599 veb->stat_offsets_loaded,
600 &oes->rx_multicast, &es->rx_multicast);
601 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
602 veb->stat_offsets_loaded,
603 &oes->rx_broadcast, &es->rx_broadcast);
604
605 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
606 veb->stat_offsets_loaded,
607 &oes->tx_bytes, &es->tx_bytes);
608 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
609 veb->stat_offsets_loaded,
610 &oes->tx_unicast, &es->tx_unicast);
611 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
612 veb->stat_offsets_loaded,
613 &oes->tx_multicast, &es->tx_multicast);
614 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
615 veb->stat_offsets_loaded,
616 &oes->tx_broadcast, &es->tx_broadcast);
617 veb->stat_offsets_loaded = true;
618}
619
620/**
621 * i40e_update_link_xoff_rx - Update XOFF received in link flow control mode
622 * @pf: the corresponding PF
623 *
624 * Update the Rx XOFF counter (PAUSE frames) in link flow control mode
625 **/
626static void i40e_update_link_xoff_rx(struct i40e_pf *pf)
627{
628 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
629 struct i40e_hw_port_stats *nsd = &pf->stats;
630 struct i40e_hw *hw = &pf->hw;
631 u64 xoff = 0;
632 u16 i, v;
633
634 if ((hw->fc.current_mode != I40E_FC_FULL) &&
635 (hw->fc.current_mode != I40E_FC_RX_PAUSE))
636 return;
637
638 xoff = nsd->link_xoff_rx;
639 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
640 pf->stat_offsets_loaded,
641 &osd->link_xoff_rx, &nsd->link_xoff_rx);
642
643 /* No new LFC xoff rx */
644 if (!(nsd->link_xoff_rx - xoff))
645 return;
646
647 /* Clear the __I40E_HANG_CHECK_ARMED bit for all Tx rings */
648 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
649 struct i40e_vsi *vsi = pf->vsi[v];
650
651 if (!vsi)
652 continue;
653
654 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000655 struct i40e_ring *ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000656 clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
657 }
658 }
659}
660
661/**
662 * i40e_update_prio_xoff_rx - Update XOFF received in PFC mode
663 * @pf: the corresponding PF
664 *
665 * Update the Rx XOFF counter (PAUSE frames) in PFC mode
666 **/
667static void i40e_update_prio_xoff_rx(struct i40e_pf *pf)
668{
669 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
670 struct i40e_hw_port_stats *nsd = &pf->stats;
671 bool xoff[I40E_MAX_TRAFFIC_CLASS] = {false};
672 struct i40e_dcbx_config *dcb_cfg;
673 struct i40e_hw *hw = &pf->hw;
674 u16 i, v;
675 u8 tc;
676
677 dcb_cfg = &hw->local_dcbx_config;
678
679 /* See if DCB enabled with PFC TC */
680 if (!(pf->flags & I40E_FLAG_DCB_ENABLED) ||
681 !(dcb_cfg->pfc.pfcenable)) {
682 i40e_update_link_xoff_rx(pf);
683 return;
684 }
685
686 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
687 u64 prio_xoff = nsd->priority_xoff_rx[i];
688 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
689 pf->stat_offsets_loaded,
690 &osd->priority_xoff_rx[i],
691 &nsd->priority_xoff_rx[i]);
692
693 /* No new PFC xoff rx */
694 if (!(nsd->priority_xoff_rx[i] - prio_xoff))
695 continue;
696 /* Get the TC for given priority */
697 tc = dcb_cfg->etscfg.prioritytable[i];
698 xoff[tc] = true;
699 }
700
701 /* Clear the __I40E_HANG_CHECK_ARMED bit for Tx rings */
702 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
703 struct i40e_vsi *vsi = pf->vsi[v];
704
705 if (!vsi)
706 continue;
707
708 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000709 struct i40e_ring *ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000710
711 tc = ring->dcb_tc;
712 if (xoff[tc])
713 clear_bit(__I40E_HANG_CHECK_ARMED,
714 &ring->state);
715 }
716 }
717}
718
719/**
720 * i40e_update_stats - Update the board statistics counters.
721 * @vsi: the VSI to be updated
722 *
723 * There are a few instances where we store the same stat in a
724 * couple of different structs. This is partly because we have
725 * the netdev stats that need to be filled out, which is slightly
726 * different from the "eth_stats" defined by the chip and used in
727 * VF communications. We sort it all out here in a central place.
728 **/
729void i40e_update_stats(struct i40e_vsi *vsi)
730{
731 struct i40e_pf *pf = vsi->back;
732 struct i40e_hw *hw = &pf->hw;
733 struct rtnl_link_stats64 *ons;
734 struct rtnl_link_stats64 *ns; /* netdev stats */
735 struct i40e_eth_stats *oes;
736 struct i40e_eth_stats *es; /* device's eth stats */
737 u32 tx_restart, tx_busy;
738 u32 rx_page, rx_buf;
739 u64 rx_p, rx_b;
740 u64 tx_p, tx_b;
741 int i;
742 u16 q;
743
744 if (test_bit(__I40E_DOWN, &vsi->state) ||
745 test_bit(__I40E_CONFIG_BUSY, &pf->state))
746 return;
747
748 ns = i40e_get_vsi_stats_struct(vsi);
749 ons = &vsi->net_stats_offsets;
750 es = &vsi->eth_stats;
751 oes = &vsi->eth_stats_offsets;
752
753 /* Gather up the netdev and vsi stats that the driver collects
754 * on the fly during packet processing
755 */
756 rx_b = rx_p = 0;
757 tx_b = tx_p = 0;
758 tx_restart = tx_busy = 0;
759 rx_page = 0;
760 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000761 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000762 for (q = 0; q < vsi->num_queue_pairs; q++) {
763 struct i40e_ring *p;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000764 u64 bytes, packets;
765 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000766
Alexander Duyck980e9b12013-09-28 06:01:03 +0000767 /* locate Tx ring */
768 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000769
Alexander Duyck980e9b12013-09-28 06:01:03 +0000770 do {
771 start = u64_stats_fetch_begin_bh(&p->syncp);
772 packets = p->stats.packets;
773 bytes = p->stats.bytes;
774 } while (u64_stats_fetch_retry_bh(&p->syncp, start));
775 tx_b += bytes;
776 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000777 tx_restart += p->tx_stats.restart_queue;
778 tx_busy += p->tx_stats.tx_busy;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000779
780 /* Rx queue is part of the same block as Tx queue */
781 p = &p[1];
782 do {
783 start = u64_stats_fetch_begin_bh(&p->syncp);
784 packets = p->stats.packets;
785 bytes = p->stats.bytes;
786 } while (u64_stats_fetch_retry_bh(&p->syncp, start));
787 rx_b += bytes;
788 rx_p += packets;
789 rx_buf += p->rx_stats.alloc_rx_buff_failed;
790 rx_page += p->rx_stats.alloc_rx_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000791 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000792 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000793 vsi->tx_restart = tx_restart;
794 vsi->tx_busy = tx_busy;
795 vsi->rx_page_failed = rx_page;
796 vsi->rx_buf_failed = rx_buf;
797
798 ns->rx_packets = rx_p;
799 ns->rx_bytes = rx_b;
800 ns->tx_packets = tx_p;
801 ns->tx_bytes = tx_b;
802
803 i40e_update_eth_stats(vsi);
804 /* update netdev stats from eth stats */
805 ons->rx_errors = oes->rx_errors;
806 ns->rx_errors = es->rx_errors;
807 ons->tx_errors = oes->tx_errors;
808 ns->tx_errors = es->tx_errors;
809 ons->multicast = oes->rx_multicast;
810 ns->multicast = es->rx_multicast;
811 ons->tx_dropped = oes->tx_discards;
812 ns->tx_dropped = es->tx_discards;
813
814 /* Get the port data only if this is the main PF VSI */
815 if (vsi == pf->vsi[pf->lan_vsi]) {
816 struct i40e_hw_port_stats *nsd = &pf->stats;
817 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
818
819 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
820 I40E_GLPRT_GORCL(hw->port),
821 pf->stat_offsets_loaded,
822 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
823 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
824 I40E_GLPRT_GOTCL(hw->port),
825 pf->stat_offsets_loaded,
826 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
827 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
828 pf->stat_offsets_loaded,
829 &osd->eth.rx_discards,
830 &nsd->eth.rx_discards);
831 i40e_stat_update32(hw, I40E_GLPRT_TDPC(hw->port),
832 pf->stat_offsets_loaded,
833 &osd->eth.tx_discards,
834 &nsd->eth.tx_discards);
835 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
836 I40E_GLPRT_MPRCL(hw->port),
837 pf->stat_offsets_loaded,
838 &osd->eth.rx_multicast,
839 &nsd->eth.rx_multicast);
840
841 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
842 pf->stat_offsets_loaded,
843 &osd->tx_dropped_link_down,
844 &nsd->tx_dropped_link_down);
845
846 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
847 pf->stat_offsets_loaded,
848 &osd->crc_errors, &nsd->crc_errors);
849 ns->rx_crc_errors = nsd->crc_errors;
850
851 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
852 pf->stat_offsets_loaded,
853 &osd->illegal_bytes, &nsd->illegal_bytes);
854 ns->rx_errors = nsd->crc_errors
855 + nsd->illegal_bytes;
856
857 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
858 pf->stat_offsets_loaded,
859 &osd->mac_local_faults,
860 &nsd->mac_local_faults);
861 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
862 pf->stat_offsets_loaded,
863 &osd->mac_remote_faults,
864 &nsd->mac_remote_faults);
865
866 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
867 pf->stat_offsets_loaded,
868 &osd->rx_length_errors,
869 &nsd->rx_length_errors);
870 ns->rx_length_errors = nsd->rx_length_errors;
871
872 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
873 pf->stat_offsets_loaded,
874 &osd->link_xon_rx, &nsd->link_xon_rx);
875 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
876 pf->stat_offsets_loaded,
877 &osd->link_xon_tx, &nsd->link_xon_tx);
878 i40e_update_prio_xoff_rx(pf); /* handles I40E_GLPRT_LXOFFRXC */
879 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
880 pf->stat_offsets_loaded,
881 &osd->link_xoff_tx, &nsd->link_xoff_tx);
882
883 for (i = 0; i < 8; i++) {
884 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
885 pf->stat_offsets_loaded,
886 &osd->priority_xon_rx[i],
887 &nsd->priority_xon_rx[i]);
888 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
889 pf->stat_offsets_loaded,
890 &osd->priority_xon_tx[i],
891 &nsd->priority_xon_tx[i]);
892 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
893 pf->stat_offsets_loaded,
894 &osd->priority_xoff_tx[i],
895 &nsd->priority_xoff_tx[i]);
896 i40e_stat_update32(hw,
897 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
898 pf->stat_offsets_loaded,
899 &osd->priority_xon_2_xoff[i],
900 &nsd->priority_xon_2_xoff[i]);
901 }
902
903 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
904 I40E_GLPRT_PRC64L(hw->port),
905 pf->stat_offsets_loaded,
906 &osd->rx_size_64, &nsd->rx_size_64);
907 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
908 I40E_GLPRT_PRC127L(hw->port),
909 pf->stat_offsets_loaded,
910 &osd->rx_size_127, &nsd->rx_size_127);
911 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
912 I40E_GLPRT_PRC255L(hw->port),
913 pf->stat_offsets_loaded,
914 &osd->rx_size_255, &nsd->rx_size_255);
915 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
916 I40E_GLPRT_PRC511L(hw->port),
917 pf->stat_offsets_loaded,
918 &osd->rx_size_511, &nsd->rx_size_511);
919 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
920 I40E_GLPRT_PRC1023L(hw->port),
921 pf->stat_offsets_loaded,
922 &osd->rx_size_1023, &nsd->rx_size_1023);
923 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
924 I40E_GLPRT_PRC1522L(hw->port),
925 pf->stat_offsets_loaded,
926 &osd->rx_size_1522, &nsd->rx_size_1522);
927 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
928 I40E_GLPRT_PRC9522L(hw->port),
929 pf->stat_offsets_loaded,
930 &osd->rx_size_big, &nsd->rx_size_big);
931
932 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
933 I40E_GLPRT_PTC64L(hw->port),
934 pf->stat_offsets_loaded,
935 &osd->tx_size_64, &nsd->tx_size_64);
936 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
937 I40E_GLPRT_PTC127L(hw->port),
938 pf->stat_offsets_loaded,
939 &osd->tx_size_127, &nsd->tx_size_127);
940 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
941 I40E_GLPRT_PTC255L(hw->port),
942 pf->stat_offsets_loaded,
943 &osd->tx_size_255, &nsd->tx_size_255);
944 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
945 I40E_GLPRT_PTC511L(hw->port),
946 pf->stat_offsets_loaded,
947 &osd->tx_size_511, &nsd->tx_size_511);
948 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
949 I40E_GLPRT_PTC1023L(hw->port),
950 pf->stat_offsets_loaded,
951 &osd->tx_size_1023, &nsd->tx_size_1023);
952 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
953 I40E_GLPRT_PTC1522L(hw->port),
954 pf->stat_offsets_loaded,
955 &osd->tx_size_1522, &nsd->tx_size_1522);
956 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
957 I40E_GLPRT_PTC9522L(hw->port),
958 pf->stat_offsets_loaded,
959 &osd->tx_size_big, &nsd->tx_size_big);
960
961 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
962 pf->stat_offsets_loaded,
963 &osd->rx_undersize, &nsd->rx_undersize);
964 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
965 pf->stat_offsets_loaded,
966 &osd->rx_fragments, &nsd->rx_fragments);
967 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
968 pf->stat_offsets_loaded,
969 &osd->rx_oversize, &nsd->rx_oversize);
970 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
971 pf->stat_offsets_loaded,
972 &osd->rx_jabber, &nsd->rx_jabber);
973 }
974
975 pf->stat_offsets_loaded = true;
976}
977
978/**
979 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
980 * @vsi: the VSI to be searched
981 * @macaddr: the MAC address
982 * @vlan: the vlan
983 * @is_vf: make sure its a vf filter, else doesn't matter
984 * @is_netdev: make sure its a netdev filter, else doesn't matter
985 *
986 * Returns ptr to the filter object or NULL
987 **/
988static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
989 u8 *macaddr, s16 vlan,
990 bool is_vf, bool is_netdev)
991{
992 struct i40e_mac_filter *f;
993
994 if (!vsi || !macaddr)
995 return NULL;
996
997 list_for_each_entry(f, &vsi->mac_filter_list, list) {
998 if ((ether_addr_equal(macaddr, f->macaddr)) &&
999 (vlan == f->vlan) &&
1000 (!is_vf || f->is_vf) &&
1001 (!is_netdev || f->is_netdev))
1002 return f;
1003 }
1004 return NULL;
1005}
1006
1007/**
1008 * i40e_find_mac - Find a mac addr in the macvlan filters list
1009 * @vsi: the VSI to be searched
1010 * @macaddr: the MAC address we are searching for
1011 * @is_vf: make sure its a vf filter, else doesn't matter
1012 * @is_netdev: make sure its a netdev filter, else doesn't matter
1013 *
1014 * Returns the first filter with the provided MAC address or NULL if
1015 * MAC address was not found
1016 **/
1017struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, u8 *macaddr,
1018 bool is_vf, bool is_netdev)
1019{
1020 struct i40e_mac_filter *f;
1021
1022 if (!vsi || !macaddr)
1023 return NULL;
1024
1025 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1026 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1027 (!is_vf || f->is_vf) &&
1028 (!is_netdev || f->is_netdev))
1029 return f;
1030 }
1031 return NULL;
1032}
1033
1034/**
1035 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1036 * @vsi: the VSI to be searched
1037 *
1038 * Returns true if VSI is in vlan mode or false otherwise
1039 **/
1040bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1041{
1042 struct i40e_mac_filter *f;
1043
1044 /* Only -1 for all the filters denotes not in vlan mode
1045 * so we have to go through all the list in order to make sure
1046 */
1047 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1048 if (f->vlan >= 0)
1049 return true;
1050 }
1051
1052 return false;
1053}
1054
1055/**
1056 * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
1057 * @vsi: the VSI to be searched
1058 * @macaddr: the mac address to be filtered
1059 * @is_vf: true if it is a vf
1060 * @is_netdev: true if it is a netdev
1061 *
1062 * Goes through all the macvlan filters and adds a
1063 * macvlan filter for each unique vlan that already exists
1064 *
1065 * Returns first filter found on success, else NULL
1066 **/
1067struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi, u8 *macaddr,
1068 bool is_vf, bool is_netdev)
1069{
1070 struct i40e_mac_filter *f;
1071
1072 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1073 if (!i40e_find_filter(vsi, macaddr, f->vlan,
1074 is_vf, is_netdev)) {
1075 if (!i40e_add_filter(vsi, macaddr, f->vlan,
1076 is_vf, is_netdev))
1077 return NULL;
1078 }
1079 }
1080
1081 return list_first_entry_or_null(&vsi->mac_filter_list,
1082 struct i40e_mac_filter, list);
1083}
1084
1085/**
1086 * i40e_add_filter - Add a mac/vlan filter to the VSI
1087 * @vsi: the VSI to be searched
1088 * @macaddr: the MAC address
1089 * @vlan: the vlan
1090 * @is_vf: make sure its a vf filter, else doesn't matter
1091 * @is_netdev: make sure its a netdev filter, else doesn't matter
1092 *
1093 * Returns ptr to the filter object or NULL when no memory available.
1094 **/
1095struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1096 u8 *macaddr, s16 vlan,
1097 bool is_vf, bool is_netdev)
1098{
1099 struct i40e_mac_filter *f;
1100
1101 if (!vsi || !macaddr)
1102 return NULL;
1103
1104 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1105 if (!f) {
1106 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1107 if (!f)
1108 goto add_filter_out;
1109
1110 memcpy(f->macaddr, macaddr, ETH_ALEN);
1111 f->vlan = vlan;
1112 f->changed = true;
1113
1114 INIT_LIST_HEAD(&f->list);
1115 list_add(&f->list, &vsi->mac_filter_list);
1116 }
1117
1118 /* increment counter and add a new flag if needed */
1119 if (is_vf) {
1120 if (!f->is_vf) {
1121 f->is_vf = true;
1122 f->counter++;
1123 }
1124 } else if (is_netdev) {
1125 if (!f->is_netdev) {
1126 f->is_netdev = true;
1127 f->counter++;
1128 }
1129 } else {
1130 f->counter++;
1131 }
1132
1133 /* changed tells sync_filters_subtask to
1134 * push the filter down to the firmware
1135 */
1136 if (f->changed) {
1137 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1138 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1139 }
1140
1141add_filter_out:
1142 return f;
1143}
1144
1145/**
1146 * i40e_del_filter - Remove a mac/vlan filter from the VSI
1147 * @vsi: the VSI to be searched
1148 * @macaddr: the MAC address
1149 * @vlan: the vlan
1150 * @is_vf: make sure it's a vf filter, else doesn't matter
1151 * @is_netdev: make sure it's a netdev filter, else doesn't matter
1152 **/
1153void i40e_del_filter(struct i40e_vsi *vsi,
1154 u8 *macaddr, s16 vlan,
1155 bool is_vf, bool is_netdev)
1156{
1157 struct i40e_mac_filter *f;
1158
1159 if (!vsi || !macaddr)
1160 return;
1161
1162 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1163 if (!f || f->counter == 0)
1164 return;
1165
1166 if (is_vf) {
1167 if (f->is_vf) {
1168 f->is_vf = false;
1169 f->counter--;
1170 }
1171 } else if (is_netdev) {
1172 if (f->is_netdev) {
1173 f->is_netdev = false;
1174 f->counter--;
1175 }
1176 } else {
1177 /* make sure we don't remove a filter in use by vf or netdev */
1178 int min_f = 0;
1179 min_f += (f->is_vf ? 1 : 0);
1180 min_f += (f->is_netdev ? 1 : 0);
1181
1182 if (f->counter > min_f)
1183 f->counter--;
1184 }
1185
1186 /* counter == 0 tells sync_filters_subtask to
1187 * remove the filter from the firmware's list
1188 */
1189 if (f->counter == 0) {
1190 f->changed = true;
1191 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1192 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1193 }
1194}
1195
1196/**
1197 * i40e_set_mac - NDO callback to set mac address
1198 * @netdev: network interface device structure
1199 * @p: pointer to an address structure
1200 *
1201 * Returns 0 on success, negative on failure
1202 **/
1203static int i40e_set_mac(struct net_device *netdev, void *p)
1204{
1205 struct i40e_netdev_priv *np = netdev_priv(netdev);
1206 struct i40e_vsi *vsi = np->vsi;
1207 struct sockaddr *addr = p;
1208 struct i40e_mac_filter *f;
1209
1210 if (!is_valid_ether_addr(addr->sa_data))
1211 return -EADDRNOTAVAIL;
1212
1213 netdev_info(netdev, "set mac address=%pM\n", addr->sa_data);
1214
1215 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
1216 return 0;
1217
1218 if (vsi->type == I40E_VSI_MAIN) {
1219 i40e_status ret;
1220 ret = i40e_aq_mac_address_write(&vsi->back->hw,
1221 I40E_AQC_WRITE_TYPE_LAA_ONLY,
1222 addr->sa_data, NULL);
1223 if (ret) {
1224 netdev_info(netdev,
1225 "Addr change for Main VSI failed: %d\n",
1226 ret);
1227 return -EADDRNOTAVAIL;
1228 }
1229
1230 memcpy(vsi->back->hw.mac.addr, addr->sa_data, netdev->addr_len);
1231 }
1232
1233 /* In order to be sure to not drop any packets, add the new address
1234 * then delete the old one.
1235 */
1236 f = i40e_add_filter(vsi, addr->sa_data, I40E_VLAN_ANY, false, false);
1237 if (!f)
1238 return -ENOMEM;
1239
1240 i40e_sync_vsi_filters(vsi);
1241 i40e_del_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY, false, false);
1242 i40e_sync_vsi_filters(vsi);
1243
1244 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1245
1246 return 0;
1247}
1248
1249/**
1250 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1251 * @vsi: the VSI being setup
1252 * @ctxt: VSI context structure
1253 * @enabled_tc: Enabled TCs bitmap
1254 * @is_add: True if called before Add VSI
1255 *
1256 * Setup VSI queue mapping for enabled traffic classes.
1257 **/
1258static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1259 struct i40e_vsi_context *ctxt,
1260 u8 enabled_tc,
1261 bool is_add)
1262{
1263 struct i40e_pf *pf = vsi->back;
1264 u16 sections = 0;
1265 u8 netdev_tc = 0;
1266 u16 numtc = 0;
1267 u16 qcount;
1268 u8 offset;
1269 u16 qmap;
1270 int i;
1271
1272 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1273 offset = 0;
1274
1275 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1276 /* Find numtc from enabled TC bitmap */
1277 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1278 if (enabled_tc & (1 << i)) /* TC is enabled */
1279 numtc++;
1280 }
1281 if (!numtc) {
1282 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1283 numtc = 1;
1284 }
1285 } else {
1286 /* At least TC0 is enabled in case of non-DCB case */
1287 numtc = 1;
1288 }
1289
1290 vsi->tc_config.numtc = numtc;
1291 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1292
1293 /* Setup queue offset/count for all TCs for given VSI */
1294 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1295 /* See if the given TC is enabled for the given VSI */
1296 if (vsi->tc_config.enabled_tc & (1 << i)) { /* TC is enabled */
1297 int pow, num_qps;
1298
1299 vsi->tc_config.tc_info[i].qoffset = offset;
1300 switch (vsi->type) {
1301 case I40E_VSI_MAIN:
1302 if (i == 0)
1303 qcount = pf->rss_size;
1304 else
1305 qcount = pf->num_tc_qps;
1306 vsi->tc_config.tc_info[i].qcount = qcount;
1307 break;
1308 case I40E_VSI_FDIR:
1309 case I40E_VSI_SRIOV:
1310 case I40E_VSI_VMDQ2:
1311 default:
1312 qcount = vsi->alloc_queue_pairs;
1313 vsi->tc_config.tc_info[i].qcount = qcount;
1314 WARN_ON(i != 0);
1315 break;
1316 }
1317
1318 /* find the power-of-2 of the number of queue pairs */
1319 num_qps = vsi->tc_config.tc_info[i].qcount;
1320 pow = 0;
1321 while (num_qps &&
1322 ((1 << pow) < vsi->tc_config.tc_info[i].qcount)) {
1323 pow++;
1324 num_qps >>= 1;
1325 }
1326
1327 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1328 qmap =
1329 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1330 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1331
1332 offset += vsi->tc_config.tc_info[i].qcount;
1333 } else {
1334 /* TC is not enabled so set the offset to
1335 * default queue and allocate one queue
1336 * for the given TC.
1337 */
1338 vsi->tc_config.tc_info[i].qoffset = 0;
1339 vsi->tc_config.tc_info[i].qcount = 1;
1340 vsi->tc_config.tc_info[i].netdev_tc = 0;
1341
1342 qmap = 0;
1343 }
1344 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1345 }
1346
1347 /* Set actual Tx/Rx queue pairs */
1348 vsi->num_queue_pairs = offset;
1349
1350 /* Scheduler section valid can only be set for ADD VSI */
1351 if (is_add) {
1352 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1353
1354 ctxt->info.up_enable_bits = enabled_tc;
1355 }
1356 if (vsi->type == I40E_VSI_SRIOV) {
1357 ctxt->info.mapping_flags |=
1358 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1359 for (i = 0; i < vsi->num_queue_pairs; i++)
1360 ctxt->info.queue_mapping[i] =
1361 cpu_to_le16(vsi->base_queue + i);
1362 } else {
1363 ctxt->info.mapping_flags |=
1364 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1365 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1366 }
1367 ctxt->info.valid_sections |= cpu_to_le16(sections);
1368}
1369
1370/**
1371 * i40e_set_rx_mode - NDO callback to set the netdev filters
1372 * @netdev: network interface device structure
1373 **/
1374static void i40e_set_rx_mode(struct net_device *netdev)
1375{
1376 struct i40e_netdev_priv *np = netdev_priv(netdev);
1377 struct i40e_mac_filter *f, *ftmp;
1378 struct i40e_vsi *vsi = np->vsi;
1379 struct netdev_hw_addr *uca;
1380 struct netdev_hw_addr *mca;
1381 struct netdev_hw_addr *ha;
1382
1383 /* add addr if not already in the filter list */
1384 netdev_for_each_uc_addr(uca, netdev) {
1385 if (!i40e_find_mac(vsi, uca->addr, false, true)) {
1386 if (i40e_is_vsi_in_vlan(vsi))
1387 i40e_put_mac_in_vlan(vsi, uca->addr,
1388 false, true);
1389 else
1390 i40e_add_filter(vsi, uca->addr, I40E_VLAN_ANY,
1391 false, true);
1392 }
1393 }
1394
1395 netdev_for_each_mc_addr(mca, netdev) {
1396 if (!i40e_find_mac(vsi, mca->addr, false, true)) {
1397 if (i40e_is_vsi_in_vlan(vsi))
1398 i40e_put_mac_in_vlan(vsi, mca->addr,
1399 false, true);
1400 else
1401 i40e_add_filter(vsi, mca->addr, I40E_VLAN_ANY,
1402 false, true);
1403 }
1404 }
1405
1406 /* remove filter if not in netdev list */
1407 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1408 bool found = false;
1409
1410 if (!f->is_netdev)
1411 continue;
1412
1413 if (is_multicast_ether_addr(f->macaddr)) {
1414 netdev_for_each_mc_addr(mca, netdev) {
1415 if (ether_addr_equal(mca->addr, f->macaddr)) {
1416 found = true;
1417 break;
1418 }
1419 }
1420 } else {
1421 netdev_for_each_uc_addr(uca, netdev) {
1422 if (ether_addr_equal(uca->addr, f->macaddr)) {
1423 found = true;
1424 break;
1425 }
1426 }
1427
1428 for_each_dev_addr(netdev, ha) {
1429 if (ether_addr_equal(ha->addr, f->macaddr)) {
1430 found = true;
1431 break;
1432 }
1433 }
1434 }
1435 if (!found)
1436 i40e_del_filter(
1437 vsi, f->macaddr, I40E_VLAN_ANY, false, true);
1438 }
1439
1440 /* check for other flag changes */
1441 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1442 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1443 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1444 }
1445}
1446
1447/**
1448 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1449 * @vsi: ptr to the VSI
1450 *
1451 * Push any outstanding VSI filter changes through the AdminQ.
1452 *
1453 * Returns 0 or error value
1454 **/
1455int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
1456{
1457 struct i40e_mac_filter *f, *ftmp;
1458 bool promisc_forced_on = false;
1459 bool add_happened = false;
1460 int filter_list_len = 0;
1461 u32 changed_flags = 0;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001462 i40e_status aq_ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001463 struct i40e_pf *pf;
1464 int num_add = 0;
1465 int num_del = 0;
1466 u16 cmd_flags;
1467
1468 /* empty array typed pointers, kcalloc later */
1469 struct i40e_aqc_add_macvlan_element_data *add_list;
1470 struct i40e_aqc_remove_macvlan_element_data *del_list;
1471
1472 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1473 usleep_range(1000, 2000);
1474 pf = vsi->back;
1475
1476 if (vsi->netdev) {
1477 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1478 vsi->current_netdev_flags = vsi->netdev->flags;
1479 }
1480
1481 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1482 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1483
1484 filter_list_len = pf->hw.aq.asq_buf_size /
1485 sizeof(struct i40e_aqc_remove_macvlan_element_data);
1486 del_list = kcalloc(filter_list_len,
1487 sizeof(struct i40e_aqc_remove_macvlan_element_data),
1488 GFP_KERNEL);
1489 if (!del_list)
1490 return -ENOMEM;
1491
1492 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1493 if (!f->changed)
1494 continue;
1495
1496 if (f->counter != 0)
1497 continue;
1498 f->changed = false;
1499 cmd_flags = 0;
1500
1501 /* add to delete list */
1502 memcpy(del_list[num_del].mac_addr,
1503 f->macaddr, ETH_ALEN);
1504 del_list[num_del].vlan_tag =
1505 cpu_to_le16((u16)(f->vlan ==
1506 I40E_VLAN_ANY ? 0 : f->vlan));
1507
1508 /* vlan0 as wild card to allow packets from all vlans */
1509 if (f->vlan == I40E_VLAN_ANY ||
1510 (vsi->netdev && !(vsi->netdev->features &
1511 NETIF_F_HW_VLAN_CTAG_FILTER)))
1512 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1513 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1514 del_list[num_del].flags = cmd_flags;
1515 num_del++;
1516
1517 /* unlink from filter list */
1518 list_del(&f->list);
1519 kfree(f);
1520
1521 /* flush a full buffer */
1522 if (num_del == filter_list_len) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001523 aq_ret = i40e_aq_remove_macvlan(&pf->hw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001524 vsi->seid, del_list, num_del,
1525 NULL);
1526 num_del = 0;
1527 memset(del_list, 0, sizeof(*del_list));
1528
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001529 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001530 dev_info(&pf->pdev->dev,
1531 "ignoring delete macvlan error, err %d, aq_err %d while flushing a full buffer\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001532 aq_ret,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001533 pf->hw.aq.asq_last_status);
1534 }
1535 }
1536 if (num_del) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001537 aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001538 del_list, num_del, NULL);
1539 num_del = 0;
1540
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001541 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001542 dev_info(&pf->pdev->dev,
1543 "ignoring delete macvlan error, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001544 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001545 }
1546
1547 kfree(del_list);
1548 del_list = NULL;
1549
1550 /* do all the adds now */
1551 filter_list_len = pf->hw.aq.asq_buf_size /
1552 sizeof(struct i40e_aqc_add_macvlan_element_data),
1553 add_list = kcalloc(filter_list_len,
1554 sizeof(struct i40e_aqc_add_macvlan_element_data),
1555 GFP_KERNEL);
1556 if (!add_list)
1557 return -ENOMEM;
1558
1559 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1560 if (!f->changed)
1561 continue;
1562
1563 if (f->counter == 0)
1564 continue;
1565 f->changed = false;
1566 add_happened = true;
1567 cmd_flags = 0;
1568
1569 /* add to add array */
1570 memcpy(add_list[num_add].mac_addr,
1571 f->macaddr, ETH_ALEN);
1572 add_list[num_add].vlan_tag =
1573 cpu_to_le16(
1574 (u16)(f->vlan == I40E_VLAN_ANY ? 0 : f->vlan));
1575 add_list[num_add].queue_number = 0;
1576
1577 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
1578
1579 /* vlan0 as wild card to allow packets from all vlans */
1580 if (f->vlan == I40E_VLAN_ANY || (vsi->netdev &&
1581 !(vsi->netdev->features &
1582 NETIF_F_HW_VLAN_CTAG_FILTER)))
1583 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
1584 add_list[num_add].flags = cpu_to_le16(cmd_flags);
1585 num_add++;
1586
1587 /* flush a full buffer */
1588 if (num_add == filter_list_len) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001589 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1590 add_list, num_add,
1591 NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001592 num_add = 0;
1593
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001594 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001595 break;
1596 memset(add_list, 0, sizeof(*add_list));
1597 }
1598 }
1599 if (num_add) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001600 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1601 add_list, num_add, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001602 num_add = 0;
1603 }
1604 kfree(add_list);
1605 add_list = NULL;
1606
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001607 if (add_happened && (!aq_ret)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001608 /* do nothing */;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001609 } else if (add_happened && (aq_ret)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001610 dev_info(&pf->pdev->dev,
1611 "add filter failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001612 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001613 if ((pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOSPC) &&
1614 !test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1615 &vsi->state)) {
1616 promisc_forced_on = true;
1617 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1618 &vsi->state);
1619 dev_info(&pf->pdev->dev, "promiscuous mode forced on\n");
1620 }
1621 }
1622 }
1623
1624 /* check for changes in promiscuous modes */
1625 if (changed_flags & IFF_ALLMULTI) {
1626 bool cur_multipromisc;
1627 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001628 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
1629 vsi->seid,
1630 cur_multipromisc,
1631 NULL);
1632 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001633 dev_info(&pf->pdev->dev,
1634 "set multi promisc failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001635 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001636 }
1637 if ((changed_flags & IFF_PROMISC) || promisc_forced_on) {
1638 bool cur_promisc;
1639 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
1640 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1641 &vsi->state));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001642 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(&vsi->back->hw,
1643 vsi->seid,
1644 cur_promisc, NULL);
1645 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001646 dev_info(&pf->pdev->dev,
1647 "set uni promisc failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001648 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001649 }
1650
1651 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
1652 return 0;
1653}
1654
1655/**
1656 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
1657 * @pf: board private structure
1658 **/
1659static void i40e_sync_filters_subtask(struct i40e_pf *pf)
1660{
1661 int v;
1662
1663 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
1664 return;
1665 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
1666
1667 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
1668 if (pf->vsi[v] &&
1669 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED))
1670 i40e_sync_vsi_filters(pf->vsi[v]);
1671 }
1672}
1673
1674/**
1675 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
1676 * @netdev: network interface device structure
1677 * @new_mtu: new value for maximum frame size
1678 *
1679 * Returns 0 on success, negative on failure
1680 **/
1681static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
1682{
1683 struct i40e_netdev_priv *np = netdev_priv(netdev);
1684 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
1685 struct i40e_vsi *vsi = np->vsi;
1686
1687 /* MTU < 68 is an error and causes problems on some kernels */
1688 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
1689 return -EINVAL;
1690
1691 netdev_info(netdev, "changing MTU from %d to %d\n",
1692 netdev->mtu, new_mtu);
1693 netdev->mtu = new_mtu;
1694 if (netif_running(netdev))
1695 i40e_vsi_reinit_locked(vsi);
1696
1697 return 0;
1698}
1699
1700/**
1701 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
1702 * @vsi: the vsi being adjusted
1703 **/
1704void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
1705{
1706 struct i40e_vsi_context ctxt;
1707 i40e_status ret;
1708
1709 if ((vsi->info.valid_sections &
1710 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1711 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
1712 return; /* already enabled */
1713
1714 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1715 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1716 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
1717
1718 ctxt.seid = vsi->seid;
1719 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1720 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1721 if (ret) {
1722 dev_info(&vsi->back->pdev->dev,
1723 "%s: update vsi failed, aq_err=%d\n",
1724 __func__, vsi->back->hw.aq.asq_last_status);
1725 }
1726}
1727
1728/**
1729 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
1730 * @vsi: the vsi being adjusted
1731 **/
1732void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
1733{
1734 struct i40e_vsi_context ctxt;
1735 i40e_status ret;
1736
1737 if ((vsi->info.valid_sections &
1738 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1739 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
1740 I40E_AQ_VSI_PVLAN_EMOD_MASK))
1741 return; /* already disabled */
1742
1743 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1744 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1745 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
1746
1747 ctxt.seid = vsi->seid;
1748 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1749 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1750 if (ret) {
1751 dev_info(&vsi->back->pdev->dev,
1752 "%s: update vsi failed, aq_err=%d\n",
1753 __func__, vsi->back->hw.aq.asq_last_status);
1754 }
1755}
1756
1757/**
1758 * i40e_vlan_rx_register - Setup or shutdown vlan offload
1759 * @netdev: network interface to be adjusted
1760 * @features: netdev features to test if VLAN offload is enabled or not
1761 **/
1762static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
1763{
1764 struct i40e_netdev_priv *np = netdev_priv(netdev);
1765 struct i40e_vsi *vsi = np->vsi;
1766
1767 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1768 i40e_vlan_stripping_enable(vsi);
1769 else
1770 i40e_vlan_stripping_disable(vsi);
1771}
1772
1773/**
1774 * i40e_vsi_add_vlan - Add vsi membership for given vlan
1775 * @vsi: the vsi being configured
1776 * @vid: vlan id to be added (0 = untagged only , -1 = any)
1777 **/
1778int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
1779{
1780 struct i40e_mac_filter *f, *add_f;
1781 bool is_netdev, is_vf;
1782 int ret;
1783
1784 is_vf = (vsi->type == I40E_VSI_SRIOV);
1785 is_netdev = !!(vsi->netdev);
1786
1787 if (is_netdev) {
1788 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid,
1789 is_vf, is_netdev);
1790 if (!add_f) {
1791 dev_info(&vsi->back->pdev->dev,
1792 "Could not add vlan filter %d for %pM\n",
1793 vid, vsi->netdev->dev_addr);
1794 return -ENOMEM;
1795 }
1796 }
1797
1798 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1799 add_f = i40e_add_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
1800 if (!add_f) {
1801 dev_info(&vsi->back->pdev->dev,
1802 "Could not add vlan filter %d for %pM\n",
1803 vid, f->macaddr);
1804 return -ENOMEM;
1805 }
1806 }
1807
1808 ret = i40e_sync_vsi_filters(vsi);
1809 if (ret) {
1810 dev_info(&vsi->back->pdev->dev,
1811 "Could not sync filters for vid %d\n", vid);
1812 return ret;
1813 }
1814
1815 /* Now if we add a vlan tag, make sure to check if it is the first
1816 * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
1817 * with 0, so we now accept untagged and specified tagged traffic
1818 * (and not any taged and untagged)
1819 */
1820 if (vid > 0) {
1821 if (is_netdev && i40e_find_filter(vsi, vsi->netdev->dev_addr,
1822 I40E_VLAN_ANY,
1823 is_vf, is_netdev)) {
1824 i40e_del_filter(vsi, vsi->netdev->dev_addr,
1825 I40E_VLAN_ANY, is_vf, is_netdev);
1826 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0,
1827 is_vf, is_netdev);
1828 if (!add_f) {
1829 dev_info(&vsi->back->pdev->dev,
1830 "Could not add filter 0 for %pM\n",
1831 vsi->netdev->dev_addr);
1832 return -ENOMEM;
1833 }
1834 }
1835
1836 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1837 if (i40e_find_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1838 is_vf, is_netdev)) {
1839 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1840 is_vf, is_netdev);
1841 add_f = i40e_add_filter(vsi, f->macaddr,
1842 0, is_vf, is_netdev);
1843 if (!add_f) {
1844 dev_info(&vsi->back->pdev->dev,
1845 "Could not add filter 0 for %pM\n",
1846 f->macaddr);
1847 return -ENOMEM;
1848 }
1849 }
1850 }
1851 ret = i40e_sync_vsi_filters(vsi);
1852 }
1853
1854 return ret;
1855}
1856
1857/**
1858 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
1859 * @vsi: the vsi being configured
1860 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001861 *
1862 * Return: 0 on success or negative otherwise
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001863 **/
1864int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
1865{
1866 struct net_device *netdev = vsi->netdev;
1867 struct i40e_mac_filter *f, *add_f;
1868 bool is_vf, is_netdev;
1869 int filter_count = 0;
1870 int ret;
1871
1872 is_vf = (vsi->type == I40E_VSI_SRIOV);
1873 is_netdev = !!(netdev);
1874
1875 if (is_netdev)
1876 i40e_del_filter(vsi, netdev->dev_addr, vid, is_vf, is_netdev);
1877
1878 list_for_each_entry(f, &vsi->mac_filter_list, list)
1879 i40e_del_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
1880
1881 ret = i40e_sync_vsi_filters(vsi);
1882 if (ret) {
1883 dev_info(&vsi->back->pdev->dev, "Could not sync filters\n");
1884 return ret;
1885 }
1886
1887 /* go through all the filters for this VSI and if there is only
1888 * vid == 0 it means there are no other filters, so vid 0 must
1889 * be replaced with -1. This signifies that we should from now
1890 * on accept any traffic (with any tag present, or untagged)
1891 */
1892 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1893 if (is_netdev) {
1894 if (f->vlan &&
1895 ether_addr_equal(netdev->dev_addr, f->macaddr))
1896 filter_count++;
1897 }
1898
1899 if (f->vlan)
1900 filter_count++;
1901 }
1902
1903 if (!filter_count && is_netdev) {
1904 i40e_del_filter(vsi, netdev->dev_addr, 0, is_vf, is_netdev);
1905 f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
1906 is_vf, is_netdev);
1907 if (!f) {
1908 dev_info(&vsi->back->pdev->dev,
1909 "Could not add filter %d for %pM\n",
1910 I40E_VLAN_ANY, netdev->dev_addr);
1911 return -ENOMEM;
1912 }
1913 }
1914
1915 if (!filter_count) {
1916 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1917 i40e_del_filter(vsi, f->macaddr, 0, is_vf, is_netdev);
1918 add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1919 is_vf, is_netdev);
1920 if (!add_f) {
1921 dev_info(&vsi->back->pdev->dev,
1922 "Could not add filter %d for %pM\n",
1923 I40E_VLAN_ANY, f->macaddr);
1924 return -ENOMEM;
1925 }
1926 }
1927 }
1928
1929 return i40e_sync_vsi_filters(vsi);
1930}
1931
1932/**
1933 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
1934 * @netdev: network interface to be adjusted
1935 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001936 *
1937 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001938 **/
1939static int i40e_vlan_rx_add_vid(struct net_device *netdev,
1940 __always_unused __be16 proto, u16 vid)
1941{
1942 struct i40e_netdev_priv *np = netdev_priv(netdev);
1943 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001944 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001945
1946 if (vid > 4095)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001947 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001948
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001949 netdev_info(netdev, "adding %pM vid=%d\n", netdev->dev_addr, vid);
1950
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001951 /* If the network stack called us with vid = 0, we should
1952 * indicate to i40e_vsi_add_vlan() that we want to receive
1953 * any traffic (i.e. with any vlan tag, or untagged)
1954 */
1955 ret = i40e_vsi_add_vlan(vsi, vid ? vid : I40E_VLAN_ANY);
1956
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001957 if (!ret && (vid < VLAN_N_VID))
1958 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001959
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001960 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001961}
1962
1963/**
1964 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
1965 * @netdev: network interface to be adjusted
1966 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001967 *
1968 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001969 **/
1970static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
1971 __always_unused __be16 proto, u16 vid)
1972{
1973 struct i40e_netdev_priv *np = netdev_priv(netdev);
1974 struct i40e_vsi *vsi = np->vsi;
1975
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001976 netdev_info(netdev, "removing %pM vid=%d\n", netdev->dev_addr, vid);
1977
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001978 /* return code is ignored as there is nothing a user
1979 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001980 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001981 */
1982 i40e_vsi_kill_vlan(vsi, vid);
1983
1984 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001985
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001986 return 0;
1987}
1988
1989/**
1990 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
1991 * @vsi: the vsi being brought back up
1992 **/
1993static void i40e_restore_vlan(struct i40e_vsi *vsi)
1994{
1995 u16 vid;
1996
1997 if (!vsi->netdev)
1998 return;
1999
2000 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2001
2002 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2003 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2004 vid);
2005}
2006
2007/**
2008 * i40e_vsi_add_pvid - Add pvid for the VSI
2009 * @vsi: the vsi being adjusted
2010 * @vid: the vlan id to set as a PVID
2011 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002012int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002013{
2014 struct i40e_vsi_context ctxt;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002015 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002016
2017 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2018 vsi->info.pvid = cpu_to_le16(vid);
2019 vsi->info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_INSERT_PVID;
2020 vsi->info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_UNTAGGED;
2021
2022 ctxt.seid = vsi->seid;
2023 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002024 aq_ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2025 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002026 dev_info(&vsi->back->pdev->dev,
2027 "%s: update vsi failed, aq_err=%d\n",
2028 __func__, vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002029 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002030 }
2031
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002032 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002033}
2034
2035/**
2036 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2037 * @vsi: the vsi being adjusted
2038 *
2039 * Just use the vlan_rx_register() service to put it back to normal
2040 **/
2041void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2042{
2043 vsi->info.pvid = 0;
2044 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2045}
2046
2047/**
2048 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2049 * @vsi: ptr to the VSI
2050 *
2051 * If this function returns with an error, then it's possible one or
2052 * more of the rings is populated (while the rest are not). It is the
2053 * callers duty to clean those orphaned rings.
2054 *
2055 * Return 0 on success, negative on failure
2056 **/
2057static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2058{
2059 int i, err = 0;
2060
2061 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002062 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002063
2064 return err;
2065}
2066
2067/**
2068 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2069 * @vsi: ptr to the VSI
2070 *
2071 * Free VSI's transmit software resources
2072 **/
2073static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2074{
2075 int i;
2076
2077 for (i = 0; i < vsi->num_queue_pairs; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002078 if (vsi->tx_rings[i]->desc)
2079 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002080}
2081
2082/**
2083 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2084 * @vsi: ptr to the VSI
2085 *
2086 * If this function returns with an error, then it's possible one or
2087 * more of the rings is populated (while the rest are not). It is the
2088 * callers duty to clean those orphaned rings.
2089 *
2090 * Return 0 on success, negative on failure
2091 **/
2092static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2093{
2094 int i, err = 0;
2095
2096 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002097 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002098 return err;
2099}
2100
2101/**
2102 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2103 * @vsi: ptr to the VSI
2104 *
2105 * Free all receive software resources
2106 **/
2107static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2108{
2109 int i;
2110
2111 for (i = 0; i < vsi->num_queue_pairs; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002112 if (vsi->rx_rings[i]->desc)
2113 i40e_free_rx_resources(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002114}
2115
2116/**
2117 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2118 * @ring: The Tx ring to configure
2119 *
2120 * Configure the Tx descriptor ring in the HMC context.
2121 **/
2122static int i40e_configure_tx_ring(struct i40e_ring *ring)
2123{
2124 struct i40e_vsi *vsi = ring->vsi;
2125 u16 pf_q = vsi->base_queue + ring->queue_index;
2126 struct i40e_hw *hw = &vsi->back->hw;
2127 struct i40e_hmc_obj_txq tx_ctx;
2128 i40e_status err = 0;
2129 u32 qtx_ctl = 0;
2130
2131 /* some ATR related tx ring init */
2132 if (vsi->back->flags & I40E_FLAG_FDIR_ATR_ENABLED) {
2133 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2134 ring->atr_count = 0;
2135 } else {
2136 ring->atr_sample_rate = 0;
2137 }
2138
2139 /* initialize XPS */
2140 if (ring->q_vector && ring->netdev &&
2141 !test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
2142 netif_set_xps_queue(ring->netdev,
2143 &ring->q_vector->affinity_mask,
2144 ring->queue_index);
2145
2146 /* clear the context structure first */
2147 memset(&tx_ctx, 0, sizeof(tx_ctx));
2148
2149 tx_ctx.new_context = 1;
2150 tx_ctx.base = (ring->dma / 128);
2151 tx_ctx.qlen = ring->count;
2152 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FDIR_ENABLED |
2153 I40E_FLAG_FDIR_ATR_ENABLED));
2154
2155 /* As part of VSI creation/update, FW allocates certain
2156 * Tx arbitration queue sets for each TC enabled for
2157 * the VSI. The FW returns the handles to these queue
2158 * sets as part of the response buffer to Add VSI,
2159 * Update VSI, etc. AQ commands. It is expected that
2160 * these queue set handles be associated with the Tx
2161 * queues by the driver as part of the TX queue context
2162 * initialization. This has to be done regardless of
2163 * DCB as by default everything is mapped to TC0.
2164 */
2165 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2166 tx_ctx.rdylist_act = 0;
2167
2168 /* clear the context in the HMC */
2169 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2170 if (err) {
2171 dev_info(&vsi->back->pdev->dev,
2172 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2173 ring->queue_index, pf_q, err);
2174 return -ENOMEM;
2175 }
2176
2177 /* set the context in the HMC */
2178 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2179 if (err) {
2180 dev_info(&vsi->back->pdev->dev,
2181 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2182 ring->queue_index, pf_q, err);
2183 return -ENOMEM;
2184 }
2185
2186 /* Now associate this queue with this PCI function */
2187 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Shannon Nelson13fd9772013-09-28 07:14:19 +00002188 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2189 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002190 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2191 i40e_flush(hw);
2192
2193 clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
2194
2195 /* cache tail off for easier writes later */
2196 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2197
2198 return 0;
2199}
2200
2201/**
2202 * i40e_configure_rx_ring - Configure a receive ring context
2203 * @ring: The Rx ring to configure
2204 *
2205 * Configure the Rx descriptor ring in the HMC context.
2206 **/
2207static int i40e_configure_rx_ring(struct i40e_ring *ring)
2208{
2209 struct i40e_vsi *vsi = ring->vsi;
2210 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2211 u16 pf_q = vsi->base_queue + ring->queue_index;
2212 struct i40e_hw *hw = &vsi->back->hw;
2213 struct i40e_hmc_obj_rxq rx_ctx;
2214 i40e_status err = 0;
2215
2216 ring->state = 0;
2217
2218 /* clear the context structure first */
2219 memset(&rx_ctx, 0, sizeof(rx_ctx));
2220
2221 ring->rx_buf_len = vsi->rx_buf_len;
2222 ring->rx_hdr_len = vsi->rx_hdr_len;
2223
2224 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
2225 rx_ctx.hbuff = ring->rx_hdr_len >> I40E_RXQ_CTX_HBUFF_SHIFT;
2226
2227 rx_ctx.base = (ring->dma / 128);
2228 rx_ctx.qlen = ring->count;
2229
2230 if (vsi->back->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED) {
2231 set_ring_16byte_desc_enabled(ring);
2232 rx_ctx.dsize = 0;
2233 } else {
2234 rx_ctx.dsize = 1;
2235 }
2236
2237 rx_ctx.dtype = vsi->dtype;
2238 if (vsi->dtype) {
2239 set_ring_ps_enabled(ring);
2240 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
2241 I40E_RX_SPLIT_IP |
2242 I40E_RX_SPLIT_TCP_UDP |
2243 I40E_RX_SPLIT_SCTP;
2244 } else {
2245 rx_ctx.hsplit_0 = 0;
2246 }
2247
2248 rx_ctx.rxmax = min_t(u16, vsi->max_frame,
2249 (chain_len * ring->rx_buf_len));
2250 rx_ctx.tphrdesc_ena = 1;
2251 rx_ctx.tphwdesc_ena = 1;
2252 rx_ctx.tphdata_ena = 1;
2253 rx_ctx.tphhead_ena = 1;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00002254 if (hw->revision_id == 0)
2255 rx_ctx.lrxqthresh = 0;
2256 else
2257 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002258 rx_ctx.crcstrip = 1;
2259 rx_ctx.l2tsel = 1;
2260 rx_ctx.showiv = 1;
2261
2262 /* clear the context in the HMC */
2263 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2264 if (err) {
2265 dev_info(&vsi->back->pdev->dev,
2266 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2267 ring->queue_index, pf_q, err);
2268 return -ENOMEM;
2269 }
2270
2271 /* set the context in the HMC */
2272 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2273 if (err) {
2274 dev_info(&vsi->back->pdev->dev,
2275 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2276 ring->queue_index, pf_q, err);
2277 return -ENOMEM;
2278 }
2279
2280 /* cache tail for quicker writes, and clear the reg before use */
2281 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2282 writel(0, ring->tail);
2283
2284 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
2285
2286 return 0;
2287}
2288
2289/**
2290 * i40e_vsi_configure_tx - Configure the VSI for Tx
2291 * @vsi: VSI structure describing this set of rings and resources
2292 *
2293 * Configure the Tx VSI for operation.
2294 **/
2295static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2296{
2297 int err = 0;
2298 u16 i;
2299
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002300 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2301 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002302
2303 return err;
2304}
2305
2306/**
2307 * i40e_vsi_configure_rx - Configure the VSI for Rx
2308 * @vsi: the VSI being configured
2309 *
2310 * Configure the Rx VSI for operation.
2311 **/
2312static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
2313{
2314 int err = 0;
2315 u16 i;
2316
2317 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
2318 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
2319 + ETH_FCS_LEN + VLAN_HLEN;
2320 else
2321 vsi->max_frame = I40E_RXBUFFER_2048;
2322
2323 /* figure out correct receive buffer length */
2324 switch (vsi->back->flags & (I40E_FLAG_RX_1BUF_ENABLED |
2325 I40E_FLAG_RX_PS_ENABLED)) {
2326 case I40E_FLAG_RX_1BUF_ENABLED:
2327 vsi->rx_hdr_len = 0;
2328 vsi->rx_buf_len = vsi->max_frame;
2329 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2330 break;
2331 case I40E_FLAG_RX_PS_ENABLED:
2332 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2333 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2334 vsi->dtype = I40E_RX_DTYPE_HEADER_SPLIT;
2335 break;
2336 default:
2337 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2338 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2339 vsi->dtype = I40E_RX_DTYPE_SPLIT_ALWAYS;
2340 break;
2341 }
2342
2343 /* round up for the chip's needs */
2344 vsi->rx_hdr_len = ALIGN(vsi->rx_hdr_len,
2345 (1 << I40E_RXQ_CTX_HBUFF_SHIFT));
2346 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
2347 (1 << I40E_RXQ_CTX_DBUFF_SHIFT));
2348
2349 /* set up individual rings */
2350 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002351 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002352
2353 return err;
2354}
2355
2356/**
2357 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
2358 * @vsi: ptr to the VSI
2359 **/
2360static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
2361{
2362 u16 qoffset, qcount;
2363 int i, n;
2364
2365 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED))
2366 return;
2367
2368 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
2369 if (!(vsi->tc_config.enabled_tc & (1 << n)))
2370 continue;
2371
2372 qoffset = vsi->tc_config.tc_info[n].qoffset;
2373 qcount = vsi->tc_config.tc_info[n].qcount;
2374 for (i = qoffset; i < (qoffset + qcount); i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002375 struct i40e_ring *rx_ring = vsi->rx_rings[i];
2376 struct i40e_ring *tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002377 rx_ring->dcb_tc = n;
2378 tx_ring->dcb_tc = n;
2379 }
2380 }
2381}
2382
2383/**
2384 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
2385 * @vsi: ptr to the VSI
2386 **/
2387static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
2388{
2389 if (vsi->netdev)
2390 i40e_set_rx_mode(vsi->netdev);
2391}
2392
2393/**
2394 * i40e_vsi_configure - Set up the VSI for action
2395 * @vsi: the VSI being configured
2396 **/
2397static int i40e_vsi_configure(struct i40e_vsi *vsi)
2398{
2399 int err;
2400
2401 i40e_set_vsi_rx_mode(vsi);
2402 i40e_restore_vlan(vsi);
2403 i40e_vsi_config_dcb_rings(vsi);
2404 err = i40e_vsi_configure_tx(vsi);
2405 if (!err)
2406 err = i40e_vsi_configure_rx(vsi);
2407
2408 return err;
2409}
2410
2411/**
2412 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
2413 * @vsi: the VSI being configured
2414 **/
2415static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
2416{
2417 struct i40e_pf *pf = vsi->back;
2418 struct i40e_q_vector *q_vector;
2419 struct i40e_hw *hw = &pf->hw;
2420 u16 vector;
2421 int i, q;
2422 u32 val;
2423 u32 qp;
2424
2425 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
2426 * and PFINT_LNKLSTn registers, e.g.:
2427 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
2428 */
2429 qp = vsi->base_queue;
2430 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00002431 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
2432 q_vector = vsi->q_vectors[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002433 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2434 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2435 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
2436 q_vector->rx.itr);
2437 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2438 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2439 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
2440 q_vector->tx.itr);
2441
2442 /* Linked list for the queuepairs assigned to this vector */
2443 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
2444 for (q = 0; q < q_vector->num_ringpairs; q++) {
2445 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2446 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2447 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
2448 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
2449 (I40E_QUEUE_TYPE_TX
2450 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
2451
2452 wr32(hw, I40E_QINT_RQCTL(qp), val);
2453
2454 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2455 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2456 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
2457 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
2458 (I40E_QUEUE_TYPE_RX
2459 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2460
2461 /* Terminate the linked list */
2462 if (q == (q_vector->num_ringpairs - 1))
2463 val |= (I40E_QUEUE_END_OF_LIST
2464 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2465
2466 wr32(hw, I40E_QINT_TQCTL(qp), val);
2467 qp++;
2468 }
2469 }
2470
2471 i40e_flush(hw);
2472}
2473
2474/**
2475 * i40e_enable_misc_int_causes - enable the non-queue interrupts
2476 * @hw: ptr to the hardware info
2477 **/
2478static void i40e_enable_misc_int_causes(struct i40e_hw *hw)
2479{
2480 u32 val;
2481
2482 /* clear things first */
2483 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
2484 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
2485
2486 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
2487 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
2488 I40E_PFINT_ICR0_ENA_GRST_MASK |
2489 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
2490 I40E_PFINT_ICR0_ENA_GPIO_MASK |
2491 I40E_PFINT_ICR0_ENA_STORM_DETECT_MASK |
2492 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
2493 I40E_PFINT_ICR0_ENA_VFLR_MASK |
2494 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2495
2496 wr32(hw, I40E_PFINT_ICR0_ENA, val);
2497
2498 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00002499 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
2500 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002501
2502 /* OTHER_ITR_IDX = 0 */
2503 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
2504}
2505
2506/**
2507 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
2508 * @vsi: the VSI being configured
2509 **/
2510static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
2511{
Alexander Duyck493fb302013-09-28 07:01:44 +00002512 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002513 struct i40e_pf *pf = vsi->back;
2514 struct i40e_hw *hw = &pf->hw;
2515 u32 val;
2516
2517 /* set the ITR configuration */
2518 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2519 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2520 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
2521 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2522 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2523 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
2524
2525 i40e_enable_misc_int_causes(hw);
2526
2527 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
2528 wr32(hw, I40E_PFINT_LNKLST0, 0);
2529
2530 /* Associate the queue pair to the vector and enable the q int */
2531 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2532 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2533 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2534
2535 wr32(hw, I40E_QINT_RQCTL(0), val);
2536
2537 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2538 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2539 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2540
2541 wr32(hw, I40E_QINT_TQCTL(0), val);
2542 i40e_flush(hw);
2543}
2544
2545/**
2546 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
2547 * @pf: board private structure
2548 **/
Shannon Nelson116a57d2013-09-28 07:13:59 +00002549void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002550{
2551 struct i40e_hw *hw = &pf->hw;
2552 u32 val;
2553
2554 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
2555 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
2556 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
2557
2558 wr32(hw, I40E_PFINT_DYN_CTL0, val);
2559 i40e_flush(hw);
2560}
2561
2562/**
2563 * i40e_irq_dynamic_enable - Enable default interrupt generation settings
2564 * @vsi: pointer to a vsi
2565 * @vector: enable a particular Hw Interrupt vector
2566 **/
2567void i40e_irq_dynamic_enable(struct i40e_vsi *vsi, int vector)
2568{
2569 struct i40e_pf *pf = vsi->back;
2570 struct i40e_hw *hw = &pf->hw;
2571 u32 val;
2572
2573 val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
2574 I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
2575 (I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2576 wr32(hw, I40E_PFINT_DYN_CTLN(vector - 1), val);
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00002577 /* skip the flush */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002578}
2579
2580/**
2581 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
2582 * @irq: interrupt number
2583 * @data: pointer to a q_vector
2584 **/
2585static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
2586{
2587 struct i40e_q_vector *q_vector = data;
2588
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002589 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002590 return IRQ_HANDLED;
2591
2592 napi_schedule(&q_vector->napi);
2593
2594 return IRQ_HANDLED;
2595}
2596
2597/**
2598 * i40e_fdir_clean_rings - Interrupt Handler for FDIR rings
2599 * @irq: interrupt number
2600 * @data: pointer to a q_vector
2601 **/
2602static irqreturn_t i40e_fdir_clean_rings(int irq, void *data)
2603{
2604 struct i40e_q_vector *q_vector = data;
2605
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002606 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002607 return IRQ_HANDLED;
2608
2609 pr_info("fdir ring cleaning needed\n");
2610
2611 return IRQ_HANDLED;
2612}
2613
2614/**
2615 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
2616 * @vsi: the VSI being configured
2617 * @basename: name for the vector
2618 *
2619 * Allocates MSI-X vectors and requests interrupts from the kernel.
2620 **/
2621static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
2622{
2623 int q_vectors = vsi->num_q_vectors;
2624 struct i40e_pf *pf = vsi->back;
2625 int base = vsi->base_vector;
2626 int rx_int_idx = 0;
2627 int tx_int_idx = 0;
2628 int vector, err;
2629
2630 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00002631 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002632
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002633 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002634 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2635 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
2636 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002637 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002638 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2639 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002640 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002641 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2642 "%s-%s-%d", basename, "tx", tx_int_idx++);
2643 } else {
2644 /* skip this unused q_vector */
2645 continue;
2646 }
2647 err = request_irq(pf->msix_entries[base + vector].vector,
2648 vsi->irq_handler,
2649 0,
2650 q_vector->name,
2651 q_vector);
2652 if (err) {
2653 dev_info(&pf->pdev->dev,
2654 "%s: request_irq failed, error: %d\n",
2655 __func__, err);
2656 goto free_queue_irqs;
2657 }
2658 /* assign the mask for this irq */
2659 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2660 &q_vector->affinity_mask);
2661 }
2662
2663 return 0;
2664
2665free_queue_irqs:
2666 while (vector) {
2667 vector--;
2668 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2669 NULL);
2670 free_irq(pf->msix_entries[base + vector].vector,
2671 &(vsi->q_vectors[vector]));
2672 }
2673 return err;
2674}
2675
2676/**
2677 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
2678 * @vsi: the VSI being un-configured
2679 **/
2680static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
2681{
2682 struct i40e_pf *pf = vsi->back;
2683 struct i40e_hw *hw = &pf->hw;
2684 int base = vsi->base_vector;
2685 int i;
2686
2687 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002688 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
2689 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002690 }
2691
2692 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2693 for (i = vsi->base_vector;
2694 i < (vsi->num_q_vectors + vsi->base_vector); i++)
2695 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
2696
2697 i40e_flush(hw);
2698 for (i = 0; i < vsi->num_q_vectors; i++)
2699 synchronize_irq(pf->msix_entries[i + base].vector);
2700 } else {
2701 /* Legacy and MSI mode - this stops all interrupt handling */
2702 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
2703 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
2704 i40e_flush(hw);
2705 synchronize_irq(pf->pdev->irq);
2706 }
2707}
2708
2709/**
2710 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
2711 * @vsi: the VSI being configured
2712 **/
2713static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
2714{
2715 struct i40e_pf *pf = vsi->back;
2716 int i;
2717
2718 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2719 for (i = vsi->base_vector;
2720 i < (vsi->num_q_vectors + vsi->base_vector); i++)
2721 i40e_irq_dynamic_enable(vsi, i);
2722 } else {
2723 i40e_irq_dynamic_enable_icr0(pf);
2724 }
2725
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00002726 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002727 return 0;
2728}
2729
2730/**
2731 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
2732 * @pf: board private structure
2733 **/
2734static void i40e_stop_misc_vector(struct i40e_pf *pf)
2735{
2736 /* Disable ICR 0 */
2737 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
2738 i40e_flush(&pf->hw);
2739}
2740
2741/**
2742 * i40e_intr - MSI/Legacy and non-queue interrupt handler
2743 * @irq: interrupt number
2744 * @data: pointer to a q_vector
2745 *
2746 * This is the handler used for all MSI/Legacy interrupts, and deals
2747 * with both queue and non-queue interrupts. This is also used in
2748 * MSIX mode to handle the non-queue interrupts.
2749 **/
2750static irqreturn_t i40e_intr(int irq, void *data)
2751{
2752 struct i40e_pf *pf = (struct i40e_pf *)data;
2753 struct i40e_hw *hw = &pf->hw;
2754 u32 icr0, icr0_remaining;
2755 u32 val, ena_mask;
2756
2757 icr0 = rd32(hw, I40E_PFINT_ICR0);
2758
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002759 val = rd32(hw, I40E_PFINT_DYN_CTL0);
2760 val = val | I40E_PFINT_DYN_CTL0_CLEARPBA_MASK;
2761 wr32(hw, I40E_PFINT_DYN_CTL0, val);
2762
Shannon Nelson116a57d2013-09-28 07:13:59 +00002763 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
2764 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
2765 return IRQ_NONE;
2766
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002767 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
2768
Shannon Nelsoncd92e722013-11-16 10:00:44 +00002769 /* if interrupt but no bits showing, must be SWINT */
2770 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
2771 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
2772 pf->sw_int_count++;
2773
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002774 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
2775 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
2776
2777 /* temporarily disable queue cause for NAPI processing */
2778 u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
2779 qval &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
2780 wr32(hw, I40E_QINT_RQCTL(0), qval);
2781
2782 qval = rd32(hw, I40E_QINT_TQCTL(0));
2783 qval &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
2784 wr32(hw, I40E_QINT_TQCTL(0), qval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002785
2786 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck493fb302013-09-28 07:01:44 +00002787 napi_schedule(&pf->vsi[pf->lan_vsi]->q_vectors[0]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002788 }
2789
2790 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
2791 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2792 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
2793 }
2794
2795 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
2796 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
2797 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
2798 }
2799
2800 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
2801 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
2802 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
2803 }
2804
2805 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
2806 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
2807 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
2808 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
2809 val = rd32(hw, I40E_GLGEN_RSTAT);
2810 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
2811 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelsond52cf0a2013-11-16 10:00:39 +00002812 if (val == I40E_RESET_CORER)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002813 pf->corer_count++;
Shannon Nelsond52cf0a2013-11-16 10:00:39 +00002814 else if (val == I40E_RESET_GLOBR)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002815 pf->globr_count++;
Shannon Nelsond52cf0a2013-11-16 10:00:39 +00002816 else if (val == I40E_RESET_EMPR)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002817 pf->empr_count++;
2818 }
2819
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002820 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
2821 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
2822 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
2823 }
2824
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002825 /* If a critical error is pending we have no choice but to reset the
2826 * device.
2827 * Report and mask out any remaining unexpected interrupts.
2828 */
2829 icr0_remaining = icr0 & ena_mask;
2830 if (icr0_remaining) {
2831 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
2832 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002833 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002834 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
2835 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK) ||
2836 (icr0_remaining & I40E_PFINT_ICR0_MAL_DETECT_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002837 dev_info(&pf->pdev->dev, "device will be reset\n");
2838 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
2839 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002840 }
2841 ena_mask &= ~icr0_remaining;
2842 }
2843
2844 /* re-enable interrupt causes */
2845 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002846 if (!test_bit(__I40E_DOWN, &pf->state)) {
2847 i40e_service_event_schedule(pf);
2848 i40e_irq_dynamic_enable_icr0(pf);
2849 }
2850
2851 return IRQ_HANDLED;
2852}
2853
2854/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002855 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002856 * @vsi: the VSI being configured
2857 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002858 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002859 **/
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002860static void map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002861{
Alexander Duyck493fb302013-09-28 07:01:44 +00002862 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002863 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
2864 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002865
2866 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002867 tx_ring->next = q_vector->tx.ring;
2868 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002869 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002870
2871 rx_ring->q_vector = q_vector;
2872 rx_ring->next = q_vector->rx.ring;
2873 q_vector->rx.ring = rx_ring;
2874 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002875}
2876
2877/**
2878 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
2879 * @vsi: the VSI being configured
2880 *
2881 * This function maps descriptor rings to the queue-specific vectors
2882 * we were allotted through the MSI-X enabling code. Ideally, we'd have
2883 * one vector per queue pair, but on a constrained vector budget, we
2884 * group the queue pairs as "efficiently" as possible.
2885 **/
2886static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
2887{
2888 int qp_remaining = vsi->num_queue_pairs;
2889 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002890 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002891 int v_start = 0;
2892 int qp_idx = 0;
2893
2894 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
2895 * group them so there are multiple queues per vector.
2896 */
2897 for (; v_start < q_vectors && qp_remaining; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002898 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
2899
2900 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
2901
2902 q_vector->num_ringpairs = num_ringpairs;
2903
2904 q_vector->rx.count = 0;
2905 q_vector->tx.count = 0;
2906 q_vector->rx.ring = NULL;
2907 q_vector->tx.ring = NULL;
2908
2909 while (num_ringpairs--) {
2910 map_vector_to_qp(vsi, v_start, qp_idx);
2911 qp_idx++;
2912 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002913 }
2914 }
2915}
2916
2917/**
2918 * i40e_vsi_request_irq - Request IRQ from the OS
2919 * @vsi: the VSI being configured
2920 * @basename: name for the vector
2921 **/
2922static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
2923{
2924 struct i40e_pf *pf = vsi->back;
2925 int err;
2926
2927 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
2928 err = i40e_vsi_request_irq_msix(vsi, basename);
2929 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
2930 err = request_irq(pf->pdev->irq, i40e_intr, 0,
2931 pf->misc_int_name, pf);
2932 else
2933 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
2934 pf->misc_int_name, pf);
2935
2936 if (err)
2937 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
2938
2939 return err;
2940}
2941
2942#ifdef CONFIG_NET_POLL_CONTROLLER
2943/**
2944 * i40e_netpoll - A Polling 'interrupt'handler
2945 * @netdev: network interface device structure
2946 *
2947 * This is used by netconsole to send skbs without having to re-enable
2948 * interrupts. It's not called while the normal interrupt routine is executing.
2949 **/
2950static void i40e_netpoll(struct net_device *netdev)
2951{
2952 struct i40e_netdev_priv *np = netdev_priv(netdev);
2953 struct i40e_vsi *vsi = np->vsi;
2954 struct i40e_pf *pf = vsi->back;
2955 int i;
2956
2957 /* if interface is down do nothing */
2958 if (test_bit(__I40E_DOWN, &vsi->state))
2959 return;
2960
2961 pf->flags |= I40E_FLAG_IN_NETPOLL;
2962 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2963 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00002964 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002965 } else {
2966 i40e_intr(pf->pdev->irq, netdev);
2967 }
2968 pf->flags &= ~I40E_FLAG_IN_NETPOLL;
2969}
2970#endif
2971
2972/**
2973 * i40e_vsi_control_tx - Start or stop a VSI's rings
2974 * @vsi: the VSI being configured
2975 * @enable: start or stop the rings
2976 **/
2977static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
2978{
2979 struct i40e_pf *pf = vsi->back;
2980 struct i40e_hw *hw = &pf->hw;
2981 int i, j, pf_q;
2982 u32 tx_reg;
2983
2984 pf_q = vsi->base_queue;
2985 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
2986 j = 1000;
2987 do {
2988 usleep_range(1000, 2000);
2989 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
2990 } while (j-- && ((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT)
2991 ^ (tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT)) & 1);
2992
2993 if (enable) {
2994 /* is STAT set ? */
2995 if ((tx_reg & I40E_QTX_ENA_QENA_STAT_MASK)) {
2996 dev_info(&pf->pdev->dev,
2997 "Tx %d already enabled\n", i);
2998 continue;
2999 }
3000 } else {
3001 /* is !STAT set ? */
3002 if (!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK)) {
3003 dev_info(&pf->pdev->dev,
3004 "Tx %d already disabled\n", i);
3005 continue;
3006 }
3007 }
3008
3009 /* turn on/off the queue */
3010 if (enable)
3011 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK |
3012 I40E_QTX_ENA_QENA_STAT_MASK;
3013 else
3014 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
3015
3016 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
3017
3018 /* wait for the change to finish */
3019 for (j = 0; j < 10; j++) {
3020 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
3021 if (enable) {
3022 if ((tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3023 break;
3024 } else {
3025 if (!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3026 break;
3027 }
3028
3029 udelay(10);
3030 }
3031 if (j >= 10) {
3032 dev_info(&pf->pdev->dev, "Tx ring %d %sable timeout\n",
3033 pf_q, (enable ? "en" : "dis"));
3034 return -ETIMEDOUT;
3035 }
3036 }
3037
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003038 if (hw->revision_id == 0)
3039 mdelay(50);
3040
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003041 return 0;
3042}
3043
3044/**
3045 * i40e_vsi_control_rx - Start or stop a VSI's rings
3046 * @vsi: the VSI being configured
3047 * @enable: start or stop the rings
3048 **/
3049static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3050{
3051 struct i40e_pf *pf = vsi->back;
3052 struct i40e_hw *hw = &pf->hw;
3053 int i, j, pf_q;
3054 u32 rx_reg;
3055
3056 pf_q = vsi->base_queue;
3057 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
3058 j = 1000;
3059 do {
3060 usleep_range(1000, 2000);
3061 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
3062 } while (j-- && ((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT)
3063 ^ (rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT)) & 1);
3064
3065 if (enable) {
3066 /* is STAT set ? */
3067 if ((rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3068 continue;
3069 } else {
3070 /* is !STAT set ? */
3071 if (!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3072 continue;
3073 }
3074
3075 /* turn on/off the queue */
3076 if (enable)
3077 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK |
3078 I40E_QRX_ENA_QENA_STAT_MASK;
3079 else
3080 rx_reg &= ~(I40E_QRX_ENA_QENA_REQ_MASK |
3081 I40E_QRX_ENA_QENA_STAT_MASK);
3082 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
3083
3084 /* wait for the change to finish */
3085 for (j = 0; j < 10; j++) {
3086 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
3087
3088 if (enable) {
3089 if ((rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3090 break;
3091 } else {
3092 if (!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3093 break;
3094 }
3095
3096 udelay(10);
3097 }
3098 if (j >= 10) {
3099 dev_info(&pf->pdev->dev, "Rx ring %d %sable timeout\n",
3100 pf_q, (enable ? "en" : "dis"));
3101 return -ETIMEDOUT;
3102 }
3103 }
3104
3105 return 0;
3106}
3107
3108/**
3109 * i40e_vsi_control_rings - Start or stop a VSI's rings
3110 * @vsi: the VSI being configured
3111 * @enable: start or stop the rings
3112 **/
3113static int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
3114{
3115 int ret;
3116
3117 /* do rx first for enable and last for disable */
3118 if (request) {
3119 ret = i40e_vsi_control_rx(vsi, request);
3120 if (ret)
3121 return ret;
3122 ret = i40e_vsi_control_tx(vsi, request);
3123 } else {
3124 ret = i40e_vsi_control_tx(vsi, request);
3125 if (ret)
3126 return ret;
3127 ret = i40e_vsi_control_rx(vsi, request);
3128 }
3129
3130 return ret;
3131}
3132
3133/**
3134 * i40e_vsi_free_irq - Free the irq association with the OS
3135 * @vsi: the VSI being configured
3136 **/
3137static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
3138{
3139 struct i40e_pf *pf = vsi->back;
3140 struct i40e_hw *hw = &pf->hw;
3141 int base = vsi->base_vector;
3142 u32 val, qp;
3143 int i;
3144
3145 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3146 if (!vsi->q_vectors)
3147 return;
3148
3149 for (i = 0; i < vsi->num_q_vectors; i++) {
3150 u16 vector = i + base;
3151
3152 /* free only the irqs that were actually requested */
Alexander Duyck493fb302013-09-28 07:01:44 +00003153 if (vsi->q_vectors[i]->num_ringpairs == 0)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003154 continue;
3155
3156 /* clear the affinity_mask in the IRQ descriptor */
3157 irq_set_affinity_hint(pf->msix_entries[vector].vector,
3158 NULL);
3159 free_irq(pf->msix_entries[vector].vector,
Alexander Duyck493fb302013-09-28 07:01:44 +00003160 vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003161
3162 /* Tear down the interrupt queue link list
3163 *
3164 * We know that they come in pairs and always
3165 * the Rx first, then the Tx. To clear the
3166 * link list, stick the EOL value into the
3167 * next_q field of the registers.
3168 */
3169 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
3170 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3171 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3172 val |= I40E_QUEUE_END_OF_LIST
3173 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3174 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
3175
3176 while (qp != I40E_QUEUE_END_OF_LIST) {
3177 u32 next;
3178
3179 val = rd32(hw, I40E_QINT_RQCTL(qp));
3180
3181 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3182 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3183 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3184 I40E_QINT_RQCTL_INTEVENT_MASK);
3185
3186 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3187 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3188
3189 wr32(hw, I40E_QINT_RQCTL(qp), val);
3190
3191 val = rd32(hw, I40E_QINT_TQCTL(qp));
3192
3193 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
3194 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
3195
3196 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3197 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3198 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3199 I40E_QINT_TQCTL_INTEVENT_MASK);
3200
3201 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3202 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3203
3204 wr32(hw, I40E_QINT_TQCTL(qp), val);
3205 qp = next;
3206 }
3207 }
3208 } else {
3209 free_irq(pf->pdev->irq, pf);
3210
3211 val = rd32(hw, I40E_PFINT_LNKLST0);
3212 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3213 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3214 val |= I40E_QUEUE_END_OF_LIST
3215 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
3216 wr32(hw, I40E_PFINT_LNKLST0, val);
3217
3218 val = rd32(hw, I40E_QINT_RQCTL(qp));
3219 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3220 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3221 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3222 I40E_QINT_RQCTL_INTEVENT_MASK);
3223
3224 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3225 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3226
3227 wr32(hw, I40E_QINT_RQCTL(qp), val);
3228
3229 val = rd32(hw, I40E_QINT_TQCTL(qp));
3230
3231 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3232 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3233 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3234 I40E_QINT_TQCTL_INTEVENT_MASK);
3235
3236 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3237 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3238
3239 wr32(hw, I40E_QINT_TQCTL(qp), val);
3240 }
3241}
3242
3243/**
Alexander Duyck493fb302013-09-28 07:01:44 +00003244 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
3245 * @vsi: the VSI being configured
3246 * @v_idx: Index of vector to be freed
3247 *
3248 * This function frees the memory allocated to the q_vector. In addition if
3249 * NAPI is enabled it will delete any references to the NAPI struct prior
3250 * to freeing the q_vector.
3251 **/
3252static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
3253{
3254 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003255 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00003256
3257 if (!q_vector)
3258 return;
3259
3260 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003261 i40e_for_each_ring(ring, q_vector->tx)
3262 ring->q_vector = NULL;
3263
3264 i40e_for_each_ring(ring, q_vector->rx)
3265 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00003266
3267 /* only VSI w/ an associated netdev is set up w/ NAPI */
3268 if (vsi->netdev)
3269 netif_napi_del(&q_vector->napi);
3270
3271 vsi->q_vectors[v_idx] = NULL;
3272
3273 kfree_rcu(q_vector, rcu);
3274}
3275
3276/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003277 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
3278 * @vsi: the VSI being un-configured
3279 *
3280 * This frees the memory allocated to the q_vectors and
3281 * deletes references to the NAPI struct.
3282 **/
3283static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
3284{
3285 int v_idx;
3286
Alexander Duyck493fb302013-09-28 07:01:44 +00003287 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
3288 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003289}
3290
3291/**
3292 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
3293 * @pf: board private structure
3294 **/
3295static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
3296{
3297 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
3298 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3299 pci_disable_msix(pf->pdev);
3300 kfree(pf->msix_entries);
3301 pf->msix_entries = NULL;
3302 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
3303 pci_disable_msi(pf->pdev);
3304 }
3305 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
3306}
3307
3308/**
3309 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
3310 * @pf: board private structure
3311 *
3312 * We go through and clear interrupt specific resources and reset the structure
3313 * to pre-load conditions
3314 **/
3315static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
3316{
3317 int i;
3318
3319 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
3320 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
3321 if (pf->vsi[i])
3322 i40e_vsi_free_q_vectors(pf->vsi[i]);
3323 i40e_reset_interrupt_capability(pf);
3324}
3325
3326/**
3327 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
3328 * @vsi: the VSI being configured
3329 **/
3330static void i40e_napi_enable_all(struct i40e_vsi *vsi)
3331{
3332 int q_idx;
3333
3334 if (!vsi->netdev)
3335 return;
3336
3337 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003338 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003339}
3340
3341/**
3342 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
3343 * @vsi: the VSI being configured
3344 **/
3345static void i40e_napi_disable_all(struct i40e_vsi *vsi)
3346{
3347 int q_idx;
3348
3349 if (!vsi->netdev)
3350 return;
3351
3352 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003353 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003354}
3355
3356/**
3357 * i40e_quiesce_vsi - Pause a given VSI
3358 * @vsi: the VSI being paused
3359 **/
3360static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
3361{
3362 if (test_bit(__I40E_DOWN, &vsi->state))
3363 return;
3364
3365 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
3366 if (vsi->netdev && netif_running(vsi->netdev)) {
3367 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
3368 } else {
3369 set_bit(__I40E_DOWN, &vsi->state);
3370 i40e_down(vsi);
3371 }
3372}
3373
3374/**
3375 * i40e_unquiesce_vsi - Resume a given VSI
3376 * @vsi: the VSI being resumed
3377 **/
3378static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
3379{
3380 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
3381 return;
3382
3383 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
3384 if (vsi->netdev && netif_running(vsi->netdev))
3385 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
3386 else
3387 i40e_up(vsi); /* this clears the DOWN bit */
3388}
3389
3390/**
3391 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
3392 * @pf: the PF
3393 **/
3394static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
3395{
3396 int v;
3397
3398 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
3399 if (pf->vsi[v])
3400 i40e_quiesce_vsi(pf->vsi[v]);
3401 }
3402}
3403
3404/**
3405 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
3406 * @pf: the PF
3407 **/
3408static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
3409{
3410 int v;
3411
3412 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
3413 if (pf->vsi[v])
3414 i40e_unquiesce_vsi(pf->vsi[v]);
3415 }
3416}
3417
3418/**
3419 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
3420 * @dcbcfg: the corresponding DCBx configuration structure
3421 *
3422 * Return the number of TCs from given DCBx configuration
3423 **/
3424static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
3425{
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003426 u8 num_tc = 0;
3427 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003428
3429 /* Scan the ETS Config Priority Table to find
3430 * traffic class enabled for a given priority
3431 * and use the traffic class index to get the
3432 * number of traffic classes enabled
3433 */
3434 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3435 if (dcbcfg->etscfg.prioritytable[i] > num_tc)
3436 num_tc = dcbcfg->etscfg.prioritytable[i];
3437 }
3438
3439 /* Traffic class index starts from zero so
3440 * increment to return the actual count
3441 */
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003442 return num_tc + 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003443}
3444
3445/**
3446 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
3447 * @dcbcfg: the corresponding DCBx configuration structure
3448 *
3449 * Query the current DCB configuration and return the number of
3450 * traffic classes enabled from the given DCBX config
3451 **/
3452static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
3453{
3454 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
3455 u8 enabled_tc = 1;
3456 u8 i;
3457
3458 for (i = 0; i < num_tc; i++)
3459 enabled_tc |= 1 << i;
3460
3461 return enabled_tc;
3462}
3463
3464/**
3465 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
3466 * @pf: PF being queried
3467 *
3468 * Return number of traffic classes enabled for the given PF
3469 **/
3470static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
3471{
3472 struct i40e_hw *hw = &pf->hw;
3473 u8 i, enabled_tc;
3474 u8 num_tc = 0;
3475 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3476
3477 /* If DCB is not enabled then always in single TC */
3478 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3479 return 1;
3480
3481 /* MFP mode return count of enabled TCs for this PF */
3482 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
3483 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3484 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3485 if (enabled_tc & (1 << i))
3486 num_tc++;
3487 }
3488 return num_tc;
3489 }
3490
3491 /* SFP mode will be enabled for all TCs on port */
3492 return i40e_dcb_get_num_tc(dcbcfg);
3493}
3494
3495/**
3496 * i40e_pf_get_default_tc - Get bitmap for first enabled TC
3497 * @pf: PF being queried
3498 *
3499 * Return a bitmap for first enabled traffic class for this PF.
3500 **/
3501static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
3502{
3503 u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3504 u8 i = 0;
3505
3506 if (!enabled_tc)
3507 return 0x1; /* TC0 */
3508
3509 /* Find the first enabled TC */
3510 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3511 if (enabled_tc & (1 << i))
3512 break;
3513 }
3514
3515 return 1 << i;
3516}
3517
3518/**
3519 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
3520 * @pf: PF being queried
3521 *
3522 * Return a bitmap for enabled traffic classes for this PF.
3523 **/
3524static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
3525{
3526 /* If DCB is not enabled for this PF then just return default TC */
3527 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3528 return i40e_pf_get_default_tc(pf);
3529
3530 /* MFP mode will have enabled TCs set by FW */
3531 if (pf->flags & I40E_FLAG_MFP_ENABLED)
3532 return pf->hw.func_caps.enabled_tcmap;
3533
3534 /* SFP mode we want PF to be enabled for all TCs */
3535 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
3536}
3537
3538/**
3539 * i40e_vsi_get_bw_info - Query VSI BW Information
3540 * @vsi: the VSI being queried
3541 *
3542 * Returns 0 on success, negative value on failure
3543 **/
3544static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
3545{
3546 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
3547 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
3548 struct i40e_pf *pf = vsi->back;
3549 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003550 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003551 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003552 int i;
3553
3554 /* Get the VSI level BW configuration */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003555 aq_ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
3556 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003557 dev_info(&pf->pdev->dev,
3558 "couldn't get pf vsi bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003559 aq_ret, pf->hw.aq.asq_last_status);
3560 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003561 }
3562
3563 /* Get the VSI level BW configuration per TC */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003564 aq_ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
3565 NULL);
3566 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003567 dev_info(&pf->pdev->dev,
3568 "couldn't get pf vsi ets bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003569 aq_ret, pf->hw.aq.asq_last_status);
3570 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003571 }
3572
3573 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
3574 dev_info(&pf->pdev->dev,
3575 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
3576 bw_config.tc_valid_bits,
3577 bw_ets_config.tc_valid_bits);
3578 /* Still continuing */
3579 }
3580
3581 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
3582 vsi->bw_max_quanta = bw_config.max_bw;
3583 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
3584 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
3585 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3586 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
3587 vsi->bw_ets_limit_credits[i] =
3588 le16_to_cpu(bw_ets_config.credits[i]);
3589 /* 3 bits out of 4 for each TC */
3590 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
3591 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003592
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003593 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003594}
3595
3596/**
3597 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
3598 * @vsi: the VSI being configured
3599 * @enabled_tc: TC bitmap
3600 * @bw_credits: BW shared credits per TC
3601 *
3602 * Returns 0 on success, negative value on failure
3603 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003604static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003605 u8 *bw_share)
3606{
3607 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003608 i40e_status aq_ret;
3609 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003610
3611 bw_data.tc_valid_bits = enabled_tc;
3612 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3613 bw_data.tc_bw_credits[i] = bw_share[i];
3614
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003615 aq_ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
3616 NULL);
3617 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003618 dev_info(&vsi->back->pdev->dev,
3619 "%s: AQ command Config VSI BW allocation per TC failed = %d\n",
3620 __func__, vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003621 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003622 }
3623
3624 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3625 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
3626
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003627 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003628}
3629
3630/**
3631 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
3632 * @vsi: the VSI being configured
3633 * @enabled_tc: TC map to be enabled
3634 *
3635 **/
3636static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3637{
3638 struct net_device *netdev = vsi->netdev;
3639 struct i40e_pf *pf = vsi->back;
3640 struct i40e_hw *hw = &pf->hw;
3641 u8 netdev_tc = 0;
3642 int i;
3643 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3644
3645 if (!netdev)
3646 return;
3647
3648 if (!enabled_tc) {
3649 netdev_reset_tc(netdev);
3650 return;
3651 }
3652
3653 /* Set up actual enabled TCs on the VSI */
3654 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
3655 return;
3656
3657 /* set per TC queues for the VSI */
3658 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3659 /* Only set TC queues for enabled tcs
3660 *
3661 * e.g. For a VSI that has TC0 and TC3 enabled the
3662 * enabled_tc bitmap would be 0x00001001; the driver
3663 * will set the numtc for netdev as 2 that will be
3664 * referenced by the netdev layer as TC 0 and 1.
3665 */
3666 if (vsi->tc_config.enabled_tc & (1 << i))
3667 netdev_set_tc_queue(netdev,
3668 vsi->tc_config.tc_info[i].netdev_tc,
3669 vsi->tc_config.tc_info[i].qcount,
3670 vsi->tc_config.tc_info[i].qoffset);
3671 }
3672
3673 /* Assign UP2TC map for the VSI */
3674 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3675 /* Get the actual TC# for the UP */
3676 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
3677 /* Get the mapped netdev TC# for the UP */
3678 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
3679 netdev_set_prio_tc_map(netdev, i, netdev_tc);
3680 }
3681}
3682
3683/**
3684 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
3685 * @vsi: the VSI being configured
3686 * @ctxt: the ctxt buffer returned from AQ VSI update param command
3687 **/
3688static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
3689 struct i40e_vsi_context *ctxt)
3690{
3691 /* copy just the sections touched not the entire info
3692 * since not all sections are valid as returned by
3693 * update vsi params
3694 */
3695 vsi->info.mapping_flags = ctxt->info.mapping_flags;
3696 memcpy(&vsi->info.queue_mapping,
3697 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
3698 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
3699 sizeof(vsi->info.tc_mapping));
3700}
3701
3702/**
3703 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
3704 * @vsi: VSI to be configured
3705 * @enabled_tc: TC bitmap
3706 *
3707 * This configures a particular VSI for TCs that are mapped to the
3708 * given TC bitmap. It uses default bandwidth share for TCs across
3709 * VSIs to configure TC for a particular VSI.
3710 *
3711 * NOTE:
3712 * It is expected that the VSI queues have been quisced before calling
3713 * this function.
3714 **/
3715static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3716{
3717 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
3718 struct i40e_vsi_context ctxt;
3719 int ret = 0;
3720 int i;
3721
3722 /* Check if enabled_tc is same as existing or new TCs */
3723 if (vsi->tc_config.enabled_tc == enabled_tc)
3724 return ret;
3725
3726 /* Enable ETS TCs with equal BW Share for now across all VSIs */
3727 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3728 if (enabled_tc & (1 << i))
3729 bw_share[i] = 1;
3730 }
3731
3732 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
3733 if (ret) {
3734 dev_info(&vsi->back->pdev->dev,
3735 "Failed configuring TC map %d for VSI %d\n",
3736 enabled_tc, vsi->seid);
3737 goto out;
3738 }
3739
3740 /* Update Queue Pairs Mapping for currently enabled UPs */
3741 ctxt.seid = vsi->seid;
3742 ctxt.pf_num = vsi->back->hw.pf_id;
3743 ctxt.vf_num = 0;
3744 ctxt.uplink_seid = vsi->uplink_seid;
3745 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
3746 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
3747
3748 /* Update the VSI after updating the VSI queue-mapping information */
3749 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3750 if (ret) {
3751 dev_info(&vsi->back->pdev->dev,
3752 "update vsi failed, aq_err=%d\n",
3753 vsi->back->hw.aq.asq_last_status);
3754 goto out;
3755 }
3756 /* update the local VSI info with updated queue map */
3757 i40e_vsi_update_queue_map(vsi, &ctxt);
3758 vsi->info.valid_sections = 0;
3759
3760 /* Update current VSI BW information */
3761 ret = i40e_vsi_get_bw_info(vsi);
3762 if (ret) {
3763 dev_info(&vsi->back->pdev->dev,
3764 "Failed updating vsi bw info, aq_err=%d\n",
3765 vsi->back->hw.aq.asq_last_status);
3766 goto out;
3767 }
3768
3769 /* Update the netdev TC setup */
3770 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
3771out:
3772 return ret;
3773}
3774
3775/**
3776 * i40e_up_complete - Finish the last steps of bringing up a connection
3777 * @vsi: the VSI being configured
3778 **/
3779static int i40e_up_complete(struct i40e_vsi *vsi)
3780{
3781 struct i40e_pf *pf = vsi->back;
3782 int err;
3783
3784 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3785 i40e_vsi_configure_msix(vsi);
3786 else
3787 i40e_configure_msi_and_legacy(vsi);
3788
3789 /* start rings */
3790 err = i40e_vsi_control_rings(vsi, true);
3791 if (err)
3792 return err;
3793
3794 clear_bit(__I40E_DOWN, &vsi->state);
3795 i40e_napi_enable_all(vsi);
3796 i40e_vsi_enable_irq(vsi);
3797
3798 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
3799 (vsi->netdev)) {
Anjali Singhai6d779b42013-09-28 06:00:02 +00003800 netdev_info(vsi->netdev, "NIC Link is Up\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003801 netif_tx_start_all_queues(vsi->netdev);
3802 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00003803 } else if (vsi->netdev) {
3804 netdev_info(vsi->netdev, "NIC Link is Down\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003805 }
3806 i40e_service_event_schedule(pf);
3807
3808 return 0;
3809}
3810
3811/**
3812 * i40e_vsi_reinit_locked - Reset the VSI
3813 * @vsi: the VSI being configured
3814 *
3815 * Rebuild the ring structs after some configuration
3816 * has changed, e.g. MTU size.
3817 **/
3818static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
3819{
3820 struct i40e_pf *pf = vsi->back;
3821
3822 WARN_ON(in_interrupt());
3823 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
3824 usleep_range(1000, 2000);
3825 i40e_down(vsi);
3826
3827 /* Give a VF some time to respond to the reset. The
3828 * two second wait is based upon the watchdog cycle in
3829 * the VF driver.
3830 */
3831 if (vsi->type == I40E_VSI_SRIOV)
3832 msleep(2000);
3833 i40e_up(vsi);
3834 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
3835}
3836
3837/**
3838 * i40e_up - Bring the connection back up after being down
3839 * @vsi: the VSI being configured
3840 **/
3841int i40e_up(struct i40e_vsi *vsi)
3842{
3843 int err;
3844
3845 err = i40e_vsi_configure(vsi);
3846 if (!err)
3847 err = i40e_up_complete(vsi);
3848
3849 return err;
3850}
3851
3852/**
3853 * i40e_down - Shutdown the connection processing
3854 * @vsi: the VSI being stopped
3855 **/
3856void i40e_down(struct i40e_vsi *vsi)
3857{
3858 int i;
3859
3860 /* It is assumed that the caller of this function
3861 * sets the vsi->state __I40E_DOWN bit.
3862 */
3863 if (vsi->netdev) {
3864 netif_carrier_off(vsi->netdev);
3865 netif_tx_disable(vsi->netdev);
3866 }
3867 i40e_vsi_disable_irq(vsi);
3868 i40e_vsi_control_rings(vsi, false);
3869 i40e_napi_disable_all(vsi);
3870
3871 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00003872 i40e_clean_tx_ring(vsi->tx_rings[i]);
3873 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003874 }
3875}
3876
3877/**
3878 * i40e_setup_tc - configure multiple traffic classes
3879 * @netdev: net device to configure
3880 * @tc: number of traffic classes to enable
3881 **/
3882static int i40e_setup_tc(struct net_device *netdev, u8 tc)
3883{
3884 struct i40e_netdev_priv *np = netdev_priv(netdev);
3885 struct i40e_vsi *vsi = np->vsi;
3886 struct i40e_pf *pf = vsi->back;
3887 u8 enabled_tc = 0;
3888 int ret = -EINVAL;
3889 int i;
3890
3891 /* Check if DCB enabled to continue */
3892 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
3893 netdev_info(netdev, "DCB is not enabled for adapter\n");
3894 goto exit;
3895 }
3896
3897 /* Check if MFP enabled */
3898 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
3899 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
3900 goto exit;
3901 }
3902
3903 /* Check whether tc count is within enabled limit */
3904 if (tc > i40e_pf_get_num_tc(pf)) {
3905 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
3906 goto exit;
3907 }
3908
3909 /* Generate TC map for number of tc requested */
3910 for (i = 0; i < tc; i++)
3911 enabled_tc |= (1 << i);
3912
3913 /* Requesting same TC configuration as already enabled */
3914 if (enabled_tc == vsi->tc_config.enabled_tc)
3915 return 0;
3916
3917 /* Quiesce VSI queues */
3918 i40e_quiesce_vsi(vsi);
3919
3920 /* Configure VSI for enabled TCs */
3921 ret = i40e_vsi_config_tc(vsi, enabled_tc);
3922 if (ret) {
3923 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
3924 vsi->seid);
3925 goto exit;
3926 }
3927
3928 /* Unquiesce VSI */
3929 i40e_unquiesce_vsi(vsi);
3930
3931exit:
3932 return ret;
3933}
3934
3935/**
3936 * i40e_open - Called when a network interface is made active
3937 * @netdev: network interface device structure
3938 *
3939 * The open entry point is called when a network interface is made
3940 * active by the system (IFF_UP). At this point all resources needed
3941 * for transmit and receive operations are allocated, the interrupt
3942 * handler is registered with the OS, the netdev watchdog subtask is
3943 * enabled, and the stack is notified that the interface is ready.
3944 *
3945 * Returns 0 on success, negative value on failure
3946 **/
3947static int i40e_open(struct net_device *netdev)
3948{
3949 struct i40e_netdev_priv *np = netdev_priv(netdev);
3950 struct i40e_vsi *vsi = np->vsi;
3951 struct i40e_pf *pf = vsi->back;
3952 char int_name[IFNAMSIZ];
3953 int err;
3954
3955 /* disallow open during test */
3956 if (test_bit(__I40E_TESTING, &pf->state))
3957 return -EBUSY;
3958
3959 netif_carrier_off(netdev);
3960
3961 /* allocate descriptors */
3962 err = i40e_vsi_setup_tx_resources(vsi);
3963 if (err)
3964 goto err_setup_tx;
3965 err = i40e_vsi_setup_rx_resources(vsi);
3966 if (err)
3967 goto err_setup_rx;
3968
3969 err = i40e_vsi_configure(vsi);
3970 if (err)
3971 goto err_setup_rx;
3972
3973 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
3974 dev_driver_string(&pf->pdev->dev), netdev->name);
3975 err = i40e_vsi_request_irq(vsi, int_name);
3976 if (err)
3977 goto err_setup_rx;
3978
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00003979 /* Notify the stack of the actual queue counts. */
3980 err = netif_set_real_num_tx_queues(netdev, pf->num_tx_queues);
3981 if (err)
3982 goto err_set_queues;
3983
3984 err = netif_set_real_num_rx_queues(netdev, pf->num_rx_queues);
3985 if (err)
3986 goto err_set_queues;
3987
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003988 err = i40e_up_complete(vsi);
3989 if (err)
3990 goto err_up_complete;
3991
3992 if ((vsi->type == I40E_VSI_MAIN) || (vsi->type == I40E_VSI_VMDQ2)) {
3993 err = i40e_aq_set_vsi_broadcast(&pf->hw, vsi->seid, true, NULL);
3994 if (err)
3995 netdev_info(netdev,
3996 "couldn't set broadcast err %d aq_err %d\n",
3997 err, pf->hw.aq.asq_last_status);
3998 }
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00003999#ifdef CONFIG_I40E_VXLAN
4000 vxlan_get_rx_port(netdev);
4001#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004002
4003 return 0;
4004
4005err_up_complete:
4006 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004007err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004008 i40e_vsi_free_irq(vsi);
4009err_setup_rx:
4010 i40e_vsi_free_rx_resources(vsi);
4011err_setup_tx:
4012 i40e_vsi_free_tx_resources(vsi);
4013 if (vsi == pf->vsi[pf->lan_vsi])
4014 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
4015
4016 return err;
4017}
4018
4019/**
4020 * i40e_close - Disables a network interface
4021 * @netdev: network interface device structure
4022 *
4023 * The close entry point is called when an interface is de-activated
4024 * by the OS. The hardware is still under the driver's control, but
4025 * this netdev interface is disabled.
4026 *
4027 * Returns 0, this is not allowed to fail
4028 **/
4029static int i40e_close(struct net_device *netdev)
4030{
4031 struct i40e_netdev_priv *np = netdev_priv(netdev);
4032 struct i40e_vsi *vsi = np->vsi;
4033
4034 if (test_and_set_bit(__I40E_DOWN, &vsi->state))
4035 return 0;
4036
4037 i40e_down(vsi);
4038 i40e_vsi_free_irq(vsi);
4039
4040 i40e_vsi_free_tx_resources(vsi);
4041 i40e_vsi_free_rx_resources(vsi);
4042
4043 return 0;
4044}
4045
4046/**
4047 * i40e_do_reset - Start a PF or Core Reset sequence
4048 * @pf: board private structure
4049 * @reset_flags: which reset is requested
4050 *
4051 * The essential difference in resets is that the PF Reset
4052 * doesn't clear the packet buffers, doesn't reset the PE
4053 * firmware, and doesn't bother the other PFs on the chip.
4054 **/
4055void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
4056{
4057 u32 val;
4058
4059 WARN_ON(in_interrupt());
4060
4061 /* do the biggest reset indicated */
4062 if (reset_flags & (1 << __I40E_GLOBAL_RESET_REQUESTED)) {
4063
4064 /* Request a Global Reset
4065 *
4066 * This will start the chip's countdown to the actual full
4067 * chip reset event, and a warning interrupt to be sent
4068 * to all PFs, including the requestor. Our handler
4069 * for the warning interrupt will deal with the shutdown
4070 * and recovery of the switch setup.
4071 */
4072 dev_info(&pf->pdev->dev, "GlobalR requested\n");
4073 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4074 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
4075 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4076
4077 } else if (reset_flags & (1 << __I40E_CORE_RESET_REQUESTED)) {
4078
4079 /* Request a Core Reset
4080 *
4081 * Same as Global Reset, except does *not* include the MAC/PHY
4082 */
4083 dev_info(&pf->pdev->dev, "CoreR requested\n");
4084 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4085 val |= I40E_GLGEN_RTRIG_CORER_MASK;
4086 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4087 i40e_flush(&pf->hw);
4088
Shannon Nelson7823fe32013-11-16 10:00:45 +00004089 } else if (reset_flags & (1 << __I40E_EMP_RESET_REQUESTED)) {
4090
4091 /* Request a Firmware Reset
4092 *
4093 * Same as Global reset, plus restarting the
4094 * embedded firmware engine.
4095 */
4096 /* enable EMP Reset */
4097 val = rd32(&pf->hw, I40E_GLGEN_RSTENA_EMP);
4098 val |= I40E_GLGEN_RSTENA_EMP_EMP_RST_ENA_MASK;
4099 wr32(&pf->hw, I40E_GLGEN_RSTENA_EMP, val);
4100
4101 /* force the reset */
4102 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4103 val |= I40E_GLGEN_RTRIG_EMPFWR_MASK;
4104 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4105 i40e_flush(&pf->hw);
4106
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004107 } else if (reset_flags & (1 << __I40E_PF_RESET_REQUESTED)) {
4108
4109 /* Request a PF Reset
4110 *
4111 * Resets only the PF-specific registers
4112 *
4113 * This goes directly to the tear-down and rebuild of
4114 * the switch, since we need to do all the recovery as
4115 * for the Core Reset.
4116 */
4117 dev_info(&pf->pdev->dev, "PFR requested\n");
4118 i40e_handle_reset_warning(pf);
4119
4120 } else if (reset_flags & (1 << __I40E_REINIT_REQUESTED)) {
4121 int v;
4122
4123 /* Find the VSI(s) that requested a re-init */
4124 dev_info(&pf->pdev->dev,
4125 "VSI reinit requested\n");
4126 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4127 struct i40e_vsi *vsi = pf->vsi[v];
4128 if (vsi != NULL &&
4129 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
4130 i40e_vsi_reinit_locked(pf->vsi[v]);
4131 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
4132 }
4133 }
4134
4135 /* no further action needed, so return now */
4136 return;
4137 } else {
4138 dev_info(&pf->pdev->dev,
4139 "bad reset request 0x%08x\n", reset_flags);
4140 return;
4141 }
4142}
4143
4144/**
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004145 * i40e_do_reset_safe - Protected reset path for userland calls.
4146 * @pf: board private structure
4147 * @reset_flags: which reset is requested
4148 *
4149 **/
4150void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
4151{
4152 rtnl_lock();
4153 i40e_do_reset(pf, reset_flags);
4154 rtnl_unlock();
4155}
4156
4157/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004158 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
4159 * @pf: board private structure
4160 * @e: event info posted on ARQ
4161 *
4162 * Handler for LAN Queue Overflow Event generated by the firmware for PF
4163 * and VF queues
4164 **/
4165static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
4166 struct i40e_arq_event_info *e)
4167{
4168 struct i40e_aqc_lan_overflow *data =
4169 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
4170 u32 queue = le32_to_cpu(data->prtdcb_rupto);
4171 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
4172 struct i40e_hw *hw = &pf->hw;
4173 struct i40e_vf *vf;
4174 u16 vf_id;
4175
4176 dev_info(&pf->pdev->dev, "%s: Rx Queue Number = %d QTX_CTL=0x%08x\n",
4177 __func__, queue, qtx_ctl);
4178
4179 /* Queue belongs to VF, find the VF and issue VF reset */
4180 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
4181 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
4182 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
4183 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
4184 vf_id -= hw->func_caps.vf_base_id;
4185 vf = &pf->vf[vf_id];
4186 i40e_vc_notify_vf_reset(vf);
4187 /* Allow VF to process pending reset notification */
4188 msleep(20);
4189 i40e_reset_vf(vf, false);
4190 }
4191}
4192
4193/**
4194 * i40e_service_event_complete - Finish up the service event
4195 * @pf: board private structure
4196 **/
4197static void i40e_service_event_complete(struct i40e_pf *pf)
4198{
4199 BUG_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
4200
4201 /* flush memory to make sure state is correct before next watchog */
4202 smp_mb__before_clear_bit();
4203 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
4204}
4205
4206/**
4207 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
4208 * @pf: board private structure
4209 **/
4210static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
4211{
4212 if (!(pf->flags & I40E_FLAG_FDIR_REQUIRES_REINIT))
4213 return;
4214
4215 pf->flags &= ~I40E_FLAG_FDIR_REQUIRES_REINIT;
4216
4217 /* if interface is down do nothing */
4218 if (test_bit(__I40E_DOWN, &pf->state))
4219 return;
4220}
4221
4222/**
4223 * i40e_vsi_link_event - notify VSI of a link event
4224 * @vsi: vsi to be notified
4225 * @link_up: link up or down
4226 **/
4227static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
4228{
4229 if (!vsi)
4230 return;
4231
4232 switch (vsi->type) {
4233 case I40E_VSI_MAIN:
4234 if (!vsi->netdev || !vsi->netdev_registered)
4235 break;
4236
4237 if (link_up) {
4238 netif_carrier_on(vsi->netdev);
4239 netif_tx_wake_all_queues(vsi->netdev);
4240 } else {
4241 netif_carrier_off(vsi->netdev);
4242 netif_tx_stop_all_queues(vsi->netdev);
4243 }
4244 break;
4245
4246 case I40E_VSI_SRIOV:
4247 break;
4248
4249 case I40E_VSI_VMDQ2:
4250 case I40E_VSI_CTRL:
4251 case I40E_VSI_MIRROR:
4252 default:
4253 /* there is no notification for other VSIs */
4254 break;
4255 }
4256}
4257
4258/**
4259 * i40e_veb_link_event - notify elements on the veb of a link event
4260 * @veb: veb to be notified
4261 * @link_up: link up or down
4262 **/
4263static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
4264{
4265 struct i40e_pf *pf;
4266 int i;
4267
4268 if (!veb || !veb->pf)
4269 return;
4270 pf = veb->pf;
4271
4272 /* depth first... */
4273 for (i = 0; i < I40E_MAX_VEB; i++)
4274 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
4275 i40e_veb_link_event(pf->veb[i], link_up);
4276
4277 /* ... now the local VSIs */
4278 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
4279 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
4280 i40e_vsi_link_event(pf->vsi[i], link_up);
4281}
4282
4283/**
4284 * i40e_link_event - Update netif_carrier status
4285 * @pf: board private structure
4286 **/
4287static void i40e_link_event(struct i40e_pf *pf)
4288{
4289 bool new_link, old_link;
4290
4291 new_link = (pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP);
4292 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
4293
4294 if (new_link == old_link)
4295 return;
4296
Anjali Singhai6d779b42013-09-28 06:00:02 +00004297 if (!test_bit(__I40E_DOWN, &pf->vsi[pf->lan_vsi]->state))
4298 netdev_info(pf->vsi[pf->lan_vsi]->netdev,
4299 "NIC Link is %s\n", (new_link ? "Up" : "Down"));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004300
4301 /* Notify the base of the switch tree connected to
4302 * the link. Floating VEBs are not notified.
4303 */
4304 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
4305 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
4306 else
4307 i40e_vsi_link_event(pf->vsi[pf->lan_vsi], new_link);
4308
4309 if (pf->vf)
4310 i40e_vc_notify_link_state(pf);
4311}
4312
4313/**
4314 * i40e_check_hang_subtask - Check for hung queues and dropped interrupts
4315 * @pf: board private structure
4316 *
4317 * Set the per-queue flags to request a check for stuck queues in the irq
4318 * clean functions, then force interrupts to be sure the irq clean is called.
4319 **/
4320static void i40e_check_hang_subtask(struct i40e_pf *pf)
4321{
4322 int i, v;
4323
4324 /* If we're down or resetting, just bail */
4325 if (test_bit(__I40E_CONFIG_BUSY, &pf->state))
4326 return;
4327
4328 /* for each VSI/netdev
4329 * for each Tx queue
4330 * set the check flag
4331 * for each q_vector
4332 * force an interrupt
4333 */
4334 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4335 struct i40e_vsi *vsi = pf->vsi[v];
4336 int armed = 0;
4337
4338 if (!pf->vsi[v] ||
4339 test_bit(__I40E_DOWN, &vsi->state) ||
4340 (vsi->netdev && !netif_carrier_ok(vsi->netdev)))
4341 continue;
4342
4343 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004344 set_check_for_tx_hang(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004345 if (test_bit(__I40E_HANG_CHECK_ARMED,
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004346 &vsi->tx_rings[i]->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004347 armed++;
4348 }
4349
4350 if (armed) {
4351 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
4352 wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0,
4353 (I40E_PFINT_DYN_CTL0_INTENA_MASK |
4354 I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK));
4355 } else {
4356 u16 vec = vsi->base_vector - 1;
4357 u32 val = (I40E_PFINT_DYN_CTLN_INTENA_MASK |
4358 I40E_PFINT_DYN_CTLN_SWINT_TRIG_MASK);
4359 for (i = 0; i < vsi->num_q_vectors; i++, vec++)
4360 wr32(&vsi->back->hw,
4361 I40E_PFINT_DYN_CTLN(vec), val);
4362 }
4363 i40e_flush(&vsi->back->hw);
4364 }
4365 }
4366}
4367
4368/**
4369 * i40e_watchdog_subtask - Check and bring link up
4370 * @pf: board private structure
4371 **/
4372static void i40e_watchdog_subtask(struct i40e_pf *pf)
4373{
4374 int i;
4375
4376 /* if interface is down do nothing */
4377 if (test_bit(__I40E_DOWN, &pf->state) ||
4378 test_bit(__I40E_CONFIG_BUSY, &pf->state))
4379 return;
4380
4381 /* Update the stats for active netdevs so the network stack
4382 * can look at updated numbers whenever it cares to
4383 */
4384 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
4385 if (pf->vsi[i] && pf->vsi[i]->netdev)
4386 i40e_update_stats(pf->vsi[i]);
4387
4388 /* Update the stats for the active switching components */
4389 for (i = 0; i < I40E_MAX_VEB; i++)
4390 if (pf->veb[i])
4391 i40e_update_veb_stats(pf->veb[i]);
4392}
4393
4394/**
4395 * i40e_reset_subtask - Set up for resetting the device and driver
4396 * @pf: board private structure
4397 **/
4398static void i40e_reset_subtask(struct i40e_pf *pf)
4399{
4400 u32 reset_flags = 0;
4401
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004402 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004403 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
4404 reset_flags |= (1 << __I40E_REINIT_REQUESTED);
4405 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
4406 }
4407 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
4408 reset_flags |= (1 << __I40E_PF_RESET_REQUESTED);
4409 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
4410 }
4411 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
4412 reset_flags |= (1 << __I40E_CORE_RESET_REQUESTED);
4413 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
4414 }
4415 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
4416 reset_flags |= (1 << __I40E_GLOBAL_RESET_REQUESTED);
4417 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
4418 }
4419
4420 /* If there's a recovery already waiting, it takes
4421 * precedence before starting a new reset sequence.
4422 */
4423 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
4424 i40e_handle_reset_warning(pf);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004425 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004426 }
4427
4428 /* If we're already down or resetting, just bail */
4429 if (reset_flags &&
4430 !test_bit(__I40E_DOWN, &pf->state) &&
4431 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
4432 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004433
4434unlock:
4435 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004436}
4437
4438/**
4439 * i40e_handle_link_event - Handle link event
4440 * @pf: board private structure
4441 * @e: event info posted on ARQ
4442 **/
4443static void i40e_handle_link_event(struct i40e_pf *pf,
4444 struct i40e_arq_event_info *e)
4445{
4446 struct i40e_hw *hw = &pf->hw;
4447 struct i40e_aqc_get_link_status *status =
4448 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
4449 struct i40e_link_status *hw_link_info = &hw->phy.link_info;
4450
4451 /* save off old link status information */
4452 memcpy(&pf->hw.phy.link_info_old, hw_link_info,
4453 sizeof(pf->hw.phy.link_info_old));
4454
4455 /* update link status */
4456 hw_link_info->phy_type = (enum i40e_aq_phy_type)status->phy_type;
4457 hw_link_info->link_speed = (enum i40e_aq_link_speed)status->link_speed;
4458 hw_link_info->link_info = status->link_info;
4459 hw_link_info->an_info = status->an_info;
4460 hw_link_info->ext_info = status->ext_info;
4461 hw_link_info->lse_enable =
4462 le16_to_cpu(status->command_flags) &
4463 I40E_AQ_LSE_ENABLE;
4464
4465 /* process the event */
4466 i40e_link_event(pf);
4467
4468 /* Do a new status request to re-enable LSE reporting
4469 * and load new status information into the hw struct,
4470 * then see if the status changed while processing the
4471 * initial event.
4472 */
4473 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
4474 i40e_link_event(pf);
4475}
4476
4477/**
4478 * i40e_clean_adminq_subtask - Clean the AdminQ rings
4479 * @pf: board private structure
4480 **/
4481static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
4482{
4483 struct i40e_arq_event_info event;
4484 struct i40e_hw *hw = &pf->hw;
4485 u16 pending, i = 0;
4486 i40e_status ret;
4487 u16 opcode;
4488 u32 val;
4489
4490 if (!test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state))
4491 return;
4492
4493 event.msg_size = I40E_MAX_AQ_BUF_SIZE;
4494 event.msg_buf = kzalloc(event.msg_size, GFP_KERNEL);
4495 if (!event.msg_buf)
4496 return;
4497
4498 do {
4499 ret = i40e_clean_arq_element(hw, &event, &pending);
4500 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
4501 dev_info(&pf->pdev->dev, "No ARQ event found\n");
4502 break;
4503 } else if (ret) {
4504 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
4505 break;
4506 }
4507
4508 opcode = le16_to_cpu(event.desc.opcode);
4509 switch (opcode) {
4510
4511 case i40e_aqc_opc_get_link_status:
4512 i40e_handle_link_event(pf, &event);
4513 break;
4514 case i40e_aqc_opc_send_msg_to_pf:
4515 ret = i40e_vc_process_vf_msg(pf,
4516 le16_to_cpu(event.desc.retval),
4517 le32_to_cpu(event.desc.cookie_high),
4518 le32_to_cpu(event.desc.cookie_low),
4519 event.msg_buf,
4520 event.msg_size);
4521 break;
4522 case i40e_aqc_opc_lldp_update_mib:
4523 dev_info(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
4524 break;
4525 case i40e_aqc_opc_event_lan_overflow:
4526 dev_info(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
4527 i40e_handle_lan_overflow_event(pf, &event);
4528 break;
4529 default:
4530 dev_info(&pf->pdev->dev,
4531 "ARQ Error: Unknown event %d received\n",
4532 event.desc.opcode);
4533 break;
4534 }
4535 } while (pending && (i++ < pf->adminq_work_limit));
4536
4537 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
4538 /* re-enable Admin queue interrupt cause */
4539 val = rd32(hw, I40E_PFINT_ICR0_ENA);
4540 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4541 wr32(hw, I40E_PFINT_ICR0_ENA, val);
4542 i40e_flush(hw);
4543
4544 kfree(event.msg_buf);
4545}
4546
4547/**
4548 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
4549 * @veb: pointer to the VEB instance
4550 *
4551 * This is a recursive function that first builds the attached VSIs then
4552 * recurses in to build the next layer of VEB. We track the connections
4553 * through our own index numbers because the seid's from the HW could
4554 * change across the reset.
4555 **/
4556static int i40e_reconstitute_veb(struct i40e_veb *veb)
4557{
4558 struct i40e_vsi *ctl_vsi = NULL;
4559 struct i40e_pf *pf = veb->pf;
4560 int v, veb_idx;
4561 int ret;
4562
4563 /* build VSI that owns this VEB, temporarily attached to base VEB */
4564 for (v = 0; v < pf->hw.func_caps.num_vsis && !ctl_vsi; v++) {
4565 if (pf->vsi[v] &&
4566 pf->vsi[v]->veb_idx == veb->idx &&
4567 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
4568 ctl_vsi = pf->vsi[v];
4569 break;
4570 }
4571 }
4572 if (!ctl_vsi) {
4573 dev_info(&pf->pdev->dev,
4574 "missing owner VSI for veb_idx %d\n", veb->idx);
4575 ret = -ENOENT;
4576 goto end_reconstitute;
4577 }
4578 if (ctl_vsi != pf->vsi[pf->lan_vsi])
4579 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
4580 ret = i40e_add_vsi(ctl_vsi);
4581 if (ret) {
4582 dev_info(&pf->pdev->dev,
4583 "rebuild of owner VSI failed: %d\n", ret);
4584 goto end_reconstitute;
4585 }
4586 i40e_vsi_reset_stats(ctl_vsi);
4587
4588 /* create the VEB in the switch and move the VSI onto the VEB */
4589 ret = i40e_add_veb(veb, ctl_vsi);
4590 if (ret)
4591 goto end_reconstitute;
4592
4593 /* create the remaining VSIs attached to this VEB */
4594 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4595 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
4596 continue;
4597
4598 if (pf->vsi[v]->veb_idx == veb->idx) {
4599 struct i40e_vsi *vsi = pf->vsi[v];
4600 vsi->uplink_seid = veb->seid;
4601 ret = i40e_add_vsi(vsi);
4602 if (ret) {
4603 dev_info(&pf->pdev->dev,
4604 "rebuild of vsi_idx %d failed: %d\n",
4605 v, ret);
4606 goto end_reconstitute;
4607 }
4608 i40e_vsi_reset_stats(vsi);
4609 }
4610 }
4611
4612 /* create any VEBs attached to this VEB - RECURSION */
4613 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
4614 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
4615 pf->veb[veb_idx]->uplink_seid = veb->seid;
4616 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
4617 if (ret)
4618 break;
4619 }
4620 }
4621
4622end_reconstitute:
4623 return ret;
4624}
4625
4626/**
4627 * i40e_get_capabilities - get info about the HW
4628 * @pf: the PF struct
4629 **/
4630static int i40e_get_capabilities(struct i40e_pf *pf)
4631{
4632 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
4633 u16 data_size;
4634 int buf_len;
4635 int err;
4636
4637 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
4638 do {
4639 cap_buf = kzalloc(buf_len, GFP_KERNEL);
4640 if (!cap_buf)
4641 return -ENOMEM;
4642
4643 /* this loads the data into the hw struct for us */
4644 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
4645 &data_size,
4646 i40e_aqc_opc_list_func_capabilities,
4647 NULL);
4648 /* data loaded, buffer no longer needed */
4649 kfree(cap_buf);
4650
4651 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
4652 /* retry with a larger buffer */
4653 buf_len = data_size;
4654 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
4655 dev_info(&pf->pdev->dev,
4656 "capability discovery failed: aq=%d\n",
4657 pf->hw.aq.asq_last_status);
4658 return -ENODEV;
4659 }
4660 } while (err);
4661
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004662 if (pf->hw.revision_id == 0 && pf->hw.func_caps.npar_enable) {
4663 pf->hw.func_caps.num_msix_vectors += 1;
4664 pf->hw.func_caps.num_tx_qp =
4665 min_t(int, pf->hw.func_caps.num_tx_qp,
4666 I40E_MAX_NPAR_QPS);
4667 }
4668
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004669 if (pf->hw.debug_mask & I40E_DEBUG_USER)
4670 dev_info(&pf->pdev->dev,
4671 "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",
4672 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
4673 pf->hw.func_caps.num_msix_vectors,
4674 pf->hw.func_caps.num_msix_vectors_vf,
4675 pf->hw.func_caps.fd_filters_guaranteed,
4676 pf->hw.func_caps.fd_filters_best_effort,
4677 pf->hw.func_caps.num_tx_qp,
4678 pf->hw.func_caps.num_vsis);
4679
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004680#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
4681 + pf->hw.func_caps.num_vfs)
4682 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
4683 dev_info(&pf->pdev->dev,
4684 "got num_vsis %d, setting num_vsis to %d\n",
4685 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
4686 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
4687 }
4688
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004689 return 0;
4690}
4691
4692/**
4693 * i40e_fdir_setup - initialize the Flow Director resources
4694 * @pf: board private structure
4695 **/
4696static void i40e_fdir_setup(struct i40e_pf *pf)
4697{
4698 struct i40e_vsi *vsi;
4699 bool new_vsi = false;
4700 int err, i;
4701
Shannon Nelson958a3e32013-09-28 07:13:28 +00004702 if (!(pf->flags & (I40E_FLAG_FDIR_ENABLED |
4703 I40E_FLAG_FDIR_ATR_ENABLED)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004704 return;
4705
4706 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
4707
4708 /* find existing or make new FDIR VSI */
4709 vsi = NULL;
4710 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
4711 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR)
4712 vsi = pf->vsi[i];
4713 if (!vsi) {
4714 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR, pf->mac_seid, 0);
4715 if (!vsi) {
4716 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
4717 pf->flags &= ~I40E_FLAG_FDIR_ENABLED;
4718 return;
4719 }
4720 new_vsi = true;
4721 }
4722 WARN_ON(vsi->base_queue != I40E_FDIR_RING);
4723 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_rings);
4724
4725 err = i40e_vsi_setup_tx_resources(vsi);
4726 if (!err)
4727 err = i40e_vsi_setup_rx_resources(vsi);
4728 if (!err)
4729 err = i40e_vsi_configure(vsi);
4730 if (!err && new_vsi) {
4731 char int_name[IFNAMSIZ + 9];
4732 snprintf(int_name, sizeof(int_name) - 1, "%s-fdir",
4733 dev_driver_string(&pf->pdev->dev));
4734 err = i40e_vsi_request_irq(vsi, int_name);
4735 }
4736 if (!err)
4737 err = i40e_up_complete(vsi);
4738
4739 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
4740}
4741
4742/**
4743 * i40e_fdir_teardown - release the Flow Director resources
4744 * @pf: board private structure
4745 **/
4746static void i40e_fdir_teardown(struct i40e_pf *pf)
4747{
4748 int i;
4749
4750 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
4751 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
4752 i40e_vsi_release(pf->vsi[i]);
4753 break;
4754 }
4755 }
4756}
4757
4758/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00004759 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004760 * @pf: board private structure
4761 *
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00004762 * Close up the VFs and other things in prep for pf Reset.
4763 **/
4764static int i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004765{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004766 struct i40e_hw *hw = &pf->hw;
4767 i40e_status ret;
4768 u32 v;
4769
4770 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
4771 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00004772 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004773
4774 dev_info(&pf->pdev->dev, "Tearing down internal switch for reset\n");
4775
4776 i40e_vc_notify_reset(pf);
4777
4778 /* quiesce the VSIs and their queues that are not already DOWN */
4779 i40e_pf_quiesce_all_vsi(pf);
4780
4781 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4782 if (pf->vsi[v])
4783 pf->vsi[v]->seid = 0;
4784 }
4785
4786 i40e_shutdown_adminq(&pf->hw);
4787
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00004788 /* call shutdown HMC */
4789 ret = i40e_shutdown_lan_hmc(hw);
4790 if (ret) {
4791 dev_info(&pf->pdev->dev, "shutdown_lan_hmc failed: %d\n", ret);
4792 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
4793 }
4794 return ret;
4795}
4796
4797/**
4798 * i40e_reset_and_rebuild - reset and rebuid using a saved config
4799 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00004800 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00004801 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00004802static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00004803{
4804 struct i40e_driver_version dv;
4805 struct i40e_hw *hw = &pf->hw;
4806 i40e_status ret;
4807 u32 v;
4808
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004809 /* Now we wait for GRST to settle out.
4810 * We don't have to delete the VEBs or VSIs from the hw switch
4811 * because the reset will make them disappear.
4812 */
4813 ret = i40e_pf_reset(hw);
4814 if (ret)
4815 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
4816 pf->pfr_count++;
4817
4818 if (test_bit(__I40E_DOWN, &pf->state))
4819 goto end_core_reset;
4820 dev_info(&pf->pdev->dev, "Rebuilding internal switch\n");
4821
4822 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
4823 ret = i40e_init_adminq(&pf->hw);
4824 if (ret) {
4825 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, %d\n", ret);
4826 goto end_core_reset;
4827 }
4828
4829 ret = i40e_get_capabilities(pf);
4830 if (ret) {
4831 dev_info(&pf->pdev->dev, "i40e_get_capabilities failed, %d\n",
4832 ret);
4833 goto end_core_reset;
4834 }
4835
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004836 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
4837 hw->func_caps.num_rx_qp,
4838 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
4839 if (ret) {
4840 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
4841 goto end_core_reset;
4842 }
4843 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
4844 if (ret) {
4845 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
4846 goto end_core_reset;
4847 }
4848
4849 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00004850 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004851 if (ret)
4852 goto end_core_reset;
4853
4854 /* Rebuild the VSIs and VEBs that existed before reset.
4855 * They are still in our local switch element arrays, so only
4856 * need to rebuild the switch model in the HW.
4857 *
4858 * If there were VEBs but the reconstitution failed, we'll try
4859 * try to recover minimal use by getting the basic PF VSI working.
4860 */
4861 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
4862 dev_info(&pf->pdev->dev, "attempting to rebuild switch\n");
4863 /* find the one VEB connected to the MAC, and find orphans */
4864 for (v = 0; v < I40E_MAX_VEB; v++) {
4865 if (!pf->veb[v])
4866 continue;
4867
4868 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
4869 pf->veb[v]->uplink_seid == 0) {
4870 ret = i40e_reconstitute_veb(pf->veb[v]);
4871
4872 if (!ret)
4873 continue;
4874
4875 /* If Main VEB failed, we're in deep doodoo,
4876 * so give up rebuilding the switch and set up
4877 * for minimal rebuild of PF VSI.
4878 * If orphan failed, we'll report the error
4879 * but try to keep going.
4880 */
4881 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
4882 dev_info(&pf->pdev->dev,
4883 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
4884 ret);
4885 pf->vsi[pf->lan_vsi]->uplink_seid
4886 = pf->mac_seid;
4887 break;
4888 } else if (pf->veb[v]->uplink_seid == 0) {
4889 dev_info(&pf->pdev->dev,
4890 "rebuild of orphan VEB failed: %d\n",
4891 ret);
4892 }
4893 }
4894 }
4895 }
4896
4897 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
4898 dev_info(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
4899 /* no VEB, so rebuild only the Main VSI */
4900 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
4901 if (ret) {
4902 dev_info(&pf->pdev->dev,
4903 "rebuild of Main VSI failed: %d\n", ret);
4904 goto end_core_reset;
4905 }
4906 }
4907
4908 /* reinit the misc interrupt */
4909 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4910 ret = i40e_setup_misc_vector(pf);
4911
4912 /* restart the VSIs that were rebuilt and running before the reset */
4913 i40e_pf_unquiesce_all_vsi(pf);
4914
4915 /* tell the firmware that we're starting */
4916 dv.major_version = DRV_VERSION_MAJOR;
4917 dv.minor_version = DRV_VERSION_MINOR;
4918 dv.build_version = DRV_VERSION_BUILD;
4919 dv.subbuild_version = 0;
4920 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
4921
4922 dev_info(&pf->pdev->dev, "PF reset done\n");
4923
4924end_core_reset:
4925 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
4926}
4927
4928/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00004929 * i40e_handle_reset_warning - prep for the pf to reset, reset and rebuild
4930 * @pf: board private structure
4931 *
4932 * Close up the VFs and other things in prep for a Core Reset,
4933 * then get ready to rebuild the world.
4934 **/
4935static void i40e_handle_reset_warning(struct i40e_pf *pf)
4936{
4937 i40e_status ret;
4938
4939 ret = i40e_prep_for_reset(pf);
4940 if (!ret)
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00004941 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00004942}
4943
4944/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004945 * i40e_handle_mdd_event
4946 * @pf: pointer to the pf structure
4947 *
4948 * Called from the MDD irq handler to identify possibly malicious vfs
4949 **/
4950static void i40e_handle_mdd_event(struct i40e_pf *pf)
4951{
4952 struct i40e_hw *hw = &pf->hw;
4953 bool mdd_detected = false;
4954 struct i40e_vf *vf;
4955 u32 reg;
4956 int i;
4957
4958 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
4959 return;
4960
4961 /* find what triggered the MDD event */
4962 reg = rd32(hw, I40E_GL_MDET_TX);
4963 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
4964 u8 func = (reg & I40E_GL_MDET_TX_FUNCTION_MASK)
4965 >> I40E_GL_MDET_TX_FUNCTION_SHIFT;
4966 u8 event = (reg & I40E_GL_MDET_TX_EVENT_SHIFT)
4967 >> I40E_GL_MDET_TX_EVENT_SHIFT;
4968 u8 queue = (reg & I40E_GL_MDET_TX_QUEUE_MASK)
4969 >> I40E_GL_MDET_TX_QUEUE_SHIFT;
4970 dev_info(&pf->pdev->dev,
4971 "Malicious Driver Detection TX event 0x%02x on q %d of function 0x%02x\n",
4972 event, queue, func);
4973 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
4974 mdd_detected = true;
4975 }
4976 reg = rd32(hw, I40E_GL_MDET_RX);
4977 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
4978 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK)
4979 >> I40E_GL_MDET_RX_FUNCTION_SHIFT;
4980 u8 event = (reg & I40E_GL_MDET_RX_EVENT_SHIFT)
4981 >> I40E_GL_MDET_RX_EVENT_SHIFT;
4982 u8 queue = (reg & I40E_GL_MDET_RX_QUEUE_MASK)
4983 >> I40E_GL_MDET_RX_QUEUE_SHIFT;
4984 dev_info(&pf->pdev->dev,
4985 "Malicious Driver Detection RX event 0x%02x on q %d of function 0x%02x\n",
4986 event, queue, func);
4987 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
4988 mdd_detected = true;
4989 }
4990
4991 /* see if one of the VFs needs its hand slapped */
4992 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
4993 vf = &(pf->vf[i]);
4994 reg = rd32(hw, I40E_VP_MDET_TX(i));
4995 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
4996 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
4997 vf->num_mdd_events++;
4998 dev_info(&pf->pdev->dev, "MDD TX event on VF %d\n", i);
4999 }
5000
5001 reg = rd32(hw, I40E_VP_MDET_RX(i));
5002 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
5003 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
5004 vf->num_mdd_events++;
5005 dev_info(&pf->pdev->dev, "MDD RX event on VF %d\n", i);
5006 }
5007
5008 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
5009 dev_info(&pf->pdev->dev,
5010 "Too many MDD events on VF %d, disabled\n", i);
5011 dev_info(&pf->pdev->dev,
5012 "Use PF Control I/F to re-enable the VF\n");
5013 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
5014 }
5015 }
5016
5017 /* re-enable mdd interrupt cause */
5018 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
5019 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
5020 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
5021 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
5022 i40e_flush(hw);
5023}
5024
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005025#ifdef CONFIG_I40E_VXLAN
5026/**
5027 * i40e_sync_vxlan_filters_subtask - Sync the VSI filter list with HW
5028 * @pf: board private structure
5029 **/
5030static void i40e_sync_vxlan_filters_subtask(struct i40e_pf *pf)
5031{
5032 const int vxlan_hdr_qwords = 4;
5033 struct i40e_hw *hw = &pf->hw;
5034 i40e_status ret;
5035 u8 filter_index;
5036 __be16 port;
5037 int i;
5038
5039 if (!(pf->flags & I40E_FLAG_VXLAN_FILTER_SYNC))
5040 return;
5041
5042 pf->flags &= ~I40E_FLAG_VXLAN_FILTER_SYNC;
5043
5044 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
5045 if (pf->pending_vxlan_bitmap & (1 << i)) {
5046 pf->pending_vxlan_bitmap &= ~(1 << i);
5047 port = pf->vxlan_ports[i];
5048 ret = port ?
5049 i40e_aq_add_udp_tunnel(hw, ntohs(port),
5050 vxlan_hdr_qwords,
5051 I40E_AQC_TUNNEL_TYPE_VXLAN,
5052 &filter_index, NULL)
5053 : i40e_aq_del_udp_tunnel(hw, i, NULL);
5054
5055 if (ret) {
5056 dev_info(&pf->pdev->dev, "Failed to execute AQ command for %s port %d with index %d\n",
5057 port ? "adding" : "deleting",
5058 ntohs(port), port ? i : i);
5059
5060 pf->vxlan_ports[i] = 0;
5061 } else {
5062 dev_info(&pf->pdev->dev, "%s port %d with AQ command with index %d\n",
5063 port ? "Added" : "Deleted",
5064 ntohs(port), port ? i : filter_index);
5065 }
5066 }
5067 }
5068}
5069
5070#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005071/**
5072 * i40e_service_task - Run the driver's async subtasks
5073 * @work: pointer to work_struct containing our data
5074 **/
5075static void i40e_service_task(struct work_struct *work)
5076{
5077 struct i40e_pf *pf = container_of(work,
5078 struct i40e_pf,
5079 service_task);
5080 unsigned long start_time = jiffies;
5081
5082 i40e_reset_subtask(pf);
5083 i40e_handle_mdd_event(pf);
5084 i40e_vc_process_vflr_event(pf);
5085 i40e_watchdog_subtask(pf);
5086 i40e_fdir_reinit_subtask(pf);
5087 i40e_check_hang_subtask(pf);
5088 i40e_sync_filters_subtask(pf);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005089#ifdef CONFIG_I40E_VXLAN
5090 i40e_sync_vxlan_filters_subtask(pf);
5091#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005092 i40e_clean_adminq_subtask(pf);
5093
5094 i40e_service_event_complete(pf);
5095
5096 /* If the tasks have taken longer than one timer cycle or there
5097 * is more work to be done, reschedule the service task now
5098 * rather than wait for the timer to tick again.
5099 */
5100 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
5101 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
5102 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
5103 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
5104 i40e_service_event_schedule(pf);
5105}
5106
5107/**
5108 * i40e_service_timer - timer callback
5109 * @data: pointer to PF struct
5110 **/
5111static void i40e_service_timer(unsigned long data)
5112{
5113 struct i40e_pf *pf = (struct i40e_pf *)data;
5114
5115 mod_timer(&pf->service_timer,
5116 round_jiffies(jiffies + pf->service_timer_period));
5117 i40e_service_event_schedule(pf);
5118}
5119
5120/**
5121 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
5122 * @vsi: the VSI being configured
5123 **/
5124static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
5125{
5126 struct i40e_pf *pf = vsi->back;
5127
5128 switch (vsi->type) {
5129 case I40E_VSI_MAIN:
5130 vsi->alloc_queue_pairs = pf->num_lan_qps;
5131 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5132 I40E_REQ_DESCRIPTOR_MULTIPLE);
5133 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5134 vsi->num_q_vectors = pf->num_lan_msix;
5135 else
5136 vsi->num_q_vectors = 1;
5137
5138 break;
5139
5140 case I40E_VSI_FDIR:
5141 vsi->alloc_queue_pairs = 1;
5142 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
5143 I40E_REQ_DESCRIPTOR_MULTIPLE);
5144 vsi->num_q_vectors = 1;
5145 break;
5146
5147 case I40E_VSI_VMDQ2:
5148 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
5149 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5150 I40E_REQ_DESCRIPTOR_MULTIPLE);
5151 vsi->num_q_vectors = pf->num_vmdq_msix;
5152 break;
5153
5154 case I40E_VSI_SRIOV:
5155 vsi->alloc_queue_pairs = pf->num_vf_qps;
5156 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5157 I40E_REQ_DESCRIPTOR_MULTIPLE);
5158 break;
5159
5160 default:
5161 WARN_ON(1);
5162 return -ENODATA;
5163 }
5164
5165 return 0;
5166}
5167
5168/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005169 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
5170 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005171 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005172 *
5173 * On error: returns error code (negative)
5174 * On success: returns 0
5175 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005176static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005177{
5178 int size;
5179 int ret = 0;
5180
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00005181 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005182 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
5183 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
5184 if (!vsi->tx_rings)
5185 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005186 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
5187
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005188 if (alloc_qvectors) {
5189 /* allocate memory for q_vector pointers */
5190 size = sizeof(struct i40e_q_vectors *) * vsi->num_q_vectors;
5191 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
5192 if (!vsi->q_vectors) {
5193 ret = -ENOMEM;
5194 goto err_vectors;
5195 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005196 }
5197 return ret;
5198
5199err_vectors:
5200 kfree(vsi->tx_rings);
5201 return ret;
5202}
5203
5204/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005205 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
5206 * @pf: board private structure
5207 * @type: type of VSI
5208 *
5209 * On error: returns error code (negative)
5210 * On success: returns vsi index in PF (positive)
5211 **/
5212static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
5213{
5214 int ret = -ENODEV;
5215 struct i40e_vsi *vsi;
5216 int vsi_idx;
5217 int i;
5218
5219 /* Need to protect the allocation of the VSIs at the PF level */
5220 mutex_lock(&pf->switch_mutex);
5221
5222 /* VSI list may be fragmented if VSI creation/destruction has
5223 * been happening. We can afford to do a quick scan to look
5224 * for any free VSIs in the list.
5225 *
5226 * find next empty vsi slot, looping back around if necessary
5227 */
5228 i = pf->next_vsi;
5229 while (i < pf->hw.func_caps.num_vsis && pf->vsi[i])
5230 i++;
5231 if (i >= pf->hw.func_caps.num_vsis) {
5232 i = 0;
5233 while (i < pf->next_vsi && pf->vsi[i])
5234 i++;
5235 }
5236
5237 if (i < pf->hw.func_caps.num_vsis && !pf->vsi[i]) {
5238 vsi_idx = i; /* Found one! */
5239 } else {
5240 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00005241 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005242 }
5243 pf->next_vsi = ++i;
5244
5245 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
5246 if (!vsi) {
5247 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00005248 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005249 }
5250 vsi->type = type;
5251 vsi->back = pf;
5252 set_bit(__I40E_DOWN, &vsi->state);
5253 vsi->flags = 0;
5254 vsi->idx = vsi_idx;
5255 vsi->rx_itr_setting = pf->rx_itr_default;
5256 vsi->tx_itr_setting = pf->tx_itr_default;
5257 vsi->netdev_registered = false;
5258 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
5259 INIT_LIST_HEAD(&vsi->mac_filter_list);
5260
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005261 ret = i40e_set_num_rings_in_vsi(vsi);
5262 if (ret)
5263 goto err_rings;
5264
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005265 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005266 if (ret)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005267 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00005268
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005269 /* Setup default MSIX irq handler for VSI */
5270 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
5271
5272 pf->vsi[vsi_idx] = vsi;
5273 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00005274 goto unlock_pf;
5275
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005276err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00005277 pf->next_vsi = i - 1;
5278 kfree(vsi);
5279unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005280 mutex_unlock(&pf->switch_mutex);
5281 return ret;
5282}
5283
5284/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005285 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
5286 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005287 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005288 *
5289 * On error: returns error code (negative)
5290 * On success: returns 0
5291 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005292static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005293{
5294 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005295 if (free_qvectors) {
5296 kfree(vsi->q_vectors);
5297 vsi->q_vectors = NULL;
5298 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005299 kfree(vsi->tx_rings);
5300 vsi->tx_rings = NULL;
5301 vsi->rx_rings = NULL;
5302}
5303
5304/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005305 * i40e_vsi_clear - Deallocate the VSI provided
5306 * @vsi: the VSI being un-configured
5307 **/
5308static int i40e_vsi_clear(struct i40e_vsi *vsi)
5309{
5310 struct i40e_pf *pf;
5311
5312 if (!vsi)
5313 return 0;
5314
5315 if (!vsi->back)
5316 goto free_vsi;
5317 pf = vsi->back;
5318
5319 mutex_lock(&pf->switch_mutex);
5320 if (!pf->vsi[vsi->idx]) {
5321 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
5322 vsi->idx, vsi->idx, vsi, vsi->type);
5323 goto unlock_vsi;
5324 }
5325
5326 if (pf->vsi[vsi->idx] != vsi) {
5327 dev_err(&pf->pdev->dev,
5328 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
5329 pf->vsi[vsi->idx]->idx,
5330 pf->vsi[vsi->idx],
5331 pf->vsi[vsi->idx]->type,
5332 vsi->idx, vsi, vsi->type);
5333 goto unlock_vsi;
5334 }
5335
5336 /* updates the pf for this cleared vsi */
5337 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
5338 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
5339
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005340 i40e_vsi_free_arrays(vsi, true);
Alexander Duyck493fb302013-09-28 07:01:44 +00005341
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005342 pf->vsi[vsi->idx] = NULL;
5343 if (vsi->idx < pf->next_vsi)
5344 pf->next_vsi = vsi->idx;
5345
5346unlock_vsi:
5347 mutex_unlock(&pf->switch_mutex);
5348free_vsi:
5349 kfree(vsi);
5350
5351 return 0;
5352}
5353
5354/**
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005355 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
5356 * @vsi: the VSI being cleaned
5357 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00005358static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005359{
5360 int i;
5361
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00005362 if (vsi->tx_rings[0]) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00005363 for (i = 0; i < vsi->num_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00005364 kfree_rcu(vsi->tx_rings[i], rcu);
5365 vsi->tx_rings[i] = NULL;
5366 vsi->rx_rings[i] = NULL;
5367 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00005368 }
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005369}
5370
5371/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005372 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
5373 * @vsi: the VSI being configured
5374 **/
5375static int i40e_alloc_rings(struct i40e_vsi *vsi)
5376{
5377 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005378 int i;
5379
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005380 /* Set basic values in the rings to be used later during open() */
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00005381 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005382 struct i40e_ring *tx_ring;
5383 struct i40e_ring *rx_ring;
5384
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00005385 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005386 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
5387 if (!tx_ring)
5388 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005389
5390 tx_ring->queue_index = i;
5391 tx_ring->reg_idx = vsi->base_queue + i;
5392 tx_ring->ring_active = false;
5393 tx_ring->vsi = vsi;
5394 tx_ring->netdev = vsi->netdev;
5395 tx_ring->dev = &pf->pdev->dev;
5396 tx_ring->count = vsi->num_desc;
5397 tx_ring->size = 0;
5398 tx_ring->dcb_tc = 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005399 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005400
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005401 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005402 rx_ring->queue_index = i;
5403 rx_ring->reg_idx = vsi->base_queue + i;
5404 rx_ring->ring_active = false;
5405 rx_ring->vsi = vsi;
5406 rx_ring->netdev = vsi->netdev;
5407 rx_ring->dev = &pf->pdev->dev;
5408 rx_ring->count = vsi->num_desc;
5409 rx_ring->size = 0;
5410 rx_ring->dcb_tc = 0;
5411 if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
5412 set_ring_16byte_desc_enabled(rx_ring);
5413 else
5414 clear_ring_16byte_desc_enabled(rx_ring);
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005415 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005416 }
5417
5418 return 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005419
5420err_out:
5421 i40e_vsi_clear_rings(vsi);
5422 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005423}
5424
5425/**
5426 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
5427 * @pf: board private structure
5428 * @vectors: the number of MSI-X vectors to request
5429 *
5430 * Returns the number of vectors reserved, or error
5431 **/
5432static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
5433{
5434 int err = 0;
5435
5436 pf->num_msix_entries = 0;
5437 while (vectors >= I40E_MIN_MSIX) {
5438 err = pci_enable_msix(pf->pdev, pf->msix_entries, vectors);
5439 if (err == 0) {
5440 /* good to go */
5441 pf->num_msix_entries = vectors;
5442 break;
5443 } else if (err < 0) {
5444 /* total failure */
5445 dev_info(&pf->pdev->dev,
5446 "MSI-X vector reservation failed: %d\n", err);
5447 vectors = 0;
5448 break;
5449 } else {
5450 /* err > 0 is the hint for retry */
5451 dev_info(&pf->pdev->dev,
5452 "MSI-X vectors wanted %d, retrying with %d\n",
5453 vectors, err);
5454 vectors = err;
5455 }
5456 }
5457
5458 if (vectors > 0 && vectors < I40E_MIN_MSIX) {
5459 dev_info(&pf->pdev->dev,
5460 "Couldn't get enough vectors, only %d available\n",
5461 vectors);
5462 vectors = 0;
5463 }
5464
5465 return vectors;
5466}
5467
5468/**
5469 * i40e_init_msix - Setup the MSIX capability
5470 * @pf: board private structure
5471 *
5472 * Work with the OS to set up the MSIX vectors needed.
5473 *
5474 * Returns 0 on success, negative on failure
5475 **/
5476static int i40e_init_msix(struct i40e_pf *pf)
5477{
5478 i40e_status err = 0;
5479 struct i40e_hw *hw = &pf->hw;
5480 int v_budget, i;
5481 int vec;
5482
5483 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
5484 return -ENODEV;
5485
5486 /* The number of vectors we'll request will be comprised of:
5487 * - Add 1 for "other" cause for Admin Queue events, etc.
5488 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00005489 * - Queues being used for RSS.
5490 * We don't need as many as max_rss_size vectors.
5491 * use rss_size instead in the calculation since that
5492 * is governed by number of cpus in the system.
5493 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005494 * - The number of VMDq pairs
5495 * Once we count this up, try the request.
5496 *
5497 * If we can't get what we want, we'll simplify to nearly nothing
5498 * and try again. If that still fails, we punt.
5499 */
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00005500 pf->num_lan_msix = pf->num_lan_qps - (pf->rss_size_max - pf->rss_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005501 pf->num_vmdq_msix = pf->num_vmdq_qps;
5502 v_budget = 1 + pf->num_lan_msix;
5503 v_budget += (pf->num_vmdq_vsis * pf->num_vmdq_msix);
5504 if (pf->flags & I40E_FLAG_FDIR_ENABLED)
5505 v_budget++;
5506
5507 /* Scale down if necessary, and the rings will share vectors */
5508 v_budget = min_t(int, v_budget, hw->func_caps.num_msix_vectors);
5509
5510 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
5511 GFP_KERNEL);
5512 if (!pf->msix_entries)
5513 return -ENOMEM;
5514
5515 for (i = 0; i < v_budget; i++)
5516 pf->msix_entries[i].entry = i;
5517 vec = i40e_reserve_msix_vectors(pf, v_budget);
5518 if (vec < I40E_MIN_MSIX) {
5519 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
5520 kfree(pf->msix_entries);
5521 pf->msix_entries = NULL;
5522 return -ENODEV;
5523
5524 } else if (vec == I40E_MIN_MSIX) {
5525 /* Adjust for minimal MSIX use */
5526 dev_info(&pf->pdev->dev, "Features disabled, not enough MSIX vectors\n");
5527 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
5528 pf->num_vmdq_vsis = 0;
5529 pf->num_vmdq_qps = 0;
5530 pf->num_vmdq_msix = 0;
5531 pf->num_lan_qps = 1;
5532 pf->num_lan_msix = 1;
5533
5534 } else if (vec != v_budget) {
5535 /* Scale vector usage down */
5536 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
5537 vec--; /* reserve the misc vector */
5538
5539 /* partition out the remaining vectors */
5540 switch (vec) {
5541 case 2:
5542 pf->num_vmdq_vsis = 1;
5543 pf->num_lan_msix = 1;
5544 break;
5545 case 3:
5546 pf->num_vmdq_vsis = 1;
5547 pf->num_lan_msix = 2;
5548 break;
5549 default:
5550 pf->num_lan_msix = min_t(int, (vec / 2),
5551 pf->num_lan_qps);
5552 pf->num_vmdq_vsis = min_t(int, (vec - pf->num_lan_msix),
5553 I40E_DEFAULT_NUM_VMDQ_VSI);
5554 break;
5555 }
5556 }
5557
5558 return err;
5559}
5560
5561/**
Alexander Duyck493fb302013-09-28 07:01:44 +00005562 * i40e_alloc_q_vector - Allocate memory for a single interrupt vector
5563 * @vsi: the VSI being configured
5564 * @v_idx: index of the vector in the vsi struct
5565 *
5566 * We allocate one q_vector. If allocation fails we return -ENOMEM.
5567 **/
5568static int i40e_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
5569{
5570 struct i40e_q_vector *q_vector;
5571
5572 /* allocate q_vector */
5573 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
5574 if (!q_vector)
5575 return -ENOMEM;
5576
5577 q_vector->vsi = vsi;
5578 q_vector->v_idx = v_idx;
5579 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
5580 if (vsi->netdev)
5581 netif_napi_add(vsi->netdev, &q_vector->napi,
5582 i40e_napi_poll, vsi->work_limit);
5583
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00005584 q_vector->rx.latency_range = I40E_LOW_LATENCY;
5585 q_vector->tx.latency_range = I40E_LOW_LATENCY;
5586
Alexander Duyck493fb302013-09-28 07:01:44 +00005587 /* tie q_vector and vsi together */
5588 vsi->q_vectors[v_idx] = q_vector;
5589
5590 return 0;
5591}
5592
5593/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005594 * i40e_alloc_q_vectors - Allocate memory for interrupt vectors
5595 * @vsi: the VSI being configured
5596 *
5597 * We allocate one q_vector per queue interrupt. If allocation fails we
5598 * return -ENOMEM.
5599 **/
5600static int i40e_alloc_q_vectors(struct i40e_vsi *vsi)
5601{
5602 struct i40e_pf *pf = vsi->back;
5603 int v_idx, num_q_vectors;
Alexander Duyck493fb302013-09-28 07:01:44 +00005604 int err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005605
5606 /* if not MSIX, give the one vector only to the LAN VSI */
5607 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5608 num_q_vectors = vsi->num_q_vectors;
5609 else if (vsi == pf->vsi[pf->lan_vsi])
5610 num_q_vectors = 1;
5611 else
5612 return -EINVAL;
5613
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005614 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00005615 err = i40e_alloc_q_vector(vsi, v_idx);
5616 if (err)
5617 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005618 }
5619
5620 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00005621
5622err_out:
5623 while (v_idx--)
5624 i40e_free_q_vector(vsi, v_idx);
5625
5626 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005627}
5628
5629/**
5630 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
5631 * @pf: board private structure to initialize
5632 **/
5633static void i40e_init_interrupt_scheme(struct i40e_pf *pf)
5634{
5635 int err = 0;
5636
5637 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
5638 err = i40e_init_msix(pf);
5639 if (err) {
Shannon Nelson958a3e32013-09-28 07:13:28 +00005640 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
5641 I40E_FLAG_RSS_ENABLED |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005642 I40E_FLAG_MQ_ENABLED |
5643 I40E_FLAG_DCB_ENABLED |
5644 I40E_FLAG_SRIOV_ENABLED |
5645 I40E_FLAG_FDIR_ENABLED |
5646 I40E_FLAG_FDIR_ATR_ENABLED |
5647 I40E_FLAG_VMDQ_ENABLED);
5648
5649 /* rework the queue expectations without MSIX */
5650 i40e_determine_queue_usage(pf);
5651 }
5652 }
5653
5654 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
5655 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Shannon Nelson958a3e32013-09-28 07:13:28 +00005656 dev_info(&pf->pdev->dev, "MSIX not available, trying MSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005657 err = pci_enable_msi(pf->pdev);
5658 if (err) {
Shannon Nelson958a3e32013-09-28 07:13:28 +00005659 dev_info(&pf->pdev->dev, "MSI init failed - %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005660 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
5661 }
5662 }
5663
Shannon Nelson958a3e32013-09-28 07:13:28 +00005664 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
5665 dev_info(&pf->pdev->dev, "MSIX and MSI not available, falling back to Legacy IRQ\n");
5666
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005667 /* track first vector for misc interrupts */
5668 err = i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT-1);
5669}
5670
5671/**
5672 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
5673 * @pf: board private structure
5674 *
5675 * This sets up the handler for MSIX 0, which is used to manage the
5676 * non-queue interrupts, e.g. AdminQ and errors. This is not used
5677 * when in MSI or Legacy interrupt mode.
5678 **/
5679static int i40e_setup_misc_vector(struct i40e_pf *pf)
5680{
5681 struct i40e_hw *hw = &pf->hw;
5682 int err = 0;
5683
5684 /* Only request the irq if this is the first time through, and
5685 * not when we're rebuilding after a Reset
5686 */
5687 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
5688 err = request_irq(pf->msix_entries[0].vector,
5689 i40e_intr, 0, pf->misc_int_name, pf);
5690 if (err) {
5691 dev_info(&pf->pdev->dev,
5692 "request_irq for msix_misc failed: %d\n", err);
5693 return -EFAULT;
5694 }
5695 }
5696
5697 i40e_enable_misc_int_causes(hw);
5698
5699 /* associate no queues to the misc vector */
5700 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
5701 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
5702
5703 i40e_flush(hw);
5704
5705 i40e_irq_dynamic_enable_icr0(pf);
5706
5707 return err;
5708}
5709
5710/**
5711 * i40e_config_rss - Prepare for RSS if used
5712 * @pf: board private structure
5713 **/
5714static int i40e_config_rss(struct i40e_pf *pf)
5715{
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00005716 const u64 default_hena =
5717 ((u64)1 << I40E_FILTER_PCTYPE_NONF_UNICAST_IPV4_UDP) |
5718 ((u64)1 << I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV4_UDP) |
5719 ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_UDP) |
5720 ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_SCTP) |
5721 ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_TCP_SYN) |
5722 ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_TCP) |
5723 ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_OTHER) |
5724 ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV4) |
5725 ((u64)1 << I40E_FILTER_PCTYPE_NONF_UNICAST_IPV6_UDP) |
5726 ((u64)1 << I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV6_UDP) |
5727 ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_UDP) |
5728 ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_TCP_SYN) |
5729 ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_TCP) |
5730 ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_SCTP) |
5731 ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_OTHER) |
5732 ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV6) |
5733 ((u64)1 << I40E_FILTER_PCTYPE_L2_PAYLOAD);
5734
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005735 /* Set of random keys generated using kernel random number generator */
5736 static const u32 seed[I40E_PFQF_HKEY_MAX_INDEX + 1] = {0x41b01687,
5737 0x183cfd8c, 0xce880440, 0x580cbc3c, 0x35897377,
5738 0x328b25e1, 0x4fa98922, 0xb7d90c14, 0xd5bad70d,
5739 0xcd15a2c1, 0xe8580225, 0x4a1e9d11, 0xfe5731be};
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00005740 struct i40e_hw *hw = &pf->hw;
5741 u32 lut = 0;
5742 int i, j;
5743 u64 hena;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005744
5745 /* Fill out hash function seed */
5746 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
5747 wr32(hw, I40E_PFQF_HKEY(i), seed[i]);
5748
5749 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
5750 hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
5751 ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00005752 hena |= default_hena;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005753 wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
5754 wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
5755
5756 /* Populate the LUT with max no. of queues in round robin fashion */
5757 for (i = 0, j = 0; i < pf->hw.func_caps.rss_table_size; i++, j++) {
5758
5759 /* The assumption is that lan qp count will be the highest
5760 * qp count for any PF VSI that needs RSS.
5761 * If multiple VSIs need RSS support, all the qp counts
5762 * for those VSIs should be a power of 2 for RSS to work.
5763 * If LAN VSI is the only consumer for RSS then this requirement
5764 * is not necessary.
5765 */
5766 if (j == pf->rss_size)
5767 j = 0;
5768 /* lut = 4-byte sliding window of 4 lut entries */
5769 lut = (lut << 8) | (j &
5770 ((0x1 << pf->hw.func_caps.rss_table_entry_width) - 1));
5771 /* On i = 3, we have 4 entries in lut; write to the register */
5772 if ((i & 3) == 3)
5773 wr32(hw, I40E_PFQF_HLUT(i >> 2), lut);
5774 }
5775 i40e_flush(hw);
5776
5777 return 0;
5778}
5779
5780/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00005781 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
5782 * @pf: board private structure
5783 * @queue_count: the requested queue count for rss.
5784 *
5785 * returns 0 if rss is not enabled, if enabled returns the final rss queue
5786 * count which may be different from the requested queue count.
5787 **/
5788int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
5789{
5790 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
5791 return 0;
5792
5793 queue_count = min_t(int, queue_count, pf->rss_size_max);
5794 queue_count = rounddown_pow_of_two(queue_count);
5795
5796 if (queue_count != pf->rss_size) {
5797 if (pf->queues_left < (queue_count - pf->rss_size)) {
5798 dev_info(&pf->pdev->dev,
5799 "Not enough queues to do RSS on %d queues: remaining queues %d\n",
5800 queue_count, pf->queues_left);
5801 return pf->rss_size;
5802 }
5803 i40e_prep_for_reset(pf);
5804
5805 pf->num_lan_qps += (queue_count - pf->rss_size);
5806 pf->queues_left -= (queue_count - pf->rss_size);
5807 pf->rss_size = queue_count;
5808
5809 i40e_reset_and_rebuild(pf, true);
5810 i40e_config_rss(pf);
5811 }
5812 dev_info(&pf->pdev->dev, "RSS count: %d\n", pf->rss_size);
5813 return pf->rss_size;
5814}
5815
5816/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005817 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
5818 * @pf: board private structure to initialize
5819 *
5820 * i40e_sw_init initializes the Adapter private data structure.
5821 * Fields are initialized based on PCI device information and
5822 * OS network device settings (MTU size).
5823 **/
5824static int i40e_sw_init(struct i40e_pf *pf)
5825{
5826 int err = 0;
5827 int size;
5828
5829 pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
5830 (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
Shannon Nelson27599972013-11-16 10:00:43 +00005831 pf->hw.debug_mask = pf->msg_enable | I40E_DEBUG_DIAG;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005832 if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
5833 if (I40E_DEBUG_USER & debug)
5834 pf->hw.debug_mask = debug;
5835 pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
5836 I40E_DEFAULT_MSG_ENABLE);
5837 }
5838
5839 /* Set default capability flags */
5840 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
5841 I40E_FLAG_MSI_ENABLED |
5842 I40E_FLAG_MSIX_ENABLED |
5843 I40E_FLAG_RX_PS_ENABLED |
5844 I40E_FLAG_MQ_ENABLED |
5845 I40E_FLAG_RX_1BUF_ENABLED;
5846
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00005847 /* Depending on PF configurations, it is possible that the RSS
5848 * maximum might end up larger than the available queues
5849 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005850 pf->rss_size_max = 0x1 << pf->hw.func_caps.rss_table_entry_width;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00005851 pf->rss_size_max = min_t(int, pf->rss_size_max,
5852 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005853 if (pf->hw.func_caps.rss) {
5854 pf->flags |= I40E_FLAG_RSS_ENABLED;
Jesse Brandeburgbf051a32013-11-26 10:49:17 +00005855 pf->rss_size = min_t(int, pf->rss_size_max, num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005856 } else {
5857 pf->rss_size = 1;
5858 }
5859
5860 if (pf->hw.func_caps.dcb)
5861 pf->num_tc_qps = I40E_DEFAULT_QUEUES_PER_TC;
5862 else
5863 pf->num_tc_qps = 0;
5864
5865 if (pf->hw.func_caps.fd) {
5866 /* FW/NVM is not yet fixed in this regard */
5867 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
5868 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
5869 pf->flags |= I40E_FLAG_FDIR_ATR_ENABLED;
5870 dev_info(&pf->pdev->dev,
5871 "Flow Director ATR mode Enabled\n");
5872 pf->flags |= I40E_FLAG_FDIR_ENABLED;
5873 dev_info(&pf->pdev->dev,
5874 "Flow Director Side Band mode Enabled\n");
5875 pf->fdir_pf_filter_count =
5876 pf->hw.func_caps.fd_filters_guaranteed;
5877 }
5878 } else {
5879 pf->fdir_pf_filter_count = 0;
5880 }
5881
5882 if (pf->hw.func_caps.vmdq) {
5883 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
5884 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
5885 pf->num_vmdq_qps = I40E_DEFAULT_QUEUES_PER_VMDQ;
5886 }
5887
5888 /* MFP mode enabled */
5889 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.mfp_mode_1) {
5890 pf->flags |= I40E_FLAG_MFP_ENABLED;
5891 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
5892 }
5893
5894#ifdef CONFIG_PCI_IOV
5895 if (pf->hw.func_caps.num_vfs) {
5896 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
5897 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
5898 pf->num_req_vfs = min_t(int,
5899 pf->hw.func_caps.num_vfs,
5900 I40E_MAX_VF_COUNT);
Anjali Singhai Jain4a38d092013-11-20 10:03:00 +00005901 dev_info(&pf->pdev->dev,
5902 "Number of VFs being requested for PF[%d] = %d\n",
5903 pf->hw.pf_id, pf->num_req_vfs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005904 }
5905#endif /* CONFIG_PCI_IOV */
5906 pf->eeprom_version = 0xDEAD;
5907 pf->lan_veb = I40E_NO_VEB;
5908 pf->lan_vsi = I40E_NO_VSI;
5909
5910 /* set up queue assignment tracking */
5911 size = sizeof(struct i40e_lump_tracking)
5912 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
5913 pf->qp_pile = kzalloc(size, GFP_KERNEL);
5914 if (!pf->qp_pile) {
5915 err = -ENOMEM;
5916 goto sw_init_done;
5917 }
5918 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
5919 pf->qp_pile->search_hint = 0;
5920
5921 /* set up vector assignment tracking */
5922 size = sizeof(struct i40e_lump_tracking)
5923 + (sizeof(u16) * pf->hw.func_caps.num_msix_vectors);
5924 pf->irq_pile = kzalloc(size, GFP_KERNEL);
5925 if (!pf->irq_pile) {
5926 kfree(pf->qp_pile);
5927 err = -ENOMEM;
5928 goto sw_init_done;
5929 }
5930 pf->irq_pile->num_entries = pf->hw.func_caps.num_msix_vectors;
5931 pf->irq_pile->search_hint = 0;
5932
5933 mutex_init(&pf->switch_mutex);
5934
5935sw_init_done:
5936 return err;
5937}
5938
5939/**
5940 * i40e_set_features - set the netdev feature flags
5941 * @netdev: ptr to the netdev being adjusted
5942 * @features: the feature set that the stack is suggesting
5943 **/
5944static int i40e_set_features(struct net_device *netdev,
5945 netdev_features_t features)
5946{
5947 struct i40e_netdev_priv *np = netdev_priv(netdev);
5948 struct i40e_vsi *vsi = np->vsi;
5949
5950 if (features & NETIF_F_HW_VLAN_CTAG_RX)
5951 i40e_vlan_stripping_enable(vsi);
5952 else
5953 i40e_vlan_stripping_disable(vsi);
5954
5955 return 0;
5956}
5957
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005958#ifdef CONFIG_I40E_VXLAN
5959/**
5960 * i40e_get_vxlan_port_idx - Lookup a possibly offloaded for Rx UDP port
5961 * @pf: board private structure
5962 * @port: The UDP port to look up
5963 *
5964 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
5965 **/
5966static u8 i40e_get_vxlan_port_idx(struct i40e_pf *pf, __be16 port)
5967{
5968 u8 i;
5969
5970 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
5971 if (pf->vxlan_ports[i] == port)
5972 return i;
5973 }
5974
5975 return i;
5976}
5977
5978/**
5979 * i40e_add_vxlan_port - Get notifications about VXLAN ports that come up
5980 * @netdev: This physical port's netdev
5981 * @sa_family: Socket Family that VXLAN is notifying us about
5982 * @port: New UDP port number that VXLAN started listening to
5983 **/
5984static void i40e_add_vxlan_port(struct net_device *netdev,
5985 sa_family_t sa_family, __be16 port)
5986{
5987 struct i40e_netdev_priv *np = netdev_priv(netdev);
5988 struct i40e_vsi *vsi = np->vsi;
5989 struct i40e_pf *pf = vsi->back;
5990 u8 next_idx;
5991 u8 idx;
5992
5993 if (sa_family == AF_INET6)
5994 return;
5995
5996 idx = i40e_get_vxlan_port_idx(pf, port);
5997
5998 /* Check if port already exists */
5999 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6000 netdev_info(netdev, "Port %d already offloaded\n", ntohs(port));
6001 return;
6002 }
6003
6004 /* Now check if there is space to add the new port */
6005 next_idx = i40e_get_vxlan_port_idx(pf, 0);
6006
6007 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6008 netdev_info(netdev, "Maximum number of UDP ports reached, not adding port %d\n",
6009 ntohs(port));
6010 return;
6011 }
6012
6013 /* New port: add it and mark its index in the bitmap */
6014 pf->vxlan_ports[next_idx] = port;
6015 pf->pending_vxlan_bitmap |= (1 << next_idx);
6016
6017 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
6018}
6019
6020/**
6021 * i40e_del_vxlan_port - Get notifications about VXLAN ports that go away
6022 * @netdev: This physical port's netdev
6023 * @sa_family: Socket Family that VXLAN is notifying us about
6024 * @port: UDP port number that VXLAN stopped listening to
6025 **/
6026static void i40e_del_vxlan_port(struct net_device *netdev,
6027 sa_family_t sa_family, __be16 port)
6028{
6029 struct i40e_netdev_priv *np = netdev_priv(netdev);
6030 struct i40e_vsi *vsi = np->vsi;
6031 struct i40e_pf *pf = vsi->back;
6032 u8 idx;
6033
6034 if (sa_family == AF_INET6)
6035 return;
6036
6037 idx = i40e_get_vxlan_port_idx(pf, port);
6038
6039 /* Check if port already exists */
6040 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6041 /* if port exists, set it to 0 (mark for deletion)
6042 * and make it pending
6043 */
6044 pf->vxlan_ports[idx] = 0;
6045
6046 pf->pending_vxlan_bitmap |= (1 << idx);
6047
6048 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
6049 } else {
6050 netdev_warn(netdev, "Port %d was not found, not deleting\n",
6051 ntohs(port));
6052 }
6053}
6054
6055#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006056static const struct net_device_ops i40e_netdev_ops = {
6057 .ndo_open = i40e_open,
6058 .ndo_stop = i40e_close,
6059 .ndo_start_xmit = i40e_lan_xmit_frame,
6060 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
6061 .ndo_set_rx_mode = i40e_set_rx_mode,
6062 .ndo_validate_addr = eth_validate_addr,
6063 .ndo_set_mac_address = i40e_set_mac,
6064 .ndo_change_mtu = i40e_change_mtu,
6065 .ndo_tx_timeout = i40e_tx_timeout,
6066 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
6067 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
6068#ifdef CONFIG_NET_POLL_CONTROLLER
6069 .ndo_poll_controller = i40e_netpoll,
6070#endif
6071 .ndo_setup_tc = i40e_setup_tc,
6072 .ndo_set_features = i40e_set_features,
6073 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
6074 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
6075 .ndo_set_vf_tx_rate = i40e_ndo_set_vf_bw,
6076 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006077#ifdef CONFIG_I40E_VXLAN
6078 .ndo_add_vxlan_port = i40e_add_vxlan_port,
6079 .ndo_del_vxlan_port = i40e_del_vxlan_port,
6080#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006081};
6082
6083/**
6084 * i40e_config_netdev - Setup the netdev flags
6085 * @vsi: the VSI being configured
6086 *
6087 * Returns 0 on success, negative value on failure
6088 **/
6089static int i40e_config_netdev(struct i40e_vsi *vsi)
6090{
6091 struct i40e_pf *pf = vsi->back;
6092 struct i40e_hw *hw = &pf->hw;
6093 struct i40e_netdev_priv *np;
6094 struct net_device *netdev;
6095 u8 mac_addr[ETH_ALEN];
6096 int etherdev_size;
6097
6098 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006099 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006100 if (!netdev)
6101 return -ENOMEM;
6102
6103 vsi->netdev = netdev;
6104 np = netdev_priv(netdev);
6105 np->vsi = vsi;
6106
6107 netdev->hw_enc_features = NETIF_F_IP_CSUM |
6108 NETIF_F_GSO_UDP_TUNNEL |
6109 NETIF_F_TSO |
6110 NETIF_F_SG;
6111
6112 netdev->features = NETIF_F_SG |
6113 NETIF_F_IP_CSUM |
6114 NETIF_F_SCTP_CSUM |
6115 NETIF_F_HIGHDMA |
6116 NETIF_F_GSO_UDP_TUNNEL |
6117 NETIF_F_HW_VLAN_CTAG_TX |
6118 NETIF_F_HW_VLAN_CTAG_RX |
6119 NETIF_F_HW_VLAN_CTAG_FILTER |
6120 NETIF_F_IPV6_CSUM |
6121 NETIF_F_TSO |
6122 NETIF_F_TSO6 |
6123 NETIF_F_RXCSUM |
6124 NETIF_F_RXHASH |
6125 0;
6126
6127 /* copy netdev features into list of user selectable features */
6128 netdev->hw_features |= netdev->features;
6129
6130 if (vsi->type == I40E_VSI_MAIN) {
6131 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
6132 memcpy(mac_addr, hw->mac.perm_addr, ETH_ALEN);
6133 } else {
6134 /* relate the VSI_VMDQ name to the VSI_MAIN name */
6135 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
6136 pf->vsi[pf->lan_vsi]->netdev->name);
6137 random_ether_addr(mac_addr);
6138 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
6139 }
6140
6141 memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
6142 memcpy(netdev->perm_addr, mac_addr, ETH_ALEN);
6143 /* vlan gets same features (except vlan offload)
6144 * after any tweaks for specific VSI types
6145 */
6146 netdev->vlan_features = netdev->features & ~(NETIF_F_HW_VLAN_CTAG_TX |
6147 NETIF_F_HW_VLAN_CTAG_RX |
6148 NETIF_F_HW_VLAN_CTAG_FILTER);
6149 netdev->priv_flags |= IFF_UNICAST_FLT;
6150 netdev->priv_flags |= IFF_SUPP_NOFCS;
6151 /* Setup netdev TC information */
6152 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
6153
6154 netdev->netdev_ops = &i40e_netdev_ops;
6155 netdev->watchdog_timeo = 5 * HZ;
6156 i40e_set_ethtool_ops(netdev);
6157
6158 return 0;
6159}
6160
6161/**
6162 * i40e_vsi_delete - Delete a VSI from the switch
6163 * @vsi: the VSI being removed
6164 *
6165 * Returns 0 on success, negative value on failure
6166 **/
6167static void i40e_vsi_delete(struct i40e_vsi *vsi)
6168{
6169 /* remove default VSI is not allowed */
6170 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
6171 return;
6172
6173 /* there is no HW VSI for FDIR */
6174 if (vsi->type == I40E_VSI_FDIR)
6175 return;
6176
6177 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
6178 return;
6179}
6180
6181/**
6182 * i40e_add_vsi - Add a VSI to the switch
6183 * @vsi: the VSI being configured
6184 *
6185 * This initializes a VSI context depending on the VSI type to be added and
6186 * passes it down to the add_vsi aq command.
6187 **/
6188static int i40e_add_vsi(struct i40e_vsi *vsi)
6189{
6190 int ret = -ENODEV;
6191 struct i40e_mac_filter *f, *ftmp;
6192 struct i40e_pf *pf = vsi->back;
6193 struct i40e_hw *hw = &pf->hw;
6194 struct i40e_vsi_context ctxt;
6195 u8 enabled_tc = 0x1; /* TC0 enabled */
6196 int f_count = 0;
6197
6198 memset(&ctxt, 0, sizeof(ctxt));
6199 switch (vsi->type) {
6200 case I40E_VSI_MAIN:
6201 /* The PF's main VSI is already setup as part of the
6202 * device initialization, so we'll not bother with
6203 * the add_vsi call, but we will retrieve the current
6204 * VSI context.
6205 */
6206 ctxt.seid = pf->main_vsi_seid;
6207 ctxt.pf_num = pf->hw.pf_id;
6208 ctxt.vf_num = 0;
6209 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6210 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6211 if (ret) {
6212 dev_info(&pf->pdev->dev,
6213 "couldn't get pf vsi config, err %d, aq_err %d\n",
6214 ret, pf->hw.aq.asq_last_status);
6215 return -ENOENT;
6216 }
6217 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
6218 vsi->info.valid_sections = 0;
6219
6220 vsi->seid = ctxt.seid;
6221 vsi->id = ctxt.vsi_number;
6222
6223 enabled_tc = i40e_pf_get_tc_map(pf);
6224
6225 /* MFP mode setup queue map and update VSI */
6226 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
6227 memset(&ctxt, 0, sizeof(ctxt));
6228 ctxt.seid = pf->main_vsi_seid;
6229 ctxt.pf_num = pf->hw.pf_id;
6230 ctxt.vf_num = 0;
6231 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
6232 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
6233 if (ret) {
6234 dev_info(&pf->pdev->dev,
6235 "update vsi failed, aq_err=%d\n",
6236 pf->hw.aq.asq_last_status);
6237 ret = -ENOENT;
6238 goto err;
6239 }
6240 /* update the local VSI info queue map */
6241 i40e_vsi_update_queue_map(vsi, &ctxt);
6242 vsi->info.valid_sections = 0;
6243 } else {
6244 /* Default/Main VSI is only enabled for TC0
6245 * reconfigure it to enable all TCs that are
6246 * available on the port in SFP mode.
6247 */
6248 ret = i40e_vsi_config_tc(vsi, enabled_tc);
6249 if (ret) {
6250 dev_info(&pf->pdev->dev,
6251 "failed to configure TCs for main VSI tc_map 0x%08x, err %d, aq_err %d\n",
6252 enabled_tc, ret,
6253 pf->hw.aq.asq_last_status);
6254 ret = -ENOENT;
6255 }
6256 }
6257 break;
6258
6259 case I40E_VSI_FDIR:
6260 /* no queue mapping or actual HW VSI needed */
6261 vsi->info.valid_sections = 0;
6262 vsi->seid = 0;
6263 vsi->id = 0;
6264 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
6265 return 0;
6266 break;
6267
6268 case I40E_VSI_VMDQ2:
6269 ctxt.pf_num = hw->pf_id;
6270 ctxt.vf_num = 0;
6271 ctxt.uplink_seid = vsi->uplink_seid;
6272 ctxt.connection_type = 0x1; /* regular data port */
6273 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6274
6275 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6276
6277 /* This VSI is connected to VEB so the switch_id
6278 * should be set to zero by default.
6279 */
6280 ctxt.info.switch_id = 0;
6281 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
6282 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6283
6284 /* Setup the VSI tx/rx queue map for TC0 only for now */
6285 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
6286 break;
6287
6288 case I40E_VSI_SRIOV:
6289 ctxt.pf_num = hw->pf_id;
6290 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
6291 ctxt.uplink_seid = vsi->uplink_seid;
6292 ctxt.connection_type = 0x1; /* regular data port */
6293 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
6294
6295 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6296
6297 /* This VSI is connected to VEB so the switch_id
6298 * should be set to zero by default.
6299 */
6300 ctxt.info.switch_id = cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6301
6302 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
6303 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
6304 /* Setup the VSI tx/rx queue map for TC0 only for now */
6305 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
6306 break;
6307
6308 default:
6309 return -ENODEV;
6310 }
6311
6312 if (vsi->type != I40E_VSI_MAIN) {
6313 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6314 if (ret) {
6315 dev_info(&vsi->back->pdev->dev,
6316 "add vsi failed, aq_err=%d\n",
6317 vsi->back->hw.aq.asq_last_status);
6318 ret = -ENOENT;
6319 goto err;
6320 }
6321 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
6322 vsi->info.valid_sections = 0;
6323 vsi->seid = ctxt.seid;
6324 vsi->id = ctxt.vsi_number;
6325 }
6326
6327 /* If macvlan filters already exist, force them to get loaded */
6328 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
6329 f->changed = true;
6330 f_count++;
6331 }
6332 if (f_count) {
6333 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
6334 pf->flags |= I40E_FLAG_FILTER_SYNC;
6335 }
6336
6337 /* Update VSI BW information */
6338 ret = i40e_vsi_get_bw_info(vsi);
6339 if (ret) {
6340 dev_info(&pf->pdev->dev,
6341 "couldn't get vsi bw info, err %d, aq_err %d\n",
6342 ret, pf->hw.aq.asq_last_status);
6343 /* VSI is already added so not tearing that up */
6344 ret = 0;
6345 }
6346
6347err:
6348 return ret;
6349}
6350
6351/**
6352 * i40e_vsi_release - Delete a VSI and free its resources
6353 * @vsi: the VSI being removed
6354 *
6355 * Returns 0 on success or < 0 on error
6356 **/
6357int i40e_vsi_release(struct i40e_vsi *vsi)
6358{
6359 struct i40e_mac_filter *f, *ftmp;
6360 struct i40e_veb *veb = NULL;
6361 struct i40e_pf *pf;
6362 u16 uplink_seid;
6363 int i, n;
6364
6365 pf = vsi->back;
6366
6367 /* release of a VEB-owner or last VSI is not allowed */
6368 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
6369 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
6370 vsi->seid, vsi->uplink_seid);
6371 return -ENODEV;
6372 }
6373 if (vsi == pf->vsi[pf->lan_vsi] &&
6374 !test_bit(__I40E_DOWN, &pf->state)) {
6375 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
6376 return -ENODEV;
6377 }
6378
6379 uplink_seid = vsi->uplink_seid;
6380 if (vsi->type != I40E_VSI_SRIOV) {
6381 if (vsi->netdev_registered) {
6382 vsi->netdev_registered = false;
6383 if (vsi->netdev) {
6384 /* results in a call to i40e_close() */
6385 unregister_netdev(vsi->netdev);
6386 free_netdev(vsi->netdev);
6387 vsi->netdev = NULL;
6388 }
6389 } else {
6390 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
6391 i40e_down(vsi);
6392 i40e_vsi_free_irq(vsi);
6393 i40e_vsi_free_tx_resources(vsi);
6394 i40e_vsi_free_rx_resources(vsi);
6395 }
6396 i40e_vsi_disable_irq(vsi);
6397 }
6398
6399 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
6400 i40e_del_filter(vsi, f->macaddr, f->vlan,
6401 f->is_vf, f->is_netdev);
6402 i40e_sync_vsi_filters(vsi);
6403
6404 i40e_vsi_delete(vsi);
6405 i40e_vsi_free_q_vectors(vsi);
6406 i40e_vsi_clear_rings(vsi);
6407 i40e_vsi_clear(vsi);
6408
6409 /* If this was the last thing on the VEB, except for the
6410 * controlling VSI, remove the VEB, which puts the controlling
6411 * VSI onto the next level down in the switch.
6412 *
6413 * Well, okay, there's one more exception here: don't remove
6414 * the orphan VEBs yet. We'll wait for an explicit remove request
6415 * from up the network stack.
6416 */
6417 for (n = 0, i = 0; i < pf->hw.func_caps.num_vsis; i++) {
6418 if (pf->vsi[i] &&
6419 pf->vsi[i]->uplink_seid == uplink_seid &&
6420 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
6421 n++; /* count the VSIs */
6422 }
6423 }
6424 for (i = 0; i < I40E_MAX_VEB; i++) {
6425 if (!pf->veb[i])
6426 continue;
6427 if (pf->veb[i]->uplink_seid == uplink_seid)
6428 n++; /* count the VEBs */
6429 if (pf->veb[i]->seid == uplink_seid)
6430 veb = pf->veb[i];
6431 }
6432 if (n == 0 && veb && veb->uplink_seid != 0)
6433 i40e_veb_release(veb);
6434
6435 return 0;
6436}
6437
6438/**
6439 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
6440 * @vsi: ptr to the VSI
6441 *
6442 * This should only be called after i40e_vsi_mem_alloc() which allocates the
6443 * corresponding SW VSI structure and initializes num_queue_pairs for the
6444 * newly allocated VSI.
6445 *
6446 * Returns 0 on success or negative on failure
6447 **/
6448static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
6449{
6450 int ret = -ENOENT;
6451 struct i40e_pf *pf = vsi->back;
6452
Alexander Duyck493fb302013-09-28 07:01:44 +00006453 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006454 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
6455 vsi->seid);
6456 return -EEXIST;
6457 }
6458
6459 if (vsi->base_vector) {
6460 dev_info(&pf->pdev->dev,
6461 "VSI %d has non-zero base vector %d\n",
6462 vsi->seid, vsi->base_vector);
6463 return -EEXIST;
6464 }
6465
6466 ret = i40e_alloc_q_vectors(vsi);
6467 if (ret) {
6468 dev_info(&pf->pdev->dev,
6469 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
6470 vsi->num_q_vectors, vsi->seid, ret);
6471 vsi->num_q_vectors = 0;
6472 goto vector_setup_out;
6473 }
6474
Shannon Nelson958a3e32013-09-28 07:13:28 +00006475 if (vsi->num_q_vectors)
6476 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
6477 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006478 if (vsi->base_vector < 0) {
6479 dev_info(&pf->pdev->dev,
6480 "failed to get q tracking for VSI %d, err=%d\n",
6481 vsi->seid, vsi->base_vector);
6482 i40e_vsi_free_q_vectors(vsi);
6483 ret = -ENOENT;
6484 goto vector_setup_out;
6485 }
6486
6487vector_setup_out:
6488 return ret;
6489}
6490
6491/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006492 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
6493 * @vsi: pointer to the vsi.
6494 *
6495 * This re-allocates a vsi's queue resources.
6496 *
6497 * Returns pointer to the successfully allocated and configured VSI sw struct
6498 * on success, otherwise returns NULL on failure.
6499 **/
6500static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
6501{
6502 struct i40e_pf *pf = vsi->back;
6503 u8 enabled_tc;
6504 int ret;
6505
6506 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
6507 i40e_vsi_clear_rings(vsi);
6508
6509 i40e_vsi_free_arrays(vsi, false);
6510 i40e_set_num_rings_in_vsi(vsi);
6511 ret = i40e_vsi_alloc_arrays(vsi, false);
6512 if (ret)
6513 goto err_vsi;
6514
6515 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
6516 if (ret < 0) {
6517 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
6518 vsi->seid, ret);
6519 goto err_vsi;
6520 }
6521 vsi->base_queue = ret;
6522
6523 /* Update the FW view of the VSI. Force a reset of TC and queue
6524 * layout configurations.
6525 */
6526 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
6527 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
6528 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
6529 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
6530
6531 /* assign it some queues */
6532 ret = i40e_alloc_rings(vsi);
6533 if (ret)
6534 goto err_rings;
6535
6536 /* map all of the rings to the q_vectors */
6537 i40e_vsi_map_rings_to_vectors(vsi);
6538 return vsi;
6539
6540err_rings:
6541 i40e_vsi_free_q_vectors(vsi);
6542 if (vsi->netdev_registered) {
6543 vsi->netdev_registered = false;
6544 unregister_netdev(vsi->netdev);
6545 free_netdev(vsi->netdev);
6546 vsi->netdev = NULL;
6547 }
6548 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
6549err_vsi:
6550 i40e_vsi_clear(vsi);
6551 return NULL;
6552}
6553
6554/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006555 * i40e_vsi_setup - Set up a VSI by a given type
6556 * @pf: board private structure
6557 * @type: VSI type
6558 * @uplink_seid: the switch element to link to
6559 * @param1: usage depends upon VSI type. For VF types, indicates VF id
6560 *
6561 * This allocates the sw VSI structure and its queue resources, then add a VSI
6562 * to the identified VEB.
6563 *
6564 * Returns pointer to the successfully allocated and configure VSI sw struct on
6565 * success, otherwise returns NULL on failure.
6566 **/
6567struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
6568 u16 uplink_seid, u32 param1)
6569{
6570 struct i40e_vsi *vsi = NULL;
6571 struct i40e_veb *veb = NULL;
6572 int ret, i;
6573 int v_idx;
6574
6575 /* The requested uplink_seid must be either
6576 * - the PF's port seid
6577 * no VEB is needed because this is the PF
6578 * or this is a Flow Director special case VSI
6579 * - seid of an existing VEB
6580 * - seid of a VSI that owns an existing VEB
6581 * - seid of a VSI that doesn't own a VEB
6582 * a new VEB is created and the VSI becomes the owner
6583 * - seid of the PF VSI, which is what creates the first VEB
6584 * this is a special case of the previous
6585 *
6586 * Find which uplink_seid we were given and create a new VEB if needed
6587 */
6588 for (i = 0; i < I40E_MAX_VEB; i++) {
6589 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
6590 veb = pf->veb[i];
6591 break;
6592 }
6593 }
6594
6595 if (!veb && uplink_seid != pf->mac_seid) {
6596
6597 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
6598 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
6599 vsi = pf->vsi[i];
6600 break;
6601 }
6602 }
6603 if (!vsi) {
6604 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
6605 uplink_seid);
6606 return NULL;
6607 }
6608
6609 if (vsi->uplink_seid == pf->mac_seid)
6610 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
6611 vsi->tc_config.enabled_tc);
6612 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
6613 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
6614 vsi->tc_config.enabled_tc);
6615
6616 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
6617 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
6618 veb = pf->veb[i];
6619 }
6620 if (!veb) {
6621 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
6622 return NULL;
6623 }
6624
6625 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
6626 uplink_seid = veb->seid;
6627 }
6628
6629 /* get vsi sw struct */
6630 v_idx = i40e_vsi_mem_alloc(pf, type);
6631 if (v_idx < 0)
6632 goto err_alloc;
6633 vsi = pf->vsi[v_idx];
6634 vsi->type = type;
6635 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
6636
6637 if (type == I40E_VSI_MAIN)
6638 pf->lan_vsi = v_idx;
6639 else if (type == I40E_VSI_SRIOV)
6640 vsi->vf_id = param1;
6641 /* assign it some queues */
6642 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
6643 if (ret < 0) {
6644 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
6645 vsi->seid, ret);
6646 goto err_vsi;
6647 }
6648 vsi->base_queue = ret;
6649
6650 /* get a VSI from the hardware */
6651 vsi->uplink_seid = uplink_seid;
6652 ret = i40e_add_vsi(vsi);
6653 if (ret)
6654 goto err_vsi;
6655
6656 switch (vsi->type) {
6657 /* setup the netdev if needed */
6658 case I40E_VSI_MAIN:
6659 case I40E_VSI_VMDQ2:
6660 ret = i40e_config_netdev(vsi);
6661 if (ret)
6662 goto err_netdev;
6663 ret = register_netdev(vsi->netdev);
6664 if (ret)
6665 goto err_netdev;
6666 vsi->netdev_registered = true;
6667 netif_carrier_off(vsi->netdev);
6668 /* fall through */
6669
6670 case I40E_VSI_FDIR:
6671 /* set up vectors and rings if needed */
6672 ret = i40e_vsi_setup_vectors(vsi);
6673 if (ret)
6674 goto err_msix;
6675
6676 ret = i40e_alloc_rings(vsi);
6677 if (ret)
6678 goto err_rings;
6679
6680 /* map all of the rings to the q_vectors */
6681 i40e_vsi_map_rings_to_vectors(vsi);
6682
6683 i40e_vsi_reset_stats(vsi);
6684 break;
6685
6686 default:
6687 /* no netdev or rings for the other VSI types */
6688 break;
6689 }
6690
6691 return vsi;
6692
6693err_rings:
6694 i40e_vsi_free_q_vectors(vsi);
6695err_msix:
6696 if (vsi->netdev_registered) {
6697 vsi->netdev_registered = false;
6698 unregister_netdev(vsi->netdev);
6699 free_netdev(vsi->netdev);
6700 vsi->netdev = NULL;
6701 }
6702err_netdev:
6703 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
6704err_vsi:
6705 i40e_vsi_clear(vsi);
6706err_alloc:
6707 return NULL;
6708}
6709
6710/**
6711 * i40e_veb_get_bw_info - Query VEB BW information
6712 * @veb: the veb to query
6713 *
6714 * Query the Tx scheduler BW configuration data for given VEB
6715 **/
6716static int i40e_veb_get_bw_info(struct i40e_veb *veb)
6717{
6718 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
6719 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
6720 struct i40e_pf *pf = veb->pf;
6721 struct i40e_hw *hw = &pf->hw;
6722 u32 tc_bw_max;
6723 int ret = 0;
6724 int i;
6725
6726 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
6727 &bw_data, NULL);
6728 if (ret) {
6729 dev_info(&pf->pdev->dev,
6730 "query veb bw config failed, aq_err=%d\n",
6731 hw->aq.asq_last_status);
6732 goto out;
6733 }
6734
6735 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
6736 &ets_data, NULL);
6737 if (ret) {
6738 dev_info(&pf->pdev->dev,
6739 "query veb bw ets config failed, aq_err=%d\n",
6740 hw->aq.asq_last_status);
6741 goto out;
6742 }
6743
6744 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
6745 veb->bw_max_quanta = ets_data.tc_bw_max;
6746 veb->is_abs_credits = bw_data.absolute_credits_enable;
6747 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
6748 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
6749 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6750 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
6751 veb->bw_tc_limit_credits[i] =
6752 le16_to_cpu(bw_data.tc_bw_limits[i]);
6753 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
6754 }
6755
6756out:
6757 return ret;
6758}
6759
6760/**
6761 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
6762 * @pf: board private structure
6763 *
6764 * On error: returns error code (negative)
6765 * On success: returns vsi index in PF (positive)
6766 **/
6767static int i40e_veb_mem_alloc(struct i40e_pf *pf)
6768{
6769 int ret = -ENOENT;
6770 struct i40e_veb *veb;
6771 int i;
6772
6773 /* Need to protect the allocation of switch elements at the PF level */
6774 mutex_lock(&pf->switch_mutex);
6775
6776 /* VEB list may be fragmented if VEB creation/destruction has
6777 * been happening. We can afford to do a quick scan to look
6778 * for any free slots in the list.
6779 *
6780 * find next empty veb slot, looping back around if necessary
6781 */
6782 i = 0;
6783 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
6784 i++;
6785 if (i >= I40E_MAX_VEB) {
6786 ret = -ENOMEM;
6787 goto err_alloc_veb; /* out of VEB slots! */
6788 }
6789
6790 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
6791 if (!veb) {
6792 ret = -ENOMEM;
6793 goto err_alloc_veb;
6794 }
6795 veb->pf = pf;
6796 veb->idx = i;
6797 veb->enabled_tc = 1;
6798
6799 pf->veb[i] = veb;
6800 ret = i;
6801err_alloc_veb:
6802 mutex_unlock(&pf->switch_mutex);
6803 return ret;
6804}
6805
6806/**
6807 * i40e_switch_branch_release - Delete a branch of the switch tree
6808 * @branch: where to start deleting
6809 *
6810 * This uses recursion to find the tips of the branch to be
6811 * removed, deleting until we get back to and can delete this VEB.
6812 **/
6813static void i40e_switch_branch_release(struct i40e_veb *branch)
6814{
6815 struct i40e_pf *pf = branch->pf;
6816 u16 branch_seid = branch->seid;
6817 u16 veb_idx = branch->idx;
6818 int i;
6819
6820 /* release any VEBs on this VEB - RECURSION */
6821 for (i = 0; i < I40E_MAX_VEB; i++) {
6822 if (!pf->veb[i])
6823 continue;
6824 if (pf->veb[i]->uplink_seid == branch->seid)
6825 i40e_switch_branch_release(pf->veb[i]);
6826 }
6827
6828 /* Release the VSIs on this VEB, but not the owner VSI.
6829 *
6830 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
6831 * the VEB itself, so don't use (*branch) after this loop.
6832 */
6833 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
6834 if (!pf->vsi[i])
6835 continue;
6836 if (pf->vsi[i]->uplink_seid == branch_seid &&
6837 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
6838 i40e_vsi_release(pf->vsi[i]);
6839 }
6840 }
6841
6842 /* There's one corner case where the VEB might not have been
6843 * removed, so double check it here and remove it if needed.
6844 * This case happens if the veb was created from the debugfs
6845 * commands and no VSIs were added to it.
6846 */
6847 if (pf->veb[veb_idx])
6848 i40e_veb_release(pf->veb[veb_idx]);
6849}
6850
6851/**
6852 * i40e_veb_clear - remove veb struct
6853 * @veb: the veb to remove
6854 **/
6855static void i40e_veb_clear(struct i40e_veb *veb)
6856{
6857 if (!veb)
6858 return;
6859
6860 if (veb->pf) {
6861 struct i40e_pf *pf = veb->pf;
6862
6863 mutex_lock(&pf->switch_mutex);
6864 if (pf->veb[veb->idx] == veb)
6865 pf->veb[veb->idx] = NULL;
6866 mutex_unlock(&pf->switch_mutex);
6867 }
6868
6869 kfree(veb);
6870}
6871
6872/**
6873 * i40e_veb_release - Delete a VEB and free its resources
6874 * @veb: the VEB being removed
6875 **/
6876void i40e_veb_release(struct i40e_veb *veb)
6877{
6878 struct i40e_vsi *vsi = NULL;
6879 struct i40e_pf *pf;
6880 int i, n = 0;
6881
6882 pf = veb->pf;
6883
6884 /* find the remaining VSI and check for extras */
6885 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
6886 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
6887 n++;
6888 vsi = pf->vsi[i];
6889 }
6890 }
6891 if (n != 1) {
6892 dev_info(&pf->pdev->dev,
6893 "can't remove VEB %d with %d VSIs left\n",
6894 veb->seid, n);
6895 return;
6896 }
6897
6898 /* move the remaining VSI to uplink veb */
6899 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
6900 if (veb->uplink_seid) {
6901 vsi->uplink_seid = veb->uplink_seid;
6902 if (veb->uplink_seid == pf->mac_seid)
6903 vsi->veb_idx = I40E_NO_VEB;
6904 else
6905 vsi->veb_idx = veb->veb_idx;
6906 } else {
6907 /* floating VEB */
6908 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6909 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
6910 }
6911
6912 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
6913 i40e_veb_clear(veb);
6914
6915 return;
6916}
6917
6918/**
6919 * i40e_add_veb - create the VEB in the switch
6920 * @veb: the VEB to be instantiated
6921 * @vsi: the controlling VSI
6922 **/
6923static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
6924{
6925 bool is_default = (vsi->idx == vsi->back->lan_vsi);
Kevin Scotte1c51b952013-11-20 10:02:51 +00006926 bool is_cloud = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006927 int ret;
6928
6929 /* get a VEB from the hardware */
6930 ret = i40e_aq_add_veb(&veb->pf->hw, veb->uplink_seid, vsi->seid,
Kevin Scotte1c51b952013-11-20 10:02:51 +00006931 veb->enabled_tc, is_default,
6932 is_cloud, &veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006933 if (ret) {
6934 dev_info(&veb->pf->pdev->dev,
6935 "couldn't add VEB, err %d, aq_err %d\n",
6936 ret, veb->pf->hw.aq.asq_last_status);
6937 return -EPERM;
6938 }
6939
6940 /* get statistics counter */
6941 ret = i40e_aq_get_veb_parameters(&veb->pf->hw, veb->seid, NULL, NULL,
6942 &veb->stats_idx, NULL, NULL, NULL);
6943 if (ret) {
6944 dev_info(&veb->pf->pdev->dev,
6945 "couldn't get VEB statistics idx, err %d, aq_err %d\n",
6946 ret, veb->pf->hw.aq.asq_last_status);
6947 return -EPERM;
6948 }
6949 ret = i40e_veb_get_bw_info(veb);
6950 if (ret) {
6951 dev_info(&veb->pf->pdev->dev,
6952 "couldn't get VEB bw info, err %d, aq_err %d\n",
6953 ret, veb->pf->hw.aq.asq_last_status);
6954 i40e_aq_delete_element(&veb->pf->hw, veb->seid, NULL);
6955 return -ENOENT;
6956 }
6957
6958 vsi->uplink_seid = veb->seid;
6959 vsi->veb_idx = veb->idx;
6960 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
6961
6962 return 0;
6963}
6964
6965/**
6966 * i40e_veb_setup - Set up a VEB
6967 * @pf: board private structure
6968 * @flags: VEB setup flags
6969 * @uplink_seid: the switch element to link to
6970 * @vsi_seid: the initial VSI seid
6971 * @enabled_tc: Enabled TC bit-map
6972 *
6973 * This allocates the sw VEB structure and links it into the switch
6974 * It is possible and legal for this to be a duplicate of an already
6975 * existing VEB. It is also possible for both uplink and vsi seids
6976 * to be zero, in order to create a floating VEB.
6977 *
6978 * Returns pointer to the successfully allocated VEB sw struct on
6979 * success, otherwise returns NULL on failure.
6980 **/
6981struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
6982 u16 uplink_seid, u16 vsi_seid,
6983 u8 enabled_tc)
6984{
6985 struct i40e_veb *veb, *uplink_veb = NULL;
6986 int vsi_idx, veb_idx;
6987 int ret;
6988
6989 /* if one seid is 0, the other must be 0 to create a floating relay */
6990 if ((uplink_seid == 0 || vsi_seid == 0) &&
6991 (uplink_seid + vsi_seid != 0)) {
6992 dev_info(&pf->pdev->dev,
6993 "one, not both seid's are 0: uplink=%d vsi=%d\n",
6994 uplink_seid, vsi_seid);
6995 return NULL;
6996 }
6997
6998 /* make sure there is such a vsi and uplink */
6999 for (vsi_idx = 0; vsi_idx < pf->hw.func_caps.num_vsis; vsi_idx++)
7000 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
7001 break;
7002 if (vsi_idx >= pf->hw.func_caps.num_vsis && vsi_seid != 0) {
7003 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
7004 vsi_seid);
7005 return NULL;
7006 }
7007
7008 if (uplink_seid && uplink_seid != pf->mac_seid) {
7009 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
7010 if (pf->veb[veb_idx] &&
7011 pf->veb[veb_idx]->seid == uplink_seid) {
7012 uplink_veb = pf->veb[veb_idx];
7013 break;
7014 }
7015 }
7016 if (!uplink_veb) {
7017 dev_info(&pf->pdev->dev,
7018 "uplink seid %d not found\n", uplink_seid);
7019 return NULL;
7020 }
7021 }
7022
7023 /* get veb sw struct */
7024 veb_idx = i40e_veb_mem_alloc(pf);
7025 if (veb_idx < 0)
7026 goto err_alloc;
7027 veb = pf->veb[veb_idx];
7028 veb->flags = flags;
7029 veb->uplink_seid = uplink_seid;
7030 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
7031 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
7032
7033 /* create the VEB in the switch */
7034 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
7035 if (ret)
7036 goto err_veb;
7037
7038 return veb;
7039
7040err_veb:
7041 i40e_veb_clear(veb);
7042err_alloc:
7043 return NULL;
7044}
7045
7046/**
7047 * i40e_setup_pf_switch_element - set pf vars based on switch type
7048 * @pf: board private structure
7049 * @ele: element we are building info from
7050 * @num_reported: total number of elements
7051 * @printconfig: should we print the contents
7052 *
7053 * helper function to assist in extracting a few useful SEID values.
7054 **/
7055static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
7056 struct i40e_aqc_switch_config_element_resp *ele,
7057 u16 num_reported, bool printconfig)
7058{
7059 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
7060 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
7061 u8 element_type = ele->element_type;
7062 u16 seid = le16_to_cpu(ele->seid);
7063
7064 if (printconfig)
7065 dev_info(&pf->pdev->dev,
7066 "type=%d seid=%d uplink=%d downlink=%d\n",
7067 element_type, seid, uplink_seid, downlink_seid);
7068
7069 switch (element_type) {
7070 case I40E_SWITCH_ELEMENT_TYPE_MAC:
7071 pf->mac_seid = seid;
7072 break;
7073 case I40E_SWITCH_ELEMENT_TYPE_VEB:
7074 /* Main VEB? */
7075 if (uplink_seid != pf->mac_seid)
7076 break;
7077 if (pf->lan_veb == I40E_NO_VEB) {
7078 int v;
7079
7080 /* find existing or else empty VEB */
7081 for (v = 0; v < I40E_MAX_VEB; v++) {
7082 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
7083 pf->lan_veb = v;
7084 break;
7085 }
7086 }
7087 if (pf->lan_veb == I40E_NO_VEB) {
7088 v = i40e_veb_mem_alloc(pf);
7089 if (v < 0)
7090 break;
7091 pf->lan_veb = v;
7092 }
7093 }
7094
7095 pf->veb[pf->lan_veb]->seid = seid;
7096 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
7097 pf->veb[pf->lan_veb]->pf = pf;
7098 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
7099 break;
7100 case I40E_SWITCH_ELEMENT_TYPE_VSI:
7101 if (num_reported != 1)
7102 break;
7103 /* This is immediately after a reset so we can assume this is
7104 * the PF's VSI
7105 */
7106 pf->mac_seid = uplink_seid;
7107 pf->pf_seid = downlink_seid;
7108 pf->main_vsi_seid = seid;
7109 if (printconfig)
7110 dev_info(&pf->pdev->dev,
7111 "pf_seid=%d main_vsi_seid=%d\n",
7112 pf->pf_seid, pf->main_vsi_seid);
7113 break;
7114 case I40E_SWITCH_ELEMENT_TYPE_PF:
7115 case I40E_SWITCH_ELEMENT_TYPE_VF:
7116 case I40E_SWITCH_ELEMENT_TYPE_EMP:
7117 case I40E_SWITCH_ELEMENT_TYPE_BMC:
7118 case I40E_SWITCH_ELEMENT_TYPE_PE:
7119 case I40E_SWITCH_ELEMENT_TYPE_PA:
7120 /* ignore these for now */
7121 break;
7122 default:
7123 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
7124 element_type, seid);
7125 break;
7126 }
7127}
7128
7129/**
7130 * i40e_fetch_switch_configuration - Get switch config from firmware
7131 * @pf: board private structure
7132 * @printconfig: should we print the contents
7133 *
7134 * Get the current switch configuration from the device and
7135 * extract a few useful SEID values.
7136 **/
7137int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
7138{
7139 struct i40e_aqc_get_switch_config_resp *sw_config;
7140 u16 next_seid = 0;
7141 int ret = 0;
7142 u8 *aq_buf;
7143 int i;
7144
7145 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
7146 if (!aq_buf)
7147 return -ENOMEM;
7148
7149 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
7150 do {
7151 u16 num_reported, num_total;
7152
7153 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
7154 I40E_AQ_LARGE_BUF,
7155 &next_seid, NULL);
7156 if (ret) {
7157 dev_info(&pf->pdev->dev,
7158 "get switch config failed %d aq_err=%x\n",
7159 ret, pf->hw.aq.asq_last_status);
7160 kfree(aq_buf);
7161 return -ENOENT;
7162 }
7163
7164 num_reported = le16_to_cpu(sw_config->header.num_reported);
7165 num_total = le16_to_cpu(sw_config->header.num_total);
7166
7167 if (printconfig)
7168 dev_info(&pf->pdev->dev,
7169 "header: %d reported %d total\n",
7170 num_reported, num_total);
7171
7172 if (num_reported) {
7173 int sz = sizeof(*sw_config) * num_reported;
7174
7175 kfree(pf->sw_config);
7176 pf->sw_config = kzalloc(sz, GFP_KERNEL);
7177 if (pf->sw_config)
7178 memcpy(pf->sw_config, sw_config, sz);
7179 }
7180
7181 for (i = 0; i < num_reported; i++) {
7182 struct i40e_aqc_switch_config_element_resp *ele =
7183 &sw_config->element[i];
7184
7185 i40e_setup_pf_switch_element(pf, ele, num_reported,
7186 printconfig);
7187 }
7188 } while (next_seid != 0);
7189
7190 kfree(aq_buf);
7191 return ret;
7192}
7193
7194/**
7195 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
7196 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007197 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007198 *
7199 * Returns 0 on success, negative value on failure
7200 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007201static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007202{
Jesse Brandeburg895106a2013-11-26 10:49:16 +00007203 u32 rxfc = 0, txfc = 0, rxfc_reg;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007204 int ret;
7205
7206 /* find out what's out there already */
7207 ret = i40e_fetch_switch_configuration(pf, false);
7208 if (ret) {
7209 dev_info(&pf->pdev->dev,
7210 "couldn't fetch switch config, err %d, aq_err %d\n",
7211 ret, pf->hw.aq.asq_last_status);
7212 return ret;
7213 }
7214 i40e_pf_reset_stats(pf);
7215
7216 /* fdir VSI must happen first to be sure it gets queue 0, but only
7217 * if there is enough room for the fdir VSI
7218 */
7219 if (pf->num_lan_qps > 1)
7220 i40e_fdir_setup(pf);
7221
7222 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007223 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007224 struct i40e_vsi *vsi = NULL;
7225 u16 uplink_seid;
7226
7227 /* Set up the PF VSI associated with the PF's main VSI
7228 * that is already in the HW switch
7229 */
7230 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
7231 uplink_seid = pf->veb[pf->lan_veb]->seid;
7232 else
7233 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007234 if (pf->lan_vsi == I40E_NO_VSI)
7235 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
7236 else if (reinit)
7237 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007238 if (!vsi) {
7239 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
7240 i40e_fdir_teardown(pf);
7241 return -EAGAIN;
7242 }
7243 /* accommodate kcompat by copying the main VSI queue count
7244 * into the pf, since this newer code pushes the pf queue
7245 * info down a level into a VSI
7246 */
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007247 pf->num_rx_queues = vsi->num_queue_pairs;
7248 pf->num_tx_queues = vsi->num_queue_pairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007249 } else {
7250 /* force a reset of TC and queue layout configurations */
7251 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
7252 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
7253 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
7254 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
7255 }
7256 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
7257
7258 /* Setup static PF queue filter control settings */
7259 ret = i40e_setup_pf_filter_control(pf);
7260 if (ret) {
7261 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
7262 ret);
7263 /* Failure here should not stop continuing other steps */
7264 }
7265
7266 /* enable RSS in the HW, even for only one queue, as the stack can use
7267 * the hash
7268 */
7269 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
7270 i40e_config_rss(pf);
7271
7272 /* fill in link information and enable LSE reporting */
7273 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
7274 i40e_link_event(pf);
7275
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007276 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007277 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
7278 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007279 /* requested_mode is set in probe or by ethtool */
7280 if (!pf->fc_autoneg_status)
7281 goto no_autoneg;
7282
7283 if ((pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX) &&
7284 (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007285 pf->hw.fc.current_mode = I40E_FC_FULL;
7286 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX)
7287 pf->hw.fc.current_mode = I40E_FC_TX_PAUSE;
7288 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX)
7289 pf->hw.fc.current_mode = I40E_FC_RX_PAUSE;
7290 else
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007291 pf->hw.fc.current_mode = I40E_FC_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007292
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007293 /* sync the flow control settings with the auto-neg values */
7294 switch (pf->hw.fc.current_mode) {
7295 case I40E_FC_FULL:
7296 txfc = 1;
7297 rxfc = 1;
7298 break;
7299 case I40E_FC_TX_PAUSE:
7300 txfc = 1;
7301 rxfc = 0;
7302 break;
7303 case I40E_FC_RX_PAUSE:
7304 txfc = 0;
7305 rxfc = 1;
7306 break;
7307 case I40E_FC_NONE:
7308 case I40E_FC_DEFAULT:
7309 txfc = 0;
7310 rxfc = 0;
7311 break;
7312 case I40E_FC_PFC:
7313 /* TBD */
7314 break;
7315 /* no default case, we have to handle all possibilities here */
7316 }
7317
7318 wr32(&pf->hw, I40E_PRTDCB_FCCFG, txfc << I40E_PRTDCB_FCCFG_TFCE_SHIFT);
7319
7320 rxfc_reg = rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
7321 ~I40E_PRTDCB_MFLCN_RFCE_MASK;
7322 rxfc_reg |= (rxfc << I40E_PRTDCB_MFLCN_RFCE_SHIFT);
7323
7324 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rxfc_reg);
7325
7326 goto fc_complete;
7327
7328no_autoneg:
7329 /* disable L2 flow control, user can turn it on if they wish */
7330 wr32(&pf->hw, I40E_PRTDCB_FCCFG, 0);
7331 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
7332 ~I40E_PRTDCB_MFLCN_RFCE_MASK);
7333
7334fc_complete:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007335 return ret;
7336}
7337
7338/**
7339 * i40e_set_rss_size - helper to set rss_size
7340 * @pf: board private structure
7341 * @queues_left: how many queues
7342 */
7343static u16 i40e_set_rss_size(struct i40e_pf *pf, int queues_left)
7344{
7345 int num_tc0;
7346
7347 num_tc0 = min_t(int, queues_left, pf->rss_size_max);
Jesse Brandeburgbf051a32013-11-26 10:49:17 +00007348 num_tc0 = min_t(int, num_tc0, num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007349 num_tc0 = rounddown_pow_of_two(num_tc0);
7350
7351 return num_tc0;
7352}
7353
7354/**
7355 * i40e_determine_queue_usage - Work out queue distribution
7356 * @pf: board private structure
7357 **/
7358static void i40e_determine_queue_usage(struct i40e_pf *pf)
7359{
7360 int accum_tc_size;
7361 int queues_left;
7362
7363 pf->num_lan_qps = 0;
7364 pf->num_tc_qps = rounddown_pow_of_two(pf->num_tc_qps);
7365 accum_tc_size = (I40E_MAX_TRAFFIC_CLASS - 1) * pf->num_tc_qps;
7366
7367 /* Find the max queues to be put into basic use. We'll always be
7368 * using TC0, whether or not DCB is running, and TC0 will get the
7369 * big RSS set.
7370 */
7371 queues_left = pf->hw.func_caps.num_tx_qp;
7372
7373 if (!((pf->flags & I40E_FLAG_MSIX_ENABLED) &&
7374 (pf->flags & I40E_FLAG_MQ_ENABLED)) ||
7375 !(pf->flags & (I40E_FLAG_RSS_ENABLED |
7376 I40E_FLAG_FDIR_ENABLED | I40E_FLAG_DCB_ENABLED)) ||
7377 (queues_left == 1)) {
7378
7379 /* one qp for PF, no queues for anything else */
7380 queues_left = 0;
7381 pf->rss_size = pf->num_lan_qps = 1;
7382
7383 /* make sure all the fancies are disabled */
7384 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
7385 I40E_FLAG_MQ_ENABLED |
7386 I40E_FLAG_FDIR_ENABLED |
7387 I40E_FLAG_FDIR_ATR_ENABLED |
7388 I40E_FLAG_DCB_ENABLED |
7389 I40E_FLAG_SRIOV_ENABLED |
7390 I40E_FLAG_VMDQ_ENABLED);
7391
7392 } else if (pf->flags & I40E_FLAG_RSS_ENABLED &&
7393 !(pf->flags & I40E_FLAG_FDIR_ENABLED) &&
7394 !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
7395
7396 pf->rss_size = i40e_set_rss_size(pf, queues_left);
7397
7398 queues_left -= pf->rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007399 pf->num_lan_qps = pf->rss_size_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007400
7401 } else if (pf->flags & I40E_FLAG_RSS_ENABLED &&
7402 !(pf->flags & I40E_FLAG_FDIR_ENABLED) &&
7403 (pf->flags & I40E_FLAG_DCB_ENABLED)) {
7404
7405 /* save num_tc_qps queues for TCs 1 thru 7 and the rest
7406 * are set up for RSS in TC0
7407 */
7408 queues_left -= accum_tc_size;
7409
7410 pf->rss_size = i40e_set_rss_size(pf, queues_left);
7411
7412 queues_left -= pf->rss_size;
7413 if (queues_left < 0) {
7414 dev_info(&pf->pdev->dev, "not enough queues for DCB\n");
7415 return;
7416 }
7417
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007418 pf->num_lan_qps = pf->rss_size_max + accum_tc_size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007419
7420 } else if (pf->flags & I40E_FLAG_RSS_ENABLED &&
7421 (pf->flags & I40E_FLAG_FDIR_ENABLED) &&
7422 !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
7423
7424 queues_left -= 1; /* save 1 queue for FD */
7425
7426 pf->rss_size = i40e_set_rss_size(pf, queues_left);
7427
7428 queues_left -= pf->rss_size;
7429 if (queues_left < 0) {
7430 dev_info(&pf->pdev->dev, "not enough queues for Flow Director\n");
7431 return;
7432 }
7433
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007434 pf->num_lan_qps = pf->rss_size_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007435
7436 } else if (pf->flags & I40E_FLAG_RSS_ENABLED &&
7437 (pf->flags & I40E_FLAG_FDIR_ENABLED) &&
7438 (pf->flags & I40E_FLAG_DCB_ENABLED)) {
7439
7440 /* save 1 queue for TCs 1 thru 7,
7441 * 1 queue for flow director,
7442 * and the rest are set up for RSS in TC0
7443 */
7444 queues_left -= 1;
7445 queues_left -= accum_tc_size;
7446
7447 pf->rss_size = i40e_set_rss_size(pf, queues_left);
7448 queues_left -= pf->rss_size;
7449 if (queues_left < 0) {
7450 dev_info(&pf->pdev->dev, "not enough queues for DCB and Flow Director\n");
7451 return;
7452 }
7453
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007454 pf->num_lan_qps = pf->rss_size_max + accum_tc_size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007455
7456 } else {
7457 dev_info(&pf->pdev->dev,
7458 "Invalid configuration, flags=0x%08llx\n", pf->flags);
7459 return;
7460 }
7461
7462 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
7463 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
7464 pf->num_req_vfs = min_t(int, pf->num_req_vfs, (queues_left /
7465 pf->num_vf_qps));
7466 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
7467 }
7468
7469 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7470 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
7471 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
7472 (queues_left / pf->num_vmdq_qps));
7473 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
7474 }
7475
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007476 pf->queues_left = queues_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007477 return;
7478}
7479
7480/**
7481 * i40e_setup_pf_filter_control - Setup PF static filter control
7482 * @pf: PF to be setup
7483 *
7484 * i40e_setup_pf_filter_control sets up a pf's initial filter control
7485 * settings. If PE/FCoE are enabled then it will also set the per PF
7486 * based filter sizes required for them. It also enables Flow director,
7487 * ethertype and macvlan type filter settings for the pf.
7488 *
7489 * Returns 0 on success, negative on failure
7490 **/
7491static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
7492{
7493 struct i40e_filter_control_settings *settings = &pf->filter_settings;
7494
7495 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
7496
7497 /* Flow Director is enabled */
7498 if (pf->flags & (I40E_FLAG_FDIR_ENABLED | I40E_FLAG_FDIR_ATR_ENABLED))
7499 settings->enable_fdir = true;
7500
7501 /* Ethtype and MACVLAN filters enabled for PF */
7502 settings->enable_ethtype = true;
7503 settings->enable_macvlan = true;
7504
7505 if (i40e_set_filter_control(&pf->hw, settings))
7506 return -ENOENT;
7507
7508 return 0;
7509}
7510
7511/**
7512 * i40e_probe - Device initialization routine
7513 * @pdev: PCI device information struct
7514 * @ent: entry in i40e_pci_tbl
7515 *
7516 * i40e_probe initializes a pf identified by a pci_dev structure.
7517 * The OS initialization, configuring of the pf private structure,
7518 * and a hardware reset occur.
7519 *
7520 * Returns 0 on success, negative on failure
7521 **/
7522static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
7523{
7524 struct i40e_driver_version dv;
7525 struct i40e_pf *pf;
7526 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00007527 static u16 pfs_found;
Catherine Sullivand4dfb812013-11-28 06:39:21 +00007528 u16 link_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007529 int err = 0;
7530 u32 len;
7531
7532 err = pci_enable_device_mem(pdev);
7533 if (err)
7534 return err;
7535
7536 /* set up for high or low dma */
7537 if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64))) {
7538 /* coherent mask for the same size will always succeed if
7539 * dma_set_mask does
7540 */
7541 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
7542 } else if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
7543 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
7544 } else {
7545 dev_err(&pdev->dev, "DMA configuration failed: %d\n", err);
7546 err = -EIO;
7547 goto err_dma;
7548 }
7549
7550 /* set up pci connections */
7551 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
7552 IORESOURCE_MEM), i40e_driver_name);
7553 if (err) {
7554 dev_info(&pdev->dev,
7555 "pci_request_selected_regions failed %d\n", err);
7556 goto err_pci_reg;
7557 }
7558
7559 pci_enable_pcie_error_reporting(pdev);
7560 pci_set_master(pdev);
7561
7562 /* Now that we have a PCI connection, we need to do the
7563 * low level device setup. This is primarily setting up
7564 * the Admin Queue structures and then querying for the
7565 * device's current profile information.
7566 */
7567 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
7568 if (!pf) {
7569 err = -ENOMEM;
7570 goto err_pf_alloc;
7571 }
7572 pf->next_vsi = 0;
7573 pf->pdev = pdev;
7574 set_bit(__I40E_DOWN, &pf->state);
7575
7576 hw = &pf->hw;
7577 hw->back = pf;
7578 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
7579 pci_resource_len(pdev, 0));
7580 if (!hw->hw_addr) {
7581 err = -EIO;
7582 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
7583 (unsigned int)pci_resource_start(pdev, 0),
7584 (unsigned int)pci_resource_len(pdev, 0), err);
7585 goto err_ioremap;
7586 }
7587 hw->vendor_id = pdev->vendor;
7588 hw->device_id = pdev->device;
7589 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
7590 hw->subsystem_vendor_id = pdev->subsystem_vendor;
7591 hw->subsystem_device_id = pdev->subsystem_device;
7592 hw->bus.device = PCI_SLOT(pdev->devfn);
7593 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00007594 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007595
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00007596 /* do a special CORER for clearing PXE mode once at init */
7597 if (hw->revision_id == 0 &&
7598 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
7599 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
7600 i40e_flush(hw);
7601 msleep(200);
7602 pf->corer_count++;
7603
7604 i40e_clear_pxe_mode(hw);
7605 }
7606
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007607 /* Reset here to make sure all is clean and to define PF 'n' */
7608 err = i40e_pf_reset(hw);
7609 if (err) {
7610 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
7611 goto err_pf_reset;
7612 }
7613 pf->pfr_count++;
7614
7615 hw->aq.num_arq_entries = I40E_AQ_LEN;
7616 hw->aq.num_asq_entries = I40E_AQ_LEN;
7617 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
7618 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
7619 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
7620 snprintf(pf->misc_int_name, sizeof(pf->misc_int_name) - 1,
7621 "%s-pf%d:misc",
7622 dev_driver_string(&pf->pdev->dev), pf->hw.pf_id);
7623
7624 err = i40e_init_shared_code(hw);
7625 if (err) {
7626 dev_info(&pdev->dev, "init_shared_code failed: %d\n", err);
7627 goto err_pf_reset;
7628 }
7629
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007630 /* set up a default setting for link flow control */
7631 pf->hw.fc.requested_mode = I40E_FC_NONE;
7632
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007633 err = i40e_init_adminq(hw);
7634 dev_info(&pdev->dev, "%s\n", i40e_fw_version_str(hw));
Anjali Singhai jainfe310702013-11-16 10:00:37 +00007635 if (((hw->nvm.version & I40E_NVM_VERSION_HI_MASK)
7636 >> I40E_NVM_VERSION_HI_SHIFT) != I40E_CURRENT_NVM_VERSION_HI) {
7637 dev_info(&pdev->dev,
7638 "warning: NVM version not supported, supported version: %02x.%02x\n",
7639 I40E_CURRENT_NVM_VERSION_HI,
7640 I40E_CURRENT_NVM_VERSION_LO);
7641 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007642 if (err) {
7643 dev_info(&pdev->dev,
7644 "init_adminq failed: %d expecting API %02x.%02x\n",
7645 err,
7646 I40E_FW_API_VERSION_MAJOR, I40E_FW_API_VERSION_MINOR);
7647 goto err_pf_reset;
7648 }
7649
7650 err = i40e_get_capabilities(pf);
7651 if (err)
7652 goto err_adminq_setup;
7653
7654 err = i40e_sw_init(pf);
7655 if (err) {
7656 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
7657 goto err_sw_init;
7658 }
7659
7660 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
7661 hw->func_caps.num_rx_qp,
7662 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
7663 if (err) {
7664 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
7665 goto err_init_lan_hmc;
7666 }
7667
7668 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
7669 if (err) {
7670 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
7671 err = -ENOENT;
7672 goto err_configure_lan_hmc;
7673 }
7674
7675 i40e_get_mac_addr(hw, hw->mac.addr);
7676 if (i40e_validate_mac_addr(hw->mac.addr)) {
7677 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
7678 err = -EIO;
7679 goto err_mac_addr;
7680 }
7681 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
7682 memcpy(hw->mac.perm_addr, hw->mac.addr, ETH_ALEN);
7683
7684 pci_set_drvdata(pdev, pf);
7685 pci_save_state(pdev);
7686
7687 /* set up periodic task facility */
7688 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
7689 pf->service_timer_period = HZ;
7690
7691 INIT_WORK(&pf->service_task, i40e_service_task);
7692 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
7693 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
7694 pf->link_check_timeout = jiffies;
7695
Shannon Nelson8e2773a2013-11-28 06:39:22 +00007696 /* WoL defaults to disabled */
7697 pf->wol_en = false;
7698 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
7699
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007700 /* set up the main switch operations */
7701 i40e_determine_queue_usage(pf);
7702 i40e_init_interrupt_scheme(pf);
7703
7704 /* Set up the *vsi struct based on the number of VSIs in the HW,
7705 * and set up our local tracking of the MAIN PF vsi.
7706 */
7707 len = sizeof(struct i40e_vsi *) * pf->hw.func_caps.num_vsis;
7708 pf->vsi = kzalloc(len, GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +00007709 if (!pf->vsi) {
7710 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007711 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +00007712 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007713
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007714 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007715 if (err) {
7716 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
7717 goto err_vsis;
7718 }
7719
7720 /* The main driver is (mostly) up and happy. We need to set this state
7721 * before setting up the misc vector or we get a race and the vector
7722 * ends up disabled forever.
7723 */
7724 clear_bit(__I40E_DOWN, &pf->state);
7725
7726 /* In case of MSIX we are going to setup the misc vector right here
7727 * to handle admin queue events etc. In case of legacy and MSI
7728 * the misc functionality and queue processing is combined in
7729 * the same vector and that gets setup at open.
7730 */
7731 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
7732 err = i40e_setup_misc_vector(pf);
7733 if (err) {
7734 dev_info(&pdev->dev,
7735 "setup of misc vector failed: %d\n", err);
7736 goto err_vsis;
7737 }
7738 }
7739
7740 /* prep for VF support */
7741 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
7742 (pf->flags & I40E_FLAG_MSIX_ENABLED)) {
7743 u32 val;
7744
7745 /* disable link interrupts for VFs */
7746 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
7747 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
7748 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
7749 i40e_flush(hw);
7750 }
7751
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00007752 pfs_found++;
7753
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007754 i40e_dbg_pf_init(pf);
7755
7756 /* tell the firmware that we're starting */
7757 dv.major_version = DRV_VERSION_MAJOR;
7758 dv.minor_version = DRV_VERSION_MINOR;
7759 dv.build_version = DRV_VERSION_BUILD;
7760 dv.subbuild_version = 0;
7761 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
7762
7763 /* since everything's happy, start the service_task timer */
7764 mod_timer(&pf->service_timer,
7765 round_jiffies(jiffies + pf->service_timer_period));
7766
Catherine Sullivand4dfb812013-11-28 06:39:21 +00007767 /* Get the negotiated link width and speed from PCI config space */
7768 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA, &link_status);
7769
7770 i40e_set_pci_config_data(hw, link_status);
7771
7772 dev_info(&pdev->dev, "PCI Express: %s %s\n",
7773 (hw->bus.speed == i40e_bus_speed_8000 ? "Speed 8.0GT/s" :
7774 hw->bus.speed == i40e_bus_speed_5000 ? "Speed 5.0GT/s" :
7775 hw->bus.speed == i40e_bus_speed_2500 ? "Speed 2.5GT/s" :
7776 "Unknown"),
7777 (hw->bus.width == i40e_bus_width_pcie_x8 ? "Width x8" :
7778 hw->bus.width == i40e_bus_width_pcie_x4 ? "Width x4" :
7779 hw->bus.width == i40e_bus_width_pcie_x2 ? "Width x2" :
7780 hw->bus.width == i40e_bus_width_pcie_x1 ? "Width x1" :
7781 "Unknown"));
7782
7783 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
7784 hw->bus.speed < i40e_bus_speed_8000) {
7785 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
7786 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
7787 }
7788
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007789 return 0;
7790
7791 /* Unwind what we've done if something failed in the setup */
7792err_vsis:
7793 set_bit(__I40E_DOWN, &pf->state);
7794err_switch_setup:
7795 i40e_clear_interrupt_scheme(pf);
7796 kfree(pf->vsi);
7797 del_timer_sync(&pf->service_timer);
7798err_mac_addr:
7799err_configure_lan_hmc:
7800 (void)i40e_shutdown_lan_hmc(hw);
7801err_init_lan_hmc:
7802 kfree(pf->qp_pile);
7803 kfree(pf->irq_pile);
7804err_sw_init:
7805err_adminq_setup:
7806 (void)i40e_shutdown_adminq(hw);
7807err_pf_reset:
7808 iounmap(hw->hw_addr);
7809err_ioremap:
7810 kfree(pf);
7811err_pf_alloc:
7812 pci_disable_pcie_error_reporting(pdev);
7813 pci_release_selected_regions(pdev,
7814 pci_select_bars(pdev, IORESOURCE_MEM));
7815err_pci_reg:
7816err_dma:
7817 pci_disable_device(pdev);
7818 return err;
7819}
7820
7821/**
7822 * i40e_remove - Device removal routine
7823 * @pdev: PCI device information struct
7824 *
7825 * i40e_remove is called by the PCI subsystem to alert the driver
7826 * that is should release a PCI device. This could be caused by a
7827 * Hot-Plug event, or because the driver is going to be removed from
7828 * memory.
7829 **/
7830static void i40e_remove(struct pci_dev *pdev)
7831{
7832 struct i40e_pf *pf = pci_get_drvdata(pdev);
7833 i40e_status ret_code;
7834 u32 reg;
7835 int i;
7836
7837 i40e_dbg_pf_exit(pf);
7838
7839 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
7840 i40e_free_vfs(pf);
7841 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
7842 }
7843
7844 /* no more scheduling of any task */
7845 set_bit(__I40E_DOWN, &pf->state);
7846 del_timer_sync(&pf->service_timer);
7847 cancel_work_sync(&pf->service_task);
7848
7849 i40e_fdir_teardown(pf);
7850
7851 /* If there is a switch structure or any orphans, remove them.
7852 * This will leave only the PF's VSI remaining.
7853 */
7854 for (i = 0; i < I40E_MAX_VEB; i++) {
7855 if (!pf->veb[i])
7856 continue;
7857
7858 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
7859 pf->veb[i]->uplink_seid == 0)
7860 i40e_switch_branch_release(pf->veb[i]);
7861 }
7862
7863 /* Now we can shutdown the PF's VSI, just before we kill
7864 * adminq and hmc.
7865 */
7866 if (pf->vsi[pf->lan_vsi])
7867 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
7868
7869 i40e_stop_misc_vector(pf);
7870 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
7871 synchronize_irq(pf->msix_entries[0].vector);
7872 free_irq(pf->msix_entries[0].vector, pf);
7873 }
7874
7875 /* shutdown and destroy the HMC */
7876 ret_code = i40e_shutdown_lan_hmc(&pf->hw);
7877 if (ret_code)
7878 dev_warn(&pdev->dev,
7879 "Failed to destroy the HMC resources: %d\n", ret_code);
7880
7881 /* shutdown the adminq */
7882 i40e_aq_queue_shutdown(&pf->hw, true);
7883 ret_code = i40e_shutdown_adminq(&pf->hw);
7884 if (ret_code)
7885 dev_warn(&pdev->dev,
7886 "Failed to destroy the Admin Queue resources: %d\n",
7887 ret_code);
7888
7889 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
7890 i40e_clear_interrupt_scheme(pf);
7891 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
7892 if (pf->vsi[i]) {
7893 i40e_vsi_clear_rings(pf->vsi[i]);
7894 i40e_vsi_clear(pf->vsi[i]);
7895 pf->vsi[i] = NULL;
7896 }
7897 }
7898
7899 for (i = 0; i < I40E_MAX_VEB; i++) {
7900 kfree(pf->veb[i]);
7901 pf->veb[i] = NULL;
7902 }
7903
7904 kfree(pf->qp_pile);
7905 kfree(pf->irq_pile);
7906 kfree(pf->sw_config);
7907 kfree(pf->vsi);
7908
7909 /* force a PF reset to clean anything leftover */
7910 reg = rd32(&pf->hw, I40E_PFGEN_CTRL);
7911 wr32(&pf->hw, I40E_PFGEN_CTRL, (reg | I40E_PFGEN_CTRL_PFSWR_MASK));
7912 i40e_flush(&pf->hw);
7913
7914 iounmap(pf->hw.hw_addr);
7915 kfree(pf);
7916 pci_release_selected_regions(pdev,
7917 pci_select_bars(pdev, IORESOURCE_MEM));
7918
7919 pci_disable_pcie_error_reporting(pdev);
7920 pci_disable_device(pdev);
7921}
7922
7923/**
7924 * i40e_pci_error_detected - warning that something funky happened in PCI land
7925 * @pdev: PCI device information struct
7926 *
7927 * Called to warn that something happened and the error handling steps
7928 * are in progress. Allows the driver to quiesce things, be ready for
7929 * remediation.
7930 **/
7931static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
7932 enum pci_channel_state error)
7933{
7934 struct i40e_pf *pf = pci_get_drvdata(pdev);
7935
7936 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
7937
7938 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +00007939 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
7940 rtnl_lock();
7941 i40e_prep_for_reset(pf);
7942 rtnl_unlock();
7943 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007944
7945 /* Request a slot reset */
7946 return PCI_ERS_RESULT_NEED_RESET;
7947}
7948
7949/**
7950 * i40e_pci_error_slot_reset - a PCI slot reset just happened
7951 * @pdev: PCI device information struct
7952 *
7953 * Called to find if the driver can work with the device now that
7954 * the pci slot has been reset. If a basic connection seems good
7955 * (registers are readable and have sane content) then return a
7956 * happy little PCI_ERS_RESULT_xxx.
7957 **/
7958static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
7959{
7960 struct i40e_pf *pf = pci_get_drvdata(pdev);
7961 pci_ers_result_t result;
7962 int err;
7963 u32 reg;
7964
7965 dev_info(&pdev->dev, "%s\n", __func__);
7966 if (pci_enable_device_mem(pdev)) {
7967 dev_info(&pdev->dev,
7968 "Cannot re-enable PCI device after reset.\n");
7969 result = PCI_ERS_RESULT_DISCONNECT;
7970 } else {
7971 pci_set_master(pdev);
7972 pci_restore_state(pdev);
7973 pci_save_state(pdev);
7974 pci_wake_from_d3(pdev, false);
7975
7976 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
7977 if (reg == 0)
7978 result = PCI_ERS_RESULT_RECOVERED;
7979 else
7980 result = PCI_ERS_RESULT_DISCONNECT;
7981 }
7982
7983 err = pci_cleanup_aer_uncorrect_error_status(pdev);
7984 if (err) {
7985 dev_info(&pdev->dev,
7986 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
7987 err);
7988 /* non-fatal, continue */
7989 }
7990
7991 return result;
7992}
7993
7994/**
7995 * i40e_pci_error_resume - restart operations after PCI error recovery
7996 * @pdev: PCI device information struct
7997 *
7998 * Called to allow the driver to bring things back up after PCI error
7999 * and/or reset recovery has finished.
8000 **/
8001static void i40e_pci_error_resume(struct pci_dev *pdev)
8002{
8003 struct i40e_pf *pf = pci_get_drvdata(pdev);
8004
8005 dev_info(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008006 if (test_bit(__I40E_SUSPENDED, &pf->state))
8007 return;
8008
8009 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008010 i40e_handle_reset_warning(pf);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008011 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008012}
8013
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008014/**
8015 * i40e_shutdown - PCI callback for shutting down
8016 * @pdev: PCI device information struct
8017 **/
8018static void i40e_shutdown(struct pci_dev *pdev)
8019{
8020 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008021 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008022
8023 set_bit(__I40E_SUSPENDED, &pf->state);
8024 set_bit(__I40E_DOWN, &pf->state);
8025 rtnl_lock();
8026 i40e_prep_for_reset(pf);
8027 rtnl_unlock();
8028
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008029 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
8030 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
8031
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008032 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008033 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008034 pci_set_power_state(pdev, PCI_D3hot);
8035 }
8036}
8037
8038#ifdef CONFIG_PM
8039/**
8040 * i40e_suspend - PCI callback for moving to D3
8041 * @pdev: PCI device information struct
8042 **/
8043static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
8044{
8045 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008046 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008047
8048 set_bit(__I40E_SUSPENDED, &pf->state);
8049 set_bit(__I40E_DOWN, &pf->state);
8050 rtnl_lock();
8051 i40e_prep_for_reset(pf);
8052 rtnl_unlock();
8053
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008054 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
8055 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
8056
8057 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008058 pci_set_power_state(pdev, PCI_D3hot);
8059
8060 return 0;
8061}
8062
8063/**
8064 * i40e_resume - PCI callback for waking up from D3
8065 * @pdev: PCI device information struct
8066 **/
8067static int i40e_resume(struct pci_dev *pdev)
8068{
8069 struct i40e_pf *pf = pci_get_drvdata(pdev);
8070 u32 err;
8071
8072 pci_set_power_state(pdev, PCI_D0);
8073 pci_restore_state(pdev);
8074 /* pci_restore_state() clears dev->state_saves, so
8075 * call pci_save_state() again to restore it.
8076 */
8077 pci_save_state(pdev);
8078
8079 err = pci_enable_device_mem(pdev);
8080 if (err) {
8081 dev_err(&pdev->dev,
8082 "%s: Cannot enable PCI device from suspend\n",
8083 __func__);
8084 return err;
8085 }
8086 pci_set_master(pdev);
8087
8088 /* no wakeup events while running */
8089 pci_wake_from_d3(pdev, false);
8090
8091 /* handling the reset will rebuild the device state */
8092 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
8093 clear_bit(__I40E_DOWN, &pf->state);
8094 rtnl_lock();
8095 i40e_reset_and_rebuild(pf, false);
8096 rtnl_unlock();
8097 }
8098
8099 return 0;
8100}
8101
8102#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008103static const struct pci_error_handlers i40e_err_handler = {
8104 .error_detected = i40e_pci_error_detected,
8105 .slot_reset = i40e_pci_error_slot_reset,
8106 .resume = i40e_pci_error_resume,
8107};
8108
8109static struct pci_driver i40e_driver = {
8110 .name = i40e_driver_name,
8111 .id_table = i40e_pci_tbl,
8112 .probe = i40e_probe,
8113 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008114#ifdef CONFIG_PM
8115 .suspend = i40e_suspend,
8116 .resume = i40e_resume,
8117#endif
8118 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008119 .err_handler = &i40e_err_handler,
8120 .sriov_configure = i40e_pci_sriov_configure,
8121};
8122
8123/**
8124 * i40e_init_module - Driver registration routine
8125 *
8126 * i40e_init_module is the first routine called when the driver is
8127 * loaded. All it does is register with the PCI subsystem.
8128 **/
8129static int __init i40e_init_module(void)
8130{
8131 pr_info("%s: %s - version %s\n", i40e_driver_name,
8132 i40e_driver_string, i40e_driver_version_str);
8133 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
8134 i40e_dbg_init();
8135 return pci_register_driver(&i40e_driver);
8136}
8137module_init(i40e_init_module);
8138
8139/**
8140 * i40e_exit_module - Driver exit cleanup routine
8141 *
8142 * i40e_exit_module is called just before the driver is removed
8143 * from memory.
8144 **/
8145static void __exit i40e_exit_module(void)
8146{
8147 pci_unregister_driver(&i40e_driver);
8148 i40e_dbg_exit();
8149}
8150module_exit(i40e_exit_module);