blob: 5ddbd54bbf55e33e72cb827ccb7ff9c3dcaca185 [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
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001218 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1219 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1220 return -EADDRNOTAVAIL;
1221
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001222 if (vsi->type == I40E_VSI_MAIN) {
1223 i40e_status ret;
1224 ret = i40e_aq_mac_address_write(&vsi->back->hw,
1225 I40E_AQC_WRITE_TYPE_LAA_ONLY,
1226 addr->sa_data, NULL);
1227 if (ret) {
1228 netdev_info(netdev,
1229 "Addr change for Main VSI failed: %d\n",
1230 ret);
1231 return -EADDRNOTAVAIL;
1232 }
1233
1234 memcpy(vsi->back->hw.mac.addr, addr->sa_data, netdev->addr_len);
1235 }
1236
1237 /* In order to be sure to not drop any packets, add the new address
1238 * then delete the old one.
1239 */
1240 f = i40e_add_filter(vsi, addr->sa_data, I40E_VLAN_ANY, false, false);
1241 if (!f)
1242 return -ENOMEM;
1243
1244 i40e_sync_vsi_filters(vsi);
1245 i40e_del_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY, false, false);
1246 i40e_sync_vsi_filters(vsi);
1247
1248 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1249
1250 return 0;
1251}
1252
1253/**
1254 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1255 * @vsi: the VSI being setup
1256 * @ctxt: VSI context structure
1257 * @enabled_tc: Enabled TCs bitmap
1258 * @is_add: True if called before Add VSI
1259 *
1260 * Setup VSI queue mapping for enabled traffic classes.
1261 **/
1262static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1263 struct i40e_vsi_context *ctxt,
1264 u8 enabled_tc,
1265 bool is_add)
1266{
1267 struct i40e_pf *pf = vsi->back;
1268 u16 sections = 0;
1269 u8 netdev_tc = 0;
1270 u16 numtc = 0;
1271 u16 qcount;
1272 u8 offset;
1273 u16 qmap;
1274 int i;
1275
1276 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1277 offset = 0;
1278
1279 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1280 /* Find numtc from enabled TC bitmap */
1281 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1282 if (enabled_tc & (1 << i)) /* TC is enabled */
1283 numtc++;
1284 }
1285 if (!numtc) {
1286 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1287 numtc = 1;
1288 }
1289 } else {
1290 /* At least TC0 is enabled in case of non-DCB case */
1291 numtc = 1;
1292 }
1293
1294 vsi->tc_config.numtc = numtc;
1295 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1296
1297 /* Setup queue offset/count for all TCs for given VSI */
1298 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1299 /* See if the given TC is enabled for the given VSI */
1300 if (vsi->tc_config.enabled_tc & (1 << i)) { /* TC is enabled */
1301 int pow, num_qps;
1302
1303 vsi->tc_config.tc_info[i].qoffset = offset;
1304 switch (vsi->type) {
1305 case I40E_VSI_MAIN:
1306 if (i == 0)
1307 qcount = pf->rss_size;
1308 else
1309 qcount = pf->num_tc_qps;
1310 vsi->tc_config.tc_info[i].qcount = qcount;
1311 break;
1312 case I40E_VSI_FDIR:
1313 case I40E_VSI_SRIOV:
1314 case I40E_VSI_VMDQ2:
1315 default:
1316 qcount = vsi->alloc_queue_pairs;
1317 vsi->tc_config.tc_info[i].qcount = qcount;
1318 WARN_ON(i != 0);
1319 break;
1320 }
1321
1322 /* find the power-of-2 of the number of queue pairs */
1323 num_qps = vsi->tc_config.tc_info[i].qcount;
1324 pow = 0;
1325 while (num_qps &&
1326 ((1 << pow) < vsi->tc_config.tc_info[i].qcount)) {
1327 pow++;
1328 num_qps >>= 1;
1329 }
1330
1331 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1332 qmap =
1333 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1334 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1335
1336 offset += vsi->tc_config.tc_info[i].qcount;
1337 } else {
1338 /* TC is not enabled so set the offset to
1339 * default queue and allocate one queue
1340 * for the given TC.
1341 */
1342 vsi->tc_config.tc_info[i].qoffset = 0;
1343 vsi->tc_config.tc_info[i].qcount = 1;
1344 vsi->tc_config.tc_info[i].netdev_tc = 0;
1345
1346 qmap = 0;
1347 }
1348 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1349 }
1350
1351 /* Set actual Tx/Rx queue pairs */
1352 vsi->num_queue_pairs = offset;
1353
1354 /* Scheduler section valid can only be set for ADD VSI */
1355 if (is_add) {
1356 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1357
1358 ctxt->info.up_enable_bits = enabled_tc;
1359 }
1360 if (vsi->type == I40E_VSI_SRIOV) {
1361 ctxt->info.mapping_flags |=
1362 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1363 for (i = 0; i < vsi->num_queue_pairs; i++)
1364 ctxt->info.queue_mapping[i] =
1365 cpu_to_le16(vsi->base_queue + i);
1366 } else {
1367 ctxt->info.mapping_flags |=
1368 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1369 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1370 }
1371 ctxt->info.valid_sections |= cpu_to_le16(sections);
1372}
1373
1374/**
1375 * i40e_set_rx_mode - NDO callback to set the netdev filters
1376 * @netdev: network interface device structure
1377 **/
1378static void i40e_set_rx_mode(struct net_device *netdev)
1379{
1380 struct i40e_netdev_priv *np = netdev_priv(netdev);
1381 struct i40e_mac_filter *f, *ftmp;
1382 struct i40e_vsi *vsi = np->vsi;
1383 struct netdev_hw_addr *uca;
1384 struct netdev_hw_addr *mca;
1385 struct netdev_hw_addr *ha;
1386
1387 /* add addr if not already in the filter list */
1388 netdev_for_each_uc_addr(uca, netdev) {
1389 if (!i40e_find_mac(vsi, uca->addr, false, true)) {
1390 if (i40e_is_vsi_in_vlan(vsi))
1391 i40e_put_mac_in_vlan(vsi, uca->addr,
1392 false, true);
1393 else
1394 i40e_add_filter(vsi, uca->addr, I40E_VLAN_ANY,
1395 false, true);
1396 }
1397 }
1398
1399 netdev_for_each_mc_addr(mca, netdev) {
1400 if (!i40e_find_mac(vsi, mca->addr, false, true)) {
1401 if (i40e_is_vsi_in_vlan(vsi))
1402 i40e_put_mac_in_vlan(vsi, mca->addr,
1403 false, true);
1404 else
1405 i40e_add_filter(vsi, mca->addr, I40E_VLAN_ANY,
1406 false, true);
1407 }
1408 }
1409
1410 /* remove filter if not in netdev list */
1411 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1412 bool found = false;
1413
1414 if (!f->is_netdev)
1415 continue;
1416
1417 if (is_multicast_ether_addr(f->macaddr)) {
1418 netdev_for_each_mc_addr(mca, netdev) {
1419 if (ether_addr_equal(mca->addr, f->macaddr)) {
1420 found = true;
1421 break;
1422 }
1423 }
1424 } else {
1425 netdev_for_each_uc_addr(uca, netdev) {
1426 if (ether_addr_equal(uca->addr, f->macaddr)) {
1427 found = true;
1428 break;
1429 }
1430 }
1431
1432 for_each_dev_addr(netdev, ha) {
1433 if (ether_addr_equal(ha->addr, f->macaddr)) {
1434 found = true;
1435 break;
1436 }
1437 }
1438 }
1439 if (!found)
1440 i40e_del_filter(
1441 vsi, f->macaddr, I40E_VLAN_ANY, false, true);
1442 }
1443
1444 /* check for other flag changes */
1445 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1446 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1447 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1448 }
1449}
1450
1451/**
1452 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1453 * @vsi: ptr to the VSI
1454 *
1455 * Push any outstanding VSI filter changes through the AdminQ.
1456 *
1457 * Returns 0 or error value
1458 **/
1459int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
1460{
1461 struct i40e_mac_filter *f, *ftmp;
1462 bool promisc_forced_on = false;
1463 bool add_happened = false;
1464 int filter_list_len = 0;
1465 u32 changed_flags = 0;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001466 i40e_status aq_ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001467 struct i40e_pf *pf;
1468 int num_add = 0;
1469 int num_del = 0;
1470 u16 cmd_flags;
1471
1472 /* empty array typed pointers, kcalloc later */
1473 struct i40e_aqc_add_macvlan_element_data *add_list;
1474 struct i40e_aqc_remove_macvlan_element_data *del_list;
1475
1476 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1477 usleep_range(1000, 2000);
1478 pf = vsi->back;
1479
1480 if (vsi->netdev) {
1481 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1482 vsi->current_netdev_flags = vsi->netdev->flags;
1483 }
1484
1485 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1486 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1487
1488 filter_list_len = pf->hw.aq.asq_buf_size /
1489 sizeof(struct i40e_aqc_remove_macvlan_element_data);
1490 del_list = kcalloc(filter_list_len,
1491 sizeof(struct i40e_aqc_remove_macvlan_element_data),
1492 GFP_KERNEL);
1493 if (!del_list)
1494 return -ENOMEM;
1495
1496 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1497 if (!f->changed)
1498 continue;
1499
1500 if (f->counter != 0)
1501 continue;
1502 f->changed = false;
1503 cmd_flags = 0;
1504
1505 /* add to delete list */
1506 memcpy(del_list[num_del].mac_addr,
1507 f->macaddr, ETH_ALEN);
1508 del_list[num_del].vlan_tag =
1509 cpu_to_le16((u16)(f->vlan ==
1510 I40E_VLAN_ANY ? 0 : f->vlan));
1511
1512 /* vlan0 as wild card to allow packets from all vlans */
1513 if (f->vlan == I40E_VLAN_ANY ||
1514 (vsi->netdev && !(vsi->netdev->features &
1515 NETIF_F_HW_VLAN_CTAG_FILTER)))
1516 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1517 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1518 del_list[num_del].flags = cmd_flags;
1519 num_del++;
1520
1521 /* unlink from filter list */
1522 list_del(&f->list);
1523 kfree(f);
1524
1525 /* flush a full buffer */
1526 if (num_del == filter_list_len) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001527 aq_ret = i40e_aq_remove_macvlan(&pf->hw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001528 vsi->seid, del_list, num_del,
1529 NULL);
1530 num_del = 0;
1531 memset(del_list, 0, sizeof(*del_list));
1532
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001533 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001534 dev_info(&pf->pdev->dev,
1535 "ignoring delete macvlan error, err %d, aq_err %d while flushing a full buffer\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001536 aq_ret,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001537 pf->hw.aq.asq_last_status);
1538 }
1539 }
1540 if (num_del) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001541 aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001542 del_list, num_del, NULL);
1543 num_del = 0;
1544
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001545 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001546 dev_info(&pf->pdev->dev,
1547 "ignoring delete macvlan error, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001548 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001549 }
1550
1551 kfree(del_list);
1552 del_list = NULL;
1553
1554 /* do all the adds now */
1555 filter_list_len = pf->hw.aq.asq_buf_size /
1556 sizeof(struct i40e_aqc_add_macvlan_element_data),
1557 add_list = kcalloc(filter_list_len,
1558 sizeof(struct i40e_aqc_add_macvlan_element_data),
1559 GFP_KERNEL);
1560 if (!add_list)
1561 return -ENOMEM;
1562
1563 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1564 if (!f->changed)
1565 continue;
1566
1567 if (f->counter == 0)
1568 continue;
1569 f->changed = false;
1570 add_happened = true;
1571 cmd_flags = 0;
1572
1573 /* add to add array */
1574 memcpy(add_list[num_add].mac_addr,
1575 f->macaddr, ETH_ALEN);
1576 add_list[num_add].vlan_tag =
1577 cpu_to_le16(
1578 (u16)(f->vlan == I40E_VLAN_ANY ? 0 : f->vlan));
1579 add_list[num_add].queue_number = 0;
1580
1581 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
1582
1583 /* vlan0 as wild card to allow packets from all vlans */
1584 if (f->vlan == I40E_VLAN_ANY || (vsi->netdev &&
1585 !(vsi->netdev->features &
1586 NETIF_F_HW_VLAN_CTAG_FILTER)))
1587 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
1588 add_list[num_add].flags = cpu_to_le16(cmd_flags);
1589 num_add++;
1590
1591 /* flush a full buffer */
1592 if (num_add == filter_list_len) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001593 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1594 add_list, num_add,
1595 NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001596 num_add = 0;
1597
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001598 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001599 break;
1600 memset(add_list, 0, sizeof(*add_list));
1601 }
1602 }
1603 if (num_add) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001604 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1605 add_list, num_add, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001606 num_add = 0;
1607 }
1608 kfree(add_list);
1609 add_list = NULL;
1610
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001611 if (add_happened && (!aq_ret)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001612 /* do nothing */;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001613 } else if (add_happened && (aq_ret)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001614 dev_info(&pf->pdev->dev,
1615 "add filter failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001616 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001617 if ((pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOSPC) &&
1618 !test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1619 &vsi->state)) {
1620 promisc_forced_on = true;
1621 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1622 &vsi->state);
1623 dev_info(&pf->pdev->dev, "promiscuous mode forced on\n");
1624 }
1625 }
1626 }
1627
1628 /* check for changes in promiscuous modes */
1629 if (changed_flags & IFF_ALLMULTI) {
1630 bool cur_multipromisc;
1631 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001632 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
1633 vsi->seid,
1634 cur_multipromisc,
1635 NULL);
1636 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001637 dev_info(&pf->pdev->dev,
1638 "set multi promisc failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001639 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001640 }
1641 if ((changed_flags & IFF_PROMISC) || promisc_forced_on) {
1642 bool cur_promisc;
1643 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
1644 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1645 &vsi->state));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001646 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(&vsi->back->hw,
1647 vsi->seid,
1648 cur_promisc, NULL);
1649 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001650 dev_info(&pf->pdev->dev,
1651 "set uni promisc failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001652 aq_ret, pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00001653 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
1654 vsi->seid,
1655 cur_promisc, NULL);
1656 if (aq_ret)
1657 dev_info(&pf->pdev->dev,
1658 "set brdcast promisc failed, err %d, aq_err %d\n",
1659 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001660 }
1661
1662 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
1663 return 0;
1664}
1665
1666/**
1667 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
1668 * @pf: board private structure
1669 **/
1670static void i40e_sync_filters_subtask(struct i40e_pf *pf)
1671{
1672 int v;
1673
1674 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
1675 return;
1676 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
1677
1678 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
1679 if (pf->vsi[v] &&
1680 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED))
1681 i40e_sync_vsi_filters(pf->vsi[v]);
1682 }
1683}
1684
1685/**
1686 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
1687 * @netdev: network interface device structure
1688 * @new_mtu: new value for maximum frame size
1689 *
1690 * Returns 0 on success, negative on failure
1691 **/
1692static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
1693{
1694 struct i40e_netdev_priv *np = netdev_priv(netdev);
1695 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
1696 struct i40e_vsi *vsi = np->vsi;
1697
1698 /* MTU < 68 is an error and causes problems on some kernels */
1699 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
1700 return -EINVAL;
1701
1702 netdev_info(netdev, "changing MTU from %d to %d\n",
1703 netdev->mtu, new_mtu);
1704 netdev->mtu = new_mtu;
1705 if (netif_running(netdev))
1706 i40e_vsi_reinit_locked(vsi);
1707
1708 return 0;
1709}
1710
1711/**
1712 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
1713 * @vsi: the vsi being adjusted
1714 **/
1715void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
1716{
1717 struct i40e_vsi_context ctxt;
1718 i40e_status ret;
1719
1720 if ((vsi->info.valid_sections &
1721 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1722 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
1723 return; /* already enabled */
1724
1725 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1726 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1727 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
1728
1729 ctxt.seid = vsi->seid;
1730 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1731 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1732 if (ret) {
1733 dev_info(&vsi->back->pdev->dev,
1734 "%s: update vsi failed, aq_err=%d\n",
1735 __func__, vsi->back->hw.aq.asq_last_status);
1736 }
1737}
1738
1739/**
1740 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
1741 * @vsi: the vsi being adjusted
1742 **/
1743void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
1744{
1745 struct i40e_vsi_context ctxt;
1746 i40e_status ret;
1747
1748 if ((vsi->info.valid_sections &
1749 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1750 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
1751 I40E_AQ_VSI_PVLAN_EMOD_MASK))
1752 return; /* already disabled */
1753
1754 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1755 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1756 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
1757
1758 ctxt.seid = vsi->seid;
1759 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1760 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1761 if (ret) {
1762 dev_info(&vsi->back->pdev->dev,
1763 "%s: update vsi failed, aq_err=%d\n",
1764 __func__, vsi->back->hw.aq.asq_last_status);
1765 }
1766}
1767
1768/**
1769 * i40e_vlan_rx_register - Setup or shutdown vlan offload
1770 * @netdev: network interface to be adjusted
1771 * @features: netdev features to test if VLAN offload is enabled or not
1772 **/
1773static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
1774{
1775 struct i40e_netdev_priv *np = netdev_priv(netdev);
1776 struct i40e_vsi *vsi = np->vsi;
1777
1778 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1779 i40e_vlan_stripping_enable(vsi);
1780 else
1781 i40e_vlan_stripping_disable(vsi);
1782}
1783
1784/**
1785 * i40e_vsi_add_vlan - Add vsi membership for given vlan
1786 * @vsi: the vsi being configured
1787 * @vid: vlan id to be added (0 = untagged only , -1 = any)
1788 **/
1789int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
1790{
1791 struct i40e_mac_filter *f, *add_f;
1792 bool is_netdev, is_vf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001793
1794 is_vf = (vsi->type == I40E_VSI_SRIOV);
1795 is_netdev = !!(vsi->netdev);
1796
1797 if (is_netdev) {
1798 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid,
1799 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, vsi->netdev->dev_addr);
1804 return -ENOMEM;
1805 }
1806 }
1807
1808 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1809 add_f = i40e_add_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
1810 if (!add_f) {
1811 dev_info(&vsi->back->pdev->dev,
1812 "Could not add vlan filter %d for %pM\n",
1813 vid, f->macaddr);
1814 return -ENOMEM;
1815 }
1816 }
1817
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001818 /* Now if we add a vlan tag, make sure to check if it is the first
1819 * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
1820 * with 0, so we now accept untagged and specified tagged traffic
1821 * (and not any taged and untagged)
1822 */
1823 if (vid > 0) {
1824 if (is_netdev && i40e_find_filter(vsi, vsi->netdev->dev_addr,
1825 I40E_VLAN_ANY,
1826 is_vf, is_netdev)) {
1827 i40e_del_filter(vsi, vsi->netdev->dev_addr,
1828 I40E_VLAN_ANY, is_vf, is_netdev);
1829 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0,
1830 is_vf, is_netdev);
1831 if (!add_f) {
1832 dev_info(&vsi->back->pdev->dev,
1833 "Could not add filter 0 for %pM\n",
1834 vsi->netdev->dev_addr);
1835 return -ENOMEM;
1836 }
1837 }
1838
1839 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1840 if (i40e_find_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1841 is_vf, is_netdev)) {
1842 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1843 is_vf, is_netdev);
1844 add_f = i40e_add_filter(vsi, f->macaddr,
1845 0, is_vf, is_netdev);
1846 if (!add_f) {
1847 dev_info(&vsi->back->pdev->dev,
1848 "Could not add filter 0 for %pM\n",
1849 f->macaddr);
1850 return -ENOMEM;
1851 }
1852 }
1853 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001854 }
1855
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001856 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1857 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1858 return 0;
1859
1860 return i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001861}
1862
1863/**
1864 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
1865 * @vsi: the vsi being configured
1866 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001867 *
1868 * Return: 0 on success or negative otherwise
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001869 **/
1870int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
1871{
1872 struct net_device *netdev = vsi->netdev;
1873 struct i40e_mac_filter *f, *add_f;
1874 bool is_vf, is_netdev;
1875 int filter_count = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001876
1877 is_vf = (vsi->type == I40E_VSI_SRIOV);
1878 is_netdev = !!(netdev);
1879
1880 if (is_netdev)
1881 i40e_del_filter(vsi, netdev->dev_addr, vid, is_vf, is_netdev);
1882
1883 list_for_each_entry(f, &vsi->mac_filter_list, list)
1884 i40e_del_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
1885
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001886 /* go through all the filters for this VSI and if there is only
1887 * vid == 0 it means there are no other filters, so vid 0 must
1888 * be replaced with -1. This signifies that we should from now
1889 * on accept any traffic (with any tag present, or untagged)
1890 */
1891 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1892 if (is_netdev) {
1893 if (f->vlan &&
1894 ether_addr_equal(netdev->dev_addr, f->macaddr))
1895 filter_count++;
1896 }
1897
1898 if (f->vlan)
1899 filter_count++;
1900 }
1901
1902 if (!filter_count && is_netdev) {
1903 i40e_del_filter(vsi, netdev->dev_addr, 0, is_vf, is_netdev);
1904 f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
1905 is_vf, is_netdev);
1906 if (!f) {
1907 dev_info(&vsi->back->pdev->dev,
1908 "Could not add filter %d for %pM\n",
1909 I40E_VLAN_ANY, netdev->dev_addr);
1910 return -ENOMEM;
1911 }
1912 }
1913
1914 if (!filter_count) {
1915 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1916 i40e_del_filter(vsi, f->macaddr, 0, is_vf, is_netdev);
1917 add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1918 is_vf, is_netdev);
1919 if (!add_f) {
1920 dev_info(&vsi->back->pdev->dev,
1921 "Could not add filter %d for %pM\n",
1922 I40E_VLAN_ANY, f->macaddr);
1923 return -ENOMEM;
1924 }
1925 }
1926 }
1927
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001928 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1929 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1930 return 0;
1931
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001932 return i40e_sync_vsi_filters(vsi);
1933}
1934
1935/**
1936 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
1937 * @netdev: network interface to be adjusted
1938 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001939 *
1940 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001941 **/
1942static int i40e_vlan_rx_add_vid(struct net_device *netdev,
1943 __always_unused __be16 proto, u16 vid)
1944{
1945 struct i40e_netdev_priv *np = netdev_priv(netdev);
1946 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001947 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001948
1949 if (vid > 4095)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001950 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001951
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001952 netdev_info(netdev, "adding %pM vid=%d\n", netdev->dev_addr, vid);
1953
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001954 /* If the network stack called us with vid = 0, we should
1955 * indicate to i40e_vsi_add_vlan() that we want to receive
1956 * any traffic (i.e. with any vlan tag, or untagged)
1957 */
1958 ret = i40e_vsi_add_vlan(vsi, vid ? vid : I40E_VLAN_ANY);
1959
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001960 if (!ret && (vid < VLAN_N_VID))
1961 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001962
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001963 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001964}
1965
1966/**
1967 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
1968 * @netdev: network interface to be adjusted
1969 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001970 *
1971 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001972 **/
1973static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
1974 __always_unused __be16 proto, u16 vid)
1975{
1976 struct i40e_netdev_priv *np = netdev_priv(netdev);
1977 struct i40e_vsi *vsi = np->vsi;
1978
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001979 netdev_info(netdev, "removing %pM vid=%d\n", netdev->dev_addr, vid);
1980
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001981 /* return code is ignored as there is nothing a user
1982 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001983 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001984 */
1985 i40e_vsi_kill_vlan(vsi, vid);
1986
1987 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001988
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001989 return 0;
1990}
1991
1992/**
1993 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
1994 * @vsi: the vsi being brought back up
1995 **/
1996static void i40e_restore_vlan(struct i40e_vsi *vsi)
1997{
1998 u16 vid;
1999
2000 if (!vsi->netdev)
2001 return;
2002
2003 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2004
2005 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2006 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2007 vid);
2008}
2009
2010/**
2011 * i40e_vsi_add_pvid - Add pvid for the VSI
2012 * @vsi: the vsi being adjusted
2013 * @vid: the vlan id to set as a PVID
2014 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002015int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002016{
2017 struct i40e_vsi_context ctxt;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002018 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002019
2020 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2021 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002022 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2023 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002024 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002025
2026 ctxt.seid = vsi->seid;
2027 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002028 aq_ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2029 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002030 dev_info(&vsi->back->pdev->dev,
2031 "%s: update vsi failed, aq_err=%d\n",
2032 __func__, vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002033 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002034 }
2035
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002036 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002037}
2038
2039/**
2040 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2041 * @vsi: the vsi being adjusted
2042 *
2043 * Just use the vlan_rx_register() service to put it back to normal
2044 **/
2045void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2046{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002047 i40e_vlan_stripping_disable(vsi);
2048
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002049 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002050}
2051
2052/**
2053 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2054 * @vsi: ptr to the VSI
2055 *
2056 * If this function returns with an error, then it's possible one or
2057 * more of the rings is populated (while the rest are not). It is the
2058 * callers duty to clean those orphaned rings.
2059 *
2060 * Return 0 on success, negative on failure
2061 **/
2062static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2063{
2064 int i, err = 0;
2065
2066 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002067 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002068
2069 return err;
2070}
2071
2072/**
2073 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2074 * @vsi: ptr to the VSI
2075 *
2076 * Free VSI's transmit software resources
2077 **/
2078static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2079{
2080 int i;
2081
2082 for (i = 0; i < vsi->num_queue_pairs; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002083 if (vsi->tx_rings[i]->desc)
2084 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002085}
2086
2087/**
2088 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2089 * @vsi: ptr to the VSI
2090 *
2091 * If this function returns with an error, then it's possible one or
2092 * more of the rings is populated (while the rest are not). It is the
2093 * callers duty to clean those orphaned rings.
2094 *
2095 * Return 0 on success, negative on failure
2096 **/
2097static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2098{
2099 int i, err = 0;
2100
2101 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002102 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002103 return err;
2104}
2105
2106/**
2107 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2108 * @vsi: ptr to the VSI
2109 *
2110 * Free all receive software resources
2111 **/
2112static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2113{
2114 int i;
2115
2116 for (i = 0; i < vsi->num_queue_pairs; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002117 if (vsi->rx_rings[i]->desc)
2118 i40e_free_rx_resources(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002119}
2120
2121/**
2122 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2123 * @ring: The Tx ring to configure
2124 *
2125 * Configure the Tx descriptor ring in the HMC context.
2126 **/
2127static int i40e_configure_tx_ring(struct i40e_ring *ring)
2128{
2129 struct i40e_vsi *vsi = ring->vsi;
2130 u16 pf_q = vsi->base_queue + ring->queue_index;
2131 struct i40e_hw *hw = &vsi->back->hw;
2132 struct i40e_hmc_obj_txq tx_ctx;
2133 i40e_status err = 0;
2134 u32 qtx_ctl = 0;
2135
2136 /* some ATR related tx ring init */
2137 if (vsi->back->flags & I40E_FLAG_FDIR_ATR_ENABLED) {
2138 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2139 ring->atr_count = 0;
2140 } else {
2141 ring->atr_sample_rate = 0;
2142 }
2143
2144 /* initialize XPS */
2145 if (ring->q_vector && ring->netdev &&
2146 !test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
2147 netif_set_xps_queue(ring->netdev,
2148 &ring->q_vector->affinity_mask,
2149 ring->queue_index);
2150
2151 /* clear the context structure first */
2152 memset(&tx_ctx, 0, sizeof(tx_ctx));
2153
2154 tx_ctx.new_context = 1;
2155 tx_ctx.base = (ring->dma / 128);
2156 tx_ctx.qlen = ring->count;
2157 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FDIR_ENABLED |
2158 I40E_FLAG_FDIR_ATR_ENABLED));
2159
2160 /* As part of VSI creation/update, FW allocates certain
2161 * Tx arbitration queue sets for each TC enabled for
2162 * the VSI. The FW returns the handles to these queue
2163 * sets as part of the response buffer to Add VSI,
2164 * Update VSI, etc. AQ commands. It is expected that
2165 * these queue set handles be associated with the Tx
2166 * queues by the driver as part of the TX queue context
2167 * initialization. This has to be done regardless of
2168 * DCB as by default everything is mapped to TC0.
2169 */
2170 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2171 tx_ctx.rdylist_act = 0;
2172
2173 /* clear the context in the HMC */
2174 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2175 if (err) {
2176 dev_info(&vsi->back->pdev->dev,
2177 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2178 ring->queue_index, pf_q, err);
2179 return -ENOMEM;
2180 }
2181
2182 /* set the context in the HMC */
2183 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2184 if (err) {
2185 dev_info(&vsi->back->pdev->dev,
2186 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2187 ring->queue_index, pf_q, err);
2188 return -ENOMEM;
2189 }
2190
2191 /* Now associate this queue with this PCI function */
2192 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Shannon Nelson13fd9772013-09-28 07:14:19 +00002193 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2194 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002195 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2196 i40e_flush(hw);
2197
2198 clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
2199
2200 /* cache tail off for easier writes later */
2201 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2202
2203 return 0;
2204}
2205
2206/**
2207 * i40e_configure_rx_ring - Configure a receive ring context
2208 * @ring: The Rx ring to configure
2209 *
2210 * Configure the Rx descriptor ring in the HMC context.
2211 **/
2212static int i40e_configure_rx_ring(struct i40e_ring *ring)
2213{
2214 struct i40e_vsi *vsi = ring->vsi;
2215 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2216 u16 pf_q = vsi->base_queue + ring->queue_index;
2217 struct i40e_hw *hw = &vsi->back->hw;
2218 struct i40e_hmc_obj_rxq rx_ctx;
2219 i40e_status err = 0;
2220
2221 ring->state = 0;
2222
2223 /* clear the context structure first */
2224 memset(&rx_ctx, 0, sizeof(rx_ctx));
2225
2226 ring->rx_buf_len = vsi->rx_buf_len;
2227 ring->rx_hdr_len = vsi->rx_hdr_len;
2228
2229 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
2230 rx_ctx.hbuff = ring->rx_hdr_len >> I40E_RXQ_CTX_HBUFF_SHIFT;
2231
2232 rx_ctx.base = (ring->dma / 128);
2233 rx_ctx.qlen = ring->count;
2234
2235 if (vsi->back->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED) {
2236 set_ring_16byte_desc_enabled(ring);
2237 rx_ctx.dsize = 0;
2238 } else {
2239 rx_ctx.dsize = 1;
2240 }
2241
2242 rx_ctx.dtype = vsi->dtype;
2243 if (vsi->dtype) {
2244 set_ring_ps_enabled(ring);
2245 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
2246 I40E_RX_SPLIT_IP |
2247 I40E_RX_SPLIT_TCP_UDP |
2248 I40E_RX_SPLIT_SCTP;
2249 } else {
2250 rx_ctx.hsplit_0 = 0;
2251 }
2252
2253 rx_ctx.rxmax = min_t(u16, vsi->max_frame,
2254 (chain_len * ring->rx_buf_len));
2255 rx_ctx.tphrdesc_ena = 1;
2256 rx_ctx.tphwdesc_ena = 1;
2257 rx_ctx.tphdata_ena = 1;
2258 rx_ctx.tphhead_ena = 1;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00002259 if (hw->revision_id == 0)
2260 rx_ctx.lrxqthresh = 0;
2261 else
2262 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002263 rx_ctx.crcstrip = 1;
2264 rx_ctx.l2tsel = 1;
2265 rx_ctx.showiv = 1;
2266
2267 /* clear the context in the HMC */
2268 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2269 if (err) {
2270 dev_info(&vsi->back->pdev->dev,
2271 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2272 ring->queue_index, pf_q, err);
2273 return -ENOMEM;
2274 }
2275
2276 /* set the context in the HMC */
2277 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2278 if (err) {
2279 dev_info(&vsi->back->pdev->dev,
2280 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2281 ring->queue_index, pf_q, err);
2282 return -ENOMEM;
2283 }
2284
2285 /* cache tail for quicker writes, and clear the reg before use */
2286 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2287 writel(0, ring->tail);
2288
2289 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
2290
2291 return 0;
2292}
2293
2294/**
2295 * i40e_vsi_configure_tx - Configure the VSI for Tx
2296 * @vsi: VSI structure describing this set of rings and resources
2297 *
2298 * Configure the Tx VSI for operation.
2299 **/
2300static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2301{
2302 int err = 0;
2303 u16 i;
2304
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002305 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2306 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002307
2308 return err;
2309}
2310
2311/**
2312 * i40e_vsi_configure_rx - Configure the VSI for Rx
2313 * @vsi: the VSI being configured
2314 *
2315 * Configure the Rx VSI for operation.
2316 **/
2317static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
2318{
2319 int err = 0;
2320 u16 i;
2321
2322 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
2323 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
2324 + ETH_FCS_LEN + VLAN_HLEN;
2325 else
2326 vsi->max_frame = I40E_RXBUFFER_2048;
2327
2328 /* figure out correct receive buffer length */
2329 switch (vsi->back->flags & (I40E_FLAG_RX_1BUF_ENABLED |
2330 I40E_FLAG_RX_PS_ENABLED)) {
2331 case I40E_FLAG_RX_1BUF_ENABLED:
2332 vsi->rx_hdr_len = 0;
2333 vsi->rx_buf_len = vsi->max_frame;
2334 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2335 break;
2336 case I40E_FLAG_RX_PS_ENABLED:
2337 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2338 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2339 vsi->dtype = I40E_RX_DTYPE_HEADER_SPLIT;
2340 break;
2341 default:
2342 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2343 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2344 vsi->dtype = I40E_RX_DTYPE_SPLIT_ALWAYS;
2345 break;
2346 }
2347
2348 /* round up for the chip's needs */
2349 vsi->rx_hdr_len = ALIGN(vsi->rx_hdr_len,
2350 (1 << I40E_RXQ_CTX_HBUFF_SHIFT));
2351 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
2352 (1 << I40E_RXQ_CTX_DBUFF_SHIFT));
2353
2354 /* set up individual rings */
2355 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002356 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002357
2358 return err;
2359}
2360
2361/**
2362 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
2363 * @vsi: ptr to the VSI
2364 **/
2365static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
2366{
2367 u16 qoffset, qcount;
2368 int i, n;
2369
2370 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED))
2371 return;
2372
2373 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
2374 if (!(vsi->tc_config.enabled_tc & (1 << n)))
2375 continue;
2376
2377 qoffset = vsi->tc_config.tc_info[n].qoffset;
2378 qcount = vsi->tc_config.tc_info[n].qcount;
2379 for (i = qoffset; i < (qoffset + qcount); i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002380 struct i40e_ring *rx_ring = vsi->rx_rings[i];
2381 struct i40e_ring *tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002382 rx_ring->dcb_tc = n;
2383 tx_ring->dcb_tc = n;
2384 }
2385 }
2386}
2387
2388/**
2389 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
2390 * @vsi: ptr to the VSI
2391 **/
2392static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
2393{
2394 if (vsi->netdev)
2395 i40e_set_rx_mode(vsi->netdev);
2396}
2397
2398/**
2399 * i40e_vsi_configure - Set up the VSI for action
2400 * @vsi: the VSI being configured
2401 **/
2402static int i40e_vsi_configure(struct i40e_vsi *vsi)
2403{
2404 int err;
2405
2406 i40e_set_vsi_rx_mode(vsi);
2407 i40e_restore_vlan(vsi);
2408 i40e_vsi_config_dcb_rings(vsi);
2409 err = i40e_vsi_configure_tx(vsi);
2410 if (!err)
2411 err = i40e_vsi_configure_rx(vsi);
2412
2413 return err;
2414}
2415
2416/**
2417 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
2418 * @vsi: the VSI being configured
2419 **/
2420static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
2421{
2422 struct i40e_pf *pf = vsi->back;
2423 struct i40e_q_vector *q_vector;
2424 struct i40e_hw *hw = &pf->hw;
2425 u16 vector;
2426 int i, q;
2427 u32 val;
2428 u32 qp;
2429
2430 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
2431 * and PFINT_LNKLSTn registers, e.g.:
2432 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
2433 */
2434 qp = vsi->base_queue;
2435 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00002436 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
2437 q_vector = vsi->q_vectors[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002438 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2439 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2440 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
2441 q_vector->rx.itr);
2442 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2443 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2444 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
2445 q_vector->tx.itr);
2446
2447 /* Linked list for the queuepairs assigned to this vector */
2448 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
2449 for (q = 0; q < q_vector->num_ringpairs; q++) {
2450 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2451 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2452 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
2453 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
2454 (I40E_QUEUE_TYPE_TX
2455 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
2456
2457 wr32(hw, I40E_QINT_RQCTL(qp), val);
2458
2459 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2460 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2461 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
2462 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
2463 (I40E_QUEUE_TYPE_RX
2464 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2465
2466 /* Terminate the linked list */
2467 if (q == (q_vector->num_ringpairs - 1))
2468 val |= (I40E_QUEUE_END_OF_LIST
2469 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2470
2471 wr32(hw, I40E_QINT_TQCTL(qp), val);
2472 qp++;
2473 }
2474 }
2475
2476 i40e_flush(hw);
2477}
2478
2479/**
2480 * i40e_enable_misc_int_causes - enable the non-queue interrupts
2481 * @hw: ptr to the hardware info
2482 **/
2483static void i40e_enable_misc_int_causes(struct i40e_hw *hw)
2484{
2485 u32 val;
2486
2487 /* clear things first */
2488 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
2489 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
2490
2491 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
2492 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
2493 I40E_PFINT_ICR0_ENA_GRST_MASK |
2494 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
2495 I40E_PFINT_ICR0_ENA_GPIO_MASK |
2496 I40E_PFINT_ICR0_ENA_STORM_DETECT_MASK |
2497 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
2498 I40E_PFINT_ICR0_ENA_VFLR_MASK |
2499 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2500
2501 wr32(hw, I40E_PFINT_ICR0_ENA, val);
2502
2503 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00002504 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
2505 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002506
2507 /* OTHER_ITR_IDX = 0 */
2508 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
2509}
2510
2511/**
2512 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
2513 * @vsi: the VSI being configured
2514 **/
2515static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
2516{
Alexander Duyck493fb302013-09-28 07:01:44 +00002517 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002518 struct i40e_pf *pf = vsi->back;
2519 struct i40e_hw *hw = &pf->hw;
2520 u32 val;
2521
2522 /* set the ITR configuration */
2523 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2524 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2525 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
2526 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2527 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2528 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
2529
2530 i40e_enable_misc_int_causes(hw);
2531
2532 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
2533 wr32(hw, I40E_PFINT_LNKLST0, 0);
2534
2535 /* Associate the queue pair to the vector and enable the q int */
2536 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2537 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2538 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2539
2540 wr32(hw, I40E_QINT_RQCTL(0), val);
2541
2542 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2543 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2544 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2545
2546 wr32(hw, I40E_QINT_TQCTL(0), val);
2547 i40e_flush(hw);
2548}
2549
2550/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00002551 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
2552 * @pf: board private structure
2553 **/
2554void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
2555{
2556 struct i40e_hw *hw = &pf->hw;
2557
2558 wr32(hw, I40E_PFINT_DYN_CTL0,
2559 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2560 i40e_flush(hw);
2561}
2562
2563/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002564 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
2565 * @pf: board private structure
2566 **/
Shannon Nelson116a57d2013-09-28 07:13:59 +00002567void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002568{
2569 struct i40e_hw *hw = &pf->hw;
2570 u32 val;
2571
2572 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
2573 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
2574 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
2575
2576 wr32(hw, I40E_PFINT_DYN_CTL0, val);
2577 i40e_flush(hw);
2578}
2579
2580/**
2581 * i40e_irq_dynamic_enable - Enable default interrupt generation settings
2582 * @vsi: pointer to a vsi
2583 * @vector: enable a particular Hw Interrupt vector
2584 **/
2585void i40e_irq_dynamic_enable(struct i40e_vsi *vsi, int vector)
2586{
2587 struct i40e_pf *pf = vsi->back;
2588 struct i40e_hw *hw = &pf->hw;
2589 u32 val;
2590
2591 val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
2592 I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
2593 (I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2594 wr32(hw, I40E_PFINT_DYN_CTLN(vector - 1), val);
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00002595 /* skip the flush */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002596}
2597
2598/**
2599 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
2600 * @irq: interrupt number
2601 * @data: pointer to a q_vector
2602 **/
2603static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
2604{
2605 struct i40e_q_vector *q_vector = data;
2606
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002607 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002608 return IRQ_HANDLED;
2609
2610 napi_schedule(&q_vector->napi);
2611
2612 return IRQ_HANDLED;
2613}
2614
2615/**
2616 * i40e_fdir_clean_rings - Interrupt Handler for FDIR rings
2617 * @irq: interrupt number
2618 * @data: pointer to a q_vector
2619 **/
2620static irqreturn_t i40e_fdir_clean_rings(int irq, void *data)
2621{
2622 struct i40e_q_vector *q_vector = data;
2623
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002624 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002625 return IRQ_HANDLED;
2626
2627 pr_info("fdir ring cleaning needed\n");
2628
2629 return IRQ_HANDLED;
2630}
2631
2632/**
2633 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
2634 * @vsi: the VSI being configured
2635 * @basename: name for the vector
2636 *
2637 * Allocates MSI-X vectors and requests interrupts from the kernel.
2638 **/
2639static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
2640{
2641 int q_vectors = vsi->num_q_vectors;
2642 struct i40e_pf *pf = vsi->back;
2643 int base = vsi->base_vector;
2644 int rx_int_idx = 0;
2645 int tx_int_idx = 0;
2646 int vector, err;
2647
2648 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00002649 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002650
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002651 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002652 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2653 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
2654 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002655 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002656 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2657 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002658 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002659 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2660 "%s-%s-%d", basename, "tx", tx_int_idx++);
2661 } else {
2662 /* skip this unused q_vector */
2663 continue;
2664 }
2665 err = request_irq(pf->msix_entries[base + vector].vector,
2666 vsi->irq_handler,
2667 0,
2668 q_vector->name,
2669 q_vector);
2670 if (err) {
2671 dev_info(&pf->pdev->dev,
2672 "%s: request_irq failed, error: %d\n",
2673 __func__, err);
2674 goto free_queue_irqs;
2675 }
2676 /* assign the mask for this irq */
2677 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2678 &q_vector->affinity_mask);
2679 }
2680
2681 return 0;
2682
2683free_queue_irqs:
2684 while (vector) {
2685 vector--;
2686 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2687 NULL);
2688 free_irq(pf->msix_entries[base + vector].vector,
2689 &(vsi->q_vectors[vector]));
2690 }
2691 return err;
2692}
2693
2694/**
2695 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
2696 * @vsi: the VSI being un-configured
2697 **/
2698static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
2699{
2700 struct i40e_pf *pf = vsi->back;
2701 struct i40e_hw *hw = &pf->hw;
2702 int base = vsi->base_vector;
2703 int i;
2704
2705 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002706 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
2707 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002708 }
2709
2710 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2711 for (i = vsi->base_vector;
2712 i < (vsi->num_q_vectors + vsi->base_vector); i++)
2713 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
2714
2715 i40e_flush(hw);
2716 for (i = 0; i < vsi->num_q_vectors; i++)
2717 synchronize_irq(pf->msix_entries[i + base].vector);
2718 } else {
2719 /* Legacy and MSI mode - this stops all interrupt handling */
2720 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
2721 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
2722 i40e_flush(hw);
2723 synchronize_irq(pf->pdev->irq);
2724 }
2725}
2726
2727/**
2728 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
2729 * @vsi: the VSI being configured
2730 **/
2731static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
2732{
2733 struct i40e_pf *pf = vsi->back;
2734 int i;
2735
2736 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2737 for (i = vsi->base_vector;
2738 i < (vsi->num_q_vectors + vsi->base_vector); i++)
2739 i40e_irq_dynamic_enable(vsi, i);
2740 } else {
2741 i40e_irq_dynamic_enable_icr0(pf);
2742 }
2743
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00002744 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002745 return 0;
2746}
2747
2748/**
2749 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
2750 * @pf: board private structure
2751 **/
2752static void i40e_stop_misc_vector(struct i40e_pf *pf)
2753{
2754 /* Disable ICR 0 */
2755 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
2756 i40e_flush(&pf->hw);
2757}
2758
2759/**
2760 * i40e_intr - MSI/Legacy and non-queue interrupt handler
2761 * @irq: interrupt number
2762 * @data: pointer to a q_vector
2763 *
2764 * This is the handler used for all MSI/Legacy interrupts, and deals
2765 * with both queue and non-queue interrupts. This is also used in
2766 * MSIX mode to handle the non-queue interrupts.
2767 **/
2768static irqreturn_t i40e_intr(int irq, void *data)
2769{
2770 struct i40e_pf *pf = (struct i40e_pf *)data;
2771 struct i40e_hw *hw = &pf->hw;
2772 u32 icr0, icr0_remaining;
2773 u32 val, ena_mask;
2774
2775 icr0 = rd32(hw, I40E_PFINT_ICR0);
2776
Shannon Nelson116a57d2013-09-28 07:13:59 +00002777 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
2778 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
2779 return IRQ_NONE;
2780
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002781 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
2782
Shannon Nelsoncd92e722013-11-16 10:00:44 +00002783 /* if interrupt but no bits showing, must be SWINT */
2784 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
2785 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
2786 pf->sw_int_count++;
2787
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002788 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
2789 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
2790
2791 /* temporarily disable queue cause for NAPI processing */
2792 u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
2793 qval &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
2794 wr32(hw, I40E_QINT_RQCTL(0), qval);
2795
2796 qval = rd32(hw, I40E_QINT_TQCTL(0));
2797 qval &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
2798 wr32(hw, I40E_QINT_TQCTL(0), qval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002799
2800 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck493fb302013-09-28 07:01:44 +00002801 napi_schedule(&pf->vsi[pf->lan_vsi]->q_vectors[0]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002802 }
2803
2804 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
2805 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2806 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
2807 }
2808
2809 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
2810 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
2811 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
2812 }
2813
2814 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
2815 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
2816 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
2817 }
2818
2819 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
2820 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
2821 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
2822 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
2823 val = rd32(hw, I40E_GLGEN_RSTAT);
2824 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
2825 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelsond52cf0a2013-11-16 10:00:39 +00002826 if (val == I40E_RESET_CORER)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002827 pf->corer_count++;
Shannon Nelsond52cf0a2013-11-16 10:00:39 +00002828 else if (val == I40E_RESET_GLOBR)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002829 pf->globr_count++;
Shannon Nelsond52cf0a2013-11-16 10:00:39 +00002830 else if (val == I40E_RESET_EMPR)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002831 pf->empr_count++;
2832 }
2833
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002834 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
2835 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
2836 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
2837 }
2838
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002839 /* If a critical error is pending we have no choice but to reset the
2840 * device.
2841 * Report and mask out any remaining unexpected interrupts.
2842 */
2843 icr0_remaining = icr0 & ena_mask;
2844 if (icr0_remaining) {
2845 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
2846 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002847 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002848 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
2849 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK) ||
2850 (icr0_remaining & I40E_PFINT_ICR0_MAL_DETECT_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002851 dev_info(&pf->pdev->dev, "device will be reset\n");
2852 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
2853 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002854 }
2855 ena_mask &= ~icr0_remaining;
2856 }
2857
2858 /* re-enable interrupt causes */
2859 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002860 if (!test_bit(__I40E_DOWN, &pf->state)) {
2861 i40e_service_event_schedule(pf);
2862 i40e_irq_dynamic_enable_icr0(pf);
2863 }
2864
2865 return IRQ_HANDLED;
2866}
2867
2868/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002869 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002870 * @vsi: the VSI being configured
2871 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002872 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002873 **/
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002874static void map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002875{
Alexander Duyck493fb302013-09-28 07:01:44 +00002876 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002877 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
2878 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002879
2880 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002881 tx_ring->next = q_vector->tx.ring;
2882 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002883 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002884
2885 rx_ring->q_vector = q_vector;
2886 rx_ring->next = q_vector->rx.ring;
2887 q_vector->rx.ring = rx_ring;
2888 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002889}
2890
2891/**
2892 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
2893 * @vsi: the VSI being configured
2894 *
2895 * This function maps descriptor rings to the queue-specific vectors
2896 * we were allotted through the MSI-X enabling code. Ideally, we'd have
2897 * one vector per queue pair, but on a constrained vector budget, we
2898 * group the queue pairs as "efficiently" as possible.
2899 **/
2900static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
2901{
2902 int qp_remaining = vsi->num_queue_pairs;
2903 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002904 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002905 int v_start = 0;
2906 int qp_idx = 0;
2907
2908 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
2909 * group them so there are multiple queues per vector.
2910 */
2911 for (; v_start < q_vectors && qp_remaining; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002912 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
2913
2914 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
2915
2916 q_vector->num_ringpairs = num_ringpairs;
2917
2918 q_vector->rx.count = 0;
2919 q_vector->tx.count = 0;
2920 q_vector->rx.ring = NULL;
2921 q_vector->tx.ring = NULL;
2922
2923 while (num_ringpairs--) {
2924 map_vector_to_qp(vsi, v_start, qp_idx);
2925 qp_idx++;
2926 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002927 }
2928 }
2929}
2930
2931/**
2932 * i40e_vsi_request_irq - Request IRQ from the OS
2933 * @vsi: the VSI being configured
2934 * @basename: name for the vector
2935 **/
2936static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
2937{
2938 struct i40e_pf *pf = vsi->back;
2939 int err;
2940
2941 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
2942 err = i40e_vsi_request_irq_msix(vsi, basename);
2943 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
2944 err = request_irq(pf->pdev->irq, i40e_intr, 0,
2945 pf->misc_int_name, pf);
2946 else
2947 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
2948 pf->misc_int_name, pf);
2949
2950 if (err)
2951 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
2952
2953 return err;
2954}
2955
2956#ifdef CONFIG_NET_POLL_CONTROLLER
2957/**
2958 * i40e_netpoll - A Polling 'interrupt'handler
2959 * @netdev: network interface device structure
2960 *
2961 * This is used by netconsole to send skbs without having to re-enable
2962 * interrupts. It's not called while the normal interrupt routine is executing.
2963 **/
2964static void i40e_netpoll(struct net_device *netdev)
2965{
2966 struct i40e_netdev_priv *np = netdev_priv(netdev);
2967 struct i40e_vsi *vsi = np->vsi;
2968 struct i40e_pf *pf = vsi->back;
2969 int i;
2970
2971 /* if interface is down do nothing */
2972 if (test_bit(__I40E_DOWN, &vsi->state))
2973 return;
2974
2975 pf->flags |= I40E_FLAG_IN_NETPOLL;
2976 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2977 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00002978 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002979 } else {
2980 i40e_intr(pf->pdev->irq, netdev);
2981 }
2982 pf->flags &= ~I40E_FLAG_IN_NETPOLL;
2983}
2984#endif
2985
2986/**
2987 * i40e_vsi_control_tx - Start or stop a VSI's rings
2988 * @vsi: the VSI being configured
2989 * @enable: start or stop the rings
2990 **/
2991static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
2992{
2993 struct i40e_pf *pf = vsi->back;
2994 struct i40e_hw *hw = &pf->hw;
2995 int i, j, pf_q;
2996 u32 tx_reg;
2997
2998 pf_q = vsi->base_queue;
2999 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
3000 j = 1000;
3001 do {
3002 usleep_range(1000, 2000);
3003 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
3004 } while (j-- && ((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT)
3005 ^ (tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT)) & 1);
3006
Mitch Williamsfda972f2013-11-28 06:39:29 +00003007 /* Skip if the queue is already in the requested state */
3008 if (enable && (tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3009 continue;
3010 if (!enable && !(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3011 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003012
3013 /* turn on/off the queue */
3014 if (enable)
3015 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK |
3016 I40E_QTX_ENA_QENA_STAT_MASK;
3017 else
3018 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
3019
3020 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
3021
3022 /* wait for the change to finish */
3023 for (j = 0; j < 10; j++) {
3024 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
3025 if (enable) {
3026 if ((tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3027 break;
3028 } else {
3029 if (!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3030 break;
3031 }
3032
3033 udelay(10);
3034 }
3035 if (j >= 10) {
3036 dev_info(&pf->pdev->dev, "Tx ring %d %sable timeout\n",
3037 pf_q, (enable ? "en" : "dis"));
3038 return -ETIMEDOUT;
3039 }
3040 }
3041
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003042 if (hw->revision_id == 0)
3043 mdelay(50);
3044
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003045 return 0;
3046}
3047
3048/**
3049 * i40e_vsi_control_rx - Start or stop a VSI's rings
3050 * @vsi: the VSI being configured
3051 * @enable: start or stop the rings
3052 **/
3053static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3054{
3055 struct i40e_pf *pf = vsi->back;
3056 struct i40e_hw *hw = &pf->hw;
3057 int i, j, pf_q;
3058 u32 rx_reg;
3059
3060 pf_q = vsi->base_queue;
3061 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
3062 j = 1000;
3063 do {
3064 usleep_range(1000, 2000);
3065 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
3066 } while (j-- && ((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT)
3067 ^ (rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT)) & 1);
3068
3069 if (enable) {
3070 /* is STAT set ? */
3071 if ((rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3072 continue;
3073 } else {
3074 /* is !STAT set ? */
3075 if (!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3076 continue;
3077 }
3078
3079 /* turn on/off the queue */
3080 if (enable)
3081 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK |
3082 I40E_QRX_ENA_QENA_STAT_MASK;
3083 else
3084 rx_reg &= ~(I40E_QRX_ENA_QENA_REQ_MASK |
3085 I40E_QRX_ENA_QENA_STAT_MASK);
3086 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
3087
3088 /* wait for the change to finish */
3089 for (j = 0; j < 10; j++) {
3090 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
3091
3092 if (enable) {
3093 if ((rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3094 break;
3095 } else {
3096 if (!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3097 break;
3098 }
3099
3100 udelay(10);
3101 }
3102 if (j >= 10) {
3103 dev_info(&pf->pdev->dev, "Rx ring %d %sable timeout\n",
3104 pf_q, (enable ? "en" : "dis"));
3105 return -ETIMEDOUT;
3106 }
3107 }
3108
3109 return 0;
3110}
3111
3112/**
3113 * i40e_vsi_control_rings - Start or stop a VSI's rings
3114 * @vsi: the VSI being configured
3115 * @enable: start or stop the rings
3116 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00003117int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003118{
3119 int ret;
3120
3121 /* do rx first for enable and last for disable */
3122 if (request) {
3123 ret = i40e_vsi_control_rx(vsi, request);
3124 if (ret)
3125 return ret;
3126 ret = i40e_vsi_control_tx(vsi, request);
3127 } else {
3128 ret = i40e_vsi_control_tx(vsi, request);
3129 if (ret)
3130 return ret;
3131 ret = i40e_vsi_control_rx(vsi, request);
3132 }
3133
3134 return ret;
3135}
3136
3137/**
3138 * i40e_vsi_free_irq - Free the irq association with the OS
3139 * @vsi: the VSI being configured
3140 **/
3141static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
3142{
3143 struct i40e_pf *pf = vsi->back;
3144 struct i40e_hw *hw = &pf->hw;
3145 int base = vsi->base_vector;
3146 u32 val, qp;
3147 int i;
3148
3149 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3150 if (!vsi->q_vectors)
3151 return;
3152
3153 for (i = 0; i < vsi->num_q_vectors; i++) {
3154 u16 vector = i + base;
3155
3156 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00003157 if (!vsi->q_vectors[i] ||
3158 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003159 continue;
3160
3161 /* clear the affinity_mask in the IRQ descriptor */
3162 irq_set_affinity_hint(pf->msix_entries[vector].vector,
3163 NULL);
3164 free_irq(pf->msix_entries[vector].vector,
Alexander Duyck493fb302013-09-28 07:01:44 +00003165 vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003166
3167 /* Tear down the interrupt queue link list
3168 *
3169 * We know that they come in pairs and always
3170 * the Rx first, then the Tx. To clear the
3171 * link list, stick the EOL value into the
3172 * next_q field of the registers.
3173 */
3174 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
3175 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3176 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3177 val |= I40E_QUEUE_END_OF_LIST
3178 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3179 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
3180
3181 while (qp != I40E_QUEUE_END_OF_LIST) {
3182 u32 next;
3183
3184 val = rd32(hw, I40E_QINT_RQCTL(qp));
3185
3186 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3187 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3188 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3189 I40E_QINT_RQCTL_INTEVENT_MASK);
3190
3191 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3192 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3193
3194 wr32(hw, I40E_QINT_RQCTL(qp), val);
3195
3196 val = rd32(hw, I40E_QINT_TQCTL(qp));
3197
3198 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
3199 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
3200
3201 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3202 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3203 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3204 I40E_QINT_TQCTL_INTEVENT_MASK);
3205
3206 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3207 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3208
3209 wr32(hw, I40E_QINT_TQCTL(qp), val);
3210 qp = next;
3211 }
3212 }
3213 } else {
3214 free_irq(pf->pdev->irq, pf);
3215
3216 val = rd32(hw, I40E_PFINT_LNKLST0);
3217 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3218 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3219 val |= I40E_QUEUE_END_OF_LIST
3220 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
3221 wr32(hw, I40E_PFINT_LNKLST0, val);
3222
3223 val = rd32(hw, I40E_QINT_RQCTL(qp));
3224 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3225 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3226 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3227 I40E_QINT_RQCTL_INTEVENT_MASK);
3228
3229 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3230 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3231
3232 wr32(hw, I40E_QINT_RQCTL(qp), val);
3233
3234 val = rd32(hw, I40E_QINT_TQCTL(qp));
3235
3236 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3237 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3238 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3239 I40E_QINT_TQCTL_INTEVENT_MASK);
3240
3241 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3242 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3243
3244 wr32(hw, I40E_QINT_TQCTL(qp), val);
3245 }
3246}
3247
3248/**
Alexander Duyck493fb302013-09-28 07:01:44 +00003249 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
3250 * @vsi: the VSI being configured
3251 * @v_idx: Index of vector to be freed
3252 *
3253 * This function frees the memory allocated to the q_vector. In addition if
3254 * NAPI is enabled it will delete any references to the NAPI struct prior
3255 * to freeing the q_vector.
3256 **/
3257static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
3258{
3259 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003260 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00003261
3262 if (!q_vector)
3263 return;
3264
3265 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003266 i40e_for_each_ring(ring, q_vector->tx)
3267 ring->q_vector = NULL;
3268
3269 i40e_for_each_ring(ring, q_vector->rx)
3270 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00003271
3272 /* only VSI w/ an associated netdev is set up w/ NAPI */
3273 if (vsi->netdev)
3274 netif_napi_del(&q_vector->napi);
3275
3276 vsi->q_vectors[v_idx] = NULL;
3277
3278 kfree_rcu(q_vector, rcu);
3279}
3280
3281/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003282 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
3283 * @vsi: the VSI being un-configured
3284 *
3285 * This frees the memory allocated to the q_vectors and
3286 * deletes references to the NAPI struct.
3287 **/
3288static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
3289{
3290 int v_idx;
3291
Alexander Duyck493fb302013-09-28 07:01:44 +00003292 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
3293 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003294}
3295
3296/**
3297 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
3298 * @pf: board private structure
3299 **/
3300static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
3301{
3302 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
3303 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3304 pci_disable_msix(pf->pdev);
3305 kfree(pf->msix_entries);
3306 pf->msix_entries = NULL;
3307 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
3308 pci_disable_msi(pf->pdev);
3309 }
3310 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
3311}
3312
3313/**
3314 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
3315 * @pf: board private structure
3316 *
3317 * We go through and clear interrupt specific resources and reset the structure
3318 * to pre-load conditions
3319 **/
3320static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
3321{
3322 int i;
3323
3324 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
3325 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
3326 if (pf->vsi[i])
3327 i40e_vsi_free_q_vectors(pf->vsi[i]);
3328 i40e_reset_interrupt_capability(pf);
3329}
3330
3331/**
3332 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
3333 * @vsi: the VSI being configured
3334 **/
3335static void i40e_napi_enable_all(struct i40e_vsi *vsi)
3336{
3337 int q_idx;
3338
3339 if (!vsi->netdev)
3340 return;
3341
3342 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003343 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003344}
3345
3346/**
3347 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
3348 * @vsi: the VSI being configured
3349 **/
3350static void i40e_napi_disable_all(struct i40e_vsi *vsi)
3351{
3352 int q_idx;
3353
3354 if (!vsi->netdev)
3355 return;
3356
3357 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003358 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003359}
3360
3361/**
3362 * i40e_quiesce_vsi - Pause a given VSI
3363 * @vsi: the VSI being paused
3364 **/
3365static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
3366{
3367 if (test_bit(__I40E_DOWN, &vsi->state))
3368 return;
3369
3370 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
3371 if (vsi->netdev && netif_running(vsi->netdev)) {
3372 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
3373 } else {
3374 set_bit(__I40E_DOWN, &vsi->state);
3375 i40e_down(vsi);
3376 }
3377}
3378
3379/**
3380 * i40e_unquiesce_vsi - Resume a given VSI
3381 * @vsi: the VSI being resumed
3382 **/
3383static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
3384{
3385 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
3386 return;
3387
3388 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
3389 if (vsi->netdev && netif_running(vsi->netdev))
3390 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
3391 else
3392 i40e_up(vsi); /* this clears the DOWN bit */
3393}
3394
3395/**
3396 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
3397 * @pf: the PF
3398 **/
3399static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
3400{
3401 int v;
3402
3403 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
3404 if (pf->vsi[v])
3405 i40e_quiesce_vsi(pf->vsi[v]);
3406 }
3407}
3408
3409/**
3410 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
3411 * @pf: the PF
3412 **/
3413static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
3414{
3415 int v;
3416
3417 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
3418 if (pf->vsi[v])
3419 i40e_unquiesce_vsi(pf->vsi[v]);
3420 }
3421}
3422
3423/**
3424 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
3425 * @dcbcfg: the corresponding DCBx configuration structure
3426 *
3427 * Return the number of TCs from given DCBx configuration
3428 **/
3429static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
3430{
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003431 u8 num_tc = 0;
3432 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003433
3434 /* Scan the ETS Config Priority Table to find
3435 * traffic class enabled for a given priority
3436 * and use the traffic class index to get the
3437 * number of traffic classes enabled
3438 */
3439 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3440 if (dcbcfg->etscfg.prioritytable[i] > num_tc)
3441 num_tc = dcbcfg->etscfg.prioritytable[i];
3442 }
3443
3444 /* Traffic class index starts from zero so
3445 * increment to return the actual count
3446 */
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003447 return num_tc + 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003448}
3449
3450/**
3451 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
3452 * @dcbcfg: the corresponding DCBx configuration structure
3453 *
3454 * Query the current DCB configuration and return the number of
3455 * traffic classes enabled from the given DCBX config
3456 **/
3457static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
3458{
3459 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
3460 u8 enabled_tc = 1;
3461 u8 i;
3462
3463 for (i = 0; i < num_tc; i++)
3464 enabled_tc |= 1 << i;
3465
3466 return enabled_tc;
3467}
3468
3469/**
3470 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
3471 * @pf: PF being queried
3472 *
3473 * Return number of traffic classes enabled for the given PF
3474 **/
3475static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
3476{
3477 struct i40e_hw *hw = &pf->hw;
3478 u8 i, enabled_tc;
3479 u8 num_tc = 0;
3480 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3481
3482 /* If DCB is not enabled then always in single TC */
3483 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3484 return 1;
3485
3486 /* MFP mode return count of enabled TCs for this PF */
3487 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
3488 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3489 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3490 if (enabled_tc & (1 << i))
3491 num_tc++;
3492 }
3493 return num_tc;
3494 }
3495
3496 /* SFP mode will be enabled for all TCs on port */
3497 return i40e_dcb_get_num_tc(dcbcfg);
3498}
3499
3500/**
3501 * i40e_pf_get_default_tc - Get bitmap for first enabled TC
3502 * @pf: PF being queried
3503 *
3504 * Return a bitmap for first enabled traffic class for this PF.
3505 **/
3506static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
3507{
3508 u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3509 u8 i = 0;
3510
3511 if (!enabled_tc)
3512 return 0x1; /* TC0 */
3513
3514 /* Find the first enabled TC */
3515 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3516 if (enabled_tc & (1 << i))
3517 break;
3518 }
3519
3520 return 1 << i;
3521}
3522
3523/**
3524 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
3525 * @pf: PF being queried
3526 *
3527 * Return a bitmap for enabled traffic classes for this PF.
3528 **/
3529static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
3530{
3531 /* If DCB is not enabled for this PF then just return default TC */
3532 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3533 return i40e_pf_get_default_tc(pf);
3534
3535 /* MFP mode will have enabled TCs set by FW */
3536 if (pf->flags & I40E_FLAG_MFP_ENABLED)
3537 return pf->hw.func_caps.enabled_tcmap;
3538
3539 /* SFP mode we want PF to be enabled for all TCs */
3540 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
3541}
3542
3543/**
3544 * i40e_vsi_get_bw_info - Query VSI BW Information
3545 * @vsi: the VSI being queried
3546 *
3547 * Returns 0 on success, negative value on failure
3548 **/
3549static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
3550{
3551 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
3552 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
3553 struct i40e_pf *pf = vsi->back;
3554 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003555 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003556 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003557 int i;
3558
3559 /* Get the VSI level BW configuration */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003560 aq_ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
3561 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003562 dev_info(&pf->pdev->dev,
3563 "couldn't get pf vsi bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003564 aq_ret, pf->hw.aq.asq_last_status);
3565 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003566 }
3567
3568 /* Get the VSI level BW configuration per TC */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003569 aq_ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
3570 NULL);
3571 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003572 dev_info(&pf->pdev->dev,
3573 "couldn't get pf vsi ets bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003574 aq_ret, pf->hw.aq.asq_last_status);
3575 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003576 }
3577
3578 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
3579 dev_info(&pf->pdev->dev,
3580 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
3581 bw_config.tc_valid_bits,
3582 bw_ets_config.tc_valid_bits);
3583 /* Still continuing */
3584 }
3585
3586 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
3587 vsi->bw_max_quanta = bw_config.max_bw;
3588 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
3589 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
3590 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3591 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
3592 vsi->bw_ets_limit_credits[i] =
3593 le16_to_cpu(bw_ets_config.credits[i]);
3594 /* 3 bits out of 4 for each TC */
3595 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
3596 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003597
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003598 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003599}
3600
3601/**
3602 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
3603 * @vsi: the VSI being configured
3604 * @enabled_tc: TC bitmap
3605 * @bw_credits: BW shared credits per TC
3606 *
3607 * Returns 0 on success, negative value on failure
3608 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003609static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003610 u8 *bw_share)
3611{
3612 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003613 i40e_status aq_ret;
3614 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003615
3616 bw_data.tc_valid_bits = enabled_tc;
3617 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3618 bw_data.tc_bw_credits[i] = bw_share[i];
3619
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003620 aq_ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
3621 NULL);
3622 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003623 dev_info(&vsi->back->pdev->dev,
3624 "%s: AQ command Config VSI BW allocation per TC failed = %d\n",
3625 __func__, vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003626 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003627 }
3628
3629 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3630 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
3631
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003632 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003633}
3634
3635/**
3636 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
3637 * @vsi: the VSI being configured
3638 * @enabled_tc: TC map to be enabled
3639 *
3640 **/
3641static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3642{
3643 struct net_device *netdev = vsi->netdev;
3644 struct i40e_pf *pf = vsi->back;
3645 struct i40e_hw *hw = &pf->hw;
3646 u8 netdev_tc = 0;
3647 int i;
3648 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3649
3650 if (!netdev)
3651 return;
3652
3653 if (!enabled_tc) {
3654 netdev_reset_tc(netdev);
3655 return;
3656 }
3657
3658 /* Set up actual enabled TCs on the VSI */
3659 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
3660 return;
3661
3662 /* set per TC queues for the VSI */
3663 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3664 /* Only set TC queues for enabled tcs
3665 *
3666 * e.g. For a VSI that has TC0 and TC3 enabled the
3667 * enabled_tc bitmap would be 0x00001001; the driver
3668 * will set the numtc for netdev as 2 that will be
3669 * referenced by the netdev layer as TC 0 and 1.
3670 */
3671 if (vsi->tc_config.enabled_tc & (1 << i))
3672 netdev_set_tc_queue(netdev,
3673 vsi->tc_config.tc_info[i].netdev_tc,
3674 vsi->tc_config.tc_info[i].qcount,
3675 vsi->tc_config.tc_info[i].qoffset);
3676 }
3677
3678 /* Assign UP2TC map for the VSI */
3679 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3680 /* Get the actual TC# for the UP */
3681 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
3682 /* Get the mapped netdev TC# for the UP */
3683 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
3684 netdev_set_prio_tc_map(netdev, i, netdev_tc);
3685 }
3686}
3687
3688/**
3689 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
3690 * @vsi: the VSI being configured
3691 * @ctxt: the ctxt buffer returned from AQ VSI update param command
3692 **/
3693static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
3694 struct i40e_vsi_context *ctxt)
3695{
3696 /* copy just the sections touched not the entire info
3697 * since not all sections are valid as returned by
3698 * update vsi params
3699 */
3700 vsi->info.mapping_flags = ctxt->info.mapping_flags;
3701 memcpy(&vsi->info.queue_mapping,
3702 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
3703 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
3704 sizeof(vsi->info.tc_mapping));
3705}
3706
3707/**
3708 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
3709 * @vsi: VSI to be configured
3710 * @enabled_tc: TC bitmap
3711 *
3712 * This configures a particular VSI for TCs that are mapped to the
3713 * given TC bitmap. It uses default bandwidth share for TCs across
3714 * VSIs to configure TC for a particular VSI.
3715 *
3716 * NOTE:
3717 * It is expected that the VSI queues have been quisced before calling
3718 * this function.
3719 **/
3720static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3721{
3722 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
3723 struct i40e_vsi_context ctxt;
3724 int ret = 0;
3725 int i;
3726
3727 /* Check if enabled_tc is same as existing or new TCs */
3728 if (vsi->tc_config.enabled_tc == enabled_tc)
3729 return ret;
3730
3731 /* Enable ETS TCs with equal BW Share for now across all VSIs */
3732 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3733 if (enabled_tc & (1 << i))
3734 bw_share[i] = 1;
3735 }
3736
3737 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
3738 if (ret) {
3739 dev_info(&vsi->back->pdev->dev,
3740 "Failed configuring TC map %d for VSI %d\n",
3741 enabled_tc, vsi->seid);
3742 goto out;
3743 }
3744
3745 /* Update Queue Pairs Mapping for currently enabled UPs */
3746 ctxt.seid = vsi->seid;
3747 ctxt.pf_num = vsi->back->hw.pf_id;
3748 ctxt.vf_num = 0;
3749 ctxt.uplink_seid = vsi->uplink_seid;
3750 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
3751 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
3752
3753 /* Update the VSI after updating the VSI queue-mapping information */
3754 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3755 if (ret) {
3756 dev_info(&vsi->back->pdev->dev,
3757 "update vsi failed, aq_err=%d\n",
3758 vsi->back->hw.aq.asq_last_status);
3759 goto out;
3760 }
3761 /* update the local VSI info with updated queue map */
3762 i40e_vsi_update_queue_map(vsi, &ctxt);
3763 vsi->info.valid_sections = 0;
3764
3765 /* Update current VSI BW information */
3766 ret = i40e_vsi_get_bw_info(vsi);
3767 if (ret) {
3768 dev_info(&vsi->back->pdev->dev,
3769 "Failed updating vsi bw info, aq_err=%d\n",
3770 vsi->back->hw.aq.asq_last_status);
3771 goto out;
3772 }
3773
3774 /* Update the netdev TC setup */
3775 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
3776out:
3777 return ret;
3778}
3779
3780/**
3781 * i40e_up_complete - Finish the last steps of bringing up a connection
3782 * @vsi: the VSI being configured
3783 **/
3784static int i40e_up_complete(struct i40e_vsi *vsi)
3785{
3786 struct i40e_pf *pf = vsi->back;
3787 int err;
3788
3789 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3790 i40e_vsi_configure_msix(vsi);
3791 else
3792 i40e_configure_msi_and_legacy(vsi);
3793
3794 /* start rings */
3795 err = i40e_vsi_control_rings(vsi, true);
3796 if (err)
3797 return err;
3798
3799 clear_bit(__I40E_DOWN, &vsi->state);
3800 i40e_napi_enable_all(vsi);
3801 i40e_vsi_enable_irq(vsi);
3802
3803 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
3804 (vsi->netdev)) {
Anjali Singhai6d779b42013-09-28 06:00:02 +00003805 netdev_info(vsi->netdev, "NIC Link is Up\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003806 netif_tx_start_all_queues(vsi->netdev);
3807 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00003808 } else if (vsi->netdev) {
3809 netdev_info(vsi->netdev, "NIC Link is Down\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003810 }
3811 i40e_service_event_schedule(pf);
3812
3813 return 0;
3814}
3815
3816/**
3817 * i40e_vsi_reinit_locked - Reset the VSI
3818 * @vsi: the VSI being configured
3819 *
3820 * Rebuild the ring structs after some configuration
3821 * has changed, e.g. MTU size.
3822 **/
3823static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
3824{
3825 struct i40e_pf *pf = vsi->back;
3826
3827 WARN_ON(in_interrupt());
3828 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
3829 usleep_range(1000, 2000);
3830 i40e_down(vsi);
3831
3832 /* Give a VF some time to respond to the reset. The
3833 * two second wait is based upon the watchdog cycle in
3834 * the VF driver.
3835 */
3836 if (vsi->type == I40E_VSI_SRIOV)
3837 msleep(2000);
3838 i40e_up(vsi);
3839 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
3840}
3841
3842/**
3843 * i40e_up - Bring the connection back up after being down
3844 * @vsi: the VSI being configured
3845 **/
3846int i40e_up(struct i40e_vsi *vsi)
3847{
3848 int err;
3849
3850 err = i40e_vsi_configure(vsi);
3851 if (!err)
3852 err = i40e_up_complete(vsi);
3853
3854 return err;
3855}
3856
3857/**
3858 * i40e_down - Shutdown the connection processing
3859 * @vsi: the VSI being stopped
3860 **/
3861void i40e_down(struct i40e_vsi *vsi)
3862{
3863 int i;
3864
3865 /* It is assumed that the caller of this function
3866 * sets the vsi->state __I40E_DOWN bit.
3867 */
3868 if (vsi->netdev) {
3869 netif_carrier_off(vsi->netdev);
3870 netif_tx_disable(vsi->netdev);
3871 }
3872 i40e_vsi_disable_irq(vsi);
3873 i40e_vsi_control_rings(vsi, false);
3874 i40e_napi_disable_all(vsi);
3875
3876 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00003877 i40e_clean_tx_ring(vsi->tx_rings[i]);
3878 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003879 }
3880}
3881
3882/**
3883 * i40e_setup_tc - configure multiple traffic classes
3884 * @netdev: net device to configure
3885 * @tc: number of traffic classes to enable
3886 **/
3887static int i40e_setup_tc(struct net_device *netdev, u8 tc)
3888{
3889 struct i40e_netdev_priv *np = netdev_priv(netdev);
3890 struct i40e_vsi *vsi = np->vsi;
3891 struct i40e_pf *pf = vsi->back;
3892 u8 enabled_tc = 0;
3893 int ret = -EINVAL;
3894 int i;
3895
3896 /* Check if DCB enabled to continue */
3897 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
3898 netdev_info(netdev, "DCB is not enabled for adapter\n");
3899 goto exit;
3900 }
3901
3902 /* Check if MFP enabled */
3903 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
3904 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
3905 goto exit;
3906 }
3907
3908 /* Check whether tc count is within enabled limit */
3909 if (tc > i40e_pf_get_num_tc(pf)) {
3910 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
3911 goto exit;
3912 }
3913
3914 /* Generate TC map for number of tc requested */
3915 for (i = 0; i < tc; i++)
3916 enabled_tc |= (1 << i);
3917
3918 /* Requesting same TC configuration as already enabled */
3919 if (enabled_tc == vsi->tc_config.enabled_tc)
3920 return 0;
3921
3922 /* Quiesce VSI queues */
3923 i40e_quiesce_vsi(vsi);
3924
3925 /* Configure VSI for enabled TCs */
3926 ret = i40e_vsi_config_tc(vsi, enabled_tc);
3927 if (ret) {
3928 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
3929 vsi->seid);
3930 goto exit;
3931 }
3932
3933 /* Unquiesce VSI */
3934 i40e_unquiesce_vsi(vsi);
3935
3936exit:
3937 return ret;
3938}
3939
3940/**
3941 * i40e_open - Called when a network interface is made active
3942 * @netdev: network interface device structure
3943 *
3944 * The open entry point is called when a network interface is made
3945 * active by the system (IFF_UP). At this point all resources needed
3946 * for transmit and receive operations are allocated, the interrupt
3947 * handler is registered with the OS, the netdev watchdog subtask is
3948 * enabled, and the stack is notified that the interface is ready.
3949 *
3950 * Returns 0 on success, negative value on failure
3951 **/
3952static int i40e_open(struct net_device *netdev)
3953{
3954 struct i40e_netdev_priv *np = netdev_priv(netdev);
3955 struct i40e_vsi *vsi = np->vsi;
3956 struct i40e_pf *pf = vsi->back;
3957 char int_name[IFNAMSIZ];
3958 int err;
3959
3960 /* disallow open during test */
3961 if (test_bit(__I40E_TESTING, &pf->state))
3962 return -EBUSY;
3963
3964 netif_carrier_off(netdev);
3965
3966 /* allocate descriptors */
3967 err = i40e_vsi_setup_tx_resources(vsi);
3968 if (err)
3969 goto err_setup_tx;
3970 err = i40e_vsi_setup_rx_resources(vsi);
3971 if (err)
3972 goto err_setup_rx;
3973
3974 err = i40e_vsi_configure(vsi);
3975 if (err)
3976 goto err_setup_rx;
3977
3978 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
3979 dev_driver_string(&pf->pdev->dev), netdev->name);
3980 err = i40e_vsi_request_irq(vsi, int_name);
3981 if (err)
3982 goto err_setup_rx;
3983
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00003984 /* Notify the stack of the actual queue counts. */
Neerav Parikhd7397642013-11-28 06:39:37 +00003985 err = netif_set_real_num_tx_queues(netdev, vsi->num_queue_pairs);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00003986 if (err)
3987 goto err_set_queues;
3988
Neerav Parikhd7397642013-11-28 06:39:37 +00003989 err = netif_set_real_num_rx_queues(netdev, vsi->num_queue_pairs);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00003990 if (err)
3991 goto err_set_queues;
3992
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003993 err = i40e_up_complete(vsi);
3994 if (err)
3995 goto err_up_complete;
3996
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00003997#ifdef CONFIG_I40E_VXLAN
3998 vxlan_get_rx_port(netdev);
3999#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004000
4001 return 0;
4002
4003err_up_complete:
4004 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004005err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004006 i40e_vsi_free_irq(vsi);
4007err_setup_rx:
4008 i40e_vsi_free_rx_resources(vsi);
4009err_setup_tx:
4010 i40e_vsi_free_tx_resources(vsi);
4011 if (vsi == pf->vsi[pf->lan_vsi])
4012 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
4013
4014 return err;
4015}
4016
4017/**
4018 * i40e_close - Disables a network interface
4019 * @netdev: network interface device structure
4020 *
4021 * The close entry point is called when an interface is de-activated
4022 * by the OS. The hardware is still under the driver's control, but
4023 * this netdev interface is disabled.
4024 *
4025 * Returns 0, this is not allowed to fail
4026 **/
4027static int i40e_close(struct net_device *netdev)
4028{
4029 struct i40e_netdev_priv *np = netdev_priv(netdev);
4030 struct i40e_vsi *vsi = np->vsi;
4031
4032 if (test_and_set_bit(__I40E_DOWN, &vsi->state))
4033 return 0;
4034
4035 i40e_down(vsi);
4036 i40e_vsi_free_irq(vsi);
4037
4038 i40e_vsi_free_tx_resources(vsi);
4039 i40e_vsi_free_rx_resources(vsi);
4040
4041 return 0;
4042}
4043
4044/**
4045 * i40e_do_reset - Start a PF or Core Reset sequence
4046 * @pf: board private structure
4047 * @reset_flags: which reset is requested
4048 *
4049 * The essential difference in resets is that the PF Reset
4050 * doesn't clear the packet buffers, doesn't reset the PE
4051 * firmware, and doesn't bother the other PFs on the chip.
4052 **/
4053void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
4054{
4055 u32 val;
4056
4057 WARN_ON(in_interrupt());
4058
4059 /* do the biggest reset indicated */
4060 if (reset_flags & (1 << __I40E_GLOBAL_RESET_REQUESTED)) {
4061
4062 /* Request a Global Reset
4063 *
4064 * This will start the chip's countdown to the actual full
4065 * chip reset event, and a warning interrupt to be sent
4066 * to all PFs, including the requestor. Our handler
4067 * for the warning interrupt will deal with the shutdown
4068 * and recovery of the switch setup.
4069 */
4070 dev_info(&pf->pdev->dev, "GlobalR requested\n");
4071 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4072 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
4073 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4074
4075 } else if (reset_flags & (1 << __I40E_CORE_RESET_REQUESTED)) {
4076
4077 /* Request a Core Reset
4078 *
4079 * Same as Global Reset, except does *not* include the MAC/PHY
4080 */
4081 dev_info(&pf->pdev->dev, "CoreR requested\n");
4082 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4083 val |= I40E_GLGEN_RTRIG_CORER_MASK;
4084 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4085 i40e_flush(&pf->hw);
4086
Shannon Nelson7823fe32013-11-16 10:00:45 +00004087 } else if (reset_flags & (1 << __I40E_EMP_RESET_REQUESTED)) {
4088
4089 /* Request a Firmware Reset
4090 *
4091 * Same as Global reset, plus restarting the
4092 * embedded firmware engine.
4093 */
4094 /* enable EMP Reset */
4095 val = rd32(&pf->hw, I40E_GLGEN_RSTENA_EMP);
4096 val |= I40E_GLGEN_RSTENA_EMP_EMP_RST_ENA_MASK;
4097 wr32(&pf->hw, I40E_GLGEN_RSTENA_EMP, val);
4098
4099 /* force the reset */
4100 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4101 val |= I40E_GLGEN_RTRIG_EMPFWR_MASK;
4102 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4103 i40e_flush(&pf->hw);
4104
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004105 } else if (reset_flags & (1 << __I40E_PF_RESET_REQUESTED)) {
4106
4107 /* Request a PF Reset
4108 *
4109 * Resets only the PF-specific registers
4110 *
4111 * This goes directly to the tear-down and rebuild of
4112 * the switch, since we need to do all the recovery as
4113 * for the Core Reset.
4114 */
4115 dev_info(&pf->pdev->dev, "PFR requested\n");
4116 i40e_handle_reset_warning(pf);
4117
4118 } else if (reset_flags & (1 << __I40E_REINIT_REQUESTED)) {
4119 int v;
4120
4121 /* Find the VSI(s) that requested a re-init */
4122 dev_info(&pf->pdev->dev,
4123 "VSI reinit requested\n");
4124 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4125 struct i40e_vsi *vsi = pf->vsi[v];
4126 if (vsi != NULL &&
4127 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
4128 i40e_vsi_reinit_locked(pf->vsi[v]);
4129 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
4130 }
4131 }
4132
4133 /* no further action needed, so return now */
4134 return;
4135 } else {
4136 dev_info(&pf->pdev->dev,
4137 "bad reset request 0x%08x\n", reset_flags);
4138 return;
4139 }
4140}
4141
4142/**
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004143 * i40e_do_reset_safe - Protected reset path for userland calls.
4144 * @pf: board private structure
4145 * @reset_flags: which reset is requested
4146 *
4147 **/
4148void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
4149{
4150 rtnl_lock();
4151 i40e_do_reset(pf, reset_flags);
4152 rtnl_unlock();
4153}
4154
4155/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004156 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
4157 * @pf: board private structure
4158 * @e: event info posted on ARQ
4159 *
4160 * Handler for LAN Queue Overflow Event generated by the firmware for PF
4161 * and VF queues
4162 **/
4163static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
4164 struct i40e_arq_event_info *e)
4165{
4166 struct i40e_aqc_lan_overflow *data =
4167 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
4168 u32 queue = le32_to_cpu(data->prtdcb_rupto);
4169 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
4170 struct i40e_hw *hw = &pf->hw;
4171 struct i40e_vf *vf;
4172 u16 vf_id;
4173
4174 dev_info(&pf->pdev->dev, "%s: Rx Queue Number = %d QTX_CTL=0x%08x\n",
4175 __func__, queue, qtx_ctl);
4176
4177 /* Queue belongs to VF, find the VF and issue VF reset */
4178 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
4179 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
4180 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
4181 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
4182 vf_id -= hw->func_caps.vf_base_id;
4183 vf = &pf->vf[vf_id];
4184 i40e_vc_notify_vf_reset(vf);
4185 /* Allow VF to process pending reset notification */
4186 msleep(20);
4187 i40e_reset_vf(vf, false);
4188 }
4189}
4190
4191/**
4192 * i40e_service_event_complete - Finish up the service event
4193 * @pf: board private structure
4194 **/
4195static void i40e_service_event_complete(struct i40e_pf *pf)
4196{
4197 BUG_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
4198
4199 /* flush memory to make sure state is correct before next watchog */
4200 smp_mb__before_clear_bit();
4201 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
4202}
4203
4204/**
4205 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
4206 * @pf: board private structure
4207 **/
4208static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
4209{
4210 if (!(pf->flags & I40E_FLAG_FDIR_REQUIRES_REINIT))
4211 return;
4212
4213 pf->flags &= ~I40E_FLAG_FDIR_REQUIRES_REINIT;
4214
4215 /* if interface is down do nothing */
4216 if (test_bit(__I40E_DOWN, &pf->state))
4217 return;
4218}
4219
4220/**
4221 * i40e_vsi_link_event - notify VSI of a link event
4222 * @vsi: vsi to be notified
4223 * @link_up: link up or down
4224 **/
4225static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
4226{
4227 if (!vsi)
4228 return;
4229
4230 switch (vsi->type) {
4231 case I40E_VSI_MAIN:
4232 if (!vsi->netdev || !vsi->netdev_registered)
4233 break;
4234
4235 if (link_up) {
4236 netif_carrier_on(vsi->netdev);
4237 netif_tx_wake_all_queues(vsi->netdev);
4238 } else {
4239 netif_carrier_off(vsi->netdev);
4240 netif_tx_stop_all_queues(vsi->netdev);
4241 }
4242 break;
4243
4244 case I40E_VSI_SRIOV:
4245 break;
4246
4247 case I40E_VSI_VMDQ2:
4248 case I40E_VSI_CTRL:
4249 case I40E_VSI_MIRROR:
4250 default:
4251 /* there is no notification for other VSIs */
4252 break;
4253 }
4254}
4255
4256/**
4257 * i40e_veb_link_event - notify elements on the veb of a link event
4258 * @veb: veb to be notified
4259 * @link_up: link up or down
4260 **/
4261static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
4262{
4263 struct i40e_pf *pf;
4264 int i;
4265
4266 if (!veb || !veb->pf)
4267 return;
4268 pf = veb->pf;
4269
4270 /* depth first... */
4271 for (i = 0; i < I40E_MAX_VEB; i++)
4272 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
4273 i40e_veb_link_event(pf->veb[i], link_up);
4274
4275 /* ... now the local VSIs */
4276 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
4277 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
4278 i40e_vsi_link_event(pf->vsi[i], link_up);
4279}
4280
4281/**
4282 * i40e_link_event - Update netif_carrier status
4283 * @pf: board private structure
4284 **/
4285static void i40e_link_event(struct i40e_pf *pf)
4286{
4287 bool new_link, old_link;
4288
4289 new_link = (pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP);
4290 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
4291
4292 if (new_link == old_link)
4293 return;
4294
Anjali Singhai6d779b42013-09-28 06:00:02 +00004295 if (!test_bit(__I40E_DOWN, &pf->vsi[pf->lan_vsi]->state))
4296 netdev_info(pf->vsi[pf->lan_vsi]->netdev,
4297 "NIC Link is %s\n", (new_link ? "Up" : "Down"));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004298
4299 /* Notify the base of the switch tree connected to
4300 * the link. Floating VEBs are not notified.
4301 */
4302 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
4303 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
4304 else
4305 i40e_vsi_link_event(pf->vsi[pf->lan_vsi], new_link);
4306
4307 if (pf->vf)
4308 i40e_vc_notify_link_state(pf);
4309}
4310
4311/**
4312 * i40e_check_hang_subtask - Check for hung queues and dropped interrupts
4313 * @pf: board private structure
4314 *
4315 * Set the per-queue flags to request a check for stuck queues in the irq
4316 * clean functions, then force interrupts to be sure the irq clean is called.
4317 **/
4318static void i40e_check_hang_subtask(struct i40e_pf *pf)
4319{
4320 int i, v;
4321
4322 /* If we're down or resetting, just bail */
4323 if (test_bit(__I40E_CONFIG_BUSY, &pf->state))
4324 return;
4325
4326 /* for each VSI/netdev
4327 * for each Tx queue
4328 * set the check flag
4329 * for each q_vector
4330 * force an interrupt
4331 */
4332 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4333 struct i40e_vsi *vsi = pf->vsi[v];
4334 int armed = 0;
4335
4336 if (!pf->vsi[v] ||
4337 test_bit(__I40E_DOWN, &vsi->state) ||
4338 (vsi->netdev && !netif_carrier_ok(vsi->netdev)))
4339 continue;
4340
4341 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004342 set_check_for_tx_hang(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004343 if (test_bit(__I40E_HANG_CHECK_ARMED,
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004344 &vsi->tx_rings[i]->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004345 armed++;
4346 }
4347
4348 if (armed) {
4349 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
4350 wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0,
4351 (I40E_PFINT_DYN_CTL0_INTENA_MASK |
4352 I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK));
4353 } else {
4354 u16 vec = vsi->base_vector - 1;
4355 u32 val = (I40E_PFINT_DYN_CTLN_INTENA_MASK |
4356 I40E_PFINT_DYN_CTLN_SWINT_TRIG_MASK);
4357 for (i = 0; i < vsi->num_q_vectors; i++, vec++)
4358 wr32(&vsi->back->hw,
4359 I40E_PFINT_DYN_CTLN(vec), val);
4360 }
4361 i40e_flush(&vsi->back->hw);
4362 }
4363 }
4364}
4365
4366/**
4367 * i40e_watchdog_subtask - Check and bring link up
4368 * @pf: board private structure
4369 **/
4370static void i40e_watchdog_subtask(struct i40e_pf *pf)
4371{
4372 int i;
4373
4374 /* if interface is down do nothing */
4375 if (test_bit(__I40E_DOWN, &pf->state) ||
4376 test_bit(__I40E_CONFIG_BUSY, &pf->state))
4377 return;
4378
4379 /* Update the stats for active netdevs so the network stack
4380 * can look at updated numbers whenever it cares to
4381 */
4382 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
4383 if (pf->vsi[i] && pf->vsi[i]->netdev)
4384 i40e_update_stats(pf->vsi[i]);
4385
4386 /* Update the stats for the active switching components */
4387 for (i = 0; i < I40E_MAX_VEB; i++)
4388 if (pf->veb[i])
4389 i40e_update_veb_stats(pf->veb[i]);
4390}
4391
4392/**
4393 * i40e_reset_subtask - Set up for resetting the device and driver
4394 * @pf: board private structure
4395 **/
4396static void i40e_reset_subtask(struct i40e_pf *pf)
4397{
4398 u32 reset_flags = 0;
4399
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004400 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004401 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
4402 reset_flags |= (1 << __I40E_REINIT_REQUESTED);
4403 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
4404 }
4405 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
4406 reset_flags |= (1 << __I40E_PF_RESET_REQUESTED);
4407 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
4408 }
4409 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
4410 reset_flags |= (1 << __I40E_CORE_RESET_REQUESTED);
4411 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
4412 }
4413 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
4414 reset_flags |= (1 << __I40E_GLOBAL_RESET_REQUESTED);
4415 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
4416 }
4417
4418 /* If there's a recovery already waiting, it takes
4419 * precedence before starting a new reset sequence.
4420 */
4421 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
4422 i40e_handle_reset_warning(pf);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004423 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004424 }
4425
4426 /* If we're already down or resetting, just bail */
4427 if (reset_flags &&
4428 !test_bit(__I40E_DOWN, &pf->state) &&
4429 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
4430 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004431
4432unlock:
4433 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004434}
4435
4436/**
4437 * i40e_handle_link_event - Handle link event
4438 * @pf: board private structure
4439 * @e: event info posted on ARQ
4440 **/
4441static void i40e_handle_link_event(struct i40e_pf *pf,
4442 struct i40e_arq_event_info *e)
4443{
4444 struct i40e_hw *hw = &pf->hw;
4445 struct i40e_aqc_get_link_status *status =
4446 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
4447 struct i40e_link_status *hw_link_info = &hw->phy.link_info;
4448
4449 /* save off old link status information */
4450 memcpy(&pf->hw.phy.link_info_old, hw_link_info,
4451 sizeof(pf->hw.phy.link_info_old));
4452
4453 /* update link status */
4454 hw_link_info->phy_type = (enum i40e_aq_phy_type)status->phy_type;
4455 hw_link_info->link_speed = (enum i40e_aq_link_speed)status->link_speed;
4456 hw_link_info->link_info = status->link_info;
4457 hw_link_info->an_info = status->an_info;
4458 hw_link_info->ext_info = status->ext_info;
4459 hw_link_info->lse_enable =
4460 le16_to_cpu(status->command_flags) &
4461 I40E_AQ_LSE_ENABLE;
4462
4463 /* process the event */
4464 i40e_link_event(pf);
4465
4466 /* Do a new status request to re-enable LSE reporting
4467 * and load new status information into the hw struct,
4468 * then see if the status changed while processing the
4469 * initial event.
4470 */
4471 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
4472 i40e_link_event(pf);
4473}
4474
4475/**
4476 * i40e_clean_adminq_subtask - Clean the AdminQ rings
4477 * @pf: board private structure
4478 **/
4479static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
4480{
4481 struct i40e_arq_event_info event;
4482 struct i40e_hw *hw = &pf->hw;
4483 u16 pending, i = 0;
4484 i40e_status ret;
4485 u16 opcode;
4486 u32 val;
4487
4488 if (!test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state))
4489 return;
4490
Mitch Williams3197ce22013-11-28 06:39:39 +00004491 event.msg_size = I40E_MAX_AQ_BUF_SIZE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004492 event.msg_buf = kzalloc(event.msg_size, GFP_KERNEL);
4493 if (!event.msg_buf)
4494 return;
4495
4496 do {
Mitch Williams2f019122013-11-28 06:39:33 +00004497 event.msg_size = I40E_MAX_AQ_BUF_SIZE; /* reinit each time */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004498 ret = i40e_clean_arq_element(hw, &event, &pending);
4499 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
4500 dev_info(&pf->pdev->dev, "No ARQ event found\n");
4501 break;
4502 } else if (ret) {
4503 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
4504 break;
4505 }
4506
4507 opcode = le16_to_cpu(event.desc.opcode);
4508 switch (opcode) {
4509
4510 case i40e_aqc_opc_get_link_status:
4511 i40e_handle_link_event(pf, &event);
4512 break;
4513 case i40e_aqc_opc_send_msg_to_pf:
4514 ret = i40e_vc_process_vf_msg(pf,
4515 le16_to_cpu(event.desc.retval),
4516 le32_to_cpu(event.desc.cookie_high),
4517 le32_to_cpu(event.desc.cookie_low),
4518 event.msg_buf,
4519 event.msg_size);
4520 break;
4521 case i40e_aqc_opc_lldp_update_mib:
4522 dev_info(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
4523 break;
4524 case i40e_aqc_opc_event_lan_overflow:
4525 dev_info(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
4526 i40e_handle_lan_overflow_event(pf, &event);
4527 break;
4528 default:
4529 dev_info(&pf->pdev->dev,
4530 "ARQ Error: Unknown event %d received\n",
4531 event.desc.opcode);
4532 break;
4533 }
4534 } while (pending && (i++ < pf->adminq_work_limit));
4535
4536 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
4537 /* re-enable Admin queue interrupt cause */
4538 val = rd32(hw, I40E_PFINT_ICR0_ENA);
4539 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4540 wr32(hw, I40E_PFINT_ICR0_ENA, val);
4541 i40e_flush(hw);
4542
4543 kfree(event.msg_buf);
4544}
4545
4546/**
4547 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
4548 * @veb: pointer to the VEB instance
4549 *
4550 * This is a recursive function that first builds the attached VSIs then
4551 * recurses in to build the next layer of VEB. We track the connections
4552 * through our own index numbers because the seid's from the HW could
4553 * change across the reset.
4554 **/
4555static int i40e_reconstitute_veb(struct i40e_veb *veb)
4556{
4557 struct i40e_vsi *ctl_vsi = NULL;
4558 struct i40e_pf *pf = veb->pf;
4559 int v, veb_idx;
4560 int ret;
4561
4562 /* build VSI that owns this VEB, temporarily attached to base VEB */
4563 for (v = 0; v < pf->hw.func_caps.num_vsis && !ctl_vsi; v++) {
4564 if (pf->vsi[v] &&
4565 pf->vsi[v]->veb_idx == veb->idx &&
4566 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
4567 ctl_vsi = pf->vsi[v];
4568 break;
4569 }
4570 }
4571 if (!ctl_vsi) {
4572 dev_info(&pf->pdev->dev,
4573 "missing owner VSI for veb_idx %d\n", veb->idx);
4574 ret = -ENOENT;
4575 goto end_reconstitute;
4576 }
4577 if (ctl_vsi != pf->vsi[pf->lan_vsi])
4578 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
4579 ret = i40e_add_vsi(ctl_vsi);
4580 if (ret) {
4581 dev_info(&pf->pdev->dev,
4582 "rebuild of owner VSI failed: %d\n", ret);
4583 goto end_reconstitute;
4584 }
4585 i40e_vsi_reset_stats(ctl_vsi);
4586
4587 /* create the VEB in the switch and move the VSI onto the VEB */
4588 ret = i40e_add_veb(veb, ctl_vsi);
4589 if (ret)
4590 goto end_reconstitute;
4591
4592 /* create the remaining VSIs attached to this VEB */
4593 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4594 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
4595 continue;
4596
4597 if (pf->vsi[v]->veb_idx == veb->idx) {
4598 struct i40e_vsi *vsi = pf->vsi[v];
4599 vsi->uplink_seid = veb->seid;
4600 ret = i40e_add_vsi(vsi);
4601 if (ret) {
4602 dev_info(&pf->pdev->dev,
4603 "rebuild of vsi_idx %d failed: %d\n",
4604 v, ret);
4605 goto end_reconstitute;
4606 }
4607 i40e_vsi_reset_stats(vsi);
4608 }
4609 }
4610
4611 /* create any VEBs attached to this VEB - RECURSION */
4612 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
4613 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
4614 pf->veb[veb_idx]->uplink_seid = veb->seid;
4615 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
4616 if (ret)
4617 break;
4618 }
4619 }
4620
4621end_reconstitute:
4622 return ret;
4623}
4624
4625/**
4626 * i40e_get_capabilities - get info about the HW
4627 * @pf: the PF struct
4628 **/
4629static int i40e_get_capabilities(struct i40e_pf *pf)
4630{
4631 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
4632 u16 data_size;
4633 int buf_len;
4634 int err;
4635
4636 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
4637 do {
4638 cap_buf = kzalloc(buf_len, GFP_KERNEL);
4639 if (!cap_buf)
4640 return -ENOMEM;
4641
4642 /* this loads the data into the hw struct for us */
4643 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
4644 &data_size,
4645 i40e_aqc_opc_list_func_capabilities,
4646 NULL);
4647 /* data loaded, buffer no longer needed */
4648 kfree(cap_buf);
4649
4650 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
4651 /* retry with a larger buffer */
4652 buf_len = data_size;
4653 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
4654 dev_info(&pf->pdev->dev,
4655 "capability discovery failed: aq=%d\n",
4656 pf->hw.aq.asq_last_status);
4657 return -ENODEV;
4658 }
4659 } while (err);
4660
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004661 if (pf->hw.revision_id == 0 && pf->hw.func_caps.npar_enable) {
4662 pf->hw.func_caps.num_msix_vectors += 1;
4663 pf->hw.func_caps.num_tx_qp =
4664 min_t(int, pf->hw.func_caps.num_tx_qp,
4665 I40E_MAX_NPAR_QPS);
4666 }
4667
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004668 if (pf->hw.debug_mask & I40E_DEBUG_USER)
4669 dev_info(&pf->pdev->dev,
4670 "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",
4671 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
4672 pf->hw.func_caps.num_msix_vectors,
4673 pf->hw.func_caps.num_msix_vectors_vf,
4674 pf->hw.func_caps.fd_filters_guaranteed,
4675 pf->hw.func_caps.fd_filters_best_effort,
4676 pf->hw.func_caps.num_tx_qp,
4677 pf->hw.func_caps.num_vsis);
4678
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004679#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
4680 + pf->hw.func_caps.num_vfs)
4681 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
4682 dev_info(&pf->pdev->dev,
4683 "got num_vsis %d, setting num_vsis to %d\n",
4684 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
4685 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
4686 }
4687
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004688 return 0;
4689}
4690
4691/**
4692 * i40e_fdir_setup - initialize the Flow Director resources
4693 * @pf: board private structure
4694 **/
4695static void i40e_fdir_setup(struct i40e_pf *pf)
4696{
4697 struct i40e_vsi *vsi;
4698 bool new_vsi = false;
4699 int err, i;
4700
Shannon Nelson958a3e32013-09-28 07:13:28 +00004701 if (!(pf->flags & (I40E_FLAG_FDIR_ENABLED |
4702 I40E_FLAG_FDIR_ATR_ENABLED)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004703 return;
4704
4705 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
4706
4707 /* find existing or make new FDIR VSI */
4708 vsi = NULL;
4709 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
4710 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR)
4711 vsi = pf->vsi[i];
4712 if (!vsi) {
4713 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR, pf->mac_seid, 0);
4714 if (!vsi) {
4715 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
4716 pf->flags &= ~I40E_FLAG_FDIR_ENABLED;
4717 return;
4718 }
4719 new_vsi = true;
4720 }
4721 WARN_ON(vsi->base_queue != I40E_FDIR_RING);
4722 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_rings);
4723
4724 err = i40e_vsi_setup_tx_resources(vsi);
4725 if (!err)
4726 err = i40e_vsi_setup_rx_resources(vsi);
4727 if (!err)
4728 err = i40e_vsi_configure(vsi);
4729 if (!err && new_vsi) {
4730 char int_name[IFNAMSIZ + 9];
4731 snprintf(int_name, sizeof(int_name) - 1, "%s-fdir",
4732 dev_driver_string(&pf->pdev->dev));
4733 err = i40e_vsi_request_irq(vsi, int_name);
4734 }
4735 if (!err)
4736 err = i40e_up_complete(vsi);
4737
4738 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
4739}
4740
4741/**
4742 * i40e_fdir_teardown - release the Flow Director resources
4743 * @pf: board private structure
4744 **/
4745static void i40e_fdir_teardown(struct i40e_pf *pf)
4746{
4747 int i;
4748
4749 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
4750 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
4751 i40e_vsi_release(pf->vsi[i]);
4752 break;
4753 }
4754 }
4755}
4756
4757/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00004758 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004759 * @pf: board private structure
4760 *
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00004761 * Close up the VFs and other things in prep for pf Reset.
4762 **/
4763static int i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004764{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004765 struct i40e_hw *hw = &pf->hw;
4766 i40e_status ret;
4767 u32 v;
4768
4769 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
4770 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00004771 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004772
4773 dev_info(&pf->pdev->dev, "Tearing down internal switch for reset\n");
4774
Anjali Singhai Jain37f0be62013-11-28 06:39:46 +00004775 if (i40e_check_asq_alive(hw))
4776 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004777
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]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00005363 for (i = 0; i < vsi->alloc_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() */
Neerav Parikhd7397642013-11-28 06:39:37 +00005381 for (i = 0; i < vsi->alloc_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_DCB_ENABLED |
5643 I40E_FLAG_SRIOV_ENABLED |
5644 I40E_FLAG_FDIR_ENABLED |
5645 I40E_FLAG_FDIR_ATR_ENABLED |
5646 I40E_FLAG_VMDQ_ENABLED);
5647
5648 /* rework the queue expectations without MSIX */
5649 i40e_determine_queue_usage(pf);
5650 }
5651 }
5652
5653 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
5654 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Shannon Nelson958a3e32013-09-28 07:13:28 +00005655 dev_info(&pf->pdev->dev, "MSIX not available, trying MSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005656 err = pci_enable_msi(pf->pdev);
5657 if (err) {
Shannon Nelson958a3e32013-09-28 07:13:28 +00005658 dev_info(&pf->pdev->dev, "MSI init failed - %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005659 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
5660 }
5661 }
5662
Shannon Nelson958a3e32013-09-28 07:13:28 +00005663 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
5664 dev_info(&pf->pdev->dev, "MSIX and MSI not available, falling back to Legacy IRQ\n");
5665
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005666 /* track first vector for misc interrupts */
5667 err = i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT-1);
5668}
5669
5670/**
5671 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
5672 * @pf: board private structure
5673 *
5674 * This sets up the handler for MSIX 0, which is used to manage the
5675 * non-queue interrupts, e.g. AdminQ and errors. This is not used
5676 * when in MSI or Legacy interrupt mode.
5677 **/
5678static int i40e_setup_misc_vector(struct i40e_pf *pf)
5679{
5680 struct i40e_hw *hw = &pf->hw;
5681 int err = 0;
5682
5683 /* Only request the irq if this is the first time through, and
5684 * not when we're rebuilding after a Reset
5685 */
5686 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
5687 err = request_irq(pf->msix_entries[0].vector,
5688 i40e_intr, 0, pf->misc_int_name, pf);
5689 if (err) {
5690 dev_info(&pf->pdev->dev,
5691 "request_irq for msix_misc failed: %d\n", err);
5692 return -EFAULT;
5693 }
5694 }
5695
5696 i40e_enable_misc_int_causes(hw);
5697
5698 /* associate no queues to the misc vector */
5699 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
5700 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
5701
5702 i40e_flush(hw);
5703
5704 i40e_irq_dynamic_enable_icr0(pf);
5705
5706 return err;
5707}
5708
5709/**
5710 * i40e_config_rss - Prepare for RSS if used
5711 * @pf: board private structure
5712 **/
5713static int i40e_config_rss(struct i40e_pf *pf)
5714{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005715 /* Set of random keys generated using kernel random number generator */
5716 static const u32 seed[I40E_PFQF_HKEY_MAX_INDEX + 1] = {0x41b01687,
5717 0x183cfd8c, 0xce880440, 0x580cbc3c, 0x35897377,
5718 0x328b25e1, 0x4fa98922, 0xb7d90c14, 0xd5bad70d,
5719 0xcd15a2c1, 0xe8580225, 0x4a1e9d11, 0xfe5731be};
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00005720 struct i40e_hw *hw = &pf->hw;
5721 u32 lut = 0;
5722 int i, j;
5723 u64 hena;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005724
5725 /* Fill out hash function seed */
5726 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
5727 wr32(hw, I40E_PFQF_HKEY(i), seed[i]);
5728
5729 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
5730 hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
5731 ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
Mitch Williams12dc4fe2013-11-28 06:39:32 +00005732 hena |= I40E_DEFAULT_RSS_HENA;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005733 wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
5734 wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
5735
5736 /* Populate the LUT with max no. of queues in round robin fashion */
5737 for (i = 0, j = 0; i < pf->hw.func_caps.rss_table_size; i++, j++) {
5738
5739 /* The assumption is that lan qp count will be the highest
5740 * qp count for any PF VSI that needs RSS.
5741 * If multiple VSIs need RSS support, all the qp counts
5742 * for those VSIs should be a power of 2 for RSS to work.
5743 * If LAN VSI is the only consumer for RSS then this requirement
5744 * is not necessary.
5745 */
5746 if (j == pf->rss_size)
5747 j = 0;
5748 /* lut = 4-byte sliding window of 4 lut entries */
5749 lut = (lut << 8) | (j &
5750 ((0x1 << pf->hw.func_caps.rss_table_entry_width) - 1));
5751 /* On i = 3, we have 4 entries in lut; write to the register */
5752 if ((i & 3) == 3)
5753 wr32(hw, I40E_PFQF_HLUT(i >> 2), lut);
5754 }
5755 i40e_flush(hw);
5756
5757 return 0;
5758}
5759
5760/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00005761 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
5762 * @pf: board private structure
5763 * @queue_count: the requested queue count for rss.
5764 *
5765 * returns 0 if rss is not enabled, if enabled returns the final rss queue
5766 * count which may be different from the requested queue count.
5767 **/
5768int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
5769{
5770 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
5771 return 0;
5772
5773 queue_count = min_t(int, queue_count, pf->rss_size_max);
5774 queue_count = rounddown_pow_of_two(queue_count);
5775
5776 if (queue_count != pf->rss_size) {
5777 if (pf->queues_left < (queue_count - pf->rss_size)) {
5778 dev_info(&pf->pdev->dev,
5779 "Not enough queues to do RSS on %d queues: remaining queues %d\n",
5780 queue_count, pf->queues_left);
5781 return pf->rss_size;
5782 }
5783 i40e_prep_for_reset(pf);
5784
5785 pf->num_lan_qps += (queue_count - pf->rss_size);
5786 pf->queues_left -= (queue_count - pf->rss_size);
5787 pf->rss_size = queue_count;
5788
5789 i40e_reset_and_rebuild(pf, true);
5790 i40e_config_rss(pf);
5791 }
5792 dev_info(&pf->pdev->dev, "RSS count: %d\n", pf->rss_size);
5793 return pf->rss_size;
5794}
5795
5796/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005797 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
5798 * @pf: board private structure to initialize
5799 *
5800 * i40e_sw_init initializes the Adapter private data structure.
5801 * Fields are initialized based on PCI device information and
5802 * OS network device settings (MTU size).
5803 **/
5804static int i40e_sw_init(struct i40e_pf *pf)
5805{
5806 int err = 0;
5807 int size;
5808
5809 pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
5810 (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
Shannon Nelson27599972013-11-16 10:00:43 +00005811 pf->hw.debug_mask = pf->msg_enable | I40E_DEBUG_DIAG;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005812 if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
5813 if (I40E_DEBUG_USER & debug)
5814 pf->hw.debug_mask = debug;
5815 pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
5816 I40E_DEFAULT_MSG_ENABLE);
5817 }
5818
5819 /* Set default capability flags */
5820 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
5821 I40E_FLAG_MSI_ENABLED |
5822 I40E_FLAG_MSIX_ENABLED |
5823 I40E_FLAG_RX_PS_ENABLED |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005824 I40E_FLAG_RX_1BUF_ENABLED;
5825
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00005826 /* Depending on PF configurations, it is possible that the RSS
5827 * maximum might end up larger than the available queues
5828 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005829 pf->rss_size_max = 0x1 << pf->hw.func_caps.rss_table_entry_width;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00005830 pf->rss_size_max = min_t(int, pf->rss_size_max,
5831 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005832 if (pf->hw.func_caps.rss) {
5833 pf->flags |= I40E_FLAG_RSS_ENABLED;
Jesse Brandeburgbf051a32013-11-26 10:49:17 +00005834 pf->rss_size = min_t(int, pf->rss_size_max, num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005835 } else {
5836 pf->rss_size = 1;
5837 }
5838
5839 if (pf->hw.func_caps.dcb)
5840 pf->num_tc_qps = I40E_DEFAULT_QUEUES_PER_TC;
5841 else
5842 pf->num_tc_qps = 0;
5843
5844 if (pf->hw.func_caps.fd) {
5845 /* FW/NVM is not yet fixed in this regard */
5846 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
5847 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
5848 pf->flags |= I40E_FLAG_FDIR_ATR_ENABLED;
5849 dev_info(&pf->pdev->dev,
5850 "Flow Director ATR mode Enabled\n");
5851 pf->flags |= I40E_FLAG_FDIR_ENABLED;
5852 dev_info(&pf->pdev->dev,
5853 "Flow Director Side Band mode Enabled\n");
5854 pf->fdir_pf_filter_count =
5855 pf->hw.func_caps.fd_filters_guaranteed;
5856 }
5857 } else {
5858 pf->fdir_pf_filter_count = 0;
5859 }
5860
5861 if (pf->hw.func_caps.vmdq) {
5862 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
5863 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
5864 pf->num_vmdq_qps = I40E_DEFAULT_QUEUES_PER_VMDQ;
5865 }
5866
5867 /* MFP mode enabled */
5868 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.mfp_mode_1) {
5869 pf->flags |= I40E_FLAG_MFP_ENABLED;
5870 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
5871 }
5872
5873#ifdef CONFIG_PCI_IOV
5874 if (pf->hw.func_caps.num_vfs) {
5875 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
5876 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
5877 pf->num_req_vfs = min_t(int,
5878 pf->hw.func_caps.num_vfs,
5879 I40E_MAX_VF_COUNT);
Anjali Singhai Jain4a38d092013-11-20 10:03:00 +00005880 dev_info(&pf->pdev->dev,
5881 "Number of VFs being requested for PF[%d] = %d\n",
5882 pf->hw.pf_id, pf->num_req_vfs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005883 }
5884#endif /* CONFIG_PCI_IOV */
5885 pf->eeprom_version = 0xDEAD;
5886 pf->lan_veb = I40E_NO_VEB;
5887 pf->lan_vsi = I40E_NO_VSI;
5888
5889 /* set up queue assignment tracking */
5890 size = sizeof(struct i40e_lump_tracking)
5891 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
5892 pf->qp_pile = kzalloc(size, GFP_KERNEL);
5893 if (!pf->qp_pile) {
5894 err = -ENOMEM;
5895 goto sw_init_done;
5896 }
5897 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
5898 pf->qp_pile->search_hint = 0;
5899
5900 /* set up vector assignment tracking */
5901 size = sizeof(struct i40e_lump_tracking)
5902 + (sizeof(u16) * pf->hw.func_caps.num_msix_vectors);
5903 pf->irq_pile = kzalloc(size, GFP_KERNEL);
5904 if (!pf->irq_pile) {
5905 kfree(pf->qp_pile);
5906 err = -ENOMEM;
5907 goto sw_init_done;
5908 }
5909 pf->irq_pile->num_entries = pf->hw.func_caps.num_msix_vectors;
5910 pf->irq_pile->search_hint = 0;
5911
5912 mutex_init(&pf->switch_mutex);
5913
5914sw_init_done:
5915 return err;
5916}
5917
5918/**
5919 * i40e_set_features - set the netdev feature flags
5920 * @netdev: ptr to the netdev being adjusted
5921 * @features: the feature set that the stack is suggesting
5922 **/
5923static int i40e_set_features(struct net_device *netdev,
5924 netdev_features_t features)
5925{
5926 struct i40e_netdev_priv *np = netdev_priv(netdev);
5927 struct i40e_vsi *vsi = np->vsi;
5928
5929 if (features & NETIF_F_HW_VLAN_CTAG_RX)
5930 i40e_vlan_stripping_enable(vsi);
5931 else
5932 i40e_vlan_stripping_disable(vsi);
5933
5934 return 0;
5935}
5936
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005937#ifdef CONFIG_I40E_VXLAN
5938/**
5939 * i40e_get_vxlan_port_idx - Lookup a possibly offloaded for Rx UDP port
5940 * @pf: board private structure
5941 * @port: The UDP port to look up
5942 *
5943 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
5944 **/
5945static u8 i40e_get_vxlan_port_idx(struct i40e_pf *pf, __be16 port)
5946{
5947 u8 i;
5948
5949 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
5950 if (pf->vxlan_ports[i] == port)
5951 return i;
5952 }
5953
5954 return i;
5955}
5956
5957/**
5958 * i40e_add_vxlan_port - Get notifications about VXLAN ports that come up
5959 * @netdev: This physical port's netdev
5960 * @sa_family: Socket Family that VXLAN is notifying us about
5961 * @port: New UDP port number that VXLAN started listening to
5962 **/
5963static void i40e_add_vxlan_port(struct net_device *netdev,
5964 sa_family_t sa_family, __be16 port)
5965{
5966 struct i40e_netdev_priv *np = netdev_priv(netdev);
5967 struct i40e_vsi *vsi = np->vsi;
5968 struct i40e_pf *pf = vsi->back;
5969 u8 next_idx;
5970 u8 idx;
5971
5972 if (sa_family == AF_INET6)
5973 return;
5974
5975 idx = i40e_get_vxlan_port_idx(pf, port);
5976
5977 /* Check if port already exists */
5978 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
5979 netdev_info(netdev, "Port %d already offloaded\n", ntohs(port));
5980 return;
5981 }
5982
5983 /* Now check if there is space to add the new port */
5984 next_idx = i40e_get_vxlan_port_idx(pf, 0);
5985
5986 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
5987 netdev_info(netdev, "Maximum number of UDP ports reached, not adding port %d\n",
5988 ntohs(port));
5989 return;
5990 }
5991
5992 /* New port: add it and mark its index in the bitmap */
5993 pf->vxlan_ports[next_idx] = port;
5994 pf->pending_vxlan_bitmap |= (1 << next_idx);
5995
5996 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
5997}
5998
5999/**
6000 * i40e_del_vxlan_port - Get notifications about VXLAN ports that go away
6001 * @netdev: This physical port's netdev
6002 * @sa_family: Socket Family that VXLAN is notifying us about
6003 * @port: UDP port number that VXLAN stopped listening to
6004 **/
6005static void i40e_del_vxlan_port(struct net_device *netdev,
6006 sa_family_t sa_family, __be16 port)
6007{
6008 struct i40e_netdev_priv *np = netdev_priv(netdev);
6009 struct i40e_vsi *vsi = np->vsi;
6010 struct i40e_pf *pf = vsi->back;
6011 u8 idx;
6012
6013 if (sa_family == AF_INET6)
6014 return;
6015
6016 idx = i40e_get_vxlan_port_idx(pf, port);
6017
6018 /* Check if port already exists */
6019 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6020 /* if port exists, set it to 0 (mark for deletion)
6021 * and make it pending
6022 */
6023 pf->vxlan_ports[idx] = 0;
6024
6025 pf->pending_vxlan_bitmap |= (1 << idx);
6026
6027 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
6028 } else {
6029 netdev_warn(netdev, "Port %d was not found, not deleting\n",
6030 ntohs(port));
6031 }
6032}
6033
6034#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006035static const struct net_device_ops i40e_netdev_ops = {
6036 .ndo_open = i40e_open,
6037 .ndo_stop = i40e_close,
6038 .ndo_start_xmit = i40e_lan_xmit_frame,
6039 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
6040 .ndo_set_rx_mode = i40e_set_rx_mode,
6041 .ndo_validate_addr = eth_validate_addr,
6042 .ndo_set_mac_address = i40e_set_mac,
6043 .ndo_change_mtu = i40e_change_mtu,
6044 .ndo_tx_timeout = i40e_tx_timeout,
6045 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
6046 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
6047#ifdef CONFIG_NET_POLL_CONTROLLER
6048 .ndo_poll_controller = i40e_netpoll,
6049#endif
6050 .ndo_setup_tc = i40e_setup_tc,
6051 .ndo_set_features = i40e_set_features,
6052 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
6053 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
6054 .ndo_set_vf_tx_rate = i40e_ndo_set_vf_bw,
6055 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006056#ifdef CONFIG_I40E_VXLAN
6057 .ndo_add_vxlan_port = i40e_add_vxlan_port,
6058 .ndo_del_vxlan_port = i40e_del_vxlan_port,
6059#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006060};
6061
6062/**
6063 * i40e_config_netdev - Setup the netdev flags
6064 * @vsi: the VSI being configured
6065 *
6066 * Returns 0 on success, negative value on failure
6067 **/
6068static int i40e_config_netdev(struct i40e_vsi *vsi)
6069{
Greg Rose1a103702013-11-28 06:42:39 +00006070 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006071 struct i40e_pf *pf = vsi->back;
6072 struct i40e_hw *hw = &pf->hw;
6073 struct i40e_netdev_priv *np;
6074 struct net_device *netdev;
6075 u8 mac_addr[ETH_ALEN];
6076 int etherdev_size;
6077
6078 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006079 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006080 if (!netdev)
6081 return -ENOMEM;
6082
6083 vsi->netdev = netdev;
6084 np = netdev_priv(netdev);
6085 np->vsi = vsi;
6086
6087 netdev->hw_enc_features = NETIF_F_IP_CSUM |
6088 NETIF_F_GSO_UDP_TUNNEL |
6089 NETIF_F_TSO |
6090 NETIF_F_SG;
6091
6092 netdev->features = NETIF_F_SG |
6093 NETIF_F_IP_CSUM |
6094 NETIF_F_SCTP_CSUM |
6095 NETIF_F_HIGHDMA |
6096 NETIF_F_GSO_UDP_TUNNEL |
6097 NETIF_F_HW_VLAN_CTAG_TX |
6098 NETIF_F_HW_VLAN_CTAG_RX |
6099 NETIF_F_HW_VLAN_CTAG_FILTER |
6100 NETIF_F_IPV6_CSUM |
6101 NETIF_F_TSO |
6102 NETIF_F_TSO6 |
6103 NETIF_F_RXCSUM |
6104 NETIF_F_RXHASH |
6105 0;
6106
6107 /* copy netdev features into list of user selectable features */
6108 netdev->hw_features |= netdev->features;
6109
6110 if (vsi->type == I40E_VSI_MAIN) {
6111 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
6112 memcpy(mac_addr, hw->mac.perm_addr, ETH_ALEN);
6113 } else {
6114 /* relate the VSI_VMDQ name to the VSI_MAIN name */
6115 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
6116 pf->vsi[pf->lan_vsi]->netdev->name);
6117 random_ether_addr(mac_addr);
6118 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
6119 }
Greg Rose1a103702013-11-28 06:42:39 +00006120 i40e_add_filter(vsi, brdcast, I40E_VLAN_ANY, false, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006121
6122 memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
6123 memcpy(netdev->perm_addr, mac_addr, ETH_ALEN);
6124 /* vlan gets same features (except vlan offload)
6125 * after any tweaks for specific VSI types
6126 */
6127 netdev->vlan_features = netdev->features & ~(NETIF_F_HW_VLAN_CTAG_TX |
6128 NETIF_F_HW_VLAN_CTAG_RX |
6129 NETIF_F_HW_VLAN_CTAG_FILTER);
6130 netdev->priv_flags |= IFF_UNICAST_FLT;
6131 netdev->priv_flags |= IFF_SUPP_NOFCS;
6132 /* Setup netdev TC information */
6133 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
6134
6135 netdev->netdev_ops = &i40e_netdev_ops;
6136 netdev->watchdog_timeo = 5 * HZ;
6137 i40e_set_ethtool_ops(netdev);
6138
6139 return 0;
6140}
6141
6142/**
6143 * i40e_vsi_delete - Delete a VSI from the switch
6144 * @vsi: the VSI being removed
6145 *
6146 * Returns 0 on success, negative value on failure
6147 **/
6148static void i40e_vsi_delete(struct i40e_vsi *vsi)
6149{
6150 /* remove default VSI is not allowed */
6151 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
6152 return;
6153
6154 /* there is no HW VSI for FDIR */
6155 if (vsi->type == I40E_VSI_FDIR)
6156 return;
6157
6158 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
6159 return;
6160}
6161
6162/**
6163 * i40e_add_vsi - Add a VSI to the switch
6164 * @vsi: the VSI being configured
6165 *
6166 * This initializes a VSI context depending on the VSI type to be added and
6167 * passes it down to the add_vsi aq command.
6168 **/
6169static int i40e_add_vsi(struct i40e_vsi *vsi)
6170{
6171 int ret = -ENODEV;
6172 struct i40e_mac_filter *f, *ftmp;
6173 struct i40e_pf *pf = vsi->back;
6174 struct i40e_hw *hw = &pf->hw;
6175 struct i40e_vsi_context ctxt;
6176 u8 enabled_tc = 0x1; /* TC0 enabled */
6177 int f_count = 0;
6178
6179 memset(&ctxt, 0, sizeof(ctxt));
6180 switch (vsi->type) {
6181 case I40E_VSI_MAIN:
6182 /* The PF's main VSI is already setup as part of the
6183 * device initialization, so we'll not bother with
6184 * the add_vsi call, but we will retrieve the current
6185 * VSI context.
6186 */
6187 ctxt.seid = pf->main_vsi_seid;
6188 ctxt.pf_num = pf->hw.pf_id;
6189 ctxt.vf_num = 0;
6190 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6191 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6192 if (ret) {
6193 dev_info(&pf->pdev->dev,
6194 "couldn't get pf vsi config, err %d, aq_err %d\n",
6195 ret, pf->hw.aq.asq_last_status);
6196 return -ENOENT;
6197 }
6198 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
6199 vsi->info.valid_sections = 0;
6200
6201 vsi->seid = ctxt.seid;
6202 vsi->id = ctxt.vsi_number;
6203
6204 enabled_tc = i40e_pf_get_tc_map(pf);
6205
6206 /* MFP mode setup queue map and update VSI */
6207 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
6208 memset(&ctxt, 0, sizeof(ctxt));
6209 ctxt.seid = pf->main_vsi_seid;
6210 ctxt.pf_num = pf->hw.pf_id;
6211 ctxt.vf_num = 0;
6212 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
6213 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
6214 if (ret) {
6215 dev_info(&pf->pdev->dev,
6216 "update vsi failed, aq_err=%d\n",
6217 pf->hw.aq.asq_last_status);
6218 ret = -ENOENT;
6219 goto err;
6220 }
6221 /* update the local VSI info queue map */
6222 i40e_vsi_update_queue_map(vsi, &ctxt);
6223 vsi->info.valid_sections = 0;
6224 } else {
6225 /* Default/Main VSI is only enabled for TC0
6226 * reconfigure it to enable all TCs that are
6227 * available on the port in SFP mode.
6228 */
6229 ret = i40e_vsi_config_tc(vsi, enabled_tc);
6230 if (ret) {
6231 dev_info(&pf->pdev->dev,
6232 "failed to configure TCs for main VSI tc_map 0x%08x, err %d, aq_err %d\n",
6233 enabled_tc, ret,
6234 pf->hw.aq.asq_last_status);
6235 ret = -ENOENT;
6236 }
6237 }
6238 break;
6239
6240 case I40E_VSI_FDIR:
6241 /* no queue mapping or actual HW VSI needed */
6242 vsi->info.valid_sections = 0;
6243 vsi->seid = 0;
6244 vsi->id = 0;
6245 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
6246 return 0;
6247 break;
6248
6249 case I40E_VSI_VMDQ2:
6250 ctxt.pf_num = hw->pf_id;
6251 ctxt.vf_num = 0;
6252 ctxt.uplink_seid = vsi->uplink_seid;
6253 ctxt.connection_type = 0x1; /* regular data port */
6254 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6255
6256 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6257
6258 /* This VSI is connected to VEB so the switch_id
6259 * should be set to zero by default.
6260 */
6261 ctxt.info.switch_id = 0;
6262 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
6263 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6264
6265 /* Setup the VSI tx/rx queue map for TC0 only for now */
6266 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
6267 break;
6268
6269 case I40E_VSI_SRIOV:
6270 ctxt.pf_num = hw->pf_id;
6271 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
6272 ctxt.uplink_seid = vsi->uplink_seid;
6273 ctxt.connection_type = 0x1; /* regular data port */
6274 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
6275
6276 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6277
6278 /* This VSI is connected to VEB so the switch_id
6279 * should be set to zero by default.
6280 */
6281 ctxt.info.switch_id = cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6282
6283 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
6284 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
6285 /* Setup the VSI tx/rx queue map for TC0 only for now */
6286 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
6287 break;
6288
6289 default:
6290 return -ENODEV;
6291 }
6292
6293 if (vsi->type != I40E_VSI_MAIN) {
6294 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6295 if (ret) {
6296 dev_info(&vsi->back->pdev->dev,
6297 "add vsi failed, aq_err=%d\n",
6298 vsi->back->hw.aq.asq_last_status);
6299 ret = -ENOENT;
6300 goto err;
6301 }
6302 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
6303 vsi->info.valid_sections = 0;
6304 vsi->seid = ctxt.seid;
6305 vsi->id = ctxt.vsi_number;
6306 }
6307
6308 /* If macvlan filters already exist, force them to get loaded */
6309 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
6310 f->changed = true;
6311 f_count++;
6312 }
6313 if (f_count) {
6314 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
6315 pf->flags |= I40E_FLAG_FILTER_SYNC;
6316 }
6317
6318 /* Update VSI BW information */
6319 ret = i40e_vsi_get_bw_info(vsi);
6320 if (ret) {
6321 dev_info(&pf->pdev->dev,
6322 "couldn't get vsi bw info, err %d, aq_err %d\n",
6323 ret, pf->hw.aq.asq_last_status);
6324 /* VSI is already added so not tearing that up */
6325 ret = 0;
6326 }
6327
6328err:
6329 return ret;
6330}
6331
6332/**
6333 * i40e_vsi_release - Delete a VSI and free its resources
6334 * @vsi: the VSI being removed
6335 *
6336 * Returns 0 on success or < 0 on error
6337 **/
6338int i40e_vsi_release(struct i40e_vsi *vsi)
6339{
6340 struct i40e_mac_filter *f, *ftmp;
6341 struct i40e_veb *veb = NULL;
6342 struct i40e_pf *pf;
6343 u16 uplink_seid;
6344 int i, n;
6345
6346 pf = vsi->back;
6347
6348 /* release of a VEB-owner or last VSI is not allowed */
6349 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
6350 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
6351 vsi->seid, vsi->uplink_seid);
6352 return -ENODEV;
6353 }
6354 if (vsi == pf->vsi[pf->lan_vsi] &&
6355 !test_bit(__I40E_DOWN, &pf->state)) {
6356 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
6357 return -ENODEV;
6358 }
6359
6360 uplink_seid = vsi->uplink_seid;
6361 if (vsi->type != I40E_VSI_SRIOV) {
6362 if (vsi->netdev_registered) {
6363 vsi->netdev_registered = false;
6364 if (vsi->netdev) {
6365 /* results in a call to i40e_close() */
6366 unregister_netdev(vsi->netdev);
6367 free_netdev(vsi->netdev);
6368 vsi->netdev = NULL;
6369 }
6370 } else {
6371 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
6372 i40e_down(vsi);
6373 i40e_vsi_free_irq(vsi);
6374 i40e_vsi_free_tx_resources(vsi);
6375 i40e_vsi_free_rx_resources(vsi);
6376 }
6377 i40e_vsi_disable_irq(vsi);
6378 }
6379
6380 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
6381 i40e_del_filter(vsi, f->macaddr, f->vlan,
6382 f->is_vf, f->is_netdev);
6383 i40e_sync_vsi_filters(vsi);
6384
6385 i40e_vsi_delete(vsi);
6386 i40e_vsi_free_q_vectors(vsi);
6387 i40e_vsi_clear_rings(vsi);
6388 i40e_vsi_clear(vsi);
6389
6390 /* If this was the last thing on the VEB, except for the
6391 * controlling VSI, remove the VEB, which puts the controlling
6392 * VSI onto the next level down in the switch.
6393 *
6394 * Well, okay, there's one more exception here: don't remove
6395 * the orphan VEBs yet. We'll wait for an explicit remove request
6396 * from up the network stack.
6397 */
6398 for (n = 0, i = 0; i < pf->hw.func_caps.num_vsis; i++) {
6399 if (pf->vsi[i] &&
6400 pf->vsi[i]->uplink_seid == uplink_seid &&
6401 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
6402 n++; /* count the VSIs */
6403 }
6404 }
6405 for (i = 0; i < I40E_MAX_VEB; i++) {
6406 if (!pf->veb[i])
6407 continue;
6408 if (pf->veb[i]->uplink_seid == uplink_seid)
6409 n++; /* count the VEBs */
6410 if (pf->veb[i]->seid == uplink_seid)
6411 veb = pf->veb[i];
6412 }
6413 if (n == 0 && veb && veb->uplink_seid != 0)
6414 i40e_veb_release(veb);
6415
6416 return 0;
6417}
6418
6419/**
6420 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
6421 * @vsi: ptr to the VSI
6422 *
6423 * This should only be called after i40e_vsi_mem_alloc() which allocates the
6424 * corresponding SW VSI structure and initializes num_queue_pairs for the
6425 * newly allocated VSI.
6426 *
6427 * Returns 0 on success or negative on failure
6428 **/
6429static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
6430{
6431 int ret = -ENOENT;
6432 struct i40e_pf *pf = vsi->back;
6433
Alexander Duyck493fb302013-09-28 07:01:44 +00006434 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006435 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
6436 vsi->seid);
6437 return -EEXIST;
6438 }
6439
6440 if (vsi->base_vector) {
6441 dev_info(&pf->pdev->dev,
6442 "VSI %d has non-zero base vector %d\n",
6443 vsi->seid, vsi->base_vector);
6444 return -EEXIST;
6445 }
6446
6447 ret = i40e_alloc_q_vectors(vsi);
6448 if (ret) {
6449 dev_info(&pf->pdev->dev,
6450 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
6451 vsi->num_q_vectors, vsi->seid, ret);
6452 vsi->num_q_vectors = 0;
6453 goto vector_setup_out;
6454 }
6455
Shannon Nelson958a3e32013-09-28 07:13:28 +00006456 if (vsi->num_q_vectors)
6457 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
6458 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006459 if (vsi->base_vector < 0) {
6460 dev_info(&pf->pdev->dev,
6461 "failed to get q tracking for VSI %d, err=%d\n",
6462 vsi->seid, vsi->base_vector);
6463 i40e_vsi_free_q_vectors(vsi);
6464 ret = -ENOENT;
6465 goto vector_setup_out;
6466 }
6467
6468vector_setup_out:
6469 return ret;
6470}
6471
6472/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006473 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
6474 * @vsi: pointer to the vsi.
6475 *
6476 * This re-allocates a vsi's queue resources.
6477 *
6478 * Returns pointer to the successfully allocated and configured VSI sw struct
6479 * on success, otherwise returns NULL on failure.
6480 **/
6481static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
6482{
6483 struct i40e_pf *pf = vsi->back;
6484 u8 enabled_tc;
6485 int ret;
6486
6487 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
6488 i40e_vsi_clear_rings(vsi);
6489
6490 i40e_vsi_free_arrays(vsi, false);
6491 i40e_set_num_rings_in_vsi(vsi);
6492 ret = i40e_vsi_alloc_arrays(vsi, false);
6493 if (ret)
6494 goto err_vsi;
6495
6496 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
6497 if (ret < 0) {
6498 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
6499 vsi->seid, ret);
6500 goto err_vsi;
6501 }
6502 vsi->base_queue = ret;
6503
6504 /* Update the FW view of the VSI. Force a reset of TC and queue
6505 * layout configurations.
6506 */
6507 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
6508 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
6509 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
6510 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
6511
6512 /* assign it some queues */
6513 ret = i40e_alloc_rings(vsi);
6514 if (ret)
6515 goto err_rings;
6516
6517 /* map all of the rings to the q_vectors */
6518 i40e_vsi_map_rings_to_vectors(vsi);
6519 return vsi;
6520
6521err_rings:
6522 i40e_vsi_free_q_vectors(vsi);
6523 if (vsi->netdev_registered) {
6524 vsi->netdev_registered = false;
6525 unregister_netdev(vsi->netdev);
6526 free_netdev(vsi->netdev);
6527 vsi->netdev = NULL;
6528 }
6529 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
6530err_vsi:
6531 i40e_vsi_clear(vsi);
6532 return NULL;
6533}
6534
6535/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006536 * i40e_vsi_setup - Set up a VSI by a given type
6537 * @pf: board private structure
6538 * @type: VSI type
6539 * @uplink_seid: the switch element to link to
6540 * @param1: usage depends upon VSI type. For VF types, indicates VF id
6541 *
6542 * This allocates the sw VSI structure and its queue resources, then add a VSI
6543 * to the identified VEB.
6544 *
6545 * Returns pointer to the successfully allocated and configure VSI sw struct on
6546 * success, otherwise returns NULL on failure.
6547 **/
6548struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
6549 u16 uplink_seid, u32 param1)
6550{
6551 struct i40e_vsi *vsi = NULL;
6552 struct i40e_veb *veb = NULL;
6553 int ret, i;
6554 int v_idx;
6555
6556 /* The requested uplink_seid must be either
6557 * - the PF's port seid
6558 * no VEB is needed because this is the PF
6559 * or this is a Flow Director special case VSI
6560 * - seid of an existing VEB
6561 * - seid of a VSI that owns an existing VEB
6562 * - seid of a VSI that doesn't own a VEB
6563 * a new VEB is created and the VSI becomes the owner
6564 * - seid of the PF VSI, which is what creates the first VEB
6565 * this is a special case of the previous
6566 *
6567 * Find which uplink_seid we were given and create a new VEB if needed
6568 */
6569 for (i = 0; i < I40E_MAX_VEB; i++) {
6570 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
6571 veb = pf->veb[i];
6572 break;
6573 }
6574 }
6575
6576 if (!veb && uplink_seid != pf->mac_seid) {
6577
6578 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
6579 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
6580 vsi = pf->vsi[i];
6581 break;
6582 }
6583 }
6584 if (!vsi) {
6585 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
6586 uplink_seid);
6587 return NULL;
6588 }
6589
6590 if (vsi->uplink_seid == pf->mac_seid)
6591 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
6592 vsi->tc_config.enabled_tc);
6593 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
6594 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
6595 vsi->tc_config.enabled_tc);
6596
6597 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
6598 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
6599 veb = pf->veb[i];
6600 }
6601 if (!veb) {
6602 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
6603 return NULL;
6604 }
6605
6606 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
6607 uplink_seid = veb->seid;
6608 }
6609
6610 /* get vsi sw struct */
6611 v_idx = i40e_vsi_mem_alloc(pf, type);
6612 if (v_idx < 0)
6613 goto err_alloc;
6614 vsi = pf->vsi[v_idx];
6615 vsi->type = type;
6616 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
6617
6618 if (type == I40E_VSI_MAIN)
6619 pf->lan_vsi = v_idx;
6620 else if (type == I40E_VSI_SRIOV)
6621 vsi->vf_id = param1;
6622 /* assign it some queues */
6623 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
6624 if (ret < 0) {
6625 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
6626 vsi->seid, ret);
6627 goto err_vsi;
6628 }
6629 vsi->base_queue = ret;
6630
6631 /* get a VSI from the hardware */
6632 vsi->uplink_seid = uplink_seid;
6633 ret = i40e_add_vsi(vsi);
6634 if (ret)
6635 goto err_vsi;
6636
6637 switch (vsi->type) {
6638 /* setup the netdev if needed */
6639 case I40E_VSI_MAIN:
6640 case I40E_VSI_VMDQ2:
6641 ret = i40e_config_netdev(vsi);
6642 if (ret)
6643 goto err_netdev;
6644 ret = register_netdev(vsi->netdev);
6645 if (ret)
6646 goto err_netdev;
6647 vsi->netdev_registered = true;
6648 netif_carrier_off(vsi->netdev);
6649 /* fall through */
6650
6651 case I40E_VSI_FDIR:
6652 /* set up vectors and rings if needed */
6653 ret = i40e_vsi_setup_vectors(vsi);
6654 if (ret)
6655 goto err_msix;
6656
6657 ret = i40e_alloc_rings(vsi);
6658 if (ret)
6659 goto err_rings;
6660
6661 /* map all of the rings to the q_vectors */
6662 i40e_vsi_map_rings_to_vectors(vsi);
6663
6664 i40e_vsi_reset_stats(vsi);
6665 break;
6666
6667 default:
6668 /* no netdev or rings for the other VSI types */
6669 break;
6670 }
6671
6672 return vsi;
6673
6674err_rings:
6675 i40e_vsi_free_q_vectors(vsi);
6676err_msix:
6677 if (vsi->netdev_registered) {
6678 vsi->netdev_registered = false;
6679 unregister_netdev(vsi->netdev);
6680 free_netdev(vsi->netdev);
6681 vsi->netdev = NULL;
6682 }
6683err_netdev:
6684 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
6685err_vsi:
6686 i40e_vsi_clear(vsi);
6687err_alloc:
6688 return NULL;
6689}
6690
6691/**
6692 * i40e_veb_get_bw_info - Query VEB BW information
6693 * @veb: the veb to query
6694 *
6695 * Query the Tx scheduler BW configuration data for given VEB
6696 **/
6697static int i40e_veb_get_bw_info(struct i40e_veb *veb)
6698{
6699 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
6700 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
6701 struct i40e_pf *pf = veb->pf;
6702 struct i40e_hw *hw = &pf->hw;
6703 u32 tc_bw_max;
6704 int ret = 0;
6705 int i;
6706
6707 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
6708 &bw_data, NULL);
6709 if (ret) {
6710 dev_info(&pf->pdev->dev,
6711 "query veb bw config failed, aq_err=%d\n",
6712 hw->aq.asq_last_status);
6713 goto out;
6714 }
6715
6716 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
6717 &ets_data, NULL);
6718 if (ret) {
6719 dev_info(&pf->pdev->dev,
6720 "query veb bw ets config failed, aq_err=%d\n",
6721 hw->aq.asq_last_status);
6722 goto out;
6723 }
6724
6725 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
6726 veb->bw_max_quanta = ets_data.tc_bw_max;
6727 veb->is_abs_credits = bw_data.absolute_credits_enable;
6728 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
6729 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
6730 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6731 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
6732 veb->bw_tc_limit_credits[i] =
6733 le16_to_cpu(bw_data.tc_bw_limits[i]);
6734 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
6735 }
6736
6737out:
6738 return ret;
6739}
6740
6741/**
6742 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
6743 * @pf: board private structure
6744 *
6745 * On error: returns error code (negative)
6746 * On success: returns vsi index in PF (positive)
6747 **/
6748static int i40e_veb_mem_alloc(struct i40e_pf *pf)
6749{
6750 int ret = -ENOENT;
6751 struct i40e_veb *veb;
6752 int i;
6753
6754 /* Need to protect the allocation of switch elements at the PF level */
6755 mutex_lock(&pf->switch_mutex);
6756
6757 /* VEB list may be fragmented if VEB creation/destruction has
6758 * been happening. We can afford to do a quick scan to look
6759 * for any free slots in the list.
6760 *
6761 * find next empty veb slot, looping back around if necessary
6762 */
6763 i = 0;
6764 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
6765 i++;
6766 if (i >= I40E_MAX_VEB) {
6767 ret = -ENOMEM;
6768 goto err_alloc_veb; /* out of VEB slots! */
6769 }
6770
6771 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
6772 if (!veb) {
6773 ret = -ENOMEM;
6774 goto err_alloc_veb;
6775 }
6776 veb->pf = pf;
6777 veb->idx = i;
6778 veb->enabled_tc = 1;
6779
6780 pf->veb[i] = veb;
6781 ret = i;
6782err_alloc_veb:
6783 mutex_unlock(&pf->switch_mutex);
6784 return ret;
6785}
6786
6787/**
6788 * i40e_switch_branch_release - Delete a branch of the switch tree
6789 * @branch: where to start deleting
6790 *
6791 * This uses recursion to find the tips of the branch to be
6792 * removed, deleting until we get back to and can delete this VEB.
6793 **/
6794static void i40e_switch_branch_release(struct i40e_veb *branch)
6795{
6796 struct i40e_pf *pf = branch->pf;
6797 u16 branch_seid = branch->seid;
6798 u16 veb_idx = branch->idx;
6799 int i;
6800
6801 /* release any VEBs on this VEB - RECURSION */
6802 for (i = 0; i < I40E_MAX_VEB; i++) {
6803 if (!pf->veb[i])
6804 continue;
6805 if (pf->veb[i]->uplink_seid == branch->seid)
6806 i40e_switch_branch_release(pf->veb[i]);
6807 }
6808
6809 /* Release the VSIs on this VEB, but not the owner VSI.
6810 *
6811 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
6812 * the VEB itself, so don't use (*branch) after this loop.
6813 */
6814 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
6815 if (!pf->vsi[i])
6816 continue;
6817 if (pf->vsi[i]->uplink_seid == branch_seid &&
6818 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
6819 i40e_vsi_release(pf->vsi[i]);
6820 }
6821 }
6822
6823 /* There's one corner case where the VEB might not have been
6824 * removed, so double check it here and remove it if needed.
6825 * This case happens if the veb was created from the debugfs
6826 * commands and no VSIs were added to it.
6827 */
6828 if (pf->veb[veb_idx])
6829 i40e_veb_release(pf->veb[veb_idx]);
6830}
6831
6832/**
6833 * i40e_veb_clear - remove veb struct
6834 * @veb: the veb to remove
6835 **/
6836static void i40e_veb_clear(struct i40e_veb *veb)
6837{
6838 if (!veb)
6839 return;
6840
6841 if (veb->pf) {
6842 struct i40e_pf *pf = veb->pf;
6843
6844 mutex_lock(&pf->switch_mutex);
6845 if (pf->veb[veb->idx] == veb)
6846 pf->veb[veb->idx] = NULL;
6847 mutex_unlock(&pf->switch_mutex);
6848 }
6849
6850 kfree(veb);
6851}
6852
6853/**
6854 * i40e_veb_release - Delete a VEB and free its resources
6855 * @veb: the VEB being removed
6856 **/
6857void i40e_veb_release(struct i40e_veb *veb)
6858{
6859 struct i40e_vsi *vsi = NULL;
6860 struct i40e_pf *pf;
6861 int i, n = 0;
6862
6863 pf = veb->pf;
6864
6865 /* find the remaining VSI and check for extras */
6866 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
6867 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
6868 n++;
6869 vsi = pf->vsi[i];
6870 }
6871 }
6872 if (n != 1) {
6873 dev_info(&pf->pdev->dev,
6874 "can't remove VEB %d with %d VSIs left\n",
6875 veb->seid, n);
6876 return;
6877 }
6878
6879 /* move the remaining VSI to uplink veb */
6880 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
6881 if (veb->uplink_seid) {
6882 vsi->uplink_seid = veb->uplink_seid;
6883 if (veb->uplink_seid == pf->mac_seid)
6884 vsi->veb_idx = I40E_NO_VEB;
6885 else
6886 vsi->veb_idx = veb->veb_idx;
6887 } else {
6888 /* floating VEB */
6889 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6890 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
6891 }
6892
6893 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
6894 i40e_veb_clear(veb);
6895
6896 return;
6897}
6898
6899/**
6900 * i40e_add_veb - create the VEB in the switch
6901 * @veb: the VEB to be instantiated
6902 * @vsi: the controlling VSI
6903 **/
6904static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
6905{
Greg Rose56747262013-11-28 06:39:37 +00006906 bool is_default = false;
Kevin Scotte1c51b952013-11-20 10:02:51 +00006907 bool is_cloud = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006908 int ret;
6909
6910 /* get a VEB from the hardware */
6911 ret = i40e_aq_add_veb(&veb->pf->hw, veb->uplink_seid, vsi->seid,
Kevin Scotte1c51b952013-11-20 10:02:51 +00006912 veb->enabled_tc, is_default,
6913 is_cloud, &veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006914 if (ret) {
6915 dev_info(&veb->pf->pdev->dev,
6916 "couldn't add VEB, err %d, aq_err %d\n",
6917 ret, veb->pf->hw.aq.asq_last_status);
6918 return -EPERM;
6919 }
6920
6921 /* get statistics counter */
6922 ret = i40e_aq_get_veb_parameters(&veb->pf->hw, veb->seid, NULL, NULL,
6923 &veb->stats_idx, NULL, NULL, NULL);
6924 if (ret) {
6925 dev_info(&veb->pf->pdev->dev,
6926 "couldn't get VEB statistics idx, err %d, aq_err %d\n",
6927 ret, veb->pf->hw.aq.asq_last_status);
6928 return -EPERM;
6929 }
6930 ret = i40e_veb_get_bw_info(veb);
6931 if (ret) {
6932 dev_info(&veb->pf->pdev->dev,
6933 "couldn't get VEB bw info, err %d, aq_err %d\n",
6934 ret, veb->pf->hw.aq.asq_last_status);
6935 i40e_aq_delete_element(&veb->pf->hw, veb->seid, NULL);
6936 return -ENOENT;
6937 }
6938
6939 vsi->uplink_seid = veb->seid;
6940 vsi->veb_idx = veb->idx;
6941 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
6942
6943 return 0;
6944}
6945
6946/**
6947 * i40e_veb_setup - Set up a VEB
6948 * @pf: board private structure
6949 * @flags: VEB setup flags
6950 * @uplink_seid: the switch element to link to
6951 * @vsi_seid: the initial VSI seid
6952 * @enabled_tc: Enabled TC bit-map
6953 *
6954 * This allocates the sw VEB structure and links it into the switch
6955 * It is possible and legal for this to be a duplicate of an already
6956 * existing VEB. It is also possible for both uplink and vsi seids
6957 * to be zero, in order to create a floating VEB.
6958 *
6959 * Returns pointer to the successfully allocated VEB sw struct on
6960 * success, otherwise returns NULL on failure.
6961 **/
6962struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
6963 u16 uplink_seid, u16 vsi_seid,
6964 u8 enabled_tc)
6965{
6966 struct i40e_veb *veb, *uplink_veb = NULL;
6967 int vsi_idx, veb_idx;
6968 int ret;
6969
6970 /* if one seid is 0, the other must be 0 to create a floating relay */
6971 if ((uplink_seid == 0 || vsi_seid == 0) &&
6972 (uplink_seid + vsi_seid != 0)) {
6973 dev_info(&pf->pdev->dev,
6974 "one, not both seid's are 0: uplink=%d vsi=%d\n",
6975 uplink_seid, vsi_seid);
6976 return NULL;
6977 }
6978
6979 /* make sure there is such a vsi and uplink */
6980 for (vsi_idx = 0; vsi_idx < pf->hw.func_caps.num_vsis; vsi_idx++)
6981 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
6982 break;
6983 if (vsi_idx >= pf->hw.func_caps.num_vsis && vsi_seid != 0) {
6984 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
6985 vsi_seid);
6986 return NULL;
6987 }
6988
6989 if (uplink_seid && uplink_seid != pf->mac_seid) {
6990 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6991 if (pf->veb[veb_idx] &&
6992 pf->veb[veb_idx]->seid == uplink_seid) {
6993 uplink_veb = pf->veb[veb_idx];
6994 break;
6995 }
6996 }
6997 if (!uplink_veb) {
6998 dev_info(&pf->pdev->dev,
6999 "uplink seid %d not found\n", uplink_seid);
7000 return NULL;
7001 }
7002 }
7003
7004 /* get veb sw struct */
7005 veb_idx = i40e_veb_mem_alloc(pf);
7006 if (veb_idx < 0)
7007 goto err_alloc;
7008 veb = pf->veb[veb_idx];
7009 veb->flags = flags;
7010 veb->uplink_seid = uplink_seid;
7011 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
7012 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
7013
7014 /* create the VEB in the switch */
7015 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
7016 if (ret)
7017 goto err_veb;
7018
7019 return veb;
7020
7021err_veb:
7022 i40e_veb_clear(veb);
7023err_alloc:
7024 return NULL;
7025}
7026
7027/**
7028 * i40e_setup_pf_switch_element - set pf vars based on switch type
7029 * @pf: board private structure
7030 * @ele: element we are building info from
7031 * @num_reported: total number of elements
7032 * @printconfig: should we print the contents
7033 *
7034 * helper function to assist in extracting a few useful SEID values.
7035 **/
7036static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
7037 struct i40e_aqc_switch_config_element_resp *ele,
7038 u16 num_reported, bool printconfig)
7039{
7040 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
7041 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
7042 u8 element_type = ele->element_type;
7043 u16 seid = le16_to_cpu(ele->seid);
7044
7045 if (printconfig)
7046 dev_info(&pf->pdev->dev,
7047 "type=%d seid=%d uplink=%d downlink=%d\n",
7048 element_type, seid, uplink_seid, downlink_seid);
7049
7050 switch (element_type) {
7051 case I40E_SWITCH_ELEMENT_TYPE_MAC:
7052 pf->mac_seid = seid;
7053 break;
7054 case I40E_SWITCH_ELEMENT_TYPE_VEB:
7055 /* Main VEB? */
7056 if (uplink_seid != pf->mac_seid)
7057 break;
7058 if (pf->lan_veb == I40E_NO_VEB) {
7059 int v;
7060
7061 /* find existing or else empty VEB */
7062 for (v = 0; v < I40E_MAX_VEB; v++) {
7063 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
7064 pf->lan_veb = v;
7065 break;
7066 }
7067 }
7068 if (pf->lan_veb == I40E_NO_VEB) {
7069 v = i40e_veb_mem_alloc(pf);
7070 if (v < 0)
7071 break;
7072 pf->lan_veb = v;
7073 }
7074 }
7075
7076 pf->veb[pf->lan_veb]->seid = seid;
7077 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
7078 pf->veb[pf->lan_veb]->pf = pf;
7079 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
7080 break;
7081 case I40E_SWITCH_ELEMENT_TYPE_VSI:
7082 if (num_reported != 1)
7083 break;
7084 /* This is immediately after a reset so we can assume this is
7085 * the PF's VSI
7086 */
7087 pf->mac_seid = uplink_seid;
7088 pf->pf_seid = downlink_seid;
7089 pf->main_vsi_seid = seid;
7090 if (printconfig)
7091 dev_info(&pf->pdev->dev,
7092 "pf_seid=%d main_vsi_seid=%d\n",
7093 pf->pf_seid, pf->main_vsi_seid);
7094 break;
7095 case I40E_SWITCH_ELEMENT_TYPE_PF:
7096 case I40E_SWITCH_ELEMENT_TYPE_VF:
7097 case I40E_SWITCH_ELEMENT_TYPE_EMP:
7098 case I40E_SWITCH_ELEMENT_TYPE_BMC:
7099 case I40E_SWITCH_ELEMENT_TYPE_PE:
7100 case I40E_SWITCH_ELEMENT_TYPE_PA:
7101 /* ignore these for now */
7102 break;
7103 default:
7104 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
7105 element_type, seid);
7106 break;
7107 }
7108}
7109
7110/**
7111 * i40e_fetch_switch_configuration - Get switch config from firmware
7112 * @pf: board private structure
7113 * @printconfig: should we print the contents
7114 *
7115 * Get the current switch configuration from the device and
7116 * extract a few useful SEID values.
7117 **/
7118int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
7119{
7120 struct i40e_aqc_get_switch_config_resp *sw_config;
7121 u16 next_seid = 0;
7122 int ret = 0;
7123 u8 *aq_buf;
7124 int i;
7125
7126 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
7127 if (!aq_buf)
7128 return -ENOMEM;
7129
7130 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
7131 do {
7132 u16 num_reported, num_total;
7133
7134 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
7135 I40E_AQ_LARGE_BUF,
7136 &next_seid, NULL);
7137 if (ret) {
7138 dev_info(&pf->pdev->dev,
7139 "get switch config failed %d aq_err=%x\n",
7140 ret, pf->hw.aq.asq_last_status);
7141 kfree(aq_buf);
7142 return -ENOENT;
7143 }
7144
7145 num_reported = le16_to_cpu(sw_config->header.num_reported);
7146 num_total = le16_to_cpu(sw_config->header.num_total);
7147
7148 if (printconfig)
7149 dev_info(&pf->pdev->dev,
7150 "header: %d reported %d total\n",
7151 num_reported, num_total);
7152
7153 if (num_reported) {
7154 int sz = sizeof(*sw_config) * num_reported;
7155
7156 kfree(pf->sw_config);
7157 pf->sw_config = kzalloc(sz, GFP_KERNEL);
7158 if (pf->sw_config)
7159 memcpy(pf->sw_config, sw_config, sz);
7160 }
7161
7162 for (i = 0; i < num_reported; i++) {
7163 struct i40e_aqc_switch_config_element_resp *ele =
7164 &sw_config->element[i];
7165
7166 i40e_setup_pf_switch_element(pf, ele, num_reported,
7167 printconfig);
7168 }
7169 } while (next_seid != 0);
7170
7171 kfree(aq_buf);
7172 return ret;
7173}
7174
7175/**
7176 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
7177 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007178 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007179 *
7180 * Returns 0 on success, negative value on failure
7181 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007182static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007183{
Jesse Brandeburg895106a2013-11-26 10:49:16 +00007184 u32 rxfc = 0, txfc = 0, rxfc_reg;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007185 int ret;
7186
7187 /* find out what's out there already */
7188 ret = i40e_fetch_switch_configuration(pf, false);
7189 if (ret) {
7190 dev_info(&pf->pdev->dev,
7191 "couldn't fetch switch config, err %d, aq_err %d\n",
7192 ret, pf->hw.aq.asq_last_status);
7193 return ret;
7194 }
7195 i40e_pf_reset_stats(pf);
7196
7197 /* fdir VSI must happen first to be sure it gets queue 0, but only
7198 * if there is enough room for the fdir VSI
7199 */
7200 if (pf->num_lan_qps > 1)
7201 i40e_fdir_setup(pf);
7202
7203 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007204 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007205 struct i40e_vsi *vsi = NULL;
7206 u16 uplink_seid;
7207
7208 /* Set up the PF VSI associated with the PF's main VSI
7209 * that is already in the HW switch
7210 */
7211 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
7212 uplink_seid = pf->veb[pf->lan_veb]->seid;
7213 else
7214 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007215 if (pf->lan_vsi == I40E_NO_VSI)
7216 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
7217 else if (reinit)
7218 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007219 if (!vsi) {
7220 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
7221 i40e_fdir_teardown(pf);
7222 return -EAGAIN;
7223 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007224 } else {
7225 /* force a reset of TC and queue layout configurations */
7226 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
7227 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
7228 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
7229 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
7230 }
7231 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
7232
7233 /* Setup static PF queue filter control settings */
7234 ret = i40e_setup_pf_filter_control(pf);
7235 if (ret) {
7236 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
7237 ret);
7238 /* Failure here should not stop continuing other steps */
7239 }
7240
7241 /* enable RSS in the HW, even for only one queue, as the stack can use
7242 * the hash
7243 */
7244 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
7245 i40e_config_rss(pf);
7246
7247 /* fill in link information and enable LSE reporting */
7248 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
7249 i40e_link_event(pf);
7250
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007251 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007252 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
7253 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007254 /* requested_mode is set in probe or by ethtool */
7255 if (!pf->fc_autoneg_status)
7256 goto no_autoneg;
7257
7258 if ((pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX) &&
7259 (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007260 pf->hw.fc.current_mode = I40E_FC_FULL;
7261 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX)
7262 pf->hw.fc.current_mode = I40E_FC_TX_PAUSE;
7263 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX)
7264 pf->hw.fc.current_mode = I40E_FC_RX_PAUSE;
7265 else
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007266 pf->hw.fc.current_mode = I40E_FC_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007267
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007268 /* sync the flow control settings with the auto-neg values */
7269 switch (pf->hw.fc.current_mode) {
7270 case I40E_FC_FULL:
7271 txfc = 1;
7272 rxfc = 1;
7273 break;
7274 case I40E_FC_TX_PAUSE:
7275 txfc = 1;
7276 rxfc = 0;
7277 break;
7278 case I40E_FC_RX_PAUSE:
7279 txfc = 0;
7280 rxfc = 1;
7281 break;
7282 case I40E_FC_NONE:
7283 case I40E_FC_DEFAULT:
7284 txfc = 0;
7285 rxfc = 0;
7286 break;
7287 case I40E_FC_PFC:
7288 /* TBD */
7289 break;
7290 /* no default case, we have to handle all possibilities here */
7291 }
7292
7293 wr32(&pf->hw, I40E_PRTDCB_FCCFG, txfc << I40E_PRTDCB_FCCFG_TFCE_SHIFT);
7294
7295 rxfc_reg = rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
7296 ~I40E_PRTDCB_MFLCN_RFCE_MASK;
7297 rxfc_reg |= (rxfc << I40E_PRTDCB_MFLCN_RFCE_SHIFT);
7298
7299 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rxfc_reg);
7300
7301 goto fc_complete;
7302
7303no_autoneg:
7304 /* disable L2 flow control, user can turn it on if they wish */
7305 wr32(&pf->hw, I40E_PRTDCB_FCCFG, 0);
7306 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
7307 ~I40E_PRTDCB_MFLCN_RFCE_MASK);
7308
7309fc_complete:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007310 return ret;
7311}
7312
7313/**
7314 * i40e_set_rss_size - helper to set rss_size
7315 * @pf: board private structure
7316 * @queues_left: how many queues
7317 */
7318static u16 i40e_set_rss_size(struct i40e_pf *pf, int queues_left)
7319{
7320 int num_tc0;
7321
7322 num_tc0 = min_t(int, queues_left, pf->rss_size_max);
Jesse Brandeburgbf051a32013-11-26 10:49:17 +00007323 num_tc0 = min_t(int, num_tc0, num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007324 num_tc0 = rounddown_pow_of_two(num_tc0);
7325
7326 return num_tc0;
7327}
7328
7329/**
7330 * i40e_determine_queue_usage - Work out queue distribution
7331 * @pf: board private structure
7332 **/
7333static void i40e_determine_queue_usage(struct i40e_pf *pf)
7334{
7335 int accum_tc_size;
7336 int queues_left;
7337
7338 pf->num_lan_qps = 0;
7339 pf->num_tc_qps = rounddown_pow_of_two(pf->num_tc_qps);
7340 accum_tc_size = (I40E_MAX_TRAFFIC_CLASS - 1) * pf->num_tc_qps;
7341
7342 /* Find the max queues to be put into basic use. We'll always be
7343 * using TC0, whether or not DCB is running, and TC0 will get the
7344 * big RSS set.
7345 */
7346 queues_left = pf->hw.func_caps.num_tx_qp;
7347
Neerav Parikh9f529872013-11-28 06:39:38 +00007348 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007349 !(pf->flags & (I40E_FLAG_RSS_ENABLED |
7350 I40E_FLAG_FDIR_ENABLED | I40E_FLAG_DCB_ENABLED)) ||
7351 (queues_left == 1)) {
7352
7353 /* one qp for PF, no queues for anything else */
7354 queues_left = 0;
7355 pf->rss_size = pf->num_lan_qps = 1;
7356
7357 /* make sure all the fancies are disabled */
7358 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007359 I40E_FLAG_FDIR_ENABLED |
7360 I40E_FLAG_FDIR_ATR_ENABLED |
7361 I40E_FLAG_DCB_ENABLED |
7362 I40E_FLAG_SRIOV_ENABLED |
7363 I40E_FLAG_VMDQ_ENABLED);
7364
7365 } else if (pf->flags & I40E_FLAG_RSS_ENABLED &&
7366 !(pf->flags & I40E_FLAG_FDIR_ENABLED) &&
7367 !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
7368
7369 pf->rss_size = i40e_set_rss_size(pf, queues_left);
7370
7371 queues_left -= pf->rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007372 pf->num_lan_qps = pf->rss_size_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007373
7374 } else if (pf->flags & I40E_FLAG_RSS_ENABLED &&
7375 !(pf->flags & I40E_FLAG_FDIR_ENABLED) &&
7376 (pf->flags & I40E_FLAG_DCB_ENABLED)) {
7377
7378 /* save num_tc_qps queues for TCs 1 thru 7 and the rest
7379 * are set up for RSS in TC0
7380 */
7381 queues_left -= accum_tc_size;
7382
7383 pf->rss_size = i40e_set_rss_size(pf, queues_left);
7384
7385 queues_left -= pf->rss_size;
7386 if (queues_left < 0) {
7387 dev_info(&pf->pdev->dev, "not enough queues for DCB\n");
7388 return;
7389 }
7390
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007391 pf->num_lan_qps = pf->rss_size_max + accum_tc_size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007392
7393 } else if (pf->flags & I40E_FLAG_RSS_ENABLED &&
7394 (pf->flags & I40E_FLAG_FDIR_ENABLED) &&
7395 !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
7396
7397 queues_left -= 1; /* save 1 queue for FD */
7398
7399 pf->rss_size = i40e_set_rss_size(pf, queues_left);
7400
7401 queues_left -= pf->rss_size;
7402 if (queues_left < 0) {
7403 dev_info(&pf->pdev->dev, "not enough queues for Flow Director\n");
7404 return;
7405 }
7406
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007407 pf->num_lan_qps = pf->rss_size_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007408
7409 } else if (pf->flags & I40E_FLAG_RSS_ENABLED &&
7410 (pf->flags & I40E_FLAG_FDIR_ENABLED) &&
7411 (pf->flags & I40E_FLAG_DCB_ENABLED)) {
7412
7413 /* save 1 queue for TCs 1 thru 7,
7414 * 1 queue for flow director,
7415 * and the rest are set up for RSS in TC0
7416 */
7417 queues_left -= 1;
7418 queues_left -= accum_tc_size;
7419
7420 pf->rss_size = i40e_set_rss_size(pf, queues_left);
7421 queues_left -= pf->rss_size;
7422 if (queues_left < 0) {
7423 dev_info(&pf->pdev->dev, "not enough queues for DCB and Flow Director\n");
7424 return;
7425 }
7426
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007427 pf->num_lan_qps = pf->rss_size_max + accum_tc_size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007428
7429 } else {
7430 dev_info(&pf->pdev->dev,
7431 "Invalid configuration, flags=0x%08llx\n", pf->flags);
7432 return;
7433 }
7434
7435 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
7436 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
7437 pf->num_req_vfs = min_t(int, pf->num_req_vfs, (queues_left /
7438 pf->num_vf_qps));
7439 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
7440 }
7441
7442 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7443 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
7444 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
7445 (queues_left / pf->num_vmdq_qps));
7446 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
7447 }
7448
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007449 pf->queues_left = queues_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007450 return;
7451}
7452
7453/**
7454 * i40e_setup_pf_filter_control - Setup PF static filter control
7455 * @pf: PF to be setup
7456 *
7457 * i40e_setup_pf_filter_control sets up a pf's initial filter control
7458 * settings. If PE/FCoE are enabled then it will also set the per PF
7459 * based filter sizes required for them. It also enables Flow director,
7460 * ethertype and macvlan type filter settings for the pf.
7461 *
7462 * Returns 0 on success, negative on failure
7463 **/
7464static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
7465{
7466 struct i40e_filter_control_settings *settings = &pf->filter_settings;
7467
7468 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
7469
7470 /* Flow Director is enabled */
7471 if (pf->flags & (I40E_FLAG_FDIR_ENABLED | I40E_FLAG_FDIR_ATR_ENABLED))
7472 settings->enable_fdir = true;
7473
7474 /* Ethtype and MACVLAN filters enabled for PF */
7475 settings->enable_ethtype = true;
7476 settings->enable_macvlan = true;
7477
7478 if (i40e_set_filter_control(&pf->hw, settings))
7479 return -ENOENT;
7480
7481 return 0;
7482}
7483
7484/**
7485 * i40e_probe - Device initialization routine
7486 * @pdev: PCI device information struct
7487 * @ent: entry in i40e_pci_tbl
7488 *
7489 * i40e_probe initializes a pf identified by a pci_dev structure.
7490 * The OS initialization, configuring of the pf private structure,
7491 * and a hardware reset occur.
7492 *
7493 * Returns 0 on success, negative on failure
7494 **/
7495static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
7496{
7497 struct i40e_driver_version dv;
7498 struct i40e_pf *pf;
7499 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00007500 static u16 pfs_found;
Catherine Sullivand4dfb812013-11-28 06:39:21 +00007501 u16 link_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007502 int err = 0;
7503 u32 len;
7504
7505 err = pci_enable_device_mem(pdev);
7506 if (err)
7507 return err;
7508
7509 /* set up for high or low dma */
7510 if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64))) {
7511 /* coherent mask for the same size will always succeed if
7512 * dma_set_mask does
7513 */
7514 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
7515 } else if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
7516 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
7517 } else {
7518 dev_err(&pdev->dev, "DMA configuration failed: %d\n", err);
7519 err = -EIO;
7520 goto err_dma;
7521 }
7522
7523 /* set up pci connections */
7524 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
7525 IORESOURCE_MEM), i40e_driver_name);
7526 if (err) {
7527 dev_info(&pdev->dev,
7528 "pci_request_selected_regions failed %d\n", err);
7529 goto err_pci_reg;
7530 }
7531
7532 pci_enable_pcie_error_reporting(pdev);
7533 pci_set_master(pdev);
7534
7535 /* Now that we have a PCI connection, we need to do the
7536 * low level device setup. This is primarily setting up
7537 * the Admin Queue structures and then querying for the
7538 * device's current profile information.
7539 */
7540 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
7541 if (!pf) {
7542 err = -ENOMEM;
7543 goto err_pf_alloc;
7544 }
7545 pf->next_vsi = 0;
7546 pf->pdev = pdev;
7547 set_bit(__I40E_DOWN, &pf->state);
7548
7549 hw = &pf->hw;
7550 hw->back = pf;
7551 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
7552 pci_resource_len(pdev, 0));
7553 if (!hw->hw_addr) {
7554 err = -EIO;
7555 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
7556 (unsigned int)pci_resource_start(pdev, 0),
7557 (unsigned int)pci_resource_len(pdev, 0), err);
7558 goto err_ioremap;
7559 }
7560 hw->vendor_id = pdev->vendor;
7561 hw->device_id = pdev->device;
7562 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
7563 hw->subsystem_vendor_id = pdev->subsystem_vendor;
7564 hw->subsystem_device_id = pdev->subsystem_device;
7565 hw->bus.device = PCI_SLOT(pdev->devfn);
7566 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00007567 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007568
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00007569 /* do a special CORER for clearing PXE mode once at init */
7570 if (hw->revision_id == 0 &&
7571 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
7572 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
7573 i40e_flush(hw);
7574 msleep(200);
7575 pf->corer_count++;
7576
7577 i40e_clear_pxe_mode(hw);
7578 }
7579
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007580 /* Reset here to make sure all is clean and to define PF 'n' */
7581 err = i40e_pf_reset(hw);
7582 if (err) {
7583 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
7584 goto err_pf_reset;
7585 }
7586 pf->pfr_count++;
7587
7588 hw->aq.num_arq_entries = I40E_AQ_LEN;
7589 hw->aq.num_asq_entries = I40E_AQ_LEN;
7590 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
7591 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
7592 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
7593 snprintf(pf->misc_int_name, sizeof(pf->misc_int_name) - 1,
7594 "%s-pf%d:misc",
7595 dev_driver_string(&pf->pdev->dev), pf->hw.pf_id);
7596
7597 err = i40e_init_shared_code(hw);
7598 if (err) {
7599 dev_info(&pdev->dev, "init_shared_code failed: %d\n", err);
7600 goto err_pf_reset;
7601 }
7602
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007603 /* set up a default setting for link flow control */
7604 pf->hw.fc.requested_mode = I40E_FC_NONE;
7605
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007606 err = i40e_init_adminq(hw);
7607 dev_info(&pdev->dev, "%s\n", i40e_fw_version_str(hw));
Anjali Singhai jainfe310702013-11-16 10:00:37 +00007608 if (((hw->nvm.version & I40E_NVM_VERSION_HI_MASK)
7609 >> I40E_NVM_VERSION_HI_SHIFT) != I40E_CURRENT_NVM_VERSION_HI) {
7610 dev_info(&pdev->dev,
7611 "warning: NVM version not supported, supported version: %02x.%02x\n",
7612 I40E_CURRENT_NVM_VERSION_HI,
7613 I40E_CURRENT_NVM_VERSION_LO);
7614 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007615 if (err) {
7616 dev_info(&pdev->dev,
7617 "init_adminq failed: %d expecting API %02x.%02x\n",
7618 err,
7619 I40E_FW_API_VERSION_MAJOR, I40E_FW_API_VERSION_MINOR);
7620 goto err_pf_reset;
7621 }
7622
7623 err = i40e_get_capabilities(pf);
7624 if (err)
7625 goto err_adminq_setup;
7626
7627 err = i40e_sw_init(pf);
7628 if (err) {
7629 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
7630 goto err_sw_init;
7631 }
7632
7633 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
7634 hw->func_caps.num_rx_qp,
7635 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
7636 if (err) {
7637 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
7638 goto err_init_lan_hmc;
7639 }
7640
7641 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
7642 if (err) {
7643 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
7644 err = -ENOENT;
7645 goto err_configure_lan_hmc;
7646 }
7647
7648 i40e_get_mac_addr(hw, hw->mac.addr);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +00007649 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007650 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
7651 err = -EIO;
7652 goto err_mac_addr;
7653 }
7654 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
7655 memcpy(hw->mac.perm_addr, hw->mac.addr, ETH_ALEN);
7656
7657 pci_set_drvdata(pdev, pf);
7658 pci_save_state(pdev);
7659
7660 /* set up periodic task facility */
7661 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
7662 pf->service_timer_period = HZ;
7663
7664 INIT_WORK(&pf->service_task, i40e_service_task);
7665 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
7666 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
7667 pf->link_check_timeout = jiffies;
7668
Shannon Nelson8e2773a2013-11-28 06:39:22 +00007669 /* WoL defaults to disabled */
7670 pf->wol_en = false;
7671 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
7672
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007673 /* set up the main switch operations */
7674 i40e_determine_queue_usage(pf);
7675 i40e_init_interrupt_scheme(pf);
7676
7677 /* Set up the *vsi struct based on the number of VSIs in the HW,
7678 * and set up our local tracking of the MAIN PF vsi.
7679 */
7680 len = sizeof(struct i40e_vsi *) * pf->hw.func_caps.num_vsis;
7681 pf->vsi = kzalloc(len, GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +00007682 if (!pf->vsi) {
7683 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007684 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +00007685 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007686
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007687 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007688 if (err) {
7689 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
7690 goto err_vsis;
7691 }
7692
7693 /* The main driver is (mostly) up and happy. We need to set this state
7694 * before setting up the misc vector or we get a race and the vector
7695 * ends up disabled forever.
7696 */
7697 clear_bit(__I40E_DOWN, &pf->state);
7698
7699 /* In case of MSIX we are going to setup the misc vector right here
7700 * to handle admin queue events etc. In case of legacy and MSI
7701 * the misc functionality and queue processing is combined in
7702 * the same vector and that gets setup at open.
7703 */
7704 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
7705 err = i40e_setup_misc_vector(pf);
7706 if (err) {
7707 dev_info(&pdev->dev,
7708 "setup of misc vector failed: %d\n", err);
7709 goto err_vsis;
7710 }
7711 }
7712
7713 /* prep for VF support */
7714 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
7715 (pf->flags & I40E_FLAG_MSIX_ENABLED)) {
7716 u32 val;
7717
7718 /* disable link interrupts for VFs */
7719 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
7720 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
7721 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
7722 i40e_flush(hw);
7723 }
7724
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00007725 pfs_found++;
7726
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007727 i40e_dbg_pf_init(pf);
7728
7729 /* tell the firmware that we're starting */
7730 dv.major_version = DRV_VERSION_MAJOR;
7731 dv.minor_version = DRV_VERSION_MINOR;
7732 dv.build_version = DRV_VERSION_BUILD;
7733 dv.subbuild_version = 0;
7734 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
7735
7736 /* since everything's happy, start the service_task timer */
7737 mod_timer(&pf->service_timer,
7738 round_jiffies(jiffies + pf->service_timer_period));
7739
Catherine Sullivand4dfb812013-11-28 06:39:21 +00007740 /* Get the negotiated link width and speed from PCI config space */
7741 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA, &link_status);
7742
7743 i40e_set_pci_config_data(hw, link_status);
7744
7745 dev_info(&pdev->dev, "PCI Express: %s %s\n",
7746 (hw->bus.speed == i40e_bus_speed_8000 ? "Speed 8.0GT/s" :
7747 hw->bus.speed == i40e_bus_speed_5000 ? "Speed 5.0GT/s" :
7748 hw->bus.speed == i40e_bus_speed_2500 ? "Speed 2.5GT/s" :
7749 "Unknown"),
7750 (hw->bus.width == i40e_bus_width_pcie_x8 ? "Width x8" :
7751 hw->bus.width == i40e_bus_width_pcie_x4 ? "Width x4" :
7752 hw->bus.width == i40e_bus_width_pcie_x2 ? "Width x2" :
7753 hw->bus.width == i40e_bus_width_pcie_x1 ? "Width x1" :
7754 "Unknown"));
7755
7756 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
7757 hw->bus.speed < i40e_bus_speed_8000) {
7758 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
7759 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
7760 }
7761
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007762 return 0;
7763
7764 /* Unwind what we've done if something failed in the setup */
7765err_vsis:
7766 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007767 i40e_clear_interrupt_scheme(pf);
7768 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +00007769err_switch_setup:
7770 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007771 del_timer_sync(&pf->service_timer);
7772err_mac_addr:
7773err_configure_lan_hmc:
7774 (void)i40e_shutdown_lan_hmc(hw);
7775err_init_lan_hmc:
7776 kfree(pf->qp_pile);
7777 kfree(pf->irq_pile);
7778err_sw_init:
7779err_adminq_setup:
7780 (void)i40e_shutdown_adminq(hw);
7781err_pf_reset:
7782 iounmap(hw->hw_addr);
7783err_ioremap:
7784 kfree(pf);
7785err_pf_alloc:
7786 pci_disable_pcie_error_reporting(pdev);
7787 pci_release_selected_regions(pdev,
7788 pci_select_bars(pdev, IORESOURCE_MEM));
7789err_pci_reg:
7790err_dma:
7791 pci_disable_device(pdev);
7792 return err;
7793}
7794
7795/**
7796 * i40e_remove - Device removal routine
7797 * @pdev: PCI device information struct
7798 *
7799 * i40e_remove is called by the PCI subsystem to alert the driver
7800 * that is should release a PCI device. This could be caused by a
7801 * Hot-Plug event, or because the driver is going to be removed from
7802 * memory.
7803 **/
7804static void i40e_remove(struct pci_dev *pdev)
7805{
7806 struct i40e_pf *pf = pci_get_drvdata(pdev);
7807 i40e_status ret_code;
7808 u32 reg;
7809 int i;
7810
7811 i40e_dbg_pf_exit(pf);
7812
7813 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
7814 i40e_free_vfs(pf);
7815 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
7816 }
7817
7818 /* no more scheduling of any task */
7819 set_bit(__I40E_DOWN, &pf->state);
7820 del_timer_sync(&pf->service_timer);
7821 cancel_work_sync(&pf->service_task);
7822
7823 i40e_fdir_teardown(pf);
7824
7825 /* If there is a switch structure or any orphans, remove them.
7826 * This will leave only the PF's VSI remaining.
7827 */
7828 for (i = 0; i < I40E_MAX_VEB; i++) {
7829 if (!pf->veb[i])
7830 continue;
7831
7832 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
7833 pf->veb[i]->uplink_seid == 0)
7834 i40e_switch_branch_release(pf->veb[i]);
7835 }
7836
7837 /* Now we can shutdown the PF's VSI, just before we kill
7838 * adminq and hmc.
7839 */
7840 if (pf->vsi[pf->lan_vsi])
7841 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
7842
7843 i40e_stop_misc_vector(pf);
7844 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
7845 synchronize_irq(pf->msix_entries[0].vector);
7846 free_irq(pf->msix_entries[0].vector, pf);
7847 }
7848
7849 /* shutdown and destroy the HMC */
7850 ret_code = i40e_shutdown_lan_hmc(&pf->hw);
7851 if (ret_code)
7852 dev_warn(&pdev->dev,
7853 "Failed to destroy the HMC resources: %d\n", ret_code);
7854
7855 /* shutdown the adminq */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007856 ret_code = i40e_shutdown_adminq(&pf->hw);
7857 if (ret_code)
7858 dev_warn(&pdev->dev,
7859 "Failed to destroy the Admin Queue resources: %d\n",
7860 ret_code);
7861
7862 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
7863 i40e_clear_interrupt_scheme(pf);
7864 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
7865 if (pf->vsi[i]) {
7866 i40e_vsi_clear_rings(pf->vsi[i]);
7867 i40e_vsi_clear(pf->vsi[i]);
7868 pf->vsi[i] = NULL;
7869 }
7870 }
7871
7872 for (i = 0; i < I40E_MAX_VEB; i++) {
7873 kfree(pf->veb[i]);
7874 pf->veb[i] = NULL;
7875 }
7876
7877 kfree(pf->qp_pile);
7878 kfree(pf->irq_pile);
7879 kfree(pf->sw_config);
7880 kfree(pf->vsi);
7881
7882 /* force a PF reset to clean anything leftover */
7883 reg = rd32(&pf->hw, I40E_PFGEN_CTRL);
7884 wr32(&pf->hw, I40E_PFGEN_CTRL, (reg | I40E_PFGEN_CTRL_PFSWR_MASK));
7885 i40e_flush(&pf->hw);
7886
7887 iounmap(pf->hw.hw_addr);
7888 kfree(pf);
7889 pci_release_selected_regions(pdev,
7890 pci_select_bars(pdev, IORESOURCE_MEM));
7891
7892 pci_disable_pcie_error_reporting(pdev);
7893 pci_disable_device(pdev);
7894}
7895
7896/**
7897 * i40e_pci_error_detected - warning that something funky happened in PCI land
7898 * @pdev: PCI device information struct
7899 *
7900 * Called to warn that something happened and the error handling steps
7901 * are in progress. Allows the driver to quiesce things, be ready for
7902 * remediation.
7903 **/
7904static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
7905 enum pci_channel_state error)
7906{
7907 struct i40e_pf *pf = pci_get_drvdata(pdev);
7908
7909 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
7910
7911 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +00007912 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
7913 rtnl_lock();
7914 i40e_prep_for_reset(pf);
7915 rtnl_unlock();
7916 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007917
7918 /* Request a slot reset */
7919 return PCI_ERS_RESULT_NEED_RESET;
7920}
7921
7922/**
7923 * i40e_pci_error_slot_reset - a PCI slot reset just happened
7924 * @pdev: PCI device information struct
7925 *
7926 * Called to find if the driver can work with the device now that
7927 * the pci slot has been reset. If a basic connection seems good
7928 * (registers are readable and have sane content) then return a
7929 * happy little PCI_ERS_RESULT_xxx.
7930 **/
7931static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
7932{
7933 struct i40e_pf *pf = pci_get_drvdata(pdev);
7934 pci_ers_result_t result;
7935 int err;
7936 u32 reg;
7937
7938 dev_info(&pdev->dev, "%s\n", __func__);
7939 if (pci_enable_device_mem(pdev)) {
7940 dev_info(&pdev->dev,
7941 "Cannot re-enable PCI device after reset.\n");
7942 result = PCI_ERS_RESULT_DISCONNECT;
7943 } else {
7944 pci_set_master(pdev);
7945 pci_restore_state(pdev);
7946 pci_save_state(pdev);
7947 pci_wake_from_d3(pdev, false);
7948
7949 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
7950 if (reg == 0)
7951 result = PCI_ERS_RESULT_RECOVERED;
7952 else
7953 result = PCI_ERS_RESULT_DISCONNECT;
7954 }
7955
7956 err = pci_cleanup_aer_uncorrect_error_status(pdev);
7957 if (err) {
7958 dev_info(&pdev->dev,
7959 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
7960 err);
7961 /* non-fatal, continue */
7962 }
7963
7964 return result;
7965}
7966
7967/**
7968 * i40e_pci_error_resume - restart operations after PCI error recovery
7969 * @pdev: PCI device information struct
7970 *
7971 * Called to allow the driver to bring things back up after PCI error
7972 * and/or reset recovery has finished.
7973 **/
7974static void i40e_pci_error_resume(struct pci_dev *pdev)
7975{
7976 struct i40e_pf *pf = pci_get_drvdata(pdev);
7977
7978 dev_info(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00007979 if (test_bit(__I40E_SUSPENDED, &pf->state))
7980 return;
7981
7982 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007983 i40e_handle_reset_warning(pf);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00007984 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007985}
7986
Shannon Nelson9007bcc2013-11-26 10:49:23 +00007987/**
7988 * i40e_shutdown - PCI callback for shutting down
7989 * @pdev: PCI device information struct
7990 **/
7991static void i40e_shutdown(struct pci_dev *pdev)
7992{
7993 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +00007994 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +00007995
7996 set_bit(__I40E_SUSPENDED, &pf->state);
7997 set_bit(__I40E_DOWN, &pf->state);
7998 rtnl_lock();
7999 i40e_prep_for_reset(pf);
8000 rtnl_unlock();
8001
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008002 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
8003 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
8004
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008005 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008006 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008007 pci_set_power_state(pdev, PCI_D3hot);
8008 }
8009}
8010
8011#ifdef CONFIG_PM
8012/**
8013 * i40e_suspend - PCI callback for moving to D3
8014 * @pdev: PCI device information struct
8015 **/
8016static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
8017{
8018 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008019 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008020
8021 set_bit(__I40E_SUSPENDED, &pf->state);
8022 set_bit(__I40E_DOWN, &pf->state);
8023 rtnl_lock();
8024 i40e_prep_for_reset(pf);
8025 rtnl_unlock();
8026
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008027 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
8028 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
8029
8030 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008031 pci_set_power_state(pdev, PCI_D3hot);
8032
8033 return 0;
8034}
8035
8036/**
8037 * i40e_resume - PCI callback for waking up from D3
8038 * @pdev: PCI device information struct
8039 **/
8040static int i40e_resume(struct pci_dev *pdev)
8041{
8042 struct i40e_pf *pf = pci_get_drvdata(pdev);
8043 u32 err;
8044
8045 pci_set_power_state(pdev, PCI_D0);
8046 pci_restore_state(pdev);
8047 /* pci_restore_state() clears dev->state_saves, so
8048 * call pci_save_state() again to restore it.
8049 */
8050 pci_save_state(pdev);
8051
8052 err = pci_enable_device_mem(pdev);
8053 if (err) {
8054 dev_err(&pdev->dev,
8055 "%s: Cannot enable PCI device from suspend\n",
8056 __func__);
8057 return err;
8058 }
8059 pci_set_master(pdev);
8060
8061 /* no wakeup events while running */
8062 pci_wake_from_d3(pdev, false);
8063
8064 /* handling the reset will rebuild the device state */
8065 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
8066 clear_bit(__I40E_DOWN, &pf->state);
8067 rtnl_lock();
8068 i40e_reset_and_rebuild(pf, false);
8069 rtnl_unlock();
8070 }
8071
8072 return 0;
8073}
8074
8075#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008076static const struct pci_error_handlers i40e_err_handler = {
8077 .error_detected = i40e_pci_error_detected,
8078 .slot_reset = i40e_pci_error_slot_reset,
8079 .resume = i40e_pci_error_resume,
8080};
8081
8082static struct pci_driver i40e_driver = {
8083 .name = i40e_driver_name,
8084 .id_table = i40e_pci_tbl,
8085 .probe = i40e_probe,
8086 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008087#ifdef CONFIG_PM
8088 .suspend = i40e_suspend,
8089 .resume = i40e_resume,
8090#endif
8091 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008092 .err_handler = &i40e_err_handler,
8093 .sriov_configure = i40e_pci_sriov_configure,
8094};
8095
8096/**
8097 * i40e_init_module - Driver registration routine
8098 *
8099 * i40e_init_module is the first routine called when the driver is
8100 * loaded. All it does is register with the PCI subsystem.
8101 **/
8102static int __init i40e_init_module(void)
8103{
8104 pr_info("%s: %s - version %s\n", i40e_driver_name,
8105 i40e_driver_string, i40e_driver_version_str);
8106 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
8107 i40e_dbg_init();
8108 return pci_register_driver(&i40e_driver);
8109}
8110module_init(i40e_init_module);
8111
8112/**
8113 * i40e_exit_module - Driver exit cleanup routine
8114 *
8115 * i40e_exit_module is called just before the driver is removed
8116 * from memory.
8117 **/
8118static void __exit i40e_exit_module(void)
8119{
8120 pci_unregister_driver(&i40e_driver);
8121 i40e_dbg_exit();
8122}
8123module_exit(i40e_exit_module);