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