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