blob: 126be0dcc23e93c872e82c2b0d1e515f7a232fd8 [file] [log] [blame]
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
Greg Rosedc641b72013-12-18 13:45:51 +00004 * Copyright(c) 2013 - 2014 Intel Corporation.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005 *
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 *
Greg Rosedc641b72013-12-18 13:45:51 +000015 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000017 *
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 *
21 * Contact Information:
22 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 ******************************************************************************/
26
27/* Local includes */
28#include "i40e.h"
Shannon Nelson4eb3f762014-03-06 08:59:58 +000029#include "i40e_diag.h"
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000030#ifdef CONFIG_I40E_VXLAN
31#include <net/vxlan.h>
32#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000033
34const char i40e_driver_name[] = "i40e";
35static const char i40e_driver_string[] =
36 "Intel(R) Ethernet Connection XL710 Network Driver";
37
38#define DRV_KERN "-k"
39
40#define DRV_VERSION_MAJOR 0
41#define DRV_VERSION_MINOR 3
Catherine Sullivanb98d1df2014-03-14 07:32:31 +000042#define DRV_VERSION_BUILD 43
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000043#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
44 __stringify(DRV_VERSION_MINOR) "." \
45 __stringify(DRV_VERSION_BUILD) DRV_KERN
46const char i40e_driver_version_str[] = DRV_VERSION;
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -080047static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000048
49/* a bit of forward declarations */
50static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
51static void i40e_handle_reset_warning(struct i40e_pf *pf);
52static int i40e_add_vsi(struct i40e_vsi *vsi);
53static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000054static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000055static int i40e_setup_misc_vector(struct i40e_pf *pf);
56static void i40e_determine_queue_usage(struct i40e_pf *pf);
57static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080058static void i40e_fdir_sb_setup(struct i40e_pf *pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080059static int i40e_veb_get_bw_info(struct i40e_veb *veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000060
61/* i40e_pci_tbl - PCI Device ID Table
62 *
63 * Last entry must be all 0s
64 *
65 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
66 * Class, Class Mask, private data (not used) }
67 */
68static DEFINE_PCI_DEVICE_TABLE(i40e_pci_tbl) = {
Shannon Nelsonab600852014-01-17 15:36:39 -080069 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
70 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X710), 0},
71 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
72 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_A), 0},
73 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
74 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
75 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_D), 0},
76 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
77 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
78 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000079 /* required last entry */
80 {0, }
81};
82MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
83
84#define I40E_MAX_VF_COUNT 128
85static int debug = -1;
86module_param(debug, int, 0);
87MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
88
89MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
90MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
91MODULE_LICENSE("GPL");
92MODULE_VERSION(DRV_VERSION);
93
94/**
95 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
96 * @hw: pointer to the HW structure
97 * @mem: ptr to mem struct to fill out
98 * @size: size of memory requested
99 * @alignment: what to align the allocation to
100 **/
101int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
102 u64 size, u32 alignment)
103{
104 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
105
106 mem->size = ALIGN(size, alignment);
107 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
108 &mem->pa, GFP_KERNEL);
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000109 if (!mem->va)
110 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000111
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000112 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000113}
114
115/**
116 * i40e_free_dma_mem_d - OS specific memory free for shared code
117 * @hw: pointer to the HW structure
118 * @mem: ptr to mem struct to free
119 **/
120int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
121{
122 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
123
124 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
125 mem->va = NULL;
126 mem->pa = 0;
127 mem->size = 0;
128
129 return 0;
130}
131
132/**
133 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
134 * @hw: pointer to the HW structure
135 * @mem: ptr to mem struct to fill out
136 * @size: size of memory requested
137 **/
138int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
139 u32 size)
140{
141 mem->size = size;
142 mem->va = kzalloc(size, GFP_KERNEL);
143
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000144 if (!mem->va)
145 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000146
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000147 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000148}
149
150/**
151 * i40e_free_virt_mem_d - OS specific memory free for shared code
152 * @hw: pointer to the HW structure
153 * @mem: ptr to mem struct to free
154 **/
155int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
156{
157 /* it's ok to kfree a NULL pointer */
158 kfree(mem->va);
159 mem->va = NULL;
160 mem->size = 0;
161
162 return 0;
163}
164
165/**
166 * i40e_get_lump - find a lump of free generic resource
167 * @pf: board private structure
168 * @pile: the pile of resource to search
169 * @needed: the number of items needed
170 * @id: an owner id to stick on the items assigned
171 *
172 * Returns the base item index of the lump, or negative for error
173 *
174 * The search_hint trick and lack of advanced fit-finding only work
175 * because we're highly likely to have all the same size lump requests.
176 * Linear search time and any fragmentation should be minimal.
177 **/
178static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
179 u16 needed, u16 id)
180{
181 int ret = -ENOMEM;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000182 int i, j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000183
184 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
185 dev_info(&pf->pdev->dev,
186 "param err: pile=%p needed=%d id=0x%04x\n",
187 pile, needed, id);
188 return -EINVAL;
189 }
190
191 /* start the linear search with an imperfect hint */
192 i = pile->search_hint;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000193 while (i < pile->num_entries) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000194 /* skip already allocated entries */
195 if (pile->list[i] & I40E_PILE_VALID_BIT) {
196 i++;
197 continue;
198 }
199
200 /* do we have enough in this lump? */
201 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
202 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
203 break;
204 }
205
206 if (j == needed) {
207 /* there was enough, so assign it to the requestor */
208 for (j = 0; j < needed; j++)
209 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
210 ret = i;
211 pile->search_hint = i + j;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000212 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000213 } else {
214 /* not enough, so skip over it and continue looking */
215 i += j;
216 }
217 }
218
219 return ret;
220}
221
222/**
223 * i40e_put_lump - return a lump of generic resource
224 * @pile: the pile of resource to search
225 * @index: the base item index
226 * @id: the owner id of the items assigned
227 *
228 * Returns the count of items in the lump
229 **/
230static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
231{
232 int valid_id = (id | I40E_PILE_VALID_BIT);
233 int count = 0;
234 int i;
235
236 if (!pile || index >= pile->num_entries)
237 return -EINVAL;
238
239 for (i = index;
240 i < pile->num_entries && pile->list[i] == valid_id;
241 i++) {
242 pile->list[i] = 0;
243 count++;
244 }
245
246 if (count && index < pile->search_hint)
247 pile->search_hint = index;
248
249 return count;
250}
251
252/**
253 * i40e_service_event_schedule - Schedule the service task to wake up
254 * @pf: board private structure
255 *
256 * If not already scheduled, this puts the task into the work queue
257 **/
258static void i40e_service_event_schedule(struct i40e_pf *pf)
259{
260 if (!test_bit(__I40E_DOWN, &pf->state) &&
261 !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) &&
262 !test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
263 schedule_work(&pf->service_task);
264}
265
266/**
267 * i40e_tx_timeout - Respond to a Tx Hang
268 * @netdev: network interface device structure
269 *
270 * If any port has noticed a Tx timeout, it is likely that the whole
271 * device is munged, not just the one netdev port, so go for the full
272 * reset.
273 **/
274static void i40e_tx_timeout(struct net_device *netdev)
275{
276 struct i40e_netdev_priv *np = netdev_priv(netdev);
277 struct i40e_vsi *vsi = np->vsi;
278 struct i40e_pf *pf = vsi->back;
279
280 pf->tx_timeout_count++;
281
282 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
283 pf->tx_timeout_recovery_level = 0;
284 pf->tx_timeout_last_recovery = jiffies;
285 netdev_info(netdev, "tx_timeout recovery level %d\n",
286 pf->tx_timeout_recovery_level);
287
288 switch (pf->tx_timeout_recovery_level) {
289 case 0:
290 /* disable and re-enable queues for the VSI */
291 if (in_interrupt()) {
292 set_bit(__I40E_REINIT_REQUESTED, &pf->state);
293 set_bit(__I40E_REINIT_REQUESTED, &vsi->state);
294 } else {
295 i40e_vsi_reinit_locked(vsi);
296 }
297 break;
298 case 1:
299 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
300 break;
301 case 2:
302 set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
303 break;
304 case 3:
305 set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
306 break;
307 default:
308 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
Akeem G Abodunrine108b0e2014-02-13 03:48:43 -0800309 set_bit(__I40E_DOWN, &vsi->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000310 i40e_down(vsi);
311 break;
312 }
313 i40e_service_event_schedule(pf);
314 pf->tx_timeout_recovery_level++;
315}
316
317/**
318 * i40e_release_rx_desc - Store the new tail and head values
319 * @rx_ring: ring to bump
320 * @val: new head index
321 **/
322static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
323{
324 rx_ring->next_to_use = val;
325
326 /* Force memory writes to complete before letting h/w
327 * know there are new descriptors to fetch. (Only
328 * applicable for weak-ordered memory model archs,
329 * such as IA-64).
330 */
331 wmb();
332 writel(val, rx_ring->tail);
333}
334
335/**
336 * i40e_get_vsi_stats_struct - Get System Network Statistics
337 * @vsi: the VSI we care about
338 *
339 * Returns the address of the device statistics structure.
340 * The statistics are actually updated from the service task.
341 **/
342struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
343{
344 return &vsi->net_stats;
345}
346
347/**
348 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
349 * @netdev: network interface device structure
350 *
351 * Returns the address of the device statistics structure.
352 * The statistics are actually updated from the service task.
353 **/
354static struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
355 struct net_device *netdev,
Alexander Duyck980e9b12013-09-28 06:01:03 +0000356 struct rtnl_link_stats64 *stats)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000357{
358 struct i40e_netdev_priv *np = netdev_priv(netdev);
359 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000360 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
361 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000362
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000363 if (test_bit(__I40E_DOWN, &vsi->state))
364 return stats;
365
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800366 if (!vsi->tx_rings)
367 return stats;
368
Alexander Duyck980e9b12013-09-28 06:01:03 +0000369 rcu_read_lock();
370 for (i = 0; i < vsi->num_queue_pairs; i++) {
371 struct i40e_ring *tx_ring, *rx_ring;
372 u64 bytes, packets;
373 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000374
Alexander Duyck980e9b12013-09-28 06:01:03 +0000375 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
376 if (!tx_ring)
377 continue;
378
379 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700380 start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000381 packets = tx_ring->stats.packets;
382 bytes = tx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700383 } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000384
385 stats->tx_packets += packets;
386 stats->tx_bytes += bytes;
387 rx_ring = &tx_ring[1];
388
389 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700390 start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000391 packets = rx_ring->stats.packets;
392 bytes = rx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700393 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000394
395 stats->rx_packets += packets;
396 stats->rx_bytes += bytes;
397 }
398 rcu_read_unlock();
399
400 /* following stats updated by ixgbe_watchdog_task() */
401 stats->multicast = vsi_stats->multicast;
402 stats->tx_errors = vsi_stats->tx_errors;
403 stats->tx_dropped = vsi_stats->tx_dropped;
404 stats->rx_errors = vsi_stats->rx_errors;
405 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
406 stats->rx_length_errors = vsi_stats->rx_length_errors;
407
408 return stats;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000409}
410
411/**
412 * i40e_vsi_reset_stats - Resets all stats of the given vsi
413 * @vsi: the VSI to have its stats reset
414 **/
415void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
416{
417 struct rtnl_link_stats64 *ns;
418 int i;
419
420 if (!vsi)
421 return;
422
423 ns = i40e_get_vsi_stats_struct(vsi);
424 memset(ns, 0, sizeof(*ns));
425 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
426 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
427 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
Greg Rose8e9dca52013-12-18 13:45:53 +0000428 if (vsi->rx_rings && vsi->rx_rings[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000429 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000430 memset(&vsi->rx_rings[i]->stats, 0 ,
431 sizeof(vsi->rx_rings[i]->stats));
432 memset(&vsi->rx_rings[i]->rx_stats, 0 ,
433 sizeof(vsi->rx_rings[i]->rx_stats));
434 memset(&vsi->tx_rings[i]->stats, 0 ,
435 sizeof(vsi->tx_rings[i]->stats));
436 memset(&vsi->tx_rings[i]->tx_stats, 0,
437 sizeof(vsi->tx_rings[i]->tx_stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000438 }
Greg Rose8e9dca52013-12-18 13:45:53 +0000439 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000440 vsi->stat_offsets_loaded = false;
441}
442
443/**
444 * i40e_pf_reset_stats - Reset all of the stats for the given pf
445 * @pf: the PF to be reset
446 **/
447void i40e_pf_reset_stats(struct i40e_pf *pf)
448{
449 memset(&pf->stats, 0, sizeof(pf->stats));
450 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
451 pf->stat_offsets_loaded = false;
452}
453
454/**
455 * i40e_stat_update48 - read and update a 48 bit stat from the chip
456 * @hw: ptr to the hardware info
457 * @hireg: the high 32 bit reg to read
458 * @loreg: the low 32 bit reg to read
459 * @offset_loaded: has the initial offset been loaded yet
460 * @offset: ptr to current offset value
461 * @stat: ptr to the stat
462 *
463 * Since the device stats are not reset at PFReset, they likely will not
464 * be zeroed when the driver starts. We'll save the first values read
465 * and use them as offsets to be subtracted from the raw values in order
466 * to report stats that count from zero. In the process, we also manage
467 * the potential roll-over.
468 **/
469static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
470 bool offset_loaded, u64 *offset, u64 *stat)
471{
472 u64 new_data;
473
Shannon Nelsonab600852014-01-17 15:36:39 -0800474 if (hw->device_id == I40E_DEV_ID_QEMU) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000475 new_data = rd32(hw, loreg);
476 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
477 } else {
478 new_data = rd64(hw, loreg);
479 }
480 if (!offset_loaded)
481 *offset = new_data;
482 if (likely(new_data >= *offset))
483 *stat = new_data - *offset;
484 else
485 *stat = (new_data + ((u64)1 << 48)) - *offset;
486 *stat &= 0xFFFFFFFFFFFFULL;
487}
488
489/**
490 * i40e_stat_update32 - read and update a 32 bit stat from the chip
491 * @hw: ptr to the hardware info
492 * @reg: the hw reg to read
493 * @offset_loaded: has the initial offset been loaded yet
494 * @offset: ptr to current offset value
495 * @stat: ptr to the stat
496 **/
497static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
498 bool offset_loaded, u64 *offset, u64 *stat)
499{
500 u32 new_data;
501
502 new_data = rd32(hw, reg);
503 if (!offset_loaded)
504 *offset = new_data;
505 if (likely(new_data >= *offset))
506 *stat = (u32)(new_data - *offset);
507 else
508 *stat = (u32)((new_data + ((u64)1 << 32)) - *offset);
509}
510
511/**
512 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
513 * @vsi: the VSI to be updated
514 **/
515void i40e_update_eth_stats(struct i40e_vsi *vsi)
516{
517 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
518 struct i40e_pf *pf = vsi->back;
519 struct i40e_hw *hw = &pf->hw;
520 struct i40e_eth_stats *oes;
521 struct i40e_eth_stats *es; /* device's eth stats */
522
523 es = &vsi->eth_stats;
524 oes = &vsi->eth_stats_offsets;
525
526 /* Gather up the stats that the hw collects */
527 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
528 vsi->stat_offsets_loaded,
529 &oes->tx_errors, &es->tx_errors);
530 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
531 vsi->stat_offsets_loaded,
532 &oes->rx_discards, &es->rx_discards);
533
534 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
535 I40E_GLV_GORCL(stat_idx),
536 vsi->stat_offsets_loaded,
537 &oes->rx_bytes, &es->rx_bytes);
538 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
539 I40E_GLV_UPRCL(stat_idx),
540 vsi->stat_offsets_loaded,
541 &oes->rx_unicast, &es->rx_unicast);
542 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
543 I40E_GLV_MPRCL(stat_idx),
544 vsi->stat_offsets_loaded,
545 &oes->rx_multicast, &es->rx_multicast);
546 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
547 I40E_GLV_BPRCL(stat_idx),
548 vsi->stat_offsets_loaded,
549 &oes->rx_broadcast, &es->rx_broadcast);
550
551 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
552 I40E_GLV_GOTCL(stat_idx),
553 vsi->stat_offsets_loaded,
554 &oes->tx_bytes, &es->tx_bytes);
555 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
556 I40E_GLV_UPTCL(stat_idx),
557 vsi->stat_offsets_loaded,
558 &oes->tx_unicast, &es->tx_unicast);
559 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
560 I40E_GLV_MPTCL(stat_idx),
561 vsi->stat_offsets_loaded,
562 &oes->tx_multicast, &es->tx_multicast);
563 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
564 I40E_GLV_BPTCL(stat_idx),
565 vsi->stat_offsets_loaded,
566 &oes->tx_broadcast, &es->tx_broadcast);
567 vsi->stat_offsets_loaded = true;
568}
569
570/**
571 * i40e_update_veb_stats - Update Switch component statistics
572 * @veb: the VEB being updated
573 **/
574static void i40e_update_veb_stats(struct i40e_veb *veb)
575{
576 struct i40e_pf *pf = veb->pf;
577 struct i40e_hw *hw = &pf->hw;
578 struct i40e_eth_stats *oes;
579 struct i40e_eth_stats *es; /* device's eth stats */
580 int idx = 0;
581
582 idx = veb->stats_idx;
583 es = &veb->stats;
584 oes = &veb->stats_offsets;
585
586 /* Gather up the stats that the hw collects */
587 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
588 veb->stat_offsets_loaded,
589 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000590 if (hw->revision_id > 0)
591 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
592 veb->stat_offsets_loaded,
593 &oes->rx_unknown_protocol,
594 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000595 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
596 veb->stat_offsets_loaded,
597 &oes->rx_bytes, &es->rx_bytes);
598 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
599 veb->stat_offsets_loaded,
600 &oes->rx_unicast, &es->rx_unicast);
601 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
602 veb->stat_offsets_loaded,
603 &oes->rx_multicast, &es->rx_multicast);
604 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
605 veb->stat_offsets_loaded,
606 &oes->rx_broadcast, &es->rx_broadcast);
607
608 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
609 veb->stat_offsets_loaded,
610 &oes->tx_bytes, &es->tx_bytes);
611 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
612 veb->stat_offsets_loaded,
613 &oes->tx_unicast, &es->tx_unicast);
614 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
615 veb->stat_offsets_loaded,
616 &oes->tx_multicast, &es->tx_multicast);
617 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
618 veb->stat_offsets_loaded,
619 &oes->tx_broadcast, &es->tx_broadcast);
620 veb->stat_offsets_loaded = true;
621}
622
623/**
624 * i40e_update_link_xoff_rx - Update XOFF received in link flow control mode
625 * @pf: the corresponding PF
626 *
627 * Update the Rx XOFF counter (PAUSE frames) in link flow control mode
628 **/
629static void i40e_update_link_xoff_rx(struct i40e_pf *pf)
630{
631 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
632 struct i40e_hw_port_stats *nsd = &pf->stats;
633 struct i40e_hw *hw = &pf->hw;
634 u64 xoff = 0;
635 u16 i, v;
636
637 if ((hw->fc.current_mode != I40E_FC_FULL) &&
638 (hw->fc.current_mode != I40E_FC_RX_PAUSE))
639 return;
640
641 xoff = nsd->link_xoff_rx;
642 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
643 pf->stat_offsets_loaded,
644 &osd->link_xoff_rx, &nsd->link_xoff_rx);
645
646 /* No new LFC xoff rx */
647 if (!(nsd->link_xoff_rx - xoff))
648 return;
649
650 /* Clear the __I40E_HANG_CHECK_ARMED bit for all Tx rings */
651 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
652 struct i40e_vsi *vsi = pf->vsi[v];
653
654 if (!vsi)
655 continue;
656
657 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000658 struct i40e_ring *ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000659 clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
660 }
661 }
662}
663
664/**
665 * i40e_update_prio_xoff_rx - Update XOFF received in PFC mode
666 * @pf: the corresponding PF
667 *
668 * Update the Rx XOFF counter (PAUSE frames) in PFC mode
669 **/
670static void i40e_update_prio_xoff_rx(struct i40e_pf *pf)
671{
672 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
673 struct i40e_hw_port_stats *nsd = &pf->stats;
674 bool xoff[I40E_MAX_TRAFFIC_CLASS] = {false};
675 struct i40e_dcbx_config *dcb_cfg;
676 struct i40e_hw *hw = &pf->hw;
677 u16 i, v;
678 u8 tc;
679
680 dcb_cfg = &hw->local_dcbx_config;
681
682 /* See if DCB enabled with PFC TC */
683 if (!(pf->flags & I40E_FLAG_DCB_ENABLED) ||
684 !(dcb_cfg->pfc.pfcenable)) {
685 i40e_update_link_xoff_rx(pf);
686 return;
687 }
688
689 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
690 u64 prio_xoff = nsd->priority_xoff_rx[i];
691 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
692 pf->stat_offsets_loaded,
693 &osd->priority_xoff_rx[i],
694 &nsd->priority_xoff_rx[i]);
695
696 /* No new PFC xoff rx */
697 if (!(nsd->priority_xoff_rx[i] - prio_xoff))
698 continue;
699 /* Get the TC for given priority */
700 tc = dcb_cfg->etscfg.prioritytable[i];
701 xoff[tc] = true;
702 }
703
704 /* Clear the __I40E_HANG_CHECK_ARMED bit for Tx rings */
705 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
706 struct i40e_vsi *vsi = pf->vsi[v];
707
708 if (!vsi)
709 continue;
710
711 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000712 struct i40e_ring *ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000713
714 tc = ring->dcb_tc;
715 if (xoff[tc])
716 clear_bit(__I40E_HANG_CHECK_ARMED,
717 &ring->state);
718 }
719 }
720}
721
722/**
723 * i40e_update_stats - Update the board statistics counters.
724 * @vsi: the VSI to be updated
725 *
726 * There are a few instances where we store the same stat in a
727 * couple of different structs. This is partly because we have
728 * the netdev stats that need to be filled out, which is slightly
729 * different from the "eth_stats" defined by the chip and used in
730 * VF communications. We sort it all out here in a central place.
731 **/
732void i40e_update_stats(struct i40e_vsi *vsi)
733{
734 struct i40e_pf *pf = vsi->back;
735 struct i40e_hw *hw = &pf->hw;
736 struct rtnl_link_stats64 *ons;
737 struct rtnl_link_stats64 *ns; /* netdev stats */
738 struct i40e_eth_stats *oes;
739 struct i40e_eth_stats *es; /* device's eth stats */
740 u32 tx_restart, tx_busy;
741 u32 rx_page, rx_buf;
742 u64 rx_p, rx_b;
743 u64 tx_p, tx_b;
Anjali Singhai Jainbee5af72014-03-06 08:59:50 +0000744 u32 val;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000745 int i;
746 u16 q;
747
748 if (test_bit(__I40E_DOWN, &vsi->state) ||
749 test_bit(__I40E_CONFIG_BUSY, &pf->state))
750 return;
751
752 ns = i40e_get_vsi_stats_struct(vsi);
753 ons = &vsi->net_stats_offsets;
754 es = &vsi->eth_stats;
755 oes = &vsi->eth_stats_offsets;
756
757 /* Gather up the netdev and vsi stats that the driver collects
758 * on the fly during packet processing
759 */
760 rx_b = rx_p = 0;
761 tx_b = tx_p = 0;
762 tx_restart = tx_busy = 0;
763 rx_page = 0;
764 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000765 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000766 for (q = 0; q < vsi->num_queue_pairs; q++) {
767 struct i40e_ring *p;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000768 u64 bytes, packets;
769 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000770
Alexander Duyck980e9b12013-09-28 06:01:03 +0000771 /* locate Tx ring */
772 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000773
Alexander Duyck980e9b12013-09-28 06:01:03 +0000774 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700775 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000776 packets = p->stats.packets;
777 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700778 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000779 tx_b += bytes;
780 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000781 tx_restart += p->tx_stats.restart_queue;
782 tx_busy += p->tx_stats.tx_busy;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000783
784 /* Rx queue is part of the same block as Tx queue */
785 p = &p[1];
786 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700787 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000788 packets = p->stats.packets;
789 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700790 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000791 rx_b += bytes;
792 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000793 rx_buf += p->rx_stats.alloc_buff_failed;
794 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000795 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000796 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000797 vsi->tx_restart = tx_restart;
798 vsi->tx_busy = tx_busy;
799 vsi->rx_page_failed = rx_page;
800 vsi->rx_buf_failed = rx_buf;
801
802 ns->rx_packets = rx_p;
803 ns->rx_bytes = rx_b;
804 ns->tx_packets = tx_p;
805 ns->tx_bytes = tx_b;
806
807 i40e_update_eth_stats(vsi);
808 /* update netdev stats from eth stats */
809 ons->rx_errors = oes->rx_errors;
810 ns->rx_errors = es->rx_errors;
811 ons->tx_errors = oes->tx_errors;
812 ns->tx_errors = es->tx_errors;
813 ons->multicast = oes->rx_multicast;
814 ns->multicast = es->rx_multicast;
815 ons->tx_dropped = oes->tx_discards;
816 ns->tx_dropped = es->tx_discards;
817
818 /* Get the port data only if this is the main PF VSI */
819 if (vsi == pf->vsi[pf->lan_vsi]) {
820 struct i40e_hw_port_stats *nsd = &pf->stats;
821 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
822
823 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
824 I40E_GLPRT_GORCL(hw->port),
825 pf->stat_offsets_loaded,
826 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
827 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
828 I40E_GLPRT_GOTCL(hw->port),
829 pf->stat_offsets_loaded,
830 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
831 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
832 pf->stat_offsets_loaded,
833 &osd->eth.rx_discards,
834 &nsd->eth.rx_discards);
835 i40e_stat_update32(hw, I40E_GLPRT_TDPC(hw->port),
836 pf->stat_offsets_loaded,
837 &osd->eth.tx_discards,
838 &nsd->eth.tx_discards);
839 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
840 I40E_GLPRT_MPRCL(hw->port),
841 pf->stat_offsets_loaded,
842 &osd->eth.rx_multicast,
843 &nsd->eth.rx_multicast);
844
845 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
846 pf->stat_offsets_loaded,
847 &osd->tx_dropped_link_down,
848 &nsd->tx_dropped_link_down);
849
850 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
851 pf->stat_offsets_loaded,
852 &osd->crc_errors, &nsd->crc_errors);
853 ns->rx_crc_errors = nsd->crc_errors;
854
855 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
856 pf->stat_offsets_loaded,
857 &osd->illegal_bytes, &nsd->illegal_bytes);
858 ns->rx_errors = nsd->crc_errors
859 + nsd->illegal_bytes;
860
861 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
862 pf->stat_offsets_loaded,
863 &osd->mac_local_faults,
864 &nsd->mac_local_faults);
865 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
866 pf->stat_offsets_loaded,
867 &osd->mac_remote_faults,
868 &nsd->mac_remote_faults);
869
870 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
871 pf->stat_offsets_loaded,
872 &osd->rx_length_errors,
873 &nsd->rx_length_errors);
874 ns->rx_length_errors = nsd->rx_length_errors;
875
876 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
877 pf->stat_offsets_loaded,
878 &osd->link_xon_rx, &nsd->link_xon_rx);
879 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
880 pf->stat_offsets_loaded,
881 &osd->link_xon_tx, &nsd->link_xon_tx);
882 i40e_update_prio_xoff_rx(pf); /* handles I40E_GLPRT_LXOFFRXC */
883 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
884 pf->stat_offsets_loaded,
885 &osd->link_xoff_tx, &nsd->link_xoff_tx);
886
887 for (i = 0; i < 8; i++) {
888 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
889 pf->stat_offsets_loaded,
890 &osd->priority_xon_rx[i],
891 &nsd->priority_xon_rx[i]);
892 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
893 pf->stat_offsets_loaded,
894 &osd->priority_xon_tx[i],
895 &nsd->priority_xon_tx[i]);
896 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
897 pf->stat_offsets_loaded,
898 &osd->priority_xoff_tx[i],
899 &nsd->priority_xoff_tx[i]);
900 i40e_stat_update32(hw,
901 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
902 pf->stat_offsets_loaded,
903 &osd->priority_xon_2_xoff[i],
904 &nsd->priority_xon_2_xoff[i]);
905 }
906
907 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
908 I40E_GLPRT_PRC64L(hw->port),
909 pf->stat_offsets_loaded,
910 &osd->rx_size_64, &nsd->rx_size_64);
911 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
912 I40E_GLPRT_PRC127L(hw->port),
913 pf->stat_offsets_loaded,
914 &osd->rx_size_127, &nsd->rx_size_127);
915 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
916 I40E_GLPRT_PRC255L(hw->port),
917 pf->stat_offsets_loaded,
918 &osd->rx_size_255, &nsd->rx_size_255);
919 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
920 I40E_GLPRT_PRC511L(hw->port),
921 pf->stat_offsets_loaded,
922 &osd->rx_size_511, &nsd->rx_size_511);
923 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
924 I40E_GLPRT_PRC1023L(hw->port),
925 pf->stat_offsets_loaded,
926 &osd->rx_size_1023, &nsd->rx_size_1023);
927 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
928 I40E_GLPRT_PRC1522L(hw->port),
929 pf->stat_offsets_loaded,
930 &osd->rx_size_1522, &nsd->rx_size_1522);
931 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
932 I40E_GLPRT_PRC9522L(hw->port),
933 pf->stat_offsets_loaded,
934 &osd->rx_size_big, &nsd->rx_size_big);
935
936 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
937 I40E_GLPRT_PTC64L(hw->port),
938 pf->stat_offsets_loaded,
939 &osd->tx_size_64, &nsd->tx_size_64);
940 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
941 I40E_GLPRT_PTC127L(hw->port),
942 pf->stat_offsets_loaded,
943 &osd->tx_size_127, &nsd->tx_size_127);
944 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
945 I40E_GLPRT_PTC255L(hw->port),
946 pf->stat_offsets_loaded,
947 &osd->tx_size_255, &nsd->tx_size_255);
948 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
949 I40E_GLPRT_PTC511L(hw->port),
950 pf->stat_offsets_loaded,
951 &osd->tx_size_511, &nsd->tx_size_511);
952 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
953 I40E_GLPRT_PTC1023L(hw->port),
954 pf->stat_offsets_loaded,
955 &osd->tx_size_1023, &nsd->tx_size_1023);
956 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
957 I40E_GLPRT_PTC1522L(hw->port),
958 pf->stat_offsets_loaded,
959 &osd->tx_size_1522, &nsd->tx_size_1522);
960 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
961 I40E_GLPRT_PTC9522L(hw->port),
962 pf->stat_offsets_loaded,
963 &osd->tx_size_big, &nsd->tx_size_big);
964
965 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
966 pf->stat_offsets_loaded,
967 &osd->rx_undersize, &nsd->rx_undersize);
968 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
969 pf->stat_offsets_loaded,
970 &osd->rx_fragments, &nsd->rx_fragments);
971 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
972 pf->stat_offsets_loaded,
973 &osd->rx_oversize, &nsd->rx_oversize);
974 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
975 pf->stat_offsets_loaded,
976 &osd->rx_jabber, &nsd->rx_jabber);
Anjali Singhai Jainbee5af72014-03-06 08:59:50 +0000977
978 val = rd32(hw, I40E_PRTPM_EEE_STAT);
979 nsd->tx_lpi_status =
980 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
981 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
982 nsd->rx_lpi_status =
983 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
984 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
985 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
986 pf->stat_offsets_loaded,
987 &osd->tx_lpi_count, &nsd->tx_lpi_count);
988 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
989 pf->stat_offsets_loaded,
990 &osd->rx_lpi_count, &nsd->rx_lpi_count);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000991 }
992
993 pf->stat_offsets_loaded = true;
994}
995
996/**
997 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
998 * @vsi: the VSI to be searched
999 * @macaddr: the MAC address
1000 * @vlan: the vlan
1001 * @is_vf: make sure its a vf filter, else doesn't matter
1002 * @is_netdev: make sure its a netdev filter, else doesn't matter
1003 *
1004 * Returns ptr to the filter object or NULL
1005 **/
1006static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1007 u8 *macaddr, s16 vlan,
1008 bool is_vf, bool is_netdev)
1009{
1010 struct i40e_mac_filter *f;
1011
1012 if (!vsi || !macaddr)
1013 return NULL;
1014
1015 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1016 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1017 (vlan == f->vlan) &&
1018 (!is_vf || f->is_vf) &&
1019 (!is_netdev || f->is_netdev))
1020 return f;
1021 }
1022 return NULL;
1023}
1024
1025/**
1026 * i40e_find_mac - Find a mac addr in the macvlan filters list
1027 * @vsi: the VSI to be searched
1028 * @macaddr: the MAC address we are searching for
1029 * @is_vf: make sure its a vf filter, else doesn't matter
1030 * @is_netdev: make sure its a netdev filter, else doesn't matter
1031 *
1032 * Returns the first filter with the provided MAC address or NULL if
1033 * MAC address was not found
1034 **/
1035struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, u8 *macaddr,
1036 bool is_vf, bool is_netdev)
1037{
1038 struct i40e_mac_filter *f;
1039
1040 if (!vsi || !macaddr)
1041 return NULL;
1042
1043 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1044 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1045 (!is_vf || f->is_vf) &&
1046 (!is_netdev || f->is_netdev))
1047 return f;
1048 }
1049 return NULL;
1050}
1051
1052/**
1053 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1054 * @vsi: the VSI to be searched
1055 *
1056 * Returns true if VSI is in vlan mode or false otherwise
1057 **/
1058bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1059{
1060 struct i40e_mac_filter *f;
1061
1062 /* Only -1 for all the filters denotes not in vlan mode
1063 * so we have to go through all the list in order to make sure
1064 */
1065 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1066 if (f->vlan >= 0)
1067 return true;
1068 }
1069
1070 return false;
1071}
1072
1073/**
1074 * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
1075 * @vsi: the VSI to be searched
1076 * @macaddr: the mac address to be filtered
1077 * @is_vf: true if it is a vf
1078 * @is_netdev: true if it is a netdev
1079 *
1080 * Goes through all the macvlan filters and adds a
1081 * macvlan filter for each unique vlan that already exists
1082 *
1083 * Returns first filter found on success, else NULL
1084 **/
1085struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi, u8 *macaddr,
1086 bool is_vf, bool is_netdev)
1087{
1088 struct i40e_mac_filter *f;
1089
1090 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1091 if (!i40e_find_filter(vsi, macaddr, f->vlan,
1092 is_vf, is_netdev)) {
1093 if (!i40e_add_filter(vsi, macaddr, f->vlan,
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -08001094 is_vf, is_netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001095 return NULL;
1096 }
1097 }
1098
1099 return list_first_entry_or_null(&vsi->mac_filter_list,
1100 struct i40e_mac_filter, list);
1101}
1102
1103/**
1104 * i40e_add_filter - Add a mac/vlan filter to the VSI
1105 * @vsi: the VSI to be searched
1106 * @macaddr: the MAC address
1107 * @vlan: the vlan
1108 * @is_vf: make sure its a vf filter, else doesn't matter
1109 * @is_netdev: make sure its a netdev filter, else doesn't matter
1110 *
1111 * Returns ptr to the filter object or NULL when no memory available.
1112 **/
1113struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1114 u8 *macaddr, s16 vlan,
1115 bool is_vf, bool is_netdev)
1116{
1117 struct i40e_mac_filter *f;
1118
1119 if (!vsi || !macaddr)
1120 return NULL;
1121
1122 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1123 if (!f) {
1124 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1125 if (!f)
1126 goto add_filter_out;
1127
1128 memcpy(f->macaddr, macaddr, ETH_ALEN);
1129 f->vlan = vlan;
1130 f->changed = true;
1131
1132 INIT_LIST_HEAD(&f->list);
1133 list_add(&f->list, &vsi->mac_filter_list);
1134 }
1135
1136 /* increment counter and add a new flag if needed */
1137 if (is_vf) {
1138 if (!f->is_vf) {
1139 f->is_vf = true;
1140 f->counter++;
1141 }
1142 } else if (is_netdev) {
1143 if (!f->is_netdev) {
1144 f->is_netdev = true;
1145 f->counter++;
1146 }
1147 } else {
1148 f->counter++;
1149 }
1150
1151 /* changed tells sync_filters_subtask to
1152 * push the filter down to the firmware
1153 */
1154 if (f->changed) {
1155 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1156 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1157 }
1158
1159add_filter_out:
1160 return f;
1161}
1162
1163/**
1164 * i40e_del_filter - Remove a mac/vlan filter from the VSI
1165 * @vsi: the VSI to be searched
1166 * @macaddr: the MAC address
1167 * @vlan: the vlan
1168 * @is_vf: make sure it's a vf filter, else doesn't matter
1169 * @is_netdev: make sure it's a netdev filter, else doesn't matter
1170 **/
1171void i40e_del_filter(struct i40e_vsi *vsi,
1172 u8 *macaddr, s16 vlan,
1173 bool is_vf, bool is_netdev)
1174{
1175 struct i40e_mac_filter *f;
1176
1177 if (!vsi || !macaddr)
1178 return;
1179
1180 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1181 if (!f || f->counter == 0)
1182 return;
1183
1184 if (is_vf) {
1185 if (f->is_vf) {
1186 f->is_vf = false;
1187 f->counter--;
1188 }
1189 } else if (is_netdev) {
1190 if (f->is_netdev) {
1191 f->is_netdev = false;
1192 f->counter--;
1193 }
1194 } else {
1195 /* make sure we don't remove a filter in use by vf or netdev */
1196 int min_f = 0;
1197 min_f += (f->is_vf ? 1 : 0);
1198 min_f += (f->is_netdev ? 1 : 0);
1199
1200 if (f->counter > min_f)
1201 f->counter--;
1202 }
1203
1204 /* counter == 0 tells sync_filters_subtask to
1205 * remove the filter from the firmware's list
1206 */
1207 if (f->counter == 0) {
1208 f->changed = true;
1209 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1210 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1211 }
1212}
1213
1214/**
1215 * i40e_set_mac - NDO callback to set mac address
1216 * @netdev: network interface device structure
1217 * @p: pointer to an address structure
1218 *
1219 * Returns 0 on success, negative on failure
1220 **/
1221static int i40e_set_mac(struct net_device *netdev, void *p)
1222{
1223 struct i40e_netdev_priv *np = netdev_priv(netdev);
1224 struct i40e_vsi *vsi = np->vsi;
1225 struct sockaddr *addr = p;
1226 struct i40e_mac_filter *f;
1227
1228 if (!is_valid_ether_addr(addr->sa_data))
1229 return -EADDRNOTAVAIL;
1230
1231 netdev_info(netdev, "set mac address=%pM\n", addr->sa_data);
1232
1233 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
1234 return 0;
1235
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001236 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1237 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1238 return -EADDRNOTAVAIL;
1239
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001240 if (vsi->type == I40E_VSI_MAIN) {
1241 i40e_status ret;
1242 ret = i40e_aq_mac_address_write(&vsi->back->hw,
1243 I40E_AQC_WRITE_TYPE_LAA_ONLY,
1244 addr->sa_data, NULL);
1245 if (ret) {
1246 netdev_info(netdev,
1247 "Addr change for Main VSI failed: %d\n",
1248 ret);
1249 return -EADDRNOTAVAIL;
1250 }
1251
1252 memcpy(vsi->back->hw.mac.addr, addr->sa_data, netdev->addr_len);
1253 }
1254
1255 /* In order to be sure to not drop any packets, add the new address
1256 * then delete the old one.
1257 */
1258 f = i40e_add_filter(vsi, addr->sa_data, I40E_VLAN_ANY, false, false);
1259 if (!f)
1260 return -ENOMEM;
1261
1262 i40e_sync_vsi_filters(vsi);
1263 i40e_del_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY, false, false);
1264 i40e_sync_vsi_filters(vsi);
1265
1266 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1267
1268 return 0;
1269}
1270
1271/**
1272 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1273 * @vsi: the VSI being setup
1274 * @ctxt: VSI context structure
1275 * @enabled_tc: Enabled TCs bitmap
1276 * @is_add: True if called before Add VSI
1277 *
1278 * Setup VSI queue mapping for enabled traffic classes.
1279 **/
1280static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1281 struct i40e_vsi_context *ctxt,
1282 u8 enabled_tc,
1283 bool is_add)
1284{
1285 struct i40e_pf *pf = vsi->back;
1286 u16 sections = 0;
1287 u8 netdev_tc = 0;
1288 u16 numtc = 0;
1289 u16 qcount;
1290 u8 offset;
1291 u16 qmap;
1292 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001293 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001294
1295 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1296 offset = 0;
1297
1298 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1299 /* Find numtc from enabled TC bitmap */
1300 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1301 if (enabled_tc & (1 << i)) /* TC is enabled */
1302 numtc++;
1303 }
1304 if (!numtc) {
1305 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1306 numtc = 1;
1307 }
1308 } else {
1309 /* At least TC0 is enabled in case of non-DCB case */
1310 numtc = 1;
1311 }
1312
1313 vsi->tc_config.numtc = numtc;
1314 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001315 /* Number of queues per enabled TC */
1316 num_tc_qps = rounddown_pow_of_two(vsi->alloc_queue_pairs/numtc);
1317 num_tc_qps = min_t(int, num_tc_qps, I40E_MAX_QUEUES_PER_TC);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001318
1319 /* Setup queue offset/count for all TCs for given VSI */
1320 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1321 /* See if the given TC is enabled for the given VSI */
1322 if (vsi->tc_config.enabled_tc & (1 << i)) { /* TC is enabled */
1323 int pow, num_qps;
1324
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001325 switch (vsi->type) {
1326 case I40E_VSI_MAIN:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001327 qcount = min_t(int, pf->rss_size, num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001328 break;
1329 case I40E_VSI_FDIR:
1330 case I40E_VSI_SRIOV:
1331 case I40E_VSI_VMDQ2:
1332 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001333 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001334 WARN_ON(i != 0);
1335 break;
1336 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001337 vsi->tc_config.tc_info[i].qoffset = offset;
1338 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001339
1340 /* find the power-of-2 of the number of queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001341 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001342 pow = 0;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001343 while (num_qps && ((1 << pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001344 pow++;
1345 num_qps >>= 1;
1346 }
1347
1348 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1349 qmap =
1350 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1351 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1352
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001353 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001354 } else {
1355 /* TC is not enabled so set the offset to
1356 * default queue and allocate one queue
1357 * for the given TC.
1358 */
1359 vsi->tc_config.tc_info[i].qoffset = 0;
1360 vsi->tc_config.tc_info[i].qcount = 1;
1361 vsi->tc_config.tc_info[i].netdev_tc = 0;
1362
1363 qmap = 0;
1364 }
1365 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1366 }
1367
1368 /* Set actual Tx/Rx queue pairs */
1369 vsi->num_queue_pairs = offset;
1370
1371 /* Scheduler section valid can only be set for ADD VSI */
1372 if (is_add) {
1373 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1374
1375 ctxt->info.up_enable_bits = enabled_tc;
1376 }
1377 if (vsi->type == I40E_VSI_SRIOV) {
1378 ctxt->info.mapping_flags |=
1379 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1380 for (i = 0; i < vsi->num_queue_pairs; i++)
1381 ctxt->info.queue_mapping[i] =
1382 cpu_to_le16(vsi->base_queue + i);
1383 } else {
1384 ctxt->info.mapping_flags |=
1385 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1386 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1387 }
1388 ctxt->info.valid_sections |= cpu_to_le16(sections);
1389}
1390
1391/**
1392 * i40e_set_rx_mode - NDO callback to set the netdev filters
1393 * @netdev: network interface device structure
1394 **/
1395static void i40e_set_rx_mode(struct net_device *netdev)
1396{
1397 struct i40e_netdev_priv *np = netdev_priv(netdev);
1398 struct i40e_mac_filter *f, *ftmp;
1399 struct i40e_vsi *vsi = np->vsi;
1400 struct netdev_hw_addr *uca;
1401 struct netdev_hw_addr *mca;
1402 struct netdev_hw_addr *ha;
1403
1404 /* add addr if not already in the filter list */
1405 netdev_for_each_uc_addr(uca, netdev) {
1406 if (!i40e_find_mac(vsi, uca->addr, false, true)) {
1407 if (i40e_is_vsi_in_vlan(vsi))
1408 i40e_put_mac_in_vlan(vsi, uca->addr,
1409 false, true);
1410 else
1411 i40e_add_filter(vsi, uca->addr, I40E_VLAN_ANY,
1412 false, true);
1413 }
1414 }
1415
1416 netdev_for_each_mc_addr(mca, netdev) {
1417 if (!i40e_find_mac(vsi, mca->addr, false, true)) {
1418 if (i40e_is_vsi_in_vlan(vsi))
1419 i40e_put_mac_in_vlan(vsi, mca->addr,
1420 false, true);
1421 else
1422 i40e_add_filter(vsi, mca->addr, I40E_VLAN_ANY,
1423 false, true);
1424 }
1425 }
1426
1427 /* remove filter if not in netdev list */
1428 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1429 bool found = false;
1430
1431 if (!f->is_netdev)
1432 continue;
1433
1434 if (is_multicast_ether_addr(f->macaddr)) {
1435 netdev_for_each_mc_addr(mca, netdev) {
1436 if (ether_addr_equal(mca->addr, f->macaddr)) {
1437 found = true;
1438 break;
1439 }
1440 }
1441 } else {
1442 netdev_for_each_uc_addr(uca, netdev) {
1443 if (ether_addr_equal(uca->addr, f->macaddr)) {
1444 found = true;
1445 break;
1446 }
1447 }
1448
1449 for_each_dev_addr(netdev, ha) {
1450 if (ether_addr_equal(ha->addr, f->macaddr)) {
1451 found = true;
1452 break;
1453 }
1454 }
1455 }
1456 if (!found)
1457 i40e_del_filter(
1458 vsi, f->macaddr, I40E_VLAN_ANY, false, true);
1459 }
1460
1461 /* check for other flag changes */
1462 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1463 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1464 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1465 }
1466}
1467
1468/**
1469 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1470 * @vsi: ptr to the VSI
1471 *
1472 * Push any outstanding VSI filter changes through the AdminQ.
1473 *
1474 * Returns 0 or error value
1475 **/
1476int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
1477{
1478 struct i40e_mac_filter *f, *ftmp;
1479 bool promisc_forced_on = false;
1480 bool add_happened = false;
1481 int filter_list_len = 0;
1482 u32 changed_flags = 0;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001483 i40e_status aq_ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001484 struct i40e_pf *pf;
1485 int num_add = 0;
1486 int num_del = 0;
1487 u16 cmd_flags;
1488
1489 /* empty array typed pointers, kcalloc later */
1490 struct i40e_aqc_add_macvlan_element_data *add_list;
1491 struct i40e_aqc_remove_macvlan_element_data *del_list;
1492
1493 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1494 usleep_range(1000, 2000);
1495 pf = vsi->back;
1496
1497 if (vsi->netdev) {
1498 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1499 vsi->current_netdev_flags = vsi->netdev->flags;
1500 }
1501
1502 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1503 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1504
1505 filter_list_len = pf->hw.aq.asq_buf_size /
1506 sizeof(struct i40e_aqc_remove_macvlan_element_data);
1507 del_list = kcalloc(filter_list_len,
1508 sizeof(struct i40e_aqc_remove_macvlan_element_data),
1509 GFP_KERNEL);
1510 if (!del_list)
1511 return -ENOMEM;
1512
1513 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1514 if (!f->changed)
1515 continue;
1516
1517 if (f->counter != 0)
1518 continue;
1519 f->changed = false;
1520 cmd_flags = 0;
1521
1522 /* add to delete list */
1523 memcpy(del_list[num_del].mac_addr,
1524 f->macaddr, ETH_ALEN);
1525 del_list[num_del].vlan_tag =
1526 cpu_to_le16((u16)(f->vlan ==
1527 I40E_VLAN_ANY ? 0 : f->vlan));
1528
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001529 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1530 del_list[num_del].flags = cmd_flags;
1531 num_del++;
1532
1533 /* unlink from filter list */
1534 list_del(&f->list);
1535 kfree(f);
1536
1537 /* flush a full buffer */
1538 if (num_del == filter_list_len) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001539 aq_ret = i40e_aq_remove_macvlan(&pf->hw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001540 vsi->seid, del_list, num_del,
1541 NULL);
1542 num_del = 0;
1543 memset(del_list, 0, sizeof(*del_list));
1544
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001545 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001546 dev_info(&pf->pdev->dev,
1547 "ignoring delete macvlan error, err %d, aq_err %d while flushing a full buffer\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001548 aq_ret,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001549 pf->hw.aq.asq_last_status);
1550 }
1551 }
1552 if (num_del) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001553 aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001554 del_list, num_del, NULL);
1555 num_del = 0;
1556
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001557 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001558 dev_info(&pf->pdev->dev,
1559 "ignoring delete macvlan error, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001560 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001561 }
1562
1563 kfree(del_list);
1564 del_list = NULL;
1565
1566 /* do all the adds now */
1567 filter_list_len = pf->hw.aq.asq_buf_size /
1568 sizeof(struct i40e_aqc_add_macvlan_element_data),
1569 add_list = kcalloc(filter_list_len,
1570 sizeof(struct i40e_aqc_add_macvlan_element_data),
1571 GFP_KERNEL);
1572 if (!add_list)
1573 return -ENOMEM;
1574
1575 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1576 if (!f->changed)
1577 continue;
1578
1579 if (f->counter == 0)
1580 continue;
1581 f->changed = false;
1582 add_happened = true;
1583 cmd_flags = 0;
1584
1585 /* add to add array */
1586 memcpy(add_list[num_add].mac_addr,
1587 f->macaddr, ETH_ALEN);
1588 add_list[num_add].vlan_tag =
1589 cpu_to_le16(
1590 (u16)(f->vlan == I40E_VLAN_ANY ? 0 : f->vlan));
1591 add_list[num_add].queue_number = 0;
1592
1593 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001594 add_list[num_add].flags = cpu_to_le16(cmd_flags);
1595 num_add++;
1596
1597 /* flush a full buffer */
1598 if (num_add == filter_list_len) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001599 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1600 add_list, num_add,
1601 NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001602 num_add = 0;
1603
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001604 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001605 break;
1606 memset(add_list, 0, sizeof(*add_list));
1607 }
1608 }
1609 if (num_add) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001610 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1611 add_list, num_add, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001612 num_add = 0;
1613 }
1614 kfree(add_list);
1615 add_list = NULL;
1616
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001617 if (add_happened && (!aq_ret)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001618 /* do nothing */;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001619 } else if (add_happened && (aq_ret)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001620 dev_info(&pf->pdev->dev,
1621 "add filter failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001622 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001623 if ((pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOSPC) &&
1624 !test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1625 &vsi->state)) {
1626 promisc_forced_on = true;
1627 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1628 &vsi->state);
1629 dev_info(&pf->pdev->dev, "promiscuous mode forced on\n");
1630 }
1631 }
1632 }
1633
1634 /* check for changes in promiscuous modes */
1635 if (changed_flags & IFF_ALLMULTI) {
1636 bool cur_multipromisc;
1637 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001638 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
1639 vsi->seid,
1640 cur_multipromisc,
1641 NULL);
1642 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001643 dev_info(&pf->pdev->dev,
1644 "set multi promisc failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001645 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001646 }
1647 if ((changed_flags & IFF_PROMISC) || promisc_forced_on) {
1648 bool cur_promisc;
1649 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
1650 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1651 &vsi->state));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001652 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(&vsi->back->hw,
1653 vsi->seid,
1654 cur_promisc, NULL);
1655 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001656 dev_info(&pf->pdev->dev,
1657 "set uni promisc failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001658 aq_ret, pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00001659 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
1660 vsi->seid,
1661 cur_promisc, NULL);
1662 if (aq_ret)
1663 dev_info(&pf->pdev->dev,
1664 "set brdcast promisc failed, err %d, aq_err %d\n",
1665 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001666 }
1667
1668 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
1669 return 0;
1670}
1671
1672/**
1673 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
1674 * @pf: board private structure
1675 **/
1676static void i40e_sync_filters_subtask(struct i40e_pf *pf)
1677{
1678 int v;
1679
1680 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
1681 return;
1682 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
1683
1684 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
1685 if (pf->vsi[v] &&
1686 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED))
1687 i40e_sync_vsi_filters(pf->vsi[v]);
1688 }
1689}
1690
1691/**
1692 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
1693 * @netdev: network interface device structure
1694 * @new_mtu: new value for maximum frame size
1695 *
1696 * Returns 0 on success, negative on failure
1697 **/
1698static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
1699{
1700 struct i40e_netdev_priv *np = netdev_priv(netdev);
1701 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
1702 struct i40e_vsi *vsi = np->vsi;
1703
1704 /* MTU < 68 is an error and causes problems on some kernels */
1705 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
1706 return -EINVAL;
1707
1708 netdev_info(netdev, "changing MTU from %d to %d\n",
1709 netdev->mtu, new_mtu);
1710 netdev->mtu = new_mtu;
1711 if (netif_running(netdev))
1712 i40e_vsi_reinit_locked(vsi);
1713
1714 return 0;
1715}
1716
1717/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00001718 * i40e_ioctl - Access the hwtstamp interface
1719 * @netdev: network interface device structure
1720 * @ifr: interface request data
1721 * @cmd: ioctl command
1722 **/
1723int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1724{
1725 struct i40e_netdev_priv *np = netdev_priv(netdev);
1726 struct i40e_pf *pf = np->vsi->back;
1727
1728 switch (cmd) {
1729 case SIOCGHWTSTAMP:
1730 return i40e_ptp_get_ts_config(pf, ifr);
1731 case SIOCSHWTSTAMP:
1732 return i40e_ptp_set_ts_config(pf, ifr);
1733 default:
1734 return -EOPNOTSUPP;
1735 }
1736}
1737
1738/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001739 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
1740 * @vsi: the vsi being adjusted
1741 **/
1742void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
1743{
1744 struct i40e_vsi_context ctxt;
1745 i40e_status ret;
1746
1747 if ((vsi->info.valid_sections &
1748 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1749 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
1750 return; /* already enabled */
1751
1752 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1753 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1754 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
1755
1756 ctxt.seid = vsi->seid;
1757 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1758 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1759 if (ret) {
1760 dev_info(&vsi->back->pdev->dev,
1761 "%s: update vsi failed, aq_err=%d\n",
1762 __func__, vsi->back->hw.aq.asq_last_status);
1763 }
1764}
1765
1766/**
1767 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
1768 * @vsi: the vsi being adjusted
1769 **/
1770void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
1771{
1772 struct i40e_vsi_context ctxt;
1773 i40e_status ret;
1774
1775 if ((vsi->info.valid_sections &
1776 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1777 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
1778 I40E_AQ_VSI_PVLAN_EMOD_MASK))
1779 return; /* already disabled */
1780
1781 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1782 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1783 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
1784
1785 ctxt.seid = vsi->seid;
1786 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1787 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1788 if (ret) {
1789 dev_info(&vsi->back->pdev->dev,
1790 "%s: update vsi failed, aq_err=%d\n",
1791 __func__, vsi->back->hw.aq.asq_last_status);
1792 }
1793}
1794
1795/**
1796 * i40e_vlan_rx_register - Setup or shutdown vlan offload
1797 * @netdev: network interface to be adjusted
1798 * @features: netdev features to test if VLAN offload is enabled or not
1799 **/
1800static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
1801{
1802 struct i40e_netdev_priv *np = netdev_priv(netdev);
1803 struct i40e_vsi *vsi = np->vsi;
1804
1805 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1806 i40e_vlan_stripping_enable(vsi);
1807 else
1808 i40e_vlan_stripping_disable(vsi);
1809}
1810
1811/**
1812 * i40e_vsi_add_vlan - Add vsi membership for given vlan
1813 * @vsi: the vsi being configured
1814 * @vid: vlan id to be added (0 = untagged only , -1 = any)
1815 **/
1816int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
1817{
1818 struct i40e_mac_filter *f, *add_f;
1819 bool is_netdev, is_vf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001820
1821 is_vf = (vsi->type == I40E_VSI_SRIOV);
1822 is_netdev = !!(vsi->netdev);
1823
1824 if (is_netdev) {
1825 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid,
1826 is_vf, is_netdev);
1827 if (!add_f) {
1828 dev_info(&vsi->back->pdev->dev,
1829 "Could not add vlan filter %d for %pM\n",
1830 vid, vsi->netdev->dev_addr);
1831 return -ENOMEM;
1832 }
1833 }
1834
1835 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1836 add_f = i40e_add_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
1837 if (!add_f) {
1838 dev_info(&vsi->back->pdev->dev,
1839 "Could not add vlan filter %d for %pM\n",
1840 vid, f->macaddr);
1841 return -ENOMEM;
1842 }
1843 }
1844
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001845 /* Now if we add a vlan tag, make sure to check if it is the first
1846 * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
1847 * with 0, so we now accept untagged and specified tagged traffic
1848 * (and not any taged and untagged)
1849 */
1850 if (vid > 0) {
1851 if (is_netdev && i40e_find_filter(vsi, vsi->netdev->dev_addr,
1852 I40E_VLAN_ANY,
1853 is_vf, is_netdev)) {
1854 i40e_del_filter(vsi, vsi->netdev->dev_addr,
1855 I40E_VLAN_ANY, is_vf, is_netdev);
1856 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0,
1857 is_vf, is_netdev);
1858 if (!add_f) {
1859 dev_info(&vsi->back->pdev->dev,
1860 "Could not add filter 0 for %pM\n",
1861 vsi->netdev->dev_addr);
1862 return -ENOMEM;
1863 }
1864 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08001865 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001866
Greg Rose8d82a7c2014-01-13 16:13:04 -08001867 /* Do not assume that I40E_VLAN_ANY should be reset to VLAN 0 */
1868 if (vid > 0 && !vsi->info.pvid) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001869 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1870 if (i40e_find_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1871 is_vf, is_netdev)) {
1872 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1873 is_vf, is_netdev);
1874 add_f = i40e_add_filter(vsi, f->macaddr,
1875 0, is_vf, is_netdev);
1876 if (!add_f) {
1877 dev_info(&vsi->back->pdev->dev,
1878 "Could not add filter 0 for %pM\n",
1879 f->macaddr);
1880 return -ENOMEM;
1881 }
1882 }
1883 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001884 }
1885
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001886 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1887 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1888 return 0;
1889
1890 return i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001891}
1892
1893/**
1894 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
1895 * @vsi: the vsi being configured
1896 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001897 *
1898 * Return: 0 on success or negative otherwise
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001899 **/
1900int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
1901{
1902 struct net_device *netdev = vsi->netdev;
1903 struct i40e_mac_filter *f, *add_f;
1904 bool is_vf, is_netdev;
1905 int filter_count = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001906
1907 is_vf = (vsi->type == I40E_VSI_SRIOV);
1908 is_netdev = !!(netdev);
1909
1910 if (is_netdev)
1911 i40e_del_filter(vsi, netdev->dev_addr, vid, is_vf, is_netdev);
1912
1913 list_for_each_entry(f, &vsi->mac_filter_list, list)
1914 i40e_del_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
1915
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001916 /* go through all the filters for this VSI and if there is only
1917 * vid == 0 it means there are no other filters, so vid 0 must
1918 * be replaced with -1. This signifies that we should from now
1919 * on accept any traffic (with any tag present, or untagged)
1920 */
1921 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1922 if (is_netdev) {
1923 if (f->vlan &&
1924 ether_addr_equal(netdev->dev_addr, f->macaddr))
1925 filter_count++;
1926 }
1927
1928 if (f->vlan)
1929 filter_count++;
1930 }
1931
1932 if (!filter_count && is_netdev) {
1933 i40e_del_filter(vsi, netdev->dev_addr, 0, is_vf, is_netdev);
1934 f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
1935 is_vf, is_netdev);
1936 if (!f) {
1937 dev_info(&vsi->back->pdev->dev,
1938 "Could not add filter %d for %pM\n",
1939 I40E_VLAN_ANY, netdev->dev_addr);
1940 return -ENOMEM;
1941 }
1942 }
1943
1944 if (!filter_count) {
1945 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1946 i40e_del_filter(vsi, f->macaddr, 0, is_vf, is_netdev);
1947 add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1948 is_vf, is_netdev);
1949 if (!add_f) {
1950 dev_info(&vsi->back->pdev->dev,
1951 "Could not add filter %d for %pM\n",
1952 I40E_VLAN_ANY, f->macaddr);
1953 return -ENOMEM;
1954 }
1955 }
1956 }
1957
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001958 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1959 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1960 return 0;
1961
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001962 return i40e_sync_vsi_filters(vsi);
1963}
1964
1965/**
1966 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
1967 * @netdev: network interface to be adjusted
1968 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001969 *
1970 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001971 **/
1972static int i40e_vlan_rx_add_vid(struct net_device *netdev,
1973 __always_unused __be16 proto, u16 vid)
1974{
1975 struct i40e_netdev_priv *np = netdev_priv(netdev);
1976 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001977 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001978
1979 if (vid > 4095)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001980 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001981
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001982 netdev_info(netdev, "adding %pM vid=%d\n", netdev->dev_addr, vid);
1983
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00001984 /* If the network stack called us with vid = 0 then
1985 * it is asking to receive priority tagged packets with
1986 * vlan id 0. Our HW receives them by default when configured
1987 * to receive untagged packets so there is no need to add an
1988 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001989 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00001990 if (vid)
1991 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001992
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001993 if (!ret && (vid < VLAN_N_VID))
1994 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001995
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001996 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001997}
1998
1999/**
2000 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2001 * @netdev: network interface to be adjusted
2002 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002003 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002004 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002005 **/
2006static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2007 __always_unused __be16 proto, u16 vid)
2008{
2009 struct i40e_netdev_priv *np = netdev_priv(netdev);
2010 struct i40e_vsi *vsi = np->vsi;
2011
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002012 netdev_info(netdev, "removing %pM vid=%d\n", netdev->dev_addr, vid);
2013
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002014 /* return code is ignored as there is nothing a user
2015 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002016 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002017 */
2018 i40e_vsi_kill_vlan(vsi, vid);
2019
2020 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002021
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002022 return 0;
2023}
2024
2025/**
2026 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2027 * @vsi: the vsi being brought back up
2028 **/
2029static void i40e_restore_vlan(struct i40e_vsi *vsi)
2030{
2031 u16 vid;
2032
2033 if (!vsi->netdev)
2034 return;
2035
2036 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2037
2038 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2039 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2040 vid);
2041}
2042
2043/**
2044 * i40e_vsi_add_pvid - Add pvid for the VSI
2045 * @vsi: the vsi being adjusted
2046 * @vid: the vlan id to set as a PVID
2047 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002048int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002049{
2050 struct i40e_vsi_context ctxt;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002051 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002052
2053 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2054 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002055 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2056 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002057 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002058
2059 ctxt.seid = vsi->seid;
2060 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002061 aq_ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2062 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002063 dev_info(&vsi->back->pdev->dev,
2064 "%s: update vsi failed, aq_err=%d\n",
2065 __func__, vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002066 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002067 }
2068
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002069 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002070}
2071
2072/**
2073 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2074 * @vsi: the vsi being adjusted
2075 *
2076 * Just use the vlan_rx_register() service to put it back to normal
2077 **/
2078void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2079{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002080 i40e_vlan_stripping_disable(vsi);
2081
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002082 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002083}
2084
2085/**
2086 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2087 * @vsi: ptr to the VSI
2088 *
2089 * If this function returns with an error, then it's possible one or
2090 * more of the rings is populated (while the rest are not). It is the
2091 * callers duty to clean those orphaned rings.
2092 *
2093 * Return 0 on success, negative on failure
2094 **/
2095static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2096{
2097 int i, err = 0;
2098
2099 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002100 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002101
2102 return err;
2103}
2104
2105/**
2106 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2107 * @vsi: ptr to the VSI
2108 *
2109 * Free VSI's transmit software resources
2110 **/
2111static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2112{
2113 int i;
2114
Greg Rose8e9dca52013-12-18 13:45:53 +00002115 if (!vsi->tx_rings)
2116 return;
2117
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002118 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002119 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002120 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002121}
2122
2123/**
2124 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2125 * @vsi: ptr to the VSI
2126 *
2127 * If this function returns with an error, then it's possible one or
2128 * more of the rings is populated (while the rest are not). It is the
2129 * callers duty to clean those orphaned rings.
2130 *
2131 * Return 0 on success, negative on failure
2132 **/
2133static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2134{
2135 int i, err = 0;
2136
2137 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002138 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002139 return err;
2140}
2141
2142/**
2143 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2144 * @vsi: ptr to the VSI
2145 *
2146 * Free all receive software resources
2147 **/
2148static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2149{
2150 int i;
2151
Greg Rose8e9dca52013-12-18 13:45:53 +00002152 if (!vsi->rx_rings)
2153 return;
2154
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002155 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002156 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002157 i40e_free_rx_resources(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002158}
2159
2160/**
2161 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2162 * @ring: The Tx ring to configure
2163 *
2164 * Configure the Tx descriptor ring in the HMC context.
2165 **/
2166static int i40e_configure_tx_ring(struct i40e_ring *ring)
2167{
2168 struct i40e_vsi *vsi = ring->vsi;
2169 u16 pf_q = vsi->base_queue + ring->queue_index;
2170 struct i40e_hw *hw = &vsi->back->hw;
2171 struct i40e_hmc_obj_txq tx_ctx;
2172 i40e_status err = 0;
2173 u32 qtx_ctl = 0;
2174
2175 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002176 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002177 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2178 ring->atr_count = 0;
2179 } else {
2180 ring->atr_sample_rate = 0;
2181 }
2182
2183 /* initialize XPS */
2184 if (ring->q_vector && ring->netdev &&
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08002185 vsi->tc_config.numtc <= 1 &&
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002186 !test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
2187 netif_set_xps_queue(ring->netdev,
2188 &ring->q_vector->affinity_mask,
2189 ring->queue_index);
2190
2191 /* clear the context structure first */
2192 memset(&tx_ctx, 0, sizeof(tx_ctx));
2193
2194 tx_ctx.new_context = 1;
2195 tx_ctx.base = (ring->dma / 128);
2196 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002197 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2198 I40E_FLAG_FD_ATR_ENABLED));
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002199 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002200 /* FDIR VSI tx ring can still use RS bit and writebacks */
2201 if (vsi->type != I40E_VSI_FDIR)
2202 tx_ctx.head_wb_ena = 1;
2203 tx_ctx.head_wb_addr = ring->dma +
2204 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002205
2206 /* As part of VSI creation/update, FW allocates certain
2207 * Tx arbitration queue sets for each TC enabled for
2208 * the VSI. The FW returns the handles to these queue
2209 * sets as part of the response buffer to Add VSI,
2210 * Update VSI, etc. AQ commands. It is expected that
2211 * these queue set handles be associated with the Tx
2212 * queues by the driver as part of the TX queue context
2213 * initialization. This has to be done regardless of
2214 * DCB as by default everything is mapped to TC0.
2215 */
2216 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2217 tx_ctx.rdylist_act = 0;
2218
2219 /* clear the context in the HMC */
2220 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2221 if (err) {
2222 dev_info(&vsi->back->pdev->dev,
2223 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2224 ring->queue_index, pf_q, err);
2225 return -ENOMEM;
2226 }
2227
2228 /* set the context in the HMC */
2229 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2230 if (err) {
2231 dev_info(&vsi->back->pdev->dev,
2232 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2233 ring->queue_index, pf_q, err);
2234 return -ENOMEM;
2235 }
2236
2237 /* Now associate this queue with this PCI function */
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002238 if (vsi->type == I40E_VSI_VMDQ2)
2239 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
2240 else
2241 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Shannon Nelson13fd9772013-09-28 07:14:19 +00002242 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2243 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002244 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2245 i40e_flush(hw);
2246
2247 clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
2248
2249 /* cache tail off for easier writes later */
2250 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2251
2252 return 0;
2253}
2254
2255/**
2256 * i40e_configure_rx_ring - Configure a receive ring context
2257 * @ring: The Rx ring to configure
2258 *
2259 * Configure the Rx descriptor ring in the HMC context.
2260 **/
2261static int i40e_configure_rx_ring(struct i40e_ring *ring)
2262{
2263 struct i40e_vsi *vsi = ring->vsi;
2264 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2265 u16 pf_q = vsi->base_queue + ring->queue_index;
2266 struct i40e_hw *hw = &vsi->back->hw;
2267 struct i40e_hmc_obj_rxq rx_ctx;
2268 i40e_status err = 0;
2269
2270 ring->state = 0;
2271
2272 /* clear the context structure first */
2273 memset(&rx_ctx, 0, sizeof(rx_ctx));
2274
2275 ring->rx_buf_len = vsi->rx_buf_len;
2276 ring->rx_hdr_len = vsi->rx_hdr_len;
2277
2278 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
2279 rx_ctx.hbuff = ring->rx_hdr_len >> I40E_RXQ_CTX_HBUFF_SHIFT;
2280
2281 rx_ctx.base = (ring->dma / 128);
2282 rx_ctx.qlen = ring->count;
2283
2284 if (vsi->back->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED) {
2285 set_ring_16byte_desc_enabled(ring);
2286 rx_ctx.dsize = 0;
2287 } else {
2288 rx_ctx.dsize = 1;
2289 }
2290
2291 rx_ctx.dtype = vsi->dtype;
2292 if (vsi->dtype) {
2293 set_ring_ps_enabled(ring);
2294 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
2295 I40E_RX_SPLIT_IP |
2296 I40E_RX_SPLIT_TCP_UDP |
2297 I40E_RX_SPLIT_SCTP;
2298 } else {
2299 rx_ctx.hsplit_0 = 0;
2300 }
2301
2302 rx_ctx.rxmax = min_t(u16, vsi->max_frame,
2303 (chain_len * ring->rx_buf_len));
2304 rx_ctx.tphrdesc_ena = 1;
2305 rx_ctx.tphwdesc_ena = 1;
2306 rx_ctx.tphdata_ena = 1;
2307 rx_ctx.tphhead_ena = 1;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00002308 if (hw->revision_id == 0)
2309 rx_ctx.lrxqthresh = 0;
2310 else
2311 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002312 rx_ctx.crcstrip = 1;
2313 rx_ctx.l2tsel = 1;
2314 rx_ctx.showiv = 1;
Catherine Sullivanacb36762014-03-06 09:02:30 +00002315 /* set the prefena field to 1 because the manual says to */
2316 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002317
2318 /* clear the context in the HMC */
2319 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2320 if (err) {
2321 dev_info(&vsi->back->pdev->dev,
2322 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2323 ring->queue_index, pf_q, err);
2324 return -ENOMEM;
2325 }
2326
2327 /* set the context in the HMC */
2328 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2329 if (err) {
2330 dev_info(&vsi->back->pdev->dev,
2331 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2332 ring->queue_index, pf_q, err);
2333 return -ENOMEM;
2334 }
2335
2336 /* cache tail for quicker writes, and clear the reg before use */
2337 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2338 writel(0, ring->tail);
2339
2340 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
2341
2342 return 0;
2343}
2344
2345/**
2346 * i40e_vsi_configure_tx - Configure the VSI for Tx
2347 * @vsi: VSI structure describing this set of rings and resources
2348 *
2349 * Configure the Tx VSI for operation.
2350 **/
2351static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2352{
2353 int err = 0;
2354 u16 i;
2355
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002356 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2357 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002358
2359 return err;
2360}
2361
2362/**
2363 * i40e_vsi_configure_rx - Configure the VSI for Rx
2364 * @vsi: the VSI being configured
2365 *
2366 * Configure the Rx VSI for operation.
2367 **/
2368static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
2369{
2370 int err = 0;
2371 u16 i;
2372
2373 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
2374 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
2375 + ETH_FCS_LEN + VLAN_HLEN;
2376 else
2377 vsi->max_frame = I40E_RXBUFFER_2048;
2378
2379 /* figure out correct receive buffer length */
2380 switch (vsi->back->flags & (I40E_FLAG_RX_1BUF_ENABLED |
2381 I40E_FLAG_RX_PS_ENABLED)) {
2382 case I40E_FLAG_RX_1BUF_ENABLED:
2383 vsi->rx_hdr_len = 0;
2384 vsi->rx_buf_len = vsi->max_frame;
2385 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2386 break;
2387 case I40E_FLAG_RX_PS_ENABLED:
2388 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2389 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2390 vsi->dtype = I40E_RX_DTYPE_HEADER_SPLIT;
2391 break;
2392 default:
2393 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2394 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2395 vsi->dtype = I40E_RX_DTYPE_SPLIT_ALWAYS;
2396 break;
2397 }
2398
2399 /* round up for the chip's needs */
2400 vsi->rx_hdr_len = ALIGN(vsi->rx_hdr_len,
2401 (1 << I40E_RXQ_CTX_HBUFF_SHIFT));
2402 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
2403 (1 << I40E_RXQ_CTX_DBUFF_SHIFT));
2404
2405 /* set up individual rings */
2406 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002407 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002408
2409 return err;
2410}
2411
2412/**
2413 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
2414 * @vsi: ptr to the VSI
2415 **/
2416static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
2417{
2418 u16 qoffset, qcount;
2419 int i, n;
2420
2421 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED))
2422 return;
2423
2424 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
2425 if (!(vsi->tc_config.enabled_tc & (1 << n)))
2426 continue;
2427
2428 qoffset = vsi->tc_config.tc_info[n].qoffset;
2429 qcount = vsi->tc_config.tc_info[n].qcount;
2430 for (i = qoffset; i < (qoffset + qcount); i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002431 struct i40e_ring *rx_ring = vsi->rx_rings[i];
2432 struct i40e_ring *tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002433 rx_ring->dcb_tc = n;
2434 tx_ring->dcb_tc = n;
2435 }
2436 }
2437}
2438
2439/**
2440 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
2441 * @vsi: ptr to the VSI
2442 **/
2443static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
2444{
2445 if (vsi->netdev)
2446 i40e_set_rx_mode(vsi->netdev);
2447}
2448
2449/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00002450 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
2451 * @vsi: Pointer to the targeted VSI
2452 *
2453 * This function replays the hlist on the hw where all the SB Flow Director
2454 * filters were saved.
2455 **/
2456static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
2457{
2458 struct i40e_fdir_filter *filter;
2459 struct i40e_pf *pf = vsi->back;
2460 struct hlist_node *node;
2461
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00002462 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
2463 return;
2464
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00002465 hlist_for_each_entry_safe(filter, node,
2466 &pf->fdir_filter_list, fdir_node) {
2467 i40e_add_del_fdir(vsi, filter, true);
2468 }
2469}
2470
2471/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002472 * i40e_vsi_configure - Set up the VSI for action
2473 * @vsi: the VSI being configured
2474 **/
2475static int i40e_vsi_configure(struct i40e_vsi *vsi)
2476{
2477 int err;
2478
2479 i40e_set_vsi_rx_mode(vsi);
2480 i40e_restore_vlan(vsi);
2481 i40e_vsi_config_dcb_rings(vsi);
2482 err = i40e_vsi_configure_tx(vsi);
2483 if (!err)
2484 err = i40e_vsi_configure_rx(vsi);
2485
2486 return err;
2487}
2488
2489/**
2490 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
2491 * @vsi: the VSI being configured
2492 **/
2493static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
2494{
2495 struct i40e_pf *pf = vsi->back;
2496 struct i40e_q_vector *q_vector;
2497 struct i40e_hw *hw = &pf->hw;
2498 u16 vector;
2499 int i, q;
2500 u32 val;
2501 u32 qp;
2502
2503 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
2504 * and PFINT_LNKLSTn registers, e.g.:
2505 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
2506 */
2507 qp = vsi->base_queue;
2508 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00002509 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
2510 q_vector = vsi->q_vectors[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002511 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_ITRN(I40E_RX_ITR, vector - 1),
2514 q_vector->rx.itr);
2515 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2516 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2517 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
2518 q_vector->tx.itr);
2519
2520 /* Linked list for the queuepairs assigned to this vector */
2521 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
2522 for (q = 0; q < q_vector->num_ringpairs; q++) {
2523 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2524 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2525 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
2526 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
2527 (I40E_QUEUE_TYPE_TX
2528 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
2529
2530 wr32(hw, I40E_QINT_RQCTL(qp), val);
2531
2532 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2533 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2534 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
2535 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
2536 (I40E_QUEUE_TYPE_RX
2537 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2538
2539 /* Terminate the linked list */
2540 if (q == (q_vector->num_ringpairs - 1))
2541 val |= (I40E_QUEUE_END_OF_LIST
2542 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2543
2544 wr32(hw, I40E_QINT_TQCTL(qp), val);
2545 qp++;
2546 }
2547 }
2548
2549 i40e_flush(hw);
2550}
2551
2552/**
2553 * i40e_enable_misc_int_causes - enable the non-queue interrupts
2554 * @hw: ptr to the hardware info
2555 **/
2556static void i40e_enable_misc_int_causes(struct i40e_hw *hw)
2557{
2558 u32 val;
2559
2560 /* clear things first */
2561 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
2562 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
2563
2564 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
2565 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
2566 I40E_PFINT_ICR0_ENA_GRST_MASK |
2567 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
2568 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002569 I40E_PFINT_ICR0_ENA_TIMESYNC_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002570 I40E_PFINT_ICR0_ENA_STORM_DETECT_MASK |
2571 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
2572 I40E_PFINT_ICR0_ENA_VFLR_MASK |
2573 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2574
2575 wr32(hw, I40E_PFINT_ICR0_ENA, val);
2576
2577 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00002578 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
2579 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002580
2581 /* OTHER_ITR_IDX = 0 */
2582 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
2583}
2584
2585/**
2586 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
2587 * @vsi: the VSI being configured
2588 **/
2589static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
2590{
Alexander Duyck493fb302013-09-28 07:01:44 +00002591 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002592 struct i40e_pf *pf = vsi->back;
2593 struct i40e_hw *hw = &pf->hw;
2594 u32 val;
2595
2596 /* set the ITR configuration */
2597 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2598 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2599 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
2600 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2601 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2602 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
2603
2604 i40e_enable_misc_int_causes(hw);
2605
2606 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
2607 wr32(hw, I40E_PFINT_LNKLST0, 0);
2608
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00002609 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002610 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2611 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2612 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2613
2614 wr32(hw, I40E_QINT_RQCTL(0), val);
2615
2616 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2617 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2618 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2619
2620 wr32(hw, I40E_QINT_TQCTL(0), val);
2621 i40e_flush(hw);
2622}
2623
2624/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00002625 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
2626 * @pf: board private structure
2627 **/
2628void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
2629{
2630 struct i40e_hw *hw = &pf->hw;
2631
2632 wr32(hw, I40E_PFINT_DYN_CTL0,
2633 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2634 i40e_flush(hw);
2635}
2636
2637/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002638 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
2639 * @pf: board private structure
2640 **/
Shannon Nelson116a57d2013-09-28 07:13:59 +00002641void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002642{
2643 struct i40e_hw *hw = &pf->hw;
2644 u32 val;
2645
2646 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
2647 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
2648 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
2649
2650 wr32(hw, I40E_PFINT_DYN_CTL0, val);
2651 i40e_flush(hw);
2652}
2653
2654/**
2655 * i40e_irq_dynamic_enable - Enable default interrupt generation settings
2656 * @vsi: pointer to a vsi
2657 * @vector: enable a particular Hw Interrupt vector
2658 **/
2659void i40e_irq_dynamic_enable(struct i40e_vsi *vsi, int vector)
2660{
2661 struct i40e_pf *pf = vsi->back;
2662 struct i40e_hw *hw = &pf->hw;
2663 u32 val;
2664
2665 val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
2666 I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
2667 (I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2668 wr32(hw, I40E_PFINT_DYN_CTLN(vector - 1), val);
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00002669 /* skip the flush */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002670}
2671
2672/**
2673 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
2674 * @irq: interrupt number
2675 * @data: pointer to a q_vector
2676 **/
2677static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
2678{
2679 struct i40e_q_vector *q_vector = data;
2680
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002681 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002682 return IRQ_HANDLED;
2683
2684 napi_schedule(&q_vector->napi);
2685
2686 return IRQ_HANDLED;
2687}
2688
2689/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002690 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
2691 * @vsi: the VSI being configured
2692 * @basename: name for the vector
2693 *
2694 * Allocates MSI-X vectors and requests interrupts from the kernel.
2695 **/
2696static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
2697{
2698 int q_vectors = vsi->num_q_vectors;
2699 struct i40e_pf *pf = vsi->back;
2700 int base = vsi->base_vector;
2701 int rx_int_idx = 0;
2702 int tx_int_idx = 0;
2703 int vector, err;
2704
2705 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00002706 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002707
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002708 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002709 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2710 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
2711 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002712 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002713 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2714 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002715 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002716 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2717 "%s-%s-%d", basename, "tx", tx_int_idx++);
2718 } else {
2719 /* skip this unused q_vector */
2720 continue;
2721 }
2722 err = request_irq(pf->msix_entries[base + vector].vector,
2723 vsi->irq_handler,
2724 0,
2725 q_vector->name,
2726 q_vector);
2727 if (err) {
2728 dev_info(&pf->pdev->dev,
2729 "%s: request_irq failed, error: %d\n",
2730 __func__, err);
2731 goto free_queue_irqs;
2732 }
2733 /* assign the mask for this irq */
2734 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2735 &q_vector->affinity_mask);
2736 }
2737
2738 return 0;
2739
2740free_queue_irqs:
2741 while (vector) {
2742 vector--;
2743 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2744 NULL);
2745 free_irq(pf->msix_entries[base + vector].vector,
2746 &(vsi->q_vectors[vector]));
2747 }
2748 return err;
2749}
2750
2751/**
2752 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
2753 * @vsi: the VSI being un-configured
2754 **/
2755static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
2756{
2757 struct i40e_pf *pf = vsi->back;
2758 struct i40e_hw *hw = &pf->hw;
2759 int base = vsi->base_vector;
2760 int i;
2761
2762 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002763 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
2764 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002765 }
2766
2767 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2768 for (i = vsi->base_vector;
2769 i < (vsi->num_q_vectors + vsi->base_vector); i++)
2770 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
2771
2772 i40e_flush(hw);
2773 for (i = 0; i < vsi->num_q_vectors; i++)
2774 synchronize_irq(pf->msix_entries[i + base].vector);
2775 } else {
2776 /* Legacy and MSI mode - this stops all interrupt handling */
2777 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
2778 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
2779 i40e_flush(hw);
2780 synchronize_irq(pf->pdev->irq);
2781 }
2782}
2783
2784/**
2785 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
2786 * @vsi: the VSI being configured
2787 **/
2788static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
2789{
2790 struct i40e_pf *pf = vsi->back;
2791 int i;
2792
2793 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2794 for (i = vsi->base_vector;
2795 i < (vsi->num_q_vectors + vsi->base_vector); i++)
2796 i40e_irq_dynamic_enable(vsi, i);
2797 } else {
2798 i40e_irq_dynamic_enable_icr0(pf);
2799 }
2800
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00002801 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002802 return 0;
2803}
2804
2805/**
2806 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
2807 * @pf: board private structure
2808 **/
2809static void i40e_stop_misc_vector(struct i40e_pf *pf)
2810{
2811 /* Disable ICR 0 */
2812 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
2813 i40e_flush(&pf->hw);
2814}
2815
2816/**
2817 * i40e_intr - MSI/Legacy and non-queue interrupt handler
2818 * @irq: interrupt number
2819 * @data: pointer to a q_vector
2820 *
2821 * This is the handler used for all MSI/Legacy interrupts, and deals
2822 * with both queue and non-queue interrupts. This is also used in
2823 * MSIX mode to handle the non-queue interrupts.
2824 **/
2825static irqreturn_t i40e_intr(int irq, void *data)
2826{
2827 struct i40e_pf *pf = (struct i40e_pf *)data;
2828 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002829 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002830 u32 icr0, icr0_remaining;
2831 u32 val, ena_mask;
2832
2833 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002834 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002835
Shannon Nelson116a57d2013-09-28 07:13:59 +00002836 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
2837 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002838 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002839
Shannon Nelsoncd92e722013-11-16 10:00:44 +00002840 /* if interrupt but no bits showing, must be SWINT */
2841 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
2842 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
2843 pf->sw_int_count++;
2844
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002845 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
2846 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
2847
2848 /* temporarily disable queue cause for NAPI processing */
2849 u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
2850 qval &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
2851 wr32(hw, I40E_QINT_RQCTL(0), qval);
2852
2853 qval = rd32(hw, I40E_QINT_TQCTL(0));
2854 qval &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
2855 wr32(hw, I40E_QINT_TQCTL(0), qval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002856
2857 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck493fb302013-09-28 07:01:44 +00002858 napi_schedule(&pf->vsi[pf->lan_vsi]->q_vectors[0]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002859 }
2860
2861 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
2862 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2863 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
2864 }
2865
2866 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
2867 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
2868 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
2869 }
2870
2871 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
2872 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
2873 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
2874 }
2875
2876 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
2877 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
2878 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
2879 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
2880 val = rd32(hw, I40E_GLGEN_RSTAT);
2881 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
2882 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002883 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002884 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002885 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002886 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002887 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002888 pf->empr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002889 set_bit(__I40E_EMP_RESET_REQUESTED, &pf->state);
2890 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002891 }
2892
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002893 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
2894 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
2895 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
2896 }
2897
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002898 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
2899 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
2900
2901 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
2902 ena_mask &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
2903 i40e_ptp_tx_hwtstamp(pf);
2904 prttsyn_stat &= ~I40E_PRTTSYN_STAT_0_TXTIME_MASK;
2905 }
2906
2907 wr32(hw, I40E_PRTTSYN_STAT_0, prttsyn_stat);
2908 }
2909
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002910 /* If a critical error is pending we have no choice but to reset the
2911 * device.
2912 * Report and mask out any remaining unexpected interrupts.
2913 */
2914 icr0_remaining = icr0 & ena_mask;
2915 if (icr0_remaining) {
2916 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
2917 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002918 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002919 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00002920 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002921 dev_info(&pf->pdev->dev, "device will be reset\n");
2922 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
2923 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002924 }
2925 ena_mask &= ~icr0_remaining;
2926 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002927 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002928
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002929enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002930 /* re-enable interrupt causes */
2931 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002932 if (!test_bit(__I40E_DOWN, &pf->state)) {
2933 i40e_service_event_schedule(pf);
2934 i40e_irq_dynamic_enable_icr0(pf);
2935 }
2936
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002937 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002938}
2939
2940/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08002941 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
2942 * @tx_ring: tx ring to clean
2943 * @budget: how many cleans we're allowed
2944 *
2945 * Returns true if there's any budget left (e.g. the clean is finished)
2946 **/
2947static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
2948{
2949 struct i40e_vsi *vsi = tx_ring->vsi;
2950 u16 i = tx_ring->next_to_clean;
2951 struct i40e_tx_buffer *tx_buf;
2952 struct i40e_tx_desc *tx_desc;
2953
2954 tx_buf = &tx_ring->tx_bi[i];
2955 tx_desc = I40E_TX_DESC(tx_ring, i);
2956 i -= tx_ring->count;
2957
2958 do {
2959 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
2960
2961 /* if next_to_watch is not set then there is no work pending */
2962 if (!eop_desc)
2963 break;
2964
2965 /* prevent any other reads prior to eop_desc */
2966 read_barrier_depends();
2967
2968 /* if the descriptor isn't done, no work yet to do */
2969 if (!(eop_desc->cmd_type_offset_bsz &
2970 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
2971 break;
2972
2973 /* clear next_to_watch to prevent false hangs */
2974 tx_buf->next_to_watch = NULL;
2975
2976 /* unmap skb header data */
2977 dma_unmap_single(tx_ring->dev,
2978 dma_unmap_addr(tx_buf, dma),
2979 dma_unmap_len(tx_buf, len),
2980 DMA_TO_DEVICE);
2981
2982 dma_unmap_len_set(tx_buf, len, 0);
2983
2984
2985 /* move to the next desc and buffer to clean */
2986 tx_buf++;
2987 tx_desc++;
2988 i++;
2989 if (unlikely(!i)) {
2990 i -= tx_ring->count;
2991 tx_buf = tx_ring->tx_bi;
2992 tx_desc = I40E_TX_DESC(tx_ring, 0);
2993 }
2994
2995 /* update budget accounting */
2996 budget--;
2997 } while (likely(budget));
2998
2999 i += tx_ring->count;
3000 tx_ring->next_to_clean = i;
3001
3002 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
3003 i40e_irq_dynamic_enable(vsi,
3004 tx_ring->q_vector->v_idx + vsi->base_vector);
3005 }
3006 return budget > 0;
3007}
3008
3009/**
3010 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3011 * @irq: interrupt number
3012 * @data: pointer to a q_vector
3013 **/
3014static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3015{
3016 struct i40e_q_vector *q_vector = data;
3017 struct i40e_vsi *vsi;
3018
3019 if (!q_vector->tx.ring)
3020 return IRQ_HANDLED;
3021
3022 vsi = q_vector->tx.ring->vsi;
3023 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3024
3025 return IRQ_HANDLED;
3026}
3027
3028/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003029 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003030 * @vsi: the VSI being configured
3031 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003032 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003033 **/
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003034static void map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003035{
Alexander Duyck493fb302013-09-28 07:01:44 +00003036 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00003037 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3038 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003039
3040 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003041 tx_ring->next = q_vector->tx.ring;
3042 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003043 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003044
3045 rx_ring->q_vector = q_vector;
3046 rx_ring->next = q_vector->rx.ring;
3047 q_vector->rx.ring = rx_ring;
3048 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003049}
3050
3051/**
3052 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3053 * @vsi: the VSI being configured
3054 *
3055 * This function maps descriptor rings to the queue-specific vectors
3056 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3057 * one vector per queue pair, but on a constrained vector budget, we
3058 * group the queue pairs as "efficiently" as possible.
3059 **/
3060static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3061{
3062 int qp_remaining = vsi->num_queue_pairs;
3063 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003064 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003065 int v_start = 0;
3066 int qp_idx = 0;
3067
3068 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3069 * group them so there are multiple queues per vector.
3070 */
3071 for (; v_start < q_vectors && qp_remaining; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003072 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3073
3074 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3075
3076 q_vector->num_ringpairs = num_ringpairs;
3077
3078 q_vector->rx.count = 0;
3079 q_vector->tx.count = 0;
3080 q_vector->rx.ring = NULL;
3081 q_vector->tx.ring = NULL;
3082
3083 while (num_ringpairs--) {
3084 map_vector_to_qp(vsi, v_start, qp_idx);
3085 qp_idx++;
3086 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003087 }
3088 }
3089}
3090
3091/**
3092 * i40e_vsi_request_irq - Request IRQ from the OS
3093 * @vsi: the VSI being configured
3094 * @basename: name for the vector
3095 **/
3096static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3097{
3098 struct i40e_pf *pf = vsi->back;
3099 int err;
3100
3101 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3102 err = i40e_vsi_request_irq_msix(vsi, basename);
3103 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3104 err = request_irq(pf->pdev->irq, i40e_intr, 0,
3105 pf->misc_int_name, pf);
3106 else
3107 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
3108 pf->misc_int_name, pf);
3109
3110 if (err)
3111 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3112
3113 return err;
3114}
3115
3116#ifdef CONFIG_NET_POLL_CONTROLLER
3117/**
3118 * i40e_netpoll - A Polling 'interrupt'handler
3119 * @netdev: network interface device structure
3120 *
3121 * This is used by netconsole to send skbs without having to re-enable
3122 * interrupts. It's not called while the normal interrupt routine is executing.
3123 **/
3124static void i40e_netpoll(struct net_device *netdev)
3125{
3126 struct i40e_netdev_priv *np = netdev_priv(netdev);
3127 struct i40e_vsi *vsi = np->vsi;
3128 struct i40e_pf *pf = vsi->back;
3129 int i;
3130
3131 /* if interface is down do nothing */
3132 if (test_bit(__I40E_DOWN, &vsi->state))
3133 return;
3134
3135 pf->flags |= I40E_FLAG_IN_NETPOLL;
3136 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3137 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003138 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003139 } else {
3140 i40e_intr(pf->pdev->irq, netdev);
3141 }
3142 pf->flags &= ~I40E_FLAG_IN_NETPOLL;
3143}
3144#endif
3145
3146/**
3147 * i40e_vsi_control_tx - Start or stop a VSI's rings
3148 * @vsi: the VSI being configured
3149 * @enable: start or stop the rings
3150 **/
3151static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3152{
3153 struct i40e_pf *pf = vsi->back;
3154 struct i40e_hw *hw = &pf->hw;
3155 int i, j, pf_q;
3156 u32 tx_reg;
3157
3158 pf_q = vsi->base_queue;
3159 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003160 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003161 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003162 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3163 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3164 break;
3165 usleep_range(1000, 2000);
3166 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003167 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c122002014-03-14 07:32:29 +00003168 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00003169 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003170
3171 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003172 if (enable) {
3173 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003174 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003175 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003176 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003177 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003178
3179 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
3180
3181 /* wait for the change to finish */
3182 for (j = 0; j < 10; j++) {
3183 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Catherine Sullivan7c122002014-03-14 07:32:29 +00003184 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3185 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003186
3187 udelay(10);
3188 }
3189 if (j >= 10) {
3190 dev_info(&pf->pdev->dev, "Tx ring %d %sable timeout\n",
3191 pf_q, (enable ? "en" : "dis"));
3192 return -ETIMEDOUT;
3193 }
3194 }
3195
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003196 if (hw->revision_id == 0)
3197 mdelay(50);
3198
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003199 return 0;
3200}
3201
3202/**
3203 * i40e_vsi_control_rx - Start or stop a VSI's rings
3204 * @vsi: the VSI being configured
3205 * @enable: start or stop the rings
3206 **/
3207static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3208{
3209 struct i40e_pf *pf = vsi->back;
3210 struct i40e_hw *hw = &pf->hw;
3211 int i, j, pf_q;
3212 u32 rx_reg;
3213
3214 pf_q = vsi->base_queue;
3215 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003216 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003217 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003218 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
3219 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
3220 break;
3221 usleep_range(1000, 2000);
3222 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003223
Catherine Sullivan7c122002014-03-14 07:32:29 +00003224 /* Skip if the queue is already in the requested state */
3225 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3226 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003227
3228 /* turn on/off the queue */
3229 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08003230 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003231 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08003232 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003233 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
3234
3235 /* wait for the change to finish */
3236 for (j = 0; j < 10; j++) {
3237 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
3238
Catherine Sullivan7c122002014-03-14 07:32:29 +00003239 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3240 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003241
3242 udelay(10);
3243 }
3244 if (j >= 10) {
3245 dev_info(&pf->pdev->dev, "Rx ring %d %sable timeout\n",
3246 pf_q, (enable ? "en" : "dis"));
3247 return -ETIMEDOUT;
3248 }
3249 }
3250
3251 return 0;
3252}
3253
3254/**
3255 * i40e_vsi_control_rings - Start or stop a VSI's rings
3256 * @vsi: the VSI being configured
3257 * @enable: start or stop the rings
3258 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00003259int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003260{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003261 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003262
3263 /* do rx first for enable and last for disable */
3264 if (request) {
3265 ret = i40e_vsi_control_rx(vsi, request);
3266 if (ret)
3267 return ret;
3268 ret = i40e_vsi_control_tx(vsi, request);
3269 } else {
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003270 /* Ignore return value, we need to shutdown whatever we can */
3271 i40e_vsi_control_tx(vsi, request);
3272 i40e_vsi_control_rx(vsi, request);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003273 }
3274
3275 return ret;
3276}
3277
3278/**
3279 * i40e_vsi_free_irq - Free the irq association with the OS
3280 * @vsi: the VSI being configured
3281 **/
3282static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
3283{
3284 struct i40e_pf *pf = vsi->back;
3285 struct i40e_hw *hw = &pf->hw;
3286 int base = vsi->base_vector;
3287 u32 val, qp;
3288 int i;
3289
3290 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3291 if (!vsi->q_vectors)
3292 return;
3293
3294 for (i = 0; i < vsi->num_q_vectors; i++) {
3295 u16 vector = i + base;
3296
3297 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00003298 if (!vsi->q_vectors[i] ||
3299 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003300 continue;
3301
3302 /* clear the affinity_mask in the IRQ descriptor */
3303 irq_set_affinity_hint(pf->msix_entries[vector].vector,
3304 NULL);
3305 free_irq(pf->msix_entries[vector].vector,
Alexander Duyck493fb302013-09-28 07:01:44 +00003306 vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003307
3308 /* Tear down the interrupt queue link list
3309 *
3310 * We know that they come in pairs and always
3311 * the Rx first, then the Tx. To clear the
3312 * link list, stick the EOL value into the
3313 * next_q field of the registers.
3314 */
3315 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
3316 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3317 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3318 val |= I40E_QUEUE_END_OF_LIST
3319 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3320 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
3321
3322 while (qp != I40E_QUEUE_END_OF_LIST) {
3323 u32 next;
3324
3325 val = rd32(hw, I40E_QINT_RQCTL(qp));
3326
3327 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3328 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3329 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3330 I40E_QINT_RQCTL_INTEVENT_MASK);
3331
3332 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3333 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3334
3335 wr32(hw, I40E_QINT_RQCTL(qp), val);
3336
3337 val = rd32(hw, I40E_QINT_TQCTL(qp));
3338
3339 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
3340 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
3341
3342 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3343 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3344 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3345 I40E_QINT_TQCTL_INTEVENT_MASK);
3346
3347 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3348 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3349
3350 wr32(hw, I40E_QINT_TQCTL(qp), val);
3351 qp = next;
3352 }
3353 }
3354 } else {
3355 free_irq(pf->pdev->irq, pf);
3356
3357 val = rd32(hw, I40E_PFINT_LNKLST0);
3358 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3359 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3360 val |= I40E_QUEUE_END_OF_LIST
3361 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
3362 wr32(hw, I40E_PFINT_LNKLST0, val);
3363
3364 val = rd32(hw, I40E_QINT_RQCTL(qp));
3365 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3366 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3367 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3368 I40E_QINT_RQCTL_INTEVENT_MASK);
3369
3370 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3371 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3372
3373 wr32(hw, I40E_QINT_RQCTL(qp), val);
3374
3375 val = rd32(hw, I40E_QINT_TQCTL(qp));
3376
3377 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3378 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3379 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3380 I40E_QINT_TQCTL_INTEVENT_MASK);
3381
3382 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3383 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3384
3385 wr32(hw, I40E_QINT_TQCTL(qp), val);
3386 }
3387}
3388
3389/**
Alexander Duyck493fb302013-09-28 07:01:44 +00003390 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
3391 * @vsi: the VSI being configured
3392 * @v_idx: Index of vector to be freed
3393 *
3394 * This function frees the memory allocated to the q_vector. In addition if
3395 * NAPI is enabled it will delete any references to the NAPI struct prior
3396 * to freeing the q_vector.
3397 **/
3398static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
3399{
3400 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003401 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00003402
3403 if (!q_vector)
3404 return;
3405
3406 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003407 i40e_for_each_ring(ring, q_vector->tx)
3408 ring->q_vector = NULL;
3409
3410 i40e_for_each_ring(ring, q_vector->rx)
3411 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00003412
3413 /* only VSI w/ an associated netdev is set up w/ NAPI */
3414 if (vsi->netdev)
3415 netif_napi_del(&q_vector->napi);
3416
3417 vsi->q_vectors[v_idx] = NULL;
3418
3419 kfree_rcu(q_vector, rcu);
3420}
3421
3422/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003423 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
3424 * @vsi: the VSI being un-configured
3425 *
3426 * This frees the memory allocated to the q_vectors and
3427 * deletes references to the NAPI struct.
3428 **/
3429static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
3430{
3431 int v_idx;
3432
Alexander Duyck493fb302013-09-28 07:01:44 +00003433 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
3434 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003435}
3436
3437/**
3438 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
3439 * @pf: board private structure
3440 **/
3441static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
3442{
3443 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
3444 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3445 pci_disable_msix(pf->pdev);
3446 kfree(pf->msix_entries);
3447 pf->msix_entries = NULL;
3448 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
3449 pci_disable_msi(pf->pdev);
3450 }
3451 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
3452}
3453
3454/**
3455 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
3456 * @pf: board private structure
3457 *
3458 * We go through and clear interrupt specific resources and reset the structure
3459 * to pre-load conditions
3460 **/
3461static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
3462{
3463 int i;
3464
3465 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
3466 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
3467 if (pf->vsi[i])
3468 i40e_vsi_free_q_vectors(pf->vsi[i]);
3469 i40e_reset_interrupt_capability(pf);
3470}
3471
3472/**
3473 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
3474 * @vsi: the VSI being configured
3475 **/
3476static void i40e_napi_enable_all(struct i40e_vsi *vsi)
3477{
3478 int q_idx;
3479
3480 if (!vsi->netdev)
3481 return;
3482
3483 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003484 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003485}
3486
3487/**
3488 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
3489 * @vsi: the VSI being configured
3490 **/
3491static void i40e_napi_disable_all(struct i40e_vsi *vsi)
3492{
3493 int q_idx;
3494
3495 if (!vsi->netdev)
3496 return;
3497
3498 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003499 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003500}
3501
3502/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00003503 * i40e_vsi_close - Shut down a VSI
3504 * @vsi: the vsi to be quelled
3505 **/
3506static void i40e_vsi_close(struct i40e_vsi *vsi)
3507{
3508 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
3509 i40e_down(vsi);
3510 i40e_vsi_free_irq(vsi);
3511 i40e_vsi_free_tx_resources(vsi);
3512 i40e_vsi_free_rx_resources(vsi);
3513}
3514
3515/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003516 * i40e_quiesce_vsi - Pause a given VSI
3517 * @vsi: the VSI being paused
3518 **/
3519static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
3520{
3521 if (test_bit(__I40E_DOWN, &vsi->state))
3522 return;
3523
3524 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
3525 if (vsi->netdev && netif_running(vsi->netdev)) {
3526 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
3527 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00003528 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003529 }
3530}
3531
3532/**
3533 * i40e_unquiesce_vsi - Resume a given VSI
3534 * @vsi: the VSI being resumed
3535 **/
3536static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
3537{
3538 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
3539 return;
3540
3541 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
3542 if (vsi->netdev && netif_running(vsi->netdev))
3543 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
3544 else
Shannon Nelson8276f752014-03-14 07:32:27 +00003545 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003546}
3547
3548/**
3549 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
3550 * @pf: the PF
3551 **/
3552static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
3553{
3554 int v;
3555
3556 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
3557 if (pf->vsi[v])
3558 i40e_quiesce_vsi(pf->vsi[v]);
3559 }
3560}
3561
3562/**
3563 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
3564 * @pf: the PF
3565 **/
3566static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
3567{
3568 int v;
3569
3570 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
3571 if (pf->vsi[v])
3572 i40e_unquiesce_vsi(pf->vsi[v]);
3573 }
3574}
3575
3576/**
3577 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
3578 * @dcbcfg: the corresponding DCBx configuration structure
3579 *
3580 * Return the number of TCs from given DCBx configuration
3581 **/
3582static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
3583{
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003584 u8 num_tc = 0;
3585 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003586
3587 /* Scan the ETS Config Priority Table to find
3588 * traffic class enabled for a given priority
3589 * and use the traffic class index to get the
3590 * number of traffic classes enabled
3591 */
3592 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3593 if (dcbcfg->etscfg.prioritytable[i] > num_tc)
3594 num_tc = dcbcfg->etscfg.prioritytable[i];
3595 }
3596
3597 /* Traffic class index starts from zero so
3598 * increment to return the actual count
3599 */
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003600 return num_tc + 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003601}
3602
3603/**
3604 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
3605 * @dcbcfg: the corresponding DCBx configuration structure
3606 *
3607 * Query the current DCB configuration and return the number of
3608 * traffic classes enabled from the given DCBX config
3609 **/
3610static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
3611{
3612 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
3613 u8 enabled_tc = 1;
3614 u8 i;
3615
3616 for (i = 0; i < num_tc; i++)
3617 enabled_tc |= 1 << i;
3618
3619 return enabled_tc;
3620}
3621
3622/**
3623 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
3624 * @pf: PF being queried
3625 *
3626 * Return number of traffic classes enabled for the given PF
3627 **/
3628static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
3629{
3630 struct i40e_hw *hw = &pf->hw;
3631 u8 i, enabled_tc;
3632 u8 num_tc = 0;
3633 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3634
3635 /* If DCB is not enabled then always in single TC */
3636 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3637 return 1;
3638
3639 /* MFP mode return count of enabled TCs for this PF */
3640 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
3641 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3642 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3643 if (enabled_tc & (1 << i))
3644 num_tc++;
3645 }
3646 return num_tc;
3647 }
3648
3649 /* SFP mode will be enabled for all TCs on port */
3650 return i40e_dcb_get_num_tc(dcbcfg);
3651}
3652
3653/**
3654 * i40e_pf_get_default_tc - Get bitmap for first enabled TC
3655 * @pf: PF being queried
3656 *
3657 * Return a bitmap for first enabled traffic class for this PF.
3658 **/
3659static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
3660{
3661 u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3662 u8 i = 0;
3663
3664 if (!enabled_tc)
3665 return 0x1; /* TC0 */
3666
3667 /* Find the first enabled TC */
3668 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3669 if (enabled_tc & (1 << i))
3670 break;
3671 }
3672
3673 return 1 << i;
3674}
3675
3676/**
3677 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
3678 * @pf: PF being queried
3679 *
3680 * Return a bitmap for enabled traffic classes for this PF.
3681 **/
3682static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
3683{
3684 /* If DCB is not enabled for this PF then just return default TC */
3685 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3686 return i40e_pf_get_default_tc(pf);
3687
3688 /* MFP mode will have enabled TCs set by FW */
3689 if (pf->flags & I40E_FLAG_MFP_ENABLED)
3690 return pf->hw.func_caps.enabled_tcmap;
3691
3692 /* SFP mode we want PF to be enabled for all TCs */
3693 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
3694}
3695
3696/**
3697 * i40e_vsi_get_bw_info - Query VSI BW Information
3698 * @vsi: the VSI being queried
3699 *
3700 * Returns 0 on success, negative value on failure
3701 **/
3702static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
3703{
3704 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
3705 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
3706 struct i40e_pf *pf = vsi->back;
3707 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003708 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003709 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003710 int i;
3711
3712 /* Get the VSI level BW configuration */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003713 aq_ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
3714 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003715 dev_info(&pf->pdev->dev,
3716 "couldn't get pf vsi bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003717 aq_ret, pf->hw.aq.asq_last_status);
3718 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003719 }
3720
3721 /* Get the VSI level BW configuration per TC */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003722 aq_ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
Jesse Brandeburg6838b532014-01-14 00:49:52 -08003723 NULL);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003724 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003725 dev_info(&pf->pdev->dev,
3726 "couldn't get pf vsi ets bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003727 aq_ret, pf->hw.aq.asq_last_status);
3728 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003729 }
3730
3731 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
3732 dev_info(&pf->pdev->dev,
3733 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
3734 bw_config.tc_valid_bits,
3735 bw_ets_config.tc_valid_bits);
3736 /* Still continuing */
3737 }
3738
3739 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
3740 vsi->bw_max_quanta = bw_config.max_bw;
3741 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
3742 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
3743 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3744 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
3745 vsi->bw_ets_limit_credits[i] =
3746 le16_to_cpu(bw_ets_config.credits[i]);
3747 /* 3 bits out of 4 for each TC */
3748 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
3749 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003750
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003751 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003752}
3753
3754/**
3755 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
3756 * @vsi: the VSI being configured
3757 * @enabled_tc: TC bitmap
3758 * @bw_credits: BW shared credits per TC
3759 *
3760 * Returns 0 on success, negative value on failure
3761 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003762static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003763 u8 *bw_share)
3764{
3765 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003766 i40e_status aq_ret;
3767 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003768
3769 bw_data.tc_valid_bits = enabled_tc;
3770 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3771 bw_data.tc_bw_credits[i] = bw_share[i];
3772
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003773 aq_ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
3774 NULL);
3775 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003776 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00003777 "AQ command Config VSI BW allocation per TC failed = %d\n",
3778 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003779 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003780 }
3781
3782 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3783 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
3784
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003785 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003786}
3787
3788/**
3789 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
3790 * @vsi: the VSI being configured
3791 * @enabled_tc: TC map to be enabled
3792 *
3793 **/
3794static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3795{
3796 struct net_device *netdev = vsi->netdev;
3797 struct i40e_pf *pf = vsi->back;
3798 struct i40e_hw *hw = &pf->hw;
3799 u8 netdev_tc = 0;
3800 int i;
3801 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3802
3803 if (!netdev)
3804 return;
3805
3806 if (!enabled_tc) {
3807 netdev_reset_tc(netdev);
3808 return;
3809 }
3810
3811 /* Set up actual enabled TCs on the VSI */
3812 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
3813 return;
3814
3815 /* set per TC queues for the VSI */
3816 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3817 /* Only set TC queues for enabled tcs
3818 *
3819 * e.g. For a VSI that has TC0 and TC3 enabled the
3820 * enabled_tc bitmap would be 0x00001001; the driver
3821 * will set the numtc for netdev as 2 that will be
3822 * referenced by the netdev layer as TC 0 and 1.
3823 */
3824 if (vsi->tc_config.enabled_tc & (1 << i))
3825 netdev_set_tc_queue(netdev,
3826 vsi->tc_config.tc_info[i].netdev_tc,
3827 vsi->tc_config.tc_info[i].qcount,
3828 vsi->tc_config.tc_info[i].qoffset);
3829 }
3830
3831 /* Assign UP2TC map for the VSI */
3832 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3833 /* Get the actual TC# for the UP */
3834 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
3835 /* Get the mapped netdev TC# for the UP */
3836 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
3837 netdev_set_prio_tc_map(netdev, i, netdev_tc);
3838 }
3839}
3840
3841/**
3842 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
3843 * @vsi: the VSI being configured
3844 * @ctxt: the ctxt buffer returned from AQ VSI update param command
3845 **/
3846static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
3847 struct i40e_vsi_context *ctxt)
3848{
3849 /* copy just the sections touched not the entire info
3850 * since not all sections are valid as returned by
3851 * update vsi params
3852 */
3853 vsi->info.mapping_flags = ctxt->info.mapping_flags;
3854 memcpy(&vsi->info.queue_mapping,
3855 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
3856 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
3857 sizeof(vsi->info.tc_mapping));
3858}
3859
3860/**
3861 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
3862 * @vsi: VSI to be configured
3863 * @enabled_tc: TC bitmap
3864 *
3865 * This configures a particular VSI for TCs that are mapped to the
3866 * given TC bitmap. It uses default bandwidth share for TCs across
3867 * VSIs to configure TC for a particular VSI.
3868 *
3869 * NOTE:
3870 * It is expected that the VSI queues have been quisced before calling
3871 * this function.
3872 **/
3873static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3874{
3875 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
3876 struct i40e_vsi_context ctxt;
3877 int ret = 0;
3878 int i;
3879
3880 /* Check if enabled_tc is same as existing or new TCs */
3881 if (vsi->tc_config.enabled_tc == enabled_tc)
3882 return ret;
3883
3884 /* Enable ETS TCs with equal BW Share for now across all VSIs */
3885 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3886 if (enabled_tc & (1 << i))
3887 bw_share[i] = 1;
3888 }
3889
3890 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
3891 if (ret) {
3892 dev_info(&vsi->back->pdev->dev,
3893 "Failed configuring TC map %d for VSI %d\n",
3894 enabled_tc, vsi->seid);
3895 goto out;
3896 }
3897
3898 /* Update Queue Pairs Mapping for currently enabled UPs */
3899 ctxt.seid = vsi->seid;
3900 ctxt.pf_num = vsi->back->hw.pf_id;
3901 ctxt.vf_num = 0;
3902 ctxt.uplink_seid = vsi->uplink_seid;
3903 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
3904 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
3905
3906 /* Update the VSI after updating the VSI queue-mapping information */
3907 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3908 if (ret) {
3909 dev_info(&vsi->back->pdev->dev,
3910 "update vsi failed, aq_err=%d\n",
3911 vsi->back->hw.aq.asq_last_status);
3912 goto out;
3913 }
3914 /* update the local VSI info with updated queue map */
3915 i40e_vsi_update_queue_map(vsi, &ctxt);
3916 vsi->info.valid_sections = 0;
3917
3918 /* Update current VSI BW information */
3919 ret = i40e_vsi_get_bw_info(vsi);
3920 if (ret) {
3921 dev_info(&vsi->back->pdev->dev,
3922 "Failed updating vsi bw info, aq_err=%d\n",
3923 vsi->back->hw.aq.asq_last_status);
3924 goto out;
3925 }
3926
3927 /* Update the netdev TC setup */
3928 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
3929out:
3930 return ret;
3931}
3932
3933/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08003934 * i40e_veb_config_tc - Configure TCs for given VEB
3935 * @veb: given VEB
3936 * @enabled_tc: TC bitmap
3937 *
3938 * Configures given TC bitmap for VEB (switching) element
3939 **/
3940int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
3941{
3942 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
3943 struct i40e_pf *pf = veb->pf;
3944 int ret = 0;
3945 int i;
3946
3947 /* No TCs or already enabled TCs just return */
3948 if (!enabled_tc || veb->enabled_tc == enabled_tc)
3949 return ret;
3950
3951 bw_data.tc_valid_bits = enabled_tc;
3952 /* bw_data.absolute_credits is not set (relative) */
3953
3954 /* Enable ETS TCs with equal BW Share for now */
3955 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3956 if (enabled_tc & (1 << i))
3957 bw_data.tc_bw_share_credits[i] = 1;
3958 }
3959
3960 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
3961 &bw_data, NULL);
3962 if (ret) {
3963 dev_info(&pf->pdev->dev,
3964 "veb bw config failed, aq_err=%d\n",
3965 pf->hw.aq.asq_last_status);
3966 goto out;
3967 }
3968
3969 /* Update the BW information */
3970 ret = i40e_veb_get_bw_info(veb);
3971 if (ret) {
3972 dev_info(&pf->pdev->dev,
3973 "Failed getting veb bw config, aq_err=%d\n",
3974 pf->hw.aq.asq_last_status);
3975 }
3976
3977out:
3978 return ret;
3979}
3980
3981#ifdef CONFIG_I40E_DCB
3982/**
3983 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
3984 * @pf: PF struct
3985 *
3986 * Reconfigure VEB/VSIs on a given PF; it is assumed that
3987 * the caller would've quiesce all the VSIs before calling
3988 * this function
3989 **/
3990static void i40e_dcb_reconfigure(struct i40e_pf *pf)
3991{
3992 u8 tc_map = 0;
3993 int ret;
3994 u8 v;
3995
3996 /* Enable the TCs available on PF to all VEBs */
3997 tc_map = i40e_pf_get_tc_map(pf);
3998 for (v = 0; v < I40E_MAX_VEB; v++) {
3999 if (!pf->veb[v])
4000 continue;
4001 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
4002 if (ret) {
4003 dev_info(&pf->pdev->dev,
4004 "Failed configuring TC for VEB seid=%d\n",
4005 pf->veb[v]->seid);
4006 /* Will try to configure as many components */
4007 }
4008 }
4009
4010 /* Update each VSI */
4011 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4012 if (!pf->vsi[v])
4013 continue;
4014
4015 /* - Enable all TCs for the LAN VSI
4016 * - For all others keep them at TC0 for now
4017 */
4018 if (v == pf->lan_vsi)
4019 tc_map = i40e_pf_get_tc_map(pf);
4020 else
4021 tc_map = i40e_pf_get_default_tc(pf);
4022
4023 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
4024 if (ret) {
4025 dev_info(&pf->pdev->dev,
4026 "Failed configuring TC for VSI seid=%d\n",
4027 pf->vsi[v]->seid);
4028 /* Will try to configure as many components */
4029 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00004030 /* Re-configure VSI vectors based on updated TC map */
4031 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004032 if (pf->vsi[v]->netdev)
4033 i40e_dcbnl_set_all(pf->vsi[v]);
4034 }
4035 }
4036}
4037
4038/**
4039 * i40e_init_pf_dcb - Initialize DCB configuration
4040 * @pf: PF being configured
4041 *
4042 * Query the current DCB configuration and cache it
4043 * in the hardware structure
4044 **/
4045static int i40e_init_pf_dcb(struct i40e_pf *pf)
4046{
4047 struct i40e_hw *hw = &pf->hw;
4048 int err = 0;
4049
4050 if (pf->hw.func_caps.npar_enable)
4051 goto out;
4052
4053 /* Get the initial DCB configuration */
4054 err = i40e_init_dcb(hw);
4055 if (!err) {
4056 /* Device/Function is not DCBX capable */
4057 if ((!hw->func_caps.dcb) ||
4058 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
4059 dev_info(&pf->pdev->dev,
4060 "DCBX offload is not supported or is disabled for this PF.\n");
4061
4062 if (pf->flags & I40E_FLAG_MFP_ENABLED)
4063 goto out;
4064
4065 } else {
4066 /* When status is not DISABLED then DCBX in FW */
4067 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
4068 DCB_CAP_DCBX_VER_IEEE;
4069 pf->flags |= I40E_FLAG_DCB_ENABLED;
4070 }
Neerav Parikh014269f2014-04-01 07:11:48 +00004071 } else {
4072 dev_info(&pf->pdev->dev, "AQ Querying DCB configuration failed: %d\n",
4073 pf->hw.aq.asq_last_status);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004074 }
4075
4076out:
4077 return err;
4078}
4079#endif /* CONFIG_I40E_DCB */
4080
4081/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004082 * i40e_up_complete - Finish the last steps of bringing up a connection
4083 * @vsi: the VSI being configured
4084 **/
4085static int i40e_up_complete(struct i40e_vsi *vsi)
4086{
4087 struct i40e_pf *pf = vsi->back;
4088 int err;
4089
4090 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4091 i40e_vsi_configure_msix(vsi);
4092 else
4093 i40e_configure_msi_and_legacy(vsi);
4094
4095 /* start rings */
4096 err = i40e_vsi_control_rings(vsi, true);
4097 if (err)
4098 return err;
4099
4100 clear_bit(__I40E_DOWN, &vsi->state);
4101 i40e_napi_enable_all(vsi);
4102 i40e_vsi_enable_irq(vsi);
4103
4104 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
4105 (vsi->netdev)) {
Anjali Singhai6d779b42013-09-28 06:00:02 +00004106 netdev_info(vsi->netdev, "NIC Link is Up\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004107 netif_tx_start_all_queues(vsi->netdev);
4108 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00004109 } else if (vsi->netdev) {
4110 netdev_info(vsi->netdev, "NIC Link is Down\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004111 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00004112
4113 /* replay FDIR SB filters */
4114 if (vsi->type == I40E_VSI_FDIR)
4115 i40e_fdir_filter_restore(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004116 i40e_service_event_schedule(pf);
4117
4118 return 0;
4119}
4120
4121/**
4122 * i40e_vsi_reinit_locked - Reset the VSI
4123 * @vsi: the VSI being configured
4124 *
4125 * Rebuild the ring structs after some configuration
4126 * has changed, e.g. MTU size.
4127 **/
4128static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
4129{
4130 struct i40e_pf *pf = vsi->back;
4131
4132 WARN_ON(in_interrupt());
4133 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
4134 usleep_range(1000, 2000);
4135 i40e_down(vsi);
4136
4137 /* Give a VF some time to respond to the reset. The
4138 * two second wait is based upon the watchdog cycle in
4139 * the VF driver.
4140 */
4141 if (vsi->type == I40E_VSI_SRIOV)
4142 msleep(2000);
4143 i40e_up(vsi);
4144 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
4145}
4146
4147/**
4148 * i40e_up - Bring the connection back up after being down
4149 * @vsi: the VSI being configured
4150 **/
4151int i40e_up(struct i40e_vsi *vsi)
4152{
4153 int err;
4154
4155 err = i40e_vsi_configure(vsi);
4156 if (!err)
4157 err = i40e_up_complete(vsi);
4158
4159 return err;
4160}
4161
4162/**
4163 * i40e_down - Shutdown the connection processing
4164 * @vsi: the VSI being stopped
4165 **/
4166void i40e_down(struct i40e_vsi *vsi)
4167{
4168 int i;
4169
4170 /* It is assumed that the caller of this function
4171 * sets the vsi->state __I40E_DOWN bit.
4172 */
4173 if (vsi->netdev) {
4174 netif_carrier_off(vsi->netdev);
4175 netif_tx_disable(vsi->netdev);
4176 }
4177 i40e_vsi_disable_irq(vsi);
4178 i40e_vsi_control_rings(vsi, false);
4179 i40e_napi_disable_all(vsi);
4180
4181 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004182 i40e_clean_tx_ring(vsi->tx_rings[i]);
4183 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004184 }
4185}
4186
4187/**
4188 * i40e_setup_tc - configure multiple traffic classes
4189 * @netdev: net device to configure
4190 * @tc: number of traffic classes to enable
4191 **/
4192static int i40e_setup_tc(struct net_device *netdev, u8 tc)
4193{
4194 struct i40e_netdev_priv *np = netdev_priv(netdev);
4195 struct i40e_vsi *vsi = np->vsi;
4196 struct i40e_pf *pf = vsi->back;
4197 u8 enabled_tc = 0;
4198 int ret = -EINVAL;
4199 int i;
4200
4201 /* Check if DCB enabled to continue */
4202 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
4203 netdev_info(netdev, "DCB is not enabled for adapter\n");
4204 goto exit;
4205 }
4206
4207 /* Check if MFP enabled */
4208 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
4209 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
4210 goto exit;
4211 }
4212
4213 /* Check whether tc count is within enabled limit */
4214 if (tc > i40e_pf_get_num_tc(pf)) {
4215 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
4216 goto exit;
4217 }
4218
4219 /* Generate TC map for number of tc requested */
4220 for (i = 0; i < tc; i++)
4221 enabled_tc |= (1 << i);
4222
4223 /* Requesting same TC configuration as already enabled */
4224 if (enabled_tc == vsi->tc_config.enabled_tc)
4225 return 0;
4226
4227 /* Quiesce VSI queues */
4228 i40e_quiesce_vsi(vsi);
4229
4230 /* Configure VSI for enabled TCs */
4231 ret = i40e_vsi_config_tc(vsi, enabled_tc);
4232 if (ret) {
4233 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
4234 vsi->seid);
4235 goto exit;
4236 }
4237
4238 /* Unquiesce VSI */
4239 i40e_unquiesce_vsi(vsi);
4240
4241exit:
4242 return ret;
4243}
4244
4245/**
4246 * i40e_open - Called when a network interface is made active
4247 * @netdev: network interface device structure
4248 *
4249 * The open entry point is called when a network interface is made
4250 * active by the system (IFF_UP). At this point all resources needed
4251 * for transmit and receive operations are allocated, the interrupt
4252 * handler is registered with the OS, the netdev watchdog subtask is
4253 * enabled, and the stack is notified that the interface is ready.
4254 *
4255 * Returns 0 on success, negative value on failure
4256 **/
4257static int i40e_open(struct net_device *netdev)
4258{
4259 struct i40e_netdev_priv *np = netdev_priv(netdev);
4260 struct i40e_vsi *vsi = np->vsi;
4261 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004262 int err;
4263
Shannon Nelson4eb3f762014-03-06 08:59:58 +00004264 /* disallow open during test or if eeprom is broken */
4265 if (test_bit(__I40E_TESTING, &pf->state) ||
4266 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004267 return -EBUSY;
4268
4269 netif_carrier_off(netdev);
4270
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004271 err = i40e_vsi_open(vsi);
4272 if (err)
4273 return err;
4274
Jesse Brandeburg059dab62014-04-01 09:07:20 +00004275 /* configure global TSO hardware offload settings */
4276 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
4277 TCP_FLAG_FIN) >> 16);
4278 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
4279 TCP_FLAG_FIN |
4280 TCP_FLAG_CWR) >> 16);
4281 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
4282
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004283#ifdef CONFIG_I40E_VXLAN
4284 vxlan_get_rx_port(netdev);
4285#endif
4286
4287 return 0;
4288}
4289
4290/**
4291 * i40e_vsi_open -
4292 * @vsi: the VSI to open
4293 *
4294 * Finish initialization of the VSI.
4295 *
4296 * Returns 0 on success, negative value on failure
4297 **/
4298int i40e_vsi_open(struct i40e_vsi *vsi)
4299{
4300 struct i40e_pf *pf = vsi->back;
4301 char int_name[IFNAMSIZ];
4302 int err;
4303
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004304 /* allocate descriptors */
4305 err = i40e_vsi_setup_tx_resources(vsi);
4306 if (err)
4307 goto err_setup_tx;
4308 err = i40e_vsi_setup_rx_resources(vsi);
4309 if (err)
4310 goto err_setup_rx;
4311
4312 err = i40e_vsi_configure(vsi);
4313 if (err)
4314 goto err_setup_rx;
4315
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00004316 if (vsi->netdev) {
4317 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
4318 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
4319 err = i40e_vsi_request_irq(vsi, int_name);
4320 if (err)
4321 goto err_setup_rx;
4322
4323 /* Notify the stack of the actual queue counts. */
4324 err = netif_set_real_num_tx_queues(vsi->netdev,
4325 vsi->num_queue_pairs);
4326 if (err)
4327 goto err_set_queues;
4328
4329 err = netif_set_real_num_rx_queues(vsi->netdev,
4330 vsi->num_queue_pairs);
4331 if (err)
4332 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00004333
4334 } else if (vsi->type == I40E_VSI_FDIR) {
4335 snprintf(int_name, sizeof(int_name) - 1, "%s-fdir",
4336 dev_driver_string(&pf->pdev->dev));
4337 err = i40e_vsi_request_irq(vsi, int_name);
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00004338 } else {
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004339 err = EINVAL;
4340 goto err_setup_rx;
4341 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004342
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004343 err = i40e_up_complete(vsi);
4344 if (err)
4345 goto err_up_complete;
4346
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004347 return 0;
4348
4349err_up_complete:
4350 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004351err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004352 i40e_vsi_free_irq(vsi);
4353err_setup_rx:
4354 i40e_vsi_free_rx_resources(vsi);
4355err_setup_tx:
4356 i40e_vsi_free_tx_resources(vsi);
4357 if (vsi == pf->vsi[pf->lan_vsi])
4358 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
4359
4360 return err;
4361}
4362
4363/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00004364 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
4365 * @pf: Pointer to pf
4366 *
4367 * This function destroys the hlist where all the Flow Director
4368 * filters were saved.
4369 **/
4370static void i40e_fdir_filter_exit(struct i40e_pf *pf)
4371{
4372 struct i40e_fdir_filter *filter;
4373 struct hlist_node *node2;
4374
4375 hlist_for_each_entry_safe(filter, node2,
4376 &pf->fdir_filter_list, fdir_node) {
4377 hlist_del(&filter->fdir_node);
4378 kfree(filter);
4379 }
4380 pf->fdir_pf_active_filters = 0;
4381}
4382
4383/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004384 * i40e_close - Disables a network interface
4385 * @netdev: network interface device structure
4386 *
4387 * The close entry point is called when an interface is de-activated
4388 * by the OS. The hardware is still under the driver's control, but
4389 * this netdev interface is disabled.
4390 *
4391 * Returns 0, this is not allowed to fail
4392 **/
4393static int i40e_close(struct net_device *netdev)
4394{
4395 struct i40e_netdev_priv *np = netdev_priv(netdev);
4396 struct i40e_vsi *vsi = np->vsi;
4397
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004398 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004399
4400 return 0;
4401}
4402
4403/**
4404 * i40e_do_reset - Start a PF or Core Reset sequence
4405 * @pf: board private structure
4406 * @reset_flags: which reset is requested
4407 *
4408 * The essential difference in resets is that the PF Reset
4409 * doesn't clear the packet buffers, doesn't reset the PE
4410 * firmware, and doesn't bother the other PFs on the chip.
4411 **/
4412void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
4413{
4414 u32 val;
4415
4416 WARN_ON(in_interrupt());
4417
4418 /* do the biggest reset indicated */
4419 if (reset_flags & (1 << __I40E_GLOBAL_RESET_REQUESTED)) {
4420
4421 /* Request a Global Reset
4422 *
4423 * This will start the chip's countdown to the actual full
4424 * chip reset event, and a warning interrupt to be sent
4425 * to all PFs, including the requestor. Our handler
4426 * for the warning interrupt will deal with the shutdown
4427 * and recovery of the switch setup.
4428 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004429 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004430 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4431 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
4432 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4433
4434 } else if (reset_flags & (1 << __I40E_CORE_RESET_REQUESTED)) {
4435
4436 /* Request a Core Reset
4437 *
4438 * Same as Global Reset, except does *not* include the MAC/PHY
4439 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004440 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004441 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4442 val |= I40E_GLGEN_RTRIG_CORER_MASK;
4443 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4444 i40e_flush(&pf->hw);
4445
Shannon Nelson7823fe32013-11-16 10:00:45 +00004446 } else if (reset_flags & (1 << __I40E_EMP_RESET_REQUESTED)) {
4447
4448 /* Request a Firmware Reset
4449 *
4450 * Same as Global reset, plus restarting the
4451 * embedded firmware engine.
4452 */
4453 /* enable EMP Reset */
4454 val = rd32(&pf->hw, I40E_GLGEN_RSTENA_EMP);
4455 val |= I40E_GLGEN_RSTENA_EMP_EMP_RST_ENA_MASK;
4456 wr32(&pf->hw, I40E_GLGEN_RSTENA_EMP, val);
4457
4458 /* force the reset */
4459 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4460 val |= I40E_GLGEN_RTRIG_EMPFWR_MASK;
4461 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4462 i40e_flush(&pf->hw);
4463
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004464 } else if (reset_flags & (1 << __I40E_PF_RESET_REQUESTED)) {
4465
4466 /* Request a PF Reset
4467 *
4468 * Resets only the PF-specific registers
4469 *
4470 * This goes directly to the tear-down and rebuild of
4471 * the switch, since we need to do all the recovery as
4472 * for the Core Reset.
4473 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004474 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004475 i40e_handle_reset_warning(pf);
4476
4477 } else if (reset_flags & (1 << __I40E_REINIT_REQUESTED)) {
4478 int v;
4479
4480 /* Find the VSI(s) that requested a re-init */
4481 dev_info(&pf->pdev->dev,
4482 "VSI reinit requested\n");
4483 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4484 struct i40e_vsi *vsi = pf->vsi[v];
4485 if (vsi != NULL &&
4486 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
4487 i40e_vsi_reinit_locked(pf->vsi[v]);
4488 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
4489 }
4490 }
4491
4492 /* no further action needed, so return now */
4493 return;
4494 } else {
4495 dev_info(&pf->pdev->dev,
4496 "bad reset request 0x%08x\n", reset_flags);
4497 return;
4498 }
4499}
4500
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004501#ifdef CONFIG_I40E_DCB
4502/**
4503 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
4504 * @pf: board private structure
4505 * @old_cfg: current DCB config
4506 * @new_cfg: new DCB config
4507 **/
4508bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
4509 struct i40e_dcbx_config *old_cfg,
4510 struct i40e_dcbx_config *new_cfg)
4511{
4512 bool need_reconfig = false;
4513
4514 /* Check if ETS configuration has changed */
4515 if (memcmp(&new_cfg->etscfg,
4516 &old_cfg->etscfg,
4517 sizeof(new_cfg->etscfg))) {
4518 /* If Priority Table has changed reconfig is needed */
4519 if (memcmp(&new_cfg->etscfg.prioritytable,
4520 &old_cfg->etscfg.prioritytable,
4521 sizeof(new_cfg->etscfg.prioritytable))) {
4522 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004523 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004524 }
4525
4526 if (memcmp(&new_cfg->etscfg.tcbwtable,
4527 &old_cfg->etscfg.tcbwtable,
4528 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004529 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004530
4531 if (memcmp(&new_cfg->etscfg.tsatable,
4532 &old_cfg->etscfg.tsatable,
4533 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004534 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004535 }
4536
4537 /* Check if PFC configuration has changed */
4538 if (memcmp(&new_cfg->pfc,
4539 &old_cfg->pfc,
4540 sizeof(new_cfg->pfc))) {
4541 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004542 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004543 }
4544
4545 /* Check if APP Table has changed */
4546 if (memcmp(&new_cfg->app,
4547 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05004548 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004549 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004550 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05004551 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004552
4553 return need_reconfig;
4554}
4555
4556/**
4557 * i40e_handle_lldp_event - Handle LLDP Change MIB event
4558 * @pf: board private structure
4559 * @e: event info posted on ARQ
4560 **/
4561static int i40e_handle_lldp_event(struct i40e_pf *pf,
4562 struct i40e_arq_event_info *e)
4563{
4564 struct i40e_aqc_lldp_get_mib *mib =
4565 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
4566 struct i40e_hw *hw = &pf->hw;
4567 struct i40e_dcbx_config *dcbx_cfg = &hw->local_dcbx_config;
4568 struct i40e_dcbx_config tmp_dcbx_cfg;
4569 bool need_reconfig = false;
4570 int ret = 0;
4571 u8 type;
4572
4573 /* Ignore if event is not for Nearest Bridge */
4574 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
4575 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
4576 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
4577 return ret;
4578
4579 /* Check MIB Type and return if event for Remote MIB update */
4580 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
4581 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
4582 /* Update the remote cached instance and return */
4583 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
4584 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
4585 &hw->remote_dcbx_config);
4586 goto exit;
4587 }
4588
4589 /* Convert/store the DCBX data from LLDPDU temporarily */
4590 memset(&tmp_dcbx_cfg, 0, sizeof(tmp_dcbx_cfg));
4591 ret = i40e_lldp_to_dcb_config(e->msg_buf, &tmp_dcbx_cfg);
4592 if (ret) {
4593 /* Error in LLDPDU parsing return */
4594 dev_info(&pf->pdev->dev, "Failed parsing LLDPDU from event buffer\n");
4595 goto exit;
4596 }
4597
4598 /* No change detected in DCBX configs */
4599 if (!memcmp(&tmp_dcbx_cfg, dcbx_cfg, sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004600 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004601 goto exit;
4602 }
4603
4604 need_reconfig = i40e_dcb_need_reconfig(pf, dcbx_cfg, &tmp_dcbx_cfg);
4605
4606 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg);
4607
4608 /* Overwrite the new configuration */
4609 *dcbx_cfg = tmp_dcbx_cfg;
4610
4611 if (!need_reconfig)
4612 goto exit;
4613
4614 /* Reconfiguration needed quiesce all VSIs */
4615 i40e_pf_quiesce_all_vsi(pf);
4616
4617 /* Changes in configuration update VEB/VSI */
4618 i40e_dcb_reconfigure(pf);
4619
4620 i40e_pf_unquiesce_all_vsi(pf);
4621exit:
4622 return ret;
4623}
4624#endif /* CONFIG_I40E_DCB */
4625
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004626/**
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004627 * i40e_do_reset_safe - Protected reset path for userland calls.
4628 * @pf: board private structure
4629 * @reset_flags: which reset is requested
4630 *
4631 **/
4632void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
4633{
4634 rtnl_lock();
4635 i40e_do_reset(pf, reset_flags);
4636 rtnl_unlock();
4637}
4638
4639/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004640 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
4641 * @pf: board private structure
4642 * @e: event info posted on ARQ
4643 *
4644 * Handler for LAN Queue Overflow Event generated by the firmware for PF
4645 * and VF queues
4646 **/
4647static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
4648 struct i40e_arq_event_info *e)
4649{
4650 struct i40e_aqc_lan_overflow *data =
4651 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
4652 u32 queue = le32_to_cpu(data->prtdcb_rupto);
4653 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
4654 struct i40e_hw *hw = &pf->hw;
4655 struct i40e_vf *vf;
4656 u16 vf_id;
4657
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004658 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
4659 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004660
4661 /* Queue belongs to VF, find the VF and issue VF reset */
4662 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
4663 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
4664 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
4665 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
4666 vf_id -= hw->func_caps.vf_base_id;
4667 vf = &pf->vf[vf_id];
4668 i40e_vc_notify_vf_reset(vf);
4669 /* Allow VF to process pending reset notification */
4670 msleep(20);
4671 i40e_reset_vf(vf, false);
4672 }
4673}
4674
4675/**
4676 * i40e_service_event_complete - Finish up the service event
4677 * @pf: board private structure
4678 **/
4679static void i40e_service_event_complete(struct i40e_pf *pf)
4680{
4681 BUG_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
4682
4683 /* flush memory to make sure state is correct before next watchog */
4684 smp_mb__before_clear_bit();
4685 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
4686}
4687
4688/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004689 * i40e_get_current_fd_count - Get the count of FD filters programmed in the HW
4690 * @pf: board private structure
4691 **/
4692int i40e_get_current_fd_count(struct i40e_pf *pf)
4693{
4694 int val, fcnt_prog;
4695 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
4696 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
4697 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
4698 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
4699 return fcnt_prog;
4700}
4701
4702/**
4703 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
4704 * @pf: board private structure
4705 **/
4706void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
4707{
4708 u32 fcnt_prog, fcnt_avail;
4709
4710 /* Check if, FD SB or ATR was auto disabled and if there is enough room
4711 * to re-enable
4712 */
4713 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4714 (pf->flags & I40E_FLAG_FD_SB_ENABLED))
4715 return;
4716 fcnt_prog = i40e_get_current_fd_count(pf);
4717 fcnt_avail = pf->hw.fdir_shared_filter_count +
4718 pf->fdir_pf_filter_count;
4719 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) {
4720 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
4721 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
4722 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
4723 dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
4724 }
4725 }
4726 /* Wait for some more space to be available to turn on ATR */
4727 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
4728 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4729 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
4730 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
4731 dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table now\n");
4732 }
4733 }
4734}
4735
4736/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004737 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
4738 * @pf: board private structure
4739 **/
4740static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
4741{
4742 if (!(pf->flags & I40E_FLAG_FDIR_REQUIRES_REINIT))
4743 return;
4744
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004745 /* if interface is down do nothing */
4746 if (test_bit(__I40E_DOWN, &pf->state))
4747 return;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004748 i40e_fdir_check_and_reenable(pf);
4749
4750 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4751 (pf->flags & I40E_FLAG_FD_SB_ENABLED))
4752 pf->flags &= ~I40E_FLAG_FDIR_REQUIRES_REINIT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004753}
4754
4755/**
4756 * i40e_vsi_link_event - notify VSI of a link event
4757 * @vsi: vsi to be notified
4758 * @link_up: link up or down
4759 **/
4760static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
4761{
4762 if (!vsi)
4763 return;
4764
4765 switch (vsi->type) {
4766 case I40E_VSI_MAIN:
4767 if (!vsi->netdev || !vsi->netdev_registered)
4768 break;
4769
4770 if (link_up) {
4771 netif_carrier_on(vsi->netdev);
4772 netif_tx_wake_all_queues(vsi->netdev);
4773 } else {
4774 netif_carrier_off(vsi->netdev);
4775 netif_tx_stop_all_queues(vsi->netdev);
4776 }
4777 break;
4778
4779 case I40E_VSI_SRIOV:
4780 break;
4781
4782 case I40E_VSI_VMDQ2:
4783 case I40E_VSI_CTRL:
4784 case I40E_VSI_MIRROR:
4785 default:
4786 /* there is no notification for other VSIs */
4787 break;
4788 }
4789}
4790
4791/**
4792 * i40e_veb_link_event - notify elements on the veb of a link event
4793 * @veb: veb to be notified
4794 * @link_up: link up or down
4795 **/
4796static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
4797{
4798 struct i40e_pf *pf;
4799 int i;
4800
4801 if (!veb || !veb->pf)
4802 return;
4803 pf = veb->pf;
4804
4805 /* depth first... */
4806 for (i = 0; i < I40E_MAX_VEB; i++)
4807 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
4808 i40e_veb_link_event(pf->veb[i], link_up);
4809
4810 /* ... now the local VSIs */
4811 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
4812 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
4813 i40e_vsi_link_event(pf->vsi[i], link_up);
4814}
4815
4816/**
4817 * i40e_link_event - Update netif_carrier status
4818 * @pf: board private structure
4819 **/
4820static void i40e_link_event(struct i40e_pf *pf)
4821{
4822 bool new_link, old_link;
4823
4824 new_link = (pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP);
4825 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
4826
4827 if (new_link == old_link)
4828 return;
4829
Anjali Singhai6d779b42013-09-28 06:00:02 +00004830 if (!test_bit(__I40E_DOWN, &pf->vsi[pf->lan_vsi]->state))
4831 netdev_info(pf->vsi[pf->lan_vsi]->netdev,
4832 "NIC Link is %s\n", (new_link ? "Up" : "Down"));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004833
4834 /* Notify the base of the switch tree connected to
4835 * the link. Floating VEBs are not notified.
4836 */
4837 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
4838 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
4839 else
4840 i40e_vsi_link_event(pf->vsi[pf->lan_vsi], new_link);
4841
4842 if (pf->vf)
4843 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00004844
4845 if (pf->flags & I40E_FLAG_PTP)
4846 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004847}
4848
4849/**
4850 * i40e_check_hang_subtask - Check for hung queues and dropped interrupts
4851 * @pf: board private structure
4852 *
4853 * Set the per-queue flags to request a check for stuck queues in the irq
4854 * clean functions, then force interrupts to be sure the irq clean is called.
4855 **/
4856static void i40e_check_hang_subtask(struct i40e_pf *pf)
4857{
4858 int i, v;
4859
4860 /* If we're down or resetting, just bail */
4861 if (test_bit(__I40E_CONFIG_BUSY, &pf->state))
4862 return;
4863
4864 /* for each VSI/netdev
4865 * for each Tx queue
4866 * set the check flag
4867 * for each q_vector
4868 * force an interrupt
4869 */
4870 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4871 struct i40e_vsi *vsi = pf->vsi[v];
4872 int armed = 0;
4873
4874 if (!pf->vsi[v] ||
4875 test_bit(__I40E_DOWN, &vsi->state) ||
4876 (vsi->netdev && !netif_carrier_ok(vsi->netdev)))
4877 continue;
4878
4879 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004880 set_check_for_tx_hang(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004881 if (test_bit(__I40E_HANG_CHECK_ARMED,
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004882 &vsi->tx_rings[i]->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004883 armed++;
4884 }
4885
4886 if (armed) {
4887 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
4888 wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0,
4889 (I40E_PFINT_DYN_CTL0_INTENA_MASK |
4890 I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK));
4891 } else {
4892 u16 vec = vsi->base_vector - 1;
4893 u32 val = (I40E_PFINT_DYN_CTLN_INTENA_MASK |
4894 I40E_PFINT_DYN_CTLN_SWINT_TRIG_MASK);
4895 for (i = 0; i < vsi->num_q_vectors; i++, vec++)
4896 wr32(&vsi->back->hw,
4897 I40E_PFINT_DYN_CTLN(vec), val);
4898 }
4899 i40e_flush(&vsi->back->hw);
4900 }
4901 }
4902}
4903
4904/**
4905 * i40e_watchdog_subtask - Check and bring link up
4906 * @pf: board private structure
4907 **/
4908static void i40e_watchdog_subtask(struct i40e_pf *pf)
4909{
4910 int i;
4911
4912 /* if interface is down do nothing */
4913 if (test_bit(__I40E_DOWN, &pf->state) ||
4914 test_bit(__I40E_CONFIG_BUSY, &pf->state))
4915 return;
4916
4917 /* Update the stats for active netdevs so the network stack
4918 * can look at updated numbers whenever it cares to
4919 */
4920 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
4921 if (pf->vsi[i] && pf->vsi[i]->netdev)
4922 i40e_update_stats(pf->vsi[i]);
4923
4924 /* Update the stats for the active switching components */
4925 for (i = 0; i < I40E_MAX_VEB; i++)
4926 if (pf->veb[i])
4927 i40e_update_veb_stats(pf->veb[i]);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00004928
4929 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004930}
4931
4932/**
4933 * i40e_reset_subtask - Set up for resetting the device and driver
4934 * @pf: board private structure
4935 **/
4936static void i40e_reset_subtask(struct i40e_pf *pf)
4937{
4938 u32 reset_flags = 0;
4939
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004940 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004941 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
4942 reset_flags |= (1 << __I40E_REINIT_REQUESTED);
4943 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
4944 }
4945 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
4946 reset_flags |= (1 << __I40E_PF_RESET_REQUESTED);
4947 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
4948 }
4949 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
4950 reset_flags |= (1 << __I40E_CORE_RESET_REQUESTED);
4951 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
4952 }
4953 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
4954 reset_flags |= (1 << __I40E_GLOBAL_RESET_REQUESTED);
4955 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
4956 }
4957
4958 /* If there's a recovery already waiting, it takes
4959 * precedence before starting a new reset sequence.
4960 */
4961 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
4962 i40e_handle_reset_warning(pf);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004963 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004964 }
4965
4966 /* If we're already down or resetting, just bail */
4967 if (reset_flags &&
4968 !test_bit(__I40E_DOWN, &pf->state) &&
4969 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
4970 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004971
4972unlock:
4973 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004974}
4975
4976/**
4977 * i40e_handle_link_event - Handle link event
4978 * @pf: board private structure
4979 * @e: event info posted on ARQ
4980 **/
4981static void i40e_handle_link_event(struct i40e_pf *pf,
4982 struct i40e_arq_event_info *e)
4983{
4984 struct i40e_hw *hw = &pf->hw;
4985 struct i40e_aqc_get_link_status *status =
4986 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
4987 struct i40e_link_status *hw_link_info = &hw->phy.link_info;
4988
4989 /* save off old link status information */
4990 memcpy(&pf->hw.phy.link_info_old, hw_link_info,
4991 sizeof(pf->hw.phy.link_info_old));
4992
4993 /* update link status */
4994 hw_link_info->phy_type = (enum i40e_aq_phy_type)status->phy_type;
4995 hw_link_info->link_speed = (enum i40e_aq_link_speed)status->link_speed;
4996 hw_link_info->link_info = status->link_info;
4997 hw_link_info->an_info = status->an_info;
4998 hw_link_info->ext_info = status->ext_info;
4999 hw_link_info->lse_enable =
5000 le16_to_cpu(status->command_flags) &
5001 I40E_AQ_LSE_ENABLE;
5002
5003 /* process the event */
5004 i40e_link_event(pf);
5005
5006 /* Do a new status request to re-enable LSE reporting
5007 * and load new status information into the hw struct,
5008 * then see if the status changed while processing the
5009 * initial event.
5010 */
5011 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
5012 i40e_link_event(pf);
5013}
5014
5015/**
5016 * i40e_clean_adminq_subtask - Clean the AdminQ rings
5017 * @pf: board private structure
5018 **/
5019static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
5020{
5021 struct i40e_arq_event_info event;
5022 struct i40e_hw *hw = &pf->hw;
5023 u16 pending, i = 0;
5024 i40e_status ret;
5025 u16 opcode;
5026 u32 val;
5027
5028 if (!test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state))
5029 return;
5030
Mitch Williams3197ce22013-11-28 06:39:39 +00005031 event.msg_size = I40E_MAX_AQ_BUF_SIZE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005032 event.msg_buf = kzalloc(event.msg_size, GFP_KERNEL);
5033 if (!event.msg_buf)
5034 return;
5035
5036 do {
Mitch Williams2f019122013-11-28 06:39:33 +00005037 event.msg_size = I40E_MAX_AQ_BUF_SIZE; /* reinit each time */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005038 ret = i40e_clean_arq_element(hw, &event, &pending);
5039 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
5040 dev_info(&pf->pdev->dev, "No ARQ event found\n");
5041 break;
5042 } else if (ret) {
5043 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
5044 break;
5045 }
5046
5047 opcode = le16_to_cpu(event.desc.opcode);
5048 switch (opcode) {
5049
5050 case i40e_aqc_opc_get_link_status:
5051 i40e_handle_link_event(pf, &event);
5052 break;
5053 case i40e_aqc_opc_send_msg_to_pf:
5054 ret = i40e_vc_process_vf_msg(pf,
5055 le16_to_cpu(event.desc.retval),
5056 le32_to_cpu(event.desc.cookie_high),
5057 le32_to_cpu(event.desc.cookie_low),
5058 event.msg_buf,
5059 event.msg_size);
5060 break;
5061 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005062 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005063#ifdef CONFIG_I40E_DCB
5064 rtnl_lock();
5065 ret = i40e_handle_lldp_event(pf, &event);
5066 rtnl_unlock();
5067#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005068 break;
5069 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005070 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005071 i40e_handle_lan_overflow_event(pf, &event);
5072 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00005073 case i40e_aqc_opc_send_msg_to_peer:
5074 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
5075 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005076 default:
5077 dev_info(&pf->pdev->dev,
Shannon Nelson0467bc92013-12-18 13:45:58 +00005078 "ARQ Error: Unknown event 0x%04x received\n",
5079 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005080 break;
5081 }
5082 } while (pending && (i++ < pf->adminq_work_limit));
5083
5084 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
5085 /* re-enable Admin queue interrupt cause */
5086 val = rd32(hw, I40E_PFINT_ICR0_ENA);
5087 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
5088 wr32(hw, I40E_PFINT_ICR0_ENA, val);
5089 i40e_flush(hw);
5090
5091 kfree(event.msg_buf);
5092}
5093
5094/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005095 * i40e_verify_eeprom - make sure eeprom is good to use
5096 * @pf: board private structure
5097 **/
5098static void i40e_verify_eeprom(struct i40e_pf *pf)
5099{
5100 int err;
5101
5102 err = i40e_diag_eeprom_test(&pf->hw);
5103 if (err) {
5104 /* retry in case of garbage read */
5105 err = i40e_diag_eeprom_test(&pf->hw);
5106 if (err) {
5107 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
5108 err);
5109 set_bit(__I40E_BAD_EEPROM, &pf->state);
5110 }
5111 }
5112
5113 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
5114 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
5115 clear_bit(__I40E_BAD_EEPROM, &pf->state);
5116 }
5117}
5118
5119/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005120 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
5121 * @veb: pointer to the VEB instance
5122 *
5123 * This is a recursive function that first builds the attached VSIs then
5124 * recurses in to build the next layer of VEB. We track the connections
5125 * through our own index numbers because the seid's from the HW could
5126 * change across the reset.
5127 **/
5128static int i40e_reconstitute_veb(struct i40e_veb *veb)
5129{
5130 struct i40e_vsi *ctl_vsi = NULL;
5131 struct i40e_pf *pf = veb->pf;
5132 int v, veb_idx;
5133 int ret;
5134
5135 /* build VSI that owns this VEB, temporarily attached to base VEB */
5136 for (v = 0; v < pf->hw.func_caps.num_vsis && !ctl_vsi; v++) {
5137 if (pf->vsi[v] &&
5138 pf->vsi[v]->veb_idx == veb->idx &&
5139 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
5140 ctl_vsi = pf->vsi[v];
5141 break;
5142 }
5143 }
5144 if (!ctl_vsi) {
5145 dev_info(&pf->pdev->dev,
5146 "missing owner VSI for veb_idx %d\n", veb->idx);
5147 ret = -ENOENT;
5148 goto end_reconstitute;
5149 }
5150 if (ctl_vsi != pf->vsi[pf->lan_vsi])
5151 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
5152 ret = i40e_add_vsi(ctl_vsi);
5153 if (ret) {
5154 dev_info(&pf->pdev->dev,
5155 "rebuild of owner VSI failed: %d\n", ret);
5156 goto end_reconstitute;
5157 }
5158 i40e_vsi_reset_stats(ctl_vsi);
5159
5160 /* create the VEB in the switch and move the VSI onto the VEB */
5161 ret = i40e_add_veb(veb, ctl_vsi);
5162 if (ret)
5163 goto end_reconstitute;
5164
5165 /* create the remaining VSIs attached to this VEB */
5166 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5167 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
5168 continue;
5169
5170 if (pf->vsi[v]->veb_idx == veb->idx) {
5171 struct i40e_vsi *vsi = pf->vsi[v];
5172 vsi->uplink_seid = veb->seid;
5173 ret = i40e_add_vsi(vsi);
5174 if (ret) {
5175 dev_info(&pf->pdev->dev,
5176 "rebuild of vsi_idx %d failed: %d\n",
5177 v, ret);
5178 goto end_reconstitute;
5179 }
5180 i40e_vsi_reset_stats(vsi);
5181 }
5182 }
5183
5184 /* create any VEBs attached to this VEB - RECURSION */
5185 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
5186 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
5187 pf->veb[veb_idx]->uplink_seid = veb->seid;
5188 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
5189 if (ret)
5190 break;
5191 }
5192 }
5193
5194end_reconstitute:
5195 return ret;
5196}
5197
5198/**
5199 * i40e_get_capabilities - get info about the HW
5200 * @pf: the PF struct
5201 **/
5202static int i40e_get_capabilities(struct i40e_pf *pf)
5203{
5204 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
5205 u16 data_size;
5206 int buf_len;
5207 int err;
5208
5209 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
5210 do {
5211 cap_buf = kzalloc(buf_len, GFP_KERNEL);
5212 if (!cap_buf)
5213 return -ENOMEM;
5214
5215 /* this loads the data into the hw struct for us */
5216 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
5217 &data_size,
5218 i40e_aqc_opc_list_func_capabilities,
5219 NULL);
5220 /* data loaded, buffer no longer needed */
5221 kfree(cap_buf);
5222
5223 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
5224 /* retry with a larger buffer */
5225 buf_len = data_size;
5226 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
5227 dev_info(&pf->pdev->dev,
5228 "capability discovery failed: aq=%d\n",
5229 pf->hw.aq.asq_last_status);
5230 return -ENODEV;
5231 }
5232 } while (err);
5233
Anjali Singhai Jainac71b7b2014-02-06 05:51:08 +00005234 if (((pf->hw.aq.fw_maj_ver == 2) && (pf->hw.aq.fw_min_ver < 22)) ||
5235 (pf->hw.aq.fw_maj_ver < 2)) {
5236 pf->hw.func_caps.num_msix_vectors++;
5237 pf->hw.func_caps.num_msix_vectors_vf++;
5238 }
5239
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005240 if (pf->hw.debug_mask & I40E_DEBUG_USER)
5241 dev_info(&pf->pdev->dev,
5242 "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",
5243 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
5244 pf->hw.func_caps.num_msix_vectors,
5245 pf->hw.func_caps.num_msix_vectors_vf,
5246 pf->hw.func_caps.fd_filters_guaranteed,
5247 pf->hw.func_caps.fd_filters_best_effort,
5248 pf->hw.func_caps.num_tx_qp,
5249 pf->hw.func_caps.num_vsis);
5250
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00005251#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
5252 + pf->hw.func_caps.num_vfs)
5253 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
5254 dev_info(&pf->pdev->dev,
5255 "got num_vsis %d, setting num_vsis to %d\n",
5256 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
5257 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
5258 }
5259
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005260 return 0;
5261}
5262
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005263static int i40e_vsi_clear(struct i40e_vsi *vsi);
5264
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005265/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005266 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005267 * @pf: board private structure
5268 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005269static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005270{
5271 struct i40e_vsi *vsi;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005272 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005273
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005274 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005275 return;
5276
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005277 /* find existing VSI and see if it needs configuring */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005278 vsi = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005279 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
5280 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005281 vsi = pf->vsi[i];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005282 break;
5283 }
5284 }
5285
5286 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005287 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005288 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
5289 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005290 if (!vsi) {
5291 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005292 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
5293 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005294 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005295 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005296
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005297 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005298}
5299
5300/**
5301 * i40e_fdir_teardown - release the Flow Director resources
5302 * @pf: board private structure
5303 **/
5304static void i40e_fdir_teardown(struct i40e_pf *pf)
5305{
5306 int i;
5307
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005308 i40e_fdir_filter_exit(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005309 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
5310 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
5311 i40e_vsi_release(pf->vsi[i]);
5312 break;
5313 }
5314 }
5315}
5316
5317/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005318 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005319 * @pf: board private structure
5320 *
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005321 * Close up the VFs and other things in prep for pf Reset.
5322 **/
5323static int i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005324{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005325 struct i40e_hw *hw = &pf->hw;
5326 i40e_status ret;
5327 u32 v;
5328
5329 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
5330 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005331 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005332
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005333 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005334
Anjali Singhai Jain37f0be62013-11-28 06:39:46 +00005335 if (i40e_check_asq_alive(hw))
5336 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005337
5338 /* quiesce the VSIs and their queues that are not already DOWN */
5339 i40e_pf_quiesce_all_vsi(pf);
5340
5341 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5342 if (pf->vsi[v])
5343 pf->vsi[v]->seid = 0;
5344 }
5345
5346 i40e_shutdown_adminq(&pf->hw);
5347
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005348 /* call shutdown HMC */
5349 ret = i40e_shutdown_lan_hmc(hw);
5350 if (ret) {
5351 dev_info(&pf->pdev->dev, "shutdown_lan_hmc failed: %d\n", ret);
5352 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
5353 }
5354 return ret;
5355}
5356
5357/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00005358 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005359 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005360 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005361 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005362static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005363{
5364 struct i40e_driver_version dv;
5365 struct i40e_hw *hw = &pf->hw;
5366 i40e_status ret;
5367 u32 v;
5368
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005369 /* Now we wait for GRST to settle out.
5370 * We don't have to delete the VEBs or VSIs from the hw switch
5371 * because the reset will make them disappear.
5372 */
5373 ret = i40e_pf_reset(hw);
5374 if (ret)
5375 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
5376 pf->pfr_count++;
5377
5378 if (test_bit(__I40E_DOWN, &pf->state))
5379 goto end_core_reset;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005380 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005381
5382 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
5383 ret = i40e_init_adminq(&pf->hw);
5384 if (ret) {
5385 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, %d\n", ret);
5386 goto end_core_reset;
5387 }
5388
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005389 /* re-verify the eeprom if we just had an EMP reset */
5390 if (test_bit(__I40E_EMP_RESET_REQUESTED, &pf->state)) {
5391 clear_bit(__I40E_EMP_RESET_REQUESTED, &pf->state);
5392 i40e_verify_eeprom(pf);
5393 }
5394
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005395 ret = i40e_get_capabilities(pf);
5396 if (ret) {
5397 dev_info(&pf->pdev->dev, "i40e_get_capabilities failed, %d\n",
5398 ret);
5399 goto end_core_reset;
5400 }
5401
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005402 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
5403 hw->func_caps.num_rx_qp,
5404 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
5405 if (ret) {
5406 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
5407 goto end_core_reset;
5408 }
5409 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
5410 if (ret) {
5411 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
5412 goto end_core_reset;
5413 }
5414
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005415#ifdef CONFIG_I40E_DCB
5416 ret = i40e_init_pf_dcb(pf);
5417 if (ret) {
5418 dev_info(&pf->pdev->dev, "init_pf_dcb failed: %d\n", ret);
5419 goto end_core_reset;
5420 }
5421#endif /* CONFIG_I40E_DCB */
5422
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005423 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005424 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005425 if (ret)
5426 goto end_core_reset;
5427
5428 /* Rebuild the VSIs and VEBs that existed before reset.
5429 * They are still in our local switch element arrays, so only
5430 * need to rebuild the switch model in the HW.
5431 *
5432 * If there were VEBs but the reconstitution failed, we'll try
5433 * try to recover minimal use by getting the basic PF VSI working.
5434 */
5435 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005436 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005437 /* find the one VEB connected to the MAC, and find orphans */
5438 for (v = 0; v < I40E_MAX_VEB; v++) {
5439 if (!pf->veb[v])
5440 continue;
5441
5442 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
5443 pf->veb[v]->uplink_seid == 0) {
5444 ret = i40e_reconstitute_veb(pf->veb[v]);
5445
5446 if (!ret)
5447 continue;
5448
5449 /* If Main VEB failed, we're in deep doodoo,
5450 * so give up rebuilding the switch and set up
5451 * for minimal rebuild of PF VSI.
5452 * If orphan failed, we'll report the error
5453 * but try to keep going.
5454 */
5455 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
5456 dev_info(&pf->pdev->dev,
5457 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
5458 ret);
5459 pf->vsi[pf->lan_vsi]->uplink_seid
5460 = pf->mac_seid;
5461 break;
5462 } else if (pf->veb[v]->uplink_seid == 0) {
5463 dev_info(&pf->pdev->dev,
5464 "rebuild of orphan VEB failed: %d\n",
5465 ret);
5466 }
5467 }
5468 }
5469 }
5470
5471 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
5472 dev_info(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
5473 /* no VEB, so rebuild only the Main VSI */
5474 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
5475 if (ret) {
5476 dev_info(&pf->pdev->dev,
5477 "rebuild of Main VSI failed: %d\n", ret);
5478 goto end_core_reset;
5479 }
5480 }
5481
5482 /* reinit the misc interrupt */
5483 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5484 ret = i40e_setup_misc_vector(pf);
5485
5486 /* restart the VSIs that were rebuilt and running before the reset */
5487 i40e_pf_unquiesce_all_vsi(pf);
5488
Mitch Williams69f64b22014-02-13 03:48:46 -08005489 if (pf->num_alloc_vfs) {
5490 for (v = 0; v < pf->num_alloc_vfs; v++)
5491 i40e_reset_vf(&pf->vf[v], true);
5492 }
5493
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005494 /* tell the firmware that we're starting */
5495 dv.major_version = DRV_VERSION_MAJOR;
5496 dv.minor_version = DRV_VERSION_MINOR;
5497 dv.build_version = DRV_VERSION_BUILD;
5498 dv.subbuild_version = 0;
5499 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
5500
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005501 dev_info(&pf->pdev->dev, "reset complete\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005502
5503end_core_reset:
5504 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
5505}
5506
5507/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005508 * i40e_handle_reset_warning - prep for the pf to reset, reset and rebuild
5509 * @pf: board private structure
5510 *
5511 * Close up the VFs and other things in prep for a Core Reset,
5512 * then get ready to rebuild the world.
5513 **/
5514static void i40e_handle_reset_warning(struct i40e_pf *pf)
5515{
5516 i40e_status ret;
5517
5518 ret = i40e_prep_for_reset(pf);
5519 if (!ret)
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005520 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005521}
5522
5523/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005524 * i40e_handle_mdd_event
5525 * @pf: pointer to the pf structure
5526 *
5527 * Called from the MDD irq handler to identify possibly malicious vfs
5528 **/
5529static void i40e_handle_mdd_event(struct i40e_pf *pf)
5530{
5531 struct i40e_hw *hw = &pf->hw;
5532 bool mdd_detected = false;
5533 struct i40e_vf *vf;
5534 u32 reg;
5535 int i;
5536
5537 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
5538 return;
5539
5540 /* find what triggered the MDD event */
5541 reg = rd32(hw, I40E_GL_MDET_TX);
5542 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
5543 u8 func = (reg & I40E_GL_MDET_TX_FUNCTION_MASK)
5544 >> I40E_GL_MDET_TX_FUNCTION_SHIFT;
5545 u8 event = (reg & I40E_GL_MDET_TX_EVENT_SHIFT)
5546 >> I40E_GL_MDET_TX_EVENT_SHIFT;
5547 u8 queue = (reg & I40E_GL_MDET_TX_QUEUE_MASK)
5548 >> I40E_GL_MDET_TX_QUEUE_SHIFT;
5549 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00005550 "Malicious Driver Detection event 0x%02x on TX queue %d of function 0x%02x\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005551 event, queue, func);
5552 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
5553 mdd_detected = true;
5554 }
5555 reg = rd32(hw, I40E_GL_MDET_RX);
5556 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
5557 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK)
5558 >> I40E_GL_MDET_RX_FUNCTION_SHIFT;
5559 u8 event = (reg & I40E_GL_MDET_RX_EVENT_SHIFT)
5560 >> I40E_GL_MDET_RX_EVENT_SHIFT;
5561 u8 queue = (reg & I40E_GL_MDET_RX_QUEUE_MASK)
5562 >> I40E_GL_MDET_RX_QUEUE_SHIFT;
5563 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00005564 "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005565 event, queue, func);
5566 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
5567 mdd_detected = true;
5568 }
5569
5570 /* see if one of the VFs needs its hand slapped */
5571 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
5572 vf = &(pf->vf[i]);
5573 reg = rd32(hw, I40E_VP_MDET_TX(i));
5574 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
5575 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
5576 vf->num_mdd_events++;
5577 dev_info(&pf->pdev->dev, "MDD TX event on VF %d\n", i);
5578 }
5579
5580 reg = rd32(hw, I40E_VP_MDET_RX(i));
5581 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
5582 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
5583 vf->num_mdd_events++;
5584 dev_info(&pf->pdev->dev, "MDD RX event on VF %d\n", i);
5585 }
5586
5587 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
5588 dev_info(&pf->pdev->dev,
5589 "Too many MDD events on VF %d, disabled\n", i);
5590 dev_info(&pf->pdev->dev,
5591 "Use PF Control I/F to re-enable the VF\n");
5592 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
5593 }
5594 }
5595
5596 /* re-enable mdd interrupt cause */
5597 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
5598 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
5599 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
5600 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
5601 i40e_flush(hw);
5602}
5603
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005604#ifdef CONFIG_I40E_VXLAN
5605/**
5606 * i40e_sync_vxlan_filters_subtask - Sync the VSI filter list with HW
5607 * @pf: board private structure
5608 **/
5609static void i40e_sync_vxlan_filters_subtask(struct i40e_pf *pf)
5610{
5611 const int vxlan_hdr_qwords = 4;
5612 struct i40e_hw *hw = &pf->hw;
5613 i40e_status ret;
5614 u8 filter_index;
5615 __be16 port;
5616 int i;
5617
5618 if (!(pf->flags & I40E_FLAG_VXLAN_FILTER_SYNC))
5619 return;
5620
5621 pf->flags &= ~I40E_FLAG_VXLAN_FILTER_SYNC;
5622
5623 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
5624 if (pf->pending_vxlan_bitmap & (1 << i)) {
5625 pf->pending_vxlan_bitmap &= ~(1 << i);
5626 port = pf->vxlan_ports[i];
5627 ret = port ?
5628 i40e_aq_add_udp_tunnel(hw, ntohs(port),
5629 vxlan_hdr_qwords,
5630 I40E_AQC_TUNNEL_TYPE_VXLAN,
5631 &filter_index, NULL)
5632 : i40e_aq_del_udp_tunnel(hw, i, NULL);
5633
5634 if (ret) {
5635 dev_info(&pf->pdev->dev, "Failed to execute AQ command for %s port %d with index %d\n",
5636 port ? "adding" : "deleting",
5637 ntohs(port), port ? i : i);
5638
5639 pf->vxlan_ports[i] = 0;
5640 } else {
5641 dev_info(&pf->pdev->dev, "%s port %d with AQ command with index %d\n",
5642 port ? "Added" : "Deleted",
5643 ntohs(port), port ? i : filter_index);
5644 }
5645 }
5646 }
5647}
5648
5649#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005650/**
5651 * i40e_service_task - Run the driver's async subtasks
5652 * @work: pointer to work_struct containing our data
5653 **/
5654static void i40e_service_task(struct work_struct *work)
5655{
5656 struct i40e_pf *pf = container_of(work,
5657 struct i40e_pf,
5658 service_task);
5659 unsigned long start_time = jiffies;
5660
5661 i40e_reset_subtask(pf);
5662 i40e_handle_mdd_event(pf);
5663 i40e_vc_process_vflr_event(pf);
5664 i40e_watchdog_subtask(pf);
5665 i40e_fdir_reinit_subtask(pf);
5666 i40e_check_hang_subtask(pf);
5667 i40e_sync_filters_subtask(pf);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005668#ifdef CONFIG_I40E_VXLAN
5669 i40e_sync_vxlan_filters_subtask(pf);
5670#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005671 i40e_clean_adminq_subtask(pf);
5672
5673 i40e_service_event_complete(pf);
5674
5675 /* If the tasks have taken longer than one timer cycle or there
5676 * is more work to be done, reschedule the service task now
5677 * rather than wait for the timer to tick again.
5678 */
5679 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
5680 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
5681 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
5682 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
5683 i40e_service_event_schedule(pf);
5684}
5685
5686/**
5687 * i40e_service_timer - timer callback
5688 * @data: pointer to PF struct
5689 **/
5690static void i40e_service_timer(unsigned long data)
5691{
5692 struct i40e_pf *pf = (struct i40e_pf *)data;
5693
5694 mod_timer(&pf->service_timer,
5695 round_jiffies(jiffies + pf->service_timer_period));
5696 i40e_service_event_schedule(pf);
5697}
5698
5699/**
5700 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
5701 * @vsi: the VSI being configured
5702 **/
5703static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
5704{
5705 struct i40e_pf *pf = vsi->back;
5706
5707 switch (vsi->type) {
5708 case I40E_VSI_MAIN:
5709 vsi->alloc_queue_pairs = pf->num_lan_qps;
5710 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5711 I40E_REQ_DESCRIPTOR_MULTIPLE);
5712 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5713 vsi->num_q_vectors = pf->num_lan_msix;
5714 else
5715 vsi->num_q_vectors = 1;
5716
5717 break;
5718
5719 case I40E_VSI_FDIR:
5720 vsi->alloc_queue_pairs = 1;
5721 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
5722 I40E_REQ_DESCRIPTOR_MULTIPLE);
5723 vsi->num_q_vectors = 1;
5724 break;
5725
5726 case I40E_VSI_VMDQ2:
5727 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
5728 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5729 I40E_REQ_DESCRIPTOR_MULTIPLE);
5730 vsi->num_q_vectors = pf->num_vmdq_msix;
5731 break;
5732
5733 case I40E_VSI_SRIOV:
5734 vsi->alloc_queue_pairs = pf->num_vf_qps;
5735 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5736 I40E_REQ_DESCRIPTOR_MULTIPLE);
5737 break;
5738
5739 default:
5740 WARN_ON(1);
5741 return -ENODATA;
5742 }
5743
5744 return 0;
5745}
5746
5747/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005748 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
5749 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005750 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005751 *
5752 * On error: returns error code (negative)
5753 * On success: returns 0
5754 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005755static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005756{
5757 int size;
5758 int ret = 0;
5759
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00005760 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005761 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
5762 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
5763 if (!vsi->tx_rings)
5764 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005765 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
5766
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005767 if (alloc_qvectors) {
5768 /* allocate memory for q_vector pointers */
5769 size = sizeof(struct i40e_q_vectors *) * vsi->num_q_vectors;
5770 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
5771 if (!vsi->q_vectors) {
5772 ret = -ENOMEM;
5773 goto err_vectors;
5774 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005775 }
5776 return ret;
5777
5778err_vectors:
5779 kfree(vsi->tx_rings);
5780 return ret;
5781}
5782
5783/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005784 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
5785 * @pf: board private structure
5786 * @type: type of VSI
5787 *
5788 * On error: returns error code (negative)
5789 * On success: returns vsi index in PF (positive)
5790 **/
5791static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
5792{
5793 int ret = -ENODEV;
5794 struct i40e_vsi *vsi;
5795 int vsi_idx;
5796 int i;
5797
5798 /* Need to protect the allocation of the VSIs at the PF level */
5799 mutex_lock(&pf->switch_mutex);
5800
5801 /* VSI list may be fragmented if VSI creation/destruction has
5802 * been happening. We can afford to do a quick scan to look
5803 * for any free VSIs in the list.
5804 *
5805 * find next empty vsi slot, looping back around if necessary
5806 */
5807 i = pf->next_vsi;
5808 while (i < pf->hw.func_caps.num_vsis && pf->vsi[i])
5809 i++;
5810 if (i >= pf->hw.func_caps.num_vsis) {
5811 i = 0;
5812 while (i < pf->next_vsi && pf->vsi[i])
5813 i++;
5814 }
5815
5816 if (i < pf->hw.func_caps.num_vsis && !pf->vsi[i]) {
5817 vsi_idx = i; /* Found one! */
5818 } else {
5819 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00005820 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005821 }
5822 pf->next_vsi = ++i;
5823
5824 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
5825 if (!vsi) {
5826 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00005827 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005828 }
5829 vsi->type = type;
5830 vsi->back = pf;
5831 set_bit(__I40E_DOWN, &vsi->state);
5832 vsi->flags = 0;
5833 vsi->idx = vsi_idx;
5834 vsi->rx_itr_setting = pf->rx_itr_default;
5835 vsi->tx_itr_setting = pf->tx_itr_default;
5836 vsi->netdev_registered = false;
5837 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
5838 INIT_LIST_HEAD(&vsi->mac_filter_list);
5839
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005840 ret = i40e_set_num_rings_in_vsi(vsi);
5841 if (ret)
5842 goto err_rings;
5843
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005844 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005845 if (ret)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005846 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00005847
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005848 /* Setup default MSIX irq handler for VSI */
5849 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
5850
5851 pf->vsi[vsi_idx] = vsi;
5852 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00005853 goto unlock_pf;
5854
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005855err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00005856 pf->next_vsi = i - 1;
5857 kfree(vsi);
5858unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005859 mutex_unlock(&pf->switch_mutex);
5860 return ret;
5861}
5862
5863/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005864 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
5865 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005866 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005867 *
5868 * On error: returns error code (negative)
5869 * On success: returns 0
5870 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005871static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005872{
5873 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005874 if (free_qvectors) {
5875 kfree(vsi->q_vectors);
5876 vsi->q_vectors = NULL;
5877 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005878 kfree(vsi->tx_rings);
5879 vsi->tx_rings = NULL;
5880 vsi->rx_rings = NULL;
5881}
5882
5883/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005884 * i40e_vsi_clear - Deallocate the VSI provided
5885 * @vsi: the VSI being un-configured
5886 **/
5887static int i40e_vsi_clear(struct i40e_vsi *vsi)
5888{
5889 struct i40e_pf *pf;
5890
5891 if (!vsi)
5892 return 0;
5893
5894 if (!vsi->back)
5895 goto free_vsi;
5896 pf = vsi->back;
5897
5898 mutex_lock(&pf->switch_mutex);
5899 if (!pf->vsi[vsi->idx]) {
5900 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
5901 vsi->idx, vsi->idx, vsi, vsi->type);
5902 goto unlock_vsi;
5903 }
5904
5905 if (pf->vsi[vsi->idx] != vsi) {
5906 dev_err(&pf->pdev->dev,
5907 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
5908 pf->vsi[vsi->idx]->idx,
5909 pf->vsi[vsi->idx],
5910 pf->vsi[vsi->idx]->type,
5911 vsi->idx, vsi, vsi->type);
5912 goto unlock_vsi;
5913 }
5914
5915 /* updates the pf for this cleared vsi */
5916 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
5917 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
5918
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005919 i40e_vsi_free_arrays(vsi, true);
Alexander Duyck493fb302013-09-28 07:01:44 +00005920
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005921 pf->vsi[vsi->idx] = NULL;
5922 if (vsi->idx < pf->next_vsi)
5923 pf->next_vsi = vsi->idx;
5924
5925unlock_vsi:
5926 mutex_unlock(&pf->switch_mutex);
5927free_vsi:
5928 kfree(vsi);
5929
5930 return 0;
5931}
5932
5933/**
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005934 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
5935 * @vsi: the VSI being cleaned
5936 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00005937static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005938{
5939 int i;
5940
Greg Rose8e9dca52013-12-18 13:45:53 +00005941 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00005942 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00005943 kfree_rcu(vsi->tx_rings[i], rcu);
5944 vsi->tx_rings[i] = NULL;
5945 vsi->rx_rings[i] = NULL;
5946 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00005947 }
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005948}
5949
5950/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005951 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
5952 * @vsi: the VSI being configured
5953 **/
5954static int i40e_alloc_rings(struct i40e_vsi *vsi)
5955{
5956 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005957 int i;
5958
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005959 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00005960 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005961 struct i40e_ring *tx_ring;
5962 struct i40e_ring *rx_ring;
5963
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00005964 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005965 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
5966 if (!tx_ring)
5967 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005968
5969 tx_ring->queue_index = i;
5970 tx_ring->reg_idx = vsi->base_queue + i;
5971 tx_ring->ring_active = false;
5972 tx_ring->vsi = vsi;
5973 tx_ring->netdev = vsi->netdev;
5974 tx_ring->dev = &pf->pdev->dev;
5975 tx_ring->count = vsi->num_desc;
5976 tx_ring->size = 0;
5977 tx_ring->dcb_tc = 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005978 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005979
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005980 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005981 rx_ring->queue_index = i;
5982 rx_ring->reg_idx = vsi->base_queue + i;
5983 rx_ring->ring_active = false;
5984 rx_ring->vsi = vsi;
5985 rx_ring->netdev = vsi->netdev;
5986 rx_ring->dev = &pf->pdev->dev;
5987 rx_ring->count = vsi->num_desc;
5988 rx_ring->size = 0;
5989 rx_ring->dcb_tc = 0;
5990 if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
5991 set_ring_16byte_desc_enabled(rx_ring);
5992 else
5993 clear_ring_16byte_desc_enabled(rx_ring);
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005994 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005995 }
5996
5997 return 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005998
5999err_out:
6000 i40e_vsi_clear_rings(vsi);
6001 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006002}
6003
6004/**
6005 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
6006 * @pf: board private structure
6007 * @vectors: the number of MSI-X vectors to request
6008 *
6009 * Returns the number of vectors reserved, or error
6010 **/
6011static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
6012{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01006013 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
6014 I40E_MIN_MSIX, vectors);
6015 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006016 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01006017 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006018 vectors = 0;
6019 }
6020
Alexander Gordeev7b37f372014-02-18 11:11:42 +01006021 pf->num_msix_entries = vectors;
6022
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006023 return vectors;
6024}
6025
6026/**
6027 * i40e_init_msix - Setup the MSIX capability
6028 * @pf: board private structure
6029 *
6030 * Work with the OS to set up the MSIX vectors needed.
6031 *
6032 * Returns 0 on success, negative on failure
6033 **/
6034static int i40e_init_msix(struct i40e_pf *pf)
6035{
6036 i40e_status err = 0;
6037 struct i40e_hw *hw = &pf->hw;
6038 int v_budget, i;
6039 int vec;
6040
6041 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
6042 return -ENODEV;
6043
6044 /* The number of vectors we'll request will be comprised of:
6045 * - Add 1 for "other" cause for Admin Queue events, etc.
6046 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006047 * - Queues being used for RSS.
6048 * We don't need as many as max_rss_size vectors.
6049 * use rss_size instead in the calculation since that
6050 * is governed by number of cpus in the system.
6051 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006052 * - The number of VMDq pairs
6053 * Once we count this up, try the request.
6054 *
6055 * If we can't get what we want, we'll simplify to nearly nothing
6056 * and try again. If that still fails, we punt.
6057 */
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006058 pf->num_lan_msix = pf->num_lan_qps - (pf->rss_size_max - pf->rss_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006059 pf->num_vmdq_msix = pf->num_vmdq_qps;
6060 v_budget = 1 + pf->num_lan_msix;
6061 v_budget += (pf->num_vmdq_vsis * pf->num_vmdq_msix);
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006062 if (pf->flags & I40E_FLAG_FD_SB_ENABLED)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006063 v_budget++;
6064
6065 /* Scale down if necessary, and the rings will share vectors */
6066 v_budget = min_t(int, v_budget, hw->func_caps.num_msix_vectors);
6067
6068 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
6069 GFP_KERNEL);
6070 if (!pf->msix_entries)
6071 return -ENOMEM;
6072
6073 for (i = 0; i < v_budget; i++)
6074 pf->msix_entries[i].entry = i;
6075 vec = i40e_reserve_msix_vectors(pf, v_budget);
6076 if (vec < I40E_MIN_MSIX) {
6077 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
6078 kfree(pf->msix_entries);
6079 pf->msix_entries = NULL;
6080 return -ENODEV;
6081
6082 } else if (vec == I40E_MIN_MSIX) {
6083 /* Adjust for minimal MSIX use */
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006084 dev_info(&pf->pdev->dev, "Features disabled, not enough MSI-X vectors\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006085 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
6086 pf->num_vmdq_vsis = 0;
6087 pf->num_vmdq_qps = 0;
6088 pf->num_vmdq_msix = 0;
6089 pf->num_lan_qps = 1;
6090 pf->num_lan_msix = 1;
6091
6092 } else if (vec != v_budget) {
6093 /* Scale vector usage down */
6094 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
6095 vec--; /* reserve the misc vector */
6096
6097 /* partition out the remaining vectors */
6098 switch (vec) {
6099 case 2:
6100 pf->num_vmdq_vsis = 1;
6101 pf->num_lan_msix = 1;
6102 break;
6103 case 3:
6104 pf->num_vmdq_vsis = 1;
6105 pf->num_lan_msix = 2;
6106 break;
6107 default:
6108 pf->num_lan_msix = min_t(int, (vec / 2),
6109 pf->num_lan_qps);
6110 pf->num_vmdq_vsis = min_t(int, (vec - pf->num_lan_msix),
6111 I40E_DEFAULT_NUM_VMDQ_VSI);
6112 break;
6113 }
6114 }
6115
6116 return err;
6117}
6118
6119/**
Greg Rose90e04072014-03-06 08:59:57 +00006120 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00006121 * @vsi: the VSI being configured
6122 * @v_idx: index of the vector in the vsi struct
6123 *
6124 * We allocate one q_vector. If allocation fails we return -ENOMEM.
6125 **/
Greg Rose90e04072014-03-06 08:59:57 +00006126static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
Alexander Duyck493fb302013-09-28 07:01:44 +00006127{
6128 struct i40e_q_vector *q_vector;
6129
6130 /* allocate q_vector */
6131 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
6132 if (!q_vector)
6133 return -ENOMEM;
6134
6135 q_vector->vsi = vsi;
6136 q_vector->v_idx = v_idx;
6137 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
6138 if (vsi->netdev)
6139 netif_napi_add(vsi->netdev, &q_vector->napi,
6140 i40e_napi_poll, vsi->work_limit);
6141
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00006142 q_vector->rx.latency_range = I40E_LOW_LATENCY;
6143 q_vector->tx.latency_range = I40E_LOW_LATENCY;
6144
Alexander Duyck493fb302013-09-28 07:01:44 +00006145 /* tie q_vector and vsi together */
6146 vsi->q_vectors[v_idx] = q_vector;
6147
6148 return 0;
6149}
6150
6151/**
Greg Rose90e04072014-03-06 08:59:57 +00006152 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006153 * @vsi: the VSI being configured
6154 *
6155 * We allocate one q_vector per queue interrupt. If allocation fails we
6156 * return -ENOMEM.
6157 **/
Greg Rose90e04072014-03-06 08:59:57 +00006158static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006159{
6160 struct i40e_pf *pf = vsi->back;
6161 int v_idx, num_q_vectors;
Alexander Duyck493fb302013-09-28 07:01:44 +00006162 int err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006163
6164 /* if not MSIX, give the one vector only to the LAN VSI */
6165 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6166 num_q_vectors = vsi->num_q_vectors;
6167 else if (vsi == pf->vsi[pf->lan_vsi])
6168 num_q_vectors = 1;
6169 else
6170 return -EINVAL;
6171
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006172 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Greg Rose90e04072014-03-06 08:59:57 +00006173 err = i40e_vsi_alloc_q_vector(vsi, v_idx);
Alexander Duyck493fb302013-09-28 07:01:44 +00006174 if (err)
6175 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006176 }
6177
6178 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00006179
6180err_out:
6181 while (v_idx--)
6182 i40e_free_q_vector(vsi, v_idx);
6183
6184 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006185}
6186
6187/**
6188 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
6189 * @pf: board private structure to initialize
6190 **/
6191static void i40e_init_interrupt_scheme(struct i40e_pf *pf)
6192{
6193 int err = 0;
6194
6195 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
6196 err = i40e_init_msix(pf);
6197 if (err) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006198 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
6199 I40E_FLAG_RSS_ENABLED |
6200 I40E_FLAG_DCB_ENABLED |
6201 I40E_FLAG_SRIOV_ENABLED |
6202 I40E_FLAG_FD_SB_ENABLED |
6203 I40E_FLAG_FD_ATR_ENABLED |
6204 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006205
6206 /* rework the queue expectations without MSIX */
6207 i40e_determine_queue_usage(pf);
6208 }
6209 }
6210
6211 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
6212 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006213 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006214 err = pci_enable_msi(pf->pdev);
6215 if (err) {
Shannon Nelson958a3e32013-09-28 07:13:28 +00006216 dev_info(&pf->pdev->dev, "MSI init failed - %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006217 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
6218 }
6219 }
6220
Shannon Nelson958a3e32013-09-28 07:13:28 +00006221 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006222 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00006223
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006224 /* track first vector for misc interrupts */
6225 err = i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT-1);
6226}
6227
6228/**
6229 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
6230 * @pf: board private structure
6231 *
6232 * This sets up the handler for MSIX 0, which is used to manage the
6233 * non-queue interrupts, e.g. AdminQ and errors. This is not used
6234 * when in MSI or Legacy interrupt mode.
6235 **/
6236static int i40e_setup_misc_vector(struct i40e_pf *pf)
6237{
6238 struct i40e_hw *hw = &pf->hw;
6239 int err = 0;
6240
6241 /* Only request the irq if this is the first time through, and
6242 * not when we're rebuilding after a Reset
6243 */
6244 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
6245 err = request_irq(pf->msix_entries[0].vector,
6246 i40e_intr, 0, pf->misc_int_name, pf);
6247 if (err) {
6248 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006249 "request_irq for %s failed: %d\n",
6250 pf->misc_int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006251 return -EFAULT;
6252 }
6253 }
6254
6255 i40e_enable_misc_int_causes(hw);
6256
6257 /* associate no queues to the misc vector */
6258 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
6259 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
6260
6261 i40e_flush(hw);
6262
6263 i40e_irq_dynamic_enable_icr0(pf);
6264
6265 return err;
6266}
6267
6268/**
6269 * i40e_config_rss - Prepare for RSS if used
6270 * @pf: board private structure
6271 **/
6272static int i40e_config_rss(struct i40e_pf *pf)
6273{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006274 /* Set of random keys generated using kernel random number generator */
6275 static const u32 seed[I40E_PFQF_HKEY_MAX_INDEX + 1] = {0x41b01687,
6276 0x183cfd8c, 0xce880440, 0x580cbc3c, 0x35897377,
6277 0x328b25e1, 0x4fa98922, 0xb7d90c14, 0xd5bad70d,
6278 0xcd15a2c1, 0xe8580225, 0x4a1e9d11, 0xfe5731be};
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00006279 struct i40e_hw *hw = &pf->hw;
6280 u32 lut = 0;
6281 int i, j;
6282 u64 hena;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006283
6284 /* Fill out hash function seed */
6285 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
6286 wr32(hw, I40E_PFQF_HKEY(i), seed[i]);
6287
6288 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
6289 hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
6290 ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
Mitch Williams12dc4fe2013-11-28 06:39:32 +00006291 hena |= I40E_DEFAULT_RSS_HENA;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006292 wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
6293 wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
6294
6295 /* Populate the LUT with max no. of queues in round robin fashion */
6296 for (i = 0, j = 0; i < pf->hw.func_caps.rss_table_size; i++, j++) {
6297
6298 /* The assumption is that lan qp count will be the highest
6299 * qp count for any PF VSI that needs RSS.
6300 * If multiple VSIs need RSS support, all the qp counts
6301 * for those VSIs should be a power of 2 for RSS to work.
6302 * If LAN VSI is the only consumer for RSS then this requirement
6303 * is not necessary.
6304 */
6305 if (j == pf->rss_size)
6306 j = 0;
6307 /* lut = 4-byte sliding window of 4 lut entries */
6308 lut = (lut << 8) | (j &
6309 ((0x1 << pf->hw.func_caps.rss_table_entry_width) - 1));
6310 /* On i = 3, we have 4 entries in lut; write to the register */
6311 if ((i & 3) == 3)
6312 wr32(hw, I40E_PFQF_HLUT(i >> 2), lut);
6313 }
6314 i40e_flush(hw);
6315
6316 return 0;
6317}
6318
6319/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006320 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
6321 * @pf: board private structure
6322 * @queue_count: the requested queue count for rss.
6323 *
6324 * returns 0 if rss is not enabled, if enabled returns the final rss queue
6325 * count which may be different from the requested queue count.
6326 **/
6327int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
6328{
6329 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
6330 return 0;
6331
6332 queue_count = min_t(int, queue_count, pf->rss_size_max);
6333 queue_count = rounddown_pow_of_two(queue_count);
6334
6335 if (queue_count != pf->rss_size) {
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006336 i40e_prep_for_reset(pf);
6337
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006338 pf->rss_size = queue_count;
6339
6340 i40e_reset_and_rebuild(pf, true);
6341 i40e_config_rss(pf);
6342 }
6343 dev_info(&pf->pdev->dev, "RSS count: %d\n", pf->rss_size);
6344 return pf->rss_size;
6345}
6346
6347/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006348 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
6349 * @pf: board private structure to initialize
6350 *
6351 * i40e_sw_init initializes the Adapter private data structure.
6352 * Fields are initialized based on PCI device information and
6353 * OS network device settings (MTU size).
6354 **/
6355static int i40e_sw_init(struct i40e_pf *pf)
6356{
6357 int err = 0;
6358 int size;
6359
6360 pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
6361 (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
Shannon Nelson27599972013-11-16 10:00:43 +00006362 pf->hw.debug_mask = pf->msg_enable | I40E_DEBUG_DIAG;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006363 if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
6364 if (I40E_DEBUG_USER & debug)
6365 pf->hw.debug_mask = debug;
6366 pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
6367 I40E_DEFAULT_MSG_ENABLE);
6368 }
6369
6370 /* Set default capability flags */
6371 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
6372 I40E_FLAG_MSI_ENABLED |
6373 I40E_FLAG_MSIX_ENABLED |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006374 I40E_FLAG_RX_1BUF_ENABLED;
6375
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006376 /* Depending on PF configurations, it is possible that the RSS
6377 * maximum might end up larger than the available queues
6378 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006379 pf->rss_size_max = 0x1 << pf->hw.func_caps.rss_table_entry_width;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006380 pf->rss_size_max = min_t(int, pf->rss_size_max,
6381 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006382 if (pf->hw.func_caps.rss) {
6383 pf->flags |= I40E_FLAG_RSS_ENABLED;
Jesse Brandeburgbf051a32013-11-26 10:49:17 +00006384 pf->rss_size = min_t(int, pf->rss_size_max, num_online_cpus());
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006385 pf->rss_size = rounddown_pow_of_two(pf->rss_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006386 } else {
6387 pf->rss_size = 1;
6388 }
6389
Catherine Sullivan2050bc62013-12-18 13:46:03 +00006390 /* MFP mode enabled */
6391 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.mfp_mode_1) {
6392 pf->flags |= I40E_FLAG_MFP_ENABLED;
6393 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
6394 }
6395
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006396 /* FW/NVM is not yet fixed in this regard */
6397 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
6398 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
6399 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
6400 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006401 if (!(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006402 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006403 } else {
6404 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00006405 "Flow Director Sideband mode Disabled in MFP mode\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006406 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006407 pf->fdir_pf_filter_count =
6408 pf->hw.func_caps.fd_filters_guaranteed;
6409 pf->hw.fdir_shared_filter_count =
6410 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006411 }
6412
6413 if (pf->hw.func_caps.vmdq) {
6414 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
6415 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
6416 pf->num_vmdq_qps = I40E_DEFAULT_QUEUES_PER_VMDQ;
6417 }
6418
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006419#ifdef CONFIG_PCI_IOV
6420 if (pf->hw.func_caps.num_vfs) {
6421 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
6422 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
6423 pf->num_req_vfs = min_t(int,
6424 pf->hw.func_caps.num_vfs,
6425 I40E_MAX_VF_COUNT);
6426 }
6427#endif /* CONFIG_PCI_IOV */
6428 pf->eeprom_version = 0xDEAD;
6429 pf->lan_veb = I40E_NO_VEB;
6430 pf->lan_vsi = I40E_NO_VSI;
6431
6432 /* set up queue assignment tracking */
6433 size = sizeof(struct i40e_lump_tracking)
6434 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
6435 pf->qp_pile = kzalloc(size, GFP_KERNEL);
6436 if (!pf->qp_pile) {
6437 err = -ENOMEM;
6438 goto sw_init_done;
6439 }
6440 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
6441 pf->qp_pile->search_hint = 0;
6442
6443 /* set up vector assignment tracking */
6444 size = sizeof(struct i40e_lump_tracking)
6445 + (sizeof(u16) * pf->hw.func_caps.num_msix_vectors);
6446 pf->irq_pile = kzalloc(size, GFP_KERNEL);
6447 if (!pf->irq_pile) {
6448 kfree(pf->qp_pile);
6449 err = -ENOMEM;
6450 goto sw_init_done;
6451 }
6452 pf->irq_pile->num_entries = pf->hw.func_caps.num_msix_vectors;
6453 pf->irq_pile->search_hint = 0;
6454
6455 mutex_init(&pf->switch_mutex);
6456
6457sw_init_done:
6458 return err;
6459}
6460
6461/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00006462 * i40e_set_ntuple - set the ntuple feature flag and take action
6463 * @pf: board private structure to initialize
6464 * @features: the feature set that the stack is suggesting
6465 *
6466 * returns a bool to indicate if reset needs to happen
6467 **/
6468bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
6469{
6470 bool need_reset = false;
6471
6472 /* Check if Flow Director n-tuple support was enabled or disabled. If
6473 * the state changed, we need to reset.
6474 */
6475 if (features & NETIF_F_NTUPLE) {
6476 /* Enable filters and mark for reset */
6477 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
6478 need_reset = true;
6479 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
6480 } else {
6481 /* turn off filters, mark for reset and clear SW filter list */
6482 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
6483 need_reset = true;
6484 i40e_fdir_filter_exit(pf);
6485 }
6486 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6487 /* if ATR was disabled it can be re-enabled. */
6488 if (!(pf->flags & I40E_FLAG_FD_ATR_ENABLED))
6489 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
6490 }
6491 return need_reset;
6492}
6493
6494/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006495 * i40e_set_features - set the netdev feature flags
6496 * @netdev: ptr to the netdev being adjusted
6497 * @features: the feature set that the stack is suggesting
6498 **/
6499static int i40e_set_features(struct net_device *netdev,
6500 netdev_features_t features)
6501{
6502 struct i40e_netdev_priv *np = netdev_priv(netdev);
6503 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00006504 struct i40e_pf *pf = vsi->back;
6505 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006506
6507 if (features & NETIF_F_HW_VLAN_CTAG_RX)
6508 i40e_vlan_stripping_enable(vsi);
6509 else
6510 i40e_vlan_stripping_disable(vsi);
6511
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00006512 need_reset = i40e_set_ntuple(pf, features);
6513
6514 if (need_reset)
6515 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
6516
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006517 return 0;
6518}
6519
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006520#ifdef CONFIG_I40E_VXLAN
6521/**
6522 * i40e_get_vxlan_port_idx - Lookup a possibly offloaded for Rx UDP port
6523 * @pf: board private structure
6524 * @port: The UDP port to look up
6525 *
6526 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
6527 **/
6528static u8 i40e_get_vxlan_port_idx(struct i40e_pf *pf, __be16 port)
6529{
6530 u8 i;
6531
6532 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
6533 if (pf->vxlan_ports[i] == port)
6534 return i;
6535 }
6536
6537 return i;
6538}
6539
6540/**
6541 * i40e_add_vxlan_port - Get notifications about VXLAN ports that come up
6542 * @netdev: This physical port's netdev
6543 * @sa_family: Socket Family that VXLAN is notifying us about
6544 * @port: New UDP port number that VXLAN started listening to
6545 **/
6546static void i40e_add_vxlan_port(struct net_device *netdev,
6547 sa_family_t sa_family, __be16 port)
6548{
6549 struct i40e_netdev_priv *np = netdev_priv(netdev);
6550 struct i40e_vsi *vsi = np->vsi;
6551 struct i40e_pf *pf = vsi->back;
6552 u8 next_idx;
6553 u8 idx;
6554
6555 if (sa_family == AF_INET6)
6556 return;
6557
6558 idx = i40e_get_vxlan_port_idx(pf, port);
6559
6560 /* Check if port already exists */
6561 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6562 netdev_info(netdev, "Port %d already offloaded\n", ntohs(port));
6563 return;
6564 }
6565
6566 /* Now check if there is space to add the new port */
6567 next_idx = i40e_get_vxlan_port_idx(pf, 0);
6568
6569 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6570 netdev_info(netdev, "Maximum number of UDP ports reached, not adding port %d\n",
6571 ntohs(port));
6572 return;
6573 }
6574
6575 /* New port: add it and mark its index in the bitmap */
6576 pf->vxlan_ports[next_idx] = port;
6577 pf->pending_vxlan_bitmap |= (1 << next_idx);
6578
6579 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
6580}
6581
6582/**
6583 * i40e_del_vxlan_port - Get notifications about VXLAN ports that go away
6584 * @netdev: This physical port's netdev
6585 * @sa_family: Socket Family that VXLAN is notifying us about
6586 * @port: UDP port number that VXLAN stopped listening to
6587 **/
6588static void i40e_del_vxlan_port(struct net_device *netdev,
6589 sa_family_t sa_family, __be16 port)
6590{
6591 struct i40e_netdev_priv *np = netdev_priv(netdev);
6592 struct i40e_vsi *vsi = np->vsi;
6593 struct i40e_pf *pf = vsi->back;
6594 u8 idx;
6595
6596 if (sa_family == AF_INET6)
6597 return;
6598
6599 idx = i40e_get_vxlan_port_idx(pf, port);
6600
6601 /* Check if port already exists */
6602 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6603 /* if port exists, set it to 0 (mark for deletion)
6604 * and make it pending
6605 */
6606 pf->vxlan_ports[idx] = 0;
6607
6608 pf->pending_vxlan_bitmap |= (1 << idx);
6609
6610 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
6611 } else {
6612 netdev_warn(netdev, "Port %d was not found, not deleting\n",
6613 ntohs(port));
6614 }
6615}
6616
6617#endif
Greg Rose4ba0dea2014-03-06 08:59:55 +00006618#ifdef HAVE_FDB_OPS
6619#ifdef USE_CONST_DEV_UC_CHAR
6620static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
6621 struct net_device *dev,
6622 const unsigned char *addr,
6623 u16 flags)
6624#else
6625static int i40e_ndo_fdb_add(struct ndmsg *ndm,
6626 struct net_device *dev,
6627 unsigned char *addr,
6628 u16 flags)
6629#endif
6630{
6631 struct i40e_netdev_priv *np = netdev_priv(dev);
6632 struct i40e_pf *pf = np->vsi->back;
6633 int err = 0;
6634
6635 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
6636 return -EOPNOTSUPP;
6637
6638 /* Hardware does not support aging addresses so if a
6639 * ndm_state is given only allow permanent addresses
6640 */
6641 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
6642 netdev_info(dev, "FDB only supports static addresses\n");
6643 return -EINVAL;
6644 }
6645
6646 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
6647 err = dev_uc_add_excl(dev, addr);
6648 else if (is_multicast_ether_addr(addr))
6649 err = dev_mc_add_excl(dev, addr);
6650 else
6651 err = -EINVAL;
6652
6653 /* Only return duplicate errors if NLM_F_EXCL is set */
6654 if (err == -EEXIST && !(flags & NLM_F_EXCL))
6655 err = 0;
6656
6657 return err;
6658}
6659
6660#ifndef USE_DEFAULT_FDB_DEL_DUMP
6661#ifdef USE_CONST_DEV_UC_CHAR
6662static int i40e_ndo_fdb_del(struct ndmsg *ndm,
6663 struct net_device *dev,
6664 const unsigned char *addr)
6665#else
6666static int i40e_ndo_fdb_del(struct ndmsg *ndm,
6667 struct net_device *dev,
6668 unsigned char *addr)
6669#endif
6670{
6671 struct i40e_netdev_priv *np = netdev_priv(dev);
6672 struct i40e_pf *pf = np->vsi->back;
6673 int err = -EOPNOTSUPP;
6674
6675 if (ndm->ndm_state & NUD_PERMANENT) {
6676 netdev_info(dev, "FDB only supports static addresses\n");
6677 return -EINVAL;
6678 }
6679
6680 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
6681 if (is_unicast_ether_addr(addr))
6682 err = dev_uc_del(dev, addr);
6683 else if (is_multicast_ether_addr(addr))
6684 err = dev_mc_del(dev, addr);
6685 else
6686 err = -EINVAL;
6687 }
6688
6689 return err;
6690}
6691
6692static int i40e_ndo_fdb_dump(struct sk_buff *skb,
6693 struct netlink_callback *cb,
6694 struct net_device *dev,
6695 int idx)
6696{
6697 struct i40e_netdev_priv *np = netdev_priv(dev);
6698 struct i40e_pf *pf = np->vsi->back;
6699
6700 if (pf->flags & I40E_FLAG_SRIOV_ENABLED)
6701 idx = ndo_dflt_fdb_dump(skb, cb, dev, idx);
6702
6703 return idx;
6704}
6705
6706#endif /* USE_DEFAULT_FDB_DEL_DUMP */
6707#endif /* HAVE_FDB_OPS */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006708static const struct net_device_ops i40e_netdev_ops = {
6709 .ndo_open = i40e_open,
6710 .ndo_stop = i40e_close,
6711 .ndo_start_xmit = i40e_lan_xmit_frame,
6712 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
6713 .ndo_set_rx_mode = i40e_set_rx_mode,
6714 .ndo_validate_addr = eth_validate_addr,
6715 .ndo_set_mac_address = i40e_set_mac,
6716 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006717 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006718 .ndo_tx_timeout = i40e_tx_timeout,
6719 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
6720 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
6721#ifdef CONFIG_NET_POLL_CONTROLLER
6722 .ndo_poll_controller = i40e_netpoll,
6723#endif
6724 .ndo_setup_tc = i40e_setup_tc,
6725 .ndo_set_features = i40e_set_features,
6726 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
6727 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
6728 .ndo_set_vf_tx_rate = i40e_ndo_set_vf_bw,
6729 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00006730 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006731#ifdef CONFIG_I40E_VXLAN
6732 .ndo_add_vxlan_port = i40e_add_vxlan_port,
6733 .ndo_del_vxlan_port = i40e_del_vxlan_port,
6734#endif
Greg Rose4ba0dea2014-03-06 08:59:55 +00006735#ifdef HAVE_FDB_OPS
6736 .ndo_fdb_add = i40e_ndo_fdb_add,
6737#ifndef USE_DEFAULT_FDB_DEL_DUMP
6738 .ndo_fdb_del = i40e_ndo_fdb_del,
6739 .ndo_fdb_dump = i40e_ndo_fdb_dump,
6740#endif
6741#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006742};
6743
6744/**
6745 * i40e_config_netdev - Setup the netdev flags
6746 * @vsi: the VSI being configured
6747 *
6748 * Returns 0 on success, negative value on failure
6749 **/
6750static int i40e_config_netdev(struct i40e_vsi *vsi)
6751{
Greg Rose1a103702013-11-28 06:42:39 +00006752 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006753 struct i40e_pf *pf = vsi->back;
6754 struct i40e_hw *hw = &pf->hw;
6755 struct i40e_netdev_priv *np;
6756 struct net_device *netdev;
6757 u8 mac_addr[ETH_ALEN];
6758 int etherdev_size;
6759
6760 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006761 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006762 if (!netdev)
6763 return -ENOMEM;
6764
6765 vsi->netdev = netdev;
6766 np = netdev_priv(netdev);
6767 np->vsi = vsi;
6768
Or Gerlitzd70e9412014-03-18 10:36:45 +02006769 netdev->hw_enc_features |= NETIF_F_IP_CSUM |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006770 NETIF_F_GSO_UDP_TUNNEL |
Or Gerlitzd70e9412014-03-18 10:36:45 +02006771 NETIF_F_TSO;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006772
6773 netdev->features = NETIF_F_SG |
6774 NETIF_F_IP_CSUM |
6775 NETIF_F_SCTP_CSUM |
6776 NETIF_F_HIGHDMA |
6777 NETIF_F_GSO_UDP_TUNNEL |
6778 NETIF_F_HW_VLAN_CTAG_TX |
6779 NETIF_F_HW_VLAN_CTAG_RX |
6780 NETIF_F_HW_VLAN_CTAG_FILTER |
6781 NETIF_F_IPV6_CSUM |
6782 NETIF_F_TSO |
Jesse Brandeburg059dab62014-04-01 09:07:20 +00006783 NETIF_F_TSO_ECN |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006784 NETIF_F_TSO6 |
6785 NETIF_F_RXCSUM |
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00006786 NETIF_F_NTUPLE |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006787 NETIF_F_RXHASH |
6788 0;
6789
6790 /* copy netdev features into list of user selectable features */
6791 netdev->hw_features |= netdev->features;
6792
6793 if (vsi->type == I40E_VSI_MAIN) {
6794 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
6795 memcpy(mac_addr, hw->mac.perm_addr, ETH_ALEN);
6796 } else {
6797 /* relate the VSI_VMDQ name to the VSI_MAIN name */
6798 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
6799 pf->vsi[pf->lan_vsi]->netdev->name);
6800 random_ether_addr(mac_addr);
6801 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
6802 }
Greg Rose1a103702013-11-28 06:42:39 +00006803 i40e_add_filter(vsi, brdcast, I40E_VLAN_ANY, false, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006804
6805 memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
6806 memcpy(netdev->perm_addr, mac_addr, ETH_ALEN);
6807 /* vlan gets same features (except vlan offload)
6808 * after any tweaks for specific VSI types
6809 */
6810 netdev->vlan_features = netdev->features & ~(NETIF_F_HW_VLAN_CTAG_TX |
6811 NETIF_F_HW_VLAN_CTAG_RX |
6812 NETIF_F_HW_VLAN_CTAG_FILTER);
6813 netdev->priv_flags |= IFF_UNICAST_FLT;
6814 netdev->priv_flags |= IFF_SUPP_NOFCS;
6815 /* Setup netdev TC information */
6816 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
6817
6818 netdev->netdev_ops = &i40e_netdev_ops;
6819 netdev->watchdog_timeo = 5 * HZ;
6820 i40e_set_ethtool_ops(netdev);
6821
6822 return 0;
6823}
6824
6825/**
6826 * i40e_vsi_delete - Delete a VSI from the switch
6827 * @vsi: the VSI being removed
6828 *
6829 * Returns 0 on success, negative value on failure
6830 **/
6831static void i40e_vsi_delete(struct i40e_vsi *vsi)
6832{
6833 /* remove default VSI is not allowed */
6834 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
6835 return;
6836
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006837 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
6838 return;
6839}
6840
6841/**
6842 * i40e_add_vsi - Add a VSI to the switch
6843 * @vsi: the VSI being configured
6844 *
6845 * This initializes a VSI context depending on the VSI type to be added and
6846 * passes it down to the add_vsi aq command.
6847 **/
6848static int i40e_add_vsi(struct i40e_vsi *vsi)
6849{
6850 int ret = -ENODEV;
6851 struct i40e_mac_filter *f, *ftmp;
6852 struct i40e_pf *pf = vsi->back;
6853 struct i40e_hw *hw = &pf->hw;
6854 struct i40e_vsi_context ctxt;
6855 u8 enabled_tc = 0x1; /* TC0 enabled */
6856 int f_count = 0;
6857
6858 memset(&ctxt, 0, sizeof(ctxt));
6859 switch (vsi->type) {
6860 case I40E_VSI_MAIN:
6861 /* The PF's main VSI is already setup as part of the
6862 * device initialization, so we'll not bother with
6863 * the add_vsi call, but we will retrieve the current
6864 * VSI context.
6865 */
6866 ctxt.seid = pf->main_vsi_seid;
6867 ctxt.pf_num = pf->hw.pf_id;
6868 ctxt.vf_num = 0;
6869 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6870 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6871 if (ret) {
6872 dev_info(&pf->pdev->dev,
6873 "couldn't get pf vsi config, err %d, aq_err %d\n",
6874 ret, pf->hw.aq.asq_last_status);
6875 return -ENOENT;
6876 }
6877 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
6878 vsi->info.valid_sections = 0;
6879
6880 vsi->seid = ctxt.seid;
6881 vsi->id = ctxt.vsi_number;
6882
6883 enabled_tc = i40e_pf_get_tc_map(pf);
6884
6885 /* MFP mode setup queue map and update VSI */
6886 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
6887 memset(&ctxt, 0, sizeof(ctxt));
6888 ctxt.seid = pf->main_vsi_seid;
6889 ctxt.pf_num = pf->hw.pf_id;
6890 ctxt.vf_num = 0;
6891 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
6892 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
6893 if (ret) {
6894 dev_info(&pf->pdev->dev,
6895 "update vsi failed, aq_err=%d\n",
6896 pf->hw.aq.asq_last_status);
6897 ret = -ENOENT;
6898 goto err;
6899 }
6900 /* update the local VSI info queue map */
6901 i40e_vsi_update_queue_map(vsi, &ctxt);
6902 vsi->info.valid_sections = 0;
6903 } else {
6904 /* Default/Main VSI is only enabled for TC0
6905 * reconfigure it to enable all TCs that are
6906 * available on the port in SFP mode.
6907 */
6908 ret = i40e_vsi_config_tc(vsi, enabled_tc);
6909 if (ret) {
6910 dev_info(&pf->pdev->dev,
6911 "failed to configure TCs for main VSI tc_map 0x%08x, err %d, aq_err %d\n",
6912 enabled_tc, ret,
6913 pf->hw.aq.asq_last_status);
6914 ret = -ENOENT;
6915 }
6916 }
6917 break;
6918
6919 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006920 ctxt.pf_num = hw->pf_id;
6921 ctxt.vf_num = 0;
6922 ctxt.uplink_seid = vsi->uplink_seid;
6923 ctxt.connection_type = 0x1; /* regular data port */
6924 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006925 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006926 break;
6927
6928 case I40E_VSI_VMDQ2:
6929 ctxt.pf_num = hw->pf_id;
6930 ctxt.vf_num = 0;
6931 ctxt.uplink_seid = vsi->uplink_seid;
6932 ctxt.connection_type = 0x1; /* regular data port */
6933 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6934
6935 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6936
6937 /* This VSI is connected to VEB so the switch_id
6938 * should be set to zero by default.
6939 */
6940 ctxt.info.switch_id = 0;
6941 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
6942 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6943
6944 /* Setup the VSI tx/rx queue map for TC0 only for now */
6945 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
6946 break;
6947
6948 case I40E_VSI_SRIOV:
6949 ctxt.pf_num = hw->pf_id;
6950 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
6951 ctxt.uplink_seid = vsi->uplink_seid;
6952 ctxt.connection_type = 0x1; /* regular data port */
6953 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
6954
6955 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6956
6957 /* This VSI is connected to VEB so the switch_id
6958 * should be set to zero by default.
6959 */
6960 ctxt.info.switch_id = cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6961
6962 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
6963 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
6964 /* Setup the VSI tx/rx queue map for TC0 only for now */
6965 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
6966 break;
6967
6968 default:
6969 return -ENODEV;
6970 }
6971
6972 if (vsi->type != I40E_VSI_MAIN) {
6973 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6974 if (ret) {
6975 dev_info(&vsi->back->pdev->dev,
6976 "add vsi failed, aq_err=%d\n",
6977 vsi->back->hw.aq.asq_last_status);
6978 ret = -ENOENT;
6979 goto err;
6980 }
6981 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
6982 vsi->info.valid_sections = 0;
6983 vsi->seid = ctxt.seid;
6984 vsi->id = ctxt.vsi_number;
6985 }
6986
6987 /* If macvlan filters already exist, force them to get loaded */
6988 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
6989 f->changed = true;
6990 f_count++;
6991 }
6992 if (f_count) {
6993 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
6994 pf->flags |= I40E_FLAG_FILTER_SYNC;
6995 }
6996
6997 /* Update VSI BW information */
6998 ret = i40e_vsi_get_bw_info(vsi);
6999 if (ret) {
7000 dev_info(&pf->pdev->dev,
7001 "couldn't get vsi bw info, err %d, aq_err %d\n",
7002 ret, pf->hw.aq.asq_last_status);
7003 /* VSI is already added so not tearing that up */
7004 ret = 0;
7005 }
7006
7007err:
7008 return ret;
7009}
7010
7011/**
7012 * i40e_vsi_release - Delete a VSI and free its resources
7013 * @vsi: the VSI being removed
7014 *
7015 * Returns 0 on success or < 0 on error
7016 **/
7017int i40e_vsi_release(struct i40e_vsi *vsi)
7018{
7019 struct i40e_mac_filter *f, *ftmp;
7020 struct i40e_veb *veb = NULL;
7021 struct i40e_pf *pf;
7022 u16 uplink_seid;
7023 int i, n;
7024
7025 pf = vsi->back;
7026
7027 /* release of a VEB-owner or last VSI is not allowed */
7028 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
7029 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
7030 vsi->seid, vsi->uplink_seid);
7031 return -ENODEV;
7032 }
7033 if (vsi == pf->vsi[pf->lan_vsi] &&
7034 !test_bit(__I40E_DOWN, &pf->state)) {
7035 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
7036 return -ENODEV;
7037 }
7038
7039 uplink_seid = vsi->uplink_seid;
7040 if (vsi->type != I40E_VSI_SRIOV) {
7041 if (vsi->netdev_registered) {
7042 vsi->netdev_registered = false;
7043 if (vsi->netdev) {
7044 /* results in a call to i40e_close() */
7045 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007046 }
7047 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00007048 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007049 }
7050 i40e_vsi_disable_irq(vsi);
7051 }
7052
7053 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
7054 i40e_del_filter(vsi, f->macaddr, f->vlan,
7055 f->is_vf, f->is_netdev);
7056 i40e_sync_vsi_filters(vsi);
7057
7058 i40e_vsi_delete(vsi);
7059 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00007060 if (vsi->netdev) {
7061 free_netdev(vsi->netdev);
7062 vsi->netdev = NULL;
7063 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007064 i40e_vsi_clear_rings(vsi);
7065 i40e_vsi_clear(vsi);
7066
7067 /* If this was the last thing on the VEB, except for the
7068 * controlling VSI, remove the VEB, which puts the controlling
7069 * VSI onto the next level down in the switch.
7070 *
7071 * Well, okay, there's one more exception here: don't remove
7072 * the orphan VEBs yet. We'll wait for an explicit remove request
7073 * from up the network stack.
7074 */
7075 for (n = 0, i = 0; i < pf->hw.func_caps.num_vsis; i++) {
7076 if (pf->vsi[i] &&
7077 pf->vsi[i]->uplink_seid == uplink_seid &&
7078 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
7079 n++; /* count the VSIs */
7080 }
7081 }
7082 for (i = 0; i < I40E_MAX_VEB; i++) {
7083 if (!pf->veb[i])
7084 continue;
7085 if (pf->veb[i]->uplink_seid == uplink_seid)
7086 n++; /* count the VEBs */
7087 if (pf->veb[i]->seid == uplink_seid)
7088 veb = pf->veb[i];
7089 }
7090 if (n == 0 && veb && veb->uplink_seid != 0)
7091 i40e_veb_release(veb);
7092
7093 return 0;
7094}
7095
7096/**
7097 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
7098 * @vsi: ptr to the VSI
7099 *
7100 * This should only be called after i40e_vsi_mem_alloc() which allocates the
7101 * corresponding SW VSI structure and initializes num_queue_pairs for the
7102 * newly allocated VSI.
7103 *
7104 * Returns 0 on success or negative on failure
7105 **/
7106static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
7107{
7108 int ret = -ENOENT;
7109 struct i40e_pf *pf = vsi->back;
7110
Alexander Duyck493fb302013-09-28 07:01:44 +00007111 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007112 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
7113 vsi->seid);
7114 return -EEXIST;
7115 }
7116
7117 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00007118 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007119 vsi->seid, vsi->base_vector);
7120 return -EEXIST;
7121 }
7122
Greg Rose90e04072014-03-06 08:59:57 +00007123 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007124 if (ret) {
7125 dev_info(&pf->pdev->dev,
7126 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
7127 vsi->num_q_vectors, vsi->seid, ret);
7128 vsi->num_q_vectors = 0;
7129 goto vector_setup_out;
7130 }
7131
Shannon Nelson958a3e32013-09-28 07:13:28 +00007132 if (vsi->num_q_vectors)
7133 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
7134 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007135 if (vsi->base_vector < 0) {
7136 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00007137 "failed to get queue tracking for VSI %d, err=%d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007138 vsi->seid, vsi->base_vector);
7139 i40e_vsi_free_q_vectors(vsi);
7140 ret = -ENOENT;
7141 goto vector_setup_out;
7142 }
7143
7144vector_setup_out:
7145 return ret;
7146}
7147
7148/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007149 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
7150 * @vsi: pointer to the vsi.
7151 *
7152 * This re-allocates a vsi's queue resources.
7153 *
7154 * Returns pointer to the successfully allocated and configured VSI sw struct
7155 * on success, otherwise returns NULL on failure.
7156 **/
7157static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
7158{
7159 struct i40e_pf *pf = vsi->back;
7160 u8 enabled_tc;
7161 int ret;
7162
7163 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7164 i40e_vsi_clear_rings(vsi);
7165
7166 i40e_vsi_free_arrays(vsi, false);
7167 i40e_set_num_rings_in_vsi(vsi);
7168 ret = i40e_vsi_alloc_arrays(vsi, false);
7169 if (ret)
7170 goto err_vsi;
7171
7172 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
7173 if (ret < 0) {
7174 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
7175 vsi->seid, ret);
7176 goto err_vsi;
7177 }
7178 vsi->base_queue = ret;
7179
7180 /* Update the FW view of the VSI. Force a reset of TC and queue
7181 * layout configurations.
7182 */
7183 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
7184 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
7185 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
7186 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
7187
7188 /* assign it some queues */
7189 ret = i40e_alloc_rings(vsi);
7190 if (ret)
7191 goto err_rings;
7192
7193 /* map all of the rings to the q_vectors */
7194 i40e_vsi_map_rings_to_vectors(vsi);
7195 return vsi;
7196
7197err_rings:
7198 i40e_vsi_free_q_vectors(vsi);
7199 if (vsi->netdev_registered) {
7200 vsi->netdev_registered = false;
7201 unregister_netdev(vsi->netdev);
7202 free_netdev(vsi->netdev);
7203 vsi->netdev = NULL;
7204 }
7205 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
7206err_vsi:
7207 i40e_vsi_clear(vsi);
7208 return NULL;
7209}
7210
7211/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007212 * i40e_vsi_setup - Set up a VSI by a given type
7213 * @pf: board private structure
7214 * @type: VSI type
7215 * @uplink_seid: the switch element to link to
7216 * @param1: usage depends upon VSI type. For VF types, indicates VF id
7217 *
7218 * This allocates the sw VSI structure and its queue resources, then add a VSI
7219 * to the identified VEB.
7220 *
7221 * Returns pointer to the successfully allocated and configure VSI sw struct on
7222 * success, otherwise returns NULL on failure.
7223 **/
7224struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
7225 u16 uplink_seid, u32 param1)
7226{
7227 struct i40e_vsi *vsi = NULL;
7228 struct i40e_veb *veb = NULL;
7229 int ret, i;
7230 int v_idx;
7231
7232 /* The requested uplink_seid must be either
7233 * - the PF's port seid
7234 * no VEB is needed because this is the PF
7235 * or this is a Flow Director special case VSI
7236 * - seid of an existing VEB
7237 * - seid of a VSI that owns an existing VEB
7238 * - seid of a VSI that doesn't own a VEB
7239 * a new VEB is created and the VSI becomes the owner
7240 * - seid of the PF VSI, which is what creates the first VEB
7241 * this is a special case of the previous
7242 *
7243 * Find which uplink_seid we were given and create a new VEB if needed
7244 */
7245 for (i = 0; i < I40E_MAX_VEB; i++) {
7246 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
7247 veb = pf->veb[i];
7248 break;
7249 }
7250 }
7251
7252 if (!veb && uplink_seid != pf->mac_seid) {
7253
7254 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
7255 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
7256 vsi = pf->vsi[i];
7257 break;
7258 }
7259 }
7260 if (!vsi) {
7261 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
7262 uplink_seid);
7263 return NULL;
7264 }
7265
7266 if (vsi->uplink_seid == pf->mac_seid)
7267 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
7268 vsi->tc_config.enabled_tc);
7269 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
7270 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
7271 vsi->tc_config.enabled_tc);
7272
7273 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
7274 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
7275 veb = pf->veb[i];
7276 }
7277 if (!veb) {
7278 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
7279 return NULL;
7280 }
7281
7282 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
7283 uplink_seid = veb->seid;
7284 }
7285
7286 /* get vsi sw struct */
7287 v_idx = i40e_vsi_mem_alloc(pf, type);
7288 if (v_idx < 0)
7289 goto err_alloc;
7290 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007291 if (!vsi)
7292 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007293 vsi->type = type;
7294 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
7295
7296 if (type == I40E_VSI_MAIN)
7297 pf->lan_vsi = v_idx;
7298 else if (type == I40E_VSI_SRIOV)
7299 vsi->vf_id = param1;
7300 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007301 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
7302 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007303 if (ret < 0) {
7304 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
7305 vsi->seid, ret);
7306 goto err_vsi;
7307 }
7308 vsi->base_queue = ret;
7309
7310 /* get a VSI from the hardware */
7311 vsi->uplink_seid = uplink_seid;
7312 ret = i40e_add_vsi(vsi);
7313 if (ret)
7314 goto err_vsi;
7315
7316 switch (vsi->type) {
7317 /* setup the netdev if needed */
7318 case I40E_VSI_MAIN:
7319 case I40E_VSI_VMDQ2:
7320 ret = i40e_config_netdev(vsi);
7321 if (ret)
7322 goto err_netdev;
7323 ret = register_netdev(vsi->netdev);
7324 if (ret)
7325 goto err_netdev;
7326 vsi->netdev_registered = true;
7327 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007328#ifdef CONFIG_I40E_DCB
7329 /* Setup DCB netlink interface */
7330 i40e_dcbnl_setup(vsi);
7331#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007332 /* fall through */
7333
7334 case I40E_VSI_FDIR:
7335 /* set up vectors and rings if needed */
7336 ret = i40e_vsi_setup_vectors(vsi);
7337 if (ret)
7338 goto err_msix;
7339
7340 ret = i40e_alloc_rings(vsi);
7341 if (ret)
7342 goto err_rings;
7343
7344 /* map all of the rings to the q_vectors */
7345 i40e_vsi_map_rings_to_vectors(vsi);
7346
7347 i40e_vsi_reset_stats(vsi);
7348 break;
7349
7350 default:
7351 /* no netdev or rings for the other VSI types */
7352 break;
7353 }
7354
7355 return vsi;
7356
7357err_rings:
7358 i40e_vsi_free_q_vectors(vsi);
7359err_msix:
7360 if (vsi->netdev_registered) {
7361 vsi->netdev_registered = false;
7362 unregister_netdev(vsi->netdev);
7363 free_netdev(vsi->netdev);
7364 vsi->netdev = NULL;
7365 }
7366err_netdev:
7367 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
7368err_vsi:
7369 i40e_vsi_clear(vsi);
7370err_alloc:
7371 return NULL;
7372}
7373
7374/**
7375 * i40e_veb_get_bw_info - Query VEB BW information
7376 * @veb: the veb to query
7377 *
7378 * Query the Tx scheduler BW configuration data for given VEB
7379 **/
7380static int i40e_veb_get_bw_info(struct i40e_veb *veb)
7381{
7382 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
7383 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
7384 struct i40e_pf *pf = veb->pf;
7385 struct i40e_hw *hw = &pf->hw;
7386 u32 tc_bw_max;
7387 int ret = 0;
7388 int i;
7389
7390 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
7391 &bw_data, NULL);
7392 if (ret) {
7393 dev_info(&pf->pdev->dev,
7394 "query veb bw config failed, aq_err=%d\n",
7395 hw->aq.asq_last_status);
7396 goto out;
7397 }
7398
7399 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
7400 &ets_data, NULL);
7401 if (ret) {
7402 dev_info(&pf->pdev->dev,
7403 "query veb bw ets config failed, aq_err=%d\n",
7404 hw->aq.asq_last_status);
7405 goto out;
7406 }
7407
7408 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
7409 veb->bw_max_quanta = ets_data.tc_bw_max;
7410 veb->is_abs_credits = bw_data.absolute_credits_enable;
7411 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
7412 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
7413 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7414 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
7415 veb->bw_tc_limit_credits[i] =
7416 le16_to_cpu(bw_data.tc_bw_limits[i]);
7417 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
7418 }
7419
7420out:
7421 return ret;
7422}
7423
7424/**
7425 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
7426 * @pf: board private structure
7427 *
7428 * On error: returns error code (negative)
7429 * On success: returns vsi index in PF (positive)
7430 **/
7431static int i40e_veb_mem_alloc(struct i40e_pf *pf)
7432{
7433 int ret = -ENOENT;
7434 struct i40e_veb *veb;
7435 int i;
7436
7437 /* Need to protect the allocation of switch elements at the PF level */
7438 mutex_lock(&pf->switch_mutex);
7439
7440 /* VEB list may be fragmented if VEB creation/destruction has
7441 * been happening. We can afford to do a quick scan to look
7442 * for any free slots in the list.
7443 *
7444 * find next empty veb slot, looping back around if necessary
7445 */
7446 i = 0;
7447 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
7448 i++;
7449 if (i >= I40E_MAX_VEB) {
7450 ret = -ENOMEM;
7451 goto err_alloc_veb; /* out of VEB slots! */
7452 }
7453
7454 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
7455 if (!veb) {
7456 ret = -ENOMEM;
7457 goto err_alloc_veb;
7458 }
7459 veb->pf = pf;
7460 veb->idx = i;
7461 veb->enabled_tc = 1;
7462
7463 pf->veb[i] = veb;
7464 ret = i;
7465err_alloc_veb:
7466 mutex_unlock(&pf->switch_mutex);
7467 return ret;
7468}
7469
7470/**
7471 * i40e_switch_branch_release - Delete a branch of the switch tree
7472 * @branch: where to start deleting
7473 *
7474 * This uses recursion to find the tips of the branch to be
7475 * removed, deleting until we get back to and can delete this VEB.
7476 **/
7477static void i40e_switch_branch_release(struct i40e_veb *branch)
7478{
7479 struct i40e_pf *pf = branch->pf;
7480 u16 branch_seid = branch->seid;
7481 u16 veb_idx = branch->idx;
7482 int i;
7483
7484 /* release any VEBs on this VEB - RECURSION */
7485 for (i = 0; i < I40E_MAX_VEB; i++) {
7486 if (!pf->veb[i])
7487 continue;
7488 if (pf->veb[i]->uplink_seid == branch->seid)
7489 i40e_switch_branch_release(pf->veb[i]);
7490 }
7491
7492 /* Release the VSIs on this VEB, but not the owner VSI.
7493 *
7494 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
7495 * the VEB itself, so don't use (*branch) after this loop.
7496 */
7497 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
7498 if (!pf->vsi[i])
7499 continue;
7500 if (pf->vsi[i]->uplink_seid == branch_seid &&
7501 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
7502 i40e_vsi_release(pf->vsi[i]);
7503 }
7504 }
7505
7506 /* There's one corner case where the VEB might not have been
7507 * removed, so double check it here and remove it if needed.
7508 * This case happens if the veb was created from the debugfs
7509 * commands and no VSIs were added to it.
7510 */
7511 if (pf->veb[veb_idx])
7512 i40e_veb_release(pf->veb[veb_idx]);
7513}
7514
7515/**
7516 * i40e_veb_clear - remove veb struct
7517 * @veb: the veb to remove
7518 **/
7519static void i40e_veb_clear(struct i40e_veb *veb)
7520{
7521 if (!veb)
7522 return;
7523
7524 if (veb->pf) {
7525 struct i40e_pf *pf = veb->pf;
7526
7527 mutex_lock(&pf->switch_mutex);
7528 if (pf->veb[veb->idx] == veb)
7529 pf->veb[veb->idx] = NULL;
7530 mutex_unlock(&pf->switch_mutex);
7531 }
7532
7533 kfree(veb);
7534}
7535
7536/**
7537 * i40e_veb_release - Delete a VEB and free its resources
7538 * @veb: the VEB being removed
7539 **/
7540void i40e_veb_release(struct i40e_veb *veb)
7541{
7542 struct i40e_vsi *vsi = NULL;
7543 struct i40e_pf *pf;
7544 int i, n = 0;
7545
7546 pf = veb->pf;
7547
7548 /* find the remaining VSI and check for extras */
7549 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
7550 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
7551 n++;
7552 vsi = pf->vsi[i];
7553 }
7554 }
7555 if (n != 1) {
7556 dev_info(&pf->pdev->dev,
7557 "can't remove VEB %d with %d VSIs left\n",
7558 veb->seid, n);
7559 return;
7560 }
7561
7562 /* move the remaining VSI to uplink veb */
7563 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
7564 if (veb->uplink_seid) {
7565 vsi->uplink_seid = veb->uplink_seid;
7566 if (veb->uplink_seid == pf->mac_seid)
7567 vsi->veb_idx = I40E_NO_VEB;
7568 else
7569 vsi->veb_idx = veb->veb_idx;
7570 } else {
7571 /* floating VEB */
7572 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
7573 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
7574 }
7575
7576 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
7577 i40e_veb_clear(veb);
7578
7579 return;
7580}
7581
7582/**
7583 * i40e_add_veb - create the VEB in the switch
7584 * @veb: the VEB to be instantiated
7585 * @vsi: the controlling VSI
7586 **/
7587static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
7588{
Greg Rose56747262013-11-28 06:39:37 +00007589 bool is_default = false;
Kevin Scotte1c51b952013-11-20 10:02:51 +00007590 bool is_cloud = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007591 int ret;
7592
7593 /* get a VEB from the hardware */
7594 ret = i40e_aq_add_veb(&veb->pf->hw, veb->uplink_seid, vsi->seid,
Kevin Scotte1c51b952013-11-20 10:02:51 +00007595 veb->enabled_tc, is_default,
7596 is_cloud, &veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007597 if (ret) {
7598 dev_info(&veb->pf->pdev->dev,
7599 "couldn't add VEB, err %d, aq_err %d\n",
7600 ret, veb->pf->hw.aq.asq_last_status);
7601 return -EPERM;
7602 }
7603
7604 /* get statistics counter */
7605 ret = i40e_aq_get_veb_parameters(&veb->pf->hw, veb->seid, NULL, NULL,
7606 &veb->stats_idx, NULL, NULL, NULL);
7607 if (ret) {
7608 dev_info(&veb->pf->pdev->dev,
7609 "couldn't get VEB statistics idx, err %d, aq_err %d\n",
7610 ret, veb->pf->hw.aq.asq_last_status);
7611 return -EPERM;
7612 }
7613 ret = i40e_veb_get_bw_info(veb);
7614 if (ret) {
7615 dev_info(&veb->pf->pdev->dev,
7616 "couldn't get VEB bw info, err %d, aq_err %d\n",
7617 ret, veb->pf->hw.aq.asq_last_status);
7618 i40e_aq_delete_element(&veb->pf->hw, veb->seid, NULL);
7619 return -ENOENT;
7620 }
7621
7622 vsi->uplink_seid = veb->seid;
7623 vsi->veb_idx = veb->idx;
7624 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
7625
7626 return 0;
7627}
7628
7629/**
7630 * i40e_veb_setup - Set up a VEB
7631 * @pf: board private structure
7632 * @flags: VEB setup flags
7633 * @uplink_seid: the switch element to link to
7634 * @vsi_seid: the initial VSI seid
7635 * @enabled_tc: Enabled TC bit-map
7636 *
7637 * This allocates the sw VEB structure and links it into the switch
7638 * It is possible and legal for this to be a duplicate of an already
7639 * existing VEB. It is also possible for both uplink and vsi seids
7640 * to be zero, in order to create a floating VEB.
7641 *
7642 * Returns pointer to the successfully allocated VEB sw struct on
7643 * success, otherwise returns NULL on failure.
7644 **/
7645struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
7646 u16 uplink_seid, u16 vsi_seid,
7647 u8 enabled_tc)
7648{
7649 struct i40e_veb *veb, *uplink_veb = NULL;
7650 int vsi_idx, veb_idx;
7651 int ret;
7652
7653 /* if one seid is 0, the other must be 0 to create a floating relay */
7654 if ((uplink_seid == 0 || vsi_seid == 0) &&
7655 (uplink_seid + vsi_seid != 0)) {
7656 dev_info(&pf->pdev->dev,
7657 "one, not both seid's are 0: uplink=%d vsi=%d\n",
7658 uplink_seid, vsi_seid);
7659 return NULL;
7660 }
7661
7662 /* make sure there is such a vsi and uplink */
7663 for (vsi_idx = 0; vsi_idx < pf->hw.func_caps.num_vsis; vsi_idx++)
7664 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
7665 break;
7666 if (vsi_idx >= pf->hw.func_caps.num_vsis && vsi_seid != 0) {
7667 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
7668 vsi_seid);
7669 return NULL;
7670 }
7671
7672 if (uplink_seid && uplink_seid != pf->mac_seid) {
7673 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
7674 if (pf->veb[veb_idx] &&
7675 pf->veb[veb_idx]->seid == uplink_seid) {
7676 uplink_veb = pf->veb[veb_idx];
7677 break;
7678 }
7679 }
7680 if (!uplink_veb) {
7681 dev_info(&pf->pdev->dev,
7682 "uplink seid %d not found\n", uplink_seid);
7683 return NULL;
7684 }
7685 }
7686
7687 /* get veb sw struct */
7688 veb_idx = i40e_veb_mem_alloc(pf);
7689 if (veb_idx < 0)
7690 goto err_alloc;
7691 veb = pf->veb[veb_idx];
7692 veb->flags = flags;
7693 veb->uplink_seid = uplink_seid;
7694 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
7695 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
7696
7697 /* create the VEB in the switch */
7698 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
7699 if (ret)
7700 goto err_veb;
7701
7702 return veb;
7703
7704err_veb:
7705 i40e_veb_clear(veb);
7706err_alloc:
7707 return NULL;
7708}
7709
7710/**
7711 * i40e_setup_pf_switch_element - set pf vars based on switch type
7712 * @pf: board private structure
7713 * @ele: element we are building info from
7714 * @num_reported: total number of elements
7715 * @printconfig: should we print the contents
7716 *
7717 * helper function to assist in extracting a few useful SEID values.
7718 **/
7719static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
7720 struct i40e_aqc_switch_config_element_resp *ele,
7721 u16 num_reported, bool printconfig)
7722{
7723 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
7724 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
7725 u8 element_type = ele->element_type;
7726 u16 seid = le16_to_cpu(ele->seid);
7727
7728 if (printconfig)
7729 dev_info(&pf->pdev->dev,
7730 "type=%d seid=%d uplink=%d downlink=%d\n",
7731 element_type, seid, uplink_seid, downlink_seid);
7732
7733 switch (element_type) {
7734 case I40E_SWITCH_ELEMENT_TYPE_MAC:
7735 pf->mac_seid = seid;
7736 break;
7737 case I40E_SWITCH_ELEMENT_TYPE_VEB:
7738 /* Main VEB? */
7739 if (uplink_seid != pf->mac_seid)
7740 break;
7741 if (pf->lan_veb == I40E_NO_VEB) {
7742 int v;
7743
7744 /* find existing or else empty VEB */
7745 for (v = 0; v < I40E_MAX_VEB; v++) {
7746 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
7747 pf->lan_veb = v;
7748 break;
7749 }
7750 }
7751 if (pf->lan_veb == I40E_NO_VEB) {
7752 v = i40e_veb_mem_alloc(pf);
7753 if (v < 0)
7754 break;
7755 pf->lan_veb = v;
7756 }
7757 }
7758
7759 pf->veb[pf->lan_veb]->seid = seid;
7760 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
7761 pf->veb[pf->lan_veb]->pf = pf;
7762 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
7763 break;
7764 case I40E_SWITCH_ELEMENT_TYPE_VSI:
7765 if (num_reported != 1)
7766 break;
7767 /* This is immediately after a reset so we can assume this is
7768 * the PF's VSI
7769 */
7770 pf->mac_seid = uplink_seid;
7771 pf->pf_seid = downlink_seid;
7772 pf->main_vsi_seid = seid;
7773 if (printconfig)
7774 dev_info(&pf->pdev->dev,
7775 "pf_seid=%d main_vsi_seid=%d\n",
7776 pf->pf_seid, pf->main_vsi_seid);
7777 break;
7778 case I40E_SWITCH_ELEMENT_TYPE_PF:
7779 case I40E_SWITCH_ELEMENT_TYPE_VF:
7780 case I40E_SWITCH_ELEMENT_TYPE_EMP:
7781 case I40E_SWITCH_ELEMENT_TYPE_BMC:
7782 case I40E_SWITCH_ELEMENT_TYPE_PE:
7783 case I40E_SWITCH_ELEMENT_TYPE_PA:
7784 /* ignore these for now */
7785 break;
7786 default:
7787 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
7788 element_type, seid);
7789 break;
7790 }
7791}
7792
7793/**
7794 * i40e_fetch_switch_configuration - Get switch config from firmware
7795 * @pf: board private structure
7796 * @printconfig: should we print the contents
7797 *
7798 * Get the current switch configuration from the device and
7799 * extract a few useful SEID values.
7800 **/
7801int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
7802{
7803 struct i40e_aqc_get_switch_config_resp *sw_config;
7804 u16 next_seid = 0;
7805 int ret = 0;
7806 u8 *aq_buf;
7807 int i;
7808
7809 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
7810 if (!aq_buf)
7811 return -ENOMEM;
7812
7813 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
7814 do {
7815 u16 num_reported, num_total;
7816
7817 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
7818 I40E_AQ_LARGE_BUF,
7819 &next_seid, NULL);
7820 if (ret) {
7821 dev_info(&pf->pdev->dev,
7822 "get switch config failed %d aq_err=%x\n",
7823 ret, pf->hw.aq.asq_last_status);
7824 kfree(aq_buf);
7825 return -ENOENT;
7826 }
7827
7828 num_reported = le16_to_cpu(sw_config->header.num_reported);
7829 num_total = le16_to_cpu(sw_config->header.num_total);
7830
7831 if (printconfig)
7832 dev_info(&pf->pdev->dev,
7833 "header: %d reported %d total\n",
7834 num_reported, num_total);
7835
7836 if (num_reported) {
7837 int sz = sizeof(*sw_config) * num_reported;
7838
7839 kfree(pf->sw_config);
7840 pf->sw_config = kzalloc(sz, GFP_KERNEL);
7841 if (pf->sw_config)
7842 memcpy(pf->sw_config, sw_config, sz);
7843 }
7844
7845 for (i = 0; i < num_reported; i++) {
7846 struct i40e_aqc_switch_config_element_resp *ele =
7847 &sw_config->element[i];
7848
7849 i40e_setup_pf_switch_element(pf, ele, num_reported,
7850 printconfig);
7851 }
7852 } while (next_seid != 0);
7853
7854 kfree(aq_buf);
7855 return ret;
7856}
7857
7858/**
7859 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
7860 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007861 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007862 *
7863 * Returns 0 on success, negative value on failure
7864 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007865static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007866{
Jesse Brandeburg895106a2013-11-26 10:49:16 +00007867 u32 rxfc = 0, txfc = 0, rxfc_reg;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007868 int ret;
7869
7870 /* find out what's out there already */
7871 ret = i40e_fetch_switch_configuration(pf, false);
7872 if (ret) {
7873 dev_info(&pf->pdev->dev,
7874 "couldn't fetch switch config, err %d, aq_err %d\n",
7875 ret, pf->hw.aq.asq_last_status);
7876 return ret;
7877 }
7878 i40e_pf_reset_stats(pf);
7879
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007880 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007881 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007882 struct i40e_vsi *vsi = NULL;
7883 u16 uplink_seid;
7884
7885 /* Set up the PF VSI associated with the PF's main VSI
7886 * that is already in the HW switch
7887 */
7888 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
7889 uplink_seid = pf->veb[pf->lan_veb]->seid;
7890 else
7891 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007892 if (pf->lan_vsi == I40E_NO_VSI)
7893 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
7894 else if (reinit)
7895 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007896 if (!vsi) {
7897 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
7898 i40e_fdir_teardown(pf);
7899 return -EAGAIN;
7900 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007901 } else {
7902 /* force a reset of TC and queue layout configurations */
7903 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
7904 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
7905 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
7906 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
7907 }
7908 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
7909
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007910 i40e_fdir_sb_setup(pf);
7911
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007912 /* Setup static PF queue filter control settings */
7913 ret = i40e_setup_pf_filter_control(pf);
7914 if (ret) {
7915 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
7916 ret);
7917 /* Failure here should not stop continuing other steps */
7918 }
7919
7920 /* enable RSS in the HW, even for only one queue, as the stack can use
7921 * the hash
7922 */
7923 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
7924 i40e_config_rss(pf);
7925
7926 /* fill in link information and enable LSE reporting */
7927 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
7928 i40e_link_event(pf);
7929
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007930 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007931 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
7932 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007933 /* requested_mode is set in probe or by ethtool */
7934 if (!pf->fc_autoneg_status)
7935 goto no_autoneg;
7936
7937 if ((pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX) &&
7938 (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007939 pf->hw.fc.current_mode = I40E_FC_FULL;
7940 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX)
7941 pf->hw.fc.current_mode = I40E_FC_TX_PAUSE;
7942 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX)
7943 pf->hw.fc.current_mode = I40E_FC_RX_PAUSE;
7944 else
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007945 pf->hw.fc.current_mode = I40E_FC_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007946
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007947 /* sync the flow control settings with the auto-neg values */
7948 switch (pf->hw.fc.current_mode) {
7949 case I40E_FC_FULL:
7950 txfc = 1;
7951 rxfc = 1;
7952 break;
7953 case I40E_FC_TX_PAUSE:
7954 txfc = 1;
7955 rxfc = 0;
7956 break;
7957 case I40E_FC_RX_PAUSE:
7958 txfc = 0;
7959 rxfc = 1;
7960 break;
7961 case I40E_FC_NONE:
7962 case I40E_FC_DEFAULT:
7963 txfc = 0;
7964 rxfc = 0;
7965 break;
7966 case I40E_FC_PFC:
7967 /* TBD */
7968 break;
7969 /* no default case, we have to handle all possibilities here */
7970 }
7971
7972 wr32(&pf->hw, I40E_PRTDCB_FCCFG, txfc << I40E_PRTDCB_FCCFG_TFCE_SHIFT);
7973
7974 rxfc_reg = rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
7975 ~I40E_PRTDCB_MFLCN_RFCE_MASK;
7976 rxfc_reg |= (rxfc << I40E_PRTDCB_MFLCN_RFCE_SHIFT);
7977
7978 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rxfc_reg);
7979
7980 goto fc_complete;
7981
7982no_autoneg:
7983 /* disable L2 flow control, user can turn it on if they wish */
7984 wr32(&pf->hw, I40E_PRTDCB_FCCFG, 0);
7985 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
7986 ~I40E_PRTDCB_MFLCN_RFCE_MASK);
7987
7988fc_complete:
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00007989 i40e_ptp_init(pf);
7990
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007991 return ret;
7992}
7993
7994/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007995 * i40e_determine_queue_usage - Work out queue distribution
7996 * @pf: board private structure
7997 **/
7998static void i40e_determine_queue_usage(struct i40e_pf *pf)
7999{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008000 int queues_left;
8001
8002 pf->num_lan_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008003
8004 /* Find the max queues to be put into basic use. We'll always be
8005 * using TC0, whether or not DCB is running, and TC0 will get the
8006 * big RSS set.
8007 */
8008 queues_left = pf->hw.func_caps.num_tx_qp;
8009
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008010 if ((queues_left == 1) ||
8011 !(pf->flags & I40E_FLAG_MSIX_ENABLED) ||
8012 !(pf->flags & (I40E_FLAG_RSS_ENABLED | I40E_FLAG_FD_SB_ENABLED |
8013 I40E_FLAG_DCB_ENABLED))) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008014 /* one qp for PF, no queues for anything else */
8015 queues_left = 0;
8016 pf->rss_size = pf->num_lan_qps = 1;
8017
8018 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008019 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
8020 I40E_FLAG_FD_SB_ENABLED |
8021 I40E_FLAG_FD_ATR_ENABLED |
8022 I40E_FLAG_DCB_ENABLED |
8023 I40E_FLAG_SRIOV_ENABLED |
8024 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008025 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008026 /* Not enough queues for all TCs */
8027 if ((pf->flags & I40E_FLAG_DCB_ENABLED) &&
8028 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
8029 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8030 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
8031 }
8032 pf->num_lan_qps = pf->rss_size_max;
8033 queues_left -= pf->num_lan_qps;
8034 }
8035
8036 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8037 if (queues_left > 1) {
8038 queues_left -= 1; /* save 1 queue for FD */
8039 } else {
8040 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8041 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
8042 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008043 }
8044
8045 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
8046 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008047 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
8048 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008049 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
8050 }
8051
8052 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
8053 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
8054 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
8055 (queues_left / pf->num_vmdq_qps));
8056 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
8057 }
8058
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008059 pf->queues_left = queues_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008060 return;
8061}
8062
8063/**
8064 * i40e_setup_pf_filter_control - Setup PF static filter control
8065 * @pf: PF to be setup
8066 *
8067 * i40e_setup_pf_filter_control sets up a pf's initial filter control
8068 * settings. If PE/FCoE are enabled then it will also set the per PF
8069 * based filter sizes required for them. It also enables Flow director,
8070 * ethertype and macvlan type filter settings for the pf.
8071 *
8072 * Returns 0 on success, negative on failure
8073 **/
8074static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
8075{
8076 struct i40e_filter_control_settings *settings = &pf->filter_settings;
8077
8078 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
8079
8080 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008081 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008082 settings->enable_fdir = true;
8083
8084 /* Ethtype and MACVLAN filters enabled for PF */
8085 settings->enable_ethtype = true;
8086 settings->enable_macvlan = true;
8087
8088 if (i40e_set_filter_control(&pf->hw, settings))
8089 return -ENOENT;
8090
8091 return 0;
8092}
8093
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008094#define INFO_STRING_LEN 255
8095static void i40e_print_features(struct i40e_pf *pf)
8096{
8097 struct i40e_hw *hw = &pf->hw;
8098 char *buf, *string;
8099
8100 string = kzalloc(INFO_STRING_LEN, GFP_KERNEL);
8101 if (!string) {
8102 dev_err(&pf->pdev->dev, "Features string allocation failed\n");
8103 return;
8104 }
8105
8106 buf = string;
8107
8108 buf += sprintf(string, "Features: PF-id[%d] ", hw->pf_id);
8109#ifdef CONFIG_PCI_IOV
8110 buf += sprintf(buf, "VFs: %d ", pf->num_req_vfs);
8111#endif
8112 buf += sprintf(buf, "VSIs: %d QP: %d ", pf->hw.func_caps.num_vsis,
8113 pf->vsi[pf->lan_vsi]->num_queue_pairs);
8114
8115 if (pf->flags & I40E_FLAG_RSS_ENABLED)
8116 buf += sprintf(buf, "RSS ");
8117 buf += sprintf(buf, "FDir ");
8118 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
8119 buf += sprintf(buf, "ATR ");
8120 if (pf->flags & I40E_FLAG_FD_SB_ENABLED)
8121 buf += sprintf(buf, "NTUPLE ");
8122 if (pf->flags & I40E_FLAG_DCB_ENABLED)
8123 buf += sprintf(buf, "DCB ");
8124 if (pf->flags & I40E_FLAG_PTP)
8125 buf += sprintf(buf, "PTP ");
8126
8127 BUG_ON(buf > (string + INFO_STRING_LEN));
8128 dev_info(&pf->pdev->dev, "%s\n", string);
8129 kfree(string);
8130}
8131
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008132/**
8133 * i40e_probe - Device initialization routine
8134 * @pdev: PCI device information struct
8135 * @ent: entry in i40e_pci_tbl
8136 *
8137 * i40e_probe initializes a pf identified by a pci_dev structure.
8138 * The OS initialization, configuring of the pf private structure,
8139 * and a hardware reset occur.
8140 *
8141 * Returns 0 on success, negative on failure
8142 **/
8143static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
8144{
8145 struct i40e_driver_version dv;
8146 struct i40e_pf *pf;
8147 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008148 static u16 pfs_found;
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008149 u16 link_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008150 int err = 0;
8151 u32 len;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008152 u32 i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008153
8154 err = pci_enable_device_mem(pdev);
8155 if (err)
8156 return err;
8157
8158 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +00008159 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00008160 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00008161 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
8162 if (err) {
8163 dev_err(&pdev->dev,
8164 "DMA configuration failed: 0x%x\n", err);
8165 goto err_dma;
8166 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008167 }
8168
8169 /* set up pci connections */
8170 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
8171 IORESOURCE_MEM), i40e_driver_name);
8172 if (err) {
8173 dev_info(&pdev->dev,
8174 "pci_request_selected_regions failed %d\n", err);
8175 goto err_pci_reg;
8176 }
8177
8178 pci_enable_pcie_error_reporting(pdev);
8179 pci_set_master(pdev);
8180
8181 /* Now that we have a PCI connection, we need to do the
8182 * low level device setup. This is primarily setting up
8183 * the Admin Queue structures and then querying for the
8184 * device's current profile information.
8185 */
8186 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
8187 if (!pf) {
8188 err = -ENOMEM;
8189 goto err_pf_alloc;
8190 }
8191 pf->next_vsi = 0;
8192 pf->pdev = pdev;
8193 set_bit(__I40E_DOWN, &pf->state);
8194
8195 hw = &pf->hw;
8196 hw->back = pf;
8197 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
8198 pci_resource_len(pdev, 0));
8199 if (!hw->hw_addr) {
8200 err = -EIO;
8201 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
8202 (unsigned int)pci_resource_start(pdev, 0),
8203 (unsigned int)pci_resource_len(pdev, 0), err);
8204 goto err_ioremap;
8205 }
8206 hw->vendor_id = pdev->vendor;
8207 hw->device_id = pdev->device;
8208 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
8209 hw->subsystem_vendor_id = pdev->subsystem_vendor;
8210 hw->subsystem_device_id = pdev->subsystem_device;
8211 hw->bus.device = PCI_SLOT(pdev->devfn);
8212 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008213 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008214
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008215 /* do a special CORER for clearing PXE mode once at init */
8216 if (hw->revision_id == 0 &&
8217 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
8218 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
8219 i40e_flush(hw);
8220 msleep(200);
8221 pf->corer_count++;
8222
8223 i40e_clear_pxe_mode(hw);
8224 }
8225
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008226 /* Reset here to make sure all is clean and to define PF 'n' */
8227 err = i40e_pf_reset(hw);
8228 if (err) {
8229 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
8230 goto err_pf_reset;
8231 }
8232 pf->pfr_count++;
8233
8234 hw->aq.num_arq_entries = I40E_AQ_LEN;
8235 hw->aq.num_asq_entries = I40E_AQ_LEN;
8236 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
8237 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
8238 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
8239 snprintf(pf->misc_int_name, sizeof(pf->misc_int_name) - 1,
8240 "%s-pf%d:misc",
8241 dev_driver_string(&pf->pdev->dev), pf->hw.pf_id);
8242
8243 err = i40e_init_shared_code(hw);
8244 if (err) {
8245 dev_info(&pdev->dev, "init_shared_code failed: %d\n", err);
8246 goto err_pf_reset;
8247 }
8248
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008249 /* set up a default setting for link flow control */
8250 pf->hw.fc.requested_mode = I40E_FC_NONE;
8251
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008252 err = i40e_init_adminq(hw);
8253 dev_info(&pdev->dev, "%s\n", i40e_fw_version_str(hw));
8254 if (err) {
8255 dev_info(&pdev->dev,
8256 "init_adminq failed: %d expecting API %02x.%02x\n",
8257 err,
8258 I40E_FW_API_VERSION_MAJOR, I40E_FW_API_VERSION_MINOR);
8259 goto err_pf_reset;
8260 }
8261
Shannon Nelson4eb3f762014-03-06 08:59:58 +00008262 i40e_verify_eeprom(pf);
8263
Shannon Nelson6ff4ef82013-12-21 05:44:49 +00008264 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008265 err = i40e_get_capabilities(pf);
8266 if (err)
8267 goto err_adminq_setup;
8268
8269 err = i40e_sw_init(pf);
8270 if (err) {
8271 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
8272 goto err_sw_init;
8273 }
8274
8275 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
8276 hw->func_caps.num_rx_qp,
8277 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
8278 if (err) {
8279 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
8280 goto err_init_lan_hmc;
8281 }
8282
8283 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
8284 if (err) {
8285 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
8286 err = -ENOENT;
8287 goto err_configure_lan_hmc;
8288 }
8289
8290 i40e_get_mac_addr(hw, hw->mac.addr);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +00008291 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008292 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
8293 err = -EIO;
8294 goto err_mac_addr;
8295 }
8296 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
8297 memcpy(hw->mac.perm_addr, hw->mac.addr, ETH_ALEN);
8298
8299 pci_set_drvdata(pdev, pf);
8300 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008301#ifdef CONFIG_I40E_DCB
8302 err = i40e_init_pf_dcb(pf);
8303 if (err) {
8304 dev_info(&pdev->dev, "init_pf_dcb failed: %d\n", err);
8305 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh014269f2014-04-01 07:11:48 +00008306 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008307 }
8308#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008309
8310 /* set up periodic task facility */
8311 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
8312 pf->service_timer_period = HZ;
8313
8314 INIT_WORK(&pf->service_task, i40e_service_task);
8315 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
8316 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
8317 pf->link_check_timeout = jiffies;
8318
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008319 /* WoL defaults to disabled */
8320 pf->wol_en = false;
8321 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
8322
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008323 /* set up the main switch operations */
8324 i40e_determine_queue_usage(pf);
8325 i40e_init_interrupt_scheme(pf);
8326
8327 /* Set up the *vsi struct based on the number of VSIs in the HW,
8328 * and set up our local tracking of the MAIN PF vsi.
8329 */
8330 len = sizeof(struct i40e_vsi *) * pf->hw.func_caps.num_vsis;
8331 pf->vsi = kzalloc(len, GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +00008332 if (!pf->vsi) {
8333 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008334 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +00008335 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008336
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008337 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008338 if (err) {
8339 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
8340 goto err_vsis;
8341 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008342 /* if FDIR VSI was set up, start it now */
8343 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
8344 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
8345 i40e_vsi_open(pf->vsi[i]);
8346 break;
8347 }
8348 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008349
8350 /* The main driver is (mostly) up and happy. We need to set this state
8351 * before setting up the misc vector or we get a race and the vector
8352 * ends up disabled forever.
8353 */
8354 clear_bit(__I40E_DOWN, &pf->state);
8355
8356 /* In case of MSIX we are going to setup the misc vector right here
8357 * to handle admin queue events etc. In case of legacy and MSI
8358 * the misc functionality and queue processing is combined in
8359 * the same vector and that gets setup at open.
8360 */
8361 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
8362 err = i40e_setup_misc_vector(pf);
8363 if (err) {
8364 dev_info(&pdev->dev,
8365 "setup of misc vector failed: %d\n", err);
8366 goto err_vsis;
8367 }
8368 }
8369
8370 /* prep for VF support */
8371 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +00008372 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
8373 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008374 u32 val;
8375
8376 /* disable link interrupts for VFs */
8377 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
8378 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
8379 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
8380 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -08008381
8382 if (pci_num_vf(pdev)) {
8383 dev_info(&pdev->dev,
8384 "Active VFs found, allocating resources.\n");
8385 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
8386 if (err)
8387 dev_info(&pdev->dev,
8388 "Error %d allocating resources for existing VFs\n",
8389 err);
8390 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008391 }
8392
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008393 pfs_found++;
8394
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008395 i40e_dbg_pf_init(pf);
8396
8397 /* tell the firmware that we're starting */
8398 dv.major_version = DRV_VERSION_MAJOR;
8399 dv.minor_version = DRV_VERSION_MINOR;
8400 dv.build_version = DRV_VERSION_BUILD;
8401 dv.subbuild_version = 0;
Shannon Nelsond2466012014-04-01 07:11:45 +00008402 strncpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008403 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
8404
8405 /* since everything's happy, start the service_task timer */
8406 mod_timer(&pf->service_timer,
8407 round_jiffies(jiffies + pf->service_timer_period));
8408
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008409 /* Get the negotiated link width and speed from PCI config space */
8410 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA, &link_status);
8411
8412 i40e_set_pci_config_data(hw, link_status);
8413
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00008414 dev_info(&pdev->dev, "PCI-Express: %s %s\n",
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008415 (hw->bus.speed == i40e_bus_speed_8000 ? "Speed 8.0GT/s" :
8416 hw->bus.speed == i40e_bus_speed_5000 ? "Speed 5.0GT/s" :
8417 hw->bus.speed == i40e_bus_speed_2500 ? "Speed 2.5GT/s" :
8418 "Unknown"),
8419 (hw->bus.width == i40e_bus_width_pcie_x8 ? "Width x8" :
8420 hw->bus.width == i40e_bus_width_pcie_x4 ? "Width x4" :
8421 hw->bus.width == i40e_bus_width_pcie_x2 ? "Width x2" :
8422 hw->bus.width == i40e_bus_width_pcie_x1 ? "Width x1" :
8423 "Unknown"));
8424
8425 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
8426 hw->bus.speed < i40e_bus_speed_8000) {
8427 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
8428 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
8429 }
8430
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008431 /* print a string summarizing features */
8432 i40e_print_features(pf);
8433
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008434 return 0;
8435
8436 /* Unwind what we've done if something failed in the setup */
8437err_vsis:
8438 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008439 i40e_clear_interrupt_scheme(pf);
8440 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +00008441err_switch_setup:
8442 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008443 del_timer_sync(&pf->service_timer);
8444err_mac_addr:
8445err_configure_lan_hmc:
8446 (void)i40e_shutdown_lan_hmc(hw);
8447err_init_lan_hmc:
8448 kfree(pf->qp_pile);
8449 kfree(pf->irq_pile);
8450err_sw_init:
8451err_adminq_setup:
8452 (void)i40e_shutdown_adminq(hw);
8453err_pf_reset:
8454 iounmap(hw->hw_addr);
8455err_ioremap:
8456 kfree(pf);
8457err_pf_alloc:
8458 pci_disable_pcie_error_reporting(pdev);
8459 pci_release_selected_regions(pdev,
8460 pci_select_bars(pdev, IORESOURCE_MEM));
8461err_pci_reg:
8462err_dma:
8463 pci_disable_device(pdev);
8464 return err;
8465}
8466
8467/**
8468 * i40e_remove - Device removal routine
8469 * @pdev: PCI device information struct
8470 *
8471 * i40e_remove is called by the PCI subsystem to alert the driver
8472 * that is should release a PCI device. This could be caused by a
8473 * Hot-Plug event, or because the driver is going to be removed from
8474 * memory.
8475 **/
8476static void i40e_remove(struct pci_dev *pdev)
8477{
8478 struct i40e_pf *pf = pci_get_drvdata(pdev);
8479 i40e_status ret_code;
8480 u32 reg;
8481 int i;
8482
8483 i40e_dbg_pf_exit(pf);
8484
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008485 i40e_ptp_stop(pf);
8486
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008487 /* no more scheduling of any task */
8488 set_bit(__I40E_DOWN, &pf->state);
8489 del_timer_sync(&pf->service_timer);
8490 cancel_work_sync(&pf->service_task);
8491
Mitch Williamseb2d80b2014-02-13 03:48:48 -08008492 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
8493 i40e_free_vfs(pf);
8494 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
8495 }
8496
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008497 i40e_fdir_teardown(pf);
8498
8499 /* If there is a switch structure or any orphans, remove them.
8500 * This will leave only the PF's VSI remaining.
8501 */
8502 for (i = 0; i < I40E_MAX_VEB; i++) {
8503 if (!pf->veb[i])
8504 continue;
8505
8506 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
8507 pf->veb[i]->uplink_seid == 0)
8508 i40e_switch_branch_release(pf->veb[i]);
8509 }
8510
8511 /* Now we can shutdown the PF's VSI, just before we kill
8512 * adminq and hmc.
8513 */
8514 if (pf->vsi[pf->lan_vsi])
8515 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
8516
8517 i40e_stop_misc_vector(pf);
8518 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
8519 synchronize_irq(pf->msix_entries[0].vector);
8520 free_irq(pf->msix_entries[0].vector, pf);
8521 }
8522
8523 /* shutdown and destroy the HMC */
8524 ret_code = i40e_shutdown_lan_hmc(&pf->hw);
8525 if (ret_code)
8526 dev_warn(&pdev->dev,
8527 "Failed to destroy the HMC resources: %d\n", ret_code);
8528
8529 /* shutdown the adminq */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008530 ret_code = i40e_shutdown_adminq(&pf->hw);
8531 if (ret_code)
8532 dev_warn(&pdev->dev,
8533 "Failed to destroy the Admin Queue resources: %d\n",
8534 ret_code);
8535
8536 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
8537 i40e_clear_interrupt_scheme(pf);
8538 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
8539 if (pf->vsi[i]) {
8540 i40e_vsi_clear_rings(pf->vsi[i]);
8541 i40e_vsi_clear(pf->vsi[i]);
8542 pf->vsi[i] = NULL;
8543 }
8544 }
8545
8546 for (i = 0; i < I40E_MAX_VEB; i++) {
8547 kfree(pf->veb[i]);
8548 pf->veb[i] = NULL;
8549 }
8550
8551 kfree(pf->qp_pile);
8552 kfree(pf->irq_pile);
8553 kfree(pf->sw_config);
8554 kfree(pf->vsi);
8555
8556 /* force a PF reset to clean anything leftover */
8557 reg = rd32(&pf->hw, I40E_PFGEN_CTRL);
8558 wr32(&pf->hw, I40E_PFGEN_CTRL, (reg | I40E_PFGEN_CTRL_PFSWR_MASK));
8559 i40e_flush(&pf->hw);
8560
8561 iounmap(pf->hw.hw_addr);
8562 kfree(pf);
8563 pci_release_selected_regions(pdev,
8564 pci_select_bars(pdev, IORESOURCE_MEM));
8565
8566 pci_disable_pcie_error_reporting(pdev);
8567 pci_disable_device(pdev);
8568}
8569
8570/**
8571 * i40e_pci_error_detected - warning that something funky happened in PCI land
8572 * @pdev: PCI device information struct
8573 *
8574 * Called to warn that something happened and the error handling steps
8575 * are in progress. Allows the driver to quiesce things, be ready for
8576 * remediation.
8577 **/
8578static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
8579 enum pci_channel_state error)
8580{
8581 struct i40e_pf *pf = pci_get_drvdata(pdev);
8582
8583 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
8584
8585 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008586 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
8587 rtnl_lock();
8588 i40e_prep_for_reset(pf);
8589 rtnl_unlock();
8590 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008591
8592 /* Request a slot reset */
8593 return PCI_ERS_RESULT_NEED_RESET;
8594}
8595
8596/**
8597 * i40e_pci_error_slot_reset - a PCI slot reset just happened
8598 * @pdev: PCI device information struct
8599 *
8600 * Called to find if the driver can work with the device now that
8601 * the pci slot has been reset. If a basic connection seems good
8602 * (registers are readable and have sane content) then return a
8603 * happy little PCI_ERS_RESULT_xxx.
8604 **/
8605static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
8606{
8607 struct i40e_pf *pf = pci_get_drvdata(pdev);
8608 pci_ers_result_t result;
8609 int err;
8610 u32 reg;
8611
8612 dev_info(&pdev->dev, "%s\n", __func__);
8613 if (pci_enable_device_mem(pdev)) {
8614 dev_info(&pdev->dev,
8615 "Cannot re-enable PCI device after reset.\n");
8616 result = PCI_ERS_RESULT_DISCONNECT;
8617 } else {
8618 pci_set_master(pdev);
8619 pci_restore_state(pdev);
8620 pci_save_state(pdev);
8621 pci_wake_from_d3(pdev, false);
8622
8623 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8624 if (reg == 0)
8625 result = PCI_ERS_RESULT_RECOVERED;
8626 else
8627 result = PCI_ERS_RESULT_DISCONNECT;
8628 }
8629
8630 err = pci_cleanup_aer_uncorrect_error_status(pdev);
8631 if (err) {
8632 dev_info(&pdev->dev,
8633 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
8634 err);
8635 /* non-fatal, continue */
8636 }
8637
8638 return result;
8639}
8640
8641/**
8642 * i40e_pci_error_resume - restart operations after PCI error recovery
8643 * @pdev: PCI device information struct
8644 *
8645 * Called to allow the driver to bring things back up after PCI error
8646 * and/or reset recovery has finished.
8647 **/
8648static void i40e_pci_error_resume(struct pci_dev *pdev)
8649{
8650 struct i40e_pf *pf = pci_get_drvdata(pdev);
8651
8652 dev_info(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008653 if (test_bit(__I40E_SUSPENDED, &pf->state))
8654 return;
8655
8656 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008657 i40e_handle_reset_warning(pf);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008658 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008659}
8660
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008661/**
8662 * i40e_shutdown - PCI callback for shutting down
8663 * @pdev: PCI device information struct
8664 **/
8665static void i40e_shutdown(struct pci_dev *pdev)
8666{
8667 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008668 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008669
8670 set_bit(__I40E_SUSPENDED, &pf->state);
8671 set_bit(__I40E_DOWN, &pf->state);
8672 rtnl_lock();
8673 i40e_prep_for_reset(pf);
8674 rtnl_unlock();
8675
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008676 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
8677 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
8678
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008679 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008680 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008681 pci_set_power_state(pdev, PCI_D3hot);
8682 }
8683}
8684
8685#ifdef CONFIG_PM
8686/**
8687 * i40e_suspend - PCI callback for moving to D3
8688 * @pdev: PCI device information struct
8689 **/
8690static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
8691{
8692 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008693 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008694
8695 set_bit(__I40E_SUSPENDED, &pf->state);
8696 set_bit(__I40E_DOWN, &pf->state);
8697 rtnl_lock();
8698 i40e_prep_for_reset(pf);
8699 rtnl_unlock();
8700
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008701 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
8702 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
8703
8704 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008705 pci_set_power_state(pdev, PCI_D3hot);
8706
8707 return 0;
8708}
8709
8710/**
8711 * i40e_resume - PCI callback for waking up from D3
8712 * @pdev: PCI device information struct
8713 **/
8714static int i40e_resume(struct pci_dev *pdev)
8715{
8716 struct i40e_pf *pf = pci_get_drvdata(pdev);
8717 u32 err;
8718
8719 pci_set_power_state(pdev, PCI_D0);
8720 pci_restore_state(pdev);
8721 /* pci_restore_state() clears dev->state_saves, so
8722 * call pci_save_state() again to restore it.
8723 */
8724 pci_save_state(pdev);
8725
8726 err = pci_enable_device_mem(pdev);
8727 if (err) {
8728 dev_err(&pdev->dev,
8729 "%s: Cannot enable PCI device from suspend\n",
8730 __func__);
8731 return err;
8732 }
8733 pci_set_master(pdev);
8734
8735 /* no wakeup events while running */
8736 pci_wake_from_d3(pdev, false);
8737
8738 /* handling the reset will rebuild the device state */
8739 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
8740 clear_bit(__I40E_DOWN, &pf->state);
8741 rtnl_lock();
8742 i40e_reset_and_rebuild(pf, false);
8743 rtnl_unlock();
8744 }
8745
8746 return 0;
8747}
8748
8749#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008750static const struct pci_error_handlers i40e_err_handler = {
8751 .error_detected = i40e_pci_error_detected,
8752 .slot_reset = i40e_pci_error_slot_reset,
8753 .resume = i40e_pci_error_resume,
8754};
8755
8756static struct pci_driver i40e_driver = {
8757 .name = i40e_driver_name,
8758 .id_table = i40e_pci_tbl,
8759 .probe = i40e_probe,
8760 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008761#ifdef CONFIG_PM
8762 .suspend = i40e_suspend,
8763 .resume = i40e_resume,
8764#endif
8765 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008766 .err_handler = &i40e_err_handler,
8767 .sriov_configure = i40e_pci_sriov_configure,
8768};
8769
8770/**
8771 * i40e_init_module - Driver registration routine
8772 *
8773 * i40e_init_module is the first routine called when the driver is
8774 * loaded. All it does is register with the PCI subsystem.
8775 **/
8776static int __init i40e_init_module(void)
8777{
8778 pr_info("%s: %s - version %s\n", i40e_driver_name,
8779 i40e_driver_string, i40e_driver_version_str);
8780 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
8781 i40e_dbg_init();
8782 return pci_register_driver(&i40e_driver);
8783}
8784module_init(i40e_init_module);
8785
8786/**
8787 * i40e_exit_module - Driver exit cleanup routine
8788 *
8789 * i40e_exit_module is called just before the driver is removed
8790 * from memory.
8791 **/
8792static void __exit i40e_exit_module(void)
8793{
8794 pci_unregister_driver(&i40e_driver);
8795 i40e_dbg_exit();
8796}
8797module_exit(i40e_exit_module);