blob: e340e3ba240a814d70a8478a77ca6861e523853d [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 Sullivanded7b9a2014-04-01 07:11:57 +000042#define DRV_VERSION_BUILD 46
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);
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +0000359 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000360 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000361 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
362 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000363
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000364 if (test_bit(__I40E_DOWN, &vsi->state))
365 return stats;
366
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800367 if (!vsi->tx_rings)
368 return stats;
369
Alexander Duyck980e9b12013-09-28 06:01:03 +0000370 rcu_read_lock();
371 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000372 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/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000723 * i40e_update_vsi_stats - Update the vsi statistics counters.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000724 * @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
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000730 * VF communications. We sort it out here.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000731 **/
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000732static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000733{
734 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000735 struct rtnl_link_stats64 *ons;
736 struct rtnl_link_stats64 *ns; /* netdev stats */
737 struct i40e_eth_stats *oes;
738 struct i40e_eth_stats *es; /* device's eth stats */
739 u32 tx_restart, tx_busy;
740 u32 rx_page, rx_buf;
741 u64 rx_p, rx_b;
742 u64 tx_p, tx_b;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000743 u16 q;
744
745 if (test_bit(__I40E_DOWN, &vsi->state) ||
746 test_bit(__I40E_CONFIG_BUSY, &pf->state))
747 return;
748
749 ns = i40e_get_vsi_stats_struct(vsi);
750 ons = &vsi->net_stats_offsets;
751 es = &vsi->eth_stats;
752 oes = &vsi->eth_stats_offsets;
753
754 /* Gather up the netdev and vsi stats that the driver collects
755 * on the fly during packet processing
756 */
757 rx_b = rx_p = 0;
758 tx_b = tx_p = 0;
759 tx_restart = tx_busy = 0;
760 rx_page = 0;
761 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000762 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000763 for (q = 0; q < vsi->num_queue_pairs; q++) {
764 struct i40e_ring *p;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000765 u64 bytes, packets;
766 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000767
Alexander Duyck980e9b12013-09-28 06:01:03 +0000768 /* locate Tx ring */
769 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000770
Alexander Duyck980e9b12013-09-28 06:01:03 +0000771 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700772 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000773 packets = p->stats.packets;
774 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700775 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000776 tx_b += bytes;
777 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000778 tx_restart += p->tx_stats.restart_queue;
779 tx_busy += p->tx_stats.tx_busy;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000780
781 /* Rx queue is part of the same block as Tx queue */
782 p = &p[1];
783 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700784 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000785 packets = p->stats.packets;
786 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700787 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000788 rx_b += bytes;
789 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000790 rx_buf += p->rx_stats.alloc_buff_failed;
791 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000792 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000793 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000794 vsi->tx_restart = tx_restart;
795 vsi->tx_busy = tx_busy;
796 vsi->rx_page_failed = rx_page;
797 vsi->rx_buf_failed = rx_buf;
798
799 ns->rx_packets = rx_p;
800 ns->rx_bytes = rx_b;
801 ns->tx_packets = tx_p;
802 ns->tx_bytes = tx_b;
803
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000804 /* update netdev stats from eth stats */
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000805 i40e_update_eth_stats(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000806 ons->rx_errors = oes->rx_errors;
807 ns->rx_errors = es->rx_errors;
808 ons->tx_errors = oes->tx_errors;
809 ns->tx_errors = es->tx_errors;
810 ons->multicast = oes->rx_multicast;
811 ns->multicast = es->rx_multicast;
812 ons->tx_dropped = oes->tx_discards;
813 ns->tx_dropped = es->tx_discards;
814
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000815 /* pull in a couple PF stats if this is the main vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000816 if (vsi == pf->vsi[pf->lan_vsi]) {
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000817 ns->rx_crc_errors = pf->stats.crc_errors;
818 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
819 ns->rx_length_errors = pf->stats.rx_length_errors;
820 }
821}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000822
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000823/**
824 * i40e_update_pf_stats - Update the pf statistics counters.
825 * @pf: the PF to be updated
826 **/
827static void i40e_update_pf_stats(struct i40e_pf *pf)
828{
829 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
830 struct i40e_hw_port_stats *nsd = &pf->stats;
831 struct i40e_hw *hw = &pf->hw;
832 u32 val;
833 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000834
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000835 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
836 I40E_GLPRT_GORCL(hw->port),
837 pf->stat_offsets_loaded,
838 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
839 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
840 I40E_GLPRT_GOTCL(hw->port),
841 pf->stat_offsets_loaded,
842 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
843 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
844 pf->stat_offsets_loaded,
845 &osd->eth.rx_discards,
846 &nsd->eth.rx_discards);
847 i40e_stat_update32(hw, I40E_GLPRT_TDPC(hw->port),
848 pf->stat_offsets_loaded,
849 &osd->eth.tx_discards,
850 &nsd->eth.tx_discards);
851 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
852 I40E_GLPRT_MPRCL(hw->port),
853 pf->stat_offsets_loaded,
854 &osd->eth.rx_multicast,
855 &nsd->eth.rx_multicast);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000856
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000857 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
858 pf->stat_offsets_loaded,
859 &osd->tx_dropped_link_down,
860 &nsd->tx_dropped_link_down);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000861
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000862 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
863 pf->stat_offsets_loaded,
864 &osd->crc_errors, &nsd->crc_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000865
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000866 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
867 pf->stat_offsets_loaded,
868 &osd->illegal_bytes, &nsd->illegal_bytes);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000869
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000870 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
871 pf->stat_offsets_loaded,
872 &osd->mac_local_faults,
873 &nsd->mac_local_faults);
874 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
875 pf->stat_offsets_loaded,
876 &osd->mac_remote_faults,
877 &nsd->mac_remote_faults);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000878
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000879 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
880 pf->stat_offsets_loaded,
881 &osd->rx_length_errors,
882 &nsd->rx_length_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000883
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000884 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
885 pf->stat_offsets_loaded,
886 &osd->link_xon_rx, &nsd->link_xon_rx);
887 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
888 pf->stat_offsets_loaded,
889 &osd->link_xon_tx, &nsd->link_xon_tx);
890 i40e_update_prio_xoff_rx(pf); /* handles I40E_GLPRT_LXOFFRXC */
891 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
892 pf->stat_offsets_loaded,
893 &osd->link_xoff_tx, &nsd->link_xoff_tx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000894
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000895 for (i = 0; i < 8; i++) {
896 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000897 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000898 &osd->priority_xon_rx[i],
899 &nsd->priority_xon_rx[i]);
900 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000901 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000902 &osd->priority_xon_tx[i],
903 &nsd->priority_xon_tx[i]);
904 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000905 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000906 &osd->priority_xoff_tx[i],
907 &nsd->priority_xoff_tx[i]);
908 i40e_stat_update32(hw,
909 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000910 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000911 &osd->priority_xon_2_xoff[i],
912 &nsd->priority_xon_2_xoff[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000913 }
914
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000915 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
916 I40E_GLPRT_PRC64L(hw->port),
917 pf->stat_offsets_loaded,
918 &osd->rx_size_64, &nsd->rx_size_64);
919 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
920 I40E_GLPRT_PRC127L(hw->port),
921 pf->stat_offsets_loaded,
922 &osd->rx_size_127, &nsd->rx_size_127);
923 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
924 I40E_GLPRT_PRC255L(hw->port),
925 pf->stat_offsets_loaded,
926 &osd->rx_size_255, &nsd->rx_size_255);
927 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
928 I40E_GLPRT_PRC511L(hw->port),
929 pf->stat_offsets_loaded,
930 &osd->rx_size_511, &nsd->rx_size_511);
931 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
932 I40E_GLPRT_PRC1023L(hw->port),
933 pf->stat_offsets_loaded,
934 &osd->rx_size_1023, &nsd->rx_size_1023);
935 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
936 I40E_GLPRT_PRC1522L(hw->port),
937 pf->stat_offsets_loaded,
938 &osd->rx_size_1522, &nsd->rx_size_1522);
939 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
940 I40E_GLPRT_PRC9522L(hw->port),
941 pf->stat_offsets_loaded,
942 &osd->rx_size_big, &nsd->rx_size_big);
943
944 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
945 I40E_GLPRT_PTC64L(hw->port),
946 pf->stat_offsets_loaded,
947 &osd->tx_size_64, &nsd->tx_size_64);
948 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
949 I40E_GLPRT_PTC127L(hw->port),
950 pf->stat_offsets_loaded,
951 &osd->tx_size_127, &nsd->tx_size_127);
952 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
953 I40E_GLPRT_PTC255L(hw->port),
954 pf->stat_offsets_loaded,
955 &osd->tx_size_255, &nsd->tx_size_255);
956 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
957 I40E_GLPRT_PTC511L(hw->port),
958 pf->stat_offsets_loaded,
959 &osd->tx_size_511, &nsd->tx_size_511);
960 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
961 I40E_GLPRT_PTC1023L(hw->port),
962 pf->stat_offsets_loaded,
963 &osd->tx_size_1023, &nsd->tx_size_1023);
964 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
965 I40E_GLPRT_PTC1522L(hw->port),
966 pf->stat_offsets_loaded,
967 &osd->tx_size_1522, &nsd->tx_size_1522);
968 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
969 I40E_GLPRT_PTC9522L(hw->port),
970 pf->stat_offsets_loaded,
971 &osd->tx_size_big, &nsd->tx_size_big);
972
973 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
974 pf->stat_offsets_loaded,
975 &osd->rx_undersize, &nsd->rx_undersize);
976 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
977 pf->stat_offsets_loaded,
978 &osd->rx_fragments, &nsd->rx_fragments);
979 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
980 pf->stat_offsets_loaded,
981 &osd->rx_oversize, &nsd->rx_oversize);
982 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
983 pf->stat_offsets_loaded,
984 &osd->rx_jabber, &nsd->rx_jabber);
985
986 val = rd32(hw, I40E_PRTPM_EEE_STAT);
987 nsd->tx_lpi_status =
988 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
989 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
990 nsd->rx_lpi_status =
991 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
992 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
993 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
994 pf->stat_offsets_loaded,
995 &osd->tx_lpi_count, &nsd->tx_lpi_count);
996 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
997 pf->stat_offsets_loaded,
998 &osd->rx_lpi_count, &nsd->rx_lpi_count);
999
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001000 pf->stat_offsets_loaded = true;
1001}
1002
1003/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001004 * i40e_update_stats - Update the various statistics counters.
1005 * @vsi: the VSI to be updated
1006 *
1007 * Update the various stats for this VSI and its related entities.
1008 **/
1009void i40e_update_stats(struct i40e_vsi *vsi)
1010{
1011 struct i40e_pf *pf = vsi->back;
1012
1013 if (vsi == pf->vsi[pf->lan_vsi])
1014 i40e_update_pf_stats(pf);
1015
1016 i40e_update_vsi_stats(vsi);
1017}
1018
1019/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001020 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1021 * @vsi: the VSI to be searched
1022 * @macaddr: the MAC address
1023 * @vlan: the vlan
1024 * @is_vf: make sure its a vf filter, else doesn't matter
1025 * @is_netdev: make sure its a netdev filter, else doesn't matter
1026 *
1027 * Returns ptr to the filter object or NULL
1028 **/
1029static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1030 u8 *macaddr, s16 vlan,
1031 bool is_vf, bool is_netdev)
1032{
1033 struct i40e_mac_filter *f;
1034
1035 if (!vsi || !macaddr)
1036 return NULL;
1037
1038 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1039 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1040 (vlan == f->vlan) &&
1041 (!is_vf || f->is_vf) &&
1042 (!is_netdev || f->is_netdev))
1043 return f;
1044 }
1045 return NULL;
1046}
1047
1048/**
1049 * i40e_find_mac - Find a mac addr in the macvlan filters list
1050 * @vsi: the VSI to be searched
1051 * @macaddr: the MAC address we are searching for
1052 * @is_vf: make sure its a vf filter, else doesn't matter
1053 * @is_netdev: make sure its a netdev filter, else doesn't matter
1054 *
1055 * Returns the first filter with the provided MAC address or NULL if
1056 * MAC address was not found
1057 **/
1058struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, u8 *macaddr,
1059 bool is_vf, bool is_netdev)
1060{
1061 struct i40e_mac_filter *f;
1062
1063 if (!vsi || !macaddr)
1064 return NULL;
1065
1066 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1067 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1068 (!is_vf || f->is_vf) &&
1069 (!is_netdev || f->is_netdev))
1070 return f;
1071 }
1072 return NULL;
1073}
1074
1075/**
1076 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1077 * @vsi: the VSI to be searched
1078 *
1079 * Returns true if VSI is in vlan mode or false otherwise
1080 **/
1081bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1082{
1083 struct i40e_mac_filter *f;
1084
1085 /* Only -1 for all the filters denotes not in vlan mode
1086 * so we have to go through all the list in order to make sure
1087 */
1088 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1089 if (f->vlan >= 0)
1090 return true;
1091 }
1092
1093 return false;
1094}
1095
1096/**
1097 * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
1098 * @vsi: the VSI to be searched
1099 * @macaddr: the mac address to be filtered
1100 * @is_vf: true if it is a vf
1101 * @is_netdev: true if it is a netdev
1102 *
1103 * Goes through all the macvlan filters and adds a
1104 * macvlan filter for each unique vlan that already exists
1105 *
1106 * Returns first filter found on success, else NULL
1107 **/
1108struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi, u8 *macaddr,
1109 bool is_vf, bool is_netdev)
1110{
1111 struct i40e_mac_filter *f;
1112
1113 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1114 if (!i40e_find_filter(vsi, macaddr, f->vlan,
1115 is_vf, is_netdev)) {
1116 if (!i40e_add_filter(vsi, macaddr, f->vlan,
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -08001117 is_vf, is_netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001118 return NULL;
1119 }
1120 }
1121
1122 return list_first_entry_or_null(&vsi->mac_filter_list,
1123 struct i40e_mac_filter, list);
1124}
1125
1126/**
1127 * i40e_add_filter - Add a mac/vlan filter to the VSI
1128 * @vsi: the VSI to be searched
1129 * @macaddr: the MAC address
1130 * @vlan: the vlan
1131 * @is_vf: make sure its a vf filter, else doesn't matter
1132 * @is_netdev: make sure its a netdev filter, else doesn't matter
1133 *
1134 * Returns ptr to the filter object or NULL when no memory available.
1135 **/
1136struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1137 u8 *macaddr, s16 vlan,
1138 bool is_vf, bool is_netdev)
1139{
1140 struct i40e_mac_filter *f;
1141
1142 if (!vsi || !macaddr)
1143 return NULL;
1144
1145 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1146 if (!f) {
1147 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1148 if (!f)
1149 goto add_filter_out;
1150
1151 memcpy(f->macaddr, macaddr, ETH_ALEN);
1152 f->vlan = vlan;
1153 f->changed = true;
1154
1155 INIT_LIST_HEAD(&f->list);
1156 list_add(&f->list, &vsi->mac_filter_list);
1157 }
1158
1159 /* increment counter and add a new flag if needed */
1160 if (is_vf) {
1161 if (!f->is_vf) {
1162 f->is_vf = true;
1163 f->counter++;
1164 }
1165 } else if (is_netdev) {
1166 if (!f->is_netdev) {
1167 f->is_netdev = true;
1168 f->counter++;
1169 }
1170 } else {
1171 f->counter++;
1172 }
1173
1174 /* changed tells sync_filters_subtask to
1175 * push the filter down to the firmware
1176 */
1177 if (f->changed) {
1178 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1179 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1180 }
1181
1182add_filter_out:
1183 return f;
1184}
1185
1186/**
1187 * i40e_del_filter - Remove a mac/vlan filter from the VSI
1188 * @vsi: the VSI to be searched
1189 * @macaddr: the MAC address
1190 * @vlan: the vlan
1191 * @is_vf: make sure it's a vf filter, else doesn't matter
1192 * @is_netdev: make sure it's a netdev filter, else doesn't matter
1193 **/
1194void i40e_del_filter(struct i40e_vsi *vsi,
1195 u8 *macaddr, s16 vlan,
1196 bool is_vf, bool is_netdev)
1197{
1198 struct i40e_mac_filter *f;
1199
1200 if (!vsi || !macaddr)
1201 return;
1202
1203 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1204 if (!f || f->counter == 0)
1205 return;
1206
1207 if (is_vf) {
1208 if (f->is_vf) {
1209 f->is_vf = false;
1210 f->counter--;
1211 }
1212 } else if (is_netdev) {
1213 if (f->is_netdev) {
1214 f->is_netdev = false;
1215 f->counter--;
1216 }
1217 } else {
1218 /* make sure we don't remove a filter in use by vf or netdev */
1219 int min_f = 0;
1220 min_f += (f->is_vf ? 1 : 0);
1221 min_f += (f->is_netdev ? 1 : 0);
1222
1223 if (f->counter > min_f)
1224 f->counter--;
1225 }
1226
1227 /* counter == 0 tells sync_filters_subtask to
1228 * remove the filter from the firmware's list
1229 */
1230 if (f->counter == 0) {
1231 f->changed = true;
1232 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1233 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1234 }
1235}
1236
1237/**
1238 * i40e_set_mac - NDO callback to set mac address
1239 * @netdev: network interface device structure
1240 * @p: pointer to an address structure
1241 *
1242 * Returns 0 on success, negative on failure
1243 **/
1244static int i40e_set_mac(struct net_device *netdev, void *p)
1245{
1246 struct i40e_netdev_priv *np = netdev_priv(netdev);
1247 struct i40e_vsi *vsi = np->vsi;
1248 struct sockaddr *addr = p;
1249 struct i40e_mac_filter *f;
1250
1251 if (!is_valid_ether_addr(addr->sa_data))
1252 return -EADDRNOTAVAIL;
1253
1254 netdev_info(netdev, "set mac address=%pM\n", addr->sa_data);
1255
1256 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
1257 return 0;
1258
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001259 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1260 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1261 return -EADDRNOTAVAIL;
1262
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001263 if (vsi->type == I40E_VSI_MAIN) {
1264 i40e_status ret;
1265 ret = i40e_aq_mac_address_write(&vsi->back->hw,
1266 I40E_AQC_WRITE_TYPE_LAA_ONLY,
1267 addr->sa_data, NULL);
1268 if (ret) {
1269 netdev_info(netdev,
1270 "Addr change for Main VSI failed: %d\n",
1271 ret);
1272 return -EADDRNOTAVAIL;
1273 }
1274
1275 memcpy(vsi->back->hw.mac.addr, addr->sa_data, netdev->addr_len);
1276 }
1277
1278 /* In order to be sure to not drop any packets, add the new address
1279 * then delete the old one.
1280 */
1281 f = i40e_add_filter(vsi, addr->sa_data, I40E_VLAN_ANY, false, false);
1282 if (!f)
1283 return -ENOMEM;
1284
1285 i40e_sync_vsi_filters(vsi);
1286 i40e_del_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY, false, false);
1287 i40e_sync_vsi_filters(vsi);
1288
1289 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1290
1291 return 0;
1292}
1293
1294/**
1295 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1296 * @vsi: the VSI being setup
1297 * @ctxt: VSI context structure
1298 * @enabled_tc: Enabled TCs bitmap
1299 * @is_add: True if called before Add VSI
1300 *
1301 * Setup VSI queue mapping for enabled traffic classes.
1302 **/
1303static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1304 struct i40e_vsi_context *ctxt,
1305 u8 enabled_tc,
1306 bool is_add)
1307{
1308 struct i40e_pf *pf = vsi->back;
1309 u16 sections = 0;
1310 u8 netdev_tc = 0;
1311 u16 numtc = 0;
1312 u16 qcount;
1313 u8 offset;
1314 u16 qmap;
1315 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001316 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001317
1318 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1319 offset = 0;
1320
1321 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1322 /* Find numtc from enabled TC bitmap */
1323 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1324 if (enabled_tc & (1 << i)) /* TC is enabled */
1325 numtc++;
1326 }
1327 if (!numtc) {
1328 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1329 numtc = 1;
1330 }
1331 } else {
1332 /* At least TC0 is enabled in case of non-DCB case */
1333 numtc = 1;
1334 }
1335
1336 vsi->tc_config.numtc = numtc;
1337 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001338 /* Number of queues per enabled TC */
1339 num_tc_qps = rounddown_pow_of_two(vsi->alloc_queue_pairs/numtc);
1340 num_tc_qps = min_t(int, num_tc_qps, I40E_MAX_QUEUES_PER_TC);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001341
1342 /* Setup queue offset/count for all TCs for given VSI */
1343 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1344 /* See if the given TC is enabled for the given VSI */
1345 if (vsi->tc_config.enabled_tc & (1 << i)) { /* TC is enabled */
1346 int pow, num_qps;
1347
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001348 switch (vsi->type) {
1349 case I40E_VSI_MAIN:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001350 qcount = min_t(int, pf->rss_size, num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001351 break;
1352 case I40E_VSI_FDIR:
1353 case I40E_VSI_SRIOV:
1354 case I40E_VSI_VMDQ2:
1355 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001356 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001357 WARN_ON(i != 0);
1358 break;
1359 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001360 vsi->tc_config.tc_info[i].qoffset = offset;
1361 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001362
1363 /* find the power-of-2 of the number of queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001364 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001365 pow = 0;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001366 while (num_qps && ((1 << pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001367 pow++;
1368 num_qps >>= 1;
1369 }
1370
1371 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1372 qmap =
1373 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1374 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1375
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001376 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001377 } else {
1378 /* TC is not enabled so set the offset to
1379 * default queue and allocate one queue
1380 * for the given TC.
1381 */
1382 vsi->tc_config.tc_info[i].qoffset = 0;
1383 vsi->tc_config.tc_info[i].qcount = 1;
1384 vsi->tc_config.tc_info[i].netdev_tc = 0;
1385
1386 qmap = 0;
1387 }
1388 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1389 }
1390
1391 /* Set actual Tx/Rx queue pairs */
1392 vsi->num_queue_pairs = offset;
1393
1394 /* Scheduler section valid can only be set for ADD VSI */
1395 if (is_add) {
1396 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1397
1398 ctxt->info.up_enable_bits = enabled_tc;
1399 }
1400 if (vsi->type == I40E_VSI_SRIOV) {
1401 ctxt->info.mapping_flags |=
1402 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1403 for (i = 0; i < vsi->num_queue_pairs; i++)
1404 ctxt->info.queue_mapping[i] =
1405 cpu_to_le16(vsi->base_queue + i);
1406 } else {
1407 ctxt->info.mapping_flags |=
1408 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1409 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1410 }
1411 ctxt->info.valid_sections |= cpu_to_le16(sections);
1412}
1413
1414/**
1415 * i40e_set_rx_mode - NDO callback to set the netdev filters
1416 * @netdev: network interface device structure
1417 **/
1418static void i40e_set_rx_mode(struct net_device *netdev)
1419{
1420 struct i40e_netdev_priv *np = netdev_priv(netdev);
1421 struct i40e_mac_filter *f, *ftmp;
1422 struct i40e_vsi *vsi = np->vsi;
1423 struct netdev_hw_addr *uca;
1424 struct netdev_hw_addr *mca;
1425 struct netdev_hw_addr *ha;
1426
1427 /* add addr if not already in the filter list */
1428 netdev_for_each_uc_addr(uca, netdev) {
1429 if (!i40e_find_mac(vsi, uca->addr, false, true)) {
1430 if (i40e_is_vsi_in_vlan(vsi))
1431 i40e_put_mac_in_vlan(vsi, uca->addr,
1432 false, true);
1433 else
1434 i40e_add_filter(vsi, uca->addr, I40E_VLAN_ANY,
1435 false, true);
1436 }
1437 }
1438
1439 netdev_for_each_mc_addr(mca, netdev) {
1440 if (!i40e_find_mac(vsi, mca->addr, false, true)) {
1441 if (i40e_is_vsi_in_vlan(vsi))
1442 i40e_put_mac_in_vlan(vsi, mca->addr,
1443 false, true);
1444 else
1445 i40e_add_filter(vsi, mca->addr, I40E_VLAN_ANY,
1446 false, true);
1447 }
1448 }
1449
1450 /* remove filter if not in netdev list */
1451 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1452 bool found = false;
1453
1454 if (!f->is_netdev)
1455 continue;
1456
1457 if (is_multicast_ether_addr(f->macaddr)) {
1458 netdev_for_each_mc_addr(mca, netdev) {
1459 if (ether_addr_equal(mca->addr, f->macaddr)) {
1460 found = true;
1461 break;
1462 }
1463 }
1464 } else {
1465 netdev_for_each_uc_addr(uca, netdev) {
1466 if (ether_addr_equal(uca->addr, f->macaddr)) {
1467 found = true;
1468 break;
1469 }
1470 }
1471
1472 for_each_dev_addr(netdev, ha) {
1473 if (ether_addr_equal(ha->addr, f->macaddr)) {
1474 found = true;
1475 break;
1476 }
1477 }
1478 }
1479 if (!found)
1480 i40e_del_filter(
1481 vsi, f->macaddr, I40E_VLAN_ANY, false, true);
1482 }
1483
1484 /* check for other flag changes */
1485 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1486 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1487 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1488 }
1489}
1490
1491/**
1492 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1493 * @vsi: ptr to the VSI
1494 *
1495 * Push any outstanding VSI filter changes through the AdminQ.
1496 *
1497 * Returns 0 or error value
1498 **/
1499int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
1500{
1501 struct i40e_mac_filter *f, *ftmp;
1502 bool promisc_forced_on = false;
1503 bool add_happened = false;
1504 int filter_list_len = 0;
1505 u32 changed_flags = 0;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001506 i40e_status aq_ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001507 struct i40e_pf *pf;
1508 int num_add = 0;
1509 int num_del = 0;
1510 u16 cmd_flags;
1511
1512 /* empty array typed pointers, kcalloc later */
1513 struct i40e_aqc_add_macvlan_element_data *add_list;
1514 struct i40e_aqc_remove_macvlan_element_data *del_list;
1515
1516 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1517 usleep_range(1000, 2000);
1518 pf = vsi->back;
1519
1520 if (vsi->netdev) {
1521 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1522 vsi->current_netdev_flags = vsi->netdev->flags;
1523 }
1524
1525 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1526 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1527
1528 filter_list_len = pf->hw.aq.asq_buf_size /
1529 sizeof(struct i40e_aqc_remove_macvlan_element_data);
1530 del_list = kcalloc(filter_list_len,
1531 sizeof(struct i40e_aqc_remove_macvlan_element_data),
1532 GFP_KERNEL);
1533 if (!del_list)
1534 return -ENOMEM;
1535
1536 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1537 if (!f->changed)
1538 continue;
1539
1540 if (f->counter != 0)
1541 continue;
1542 f->changed = false;
1543 cmd_flags = 0;
1544
1545 /* add to delete list */
1546 memcpy(del_list[num_del].mac_addr,
1547 f->macaddr, ETH_ALEN);
1548 del_list[num_del].vlan_tag =
1549 cpu_to_le16((u16)(f->vlan ==
1550 I40E_VLAN_ANY ? 0 : f->vlan));
1551
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001552 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1553 del_list[num_del].flags = cmd_flags;
1554 num_del++;
1555
1556 /* unlink from filter list */
1557 list_del(&f->list);
1558 kfree(f);
1559
1560 /* flush a full buffer */
1561 if (num_del == filter_list_len) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001562 aq_ret = i40e_aq_remove_macvlan(&pf->hw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001563 vsi->seid, del_list, num_del,
1564 NULL);
1565 num_del = 0;
1566 memset(del_list, 0, sizeof(*del_list));
1567
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001568 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001569 dev_info(&pf->pdev->dev,
1570 "ignoring delete macvlan error, err %d, aq_err %d while flushing a full buffer\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001571 aq_ret,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001572 pf->hw.aq.asq_last_status);
1573 }
1574 }
1575 if (num_del) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001576 aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001577 del_list, num_del, NULL);
1578 num_del = 0;
1579
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001580 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001581 dev_info(&pf->pdev->dev,
1582 "ignoring delete macvlan error, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001583 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001584 }
1585
1586 kfree(del_list);
1587 del_list = NULL;
1588
1589 /* do all the adds now */
1590 filter_list_len = pf->hw.aq.asq_buf_size /
1591 sizeof(struct i40e_aqc_add_macvlan_element_data),
1592 add_list = kcalloc(filter_list_len,
1593 sizeof(struct i40e_aqc_add_macvlan_element_data),
1594 GFP_KERNEL);
1595 if (!add_list)
1596 return -ENOMEM;
1597
1598 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1599 if (!f->changed)
1600 continue;
1601
1602 if (f->counter == 0)
1603 continue;
1604 f->changed = false;
1605 add_happened = true;
1606 cmd_flags = 0;
1607
1608 /* add to add array */
1609 memcpy(add_list[num_add].mac_addr,
1610 f->macaddr, ETH_ALEN);
1611 add_list[num_add].vlan_tag =
1612 cpu_to_le16(
1613 (u16)(f->vlan == I40E_VLAN_ANY ? 0 : f->vlan));
1614 add_list[num_add].queue_number = 0;
1615
1616 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001617 add_list[num_add].flags = cpu_to_le16(cmd_flags);
1618 num_add++;
1619
1620 /* flush a full buffer */
1621 if (num_add == filter_list_len) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001622 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1623 add_list, num_add,
1624 NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001625 num_add = 0;
1626
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001627 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001628 break;
1629 memset(add_list, 0, sizeof(*add_list));
1630 }
1631 }
1632 if (num_add) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001633 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1634 add_list, num_add, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001635 num_add = 0;
1636 }
1637 kfree(add_list);
1638 add_list = NULL;
1639
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001640 if (add_happened && (!aq_ret)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001641 /* do nothing */;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001642 } else if (add_happened && (aq_ret)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001643 dev_info(&pf->pdev->dev,
1644 "add filter 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 if ((pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOSPC) &&
1647 !test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1648 &vsi->state)) {
1649 promisc_forced_on = true;
1650 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1651 &vsi->state);
1652 dev_info(&pf->pdev->dev, "promiscuous mode forced on\n");
1653 }
1654 }
1655 }
1656
1657 /* check for changes in promiscuous modes */
1658 if (changed_flags & IFF_ALLMULTI) {
1659 bool cur_multipromisc;
1660 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001661 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
1662 vsi->seid,
1663 cur_multipromisc,
1664 NULL);
1665 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001666 dev_info(&pf->pdev->dev,
1667 "set multi promisc failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001668 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001669 }
1670 if ((changed_flags & IFF_PROMISC) || promisc_forced_on) {
1671 bool cur_promisc;
1672 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
1673 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1674 &vsi->state));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001675 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(&vsi->back->hw,
1676 vsi->seid,
1677 cur_promisc, NULL);
1678 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001679 dev_info(&pf->pdev->dev,
1680 "set uni promisc failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001681 aq_ret, pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00001682 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
1683 vsi->seid,
1684 cur_promisc, NULL);
1685 if (aq_ret)
1686 dev_info(&pf->pdev->dev,
1687 "set brdcast promisc failed, err %d, aq_err %d\n",
1688 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001689 }
1690
1691 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
1692 return 0;
1693}
1694
1695/**
1696 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
1697 * @pf: board private structure
1698 **/
1699static void i40e_sync_filters_subtask(struct i40e_pf *pf)
1700{
1701 int v;
1702
1703 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
1704 return;
1705 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
1706
1707 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
1708 if (pf->vsi[v] &&
1709 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED))
1710 i40e_sync_vsi_filters(pf->vsi[v]);
1711 }
1712}
1713
1714/**
1715 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
1716 * @netdev: network interface device structure
1717 * @new_mtu: new value for maximum frame size
1718 *
1719 * Returns 0 on success, negative on failure
1720 **/
1721static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
1722{
1723 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg61a46a42014-04-23 04:50:05 +00001724 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001725 struct i40e_vsi *vsi = np->vsi;
1726
1727 /* MTU < 68 is an error and causes problems on some kernels */
1728 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
1729 return -EINVAL;
1730
1731 netdev_info(netdev, "changing MTU from %d to %d\n",
1732 netdev->mtu, new_mtu);
1733 netdev->mtu = new_mtu;
1734 if (netif_running(netdev))
1735 i40e_vsi_reinit_locked(vsi);
1736
1737 return 0;
1738}
1739
1740/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00001741 * i40e_ioctl - Access the hwtstamp interface
1742 * @netdev: network interface device structure
1743 * @ifr: interface request data
1744 * @cmd: ioctl command
1745 **/
1746int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1747{
1748 struct i40e_netdev_priv *np = netdev_priv(netdev);
1749 struct i40e_pf *pf = np->vsi->back;
1750
1751 switch (cmd) {
1752 case SIOCGHWTSTAMP:
1753 return i40e_ptp_get_ts_config(pf, ifr);
1754 case SIOCSHWTSTAMP:
1755 return i40e_ptp_set_ts_config(pf, ifr);
1756 default:
1757 return -EOPNOTSUPP;
1758 }
1759}
1760
1761/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001762 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
1763 * @vsi: the vsi being adjusted
1764 **/
1765void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
1766{
1767 struct i40e_vsi_context ctxt;
1768 i40e_status ret;
1769
1770 if ((vsi->info.valid_sections &
1771 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1772 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
1773 return; /* already enabled */
1774
1775 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1776 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1777 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
1778
1779 ctxt.seid = vsi->seid;
1780 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1781 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1782 if (ret) {
1783 dev_info(&vsi->back->pdev->dev,
1784 "%s: update vsi failed, aq_err=%d\n",
1785 __func__, vsi->back->hw.aq.asq_last_status);
1786 }
1787}
1788
1789/**
1790 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
1791 * @vsi: the vsi being adjusted
1792 **/
1793void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
1794{
1795 struct i40e_vsi_context ctxt;
1796 i40e_status ret;
1797
1798 if ((vsi->info.valid_sections &
1799 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1800 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
1801 I40E_AQ_VSI_PVLAN_EMOD_MASK))
1802 return; /* already disabled */
1803
1804 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1805 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1806 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
1807
1808 ctxt.seid = vsi->seid;
1809 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1810 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1811 if (ret) {
1812 dev_info(&vsi->back->pdev->dev,
1813 "%s: update vsi failed, aq_err=%d\n",
1814 __func__, vsi->back->hw.aq.asq_last_status);
1815 }
1816}
1817
1818/**
1819 * i40e_vlan_rx_register - Setup or shutdown vlan offload
1820 * @netdev: network interface to be adjusted
1821 * @features: netdev features to test if VLAN offload is enabled or not
1822 **/
1823static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
1824{
1825 struct i40e_netdev_priv *np = netdev_priv(netdev);
1826 struct i40e_vsi *vsi = np->vsi;
1827
1828 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1829 i40e_vlan_stripping_enable(vsi);
1830 else
1831 i40e_vlan_stripping_disable(vsi);
1832}
1833
1834/**
1835 * i40e_vsi_add_vlan - Add vsi membership for given vlan
1836 * @vsi: the vsi being configured
1837 * @vid: vlan id to be added (0 = untagged only , -1 = any)
1838 **/
1839int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
1840{
1841 struct i40e_mac_filter *f, *add_f;
1842 bool is_netdev, is_vf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001843
1844 is_vf = (vsi->type == I40E_VSI_SRIOV);
1845 is_netdev = !!(vsi->netdev);
1846
1847 if (is_netdev) {
1848 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid,
1849 is_vf, is_netdev);
1850 if (!add_f) {
1851 dev_info(&vsi->back->pdev->dev,
1852 "Could not add vlan filter %d for %pM\n",
1853 vid, vsi->netdev->dev_addr);
1854 return -ENOMEM;
1855 }
1856 }
1857
1858 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1859 add_f = i40e_add_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
1860 if (!add_f) {
1861 dev_info(&vsi->back->pdev->dev,
1862 "Could not add vlan filter %d for %pM\n",
1863 vid, f->macaddr);
1864 return -ENOMEM;
1865 }
1866 }
1867
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001868 /* Now if we add a vlan tag, make sure to check if it is the first
1869 * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
1870 * with 0, so we now accept untagged and specified tagged traffic
1871 * (and not any taged and untagged)
1872 */
1873 if (vid > 0) {
1874 if (is_netdev && i40e_find_filter(vsi, vsi->netdev->dev_addr,
1875 I40E_VLAN_ANY,
1876 is_vf, is_netdev)) {
1877 i40e_del_filter(vsi, vsi->netdev->dev_addr,
1878 I40E_VLAN_ANY, is_vf, is_netdev);
1879 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0,
1880 is_vf, is_netdev);
1881 if (!add_f) {
1882 dev_info(&vsi->back->pdev->dev,
1883 "Could not add filter 0 for %pM\n",
1884 vsi->netdev->dev_addr);
1885 return -ENOMEM;
1886 }
1887 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08001888 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001889
Greg Rose8d82a7c2014-01-13 16:13:04 -08001890 /* Do not assume that I40E_VLAN_ANY should be reset to VLAN 0 */
1891 if (vid > 0 && !vsi->info.pvid) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001892 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1893 if (i40e_find_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1894 is_vf, is_netdev)) {
1895 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1896 is_vf, is_netdev);
1897 add_f = i40e_add_filter(vsi, f->macaddr,
1898 0, is_vf, is_netdev);
1899 if (!add_f) {
1900 dev_info(&vsi->back->pdev->dev,
1901 "Could not add filter 0 for %pM\n",
1902 f->macaddr);
1903 return -ENOMEM;
1904 }
1905 }
1906 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001907 }
1908
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001909 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1910 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1911 return 0;
1912
1913 return i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001914}
1915
1916/**
1917 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
1918 * @vsi: the vsi being configured
1919 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001920 *
1921 * Return: 0 on success or negative otherwise
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001922 **/
1923int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
1924{
1925 struct net_device *netdev = vsi->netdev;
1926 struct i40e_mac_filter *f, *add_f;
1927 bool is_vf, is_netdev;
1928 int filter_count = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001929
1930 is_vf = (vsi->type == I40E_VSI_SRIOV);
1931 is_netdev = !!(netdev);
1932
1933 if (is_netdev)
1934 i40e_del_filter(vsi, netdev->dev_addr, vid, is_vf, is_netdev);
1935
1936 list_for_each_entry(f, &vsi->mac_filter_list, list)
1937 i40e_del_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
1938
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001939 /* go through all the filters for this VSI and if there is only
1940 * vid == 0 it means there are no other filters, so vid 0 must
1941 * be replaced with -1. This signifies that we should from now
1942 * on accept any traffic (with any tag present, or untagged)
1943 */
1944 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1945 if (is_netdev) {
1946 if (f->vlan &&
1947 ether_addr_equal(netdev->dev_addr, f->macaddr))
1948 filter_count++;
1949 }
1950
1951 if (f->vlan)
1952 filter_count++;
1953 }
1954
1955 if (!filter_count && is_netdev) {
1956 i40e_del_filter(vsi, netdev->dev_addr, 0, is_vf, is_netdev);
1957 f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
1958 is_vf, is_netdev);
1959 if (!f) {
1960 dev_info(&vsi->back->pdev->dev,
1961 "Could not add filter %d for %pM\n",
1962 I40E_VLAN_ANY, netdev->dev_addr);
1963 return -ENOMEM;
1964 }
1965 }
1966
1967 if (!filter_count) {
1968 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1969 i40e_del_filter(vsi, f->macaddr, 0, is_vf, is_netdev);
1970 add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1971 is_vf, is_netdev);
1972 if (!add_f) {
1973 dev_info(&vsi->back->pdev->dev,
1974 "Could not add filter %d for %pM\n",
1975 I40E_VLAN_ANY, f->macaddr);
1976 return -ENOMEM;
1977 }
1978 }
1979 }
1980
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001981 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1982 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1983 return 0;
1984
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001985 return i40e_sync_vsi_filters(vsi);
1986}
1987
1988/**
1989 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
1990 * @netdev: network interface to be adjusted
1991 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001992 *
1993 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001994 **/
1995static int i40e_vlan_rx_add_vid(struct net_device *netdev,
1996 __always_unused __be16 proto, u16 vid)
1997{
1998 struct i40e_netdev_priv *np = netdev_priv(netdev);
1999 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002000 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002001
2002 if (vid > 4095)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002003 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002004
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002005 netdev_info(netdev, "adding %pM vid=%d\n", netdev->dev_addr, vid);
2006
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002007 /* If the network stack called us with vid = 0 then
2008 * it is asking to receive priority tagged packets with
2009 * vlan id 0. Our HW receives them by default when configured
2010 * to receive untagged packets so there is no need to add an
2011 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002012 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002013 if (vid)
2014 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002015
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002016 if (!ret && (vid < VLAN_N_VID))
2017 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002018
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002019 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002020}
2021
2022/**
2023 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2024 * @netdev: network interface to be adjusted
2025 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002026 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002027 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002028 **/
2029static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2030 __always_unused __be16 proto, u16 vid)
2031{
2032 struct i40e_netdev_priv *np = netdev_priv(netdev);
2033 struct i40e_vsi *vsi = np->vsi;
2034
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002035 netdev_info(netdev, "removing %pM vid=%d\n", netdev->dev_addr, vid);
2036
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002037 /* return code is ignored as there is nothing a user
2038 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002039 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002040 */
2041 i40e_vsi_kill_vlan(vsi, vid);
2042
2043 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002044
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002045 return 0;
2046}
2047
2048/**
2049 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2050 * @vsi: the vsi being brought back up
2051 **/
2052static void i40e_restore_vlan(struct i40e_vsi *vsi)
2053{
2054 u16 vid;
2055
2056 if (!vsi->netdev)
2057 return;
2058
2059 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2060
2061 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2062 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2063 vid);
2064}
2065
2066/**
2067 * i40e_vsi_add_pvid - Add pvid for the VSI
2068 * @vsi: the vsi being adjusted
2069 * @vid: the vlan id to set as a PVID
2070 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002071int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002072{
2073 struct i40e_vsi_context ctxt;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002074 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002075
2076 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2077 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002078 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2079 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002080 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002081
2082 ctxt.seid = vsi->seid;
2083 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002084 aq_ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2085 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002086 dev_info(&vsi->back->pdev->dev,
2087 "%s: update vsi failed, aq_err=%d\n",
2088 __func__, vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002089 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002090 }
2091
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002092 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002093}
2094
2095/**
2096 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2097 * @vsi: the vsi being adjusted
2098 *
2099 * Just use the vlan_rx_register() service to put it back to normal
2100 **/
2101void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2102{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002103 i40e_vlan_stripping_disable(vsi);
2104
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002105 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002106}
2107
2108/**
2109 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2110 * @vsi: ptr to the VSI
2111 *
2112 * If this function returns with an error, then it's possible one or
2113 * more of the rings is populated (while the rest are not). It is the
2114 * callers duty to clean those orphaned rings.
2115 *
2116 * Return 0 on success, negative on failure
2117 **/
2118static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2119{
2120 int i, err = 0;
2121
2122 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002123 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002124
2125 return err;
2126}
2127
2128/**
2129 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2130 * @vsi: ptr to the VSI
2131 *
2132 * Free VSI's transmit software resources
2133 **/
2134static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2135{
2136 int i;
2137
Greg Rose8e9dca52013-12-18 13:45:53 +00002138 if (!vsi->tx_rings)
2139 return;
2140
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002141 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002142 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002143 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002144}
2145
2146/**
2147 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2148 * @vsi: ptr to the VSI
2149 *
2150 * If this function returns with an error, then it's possible one or
2151 * more of the rings is populated (while the rest are not). It is the
2152 * callers duty to clean those orphaned rings.
2153 *
2154 * Return 0 on success, negative on failure
2155 **/
2156static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2157{
2158 int i, err = 0;
2159
2160 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002161 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002162 return err;
2163}
2164
2165/**
2166 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2167 * @vsi: ptr to the VSI
2168 *
2169 * Free all receive software resources
2170 **/
2171static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2172{
2173 int i;
2174
Greg Rose8e9dca52013-12-18 13:45:53 +00002175 if (!vsi->rx_rings)
2176 return;
2177
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002178 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002179 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002180 i40e_free_rx_resources(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002181}
2182
2183/**
2184 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2185 * @ring: The Tx ring to configure
2186 *
2187 * Configure the Tx descriptor ring in the HMC context.
2188 **/
2189static int i40e_configure_tx_ring(struct i40e_ring *ring)
2190{
2191 struct i40e_vsi *vsi = ring->vsi;
2192 u16 pf_q = vsi->base_queue + ring->queue_index;
2193 struct i40e_hw *hw = &vsi->back->hw;
2194 struct i40e_hmc_obj_txq tx_ctx;
2195 i40e_status err = 0;
2196 u32 qtx_ctl = 0;
2197
2198 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002199 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002200 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2201 ring->atr_count = 0;
2202 } else {
2203 ring->atr_sample_rate = 0;
2204 }
2205
2206 /* initialize XPS */
2207 if (ring->q_vector && ring->netdev &&
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08002208 vsi->tc_config.numtc <= 1 &&
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002209 !test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
2210 netif_set_xps_queue(ring->netdev,
2211 &ring->q_vector->affinity_mask,
2212 ring->queue_index);
2213
2214 /* clear the context structure first */
2215 memset(&tx_ctx, 0, sizeof(tx_ctx));
2216
2217 tx_ctx.new_context = 1;
2218 tx_ctx.base = (ring->dma / 128);
2219 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002220 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2221 I40E_FLAG_FD_ATR_ENABLED));
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002222 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002223 /* FDIR VSI tx ring can still use RS bit and writebacks */
2224 if (vsi->type != I40E_VSI_FDIR)
2225 tx_ctx.head_wb_ena = 1;
2226 tx_ctx.head_wb_addr = ring->dma +
2227 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002228
2229 /* As part of VSI creation/update, FW allocates certain
2230 * Tx arbitration queue sets for each TC enabled for
2231 * the VSI. The FW returns the handles to these queue
2232 * sets as part of the response buffer to Add VSI,
2233 * Update VSI, etc. AQ commands. It is expected that
2234 * these queue set handles be associated with the Tx
2235 * queues by the driver as part of the TX queue context
2236 * initialization. This has to be done regardless of
2237 * DCB as by default everything is mapped to TC0.
2238 */
2239 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2240 tx_ctx.rdylist_act = 0;
2241
2242 /* clear the context in the HMC */
2243 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2244 if (err) {
2245 dev_info(&vsi->back->pdev->dev,
2246 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2247 ring->queue_index, pf_q, err);
2248 return -ENOMEM;
2249 }
2250
2251 /* set the context in the HMC */
2252 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2253 if (err) {
2254 dev_info(&vsi->back->pdev->dev,
2255 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2256 ring->queue_index, pf_q, err);
2257 return -ENOMEM;
2258 }
2259
2260 /* Now associate this queue with this PCI function */
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002261 if (vsi->type == I40E_VSI_VMDQ2)
2262 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
2263 else
2264 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Shannon Nelson13fd9772013-09-28 07:14:19 +00002265 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2266 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002267 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2268 i40e_flush(hw);
2269
2270 clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
2271
2272 /* cache tail off for easier writes later */
2273 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2274
2275 return 0;
2276}
2277
2278/**
2279 * i40e_configure_rx_ring - Configure a receive ring context
2280 * @ring: The Rx ring to configure
2281 *
2282 * Configure the Rx descriptor ring in the HMC context.
2283 **/
2284static int i40e_configure_rx_ring(struct i40e_ring *ring)
2285{
2286 struct i40e_vsi *vsi = ring->vsi;
2287 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2288 u16 pf_q = vsi->base_queue + ring->queue_index;
2289 struct i40e_hw *hw = &vsi->back->hw;
2290 struct i40e_hmc_obj_rxq rx_ctx;
2291 i40e_status err = 0;
2292
2293 ring->state = 0;
2294
2295 /* clear the context structure first */
2296 memset(&rx_ctx, 0, sizeof(rx_ctx));
2297
2298 ring->rx_buf_len = vsi->rx_buf_len;
2299 ring->rx_hdr_len = vsi->rx_hdr_len;
2300
2301 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
2302 rx_ctx.hbuff = ring->rx_hdr_len >> I40E_RXQ_CTX_HBUFF_SHIFT;
2303
2304 rx_ctx.base = (ring->dma / 128);
2305 rx_ctx.qlen = ring->count;
2306
2307 if (vsi->back->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED) {
2308 set_ring_16byte_desc_enabled(ring);
2309 rx_ctx.dsize = 0;
2310 } else {
2311 rx_ctx.dsize = 1;
2312 }
2313
2314 rx_ctx.dtype = vsi->dtype;
2315 if (vsi->dtype) {
2316 set_ring_ps_enabled(ring);
2317 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
2318 I40E_RX_SPLIT_IP |
2319 I40E_RX_SPLIT_TCP_UDP |
2320 I40E_RX_SPLIT_SCTP;
2321 } else {
2322 rx_ctx.hsplit_0 = 0;
2323 }
2324
2325 rx_ctx.rxmax = min_t(u16, vsi->max_frame,
2326 (chain_len * ring->rx_buf_len));
2327 rx_ctx.tphrdesc_ena = 1;
2328 rx_ctx.tphwdesc_ena = 1;
2329 rx_ctx.tphdata_ena = 1;
2330 rx_ctx.tphhead_ena = 1;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00002331 if (hw->revision_id == 0)
2332 rx_ctx.lrxqthresh = 0;
2333 else
2334 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002335 rx_ctx.crcstrip = 1;
2336 rx_ctx.l2tsel = 1;
2337 rx_ctx.showiv = 1;
Catherine Sullivanacb36762014-03-06 09:02:30 +00002338 /* set the prefena field to 1 because the manual says to */
2339 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002340
2341 /* clear the context in the HMC */
2342 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2343 if (err) {
2344 dev_info(&vsi->back->pdev->dev,
2345 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2346 ring->queue_index, pf_q, err);
2347 return -ENOMEM;
2348 }
2349
2350 /* set the context in the HMC */
2351 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2352 if (err) {
2353 dev_info(&vsi->back->pdev->dev,
2354 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2355 ring->queue_index, pf_q, err);
2356 return -ENOMEM;
2357 }
2358
2359 /* cache tail for quicker writes, and clear the reg before use */
2360 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2361 writel(0, ring->tail);
2362
2363 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
2364
2365 return 0;
2366}
2367
2368/**
2369 * i40e_vsi_configure_tx - Configure the VSI for Tx
2370 * @vsi: VSI structure describing this set of rings and resources
2371 *
2372 * Configure the Tx VSI for operation.
2373 **/
2374static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2375{
2376 int err = 0;
2377 u16 i;
2378
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002379 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2380 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002381
2382 return err;
2383}
2384
2385/**
2386 * i40e_vsi_configure_rx - Configure the VSI for Rx
2387 * @vsi: the VSI being configured
2388 *
2389 * Configure the Rx VSI for operation.
2390 **/
2391static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
2392{
2393 int err = 0;
2394 u16 i;
2395
2396 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
2397 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
2398 + ETH_FCS_LEN + VLAN_HLEN;
2399 else
2400 vsi->max_frame = I40E_RXBUFFER_2048;
2401
2402 /* figure out correct receive buffer length */
2403 switch (vsi->back->flags & (I40E_FLAG_RX_1BUF_ENABLED |
2404 I40E_FLAG_RX_PS_ENABLED)) {
2405 case I40E_FLAG_RX_1BUF_ENABLED:
2406 vsi->rx_hdr_len = 0;
2407 vsi->rx_buf_len = vsi->max_frame;
2408 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2409 break;
2410 case I40E_FLAG_RX_PS_ENABLED:
2411 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2412 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2413 vsi->dtype = I40E_RX_DTYPE_HEADER_SPLIT;
2414 break;
2415 default:
2416 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2417 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2418 vsi->dtype = I40E_RX_DTYPE_SPLIT_ALWAYS;
2419 break;
2420 }
2421
2422 /* round up for the chip's needs */
2423 vsi->rx_hdr_len = ALIGN(vsi->rx_hdr_len,
2424 (1 << I40E_RXQ_CTX_HBUFF_SHIFT));
2425 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
2426 (1 << I40E_RXQ_CTX_DBUFF_SHIFT));
2427
2428 /* set up individual rings */
2429 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002430 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002431
2432 return err;
2433}
2434
2435/**
2436 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
2437 * @vsi: ptr to the VSI
2438 **/
2439static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
2440{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00002441 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002442 u16 qoffset, qcount;
2443 int i, n;
2444
2445 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED))
2446 return;
2447
2448 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
2449 if (!(vsi->tc_config.enabled_tc & (1 << n)))
2450 continue;
2451
2452 qoffset = vsi->tc_config.tc_info[n].qoffset;
2453 qcount = vsi->tc_config.tc_info[n].qcount;
2454 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00002455 rx_ring = vsi->rx_rings[i];
2456 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002457 rx_ring->dcb_tc = n;
2458 tx_ring->dcb_tc = n;
2459 }
2460 }
2461}
2462
2463/**
2464 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
2465 * @vsi: ptr to the VSI
2466 **/
2467static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
2468{
2469 if (vsi->netdev)
2470 i40e_set_rx_mode(vsi->netdev);
2471}
2472
2473/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00002474 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
2475 * @vsi: Pointer to the targeted VSI
2476 *
2477 * This function replays the hlist on the hw where all the SB Flow Director
2478 * filters were saved.
2479 **/
2480static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
2481{
2482 struct i40e_fdir_filter *filter;
2483 struct i40e_pf *pf = vsi->back;
2484 struct hlist_node *node;
2485
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00002486 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
2487 return;
2488
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00002489 hlist_for_each_entry_safe(filter, node,
2490 &pf->fdir_filter_list, fdir_node) {
2491 i40e_add_del_fdir(vsi, filter, true);
2492 }
2493}
2494
2495/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002496 * i40e_vsi_configure - Set up the VSI for action
2497 * @vsi: the VSI being configured
2498 **/
2499static int i40e_vsi_configure(struct i40e_vsi *vsi)
2500{
2501 int err;
2502
2503 i40e_set_vsi_rx_mode(vsi);
2504 i40e_restore_vlan(vsi);
2505 i40e_vsi_config_dcb_rings(vsi);
2506 err = i40e_vsi_configure_tx(vsi);
2507 if (!err)
2508 err = i40e_vsi_configure_rx(vsi);
2509
2510 return err;
2511}
2512
2513/**
2514 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
2515 * @vsi: the VSI being configured
2516 **/
2517static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
2518{
2519 struct i40e_pf *pf = vsi->back;
2520 struct i40e_q_vector *q_vector;
2521 struct i40e_hw *hw = &pf->hw;
2522 u16 vector;
2523 int i, q;
2524 u32 val;
2525 u32 qp;
2526
2527 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
2528 * and PFINT_LNKLSTn registers, e.g.:
2529 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
2530 */
2531 qp = vsi->base_queue;
2532 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00002533 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
2534 q_vector = vsi->q_vectors[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002535 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2536 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2537 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
2538 q_vector->rx.itr);
2539 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2540 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2541 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
2542 q_vector->tx.itr);
2543
2544 /* Linked list for the queuepairs assigned to this vector */
2545 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
2546 for (q = 0; q < q_vector->num_ringpairs; q++) {
2547 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2548 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2549 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
2550 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
2551 (I40E_QUEUE_TYPE_TX
2552 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
2553
2554 wr32(hw, I40E_QINT_RQCTL(qp), val);
2555
2556 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2557 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2558 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
2559 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
2560 (I40E_QUEUE_TYPE_RX
2561 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2562
2563 /* Terminate the linked list */
2564 if (q == (q_vector->num_ringpairs - 1))
2565 val |= (I40E_QUEUE_END_OF_LIST
2566 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2567
2568 wr32(hw, I40E_QINT_TQCTL(qp), val);
2569 qp++;
2570 }
2571 }
2572
2573 i40e_flush(hw);
2574}
2575
2576/**
2577 * i40e_enable_misc_int_causes - enable the non-queue interrupts
2578 * @hw: ptr to the hardware info
2579 **/
2580static void i40e_enable_misc_int_causes(struct i40e_hw *hw)
2581{
2582 u32 val;
2583
2584 /* clear things first */
2585 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
2586 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
2587
2588 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
2589 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
2590 I40E_PFINT_ICR0_ENA_GRST_MASK |
2591 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
2592 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002593 I40E_PFINT_ICR0_ENA_TIMESYNC_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002594 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
2595 I40E_PFINT_ICR0_ENA_VFLR_MASK |
2596 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2597
2598 wr32(hw, I40E_PFINT_ICR0_ENA, val);
2599
2600 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00002601 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
2602 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002603
2604 /* OTHER_ITR_IDX = 0 */
2605 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
2606}
2607
2608/**
2609 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
2610 * @vsi: the VSI being configured
2611 **/
2612static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
2613{
Alexander Duyck493fb302013-09-28 07:01:44 +00002614 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002615 struct i40e_pf *pf = vsi->back;
2616 struct i40e_hw *hw = &pf->hw;
2617 u32 val;
2618
2619 /* set the ITR configuration */
2620 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2621 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2622 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
2623 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2624 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2625 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
2626
2627 i40e_enable_misc_int_causes(hw);
2628
2629 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
2630 wr32(hw, I40E_PFINT_LNKLST0, 0);
2631
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00002632 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002633 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2634 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2635 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2636
2637 wr32(hw, I40E_QINT_RQCTL(0), val);
2638
2639 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2640 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2641 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2642
2643 wr32(hw, I40E_QINT_TQCTL(0), val);
2644 i40e_flush(hw);
2645}
2646
2647/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00002648 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
2649 * @pf: board private structure
2650 **/
2651void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
2652{
2653 struct i40e_hw *hw = &pf->hw;
2654
2655 wr32(hw, I40E_PFINT_DYN_CTL0,
2656 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2657 i40e_flush(hw);
2658}
2659
2660/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002661 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
2662 * @pf: board private structure
2663 **/
Shannon Nelson116a57d2013-09-28 07:13:59 +00002664void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002665{
2666 struct i40e_hw *hw = &pf->hw;
2667 u32 val;
2668
2669 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
2670 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
2671 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
2672
2673 wr32(hw, I40E_PFINT_DYN_CTL0, val);
2674 i40e_flush(hw);
2675}
2676
2677/**
2678 * i40e_irq_dynamic_enable - Enable default interrupt generation settings
2679 * @vsi: pointer to a vsi
2680 * @vector: enable a particular Hw Interrupt vector
2681 **/
2682void i40e_irq_dynamic_enable(struct i40e_vsi *vsi, int vector)
2683{
2684 struct i40e_pf *pf = vsi->back;
2685 struct i40e_hw *hw = &pf->hw;
2686 u32 val;
2687
2688 val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
2689 I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
2690 (I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2691 wr32(hw, I40E_PFINT_DYN_CTLN(vector - 1), val);
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00002692 /* skip the flush */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002693}
2694
2695/**
2696 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
2697 * @irq: interrupt number
2698 * @data: pointer to a q_vector
2699 **/
2700static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
2701{
2702 struct i40e_q_vector *q_vector = data;
2703
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002704 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002705 return IRQ_HANDLED;
2706
2707 napi_schedule(&q_vector->napi);
2708
2709 return IRQ_HANDLED;
2710}
2711
2712/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002713 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
2714 * @vsi: the VSI being configured
2715 * @basename: name for the vector
2716 *
2717 * Allocates MSI-X vectors and requests interrupts from the kernel.
2718 **/
2719static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
2720{
2721 int q_vectors = vsi->num_q_vectors;
2722 struct i40e_pf *pf = vsi->back;
2723 int base = vsi->base_vector;
2724 int rx_int_idx = 0;
2725 int tx_int_idx = 0;
2726 int vector, err;
2727
2728 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00002729 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002730
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002731 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002732 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2733 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
2734 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002735 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002736 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2737 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002738 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002739 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2740 "%s-%s-%d", basename, "tx", tx_int_idx++);
2741 } else {
2742 /* skip this unused q_vector */
2743 continue;
2744 }
2745 err = request_irq(pf->msix_entries[base + vector].vector,
2746 vsi->irq_handler,
2747 0,
2748 q_vector->name,
2749 q_vector);
2750 if (err) {
2751 dev_info(&pf->pdev->dev,
2752 "%s: request_irq failed, error: %d\n",
2753 __func__, err);
2754 goto free_queue_irqs;
2755 }
2756 /* assign the mask for this irq */
2757 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2758 &q_vector->affinity_mask);
2759 }
2760
2761 return 0;
2762
2763free_queue_irqs:
2764 while (vector) {
2765 vector--;
2766 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2767 NULL);
2768 free_irq(pf->msix_entries[base + vector].vector,
2769 &(vsi->q_vectors[vector]));
2770 }
2771 return err;
2772}
2773
2774/**
2775 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
2776 * @vsi: the VSI being un-configured
2777 **/
2778static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
2779{
2780 struct i40e_pf *pf = vsi->back;
2781 struct i40e_hw *hw = &pf->hw;
2782 int base = vsi->base_vector;
2783 int i;
2784
2785 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002786 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
2787 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002788 }
2789
2790 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2791 for (i = vsi->base_vector;
2792 i < (vsi->num_q_vectors + vsi->base_vector); i++)
2793 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
2794
2795 i40e_flush(hw);
2796 for (i = 0; i < vsi->num_q_vectors; i++)
2797 synchronize_irq(pf->msix_entries[i + base].vector);
2798 } else {
2799 /* Legacy and MSI mode - this stops all interrupt handling */
2800 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
2801 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
2802 i40e_flush(hw);
2803 synchronize_irq(pf->pdev->irq);
2804 }
2805}
2806
2807/**
2808 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
2809 * @vsi: the VSI being configured
2810 **/
2811static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
2812{
2813 struct i40e_pf *pf = vsi->back;
2814 int i;
2815
2816 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2817 for (i = vsi->base_vector;
2818 i < (vsi->num_q_vectors + vsi->base_vector); i++)
2819 i40e_irq_dynamic_enable(vsi, i);
2820 } else {
2821 i40e_irq_dynamic_enable_icr0(pf);
2822 }
2823
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00002824 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002825 return 0;
2826}
2827
2828/**
2829 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
2830 * @pf: board private structure
2831 **/
2832static void i40e_stop_misc_vector(struct i40e_pf *pf)
2833{
2834 /* Disable ICR 0 */
2835 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
2836 i40e_flush(&pf->hw);
2837}
2838
2839/**
2840 * i40e_intr - MSI/Legacy and non-queue interrupt handler
2841 * @irq: interrupt number
2842 * @data: pointer to a q_vector
2843 *
2844 * This is the handler used for all MSI/Legacy interrupts, and deals
2845 * with both queue and non-queue interrupts. This is also used in
2846 * MSIX mode to handle the non-queue interrupts.
2847 **/
2848static irqreturn_t i40e_intr(int irq, void *data)
2849{
2850 struct i40e_pf *pf = (struct i40e_pf *)data;
2851 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002852 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002853 u32 icr0, icr0_remaining;
2854 u32 val, ena_mask;
2855
2856 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002857 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002858
Shannon Nelson116a57d2013-09-28 07:13:59 +00002859 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
2860 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002861 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002862
Shannon Nelsoncd92e722013-11-16 10:00:44 +00002863 /* if interrupt but no bits showing, must be SWINT */
2864 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
2865 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
2866 pf->sw_int_count++;
2867
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002868 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
2869 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
2870
2871 /* temporarily disable queue cause for NAPI processing */
2872 u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
2873 qval &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
2874 wr32(hw, I40E_QINT_RQCTL(0), qval);
2875
2876 qval = rd32(hw, I40E_QINT_TQCTL(0));
2877 qval &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
2878 wr32(hw, I40E_QINT_TQCTL(0), qval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002879
2880 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck493fb302013-09-28 07:01:44 +00002881 napi_schedule(&pf->vsi[pf->lan_vsi]->q_vectors[0]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002882 }
2883
2884 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
2885 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2886 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
2887 }
2888
2889 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
2890 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
2891 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
2892 }
2893
2894 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
2895 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
2896 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
2897 }
2898
2899 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
2900 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
2901 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
2902 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
2903 val = rd32(hw, I40E_GLGEN_RSTAT);
2904 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
2905 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002906 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002907 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002908 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002909 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002910 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002911 pf->empr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002912 set_bit(__I40E_EMP_RESET_REQUESTED, &pf->state);
2913 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002914 }
2915
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002916 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
2917 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
2918 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
2919 }
2920
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002921 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
2922 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
2923
2924 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07002925 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002926 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002927 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002928 }
2929
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002930 /* If a critical error is pending we have no choice but to reset the
2931 * device.
2932 * Report and mask out any remaining unexpected interrupts.
2933 */
2934 icr0_remaining = icr0 & ena_mask;
2935 if (icr0_remaining) {
2936 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
2937 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002938 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002939 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00002940 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002941 dev_info(&pf->pdev->dev, "device will be reset\n");
2942 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
2943 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002944 }
2945 ena_mask &= ~icr0_remaining;
2946 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002947 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002948
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002949enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002950 /* re-enable interrupt causes */
2951 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002952 if (!test_bit(__I40E_DOWN, &pf->state)) {
2953 i40e_service_event_schedule(pf);
2954 i40e_irq_dynamic_enable_icr0(pf);
2955 }
2956
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002957 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002958}
2959
2960/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08002961 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
2962 * @tx_ring: tx ring to clean
2963 * @budget: how many cleans we're allowed
2964 *
2965 * Returns true if there's any budget left (e.g. the clean is finished)
2966 **/
2967static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
2968{
2969 struct i40e_vsi *vsi = tx_ring->vsi;
2970 u16 i = tx_ring->next_to_clean;
2971 struct i40e_tx_buffer *tx_buf;
2972 struct i40e_tx_desc *tx_desc;
2973
2974 tx_buf = &tx_ring->tx_bi[i];
2975 tx_desc = I40E_TX_DESC(tx_ring, i);
2976 i -= tx_ring->count;
2977
2978 do {
2979 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
2980
2981 /* if next_to_watch is not set then there is no work pending */
2982 if (!eop_desc)
2983 break;
2984
2985 /* prevent any other reads prior to eop_desc */
2986 read_barrier_depends();
2987
2988 /* if the descriptor isn't done, no work yet to do */
2989 if (!(eop_desc->cmd_type_offset_bsz &
2990 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
2991 break;
2992
2993 /* clear next_to_watch to prevent false hangs */
2994 tx_buf->next_to_watch = NULL;
2995
2996 /* unmap skb header data */
2997 dma_unmap_single(tx_ring->dev,
2998 dma_unmap_addr(tx_buf, dma),
2999 dma_unmap_len(tx_buf, len),
3000 DMA_TO_DEVICE);
3001
3002 dma_unmap_len_set(tx_buf, len, 0);
3003
3004
3005 /* move to the next desc and buffer to clean */
3006 tx_buf++;
3007 tx_desc++;
3008 i++;
3009 if (unlikely(!i)) {
3010 i -= tx_ring->count;
3011 tx_buf = tx_ring->tx_bi;
3012 tx_desc = I40E_TX_DESC(tx_ring, 0);
3013 }
3014
3015 /* update budget accounting */
3016 budget--;
3017 } while (likely(budget));
3018
3019 i += tx_ring->count;
3020 tx_ring->next_to_clean = i;
3021
3022 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
3023 i40e_irq_dynamic_enable(vsi,
3024 tx_ring->q_vector->v_idx + vsi->base_vector);
3025 }
3026 return budget > 0;
3027}
3028
3029/**
3030 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3031 * @irq: interrupt number
3032 * @data: pointer to a q_vector
3033 **/
3034static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3035{
3036 struct i40e_q_vector *q_vector = data;
3037 struct i40e_vsi *vsi;
3038
3039 if (!q_vector->tx.ring)
3040 return IRQ_HANDLED;
3041
3042 vsi = q_vector->tx.ring->vsi;
3043 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3044
3045 return IRQ_HANDLED;
3046}
3047
3048/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003049 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003050 * @vsi: the VSI being configured
3051 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003052 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003053 **/
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003054static void map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003055{
Alexander Duyck493fb302013-09-28 07:01:44 +00003056 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00003057 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3058 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003059
3060 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003061 tx_ring->next = q_vector->tx.ring;
3062 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003063 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003064
3065 rx_ring->q_vector = q_vector;
3066 rx_ring->next = q_vector->rx.ring;
3067 q_vector->rx.ring = rx_ring;
3068 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003069}
3070
3071/**
3072 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3073 * @vsi: the VSI being configured
3074 *
3075 * This function maps descriptor rings to the queue-specific vectors
3076 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3077 * one vector per queue pair, but on a constrained vector budget, we
3078 * group the queue pairs as "efficiently" as possible.
3079 **/
3080static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3081{
3082 int qp_remaining = vsi->num_queue_pairs;
3083 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003084 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003085 int v_start = 0;
3086 int qp_idx = 0;
3087
3088 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3089 * group them so there are multiple queues per vector.
3090 */
3091 for (; v_start < q_vectors && qp_remaining; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003092 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3093
3094 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3095
3096 q_vector->num_ringpairs = num_ringpairs;
3097
3098 q_vector->rx.count = 0;
3099 q_vector->tx.count = 0;
3100 q_vector->rx.ring = NULL;
3101 q_vector->tx.ring = NULL;
3102
3103 while (num_ringpairs--) {
3104 map_vector_to_qp(vsi, v_start, qp_idx);
3105 qp_idx++;
3106 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003107 }
3108 }
3109}
3110
3111/**
3112 * i40e_vsi_request_irq - Request IRQ from the OS
3113 * @vsi: the VSI being configured
3114 * @basename: name for the vector
3115 **/
3116static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3117{
3118 struct i40e_pf *pf = vsi->back;
3119 int err;
3120
3121 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3122 err = i40e_vsi_request_irq_msix(vsi, basename);
3123 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3124 err = request_irq(pf->pdev->irq, i40e_intr, 0,
3125 pf->misc_int_name, pf);
3126 else
3127 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
3128 pf->misc_int_name, pf);
3129
3130 if (err)
3131 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3132
3133 return err;
3134}
3135
3136#ifdef CONFIG_NET_POLL_CONTROLLER
3137/**
3138 * i40e_netpoll - A Polling 'interrupt'handler
3139 * @netdev: network interface device structure
3140 *
3141 * This is used by netconsole to send skbs without having to re-enable
3142 * interrupts. It's not called while the normal interrupt routine is executing.
3143 **/
3144static void i40e_netpoll(struct net_device *netdev)
3145{
3146 struct i40e_netdev_priv *np = netdev_priv(netdev);
3147 struct i40e_vsi *vsi = np->vsi;
3148 struct i40e_pf *pf = vsi->back;
3149 int i;
3150
3151 /* if interface is down do nothing */
3152 if (test_bit(__I40E_DOWN, &vsi->state))
3153 return;
3154
3155 pf->flags |= I40E_FLAG_IN_NETPOLL;
3156 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3157 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003158 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003159 } else {
3160 i40e_intr(pf->pdev->irq, netdev);
3161 }
3162 pf->flags &= ~I40E_FLAG_IN_NETPOLL;
3163}
3164#endif
3165
3166/**
3167 * i40e_vsi_control_tx - Start or stop a VSI's rings
3168 * @vsi: the VSI being configured
3169 * @enable: start or stop the rings
3170 **/
3171static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3172{
3173 struct i40e_pf *pf = vsi->back;
3174 struct i40e_hw *hw = &pf->hw;
3175 int i, j, pf_q;
3176 u32 tx_reg;
3177
3178 pf_q = vsi->base_queue;
3179 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003180 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003181 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003182 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3183 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3184 break;
3185 usleep_range(1000, 2000);
3186 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003187 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c122002014-03-14 07:32:29 +00003188 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00003189 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003190
3191 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003192 if (enable) {
3193 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003194 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003195 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003196 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003197 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003198
3199 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
3200
3201 /* wait for the change to finish */
3202 for (j = 0; j < 10; j++) {
3203 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Catherine Sullivan7c122002014-03-14 07:32:29 +00003204 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3205 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003206
3207 udelay(10);
3208 }
3209 if (j >= 10) {
3210 dev_info(&pf->pdev->dev, "Tx ring %d %sable timeout\n",
3211 pf_q, (enable ? "en" : "dis"));
3212 return -ETIMEDOUT;
3213 }
3214 }
3215
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003216 if (hw->revision_id == 0)
3217 mdelay(50);
3218
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003219 return 0;
3220}
3221
3222/**
3223 * i40e_vsi_control_rx - Start or stop a VSI's rings
3224 * @vsi: the VSI being configured
3225 * @enable: start or stop the rings
3226 **/
3227static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3228{
3229 struct i40e_pf *pf = vsi->back;
3230 struct i40e_hw *hw = &pf->hw;
3231 int i, j, pf_q;
3232 u32 rx_reg;
3233
3234 pf_q = vsi->base_queue;
3235 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003236 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003237 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003238 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
3239 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
3240 break;
3241 usleep_range(1000, 2000);
3242 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003243
Catherine Sullivan7c122002014-03-14 07:32:29 +00003244 /* Skip if the queue is already in the requested state */
3245 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3246 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003247
3248 /* turn on/off the queue */
3249 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08003250 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003251 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08003252 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003253 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
3254
3255 /* wait for the change to finish */
3256 for (j = 0; j < 10; j++) {
3257 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
3258
Catherine Sullivan7c122002014-03-14 07:32:29 +00003259 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3260 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003261
3262 udelay(10);
3263 }
3264 if (j >= 10) {
3265 dev_info(&pf->pdev->dev, "Rx ring %d %sable timeout\n",
3266 pf_q, (enable ? "en" : "dis"));
3267 return -ETIMEDOUT;
3268 }
3269 }
3270
3271 return 0;
3272}
3273
3274/**
3275 * i40e_vsi_control_rings - Start or stop a VSI's rings
3276 * @vsi: the VSI being configured
3277 * @enable: start or stop the rings
3278 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00003279int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003280{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003281 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003282
3283 /* do rx first for enable and last for disable */
3284 if (request) {
3285 ret = i40e_vsi_control_rx(vsi, request);
3286 if (ret)
3287 return ret;
3288 ret = i40e_vsi_control_tx(vsi, request);
3289 } else {
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003290 /* Ignore return value, we need to shutdown whatever we can */
3291 i40e_vsi_control_tx(vsi, request);
3292 i40e_vsi_control_rx(vsi, request);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003293 }
3294
3295 return ret;
3296}
3297
3298/**
3299 * i40e_vsi_free_irq - Free the irq association with the OS
3300 * @vsi: the VSI being configured
3301 **/
3302static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
3303{
3304 struct i40e_pf *pf = vsi->back;
3305 struct i40e_hw *hw = &pf->hw;
3306 int base = vsi->base_vector;
3307 u32 val, qp;
3308 int i;
3309
3310 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3311 if (!vsi->q_vectors)
3312 return;
3313
3314 for (i = 0; i < vsi->num_q_vectors; i++) {
3315 u16 vector = i + base;
3316
3317 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00003318 if (!vsi->q_vectors[i] ||
3319 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003320 continue;
3321
3322 /* clear the affinity_mask in the IRQ descriptor */
3323 irq_set_affinity_hint(pf->msix_entries[vector].vector,
3324 NULL);
3325 free_irq(pf->msix_entries[vector].vector,
Alexander Duyck493fb302013-09-28 07:01:44 +00003326 vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003327
3328 /* Tear down the interrupt queue link list
3329 *
3330 * We know that they come in pairs and always
3331 * the Rx first, then the Tx. To clear the
3332 * link list, stick the EOL value into the
3333 * next_q field of the registers.
3334 */
3335 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
3336 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3337 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3338 val |= I40E_QUEUE_END_OF_LIST
3339 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3340 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
3341
3342 while (qp != I40E_QUEUE_END_OF_LIST) {
3343 u32 next;
3344
3345 val = rd32(hw, I40E_QINT_RQCTL(qp));
3346
3347 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3348 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3349 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3350 I40E_QINT_RQCTL_INTEVENT_MASK);
3351
3352 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3353 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3354
3355 wr32(hw, I40E_QINT_RQCTL(qp), val);
3356
3357 val = rd32(hw, I40E_QINT_TQCTL(qp));
3358
3359 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
3360 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
3361
3362 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3363 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3364 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3365 I40E_QINT_TQCTL_INTEVENT_MASK);
3366
3367 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3368 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3369
3370 wr32(hw, I40E_QINT_TQCTL(qp), val);
3371 qp = next;
3372 }
3373 }
3374 } else {
3375 free_irq(pf->pdev->irq, pf);
3376
3377 val = rd32(hw, I40E_PFINT_LNKLST0);
3378 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3379 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3380 val |= I40E_QUEUE_END_OF_LIST
3381 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
3382 wr32(hw, I40E_PFINT_LNKLST0, val);
3383
3384 val = rd32(hw, I40E_QINT_RQCTL(qp));
3385 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3386 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3387 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3388 I40E_QINT_RQCTL_INTEVENT_MASK);
3389
3390 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3391 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3392
3393 wr32(hw, I40E_QINT_RQCTL(qp), val);
3394
3395 val = rd32(hw, I40E_QINT_TQCTL(qp));
3396
3397 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3398 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3399 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3400 I40E_QINT_TQCTL_INTEVENT_MASK);
3401
3402 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3403 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3404
3405 wr32(hw, I40E_QINT_TQCTL(qp), val);
3406 }
3407}
3408
3409/**
Alexander Duyck493fb302013-09-28 07:01:44 +00003410 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
3411 * @vsi: the VSI being configured
3412 * @v_idx: Index of vector to be freed
3413 *
3414 * This function frees the memory allocated to the q_vector. In addition if
3415 * NAPI is enabled it will delete any references to the NAPI struct prior
3416 * to freeing the q_vector.
3417 **/
3418static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
3419{
3420 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003421 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00003422
3423 if (!q_vector)
3424 return;
3425
3426 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003427 i40e_for_each_ring(ring, q_vector->tx)
3428 ring->q_vector = NULL;
3429
3430 i40e_for_each_ring(ring, q_vector->rx)
3431 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00003432
3433 /* only VSI w/ an associated netdev is set up w/ NAPI */
3434 if (vsi->netdev)
3435 netif_napi_del(&q_vector->napi);
3436
3437 vsi->q_vectors[v_idx] = NULL;
3438
3439 kfree_rcu(q_vector, rcu);
3440}
3441
3442/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003443 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
3444 * @vsi: the VSI being un-configured
3445 *
3446 * This frees the memory allocated to the q_vectors and
3447 * deletes references to the NAPI struct.
3448 **/
3449static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
3450{
3451 int v_idx;
3452
Alexander Duyck493fb302013-09-28 07:01:44 +00003453 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
3454 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003455}
3456
3457/**
3458 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
3459 * @pf: board private structure
3460 **/
3461static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
3462{
3463 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
3464 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3465 pci_disable_msix(pf->pdev);
3466 kfree(pf->msix_entries);
3467 pf->msix_entries = NULL;
3468 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
3469 pci_disable_msi(pf->pdev);
3470 }
3471 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
3472}
3473
3474/**
3475 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
3476 * @pf: board private structure
3477 *
3478 * We go through and clear interrupt specific resources and reset the structure
3479 * to pre-load conditions
3480 **/
3481static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
3482{
3483 int i;
3484
3485 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
3486 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
3487 if (pf->vsi[i])
3488 i40e_vsi_free_q_vectors(pf->vsi[i]);
3489 i40e_reset_interrupt_capability(pf);
3490}
3491
3492/**
3493 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
3494 * @vsi: the VSI being configured
3495 **/
3496static void i40e_napi_enable_all(struct i40e_vsi *vsi)
3497{
3498 int q_idx;
3499
3500 if (!vsi->netdev)
3501 return;
3502
3503 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003504 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003505}
3506
3507/**
3508 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
3509 * @vsi: the VSI being configured
3510 **/
3511static void i40e_napi_disable_all(struct i40e_vsi *vsi)
3512{
3513 int q_idx;
3514
3515 if (!vsi->netdev)
3516 return;
3517
3518 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003519 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003520}
3521
3522/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00003523 * i40e_vsi_close - Shut down a VSI
3524 * @vsi: the vsi to be quelled
3525 **/
3526static void i40e_vsi_close(struct i40e_vsi *vsi)
3527{
3528 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
3529 i40e_down(vsi);
3530 i40e_vsi_free_irq(vsi);
3531 i40e_vsi_free_tx_resources(vsi);
3532 i40e_vsi_free_rx_resources(vsi);
3533}
3534
3535/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003536 * i40e_quiesce_vsi - Pause a given VSI
3537 * @vsi: the VSI being paused
3538 **/
3539static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
3540{
3541 if (test_bit(__I40E_DOWN, &vsi->state))
3542 return;
3543
3544 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
3545 if (vsi->netdev && netif_running(vsi->netdev)) {
3546 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
3547 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00003548 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003549 }
3550}
3551
3552/**
3553 * i40e_unquiesce_vsi - Resume a given VSI
3554 * @vsi: the VSI being resumed
3555 **/
3556static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
3557{
3558 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
3559 return;
3560
3561 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
3562 if (vsi->netdev && netif_running(vsi->netdev))
3563 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
3564 else
Shannon Nelson8276f752014-03-14 07:32:27 +00003565 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003566}
3567
3568/**
3569 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
3570 * @pf: the PF
3571 **/
3572static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
3573{
3574 int v;
3575
3576 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
3577 if (pf->vsi[v])
3578 i40e_quiesce_vsi(pf->vsi[v]);
3579 }
3580}
3581
3582/**
3583 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
3584 * @pf: the PF
3585 **/
3586static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
3587{
3588 int v;
3589
3590 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
3591 if (pf->vsi[v])
3592 i40e_unquiesce_vsi(pf->vsi[v]);
3593 }
3594}
3595
3596/**
3597 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
3598 * @dcbcfg: the corresponding DCBx configuration structure
3599 *
3600 * Return the number of TCs from given DCBx configuration
3601 **/
3602static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
3603{
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003604 u8 num_tc = 0;
3605 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003606
3607 /* Scan the ETS Config Priority Table to find
3608 * traffic class enabled for a given priority
3609 * and use the traffic class index to get the
3610 * number of traffic classes enabled
3611 */
3612 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3613 if (dcbcfg->etscfg.prioritytable[i] > num_tc)
3614 num_tc = dcbcfg->etscfg.prioritytable[i];
3615 }
3616
3617 /* Traffic class index starts from zero so
3618 * increment to return the actual count
3619 */
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003620 return num_tc + 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003621}
3622
3623/**
3624 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
3625 * @dcbcfg: the corresponding DCBx configuration structure
3626 *
3627 * Query the current DCB configuration and return the number of
3628 * traffic classes enabled from the given DCBX config
3629 **/
3630static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
3631{
3632 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
3633 u8 enabled_tc = 1;
3634 u8 i;
3635
3636 for (i = 0; i < num_tc; i++)
3637 enabled_tc |= 1 << i;
3638
3639 return enabled_tc;
3640}
3641
3642/**
3643 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
3644 * @pf: PF being queried
3645 *
3646 * Return number of traffic classes enabled for the given PF
3647 **/
3648static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
3649{
3650 struct i40e_hw *hw = &pf->hw;
3651 u8 i, enabled_tc;
3652 u8 num_tc = 0;
3653 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3654
3655 /* If DCB is not enabled then always in single TC */
3656 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3657 return 1;
3658
3659 /* MFP mode return count of enabled TCs for this PF */
3660 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
3661 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3662 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3663 if (enabled_tc & (1 << i))
3664 num_tc++;
3665 }
3666 return num_tc;
3667 }
3668
3669 /* SFP mode will be enabled for all TCs on port */
3670 return i40e_dcb_get_num_tc(dcbcfg);
3671}
3672
3673/**
3674 * i40e_pf_get_default_tc - Get bitmap for first enabled TC
3675 * @pf: PF being queried
3676 *
3677 * Return a bitmap for first enabled traffic class for this PF.
3678 **/
3679static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
3680{
3681 u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3682 u8 i = 0;
3683
3684 if (!enabled_tc)
3685 return 0x1; /* TC0 */
3686
3687 /* Find the first enabled TC */
3688 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3689 if (enabled_tc & (1 << i))
3690 break;
3691 }
3692
3693 return 1 << i;
3694}
3695
3696/**
3697 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
3698 * @pf: PF being queried
3699 *
3700 * Return a bitmap for enabled traffic classes for this PF.
3701 **/
3702static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
3703{
3704 /* If DCB is not enabled for this PF then just return default TC */
3705 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3706 return i40e_pf_get_default_tc(pf);
3707
3708 /* MFP mode will have enabled TCs set by FW */
3709 if (pf->flags & I40E_FLAG_MFP_ENABLED)
3710 return pf->hw.func_caps.enabled_tcmap;
3711
3712 /* SFP mode we want PF to be enabled for all TCs */
3713 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
3714}
3715
3716/**
3717 * i40e_vsi_get_bw_info - Query VSI BW Information
3718 * @vsi: the VSI being queried
3719 *
3720 * Returns 0 on success, negative value on failure
3721 **/
3722static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
3723{
3724 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
3725 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
3726 struct i40e_pf *pf = vsi->back;
3727 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003728 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003729 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003730 int i;
3731
3732 /* Get the VSI level BW configuration */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003733 aq_ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
3734 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003735 dev_info(&pf->pdev->dev,
3736 "couldn't get pf vsi bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003737 aq_ret, pf->hw.aq.asq_last_status);
3738 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003739 }
3740
3741 /* Get the VSI level BW configuration per TC */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003742 aq_ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
Jesse Brandeburg6838b532014-01-14 00:49:52 -08003743 NULL);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003744 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003745 dev_info(&pf->pdev->dev,
3746 "couldn't get pf vsi ets bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003747 aq_ret, pf->hw.aq.asq_last_status);
3748 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003749 }
3750
3751 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
3752 dev_info(&pf->pdev->dev,
3753 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
3754 bw_config.tc_valid_bits,
3755 bw_ets_config.tc_valid_bits);
3756 /* Still continuing */
3757 }
3758
3759 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
3760 vsi->bw_max_quanta = bw_config.max_bw;
3761 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
3762 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
3763 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3764 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
3765 vsi->bw_ets_limit_credits[i] =
3766 le16_to_cpu(bw_ets_config.credits[i]);
3767 /* 3 bits out of 4 for each TC */
3768 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
3769 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003770
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003771 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003772}
3773
3774/**
3775 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
3776 * @vsi: the VSI being configured
3777 * @enabled_tc: TC bitmap
3778 * @bw_credits: BW shared credits per TC
3779 *
3780 * Returns 0 on success, negative value on failure
3781 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003782static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003783 u8 *bw_share)
3784{
3785 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003786 i40e_status aq_ret;
3787 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003788
3789 bw_data.tc_valid_bits = enabled_tc;
3790 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3791 bw_data.tc_bw_credits[i] = bw_share[i];
3792
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003793 aq_ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
3794 NULL);
3795 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003796 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00003797 "AQ command Config VSI BW allocation per TC failed = %d\n",
3798 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003799 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003800 }
3801
3802 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3803 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
3804
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003805 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003806}
3807
3808/**
3809 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
3810 * @vsi: the VSI being configured
3811 * @enabled_tc: TC map to be enabled
3812 *
3813 **/
3814static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3815{
3816 struct net_device *netdev = vsi->netdev;
3817 struct i40e_pf *pf = vsi->back;
3818 struct i40e_hw *hw = &pf->hw;
3819 u8 netdev_tc = 0;
3820 int i;
3821 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3822
3823 if (!netdev)
3824 return;
3825
3826 if (!enabled_tc) {
3827 netdev_reset_tc(netdev);
3828 return;
3829 }
3830
3831 /* Set up actual enabled TCs on the VSI */
3832 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
3833 return;
3834
3835 /* set per TC queues for the VSI */
3836 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3837 /* Only set TC queues for enabled tcs
3838 *
3839 * e.g. For a VSI that has TC0 and TC3 enabled the
3840 * enabled_tc bitmap would be 0x00001001; the driver
3841 * will set the numtc for netdev as 2 that will be
3842 * referenced by the netdev layer as TC 0 and 1.
3843 */
3844 if (vsi->tc_config.enabled_tc & (1 << i))
3845 netdev_set_tc_queue(netdev,
3846 vsi->tc_config.tc_info[i].netdev_tc,
3847 vsi->tc_config.tc_info[i].qcount,
3848 vsi->tc_config.tc_info[i].qoffset);
3849 }
3850
3851 /* Assign UP2TC map for the VSI */
3852 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3853 /* Get the actual TC# for the UP */
3854 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
3855 /* Get the mapped netdev TC# for the UP */
3856 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
3857 netdev_set_prio_tc_map(netdev, i, netdev_tc);
3858 }
3859}
3860
3861/**
3862 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
3863 * @vsi: the VSI being configured
3864 * @ctxt: the ctxt buffer returned from AQ VSI update param command
3865 **/
3866static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
3867 struct i40e_vsi_context *ctxt)
3868{
3869 /* copy just the sections touched not the entire info
3870 * since not all sections are valid as returned by
3871 * update vsi params
3872 */
3873 vsi->info.mapping_flags = ctxt->info.mapping_flags;
3874 memcpy(&vsi->info.queue_mapping,
3875 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
3876 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
3877 sizeof(vsi->info.tc_mapping));
3878}
3879
3880/**
3881 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
3882 * @vsi: VSI to be configured
3883 * @enabled_tc: TC bitmap
3884 *
3885 * This configures a particular VSI for TCs that are mapped to the
3886 * given TC bitmap. It uses default bandwidth share for TCs across
3887 * VSIs to configure TC for a particular VSI.
3888 *
3889 * NOTE:
3890 * It is expected that the VSI queues have been quisced before calling
3891 * this function.
3892 **/
3893static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3894{
3895 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
3896 struct i40e_vsi_context ctxt;
3897 int ret = 0;
3898 int i;
3899
3900 /* Check if enabled_tc is same as existing or new TCs */
3901 if (vsi->tc_config.enabled_tc == enabled_tc)
3902 return ret;
3903
3904 /* Enable ETS TCs with equal BW Share for now across all VSIs */
3905 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3906 if (enabled_tc & (1 << i))
3907 bw_share[i] = 1;
3908 }
3909
3910 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
3911 if (ret) {
3912 dev_info(&vsi->back->pdev->dev,
3913 "Failed configuring TC map %d for VSI %d\n",
3914 enabled_tc, vsi->seid);
3915 goto out;
3916 }
3917
3918 /* Update Queue Pairs Mapping for currently enabled UPs */
3919 ctxt.seid = vsi->seid;
3920 ctxt.pf_num = vsi->back->hw.pf_id;
3921 ctxt.vf_num = 0;
3922 ctxt.uplink_seid = vsi->uplink_seid;
3923 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
3924 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
3925
3926 /* Update the VSI after updating the VSI queue-mapping information */
3927 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3928 if (ret) {
3929 dev_info(&vsi->back->pdev->dev,
3930 "update vsi failed, aq_err=%d\n",
3931 vsi->back->hw.aq.asq_last_status);
3932 goto out;
3933 }
3934 /* update the local VSI info with updated queue map */
3935 i40e_vsi_update_queue_map(vsi, &ctxt);
3936 vsi->info.valid_sections = 0;
3937
3938 /* Update current VSI BW information */
3939 ret = i40e_vsi_get_bw_info(vsi);
3940 if (ret) {
3941 dev_info(&vsi->back->pdev->dev,
3942 "Failed updating vsi bw info, aq_err=%d\n",
3943 vsi->back->hw.aq.asq_last_status);
3944 goto out;
3945 }
3946
3947 /* Update the netdev TC setup */
3948 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
3949out:
3950 return ret;
3951}
3952
3953/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08003954 * i40e_veb_config_tc - Configure TCs for given VEB
3955 * @veb: given VEB
3956 * @enabled_tc: TC bitmap
3957 *
3958 * Configures given TC bitmap for VEB (switching) element
3959 **/
3960int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
3961{
3962 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
3963 struct i40e_pf *pf = veb->pf;
3964 int ret = 0;
3965 int i;
3966
3967 /* No TCs or already enabled TCs just return */
3968 if (!enabled_tc || veb->enabled_tc == enabled_tc)
3969 return ret;
3970
3971 bw_data.tc_valid_bits = enabled_tc;
3972 /* bw_data.absolute_credits is not set (relative) */
3973
3974 /* Enable ETS TCs with equal BW Share for now */
3975 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3976 if (enabled_tc & (1 << i))
3977 bw_data.tc_bw_share_credits[i] = 1;
3978 }
3979
3980 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
3981 &bw_data, NULL);
3982 if (ret) {
3983 dev_info(&pf->pdev->dev,
3984 "veb bw config failed, aq_err=%d\n",
3985 pf->hw.aq.asq_last_status);
3986 goto out;
3987 }
3988
3989 /* Update the BW information */
3990 ret = i40e_veb_get_bw_info(veb);
3991 if (ret) {
3992 dev_info(&pf->pdev->dev,
3993 "Failed getting veb bw config, aq_err=%d\n",
3994 pf->hw.aq.asq_last_status);
3995 }
3996
3997out:
3998 return ret;
3999}
4000
4001#ifdef CONFIG_I40E_DCB
4002/**
4003 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
4004 * @pf: PF struct
4005 *
4006 * Reconfigure VEB/VSIs on a given PF; it is assumed that
4007 * the caller would've quiesce all the VSIs before calling
4008 * this function
4009 **/
4010static void i40e_dcb_reconfigure(struct i40e_pf *pf)
4011{
4012 u8 tc_map = 0;
4013 int ret;
4014 u8 v;
4015
4016 /* Enable the TCs available on PF to all VEBs */
4017 tc_map = i40e_pf_get_tc_map(pf);
4018 for (v = 0; v < I40E_MAX_VEB; v++) {
4019 if (!pf->veb[v])
4020 continue;
4021 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
4022 if (ret) {
4023 dev_info(&pf->pdev->dev,
4024 "Failed configuring TC for VEB seid=%d\n",
4025 pf->veb[v]->seid);
4026 /* Will try to configure as many components */
4027 }
4028 }
4029
4030 /* Update each VSI */
4031 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4032 if (!pf->vsi[v])
4033 continue;
4034
4035 /* - Enable all TCs for the LAN VSI
4036 * - For all others keep them at TC0 for now
4037 */
4038 if (v == pf->lan_vsi)
4039 tc_map = i40e_pf_get_tc_map(pf);
4040 else
4041 tc_map = i40e_pf_get_default_tc(pf);
4042
4043 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
4044 if (ret) {
4045 dev_info(&pf->pdev->dev,
4046 "Failed configuring TC for VSI seid=%d\n",
4047 pf->vsi[v]->seid);
4048 /* Will try to configure as many components */
4049 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00004050 /* Re-configure VSI vectors based on updated TC map */
4051 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004052 if (pf->vsi[v]->netdev)
4053 i40e_dcbnl_set_all(pf->vsi[v]);
4054 }
4055 }
4056}
4057
4058/**
4059 * i40e_init_pf_dcb - Initialize DCB configuration
4060 * @pf: PF being configured
4061 *
4062 * Query the current DCB configuration and cache it
4063 * in the hardware structure
4064 **/
4065static int i40e_init_pf_dcb(struct i40e_pf *pf)
4066{
4067 struct i40e_hw *hw = &pf->hw;
4068 int err = 0;
4069
4070 if (pf->hw.func_caps.npar_enable)
4071 goto out;
4072
4073 /* Get the initial DCB configuration */
4074 err = i40e_init_dcb(hw);
4075 if (!err) {
4076 /* Device/Function is not DCBX capable */
4077 if ((!hw->func_caps.dcb) ||
4078 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
4079 dev_info(&pf->pdev->dev,
4080 "DCBX offload is not supported or is disabled for this PF.\n");
4081
4082 if (pf->flags & I40E_FLAG_MFP_ENABLED)
4083 goto out;
4084
4085 } else {
4086 /* When status is not DISABLED then DCBX in FW */
4087 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
4088 DCB_CAP_DCBX_VER_IEEE;
4089 pf->flags |= I40E_FLAG_DCB_ENABLED;
4090 }
Neerav Parikh014269f2014-04-01 07:11:48 +00004091 } else {
4092 dev_info(&pf->pdev->dev, "AQ Querying DCB configuration failed: %d\n",
4093 pf->hw.aq.asq_last_status);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004094 }
4095
4096out:
4097 return err;
4098}
4099#endif /* CONFIG_I40E_DCB */
4100
4101/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004102 * i40e_up_complete - Finish the last steps of bringing up a connection
4103 * @vsi: the VSI being configured
4104 **/
4105static int i40e_up_complete(struct i40e_vsi *vsi)
4106{
4107 struct i40e_pf *pf = vsi->back;
4108 int err;
4109
4110 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4111 i40e_vsi_configure_msix(vsi);
4112 else
4113 i40e_configure_msi_and_legacy(vsi);
4114
4115 /* start rings */
4116 err = i40e_vsi_control_rings(vsi, true);
4117 if (err)
4118 return err;
4119
4120 clear_bit(__I40E_DOWN, &vsi->state);
4121 i40e_napi_enable_all(vsi);
4122 i40e_vsi_enable_irq(vsi);
4123
4124 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
4125 (vsi->netdev)) {
Anjali Singhai6d779b42013-09-28 06:00:02 +00004126 netdev_info(vsi->netdev, "NIC Link is Up\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004127 netif_tx_start_all_queues(vsi->netdev);
4128 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00004129 } else if (vsi->netdev) {
4130 netdev_info(vsi->netdev, "NIC Link is Down\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004131 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00004132
4133 /* replay FDIR SB filters */
4134 if (vsi->type == I40E_VSI_FDIR)
4135 i40e_fdir_filter_restore(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004136 i40e_service_event_schedule(pf);
4137
4138 return 0;
4139}
4140
4141/**
4142 * i40e_vsi_reinit_locked - Reset the VSI
4143 * @vsi: the VSI being configured
4144 *
4145 * Rebuild the ring structs after some configuration
4146 * has changed, e.g. MTU size.
4147 **/
4148static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
4149{
4150 struct i40e_pf *pf = vsi->back;
4151
4152 WARN_ON(in_interrupt());
4153 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
4154 usleep_range(1000, 2000);
4155 i40e_down(vsi);
4156
4157 /* Give a VF some time to respond to the reset. The
4158 * two second wait is based upon the watchdog cycle in
4159 * the VF driver.
4160 */
4161 if (vsi->type == I40E_VSI_SRIOV)
4162 msleep(2000);
4163 i40e_up(vsi);
4164 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
4165}
4166
4167/**
4168 * i40e_up - Bring the connection back up after being down
4169 * @vsi: the VSI being configured
4170 **/
4171int i40e_up(struct i40e_vsi *vsi)
4172{
4173 int err;
4174
4175 err = i40e_vsi_configure(vsi);
4176 if (!err)
4177 err = i40e_up_complete(vsi);
4178
4179 return err;
4180}
4181
4182/**
4183 * i40e_down - Shutdown the connection processing
4184 * @vsi: the VSI being stopped
4185 **/
4186void i40e_down(struct i40e_vsi *vsi)
4187{
4188 int i;
4189
4190 /* It is assumed that the caller of this function
4191 * sets the vsi->state __I40E_DOWN bit.
4192 */
4193 if (vsi->netdev) {
4194 netif_carrier_off(vsi->netdev);
4195 netif_tx_disable(vsi->netdev);
4196 }
4197 i40e_vsi_disable_irq(vsi);
4198 i40e_vsi_control_rings(vsi, false);
4199 i40e_napi_disable_all(vsi);
4200
4201 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004202 i40e_clean_tx_ring(vsi->tx_rings[i]);
4203 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004204 }
4205}
4206
4207/**
4208 * i40e_setup_tc - configure multiple traffic classes
4209 * @netdev: net device to configure
4210 * @tc: number of traffic classes to enable
4211 **/
4212static int i40e_setup_tc(struct net_device *netdev, u8 tc)
4213{
4214 struct i40e_netdev_priv *np = netdev_priv(netdev);
4215 struct i40e_vsi *vsi = np->vsi;
4216 struct i40e_pf *pf = vsi->back;
4217 u8 enabled_tc = 0;
4218 int ret = -EINVAL;
4219 int i;
4220
4221 /* Check if DCB enabled to continue */
4222 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
4223 netdev_info(netdev, "DCB is not enabled for adapter\n");
4224 goto exit;
4225 }
4226
4227 /* Check if MFP enabled */
4228 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
4229 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
4230 goto exit;
4231 }
4232
4233 /* Check whether tc count is within enabled limit */
4234 if (tc > i40e_pf_get_num_tc(pf)) {
4235 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
4236 goto exit;
4237 }
4238
4239 /* Generate TC map for number of tc requested */
4240 for (i = 0; i < tc; i++)
4241 enabled_tc |= (1 << i);
4242
4243 /* Requesting same TC configuration as already enabled */
4244 if (enabled_tc == vsi->tc_config.enabled_tc)
4245 return 0;
4246
4247 /* Quiesce VSI queues */
4248 i40e_quiesce_vsi(vsi);
4249
4250 /* Configure VSI for enabled TCs */
4251 ret = i40e_vsi_config_tc(vsi, enabled_tc);
4252 if (ret) {
4253 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
4254 vsi->seid);
4255 goto exit;
4256 }
4257
4258 /* Unquiesce VSI */
4259 i40e_unquiesce_vsi(vsi);
4260
4261exit:
4262 return ret;
4263}
4264
4265/**
4266 * i40e_open - Called when a network interface is made active
4267 * @netdev: network interface device structure
4268 *
4269 * The open entry point is called when a network interface is made
4270 * active by the system (IFF_UP). At this point all resources needed
4271 * for transmit and receive operations are allocated, the interrupt
4272 * handler is registered with the OS, the netdev watchdog subtask is
4273 * enabled, and the stack is notified that the interface is ready.
4274 *
4275 * Returns 0 on success, negative value on failure
4276 **/
4277static int i40e_open(struct net_device *netdev)
4278{
4279 struct i40e_netdev_priv *np = netdev_priv(netdev);
4280 struct i40e_vsi *vsi = np->vsi;
4281 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004282 int err;
4283
Shannon Nelson4eb3f762014-03-06 08:59:58 +00004284 /* disallow open during test or if eeprom is broken */
4285 if (test_bit(__I40E_TESTING, &pf->state) ||
4286 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004287 return -EBUSY;
4288
4289 netif_carrier_off(netdev);
4290
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004291 err = i40e_vsi_open(vsi);
4292 if (err)
4293 return err;
4294
Jesse Brandeburg059dab62014-04-01 09:07:20 +00004295 /* configure global TSO hardware offload settings */
4296 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
4297 TCP_FLAG_FIN) >> 16);
4298 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
4299 TCP_FLAG_FIN |
4300 TCP_FLAG_CWR) >> 16);
4301 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
4302
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004303#ifdef CONFIG_I40E_VXLAN
4304 vxlan_get_rx_port(netdev);
4305#endif
4306
4307 return 0;
4308}
4309
4310/**
4311 * i40e_vsi_open -
4312 * @vsi: the VSI to open
4313 *
4314 * Finish initialization of the VSI.
4315 *
4316 * Returns 0 on success, negative value on failure
4317 **/
4318int i40e_vsi_open(struct i40e_vsi *vsi)
4319{
4320 struct i40e_pf *pf = vsi->back;
4321 char int_name[IFNAMSIZ];
4322 int err;
4323
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004324 /* allocate descriptors */
4325 err = i40e_vsi_setup_tx_resources(vsi);
4326 if (err)
4327 goto err_setup_tx;
4328 err = i40e_vsi_setup_rx_resources(vsi);
4329 if (err)
4330 goto err_setup_rx;
4331
4332 err = i40e_vsi_configure(vsi);
4333 if (err)
4334 goto err_setup_rx;
4335
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00004336 if (vsi->netdev) {
4337 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
4338 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
4339 err = i40e_vsi_request_irq(vsi, int_name);
4340 if (err)
4341 goto err_setup_rx;
4342
4343 /* Notify the stack of the actual queue counts. */
4344 err = netif_set_real_num_tx_queues(vsi->netdev,
4345 vsi->num_queue_pairs);
4346 if (err)
4347 goto err_set_queues;
4348
4349 err = netif_set_real_num_rx_queues(vsi->netdev,
4350 vsi->num_queue_pairs);
4351 if (err)
4352 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00004353
4354 } else if (vsi->type == I40E_VSI_FDIR) {
4355 snprintf(int_name, sizeof(int_name) - 1, "%s-fdir",
4356 dev_driver_string(&pf->pdev->dev));
4357 err = i40e_vsi_request_irq(vsi, int_name);
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00004358 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00004359 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004360 goto err_setup_rx;
4361 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004362
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004363 err = i40e_up_complete(vsi);
4364 if (err)
4365 goto err_up_complete;
4366
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004367 return 0;
4368
4369err_up_complete:
4370 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004371err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004372 i40e_vsi_free_irq(vsi);
4373err_setup_rx:
4374 i40e_vsi_free_rx_resources(vsi);
4375err_setup_tx:
4376 i40e_vsi_free_tx_resources(vsi);
4377 if (vsi == pf->vsi[pf->lan_vsi])
4378 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
4379
4380 return err;
4381}
4382
4383/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00004384 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
4385 * @pf: Pointer to pf
4386 *
4387 * This function destroys the hlist where all the Flow Director
4388 * filters were saved.
4389 **/
4390static void i40e_fdir_filter_exit(struct i40e_pf *pf)
4391{
4392 struct i40e_fdir_filter *filter;
4393 struct hlist_node *node2;
4394
4395 hlist_for_each_entry_safe(filter, node2,
4396 &pf->fdir_filter_list, fdir_node) {
4397 hlist_del(&filter->fdir_node);
4398 kfree(filter);
4399 }
4400 pf->fdir_pf_active_filters = 0;
4401}
4402
4403/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004404 * i40e_close - Disables a network interface
4405 * @netdev: network interface device structure
4406 *
4407 * The close entry point is called when an interface is de-activated
4408 * by the OS. The hardware is still under the driver's control, but
4409 * this netdev interface is disabled.
4410 *
4411 * Returns 0, this is not allowed to fail
4412 **/
4413static int i40e_close(struct net_device *netdev)
4414{
4415 struct i40e_netdev_priv *np = netdev_priv(netdev);
4416 struct i40e_vsi *vsi = np->vsi;
4417
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004418 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004419
4420 return 0;
4421}
4422
4423/**
4424 * i40e_do_reset - Start a PF or Core Reset sequence
4425 * @pf: board private structure
4426 * @reset_flags: which reset is requested
4427 *
4428 * The essential difference in resets is that the PF Reset
4429 * doesn't clear the packet buffers, doesn't reset the PE
4430 * firmware, and doesn't bother the other PFs on the chip.
4431 **/
4432void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
4433{
4434 u32 val;
4435
4436 WARN_ON(in_interrupt());
4437
Mitch Williams263fc482014-04-23 04:50:11 +00004438 if (i40e_check_asq_alive(&pf->hw))
4439 i40e_vc_notify_reset(pf);
4440
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004441 /* do the biggest reset indicated */
4442 if (reset_flags & (1 << __I40E_GLOBAL_RESET_REQUESTED)) {
4443
4444 /* Request a Global Reset
4445 *
4446 * This will start the chip's countdown to the actual full
4447 * chip reset event, and a warning interrupt to be sent
4448 * to all PFs, including the requestor. Our handler
4449 * for the warning interrupt will deal with the shutdown
4450 * and recovery of the switch setup.
4451 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004452 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004453 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4454 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
4455 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4456
4457 } else if (reset_flags & (1 << __I40E_CORE_RESET_REQUESTED)) {
4458
4459 /* Request a Core Reset
4460 *
4461 * Same as Global Reset, except does *not* include the MAC/PHY
4462 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004463 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004464 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4465 val |= I40E_GLGEN_RTRIG_CORER_MASK;
4466 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4467 i40e_flush(&pf->hw);
4468
Shannon Nelson7823fe32013-11-16 10:00:45 +00004469 } else if (reset_flags & (1 << __I40E_EMP_RESET_REQUESTED)) {
4470
4471 /* Request a Firmware Reset
4472 *
4473 * Same as Global reset, plus restarting the
4474 * embedded firmware engine.
4475 */
4476 /* enable EMP Reset */
4477 val = rd32(&pf->hw, I40E_GLGEN_RSTENA_EMP);
4478 val |= I40E_GLGEN_RSTENA_EMP_EMP_RST_ENA_MASK;
4479 wr32(&pf->hw, I40E_GLGEN_RSTENA_EMP, val);
4480
4481 /* force the reset */
4482 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4483 val |= I40E_GLGEN_RTRIG_EMPFWR_MASK;
4484 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4485 i40e_flush(&pf->hw);
4486
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004487 } else if (reset_flags & (1 << __I40E_PF_RESET_REQUESTED)) {
4488
4489 /* Request a PF Reset
4490 *
4491 * Resets only the PF-specific registers
4492 *
4493 * This goes directly to the tear-down and rebuild of
4494 * the switch, since we need to do all the recovery as
4495 * for the Core Reset.
4496 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004497 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004498 i40e_handle_reset_warning(pf);
4499
4500 } else if (reset_flags & (1 << __I40E_REINIT_REQUESTED)) {
4501 int v;
4502
4503 /* Find the VSI(s) that requested a re-init */
4504 dev_info(&pf->pdev->dev,
4505 "VSI reinit requested\n");
4506 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4507 struct i40e_vsi *vsi = pf->vsi[v];
4508 if (vsi != NULL &&
4509 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
4510 i40e_vsi_reinit_locked(pf->vsi[v]);
4511 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
4512 }
4513 }
4514
4515 /* no further action needed, so return now */
4516 return;
4517 } else {
4518 dev_info(&pf->pdev->dev,
4519 "bad reset request 0x%08x\n", reset_flags);
4520 return;
4521 }
4522}
4523
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004524#ifdef CONFIG_I40E_DCB
4525/**
4526 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
4527 * @pf: board private structure
4528 * @old_cfg: current DCB config
4529 * @new_cfg: new DCB config
4530 **/
4531bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
4532 struct i40e_dcbx_config *old_cfg,
4533 struct i40e_dcbx_config *new_cfg)
4534{
4535 bool need_reconfig = false;
4536
4537 /* Check if ETS configuration has changed */
4538 if (memcmp(&new_cfg->etscfg,
4539 &old_cfg->etscfg,
4540 sizeof(new_cfg->etscfg))) {
4541 /* If Priority Table has changed reconfig is needed */
4542 if (memcmp(&new_cfg->etscfg.prioritytable,
4543 &old_cfg->etscfg.prioritytable,
4544 sizeof(new_cfg->etscfg.prioritytable))) {
4545 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004546 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004547 }
4548
4549 if (memcmp(&new_cfg->etscfg.tcbwtable,
4550 &old_cfg->etscfg.tcbwtable,
4551 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004552 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004553
4554 if (memcmp(&new_cfg->etscfg.tsatable,
4555 &old_cfg->etscfg.tsatable,
4556 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004557 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004558 }
4559
4560 /* Check if PFC configuration has changed */
4561 if (memcmp(&new_cfg->pfc,
4562 &old_cfg->pfc,
4563 sizeof(new_cfg->pfc))) {
4564 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004565 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004566 }
4567
4568 /* Check if APP Table has changed */
4569 if (memcmp(&new_cfg->app,
4570 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05004571 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004572 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004573 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05004574 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004575
4576 return need_reconfig;
4577}
4578
4579/**
4580 * i40e_handle_lldp_event - Handle LLDP Change MIB event
4581 * @pf: board private structure
4582 * @e: event info posted on ARQ
4583 **/
4584static int i40e_handle_lldp_event(struct i40e_pf *pf,
4585 struct i40e_arq_event_info *e)
4586{
4587 struct i40e_aqc_lldp_get_mib *mib =
4588 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
4589 struct i40e_hw *hw = &pf->hw;
4590 struct i40e_dcbx_config *dcbx_cfg = &hw->local_dcbx_config;
4591 struct i40e_dcbx_config tmp_dcbx_cfg;
4592 bool need_reconfig = false;
4593 int ret = 0;
4594 u8 type;
4595
4596 /* Ignore if event is not for Nearest Bridge */
4597 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
4598 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
4599 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
4600 return ret;
4601
4602 /* Check MIB Type and return if event for Remote MIB update */
4603 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
4604 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
4605 /* Update the remote cached instance and return */
4606 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
4607 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
4608 &hw->remote_dcbx_config);
4609 goto exit;
4610 }
4611
4612 /* Convert/store the DCBX data from LLDPDU temporarily */
4613 memset(&tmp_dcbx_cfg, 0, sizeof(tmp_dcbx_cfg));
4614 ret = i40e_lldp_to_dcb_config(e->msg_buf, &tmp_dcbx_cfg);
4615 if (ret) {
4616 /* Error in LLDPDU parsing return */
4617 dev_info(&pf->pdev->dev, "Failed parsing LLDPDU from event buffer\n");
4618 goto exit;
4619 }
4620
4621 /* No change detected in DCBX configs */
4622 if (!memcmp(&tmp_dcbx_cfg, dcbx_cfg, sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004623 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004624 goto exit;
4625 }
4626
4627 need_reconfig = i40e_dcb_need_reconfig(pf, dcbx_cfg, &tmp_dcbx_cfg);
4628
4629 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg);
4630
4631 /* Overwrite the new configuration */
4632 *dcbx_cfg = tmp_dcbx_cfg;
4633
4634 if (!need_reconfig)
4635 goto exit;
4636
4637 /* Reconfiguration needed quiesce all VSIs */
4638 i40e_pf_quiesce_all_vsi(pf);
4639
4640 /* Changes in configuration update VEB/VSI */
4641 i40e_dcb_reconfigure(pf);
4642
4643 i40e_pf_unquiesce_all_vsi(pf);
4644exit:
4645 return ret;
4646}
4647#endif /* CONFIG_I40E_DCB */
4648
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004649/**
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004650 * i40e_do_reset_safe - Protected reset path for userland calls.
4651 * @pf: board private structure
4652 * @reset_flags: which reset is requested
4653 *
4654 **/
4655void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
4656{
4657 rtnl_lock();
4658 i40e_do_reset(pf, reset_flags);
4659 rtnl_unlock();
4660}
4661
4662/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004663 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
4664 * @pf: board private structure
4665 * @e: event info posted on ARQ
4666 *
4667 * Handler for LAN Queue Overflow Event generated by the firmware for PF
4668 * and VF queues
4669 **/
4670static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
4671 struct i40e_arq_event_info *e)
4672{
4673 struct i40e_aqc_lan_overflow *data =
4674 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
4675 u32 queue = le32_to_cpu(data->prtdcb_rupto);
4676 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
4677 struct i40e_hw *hw = &pf->hw;
4678 struct i40e_vf *vf;
4679 u16 vf_id;
4680
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004681 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
4682 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004683
4684 /* Queue belongs to VF, find the VF and issue VF reset */
4685 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
4686 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
4687 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
4688 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
4689 vf_id -= hw->func_caps.vf_base_id;
4690 vf = &pf->vf[vf_id];
4691 i40e_vc_notify_vf_reset(vf);
4692 /* Allow VF to process pending reset notification */
4693 msleep(20);
4694 i40e_reset_vf(vf, false);
4695 }
4696}
4697
4698/**
4699 * i40e_service_event_complete - Finish up the service event
4700 * @pf: board private structure
4701 **/
4702static void i40e_service_event_complete(struct i40e_pf *pf)
4703{
4704 BUG_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
4705
4706 /* flush memory to make sure state is correct before next watchog */
4707 smp_mb__before_clear_bit();
4708 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
4709}
4710
4711/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004712 * i40e_get_current_fd_count - Get the count of FD filters programmed in the HW
4713 * @pf: board private structure
4714 **/
4715int i40e_get_current_fd_count(struct i40e_pf *pf)
4716{
4717 int val, fcnt_prog;
4718 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
4719 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
4720 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
4721 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
4722 return fcnt_prog;
4723}
4724
4725/**
4726 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
4727 * @pf: board private structure
4728 **/
4729void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
4730{
4731 u32 fcnt_prog, fcnt_avail;
4732
4733 /* Check if, FD SB or ATR was auto disabled and if there is enough room
4734 * to re-enable
4735 */
4736 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4737 (pf->flags & I40E_FLAG_FD_SB_ENABLED))
4738 return;
4739 fcnt_prog = i40e_get_current_fd_count(pf);
Anjali Singhai Jain89132782014-04-09 05:59:01 +00004740 fcnt_avail = i40e_get_fd_cnt_all(pf);
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004741 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) {
4742 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
4743 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
4744 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
4745 dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
4746 }
4747 }
4748 /* Wait for some more space to be available to turn on ATR */
4749 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
4750 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4751 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
4752 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
4753 dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table now\n");
4754 }
4755 }
4756}
4757
4758/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004759 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
4760 * @pf: board private structure
4761 **/
4762static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
4763{
4764 if (!(pf->flags & I40E_FLAG_FDIR_REQUIRES_REINIT))
4765 return;
4766
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004767 /* if interface is down do nothing */
4768 if (test_bit(__I40E_DOWN, &pf->state))
4769 return;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004770 i40e_fdir_check_and_reenable(pf);
4771
4772 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4773 (pf->flags & I40E_FLAG_FD_SB_ENABLED))
4774 pf->flags &= ~I40E_FLAG_FDIR_REQUIRES_REINIT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004775}
4776
4777/**
4778 * i40e_vsi_link_event - notify VSI of a link event
4779 * @vsi: vsi to be notified
4780 * @link_up: link up or down
4781 **/
4782static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
4783{
4784 if (!vsi)
4785 return;
4786
4787 switch (vsi->type) {
4788 case I40E_VSI_MAIN:
4789 if (!vsi->netdev || !vsi->netdev_registered)
4790 break;
4791
4792 if (link_up) {
4793 netif_carrier_on(vsi->netdev);
4794 netif_tx_wake_all_queues(vsi->netdev);
4795 } else {
4796 netif_carrier_off(vsi->netdev);
4797 netif_tx_stop_all_queues(vsi->netdev);
4798 }
4799 break;
4800
4801 case I40E_VSI_SRIOV:
4802 break;
4803
4804 case I40E_VSI_VMDQ2:
4805 case I40E_VSI_CTRL:
4806 case I40E_VSI_MIRROR:
4807 default:
4808 /* there is no notification for other VSIs */
4809 break;
4810 }
4811}
4812
4813/**
4814 * i40e_veb_link_event - notify elements on the veb of a link event
4815 * @veb: veb to be notified
4816 * @link_up: link up or down
4817 **/
4818static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
4819{
4820 struct i40e_pf *pf;
4821 int i;
4822
4823 if (!veb || !veb->pf)
4824 return;
4825 pf = veb->pf;
4826
4827 /* depth first... */
4828 for (i = 0; i < I40E_MAX_VEB; i++)
4829 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
4830 i40e_veb_link_event(pf->veb[i], link_up);
4831
4832 /* ... now the local VSIs */
4833 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
4834 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
4835 i40e_vsi_link_event(pf->vsi[i], link_up);
4836}
4837
4838/**
4839 * i40e_link_event - Update netif_carrier status
4840 * @pf: board private structure
4841 **/
4842static void i40e_link_event(struct i40e_pf *pf)
4843{
4844 bool new_link, old_link;
4845
4846 new_link = (pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP);
4847 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
4848
4849 if (new_link == old_link)
4850 return;
4851
Anjali Singhai6d779b42013-09-28 06:00:02 +00004852 if (!test_bit(__I40E_DOWN, &pf->vsi[pf->lan_vsi]->state))
4853 netdev_info(pf->vsi[pf->lan_vsi]->netdev,
4854 "NIC Link is %s\n", (new_link ? "Up" : "Down"));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004855
4856 /* Notify the base of the switch tree connected to
4857 * the link. Floating VEBs are not notified.
4858 */
4859 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
4860 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
4861 else
4862 i40e_vsi_link_event(pf->vsi[pf->lan_vsi], new_link);
4863
4864 if (pf->vf)
4865 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00004866
4867 if (pf->flags & I40E_FLAG_PTP)
4868 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004869}
4870
4871/**
4872 * i40e_check_hang_subtask - Check for hung queues and dropped interrupts
4873 * @pf: board private structure
4874 *
4875 * Set the per-queue flags to request a check for stuck queues in the irq
4876 * clean functions, then force interrupts to be sure the irq clean is called.
4877 **/
4878static void i40e_check_hang_subtask(struct i40e_pf *pf)
4879{
4880 int i, v;
4881
4882 /* If we're down or resetting, just bail */
4883 if (test_bit(__I40E_CONFIG_BUSY, &pf->state))
4884 return;
4885
4886 /* for each VSI/netdev
4887 * for each Tx queue
4888 * set the check flag
4889 * for each q_vector
4890 * force an interrupt
4891 */
4892 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4893 struct i40e_vsi *vsi = pf->vsi[v];
4894 int armed = 0;
4895
4896 if (!pf->vsi[v] ||
4897 test_bit(__I40E_DOWN, &vsi->state) ||
4898 (vsi->netdev && !netif_carrier_ok(vsi->netdev)))
4899 continue;
4900
4901 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004902 set_check_for_tx_hang(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004903 if (test_bit(__I40E_HANG_CHECK_ARMED,
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004904 &vsi->tx_rings[i]->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004905 armed++;
4906 }
4907
4908 if (armed) {
4909 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
4910 wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0,
4911 (I40E_PFINT_DYN_CTL0_INTENA_MASK |
4912 I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK));
4913 } else {
4914 u16 vec = vsi->base_vector - 1;
4915 u32 val = (I40E_PFINT_DYN_CTLN_INTENA_MASK |
4916 I40E_PFINT_DYN_CTLN_SWINT_TRIG_MASK);
4917 for (i = 0; i < vsi->num_q_vectors; i++, vec++)
4918 wr32(&vsi->back->hw,
4919 I40E_PFINT_DYN_CTLN(vec), val);
4920 }
4921 i40e_flush(&vsi->back->hw);
4922 }
4923 }
4924}
4925
4926/**
4927 * i40e_watchdog_subtask - Check and bring link up
4928 * @pf: board private structure
4929 **/
4930static void i40e_watchdog_subtask(struct i40e_pf *pf)
4931{
4932 int i;
4933
4934 /* if interface is down do nothing */
4935 if (test_bit(__I40E_DOWN, &pf->state) ||
4936 test_bit(__I40E_CONFIG_BUSY, &pf->state))
4937 return;
4938
4939 /* Update the stats for active netdevs so the network stack
4940 * can look at updated numbers whenever it cares to
4941 */
4942 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
4943 if (pf->vsi[i] && pf->vsi[i]->netdev)
4944 i40e_update_stats(pf->vsi[i]);
4945
4946 /* Update the stats for the active switching components */
4947 for (i = 0; i < I40E_MAX_VEB; i++)
4948 if (pf->veb[i])
4949 i40e_update_veb_stats(pf->veb[i]);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00004950
4951 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004952}
4953
4954/**
4955 * i40e_reset_subtask - Set up for resetting the device and driver
4956 * @pf: board private structure
4957 **/
4958static void i40e_reset_subtask(struct i40e_pf *pf)
4959{
4960 u32 reset_flags = 0;
4961
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004962 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004963 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
4964 reset_flags |= (1 << __I40E_REINIT_REQUESTED);
4965 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
4966 }
4967 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
4968 reset_flags |= (1 << __I40E_PF_RESET_REQUESTED);
4969 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
4970 }
4971 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
4972 reset_flags |= (1 << __I40E_CORE_RESET_REQUESTED);
4973 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
4974 }
4975 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
4976 reset_flags |= (1 << __I40E_GLOBAL_RESET_REQUESTED);
4977 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
4978 }
4979
4980 /* If there's a recovery already waiting, it takes
4981 * precedence before starting a new reset sequence.
4982 */
4983 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
4984 i40e_handle_reset_warning(pf);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004985 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004986 }
4987
4988 /* If we're already down or resetting, just bail */
4989 if (reset_flags &&
4990 !test_bit(__I40E_DOWN, &pf->state) &&
4991 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
4992 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004993
4994unlock:
4995 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004996}
4997
4998/**
4999 * i40e_handle_link_event - Handle link event
5000 * @pf: board private structure
5001 * @e: event info posted on ARQ
5002 **/
5003static void i40e_handle_link_event(struct i40e_pf *pf,
5004 struct i40e_arq_event_info *e)
5005{
5006 struct i40e_hw *hw = &pf->hw;
5007 struct i40e_aqc_get_link_status *status =
5008 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
5009 struct i40e_link_status *hw_link_info = &hw->phy.link_info;
5010
5011 /* save off old link status information */
5012 memcpy(&pf->hw.phy.link_info_old, hw_link_info,
5013 sizeof(pf->hw.phy.link_info_old));
5014
5015 /* update link status */
5016 hw_link_info->phy_type = (enum i40e_aq_phy_type)status->phy_type;
5017 hw_link_info->link_speed = (enum i40e_aq_link_speed)status->link_speed;
5018 hw_link_info->link_info = status->link_info;
5019 hw_link_info->an_info = status->an_info;
5020 hw_link_info->ext_info = status->ext_info;
5021 hw_link_info->lse_enable =
5022 le16_to_cpu(status->command_flags) &
5023 I40E_AQ_LSE_ENABLE;
5024
5025 /* process the event */
5026 i40e_link_event(pf);
5027
5028 /* Do a new status request to re-enable LSE reporting
5029 * and load new status information into the hw struct,
5030 * then see if the status changed while processing the
5031 * initial event.
5032 */
5033 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
5034 i40e_link_event(pf);
5035}
5036
5037/**
5038 * i40e_clean_adminq_subtask - Clean the AdminQ rings
5039 * @pf: board private structure
5040 **/
5041static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
5042{
5043 struct i40e_arq_event_info event;
5044 struct i40e_hw *hw = &pf->hw;
5045 u16 pending, i = 0;
5046 i40e_status ret;
5047 u16 opcode;
5048 u32 val;
5049
5050 if (!test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state))
5051 return;
5052
Mitch Williams3197ce22013-11-28 06:39:39 +00005053 event.msg_size = I40E_MAX_AQ_BUF_SIZE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005054 event.msg_buf = kzalloc(event.msg_size, GFP_KERNEL);
5055 if (!event.msg_buf)
5056 return;
5057
5058 do {
Mitch Williams2f019122013-11-28 06:39:33 +00005059 event.msg_size = I40E_MAX_AQ_BUF_SIZE; /* reinit each time */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005060 ret = i40e_clean_arq_element(hw, &event, &pending);
5061 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
5062 dev_info(&pf->pdev->dev, "No ARQ event found\n");
5063 break;
5064 } else if (ret) {
5065 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
5066 break;
5067 }
5068
5069 opcode = le16_to_cpu(event.desc.opcode);
5070 switch (opcode) {
5071
5072 case i40e_aqc_opc_get_link_status:
5073 i40e_handle_link_event(pf, &event);
5074 break;
5075 case i40e_aqc_opc_send_msg_to_pf:
5076 ret = i40e_vc_process_vf_msg(pf,
5077 le16_to_cpu(event.desc.retval),
5078 le32_to_cpu(event.desc.cookie_high),
5079 le32_to_cpu(event.desc.cookie_low),
5080 event.msg_buf,
5081 event.msg_size);
5082 break;
5083 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005084 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005085#ifdef CONFIG_I40E_DCB
5086 rtnl_lock();
5087 ret = i40e_handle_lldp_event(pf, &event);
5088 rtnl_unlock();
5089#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005090 break;
5091 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005092 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005093 i40e_handle_lan_overflow_event(pf, &event);
5094 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00005095 case i40e_aqc_opc_send_msg_to_peer:
5096 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
5097 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005098 default:
5099 dev_info(&pf->pdev->dev,
Shannon Nelson0467bc92013-12-18 13:45:58 +00005100 "ARQ Error: Unknown event 0x%04x received\n",
5101 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005102 break;
5103 }
5104 } while (pending && (i++ < pf->adminq_work_limit));
5105
5106 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
5107 /* re-enable Admin queue interrupt cause */
5108 val = rd32(hw, I40E_PFINT_ICR0_ENA);
5109 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
5110 wr32(hw, I40E_PFINT_ICR0_ENA, val);
5111 i40e_flush(hw);
5112
5113 kfree(event.msg_buf);
5114}
5115
5116/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005117 * i40e_verify_eeprom - make sure eeprom is good to use
5118 * @pf: board private structure
5119 **/
5120static void i40e_verify_eeprom(struct i40e_pf *pf)
5121{
5122 int err;
5123
5124 err = i40e_diag_eeprom_test(&pf->hw);
5125 if (err) {
5126 /* retry in case of garbage read */
5127 err = i40e_diag_eeprom_test(&pf->hw);
5128 if (err) {
5129 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
5130 err);
5131 set_bit(__I40E_BAD_EEPROM, &pf->state);
5132 }
5133 }
5134
5135 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
5136 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
5137 clear_bit(__I40E_BAD_EEPROM, &pf->state);
5138 }
5139}
5140
5141/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005142 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
5143 * @veb: pointer to the VEB instance
5144 *
5145 * This is a recursive function that first builds the attached VSIs then
5146 * recurses in to build the next layer of VEB. We track the connections
5147 * through our own index numbers because the seid's from the HW could
5148 * change across the reset.
5149 **/
5150static int i40e_reconstitute_veb(struct i40e_veb *veb)
5151{
5152 struct i40e_vsi *ctl_vsi = NULL;
5153 struct i40e_pf *pf = veb->pf;
5154 int v, veb_idx;
5155 int ret;
5156
5157 /* build VSI that owns this VEB, temporarily attached to base VEB */
5158 for (v = 0; v < pf->hw.func_caps.num_vsis && !ctl_vsi; v++) {
5159 if (pf->vsi[v] &&
5160 pf->vsi[v]->veb_idx == veb->idx &&
5161 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
5162 ctl_vsi = pf->vsi[v];
5163 break;
5164 }
5165 }
5166 if (!ctl_vsi) {
5167 dev_info(&pf->pdev->dev,
5168 "missing owner VSI for veb_idx %d\n", veb->idx);
5169 ret = -ENOENT;
5170 goto end_reconstitute;
5171 }
5172 if (ctl_vsi != pf->vsi[pf->lan_vsi])
5173 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
5174 ret = i40e_add_vsi(ctl_vsi);
5175 if (ret) {
5176 dev_info(&pf->pdev->dev,
5177 "rebuild of owner VSI failed: %d\n", ret);
5178 goto end_reconstitute;
5179 }
5180 i40e_vsi_reset_stats(ctl_vsi);
5181
5182 /* create the VEB in the switch and move the VSI onto the VEB */
5183 ret = i40e_add_veb(veb, ctl_vsi);
5184 if (ret)
5185 goto end_reconstitute;
5186
5187 /* create the remaining VSIs attached to this VEB */
5188 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5189 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
5190 continue;
5191
5192 if (pf->vsi[v]->veb_idx == veb->idx) {
5193 struct i40e_vsi *vsi = pf->vsi[v];
5194 vsi->uplink_seid = veb->seid;
5195 ret = i40e_add_vsi(vsi);
5196 if (ret) {
5197 dev_info(&pf->pdev->dev,
5198 "rebuild of vsi_idx %d failed: %d\n",
5199 v, ret);
5200 goto end_reconstitute;
5201 }
5202 i40e_vsi_reset_stats(vsi);
5203 }
5204 }
5205
5206 /* create any VEBs attached to this VEB - RECURSION */
5207 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
5208 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
5209 pf->veb[veb_idx]->uplink_seid = veb->seid;
5210 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
5211 if (ret)
5212 break;
5213 }
5214 }
5215
5216end_reconstitute:
5217 return ret;
5218}
5219
5220/**
5221 * i40e_get_capabilities - get info about the HW
5222 * @pf: the PF struct
5223 **/
5224static int i40e_get_capabilities(struct i40e_pf *pf)
5225{
5226 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
5227 u16 data_size;
5228 int buf_len;
5229 int err;
5230
5231 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
5232 do {
5233 cap_buf = kzalloc(buf_len, GFP_KERNEL);
5234 if (!cap_buf)
5235 return -ENOMEM;
5236
5237 /* this loads the data into the hw struct for us */
5238 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
5239 &data_size,
5240 i40e_aqc_opc_list_func_capabilities,
5241 NULL);
5242 /* data loaded, buffer no longer needed */
5243 kfree(cap_buf);
5244
5245 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
5246 /* retry with a larger buffer */
5247 buf_len = data_size;
5248 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
5249 dev_info(&pf->pdev->dev,
5250 "capability discovery failed: aq=%d\n",
5251 pf->hw.aq.asq_last_status);
5252 return -ENODEV;
5253 }
5254 } while (err);
5255
Anjali Singhai Jainac71b7b2014-02-06 05:51:08 +00005256 if (((pf->hw.aq.fw_maj_ver == 2) && (pf->hw.aq.fw_min_ver < 22)) ||
5257 (pf->hw.aq.fw_maj_ver < 2)) {
5258 pf->hw.func_caps.num_msix_vectors++;
5259 pf->hw.func_caps.num_msix_vectors_vf++;
5260 }
5261
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005262 if (pf->hw.debug_mask & I40E_DEBUG_USER)
5263 dev_info(&pf->pdev->dev,
5264 "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",
5265 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
5266 pf->hw.func_caps.num_msix_vectors,
5267 pf->hw.func_caps.num_msix_vectors_vf,
5268 pf->hw.func_caps.fd_filters_guaranteed,
5269 pf->hw.func_caps.fd_filters_best_effort,
5270 pf->hw.func_caps.num_tx_qp,
5271 pf->hw.func_caps.num_vsis);
5272
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00005273#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
5274 + pf->hw.func_caps.num_vfs)
5275 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
5276 dev_info(&pf->pdev->dev,
5277 "got num_vsis %d, setting num_vsis to %d\n",
5278 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
5279 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
5280 }
5281
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005282 return 0;
5283}
5284
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005285static int i40e_vsi_clear(struct i40e_vsi *vsi);
5286
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005287/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005288 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005289 * @pf: board private structure
5290 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005291static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005292{
5293 struct i40e_vsi *vsi;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005294 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005295
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005296 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005297 return;
5298
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005299 /* find existing VSI and see if it needs configuring */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005300 vsi = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005301 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
5302 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005303 vsi = pf->vsi[i];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005304 break;
5305 }
5306 }
5307
5308 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005309 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005310 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
5311 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005312 if (!vsi) {
5313 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005314 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
5315 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005316 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005317 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005318
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005319 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005320}
5321
5322/**
5323 * i40e_fdir_teardown - release the Flow Director resources
5324 * @pf: board private structure
5325 **/
5326static void i40e_fdir_teardown(struct i40e_pf *pf)
5327{
5328 int i;
5329
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005330 i40e_fdir_filter_exit(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005331 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
5332 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
5333 i40e_vsi_release(pf->vsi[i]);
5334 break;
5335 }
5336 }
5337}
5338
5339/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005340 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005341 * @pf: board private structure
5342 *
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005343 * Close up the VFs and other things in prep for pf Reset.
5344 **/
5345static int i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005346{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005347 struct i40e_hw *hw = &pf->hw;
5348 i40e_status ret;
5349 u32 v;
5350
5351 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
5352 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005353 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005354
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005355 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005356
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005357 /* quiesce the VSIs and their queues that are not already DOWN */
5358 i40e_pf_quiesce_all_vsi(pf);
5359
5360 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5361 if (pf->vsi[v])
5362 pf->vsi[v]->seid = 0;
5363 }
5364
5365 i40e_shutdown_adminq(&pf->hw);
5366
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005367 /* call shutdown HMC */
5368 ret = i40e_shutdown_lan_hmc(hw);
5369 if (ret) {
5370 dev_info(&pf->pdev->dev, "shutdown_lan_hmc failed: %d\n", ret);
5371 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
5372 }
5373 return ret;
5374}
5375
5376/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00005377 * i40e_send_version - update firmware with driver version
5378 * @pf: PF struct
5379 */
5380static void i40e_send_version(struct i40e_pf *pf)
5381{
5382 struct i40e_driver_version dv;
5383
5384 dv.major_version = DRV_VERSION_MAJOR;
5385 dv.minor_version = DRV_VERSION_MINOR;
5386 dv.build_version = DRV_VERSION_BUILD;
5387 dv.subbuild_version = 0;
5388 strncpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
5389 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
5390}
5391
5392/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00005393 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005394 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005395 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005396 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005397static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005398{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005399 struct i40e_hw *hw = &pf->hw;
5400 i40e_status ret;
5401 u32 v;
5402
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005403 /* Now we wait for GRST to settle out.
5404 * We don't have to delete the VEBs or VSIs from the hw switch
5405 * because the reset will make them disappear.
5406 */
5407 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00005408 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005409 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00005410 goto end_core_reset;
5411 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005412 pf->pfr_count++;
5413
5414 if (test_bit(__I40E_DOWN, &pf->state))
5415 goto end_core_reset;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005416 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005417
5418 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
5419 ret = i40e_init_adminq(&pf->hw);
5420 if (ret) {
5421 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, %d\n", ret);
5422 goto end_core_reset;
5423 }
5424
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005425 /* re-verify the eeprom if we just had an EMP reset */
5426 if (test_bit(__I40E_EMP_RESET_REQUESTED, &pf->state)) {
5427 clear_bit(__I40E_EMP_RESET_REQUESTED, &pf->state);
5428 i40e_verify_eeprom(pf);
5429 }
5430
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005431 ret = i40e_get_capabilities(pf);
5432 if (ret) {
5433 dev_info(&pf->pdev->dev, "i40e_get_capabilities failed, %d\n",
5434 ret);
5435 goto end_core_reset;
5436 }
5437
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005438 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
5439 hw->func_caps.num_rx_qp,
5440 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
5441 if (ret) {
5442 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
5443 goto end_core_reset;
5444 }
5445 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
5446 if (ret) {
5447 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
5448 goto end_core_reset;
5449 }
5450
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005451#ifdef CONFIG_I40E_DCB
5452 ret = i40e_init_pf_dcb(pf);
5453 if (ret) {
5454 dev_info(&pf->pdev->dev, "init_pf_dcb failed: %d\n", ret);
5455 goto end_core_reset;
5456 }
5457#endif /* CONFIG_I40E_DCB */
5458
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005459 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005460 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005461 if (ret)
5462 goto end_core_reset;
5463
5464 /* Rebuild the VSIs and VEBs that existed before reset.
5465 * They are still in our local switch element arrays, so only
5466 * need to rebuild the switch model in the HW.
5467 *
5468 * If there were VEBs but the reconstitution failed, we'll try
5469 * try to recover minimal use by getting the basic PF VSI working.
5470 */
5471 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005472 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005473 /* find the one VEB connected to the MAC, and find orphans */
5474 for (v = 0; v < I40E_MAX_VEB; v++) {
5475 if (!pf->veb[v])
5476 continue;
5477
5478 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
5479 pf->veb[v]->uplink_seid == 0) {
5480 ret = i40e_reconstitute_veb(pf->veb[v]);
5481
5482 if (!ret)
5483 continue;
5484
5485 /* If Main VEB failed, we're in deep doodoo,
5486 * so give up rebuilding the switch and set up
5487 * for minimal rebuild of PF VSI.
5488 * If orphan failed, we'll report the error
5489 * but try to keep going.
5490 */
5491 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
5492 dev_info(&pf->pdev->dev,
5493 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
5494 ret);
5495 pf->vsi[pf->lan_vsi]->uplink_seid
5496 = pf->mac_seid;
5497 break;
5498 } else if (pf->veb[v]->uplink_seid == 0) {
5499 dev_info(&pf->pdev->dev,
5500 "rebuild of orphan VEB failed: %d\n",
5501 ret);
5502 }
5503 }
5504 }
5505 }
5506
5507 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
5508 dev_info(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
5509 /* no VEB, so rebuild only the Main VSI */
5510 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
5511 if (ret) {
5512 dev_info(&pf->pdev->dev,
5513 "rebuild of Main VSI failed: %d\n", ret);
5514 goto end_core_reset;
5515 }
5516 }
5517
5518 /* reinit the misc interrupt */
5519 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5520 ret = i40e_setup_misc_vector(pf);
5521
5522 /* restart the VSIs that were rebuilt and running before the reset */
5523 i40e_pf_unquiesce_all_vsi(pf);
5524
Mitch Williams69f64b22014-02-13 03:48:46 -08005525 if (pf->num_alloc_vfs) {
5526 for (v = 0; v < pf->num_alloc_vfs; v++)
5527 i40e_reset_vf(&pf->vf[v], true);
5528 }
5529
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005530 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00005531 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005532
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005533 dev_info(&pf->pdev->dev, "reset complete\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005534
5535end_core_reset:
5536 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
5537}
5538
5539/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005540 * i40e_handle_reset_warning - prep for the pf to reset, reset and rebuild
5541 * @pf: board private structure
5542 *
5543 * Close up the VFs and other things in prep for a Core Reset,
5544 * then get ready to rebuild the world.
5545 **/
5546static void i40e_handle_reset_warning(struct i40e_pf *pf)
5547{
5548 i40e_status ret;
5549
5550 ret = i40e_prep_for_reset(pf);
5551 if (!ret)
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005552 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005553}
5554
5555/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005556 * i40e_handle_mdd_event
5557 * @pf: pointer to the pf structure
5558 *
5559 * Called from the MDD irq handler to identify possibly malicious vfs
5560 **/
5561static void i40e_handle_mdd_event(struct i40e_pf *pf)
5562{
5563 struct i40e_hw *hw = &pf->hw;
5564 bool mdd_detected = false;
5565 struct i40e_vf *vf;
5566 u32 reg;
5567 int i;
5568
5569 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
5570 return;
5571
5572 /* find what triggered the MDD event */
5573 reg = rd32(hw, I40E_GL_MDET_TX);
5574 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
5575 u8 func = (reg & I40E_GL_MDET_TX_FUNCTION_MASK)
5576 >> I40E_GL_MDET_TX_FUNCTION_SHIFT;
5577 u8 event = (reg & I40E_GL_MDET_TX_EVENT_SHIFT)
5578 >> I40E_GL_MDET_TX_EVENT_SHIFT;
5579 u8 queue = (reg & I40E_GL_MDET_TX_QUEUE_MASK)
5580 >> I40E_GL_MDET_TX_QUEUE_SHIFT;
5581 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00005582 "Malicious Driver Detection event 0x%02x on TX queue %d of function 0x%02x\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005583 event, queue, func);
5584 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
5585 mdd_detected = true;
5586 }
5587 reg = rd32(hw, I40E_GL_MDET_RX);
5588 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
5589 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK)
5590 >> I40E_GL_MDET_RX_FUNCTION_SHIFT;
5591 u8 event = (reg & I40E_GL_MDET_RX_EVENT_SHIFT)
5592 >> I40E_GL_MDET_RX_EVENT_SHIFT;
5593 u8 queue = (reg & I40E_GL_MDET_RX_QUEUE_MASK)
5594 >> I40E_GL_MDET_RX_QUEUE_SHIFT;
5595 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00005596 "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005597 event, queue, func);
5598 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
5599 mdd_detected = true;
5600 }
5601
5602 /* see if one of the VFs needs its hand slapped */
5603 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
5604 vf = &(pf->vf[i]);
5605 reg = rd32(hw, I40E_VP_MDET_TX(i));
5606 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
5607 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
5608 vf->num_mdd_events++;
5609 dev_info(&pf->pdev->dev, "MDD TX event on VF %d\n", i);
5610 }
5611
5612 reg = rd32(hw, I40E_VP_MDET_RX(i));
5613 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
5614 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
5615 vf->num_mdd_events++;
5616 dev_info(&pf->pdev->dev, "MDD RX event on VF %d\n", i);
5617 }
5618
5619 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
5620 dev_info(&pf->pdev->dev,
5621 "Too many MDD events on VF %d, disabled\n", i);
5622 dev_info(&pf->pdev->dev,
5623 "Use PF Control I/F to re-enable the VF\n");
5624 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
5625 }
5626 }
5627
5628 /* re-enable mdd interrupt cause */
5629 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
5630 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
5631 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
5632 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
5633 i40e_flush(hw);
5634}
5635
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005636#ifdef CONFIG_I40E_VXLAN
5637/**
5638 * i40e_sync_vxlan_filters_subtask - Sync the VSI filter list with HW
5639 * @pf: board private structure
5640 **/
5641static void i40e_sync_vxlan_filters_subtask(struct i40e_pf *pf)
5642{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005643 struct i40e_hw *hw = &pf->hw;
5644 i40e_status ret;
5645 u8 filter_index;
5646 __be16 port;
5647 int i;
5648
5649 if (!(pf->flags & I40E_FLAG_VXLAN_FILTER_SYNC))
5650 return;
5651
5652 pf->flags &= ~I40E_FLAG_VXLAN_FILTER_SYNC;
5653
5654 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
5655 if (pf->pending_vxlan_bitmap & (1 << i)) {
5656 pf->pending_vxlan_bitmap &= ~(1 << i);
5657 port = pf->vxlan_ports[i];
5658 ret = port ?
5659 i40e_aq_add_udp_tunnel(hw, ntohs(port),
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005660 I40E_AQC_TUNNEL_TYPE_VXLAN,
5661 &filter_index, NULL)
5662 : i40e_aq_del_udp_tunnel(hw, i, NULL);
5663
5664 if (ret) {
5665 dev_info(&pf->pdev->dev, "Failed to execute AQ command for %s port %d with index %d\n",
5666 port ? "adding" : "deleting",
5667 ntohs(port), port ? i : i);
5668
5669 pf->vxlan_ports[i] = 0;
5670 } else {
5671 dev_info(&pf->pdev->dev, "%s port %d with AQ command with index %d\n",
5672 port ? "Added" : "Deleted",
5673 ntohs(port), port ? i : filter_index);
5674 }
5675 }
5676 }
5677}
5678
5679#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005680/**
5681 * i40e_service_task - Run the driver's async subtasks
5682 * @work: pointer to work_struct containing our data
5683 **/
5684static void i40e_service_task(struct work_struct *work)
5685{
5686 struct i40e_pf *pf = container_of(work,
5687 struct i40e_pf,
5688 service_task);
5689 unsigned long start_time = jiffies;
5690
5691 i40e_reset_subtask(pf);
5692 i40e_handle_mdd_event(pf);
5693 i40e_vc_process_vflr_event(pf);
5694 i40e_watchdog_subtask(pf);
5695 i40e_fdir_reinit_subtask(pf);
5696 i40e_check_hang_subtask(pf);
5697 i40e_sync_filters_subtask(pf);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005698#ifdef CONFIG_I40E_VXLAN
5699 i40e_sync_vxlan_filters_subtask(pf);
5700#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005701 i40e_clean_adminq_subtask(pf);
5702
5703 i40e_service_event_complete(pf);
5704
5705 /* If the tasks have taken longer than one timer cycle or there
5706 * is more work to be done, reschedule the service task now
5707 * rather than wait for the timer to tick again.
5708 */
5709 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
5710 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
5711 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
5712 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
5713 i40e_service_event_schedule(pf);
5714}
5715
5716/**
5717 * i40e_service_timer - timer callback
5718 * @data: pointer to PF struct
5719 **/
5720static void i40e_service_timer(unsigned long data)
5721{
5722 struct i40e_pf *pf = (struct i40e_pf *)data;
5723
5724 mod_timer(&pf->service_timer,
5725 round_jiffies(jiffies + pf->service_timer_period));
5726 i40e_service_event_schedule(pf);
5727}
5728
5729/**
5730 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
5731 * @vsi: the VSI being configured
5732 **/
5733static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
5734{
5735 struct i40e_pf *pf = vsi->back;
5736
5737 switch (vsi->type) {
5738 case I40E_VSI_MAIN:
5739 vsi->alloc_queue_pairs = pf->num_lan_qps;
5740 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5741 I40E_REQ_DESCRIPTOR_MULTIPLE);
5742 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5743 vsi->num_q_vectors = pf->num_lan_msix;
5744 else
5745 vsi->num_q_vectors = 1;
5746
5747 break;
5748
5749 case I40E_VSI_FDIR:
5750 vsi->alloc_queue_pairs = 1;
5751 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
5752 I40E_REQ_DESCRIPTOR_MULTIPLE);
5753 vsi->num_q_vectors = 1;
5754 break;
5755
5756 case I40E_VSI_VMDQ2:
5757 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
5758 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5759 I40E_REQ_DESCRIPTOR_MULTIPLE);
5760 vsi->num_q_vectors = pf->num_vmdq_msix;
5761 break;
5762
5763 case I40E_VSI_SRIOV:
5764 vsi->alloc_queue_pairs = pf->num_vf_qps;
5765 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5766 I40E_REQ_DESCRIPTOR_MULTIPLE);
5767 break;
5768
5769 default:
5770 WARN_ON(1);
5771 return -ENODATA;
5772 }
5773
5774 return 0;
5775}
5776
5777/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005778 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
5779 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005780 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005781 *
5782 * On error: returns error code (negative)
5783 * On success: returns 0
5784 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005785static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005786{
5787 int size;
5788 int ret = 0;
5789
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00005790 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005791 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
5792 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
5793 if (!vsi->tx_rings)
5794 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005795 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
5796
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005797 if (alloc_qvectors) {
5798 /* allocate memory for q_vector pointers */
5799 size = sizeof(struct i40e_q_vectors *) * vsi->num_q_vectors;
5800 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
5801 if (!vsi->q_vectors) {
5802 ret = -ENOMEM;
5803 goto err_vectors;
5804 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005805 }
5806 return ret;
5807
5808err_vectors:
5809 kfree(vsi->tx_rings);
5810 return ret;
5811}
5812
5813/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005814 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
5815 * @pf: board private structure
5816 * @type: type of VSI
5817 *
5818 * On error: returns error code (negative)
5819 * On success: returns vsi index in PF (positive)
5820 **/
5821static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
5822{
5823 int ret = -ENODEV;
5824 struct i40e_vsi *vsi;
5825 int vsi_idx;
5826 int i;
5827
5828 /* Need to protect the allocation of the VSIs at the PF level */
5829 mutex_lock(&pf->switch_mutex);
5830
5831 /* VSI list may be fragmented if VSI creation/destruction has
5832 * been happening. We can afford to do a quick scan to look
5833 * for any free VSIs in the list.
5834 *
5835 * find next empty vsi slot, looping back around if necessary
5836 */
5837 i = pf->next_vsi;
5838 while (i < pf->hw.func_caps.num_vsis && pf->vsi[i])
5839 i++;
5840 if (i >= pf->hw.func_caps.num_vsis) {
5841 i = 0;
5842 while (i < pf->next_vsi && pf->vsi[i])
5843 i++;
5844 }
5845
5846 if (i < pf->hw.func_caps.num_vsis && !pf->vsi[i]) {
5847 vsi_idx = i; /* Found one! */
5848 } else {
5849 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00005850 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005851 }
5852 pf->next_vsi = ++i;
5853
5854 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
5855 if (!vsi) {
5856 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00005857 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005858 }
5859 vsi->type = type;
5860 vsi->back = pf;
5861 set_bit(__I40E_DOWN, &vsi->state);
5862 vsi->flags = 0;
5863 vsi->idx = vsi_idx;
5864 vsi->rx_itr_setting = pf->rx_itr_default;
5865 vsi->tx_itr_setting = pf->tx_itr_default;
5866 vsi->netdev_registered = false;
5867 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
5868 INIT_LIST_HEAD(&vsi->mac_filter_list);
5869
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005870 ret = i40e_set_num_rings_in_vsi(vsi);
5871 if (ret)
5872 goto err_rings;
5873
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005874 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005875 if (ret)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005876 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00005877
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005878 /* Setup default MSIX irq handler for VSI */
5879 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
5880
5881 pf->vsi[vsi_idx] = vsi;
5882 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00005883 goto unlock_pf;
5884
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005885err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00005886 pf->next_vsi = i - 1;
5887 kfree(vsi);
5888unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005889 mutex_unlock(&pf->switch_mutex);
5890 return ret;
5891}
5892
5893/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005894 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
5895 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005896 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005897 *
5898 * On error: returns error code (negative)
5899 * On success: returns 0
5900 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005901static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005902{
5903 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005904 if (free_qvectors) {
5905 kfree(vsi->q_vectors);
5906 vsi->q_vectors = NULL;
5907 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005908 kfree(vsi->tx_rings);
5909 vsi->tx_rings = NULL;
5910 vsi->rx_rings = NULL;
5911}
5912
5913/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005914 * i40e_vsi_clear - Deallocate the VSI provided
5915 * @vsi: the VSI being un-configured
5916 **/
5917static int i40e_vsi_clear(struct i40e_vsi *vsi)
5918{
5919 struct i40e_pf *pf;
5920
5921 if (!vsi)
5922 return 0;
5923
5924 if (!vsi->back)
5925 goto free_vsi;
5926 pf = vsi->back;
5927
5928 mutex_lock(&pf->switch_mutex);
5929 if (!pf->vsi[vsi->idx]) {
5930 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
5931 vsi->idx, vsi->idx, vsi, vsi->type);
5932 goto unlock_vsi;
5933 }
5934
5935 if (pf->vsi[vsi->idx] != vsi) {
5936 dev_err(&pf->pdev->dev,
5937 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
5938 pf->vsi[vsi->idx]->idx,
5939 pf->vsi[vsi->idx],
5940 pf->vsi[vsi->idx]->type,
5941 vsi->idx, vsi, vsi->type);
5942 goto unlock_vsi;
5943 }
5944
5945 /* updates the pf for this cleared vsi */
5946 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
5947 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
5948
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005949 i40e_vsi_free_arrays(vsi, true);
Alexander Duyck493fb302013-09-28 07:01:44 +00005950
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005951 pf->vsi[vsi->idx] = NULL;
5952 if (vsi->idx < pf->next_vsi)
5953 pf->next_vsi = vsi->idx;
5954
5955unlock_vsi:
5956 mutex_unlock(&pf->switch_mutex);
5957free_vsi:
5958 kfree(vsi);
5959
5960 return 0;
5961}
5962
5963/**
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005964 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
5965 * @vsi: the VSI being cleaned
5966 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00005967static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005968{
5969 int i;
5970
Greg Rose8e9dca52013-12-18 13:45:53 +00005971 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00005972 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00005973 kfree_rcu(vsi->tx_rings[i], rcu);
5974 vsi->tx_rings[i] = NULL;
5975 vsi->rx_rings[i] = NULL;
5976 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00005977 }
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005978}
5979
5980/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005981 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
5982 * @vsi: the VSI being configured
5983 **/
5984static int i40e_alloc_rings(struct i40e_vsi *vsi)
5985{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00005986 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005987 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005988 int i;
5989
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005990 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00005991 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00005992 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005993 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
5994 if (!tx_ring)
5995 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005996
5997 tx_ring->queue_index = i;
5998 tx_ring->reg_idx = vsi->base_queue + i;
5999 tx_ring->ring_active = false;
6000 tx_ring->vsi = vsi;
6001 tx_ring->netdev = vsi->netdev;
6002 tx_ring->dev = &pf->pdev->dev;
6003 tx_ring->count = vsi->num_desc;
6004 tx_ring->size = 0;
6005 tx_ring->dcb_tc = 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006006 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006007
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006008 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006009 rx_ring->queue_index = i;
6010 rx_ring->reg_idx = vsi->base_queue + i;
6011 rx_ring->ring_active = false;
6012 rx_ring->vsi = vsi;
6013 rx_ring->netdev = vsi->netdev;
6014 rx_ring->dev = &pf->pdev->dev;
6015 rx_ring->count = vsi->num_desc;
6016 rx_ring->size = 0;
6017 rx_ring->dcb_tc = 0;
6018 if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
6019 set_ring_16byte_desc_enabled(rx_ring);
6020 else
6021 clear_ring_16byte_desc_enabled(rx_ring);
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006022 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006023 }
6024
6025 return 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006026
6027err_out:
6028 i40e_vsi_clear_rings(vsi);
6029 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006030}
6031
6032/**
6033 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
6034 * @pf: board private structure
6035 * @vectors: the number of MSI-X vectors to request
6036 *
6037 * Returns the number of vectors reserved, or error
6038 **/
6039static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
6040{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01006041 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
6042 I40E_MIN_MSIX, vectors);
6043 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006044 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01006045 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006046 vectors = 0;
6047 }
6048
Alexander Gordeev7b37f372014-02-18 11:11:42 +01006049 pf->num_msix_entries = vectors;
6050
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006051 return vectors;
6052}
6053
6054/**
6055 * i40e_init_msix - Setup the MSIX capability
6056 * @pf: board private structure
6057 *
6058 * Work with the OS to set up the MSIX vectors needed.
6059 *
6060 * Returns 0 on success, negative on failure
6061 **/
6062static int i40e_init_msix(struct i40e_pf *pf)
6063{
6064 i40e_status err = 0;
6065 struct i40e_hw *hw = &pf->hw;
6066 int v_budget, i;
6067 int vec;
6068
6069 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
6070 return -ENODEV;
6071
6072 /* The number of vectors we'll request will be comprised of:
6073 * - Add 1 for "other" cause for Admin Queue events, etc.
6074 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006075 * - Queues being used for RSS.
6076 * We don't need as many as max_rss_size vectors.
6077 * use rss_size instead in the calculation since that
6078 * is governed by number of cpus in the system.
6079 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006080 * - The number of VMDq pairs
6081 * Once we count this up, try the request.
6082 *
6083 * If we can't get what we want, we'll simplify to nearly nothing
6084 * and try again. If that still fails, we punt.
6085 */
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006086 pf->num_lan_msix = pf->num_lan_qps - (pf->rss_size_max - pf->rss_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006087 pf->num_vmdq_msix = pf->num_vmdq_qps;
6088 v_budget = 1 + pf->num_lan_msix;
6089 v_budget += (pf->num_vmdq_vsis * pf->num_vmdq_msix);
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006090 if (pf->flags & I40E_FLAG_FD_SB_ENABLED)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006091 v_budget++;
6092
6093 /* Scale down if necessary, and the rings will share vectors */
6094 v_budget = min_t(int, v_budget, hw->func_caps.num_msix_vectors);
6095
6096 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
6097 GFP_KERNEL);
6098 if (!pf->msix_entries)
6099 return -ENOMEM;
6100
6101 for (i = 0; i < v_budget; i++)
6102 pf->msix_entries[i].entry = i;
6103 vec = i40e_reserve_msix_vectors(pf, v_budget);
6104 if (vec < I40E_MIN_MSIX) {
6105 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
6106 kfree(pf->msix_entries);
6107 pf->msix_entries = NULL;
6108 return -ENODEV;
6109
6110 } else if (vec == I40E_MIN_MSIX) {
6111 /* Adjust for minimal MSIX use */
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006112 dev_info(&pf->pdev->dev, "Features disabled, not enough MSI-X vectors\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006113 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
6114 pf->num_vmdq_vsis = 0;
6115 pf->num_vmdq_qps = 0;
6116 pf->num_vmdq_msix = 0;
6117 pf->num_lan_qps = 1;
6118 pf->num_lan_msix = 1;
6119
6120 } else if (vec != v_budget) {
6121 /* Scale vector usage down */
6122 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
6123 vec--; /* reserve the misc vector */
6124
6125 /* partition out the remaining vectors */
6126 switch (vec) {
6127 case 2:
6128 pf->num_vmdq_vsis = 1;
6129 pf->num_lan_msix = 1;
6130 break;
6131 case 3:
6132 pf->num_vmdq_vsis = 1;
6133 pf->num_lan_msix = 2;
6134 break;
6135 default:
6136 pf->num_lan_msix = min_t(int, (vec / 2),
6137 pf->num_lan_qps);
6138 pf->num_vmdq_vsis = min_t(int, (vec - pf->num_lan_msix),
6139 I40E_DEFAULT_NUM_VMDQ_VSI);
6140 break;
6141 }
6142 }
6143
6144 return err;
6145}
6146
6147/**
Greg Rose90e04072014-03-06 08:59:57 +00006148 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00006149 * @vsi: the VSI being configured
6150 * @v_idx: index of the vector in the vsi struct
6151 *
6152 * We allocate one q_vector. If allocation fails we return -ENOMEM.
6153 **/
Greg Rose90e04072014-03-06 08:59:57 +00006154static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
Alexander Duyck493fb302013-09-28 07:01:44 +00006155{
6156 struct i40e_q_vector *q_vector;
6157
6158 /* allocate q_vector */
6159 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
6160 if (!q_vector)
6161 return -ENOMEM;
6162
6163 q_vector->vsi = vsi;
6164 q_vector->v_idx = v_idx;
6165 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
6166 if (vsi->netdev)
6167 netif_napi_add(vsi->netdev, &q_vector->napi,
6168 i40e_napi_poll, vsi->work_limit);
6169
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00006170 q_vector->rx.latency_range = I40E_LOW_LATENCY;
6171 q_vector->tx.latency_range = I40E_LOW_LATENCY;
6172
Alexander Duyck493fb302013-09-28 07:01:44 +00006173 /* tie q_vector and vsi together */
6174 vsi->q_vectors[v_idx] = q_vector;
6175
6176 return 0;
6177}
6178
6179/**
Greg Rose90e04072014-03-06 08:59:57 +00006180 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006181 * @vsi: the VSI being configured
6182 *
6183 * We allocate one q_vector per queue interrupt. If allocation fails we
6184 * return -ENOMEM.
6185 **/
Greg Rose90e04072014-03-06 08:59:57 +00006186static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006187{
6188 struct i40e_pf *pf = vsi->back;
6189 int v_idx, num_q_vectors;
Alexander Duyck493fb302013-09-28 07:01:44 +00006190 int err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006191
6192 /* if not MSIX, give the one vector only to the LAN VSI */
6193 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6194 num_q_vectors = vsi->num_q_vectors;
6195 else if (vsi == pf->vsi[pf->lan_vsi])
6196 num_q_vectors = 1;
6197 else
6198 return -EINVAL;
6199
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006200 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Greg Rose90e04072014-03-06 08:59:57 +00006201 err = i40e_vsi_alloc_q_vector(vsi, v_idx);
Alexander Duyck493fb302013-09-28 07:01:44 +00006202 if (err)
6203 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006204 }
6205
6206 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00006207
6208err_out:
6209 while (v_idx--)
6210 i40e_free_q_vector(vsi, v_idx);
6211
6212 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006213}
6214
6215/**
6216 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
6217 * @pf: board private structure to initialize
6218 **/
6219static void i40e_init_interrupt_scheme(struct i40e_pf *pf)
6220{
6221 int err = 0;
6222
6223 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
6224 err = i40e_init_msix(pf);
6225 if (err) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006226 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
6227 I40E_FLAG_RSS_ENABLED |
6228 I40E_FLAG_DCB_ENABLED |
6229 I40E_FLAG_SRIOV_ENABLED |
6230 I40E_FLAG_FD_SB_ENABLED |
6231 I40E_FLAG_FD_ATR_ENABLED |
6232 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006233
6234 /* rework the queue expectations without MSIX */
6235 i40e_determine_queue_usage(pf);
6236 }
6237 }
6238
6239 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
6240 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006241 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006242 err = pci_enable_msi(pf->pdev);
6243 if (err) {
Shannon Nelson958a3e32013-09-28 07:13:28 +00006244 dev_info(&pf->pdev->dev, "MSI init failed - %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006245 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
6246 }
6247 }
6248
Shannon Nelson958a3e32013-09-28 07:13:28 +00006249 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006250 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00006251
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006252 /* track first vector for misc interrupts */
6253 err = i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT-1);
6254}
6255
6256/**
6257 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
6258 * @pf: board private structure
6259 *
6260 * This sets up the handler for MSIX 0, which is used to manage the
6261 * non-queue interrupts, e.g. AdminQ and errors. This is not used
6262 * when in MSI or Legacy interrupt mode.
6263 **/
6264static int i40e_setup_misc_vector(struct i40e_pf *pf)
6265{
6266 struct i40e_hw *hw = &pf->hw;
6267 int err = 0;
6268
6269 /* Only request the irq if this is the first time through, and
6270 * not when we're rebuilding after a Reset
6271 */
6272 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
6273 err = request_irq(pf->msix_entries[0].vector,
6274 i40e_intr, 0, pf->misc_int_name, pf);
6275 if (err) {
6276 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006277 "request_irq for %s failed: %d\n",
6278 pf->misc_int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006279 return -EFAULT;
6280 }
6281 }
6282
6283 i40e_enable_misc_int_causes(hw);
6284
6285 /* associate no queues to the misc vector */
6286 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
6287 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
6288
6289 i40e_flush(hw);
6290
6291 i40e_irq_dynamic_enable_icr0(pf);
6292
6293 return err;
6294}
6295
6296/**
6297 * i40e_config_rss - Prepare for RSS if used
6298 * @pf: board private structure
6299 **/
6300static int i40e_config_rss(struct i40e_pf *pf)
6301{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006302 /* Set of random keys generated using kernel random number generator */
6303 static const u32 seed[I40E_PFQF_HKEY_MAX_INDEX + 1] = {0x41b01687,
6304 0x183cfd8c, 0xce880440, 0x580cbc3c, 0x35897377,
6305 0x328b25e1, 0x4fa98922, 0xb7d90c14, 0xd5bad70d,
6306 0xcd15a2c1, 0xe8580225, 0x4a1e9d11, 0xfe5731be};
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00006307 struct i40e_hw *hw = &pf->hw;
6308 u32 lut = 0;
6309 int i, j;
6310 u64 hena;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006311
6312 /* Fill out hash function seed */
6313 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
6314 wr32(hw, I40E_PFQF_HKEY(i), seed[i]);
6315
6316 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
6317 hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
6318 ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
Mitch Williams12dc4fe2013-11-28 06:39:32 +00006319 hena |= I40E_DEFAULT_RSS_HENA;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006320 wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
6321 wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
6322
6323 /* Populate the LUT with max no. of queues in round robin fashion */
6324 for (i = 0, j = 0; i < pf->hw.func_caps.rss_table_size; i++, j++) {
6325
6326 /* The assumption is that lan qp count will be the highest
6327 * qp count for any PF VSI that needs RSS.
6328 * If multiple VSIs need RSS support, all the qp counts
6329 * for those VSIs should be a power of 2 for RSS to work.
6330 * If LAN VSI is the only consumer for RSS then this requirement
6331 * is not necessary.
6332 */
6333 if (j == pf->rss_size)
6334 j = 0;
6335 /* lut = 4-byte sliding window of 4 lut entries */
6336 lut = (lut << 8) | (j &
6337 ((0x1 << pf->hw.func_caps.rss_table_entry_width) - 1));
6338 /* On i = 3, we have 4 entries in lut; write to the register */
6339 if ((i & 3) == 3)
6340 wr32(hw, I40E_PFQF_HLUT(i >> 2), lut);
6341 }
6342 i40e_flush(hw);
6343
6344 return 0;
6345}
6346
6347/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006348 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
6349 * @pf: board private structure
6350 * @queue_count: the requested queue count for rss.
6351 *
6352 * returns 0 if rss is not enabled, if enabled returns the final rss queue
6353 * count which may be different from the requested queue count.
6354 **/
6355int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
6356{
6357 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
6358 return 0;
6359
6360 queue_count = min_t(int, queue_count, pf->rss_size_max);
6361 queue_count = rounddown_pow_of_two(queue_count);
6362
6363 if (queue_count != pf->rss_size) {
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006364 i40e_prep_for_reset(pf);
6365
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006366 pf->rss_size = queue_count;
6367
6368 i40e_reset_and_rebuild(pf, true);
6369 i40e_config_rss(pf);
6370 }
6371 dev_info(&pf->pdev->dev, "RSS count: %d\n", pf->rss_size);
6372 return pf->rss_size;
6373}
6374
6375/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006376 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
6377 * @pf: board private structure to initialize
6378 *
6379 * i40e_sw_init initializes the Adapter private data structure.
6380 * Fields are initialized based on PCI device information and
6381 * OS network device settings (MTU size).
6382 **/
6383static int i40e_sw_init(struct i40e_pf *pf)
6384{
6385 int err = 0;
6386 int size;
6387
6388 pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
6389 (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
Shannon Nelson27599972013-11-16 10:00:43 +00006390 pf->hw.debug_mask = pf->msg_enable | I40E_DEBUG_DIAG;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006391 if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
6392 if (I40E_DEBUG_USER & debug)
6393 pf->hw.debug_mask = debug;
6394 pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
6395 I40E_DEFAULT_MSG_ENABLE);
6396 }
6397
6398 /* Set default capability flags */
6399 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
6400 I40E_FLAG_MSI_ENABLED |
6401 I40E_FLAG_MSIX_ENABLED |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006402 I40E_FLAG_RX_1BUF_ENABLED;
6403
Mitch Williamsca99eb92014-04-04 04:43:07 +00006404 /* Set default ITR */
6405 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
6406 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
6407
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006408 /* Depending on PF configurations, it is possible that the RSS
6409 * maximum might end up larger than the available queues
6410 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006411 pf->rss_size_max = 0x1 << pf->hw.func_caps.rss_table_entry_width;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006412 pf->rss_size_max = min_t(int, pf->rss_size_max,
6413 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006414 if (pf->hw.func_caps.rss) {
6415 pf->flags |= I40E_FLAG_RSS_ENABLED;
Jesse Brandeburgbf051a32013-11-26 10:49:17 +00006416 pf->rss_size = min_t(int, pf->rss_size_max, num_online_cpus());
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006417 pf->rss_size = rounddown_pow_of_two(pf->rss_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006418 } else {
6419 pf->rss_size = 1;
6420 }
6421
Catherine Sullivan2050bc62013-12-18 13:46:03 +00006422 /* MFP mode enabled */
6423 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.mfp_mode_1) {
6424 pf->flags |= I40E_FLAG_MFP_ENABLED;
6425 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
6426 }
6427
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006428 /* FW/NVM is not yet fixed in this regard */
6429 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
6430 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
6431 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
6432 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006433 if (!(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006434 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006435 } else {
6436 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00006437 "Flow Director Sideband mode Disabled in MFP mode\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006438 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006439 pf->fdir_pf_filter_count =
6440 pf->hw.func_caps.fd_filters_guaranteed;
6441 pf->hw.fdir_shared_filter_count =
6442 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006443 }
6444
6445 if (pf->hw.func_caps.vmdq) {
6446 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
6447 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
6448 pf->num_vmdq_qps = I40E_DEFAULT_QUEUES_PER_VMDQ;
6449 }
6450
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006451#ifdef CONFIG_PCI_IOV
6452 if (pf->hw.func_caps.num_vfs) {
6453 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
6454 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
6455 pf->num_req_vfs = min_t(int,
6456 pf->hw.func_caps.num_vfs,
6457 I40E_MAX_VF_COUNT);
6458 }
6459#endif /* CONFIG_PCI_IOV */
6460 pf->eeprom_version = 0xDEAD;
6461 pf->lan_veb = I40E_NO_VEB;
6462 pf->lan_vsi = I40E_NO_VSI;
6463
6464 /* set up queue assignment tracking */
6465 size = sizeof(struct i40e_lump_tracking)
6466 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
6467 pf->qp_pile = kzalloc(size, GFP_KERNEL);
6468 if (!pf->qp_pile) {
6469 err = -ENOMEM;
6470 goto sw_init_done;
6471 }
6472 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
6473 pf->qp_pile->search_hint = 0;
6474
6475 /* set up vector assignment tracking */
6476 size = sizeof(struct i40e_lump_tracking)
6477 + (sizeof(u16) * pf->hw.func_caps.num_msix_vectors);
6478 pf->irq_pile = kzalloc(size, GFP_KERNEL);
6479 if (!pf->irq_pile) {
6480 kfree(pf->qp_pile);
6481 err = -ENOMEM;
6482 goto sw_init_done;
6483 }
6484 pf->irq_pile->num_entries = pf->hw.func_caps.num_msix_vectors;
6485 pf->irq_pile->search_hint = 0;
6486
6487 mutex_init(&pf->switch_mutex);
6488
6489sw_init_done:
6490 return err;
6491}
6492
6493/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00006494 * i40e_set_ntuple - set the ntuple feature flag and take action
6495 * @pf: board private structure to initialize
6496 * @features: the feature set that the stack is suggesting
6497 *
6498 * returns a bool to indicate if reset needs to happen
6499 **/
6500bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
6501{
6502 bool need_reset = false;
6503
6504 /* Check if Flow Director n-tuple support was enabled or disabled. If
6505 * the state changed, we need to reset.
6506 */
6507 if (features & NETIF_F_NTUPLE) {
6508 /* Enable filters and mark for reset */
6509 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
6510 need_reset = true;
6511 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
6512 } else {
6513 /* turn off filters, mark for reset and clear SW filter list */
6514 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
6515 need_reset = true;
6516 i40e_fdir_filter_exit(pf);
6517 }
6518 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6519 /* if ATR was disabled it can be re-enabled. */
6520 if (!(pf->flags & I40E_FLAG_FD_ATR_ENABLED))
6521 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
6522 }
6523 return need_reset;
6524}
6525
6526/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006527 * i40e_set_features - set the netdev feature flags
6528 * @netdev: ptr to the netdev being adjusted
6529 * @features: the feature set that the stack is suggesting
6530 **/
6531static int i40e_set_features(struct net_device *netdev,
6532 netdev_features_t features)
6533{
6534 struct i40e_netdev_priv *np = netdev_priv(netdev);
6535 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00006536 struct i40e_pf *pf = vsi->back;
6537 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006538
6539 if (features & NETIF_F_HW_VLAN_CTAG_RX)
6540 i40e_vlan_stripping_enable(vsi);
6541 else
6542 i40e_vlan_stripping_disable(vsi);
6543
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00006544 need_reset = i40e_set_ntuple(pf, features);
6545
6546 if (need_reset)
6547 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
6548
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006549 return 0;
6550}
6551
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006552#ifdef CONFIG_I40E_VXLAN
6553/**
6554 * i40e_get_vxlan_port_idx - Lookup a possibly offloaded for Rx UDP port
6555 * @pf: board private structure
6556 * @port: The UDP port to look up
6557 *
6558 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
6559 **/
6560static u8 i40e_get_vxlan_port_idx(struct i40e_pf *pf, __be16 port)
6561{
6562 u8 i;
6563
6564 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
6565 if (pf->vxlan_ports[i] == port)
6566 return i;
6567 }
6568
6569 return i;
6570}
6571
6572/**
6573 * i40e_add_vxlan_port - Get notifications about VXLAN ports that come up
6574 * @netdev: This physical port's netdev
6575 * @sa_family: Socket Family that VXLAN is notifying us about
6576 * @port: New UDP port number that VXLAN started listening to
6577 **/
6578static void i40e_add_vxlan_port(struct net_device *netdev,
6579 sa_family_t sa_family, __be16 port)
6580{
6581 struct i40e_netdev_priv *np = netdev_priv(netdev);
6582 struct i40e_vsi *vsi = np->vsi;
6583 struct i40e_pf *pf = vsi->back;
6584 u8 next_idx;
6585 u8 idx;
6586
6587 if (sa_family == AF_INET6)
6588 return;
6589
6590 idx = i40e_get_vxlan_port_idx(pf, port);
6591
6592 /* Check if port already exists */
6593 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6594 netdev_info(netdev, "Port %d already offloaded\n", ntohs(port));
6595 return;
6596 }
6597
6598 /* Now check if there is space to add the new port */
6599 next_idx = i40e_get_vxlan_port_idx(pf, 0);
6600
6601 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6602 netdev_info(netdev, "Maximum number of UDP ports reached, not adding port %d\n",
6603 ntohs(port));
6604 return;
6605 }
6606
6607 /* New port: add it and mark its index in the bitmap */
6608 pf->vxlan_ports[next_idx] = port;
6609 pf->pending_vxlan_bitmap |= (1 << next_idx);
6610
6611 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
6612}
6613
6614/**
6615 * i40e_del_vxlan_port - Get notifications about VXLAN ports that go away
6616 * @netdev: This physical port's netdev
6617 * @sa_family: Socket Family that VXLAN is notifying us about
6618 * @port: UDP port number that VXLAN stopped listening to
6619 **/
6620static void i40e_del_vxlan_port(struct net_device *netdev,
6621 sa_family_t sa_family, __be16 port)
6622{
6623 struct i40e_netdev_priv *np = netdev_priv(netdev);
6624 struct i40e_vsi *vsi = np->vsi;
6625 struct i40e_pf *pf = vsi->back;
6626 u8 idx;
6627
6628 if (sa_family == AF_INET6)
6629 return;
6630
6631 idx = i40e_get_vxlan_port_idx(pf, port);
6632
6633 /* Check if port already exists */
6634 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6635 /* if port exists, set it to 0 (mark for deletion)
6636 * and make it pending
6637 */
6638 pf->vxlan_ports[idx] = 0;
6639
6640 pf->pending_vxlan_bitmap |= (1 << idx);
6641
6642 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
6643 } else {
6644 netdev_warn(netdev, "Port %d was not found, not deleting\n",
6645 ntohs(port));
6646 }
6647}
6648
6649#endif
Greg Rose4ba0dea2014-03-06 08:59:55 +00006650#ifdef HAVE_FDB_OPS
6651#ifdef USE_CONST_DEV_UC_CHAR
6652static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
6653 struct net_device *dev,
6654 const unsigned char *addr,
6655 u16 flags)
6656#else
6657static int i40e_ndo_fdb_add(struct ndmsg *ndm,
6658 struct net_device *dev,
6659 unsigned char *addr,
6660 u16 flags)
6661#endif
6662{
6663 struct i40e_netdev_priv *np = netdev_priv(dev);
6664 struct i40e_pf *pf = np->vsi->back;
6665 int err = 0;
6666
6667 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
6668 return -EOPNOTSUPP;
6669
6670 /* Hardware does not support aging addresses so if a
6671 * ndm_state is given only allow permanent addresses
6672 */
6673 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
6674 netdev_info(dev, "FDB only supports static addresses\n");
6675 return -EINVAL;
6676 }
6677
6678 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
6679 err = dev_uc_add_excl(dev, addr);
6680 else if (is_multicast_ether_addr(addr))
6681 err = dev_mc_add_excl(dev, addr);
6682 else
6683 err = -EINVAL;
6684
6685 /* Only return duplicate errors if NLM_F_EXCL is set */
6686 if (err == -EEXIST && !(flags & NLM_F_EXCL))
6687 err = 0;
6688
6689 return err;
6690}
6691
6692#ifndef USE_DEFAULT_FDB_DEL_DUMP
6693#ifdef USE_CONST_DEV_UC_CHAR
6694static int i40e_ndo_fdb_del(struct ndmsg *ndm,
6695 struct net_device *dev,
6696 const unsigned char *addr)
6697#else
6698static int i40e_ndo_fdb_del(struct ndmsg *ndm,
6699 struct net_device *dev,
6700 unsigned char *addr)
6701#endif
6702{
6703 struct i40e_netdev_priv *np = netdev_priv(dev);
6704 struct i40e_pf *pf = np->vsi->back;
6705 int err = -EOPNOTSUPP;
6706
6707 if (ndm->ndm_state & NUD_PERMANENT) {
6708 netdev_info(dev, "FDB only supports static addresses\n");
6709 return -EINVAL;
6710 }
6711
6712 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
6713 if (is_unicast_ether_addr(addr))
6714 err = dev_uc_del(dev, addr);
6715 else if (is_multicast_ether_addr(addr))
6716 err = dev_mc_del(dev, addr);
6717 else
6718 err = -EINVAL;
6719 }
6720
6721 return err;
6722}
6723
6724static int i40e_ndo_fdb_dump(struct sk_buff *skb,
6725 struct netlink_callback *cb,
6726 struct net_device *dev,
6727 int idx)
6728{
6729 struct i40e_netdev_priv *np = netdev_priv(dev);
6730 struct i40e_pf *pf = np->vsi->back;
6731
6732 if (pf->flags & I40E_FLAG_SRIOV_ENABLED)
6733 idx = ndo_dflt_fdb_dump(skb, cb, dev, idx);
6734
6735 return idx;
6736}
6737
6738#endif /* USE_DEFAULT_FDB_DEL_DUMP */
6739#endif /* HAVE_FDB_OPS */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006740static const struct net_device_ops i40e_netdev_ops = {
6741 .ndo_open = i40e_open,
6742 .ndo_stop = i40e_close,
6743 .ndo_start_xmit = i40e_lan_xmit_frame,
6744 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
6745 .ndo_set_rx_mode = i40e_set_rx_mode,
6746 .ndo_validate_addr = eth_validate_addr,
6747 .ndo_set_mac_address = i40e_set_mac,
6748 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006749 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006750 .ndo_tx_timeout = i40e_tx_timeout,
6751 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
6752 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
6753#ifdef CONFIG_NET_POLL_CONTROLLER
6754 .ndo_poll_controller = i40e_netpoll,
6755#endif
6756 .ndo_setup_tc = i40e_setup_tc,
6757 .ndo_set_features = i40e_set_features,
6758 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
6759 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04006760 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006761 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00006762 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006763#ifdef CONFIG_I40E_VXLAN
6764 .ndo_add_vxlan_port = i40e_add_vxlan_port,
6765 .ndo_del_vxlan_port = i40e_del_vxlan_port,
6766#endif
Greg Rose4ba0dea2014-03-06 08:59:55 +00006767#ifdef HAVE_FDB_OPS
6768 .ndo_fdb_add = i40e_ndo_fdb_add,
6769#ifndef USE_DEFAULT_FDB_DEL_DUMP
6770 .ndo_fdb_del = i40e_ndo_fdb_del,
6771 .ndo_fdb_dump = i40e_ndo_fdb_dump,
6772#endif
6773#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006774};
6775
6776/**
6777 * i40e_config_netdev - Setup the netdev flags
6778 * @vsi: the VSI being configured
6779 *
6780 * Returns 0 on success, negative value on failure
6781 **/
6782static int i40e_config_netdev(struct i40e_vsi *vsi)
6783{
Greg Rose1a103702013-11-28 06:42:39 +00006784 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006785 struct i40e_pf *pf = vsi->back;
6786 struct i40e_hw *hw = &pf->hw;
6787 struct i40e_netdev_priv *np;
6788 struct net_device *netdev;
6789 u8 mac_addr[ETH_ALEN];
6790 int etherdev_size;
6791
6792 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006793 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006794 if (!netdev)
6795 return -ENOMEM;
6796
6797 vsi->netdev = netdev;
6798 np = netdev_priv(netdev);
6799 np->vsi = vsi;
6800
Or Gerlitzd70e9412014-03-18 10:36:45 +02006801 netdev->hw_enc_features |= NETIF_F_IP_CSUM |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006802 NETIF_F_GSO_UDP_TUNNEL |
Or Gerlitzd70e9412014-03-18 10:36:45 +02006803 NETIF_F_TSO;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006804
6805 netdev->features = NETIF_F_SG |
6806 NETIF_F_IP_CSUM |
6807 NETIF_F_SCTP_CSUM |
6808 NETIF_F_HIGHDMA |
6809 NETIF_F_GSO_UDP_TUNNEL |
6810 NETIF_F_HW_VLAN_CTAG_TX |
6811 NETIF_F_HW_VLAN_CTAG_RX |
6812 NETIF_F_HW_VLAN_CTAG_FILTER |
6813 NETIF_F_IPV6_CSUM |
6814 NETIF_F_TSO |
Jesse Brandeburg059dab62014-04-01 09:07:20 +00006815 NETIF_F_TSO_ECN |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006816 NETIF_F_TSO6 |
6817 NETIF_F_RXCSUM |
6818 NETIF_F_RXHASH |
6819 0;
6820
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00006821 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
6822 netdev->features |= NETIF_F_NTUPLE;
6823
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006824 /* copy netdev features into list of user selectable features */
6825 netdev->hw_features |= netdev->features;
6826
6827 if (vsi->type == I40E_VSI_MAIN) {
6828 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
6829 memcpy(mac_addr, hw->mac.perm_addr, ETH_ALEN);
6830 } else {
6831 /* relate the VSI_VMDQ name to the VSI_MAIN name */
6832 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
6833 pf->vsi[pf->lan_vsi]->netdev->name);
6834 random_ether_addr(mac_addr);
6835 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
6836 }
Greg Rose1a103702013-11-28 06:42:39 +00006837 i40e_add_filter(vsi, brdcast, I40E_VLAN_ANY, false, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006838
6839 memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
6840 memcpy(netdev->perm_addr, mac_addr, ETH_ALEN);
6841 /* vlan gets same features (except vlan offload)
6842 * after any tweaks for specific VSI types
6843 */
6844 netdev->vlan_features = netdev->features & ~(NETIF_F_HW_VLAN_CTAG_TX |
6845 NETIF_F_HW_VLAN_CTAG_RX |
6846 NETIF_F_HW_VLAN_CTAG_FILTER);
6847 netdev->priv_flags |= IFF_UNICAST_FLT;
6848 netdev->priv_flags |= IFF_SUPP_NOFCS;
6849 /* Setup netdev TC information */
6850 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
6851
6852 netdev->netdev_ops = &i40e_netdev_ops;
6853 netdev->watchdog_timeo = 5 * HZ;
6854 i40e_set_ethtool_ops(netdev);
6855
6856 return 0;
6857}
6858
6859/**
6860 * i40e_vsi_delete - Delete a VSI from the switch
6861 * @vsi: the VSI being removed
6862 *
6863 * Returns 0 on success, negative value on failure
6864 **/
6865static void i40e_vsi_delete(struct i40e_vsi *vsi)
6866{
6867 /* remove default VSI is not allowed */
6868 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
6869 return;
6870
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006871 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006872}
6873
6874/**
6875 * i40e_add_vsi - Add a VSI to the switch
6876 * @vsi: the VSI being configured
6877 *
6878 * This initializes a VSI context depending on the VSI type to be added and
6879 * passes it down to the add_vsi aq command.
6880 **/
6881static int i40e_add_vsi(struct i40e_vsi *vsi)
6882{
6883 int ret = -ENODEV;
6884 struct i40e_mac_filter *f, *ftmp;
6885 struct i40e_pf *pf = vsi->back;
6886 struct i40e_hw *hw = &pf->hw;
6887 struct i40e_vsi_context ctxt;
6888 u8 enabled_tc = 0x1; /* TC0 enabled */
6889 int f_count = 0;
6890
6891 memset(&ctxt, 0, sizeof(ctxt));
6892 switch (vsi->type) {
6893 case I40E_VSI_MAIN:
6894 /* The PF's main VSI is already setup as part of the
6895 * device initialization, so we'll not bother with
6896 * the add_vsi call, but we will retrieve the current
6897 * VSI context.
6898 */
6899 ctxt.seid = pf->main_vsi_seid;
6900 ctxt.pf_num = pf->hw.pf_id;
6901 ctxt.vf_num = 0;
6902 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6903 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6904 if (ret) {
6905 dev_info(&pf->pdev->dev,
6906 "couldn't get pf vsi config, err %d, aq_err %d\n",
6907 ret, pf->hw.aq.asq_last_status);
6908 return -ENOENT;
6909 }
6910 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
6911 vsi->info.valid_sections = 0;
6912
6913 vsi->seid = ctxt.seid;
6914 vsi->id = ctxt.vsi_number;
6915
6916 enabled_tc = i40e_pf_get_tc_map(pf);
6917
6918 /* MFP mode setup queue map and update VSI */
6919 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
6920 memset(&ctxt, 0, sizeof(ctxt));
6921 ctxt.seid = pf->main_vsi_seid;
6922 ctxt.pf_num = pf->hw.pf_id;
6923 ctxt.vf_num = 0;
6924 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
6925 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
6926 if (ret) {
6927 dev_info(&pf->pdev->dev,
6928 "update vsi failed, aq_err=%d\n",
6929 pf->hw.aq.asq_last_status);
6930 ret = -ENOENT;
6931 goto err;
6932 }
6933 /* update the local VSI info queue map */
6934 i40e_vsi_update_queue_map(vsi, &ctxt);
6935 vsi->info.valid_sections = 0;
6936 } else {
6937 /* Default/Main VSI is only enabled for TC0
6938 * reconfigure it to enable all TCs that are
6939 * available on the port in SFP mode.
6940 */
6941 ret = i40e_vsi_config_tc(vsi, enabled_tc);
6942 if (ret) {
6943 dev_info(&pf->pdev->dev,
6944 "failed to configure TCs for main VSI tc_map 0x%08x, err %d, aq_err %d\n",
6945 enabled_tc, ret,
6946 pf->hw.aq.asq_last_status);
6947 ret = -ENOENT;
6948 }
6949 }
6950 break;
6951
6952 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006953 ctxt.pf_num = hw->pf_id;
6954 ctxt.vf_num = 0;
6955 ctxt.uplink_seid = vsi->uplink_seid;
6956 ctxt.connection_type = 0x1; /* regular data port */
6957 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006958 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006959 break;
6960
6961 case I40E_VSI_VMDQ2:
6962 ctxt.pf_num = hw->pf_id;
6963 ctxt.vf_num = 0;
6964 ctxt.uplink_seid = vsi->uplink_seid;
6965 ctxt.connection_type = 0x1; /* regular data port */
6966 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6967
6968 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6969
6970 /* This VSI is connected to VEB so the switch_id
6971 * should be set to zero by default.
6972 */
6973 ctxt.info.switch_id = 0;
6974 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
6975 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6976
6977 /* Setup the VSI tx/rx queue map for TC0 only for now */
6978 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
6979 break;
6980
6981 case I40E_VSI_SRIOV:
6982 ctxt.pf_num = hw->pf_id;
6983 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
6984 ctxt.uplink_seid = vsi->uplink_seid;
6985 ctxt.connection_type = 0x1; /* regular data port */
6986 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
6987
6988 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6989
6990 /* This VSI is connected to VEB so the switch_id
6991 * should be set to zero by default.
6992 */
6993 ctxt.info.switch_id = cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6994
6995 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
6996 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
6997 /* Setup the VSI tx/rx queue map for TC0 only for now */
6998 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
6999 break;
7000
7001 default:
7002 return -ENODEV;
7003 }
7004
7005 if (vsi->type != I40E_VSI_MAIN) {
7006 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
7007 if (ret) {
7008 dev_info(&vsi->back->pdev->dev,
7009 "add vsi failed, aq_err=%d\n",
7010 vsi->back->hw.aq.asq_last_status);
7011 ret = -ENOENT;
7012 goto err;
7013 }
7014 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
7015 vsi->info.valid_sections = 0;
7016 vsi->seid = ctxt.seid;
7017 vsi->id = ctxt.vsi_number;
7018 }
7019
7020 /* If macvlan filters already exist, force them to get loaded */
7021 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
7022 f->changed = true;
7023 f_count++;
7024 }
7025 if (f_count) {
7026 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
7027 pf->flags |= I40E_FLAG_FILTER_SYNC;
7028 }
7029
7030 /* Update VSI BW information */
7031 ret = i40e_vsi_get_bw_info(vsi);
7032 if (ret) {
7033 dev_info(&pf->pdev->dev,
7034 "couldn't get vsi bw info, err %d, aq_err %d\n",
7035 ret, pf->hw.aq.asq_last_status);
7036 /* VSI is already added so not tearing that up */
7037 ret = 0;
7038 }
7039
7040err:
7041 return ret;
7042}
7043
7044/**
7045 * i40e_vsi_release - Delete a VSI and free its resources
7046 * @vsi: the VSI being removed
7047 *
7048 * Returns 0 on success or < 0 on error
7049 **/
7050int i40e_vsi_release(struct i40e_vsi *vsi)
7051{
7052 struct i40e_mac_filter *f, *ftmp;
7053 struct i40e_veb *veb = NULL;
7054 struct i40e_pf *pf;
7055 u16 uplink_seid;
7056 int i, n;
7057
7058 pf = vsi->back;
7059
7060 /* release of a VEB-owner or last VSI is not allowed */
7061 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
7062 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
7063 vsi->seid, vsi->uplink_seid);
7064 return -ENODEV;
7065 }
7066 if (vsi == pf->vsi[pf->lan_vsi] &&
7067 !test_bit(__I40E_DOWN, &pf->state)) {
7068 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
7069 return -ENODEV;
7070 }
7071
7072 uplink_seid = vsi->uplink_seid;
7073 if (vsi->type != I40E_VSI_SRIOV) {
7074 if (vsi->netdev_registered) {
7075 vsi->netdev_registered = false;
7076 if (vsi->netdev) {
7077 /* results in a call to i40e_close() */
7078 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007079 }
7080 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00007081 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007082 }
7083 i40e_vsi_disable_irq(vsi);
7084 }
7085
7086 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
7087 i40e_del_filter(vsi, f->macaddr, f->vlan,
7088 f->is_vf, f->is_netdev);
7089 i40e_sync_vsi_filters(vsi);
7090
7091 i40e_vsi_delete(vsi);
7092 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00007093 if (vsi->netdev) {
7094 free_netdev(vsi->netdev);
7095 vsi->netdev = NULL;
7096 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007097 i40e_vsi_clear_rings(vsi);
7098 i40e_vsi_clear(vsi);
7099
7100 /* If this was the last thing on the VEB, except for the
7101 * controlling VSI, remove the VEB, which puts the controlling
7102 * VSI onto the next level down in the switch.
7103 *
7104 * Well, okay, there's one more exception here: don't remove
7105 * the orphan VEBs yet. We'll wait for an explicit remove request
7106 * from up the network stack.
7107 */
7108 for (n = 0, i = 0; i < pf->hw.func_caps.num_vsis; i++) {
7109 if (pf->vsi[i] &&
7110 pf->vsi[i]->uplink_seid == uplink_seid &&
7111 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
7112 n++; /* count the VSIs */
7113 }
7114 }
7115 for (i = 0; i < I40E_MAX_VEB; i++) {
7116 if (!pf->veb[i])
7117 continue;
7118 if (pf->veb[i]->uplink_seid == uplink_seid)
7119 n++; /* count the VEBs */
7120 if (pf->veb[i]->seid == uplink_seid)
7121 veb = pf->veb[i];
7122 }
7123 if (n == 0 && veb && veb->uplink_seid != 0)
7124 i40e_veb_release(veb);
7125
7126 return 0;
7127}
7128
7129/**
7130 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
7131 * @vsi: ptr to the VSI
7132 *
7133 * This should only be called after i40e_vsi_mem_alloc() which allocates the
7134 * corresponding SW VSI structure and initializes num_queue_pairs for the
7135 * newly allocated VSI.
7136 *
7137 * Returns 0 on success or negative on failure
7138 **/
7139static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
7140{
7141 int ret = -ENOENT;
7142 struct i40e_pf *pf = vsi->back;
7143
Alexander Duyck493fb302013-09-28 07:01:44 +00007144 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007145 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
7146 vsi->seid);
7147 return -EEXIST;
7148 }
7149
7150 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00007151 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007152 vsi->seid, vsi->base_vector);
7153 return -EEXIST;
7154 }
7155
Greg Rose90e04072014-03-06 08:59:57 +00007156 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007157 if (ret) {
7158 dev_info(&pf->pdev->dev,
7159 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
7160 vsi->num_q_vectors, vsi->seid, ret);
7161 vsi->num_q_vectors = 0;
7162 goto vector_setup_out;
7163 }
7164
Shannon Nelson958a3e32013-09-28 07:13:28 +00007165 if (vsi->num_q_vectors)
7166 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
7167 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007168 if (vsi->base_vector < 0) {
7169 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00007170 "failed to get queue tracking for VSI %d, err=%d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007171 vsi->seid, vsi->base_vector);
7172 i40e_vsi_free_q_vectors(vsi);
7173 ret = -ENOENT;
7174 goto vector_setup_out;
7175 }
7176
7177vector_setup_out:
7178 return ret;
7179}
7180
7181/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007182 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
7183 * @vsi: pointer to the vsi.
7184 *
7185 * This re-allocates a vsi's queue resources.
7186 *
7187 * Returns pointer to the successfully allocated and configured VSI sw struct
7188 * on success, otherwise returns NULL on failure.
7189 **/
7190static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
7191{
7192 struct i40e_pf *pf = vsi->back;
7193 u8 enabled_tc;
7194 int ret;
7195
7196 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7197 i40e_vsi_clear_rings(vsi);
7198
7199 i40e_vsi_free_arrays(vsi, false);
7200 i40e_set_num_rings_in_vsi(vsi);
7201 ret = i40e_vsi_alloc_arrays(vsi, false);
7202 if (ret)
7203 goto err_vsi;
7204
7205 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
7206 if (ret < 0) {
7207 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
7208 vsi->seid, ret);
7209 goto err_vsi;
7210 }
7211 vsi->base_queue = ret;
7212
7213 /* Update the FW view of the VSI. Force a reset of TC and queue
7214 * layout configurations.
7215 */
7216 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
7217 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
7218 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
7219 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
7220
7221 /* assign it some queues */
7222 ret = i40e_alloc_rings(vsi);
7223 if (ret)
7224 goto err_rings;
7225
7226 /* map all of the rings to the q_vectors */
7227 i40e_vsi_map_rings_to_vectors(vsi);
7228 return vsi;
7229
7230err_rings:
7231 i40e_vsi_free_q_vectors(vsi);
7232 if (vsi->netdev_registered) {
7233 vsi->netdev_registered = false;
7234 unregister_netdev(vsi->netdev);
7235 free_netdev(vsi->netdev);
7236 vsi->netdev = NULL;
7237 }
7238 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
7239err_vsi:
7240 i40e_vsi_clear(vsi);
7241 return NULL;
7242}
7243
7244/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007245 * i40e_vsi_setup - Set up a VSI by a given type
7246 * @pf: board private structure
7247 * @type: VSI type
7248 * @uplink_seid: the switch element to link to
7249 * @param1: usage depends upon VSI type. For VF types, indicates VF id
7250 *
7251 * This allocates the sw VSI structure and its queue resources, then add a VSI
7252 * to the identified VEB.
7253 *
7254 * Returns pointer to the successfully allocated and configure VSI sw struct on
7255 * success, otherwise returns NULL on failure.
7256 **/
7257struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
7258 u16 uplink_seid, u32 param1)
7259{
7260 struct i40e_vsi *vsi = NULL;
7261 struct i40e_veb *veb = NULL;
7262 int ret, i;
7263 int v_idx;
7264
7265 /* The requested uplink_seid must be either
7266 * - the PF's port seid
7267 * no VEB is needed because this is the PF
7268 * or this is a Flow Director special case VSI
7269 * - seid of an existing VEB
7270 * - seid of a VSI that owns an existing VEB
7271 * - seid of a VSI that doesn't own a VEB
7272 * a new VEB is created and the VSI becomes the owner
7273 * - seid of the PF VSI, which is what creates the first VEB
7274 * this is a special case of the previous
7275 *
7276 * Find which uplink_seid we were given and create a new VEB if needed
7277 */
7278 for (i = 0; i < I40E_MAX_VEB; i++) {
7279 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
7280 veb = pf->veb[i];
7281 break;
7282 }
7283 }
7284
7285 if (!veb && uplink_seid != pf->mac_seid) {
7286
7287 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
7288 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
7289 vsi = pf->vsi[i];
7290 break;
7291 }
7292 }
7293 if (!vsi) {
7294 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
7295 uplink_seid);
7296 return NULL;
7297 }
7298
7299 if (vsi->uplink_seid == pf->mac_seid)
7300 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
7301 vsi->tc_config.enabled_tc);
7302 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
7303 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
7304 vsi->tc_config.enabled_tc);
7305
7306 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
7307 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
7308 veb = pf->veb[i];
7309 }
7310 if (!veb) {
7311 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
7312 return NULL;
7313 }
7314
7315 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
7316 uplink_seid = veb->seid;
7317 }
7318
7319 /* get vsi sw struct */
7320 v_idx = i40e_vsi_mem_alloc(pf, type);
7321 if (v_idx < 0)
7322 goto err_alloc;
7323 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007324 if (!vsi)
7325 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007326 vsi->type = type;
7327 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
7328
7329 if (type == I40E_VSI_MAIN)
7330 pf->lan_vsi = v_idx;
7331 else if (type == I40E_VSI_SRIOV)
7332 vsi->vf_id = param1;
7333 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007334 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
7335 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007336 if (ret < 0) {
7337 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
7338 vsi->seid, ret);
7339 goto err_vsi;
7340 }
7341 vsi->base_queue = ret;
7342
7343 /* get a VSI from the hardware */
7344 vsi->uplink_seid = uplink_seid;
7345 ret = i40e_add_vsi(vsi);
7346 if (ret)
7347 goto err_vsi;
7348
7349 switch (vsi->type) {
7350 /* setup the netdev if needed */
7351 case I40E_VSI_MAIN:
7352 case I40E_VSI_VMDQ2:
7353 ret = i40e_config_netdev(vsi);
7354 if (ret)
7355 goto err_netdev;
7356 ret = register_netdev(vsi->netdev);
7357 if (ret)
7358 goto err_netdev;
7359 vsi->netdev_registered = true;
7360 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007361#ifdef CONFIG_I40E_DCB
7362 /* Setup DCB netlink interface */
7363 i40e_dcbnl_setup(vsi);
7364#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007365 /* fall through */
7366
7367 case I40E_VSI_FDIR:
7368 /* set up vectors and rings if needed */
7369 ret = i40e_vsi_setup_vectors(vsi);
7370 if (ret)
7371 goto err_msix;
7372
7373 ret = i40e_alloc_rings(vsi);
7374 if (ret)
7375 goto err_rings;
7376
7377 /* map all of the rings to the q_vectors */
7378 i40e_vsi_map_rings_to_vectors(vsi);
7379
7380 i40e_vsi_reset_stats(vsi);
7381 break;
7382
7383 default:
7384 /* no netdev or rings for the other VSI types */
7385 break;
7386 }
7387
7388 return vsi;
7389
7390err_rings:
7391 i40e_vsi_free_q_vectors(vsi);
7392err_msix:
7393 if (vsi->netdev_registered) {
7394 vsi->netdev_registered = false;
7395 unregister_netdev(vsi->netdev);
7396 free_netdev(vsi->netdev);
7397 vsi->netdev = NULL;
7398 }
7399err_netdev:
7400 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
7401err_vsi:
7402 i40e_vsi_clear(vsi);
7403err_alloc:
7404 return NULL;
7405}
7406
7407/**
7408 * i40e_veb_get_bw_info - Query VEB BW information
7409 * @veb: the veb to query
7410 *
7411 * Query the Tx scheduler BW configuration data for given VEB
7412 **/
7413static int i40e_veb_get_bw_info(struct i40e_veb *veb)
7414{
7415 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
7416 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
7417 struct i40e_pf *pf = veb->pf;
7418 struct i40e_hw *hw = &pf->hw;
7419 u32 tc_bw_max;
7420 int ret = 0;
7421 int i;
7422
7423 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
7424 &bw_data, NULL);
7425 if (ret) {
7426 dev_info(&pf->pdev->dev,
7427 "query veb bw config failed, aq_err=%d\n",
7428 hw->aq.asq_last_status);
7429 goto out;
7430 }
7431
7432 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
7433 &ets_data, NULL);
7434 if (ret) {
7435 dev_info(&pf->pdev->dev,
7436 "query veb bw ets config failed, aq_err=%d\n",
7437 hw->aq.asq_last_status);
7438 goto out;
7439 }
7440
7441 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
7442 veb->bw_max_quanta = ets_data.tc_bw_max;
7443 veb->is_abs_credits = bw_data.absolute_credits_enable;
7444 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
7445 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
7446 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7447 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
7448 veb->bw_tc_limit_credits[i] =
7449 le16_to_cpu(bw_data.tc_bw_limits[i]);
7450 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
7451 }
7452
7453out:
7454 return ret;
7455}
7456
7457/**
7458 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
7459 * @pf: board private structure
7460 *
7461 * On error: returns error code (negative)
7462 * On success: returns vsi index in PF (positive)
7463 **/
7464static int i40e_veb_mem_alloc(struct i40e_pf *pf)
7465{
7466 int ret = -ENOENT;
7467 struct i40e_veb *veb;
7468 int i;
7469
7470 /* Need to protect the allocation of switch elements at the PF level */
7471 mutex_lock(&pf->switch_mutex);
7472
7473 /* VEB list may be fragmented if VEB creation/destruction has
7474 * been happening. We can afford to do a quick scan to look
7475 * for any free slots in the list.
7476 *
7477 * find next empty veb slot, looping back around if necessary
7478 */
7479 i = 0;
7480 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
7481 i++;
7482 if (i >= I40E_MAX_VEB) {
7483 ret = -ENOMEM;
7484 goto err_alloc_veb; /* out of VEB slots! */
7485 }
7486
7487 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
7488 if (!veb) {
7489 ret = -ENOMEM;
7490 goto err_alloc_veb;
7491 }
7492 veb->pf = pf;
7493 veb->idx = i;
7494 veb->enabled_tc = 1;
7495
7496 pf->veb[i] = veb;
7497 ret = i;
7498err_alloc_veb:
7499 mutex_unlock(&pf->switch_mutex);
7500 return ret;
7501}
7502
7503/**
7504 * i40e_switch_branch_release - Delete a branch of the switch tree
7505 * @branch: where to start deleting
7506 *
7507 * This uses recursion to find the tips of the branch to be
7508 * removed, deleting until we get back to and can delete this VEB.
7509 **/
7510static void i40e_switch_branch_release(struct i40e_veb *branch)
7511{
7512 struct i40e_pf *pf = branch->pf;
7513 u16 branch_seid = branch->seid;
7514 u16 veb_idx = branch->idx;
7515 int i;
7516
7517 /* release any VEBs on this VEB - RECURSION */
7518 for (i = 0; i < I40E_MAX_VEB; i++) {
7519 if (!pf->veb[i])
7520 continue;
7521 if (pf->veb[i]->uplink_seid == branch->seid)
7522 i40e_switch_branch_release(pf->veb[i]);
7523 }
7524
7525 /* Release the VSIs on this VEB, but not the owner VSI.
7526 *
7527 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
7528 * the VEB itself, so don't use (*branch) after this loop.
7529 */
7530 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
7531 if (!pf->vsi[i])
7532 continue;
7533 if (pf->vsi[i]->uplink_seid == branch_seid &&
7534 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
7535 i40e_vsi_release(pf->vsi[i]);
7536 }
7537 }
7538
7539 /* There's one corner case where the VEB might not have been
7540 * removed, so double check it here and remove it if needed.
7541 * This case happens if the veb was created from the debugfs
7542 * commands and no VSIs were added to it.
7543 */
7544 if (pf->veb[veb_idx])
7545 i40e_veb_release(pf->veb[veb_idx]);
7546}
7547
7548/**
7549 * i40e_veb_clear - remove veb struct
7550 * @veb: the veb to remove
7551 **/
7552static void i40e_veb_clear(struct i40e_veb *veb)
7553{
7554 if (!veb)
7555 return;
7556
7557 if (veb->pf) {
7558 struct i40e_pf *pf = veb->pf;
7559
7560 mutex_lock(&pf->switch_mutex);
7561 if (pf->veb[veb->idx] == veb)
7562 pf->veb[veb->idx] = NULL;
7563 mutex_unlock(&pf->switch_mutex);
7564 }
7565
7566 kfree(veb);
7567}
7568
7569/**
7570 * i40e_veb_release - Delete a VEB and free its resources
7571 * @veb: the VEB being removed
7572 **/
7573void i40e_veb_release(struct i40e_veb *veb)
7574{
7575 struct i40e_vsi *vsi = NULL;
7576 struct i40e_pf *pf;
7577 int i, n = 0;
7578
7579 pf = veb->pf;
7580
7581 /* find the remaining VSI and check for extras */
7582 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
7583 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
7584 n++;
7585 vsi = pf->vsi[i];
7586 }
7587 }
7588 if (n != 1) {
7589 dev_info(&pf->pdev->dev,
7590 "can't remove VEB %d with %d VSIs left\n",
7591 veb->seid, n);
7592 return;
7593 }
7594
7595 /* move the remaining VSI to uplink veb */
7596 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
7597 if (veb->uplink_seid) {
7598 vsi->uplink_seid = veb->uplink_seid;
7599 if (veb->uplink_seid == pf->mac_seid)
7600 vsi->veb_idx = I40E_NO_VEB;
7601 else
7602 vsi->veb_idx = veb->veb_idx;
7603 } else {
7604 /* floating VEB */
7605 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
7606 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
7607 }
7608
7609 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
7610 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007611}
7612
7613/**
7614 * i40e_add_veb - create the VEB in the switch
7615 * @veb: the VEB to be instantiated
7616 * @vsi: the controlling VSI
7617 **/
7618static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
7619{
Greg Rose56747262013-11-28 06:39:37 +00007620 bool is_default = false;
Kevin Scotte1c51b952013-11-20 10:02:51 +00007621 bool is_cloud = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007622 int ret;
7623
7624 /* get a VEB from the hardware */
7625 ret = i40e_aq_add_veb(&veb->pf->hw, veb->uplink_seid, vsi->seid,
Kevin Scotte1c51b952013-11-20 10:02:51 +00007626 veb->enabled_tc, is_default,
7627 is_cloud, &veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007628 if (ret) {
7629 dev_info(&veb->pf->pdev->dev,
7630 "couldn't add VEB, err %d, aq_err %d\n",
7631 ret, veb->pf->hw.aq.asq_last_status);
7632 return -EPERM;
7633 }
7634
7635 /* get statistics counter */
7636 ret = i40e_aq_get_veb_parameters(&veb->pf->hw, veb->seid, NULL, NULL,
7637 &veb->stats_idx, NULL, NULL, NULL);
7638 if (ret) {
7639 dev_info(&veb->pf->pdev->dev,
7640 "couldn't get VEB statistics idx, err %d, aq_err %d\n",
7641 ret, veb->pf->hw.aq.asq_last_status);
7642 return -EPERM;
7643 }
7644 ret = i40e_veb_get_bw_info(veb);
7645 if (ret) {
7646 dev_info(&veb->pf->pdev->dev,
7647 "couldn't get VEB bw info, err %d, aq_err %d\n",
7648 ret, veb->pf->hw.aq.asq_last_status);
7649 i40e_aq_delete_element(&veb->pf->hw, veb->seid, NULL);
7650 return -ENOENT;
7651 }
7652
7653 vsi->uplink_seid = veb->seid;
7654 vsi->veb_idx = veb->idx;
7655 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
7656
7657 return 0;
7658}
7659
7660/**
7661 * i40e_veb_setup - Set up a VEB
7662 * @pf: board private structure
7663 * @flags: VEB setup flags
7664 * @uplink_seid: the switch element to link to
7665 * @vsi_seid: the initial VSI seid
7666 * @enabled_tc: Enabled TC bit-map
7667 *
7668 * This allocates the sw VEB structure and links it into the switch
7669 * It is possible and legal for this to be a duplicate of an already
7670 * existing VEB. It is also possible for both uplink and vsi seids
7671 * to be zero, in order to create a floating VEB.
7672 *
7673 * Returns pointer to the successfully allocated VEB sw struct on
7674 * success, otherwise returns NULL on failure.
7675 **/
7676struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
7677 u16 uplink_seid, u16 vsi_seid,
7678 u8 enabled_tc)
7679{
7680 struct i40e_veb *veb, *uplink_veb = NULL;
7681 int vsi_idx, veb_idx;
7682 int ret;
7683
7684 /* if one seid is 0, the other must be 0 to create a floating relay */
7685 if ((uplink_seid == 0 || vsi_seid == 0) &&
7686 (uplink_seid + vsi_seid != 0)) {
7687 dev_info(&pf->pdev->dev,
7688 "one, not both seid's are 0: uplink=%d vsi=%d\n",
7689 uplink_seid, vsi_seid);
7690 return NULL;
7691 }
7692
7693 /* make sure there is such a vsi and uplink */
7694 for (vsi_idx = 0; vsi_idx < pf->hw.func_caps.num_vsis; vsi_idx++)
7695 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
7696 break;
7697 if (vsi_idx >= pf->hw.func_caps.num_vsis && vsi_seid != 0) {
7698 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
7699 vsi_seid);
7700 return NULL;
7701 }
7702
7703 if (uplink_seid && uplink_seid != pf->mac_seid) {
7704 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
7705 if (pf->veb[veb_idx] &&
7706 pf->veb[veb_idx]->seid == uplink_seid) {
7707 uplink_veb = pf->veb[veb_idx];
7708 break;
7709 }
7710 }
7711 if (!uplink_veb) {
7712 dev_info(&pf->pdev->dev,
7713 "uplink seid %d not found\n", uplink_seid);
7714 return NULL;
7715 }
7716 }
7717
7718 /* get veb sw struct */
7719 veb_idx = i40e_veb_mem_alloc(pf);
7720 if (veb_idx < 0)
7721 goto err_alloc;
7722 veb = pf->veb[veb_idx];
7723 veb->flags = flags;
7724 veb->uplink_seid = uplink_seid;
7725 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
7726 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
7727
7728 /* create the VEB in the switch */
7729 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
7730 if (ret)
7731 goto err_veb;
7732
7733 return veb;
7734
7735err_veb:
7736 i40e_veb_clear(veb);
7737err_alloc:
7738 return NULL;
7739}
7740
7741/**
7742 * i40e_setup_pf_switch_element - set pf vars based on switch type
7743 * @pf: board private structure
7744 * @ele: element we are building info from
7745 * @num_reported: total number of elements
7746 * @printconfig: should we print the contents
7747 *
7748 * helper function to assist in extracting a few useful SEID values.
7749 **/
7750static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
7751 struct i40e_aqc_switch_config_element_resp *ele,
7752 u16 num_reported, bool printconfig)
7753{
7754 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
7755 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
7756 u8 element_type = ele->element_type;
7757 u16 seid = le16_to_cpu(ele->seid);
7758
7759 if (printconfig)
7760 dev_info(&pf->pdev->dev,
7761 "type=%d seid=%d uplink=%d downlink=%d\n",
7762 element_type, seid, uplink_seid, downlink_seid);
7763
7764 switch (element_type) {
7765 case I40E_SWITCH_ELEMENT_TYPE_MAC:
7766 pf->mac_seid = seid;
7767 break;
7768 case I40E_SWITCH_ELEMENT_TYPE_VEB:
7769 /* Main VEB? */
7770 if (uplink_seid != pf->mac_seid)
7771 break;
7772 if (pf->lan_veb == I40E_NO_VEB) {
7773 int v;
7774
7775 /* find existing or else empty VEB */
7776 for (v = 0; v < I40E_MAX_VEB; v++) {
7777 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
7778 pf->lan_veb = v;
7779 break;
7780 }
7781 }
7782 if (pf->lan_veb == I40E_NO_VEB) {
7783 v = i40e_veb_mem_alloc(pf);
7784 if (v < 0)
7785 break;
7786 pf->lan_veb = v;
7787 }
7788 }
7789
7790 pf->veb[pf->lan_veb]->seid = seid;
7791 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
7792 pf->veb[pf->lan_veb]->pf = pf;
7793 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
7794 break;
7795 case I40E_SWITCH_ELEMENT_TYPE_VSI:
7796 if (num_reported != 1)
7797 break;
7798 /* This is immediately after a reset so we can assume this is
7799 * the PF's VSI
7800 */
7801 pf->mac_seid = uplink_seid;
7802 pf->pf_seid = downlink_seid;
7803 pf->main_vsi_seid = seid;
7804 if (printconfig)
7805 dev_info(&pf->pdev->dev,
7806 "pf_seid=%d main_vsi_seid=%d\n",
7807 pf->pf_seid, pf->main_vsi_seid);
7808 break;
7809 case I40E_SWITCH_ELEMENT_TYPE_PF:
7810 case I40E_SWITCH_ELEMENT_TYPE_VF:
7811 case I40E_SWITCH_ELEMENT_TYPE_EMP:
7812 case I40E_SWITCH_ELEMENT_TYPE_BMC:
7813 case I40E_SWITCH_ELEMENT_TYPE_PE:
7814 case I40E_SWITCH_ELEMENT_TYPE_PA:
7815 /* ignore these for now */
7816 break;
7817 default:
7818 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
7819 element_type, seid);
7820 break;
7821 }
7822}
7823
7824/**
7825 * i40e_fetch_switch_configuration - Get switch config from firmware
7826 * @pf: board private structure
7827 * @printconfig: should we print the contents
7828 *
7829 * Get the current switch configuration from the device and
7830 * extract a few useful SEID values.
7831 **/
7832int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
7833{
7834 struct i40e_aqc_get_switch_config_resp *sw_config;
7835 u16 next_seid = 0;
7836 int ret = 0;
7837 u8 *aq_buf;
7838 int i;
7839
7840 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
7841 if (!aq_buf)
7842 return -ENOMEM;
7843
7844 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
7845 do {
7846 u16 num_reported, num_total;
7847
7848 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
7849 I40E_AQ_LARGE_BUF,
7850 &next_seid, NULL);
7851 if (ret) {
7852 dev_info(&pf->pdev->dev,
7853 "get switch config failed %d aq_err=%x\n",
7854 ret, pf->hw.aq.asq_last_status);
7855 kfree(aq_buf);
7856 return -ENOENT;
7857 }
7858
7859 num_reported = le16_to_cpu(sw_config->header.num_reported);
7860 num_total = le16_to_cpu(sw_config->header.num_total);
7861
7862 if (printconfig)
7863 dev_info(&pf->pdev->dev,
7864 "header: %d reported %d total\n",
7865 num_reported, num_total);
7866
7867 if (num_reported) {
7868 int sz = sizeof(*sw_config) * num_reported;
7869
7870 kfree(pf->sw_config);
7871 pf->sw_config = kzalloc(sz, GFP_KERNEL);
7872 if (pf->sw_config)
7873 memcpy(pf->sw_config, sw_config, sz);
7874 }
7875
7876 for (i = 0; i < num_reported; i++) {
7877 struct i40e_aqc_switch_config_element_resp *ele =
7878 &sw_config->element[i];
7879
7880 i40e_setup_pf_switch_element(pf, ele, num_reported,
7881 printconfig);
7882 }
7883 } while (next_seid != 0);
7884
7885 kfree(aq_buf);
7886 return ret;
7887}
7888
7889/**
7890 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
7891 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007892 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007893 *
7894 * Returns 0 on success, negative value on failure
7895 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007896static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007897{
Jesse Brandeburg895106a2013-11-26 10:49:16 +00007898 u32 rxfc = 0, txfc = 0, rxfc_reg;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007899 int ret;
7900
7901 /* find out what's out there already */
7902 ret = i40e_fetch_switch_configuration(pf, false);
7903 if (ret) {
7904 dev_info(&pf->pdev->dev,
7905 "couldn't fetch switch config, err %d, aq_err %d\n",
7906 ret, pf->hw.aq.asq_last_status);
7907 return ret;
7908 }
7909 i40e_pf_reset_stats(pf);
7910
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007911 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007912 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007913 struct i40e_vsi *vsi = NULL;
7914 u16 uplink_seid;
7915
7916 /* Set up the PF VSI associated with the PF's main VSI
7917 * that is already in the HW switch
7918 */
7919 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
7920 uplink_seid = pf->veb[pf->lan_veb]->seid;
7921 else
7922 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007923 if (pf->lan_vsi == I40E_NO_VSI)
7924 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
7925 else if (reinit)
7926 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007927 if (!vsi) {
7928 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
7929 i40e_fdir_teardown(pf);
7930 return -EAGAIN;
7931 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007932 } else {
7933 /* force a reset of TC and queue layout configurations */
7934 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
7935 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
7936 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
7937 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
7938 }
7939 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
7940
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007941 i40e_fdir_sb_setup(pf);
7942
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007943 /* Setup static PF queue filter control settings */
7944 ret = i40e_setup_pf_filter_control(pf);
7945 if (ret) {
7946 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
7947 ret);
7948 /* Failure here should not stop continuing other steps */
7949 }
7950
7951 /* enable RSS in the HW, even for only one queue, as the stack can use
7952 * the hash
7953 */
7954 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
7955 i40e_config_rss(pf);
7956
7957 /* fill in link information and enable LSE reporting */
7958 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
7959 i40e_link_event(pf);
7960
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007961 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007962 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
7963 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007964 /* requested_mode is set in probe or by ethtool */
7965 if (!pf->fc_autoneg_status)
7966 goto no_autoneg;
7967
7968 if ((pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX) &&
7969 (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007970 pf->hw.fc.current_mode = I40E_FC_FULL;
7971 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX)
7972 pf->hw.fc.current_mode = I40E_FC_TX_PAUSE;
7973 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX)
7974 pf->hw.fc.current_mode = I40E_FC_RX_PAUSE;
7975 else
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007976 pf->hw.fc.current_mode = I40E_FC_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007977
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007978 /* sync the flow control settings with the auto-neg values */
7979 switch (pf->hw.fc.current_mode) {
7980 case I40E_FC_FULL:
7981 txfc = 1;
7982 rxfc = 1;
7983 break;
7984 case I40E_FC_TX_PAUSE:
7985 txfc = 1;
7986 rxfc = 0;
7987 break;
7988 case I40E_FC_RX_PAUSE:
7989 txfc = 0;
7990 rxfc = 1;
7991 break;
7992 case I40E_FC_NONE:
7993 case I40E_FC_DEFAULT:
7994 txfc = 0;
7995 rxfc = 0;
7996 break;
7997 case I40E_FC_PFC:
7998 /* TBD */
7999 break;
8000 /* no default case, we have to handle all possibilities here */
8001 }
8002
8003 wr32(&pf->hw, I40E_PRTDCB_FCCFG, txfc << I40E_PRTDCB_FCCFG_TFCE_SHIFT);
8004
8005 rxfc_reg = rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
8006 ~I40E_PRTDCB_MFLCN_RFCE_MASK;
8007 rxfc_reg |= (rxfc << I40E_PRTDCB_MFLCN_RFCE_SHIFT);
8008
8009 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rxfc_reg);
8010
8011 goto fc_complete;
8012
8013no_autoneg:
8014 /* disable L2 flow control, user can turn it on if they wish */
8015 wr32(&pf->hw, I40E_PRTDCB_FCCFG, 0);
8016 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
8017 ~I40E_PRTDCB_MFLCN_RFCE_MASK);
8018
8019fc_complete:
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008020 i40e_ptp_init(pf);
8021
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008022 return ret;
8023}
8024
8025/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008026 * i40e_determine_queue_usage - Work out queue distribution
8027 * @pf: board private structure
8028 **/
8029static void i40e_determine_queue_usage(struct i40e_pf *pf)
8030{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008031 int queues_left;
8032
8033 pf->num_lan_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008034
8035 /* Find the max queues to be put into basic use. We'll always be
8036 * using TC0, whether or not DCB is running, and TC0 will get the
8037 * big RSS set.
8038 */
8039 queues_left = pf->hw.func_caps.num_tx_qp;
8040
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008041 if ((queues_left == 1) ||
8042 !(pf->flags & I40E_FLAG_MSIX_ENABLED) ||
8043 !(pf->flags & (I40E_FLAG_RSS_ENABLED | I40E_FLAG_FD_SB_ENABLED |
8044 I40E_FLAG_DCB_ENABLED))) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008045 /* one qp for PF, no queues for anything else */
8046 queues_left = 0;
8047 pf->rss_size = pf->num_lan_qps = 1;
8048
8049 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008050 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
8051 I40E_FLAG_FD_SB_ENABLED |
8052 I40E_FLAG_FD_ATR_ENABLED |
8053 I40E_FLAG_DCB_ENABLED |
8054 I40E_FLAG_SRIOV_ENABLED |
8055 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008056 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008057 /* Not enough queues for all TCs */
8058 if ((pf->flags & I40E_FLAG_DCB_ENABLED) &&
8059 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
8060 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8061 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
8062 }
8063 pf->num_lan_qps = pf->rss_size_max;
8064 queues_left -= pf->num_lan_qps;
8065 }
8066
8067 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8068 if (queues_left > 1) {
8069 queues_left -= 1; /* save 1 queue for FD */
8070 } else {
8071 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8072 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
8073 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008074 }
8075
8076 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
8077 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008078 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
8079 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008080 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
8081 }
8082
8083 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
8084 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
8085 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
8086 (queues_left / pf->num_vmdq_qps));
8087 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
8088 }
8089
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008090 pf->queues_left = queues_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008091}
8092
8093/**
8094 * i40e_setup_pf_filter_control - Setup PF static filter control
8095 * @pf: PF to be setup
8096 *
8097 * i40e_setup_pf_filter_control sets up a pf's initial filter control
8098 * settings. If PE/FCoE are enabled then it will also set the per PF
8099 * based filter sizes required for them. It also enables Flow director,
8100 * ethertype and macvlan type filter settings for the pf.
8101 *
8102 * Returns 0 on success, negative on failure
8103 **/
8104static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
8105{
8106 struct i40e_filter_control_settings *settings = &pf->filter_settings;
8107
8108 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
8109
8110 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008111 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008112 settings->enable_fdir = true;
8113
8114 /* Ethtype and MACVLAN filters enabled for PF */
8115 settings->enable_ethtype = true;
8116 settings->enable_macvlan = true;
8117
8118 if (i40e_set_filter_control(&pf->hw, settings))
8119 return -ENOENT;
8120
8121 return 0;
8122}
8123
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008124#define INFO_STRING_LEN 255
8125static void i40e_print_features(struct i40e_pf *pf)
8126{
8127 struct i40e_hw *hw = &pf->hw;
8128 char *buf, *string;
8129
8130 string = kzalloc(INFO_STRING_LEN, GFP_KERNEL);
8131 if (!string) {
8132 dev_err(&pf->pdev->dev, "Features string allocation failed\n");
8133 return;
8134 }
8135
8136 buf = string;
8137
8138 buf += sprintf(string, "Features: PF-id[%d] ", hw->pf_id);
8139#ifdef CONFIG_PCI_IOV
8140 buf += sprintf(buf, "VFs: %d ", pf->num_req_vfs);
8141#endif
8142 buf += sprintf(buf, "VSIs: %d QP: %d ", pf->hw.func_caps.num_vsis,
8143 pf->vsi[pf->lan_vsi]->num_queue_pairs);
8144
8145 if (pf->flags & I40E_FLAG_RSS_ENABLED)
8146 buf += sprintf(buf, "RSS ");
8147 buf += sprintf(buf, "FDir ");
8148 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
8149 buf += sprintf(buf, "ATR ");
8150 if (pf->flags & I40E_FLAG_FD_SB_ENABLED)
8151 buf += sprintf(buf, "NTUPLE ");
8152 if (pf->flags & I40E_FLAG_DCB_ENABLED)
8153 buf += sprintf(buf, "DCB ");
8154 if (pf->flags & I40E_FLAG_PTP)
8155 buf += sprintf(buf, "PTP ");
8156
8157 BUG_ON(buf > (string + INFO_STRING_LEN));
8158 dev_info(&pf->pdev->dev, "%s\n", string);
8159 kfree(string);
8160}
8161
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008162/**
8163 * i40e_probe - Device initialization routine
8164 * @pdev: PCI device information struct
8165 * @ent: entry in i40e_pci_tbl
8166 *
8167 * i40e_probe initializes a pf identified by a pci_dev structure.
8168 * The OS initialization, configuring of the pf private structure,
8169 * and a hardware reset occur.
8170 *
8171 * Returns 0 on success, negative on failure
8172 **/
8173static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
8174{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008175 struct i40e_pf *pf;
8176 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008177 static u16 pfs_found;
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008178 u16 link_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008179 int err = 0;
8180 u32 len;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008181 u32 i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008182
8183 err = pci_enable_device_mem(pdev);
8184 if (err)
8185 return err;
8186
8187 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +00008188 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00008189 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00008190 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
8191 if (err) {
8192 dev_err(&pdev->dev,
8193 "DMA configuration failed: 0x%x\n", err);
8194 goto err_dma;
8195 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008196 }
8197
8198 /* set up pci connections */
8199 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
8200 IORESOURCE_MEM), i40e_driver_name);
8201 if (err) {
8202 dev_info(&pdev->dev,
8203 "pci_request_selected_regions failed %d\n", err);
8204 goto err_pci_reg;
8205 }
8206
8207 pci_enable_pcie_error_reporting(pdev);
8208 pci_set_master(pdev);
8209
8210 /* Now that we have a PCI connection, we need to do the
8211 * low level device setup. This is primarily setting up
8212 * the Admin Queue structures and then querying for the
8213 * device's current profile information.
8214 */
8215 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
8216 if (!pf) {
8217 err = -ENOMEM;
8218 goto err_pf_alloc;
8219 }
8220 pf->next_vsi = 0;
8221 pf->pdev = pdev;
8222 set_bit(__I40E_DOWN, &pf->state);
8223
8224 hw = &pf->hw;
8225 hw->back = pf;
8226 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
8227 pci_resource_len(pdev, 0));
8228 if (!hw->hw_addr) {
8229 err = -EIO;
8230 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
8231 (unsigned int)pci_resource_start(pdev, 0),
8232 (unsigned int)pci_resource_len(pdev, 0), err);
8233 goto err_ioremap;
8234 }
8235 hw->vendor_id = pdev->vendor;
8236 hw->device_id = pdev->device;
8237 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
8238 hw->subsystem_vendor_id = pdev->subsystem_vendor;
8239 hw->subsystem_device_id = pdev->subsystem_device;
8240 hw->bus.device = PCI_SLOT(pdev->devfn);
8241 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008242 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008243
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008244 /* do a special CORER for clearing PXE mode once at init */
8245 if (hw->revision_id == 0 &&
8246 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
8247 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
8248 i40e_flush(hw);
8249 msleep(200);
8250 pf->corer_count++;
8251
8252 i40e_clear_pxe_mode(hw);
8253 }
8254
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008255 /* Reset here to make sure all is clean and to define PF 'n' */
8256 err = i40e_pf_reset(hw);
8257 if (err) {
8258 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
8259 goto err_pf_reset;
8260 }
8261 pf->pfr_count++;
8262
8263 hw->aq.num_arq_entries = I40E_AQ_LEN;
8264 hw->aq.num_asq_entries = I40E_AQ_LEN;
8265 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
8266 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
8267 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
8268 snprintf(pf->misc_int_name, sizeof(pf->misc_int_name) - 1,
8269 "%s-pf%d:misc",
8270 dev_driver_string(&pf->pdev->dev), pf->hw.pf_id);
8271
8272 err = i40e_init_shared_code(hw);
8273 if (err) {
8274 dev_info(&pdev->dev, "init_shared_code failed: %d\n", err);
8275 goto err_pf_reset;
8276 }
8277
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008278 /* set up a default setting for link flow control */
8279 pf->hw.fc.requested_mode = I40E_FC_NONE;
8280
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008281 err = i40e_init_adminq(hw);
8282 dev_info(&pdev->dev, "%s\n", i40e_fw_version_str(hw));
8283 if (err) {
8284 dev_info(&pdev->dev,
8285 "init_adminq failed: %d expecting API %02x.%02x\n",
8286 err,
8287 I40E_FW_API_VERSION_MAJOR, I40E_FW_API_VERSION_MINOR);
8288 goto err_pf_reset;
8289 }
8290
Shannon Nelson4eb3f762014-03-06 08:59:58 +00008291 i40e_verify_eeprom(pf);
8292
Shannon Nelson6ff4ef82013-12-21 05:44:49 +00008293 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008294 err = i40e_get_capabilities(pf);
8295 if (err)
8296 goto err_adminq_setup;
8297
8298 err = i40e_sw_init(pf);
8299 if (err) {
8300 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
8301 goto err_sw_init;
8302 }
8303
8304 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
8305 hw->func_caps.num_rx_qp,
8306 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
8307 if (err) {
8308 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
8309 goto err_init_lan_hmc;
8310 }
8311
8312 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
8313 if (err) {
8314 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
8315 err = -ENOENT;
8316 goto err_configure_lan_hmc;
8317 }
8318
8319 i40e_get_mac_addr(hw, hw->mac.addr);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +00008320 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008321 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
8322 err = -EIO;
8323 goto err_mac_addr;
8324 }
8325 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
8326 memcpy(hw->mac.perm_addr, hw->mac.addr, ETH_ALEN);
8327
8328 pci_set_drvdata(pdev, pf);
8329 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008330#ifdef CONFIG_I40E_DCB
8331 err = i40e_init_pf_dcb(pf);
8332 if (err) {
8333 dev_info(&pdev->dev, "init_pf_dcb failed: %d\n", err);
8334 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh014269f2014-04-01 07:11:48 +00008335 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008336 }
8337#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008338
8339 /* set up periodic task facility */
8340 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
8341 pf->service_timer_period = HZ;
8342
8343 INIT_WORK(&pf->service_task, i40e_service_task);
8344 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
8345 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
8346 pf->link_check_timeout = jiffies;
8347
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008348 /* WoL defaults to disabled */
8349 pf->wol_en = false;
8350 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
8351
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008352 /* set up the main switch operations */
8353 i40e_determine_queue_usage(pf);
8354 i40e_init_interrupt_scheme(pf);
8355
8356 /* Set up the *vsi struct based on the number of VSIs in the HW,
8357 * and set up our local tracking of the MAIN PF vsi.
8358 */
8359 len = sizeof(struct i40e_vsi *) * pf->hw.func_caps.num_vsis;
8360 pf->vsi = kzalloc(len, GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +00008361 if (!pf->vsi) {
8362 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008363 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +00008364 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008365
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008366 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008367 if (err) {
8368 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
8369 goto err_vsis;
8370 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008371 /* if FDIR VSI was set up, start it now */
8372 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
8373 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
8374 i40e_vsi_open(pf->vsi[i]);
8375 break;
8376 }
8377 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008378
8379 /* The main driver is (mostly) up and happy. We need to set this state
8380 * before setting up the misc vector or we get a race and the vector
8381 * ends up disabled forever.
8382 */
8383 clear_bit(__I40E_DOWN, &pf->state);
8384
8385 /* In case of MSIX we are going to setup the misc vector right here
8386 * to handle admin queue events etc. In case of legacy and MSI
8387 * the misc functionality and queue processing is combined in
8388 * the same vector and that gets setup at open.
8389 */
8390 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
8391 err = i40e_setup_misc_vector(pf);
8392 if (err) {
8393 dev_info(&pdev->dev,
8394 "setup of misc vector failed: %d\n", err);
8395 goto err_vsis;
8396 }
8397 }
8398
Greg Rosedf805f62014-04-04 04:43:16 +00008399#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008400 /* prep for VF support */
8401 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +00008402 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
8403 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008404 u32 val;
8405
8406 /* disable link interrupts for VFs */
8407 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
8408 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
8409 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
8410 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -08008411
8412 if (pci_num_vf(pdev)) {
8413 dev_info(&pdev->dev,
8414 "Active VFs found, allocating resources.\n");
8415 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
8416 if (err)
8417 dev_info(&pdev->dev,
8418 "Error %d allocating resources for existing VFs\n",
8419 err);
8420 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008421 }
Greg Rosedf805f62014-04-04 04:43:16 +00008422#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008423
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008424 pfs_found++;
8425
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008426 i40e_dbg_pf_init(pf);
8427
8428 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00008429 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008430
8431 /* since everything's happy, start the service_task timer */
8432 mod_timer(&pf->service_timer,
8433 round_jiffies(jiffies + pf->service_timer_period));
8434
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008435 /* Get the negotiated link width and speed from PCI config space */
8436 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA, &link_status);
8437
8438 i40e_set_pci_config_data(hw, link_status);
8439
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00008440 dev_info(&pdev->dev, "PCI-Express: %s %s\n",
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008441 (hw->bus.speed == i40e_bus_speed_8000 ? "Speed 8.0GT/s" :
8442 hw->bus.speed == i40e_bus_speed_5000 ? "Speed 5.0GT/s" :
8443 hw->bus.speed == i40e_bus_speed_2500 ? "Speed 2.5GT/s" :
8444 "Unknown"),
8445 (hw->bus.width == i40e_bus_width_pcie_x8 ? "Width x8" :
8446 hw->bus.width == i40e_bus_width_pcie_x4 ? "Width x4" :
8447 hw->bus.width == i40e_bus_width_pcie_x2 ? "Width x2" :
8448 hw->bus.width == i40e_bus_width_pcie_x1 ? "Width x1" :
8449 "Unknown"));
8450
8451 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
8452 hw->bus.speed < i40e_bus_speed_8000) {
8453 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
8454 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
8455 }
8456
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008457 /* print a string summarizing features */
8458 i40e_print_features(pf);
8459
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008460 return 0;
8461
8462 /* Unwind what we've done if something failed in the setup */
8463err_vsis:
8464 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008465 i40e_clear_interrupt_scheme(pf);
8466 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +00008467err_switch_setup:
8468 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008469 del_timer_sync(&pf->service_timer);
8470err_mac_addr:
8471err_configure_lan_hmc:
8472 (void)i40e_shutdown_lan_hmc(hw);
8473err_init_lan_hmc:
8474 kfree(pf->qp_pile);
8475 kfree(pf->irq_pile);
8476err_sw_init:
8477err_adminq_setup:
8478 (void)i40e_shutdown_adminq(hw);
8479err_pf_reset:
8480 iounmap(hw->hw_addr);
8481err_ioremap:
8482 kfree(pf);
8483err_pf_alloc:
8484 pci_disable_pcie_error_reporting(pdev);
8485 pci_release_selected_regions(pdev,
8486 pci_select_bars(pdev, IORESOURCE_MEM));
8487err_pci_reg:
8488err_dma:
8489 pci_disable_device(pdev);
8490 return err;
8491}
8492
8493/**
8494 * i40e_remove - Device removal routine
8495 * @pdev: PCI device information struct
8496 *
8497 * i40e_remove is called by the PCI subsystem to alert the driver
8498 * that is should release a PCI device. This could be caused by a
8499 * Hot-Plug event, or because the driver is going to be removed from
8500 * memory.
8501 **/
8502static void i40e_remove(struct pci_dev *pdev)
8503{
8504 struct i40e_pf *pf = pci_get_drvdata(pdev);
8505 i40e_status ret_code;
8506 u32 reg;
8507 int i;
8508
8509 i40e_dbg_pf_exit(pf);
8510
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008511 i40e_ptp_stop(pf);
8512
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008513 /* no more scheduling of any task */
8514 set_bit(__I40E_DOWN, &pf->state);
8515 del_timer_sync(&pf->service_timer);
8516 cancel_work_sync(&pf->service_task);
8517
Mitch Williamseb2d80b2014-02-13 03:48:48 -08008518 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
8519 i40e_free_vfs(pf);
8520 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
8521 }
8522
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008523 i40e_fdir_teardown(pf);
8524
8525 /* If there is a switch structure or any orphans, remove them.
8526 * This will leave only the PF's VSI remaining.
8527 */
8528 for (i = 0; i < I40E_MAX_VEB; i++) {
8529 if (!pf->veb[i])
8530 continue;
8531
8532 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
8533 pf->veb[i]->uplink_seid == 0)
8534 i40e_switch_branch_release(pf->veb[i]);
8535 }
8536
8537 /* Now we can shutdown the PF's VSI, just before we kill
8538 * adminq and hmc.
8539 */
8540 if (pf->vsi[pf->lan_vsi])
8541 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
8542
8543 i40e_stop_misc_vector(pf);
8544 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
8545 synchronize_irq(pf->msix_entries[0].vector);
8546 free_irq(pf->msix_entries[0].vector, pf);
8547 }
8548
8549 /* shutdown and destroy the HMC */
8550 ret_code = i40e_shutdown_lan_hmc(&pf->hw);
8551 if (ret_code)
8552 dev_warn(&pdev->dev,
8553 "Failed to destroy the HMC resources: %d\n", ret_code);
8554
8555 /* shutdown the adminq */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008556 ret_code = i40e_shutdown_adminq(&pf->hw);
8557 if (ret_code)
8558 dev_warn(&pdev->dev,
8559 "Failed to destroy the Admin Queue resources: %d\n",
8560 ret_code);
8561
8562 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
8563 i40e_clear_interrupt_scheme(pf);
8564 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
8565 if (pf->vsi[i]) {
8566 i40e_vsi_clear_rings(pf->vsi[i]);
8567 i40e_vsi_clear(pf->vsi[i]);
8568 pf->vsi[i] = NULL;
8569 }
8570 }
8571
8572 for (i = 0; i < I40E_MAX_VEB; i++) {
8573 kfree(pf->veb[i]);
8574 pf->veb[i] = NULL;
8575 }
8576
8577 kfree(pf->qp_pile);
8578 kfree(pf->irq_pile);
8579 kfree(pf->sw_config);
8580 kfree(pf->vsi);
8581
8582 /* force a PF reset to clean anything leftover */
8583 reg = rd32(&pf->hw, I40E_PFGEN_CTRL);
8584 wr32(&pf->hw, I40E_PFGEN_CTRL, (reg | I40E_PFGEN_CTRL_PFSWR_MASK));
8585 i40e_flush(&pf->hw);
8586
8587 iounmap(pf->hw.hw_addr);
8588 kfree(pf);
8589 pci_release_selected_regions(pdev,
8590 pci_select_bars(pdev, IORESOURCE_MEM));
8591
8592 pci_disable_pcie_error_reporting(pdev);
8593 pci_disable_device(pdev);
8594}
8595
8596/**
8597 * i40e_pci_error_detected - warning that something funky happened in PCI land
8598 * @pdev: PCI device information struct
8599 *
8600 * Called to warn that something happened and the error handling steps
8601 * are in progress. Allows the driver to quiesce things, be ready for
8602 * remediation.
8603 **/
8604static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
8605 enum pci_channel_state error)
8606{
8607 struct i40e_pf *pf = pci_get_drvdata(pdev);
8608
8609 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
8610
8611 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008612 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
8613 rtnl_lock();
8614 i40e_prep_for_reset(pf);
8615 rtnl_unlock();
8616 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008617
8618 /* Request a slot reset */
8619 return PCI_ERS_RESULT_NEED_RESET;
8620}
8621
8622/**
8623 * i40e_pci_error_slot_reset - a PCI slot reset just happened
8624 * @pdev: PCI device information struct
8625 *
8626 * Called to find if the driver can work with the device now that
8627 * the pci slot has been reset. If a basic connection seems good
8628 * (registers are readable and have sane content) then return a
8629 * happy little PCI_ERS_RESULT_xxx.
8630 **/
8631static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
8632{
8633 struct i40e_pf *pf = pci_get_drvdata(pdev);
8634 pci_ers_result_t result;
8635 int err;
8636 u32 reg;
8637
8638 dev_info(&pdev->dev, "%s\n", __func__);
8639 if (pci_enable_device_mem(pdev)) {
8640 dev_info(&pdev->dev,
8641 "Cannot re-enable PCI device after reset.\n");
8642 result = PCI_ERS_RESULT_DISCONNECT;
8643 } else {
8644 pci_set_master(pdev);
8645 pci_restore_state(pdev);
8646 pci_save_state(pdev);
8647 pci_wake_from_d3(pdev, false);
8648
8649 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8650 if (reg == 0)
8651 result = PCI_ERS_RESULT_RECOVERED;
8652 else
8653 result = PCI_ERS_RESULT_DISCONNECT;
8654 }
8655
8656 err = pci_cleanup_aer_uncorrect_error_status(pdev);
8657 if (err) {
8658 dev_info(&pdev->dev,
8659 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
8660 err);
8661 /* non-fatal, continue */
8662 }
8663
8664 return result;
8665}
8666
8667/**
8668 * i40e_pci_error_resume - restart operations after PCI error recovery
8669 * @pdev: PCI device information struct
8670 *
8671 * Called to allow the driver to bring things back up after PCI error
8672 * and/or reset recovery has finished.
8673 **/
8674static void i40e_pci_error_resume(struct pci_dev *pdev)
8675{
8676 struct i40e_pf *pf = pci_get_drvdata(pdev);
8677
8678 dev_info(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008679 if (test_bit(__I40E_SUSPENDED, &pf->state))
8680 return;
8681
8682 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008683 i40e_handle_reset_warning(pf);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008684 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008685}
8686
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008687/**
8688 * i40e_shutdown - PCI callback for shutting down
8689 * @pdev: PCI device information struct
8690 **/
8691static void i40e_shutdown(struct pci_dev *pdev)
8692{
8693 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008694 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008695
8696 set_bit(__I40E_SUSPENDED, &pf->state);
8697 set_bit(__I40E_DOWN, &pf->state);
8698 rtnl_lock();
8699 i40e_prep_for_reset(pf);
8700 rtnl_unlock();
8701
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008702 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
8703 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
8704
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008705 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008706 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008707 pci_set_power_state(pdev, PCI_D3hot);
8708 }
8709}
8710
8711#ifdef CONFIG_PM
8712/**
8713 * i40e_suspend - PCI callback for moving to D3
8714 * @pdev: PCI device information struct
8715 **/
8716static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
8717{
8718 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008719 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008720
8721 set_bit(__I40E_SUSPENDED, &pf->state);
8722 set_bit(__I40E_DOWN, &pf->state);
8723 rtnl_lock();
8724 i40e_prep_for_reset(pf);
8725 rtnl_unlock();
8726
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008727 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
8728 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
8729
8730 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008731 pci_set_power_state(pdev, PCI_D3hot);
8732
8733 return 0;
8734}
8735
8736/**
8737 * i40e_resume - PCI callback for waking up from D3
8738 * @pdev: PCI device information struct
8739 **/
8740static int i40e_resume(struct pci_dev *pdev)
8741{
8742 struct i40e_pf *pf = pci_get_drvdata(pdev);
8743 u32 err;
8744
8745 pci_set_power_state(pdev, PCI_D0);
8746 pci_restore_state(pdev);
8747 /* pci_restore_state() clears dev->state_saves, so
8748 * call pci_save_state() again to restore it.
8749 */
8750 pci_save_state(pdev);
8751
8752 err = pci_enable_device_mem(pdev);
8753 if (err) {
8754 dev_err(&pdev->dev,
8755 "%s: Cannot enable PCI device from suspend\n",
8756 __func__);
8757 return err;
8758 }
8759 pci_set_master(pdev);
8760
8761 /* no wakeup events while running */
8762 pci_wake_from_d3(pdev, false);
8763
8764 /* handling the reset will rebuild the device state */
8765 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
8766 clear_bit(__I40E_DOWN, &pf->state);
8767 rtnl_lock();
8768 i40e_reset_and_rebuild(pf, false);
8769 rtnl_unlock();
8770 }
8771
8772 return 0;
8773}
8774
8775#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008776static const struct pci_error_handlers i40e_err_handler = {
8777 .error_detected = i40e_pci_error_detected,
8778 .slot_reset = i40e_pci_error_slot_reset,
8779 .resume = i40e_pci_error_resume,
8780};
8781
8782static struct pci_driver i40e_driver = {
8783 .name = i40e_driver_name,
8784 .id_table = i40e_pci_tbl,
8785 .probe = i40e_probe,
8786 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008787#ifdef CONFIG_PM
8788 .suspend = i40e_suspend,
8789 .resume = i40e_resume,
8790#endif
8791 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008792 .err_handler = &i40e_err_handler,
8793 .sriov_configure = i40e_pci_sriov_configure,
8794};
8795
8796/**
8797 * i40e_init_module - Driver registration routine
8798 *
8799 * i40e_init_module is the first routine called when the driver is
8800 * loaded. All it does is register with the PCI subsystem.
8801 **/
8802static int __init i40e_init_module(void)
8803{
8804 pr_info("%s: %s - version %s\n", i40e_driver_name,
8805 i40e_driver_string, i40e_driver_version_str);
8806 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
8807 i40e_dbg_init();
8808 return pci_register_driver(&i40e_driver);
8809}
8810module_init(i40e_init_module);
8811
8812/**
8813 * i40e_exit_module - Driver exit cleanup routine
8814 *
8815 * i40e_exit_module is called just before the driver is removed
8816 * from memory.
8817 **/
8818static void __exit i40e_exit_module(void)
8819{
8820 pci_unregister_driver(&i40e_driver);
8821 i40e_dbg_exit();
8822}
8823module_exit(i40e_exit_module);