blob: 35e5ba4a7856e188a4577852068fc7e29baba1b1 [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);
Shannon Nelson41a9e552014-04-23 04:50:20 +0000533 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
534 vsi->stat_offsets_loaded,
535 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
536 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
537 vsi->stat_offsets_loaded,
538 &oes->tx_errors, &es->tx_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000539
540 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
541 I40E_GLV_GORCL(stat_idx),
542 vsi->stat_offsets_loaded,
543 &oes->rx_bytes, &es->rx_bytes);
544 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
545 I40E_GLV_UPRCL(stat_idx),
546 vsi->stat_offsets_loaded,
547 &oes->rx_unicast, &es->rx_unicast);
548 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
549 I40E_GLV_MPRCL(stat_idx),
550 vsi->stat_offsets_loaded,
551 &oes->rx_multicast, &es->rx_multicast);
552 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
553 I40E_GLV_BPRCL(stat_idx),
554 vsi->stat_offsets_loaded,
555 &oes->rx_broadcast, &es->rx_broadcast);
556
557 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
558 I40E_GLV_GOTCL(stat_idx),
559 vsi->stat_offsets_loaded,
560 &oes->tx_bytes, &es->tx_bytes);
561 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
562 I40E_GLV_UPTCL(stat_idx),
563 vsi->stat_offsets_loaded,
564 &oes->tx_unicast, &es->tx_unicast);
565 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
566 I40E_GLV_MPTCL(stat_idx),
567 vsi->stat_offsets_loaded,
568 &oes->tx_multicast, &es->tx_multicast);
569 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
570 I40E_GLV_BPTCL(stat_idx),
571 vsi->stat_offsets_loaded,
572 &oes->tx_broadcast, &es->tx_broadcast);
573 vsi->stat_offsets_loaded = true;
574}
575
576/**
577 * i40e_update_veb_stats - Update Switch component statistics
578 * @veb: the VEB being updated
579 **/
580static void i40e_update_veb_stats(struct i40e_veb *veb)
581{
582 struct i40e_pf *pf = veb->pf;
583 struct i40e_hw *hw = &pf->hw;
584 struct i40e_eth_stats *oes;
585 struct i40e_eth_stats *es; /* device's eth stats */
586 int idx = 0;
587
588 idx = veb->stats_idx;
589 es = &veb->stats;
590 oes = &veb->stats_offsets;
591
592 /* Gather up the stats that the hw collects */
593 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
594 veb->stat_offsets_loaded,
595 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000596 if (hw->revision_id > 0)
597 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
598 veb->stat_offsets_loaded,
599 &oes->rx_unknown_protocol,
600 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000601 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
602 veb->stat_offsets_loaded,
603 &oes->rx_bytes, &es->rx_bytes);
604 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
605 veb->stat_offsets_loaded,
606 &oes->rx_unicast, &es->rx_unicast);
607 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
608 veb->stat_offsets_loaded,
609 &oes->rx_multicast, &es->rx_multicast);
610 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
611 veb->stat_offsets_loaded,
612 &oes->rx_broadcast, &es->rx_broadcast);
613
614 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
615 veb->stat_offsets_loaded,
616 &oes->tx_bytes, &es->tx_bytes);
617 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
618 veb->stat_offsets_loaded,
619 &oes->tx_unicast, &es->tx_unicast);
620 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
621 veb->stat_offsets_loaded,
622 &oes->tx_multicast, &es->tx_multicast);
623 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
624 veb->stat_offsets_loaded,
625 &oes->tx_broadcast, &es->tx_broadcast);
626 veb->stat_offsets_loaded = true;
627}
628
629/**
630 * i40e_update_link_xoff_rx - Update XOFF received in link flow control mode
631 * @pf: the corresponding PF
632 *
633 * Update the Rx XOFF counter (PAUSE frames) in link flow control mode
634 **/
635static void i40e_update_link_xoff_rx(struct i40e_pf *pf)
636{
637 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
638 struct i40e_hw_port_stats *nsd = &pf->stats;
639 struct i40e_hw *hw = &pf->hw;
640 u64 xoff = 0;
641 u16 i, v;
642
643 if ((hw->fc.current_mode != I40E_FC_FULL) &&
644 (hw->fc.current_mode != I40E_FC_RX_PAUSE))
645 return;
646
647 xoff = nsd->link_xoff_rx;
648 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
649 pf->stat_offsets_loaded,
650 &osd->link_xoff_rx, &nsd->link_xoff_rx);
651
652 /* No new LFC xoff rx */
653 if (!(nsd->link_xoff_rx - xoff))
654 return;
655
656 /* Clear the __I40E_HANG_CHECK_ARMED bit for all Tx rings */
657 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
658 struct i40e_vsi *vsi = pf->vsi[v];
659
660 if (!vsi)
661 continue;
662
663 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000664 struct i40e_ring *ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000665 clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
666 }
667 }
668}
669
670/**
671 * i40e_update_prio_xoff_rx - Update XOFF received in PFC mode
672 * @pf: the corresponding PF
673 *
674 * Update the Rx XOFF counter (PAUSE frames) in PFC mode
675 **/
676static void i40e_update_prio_xoff_rx(struct i40e_pf *pf)
677{
678 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
679 struct i40e_hw_port_stats *nsd = &pf->stats;
680 bool xoff[I40E_MAX_TRAFFIC_CLASS] = {false};
681 struct i40e_dcbx_config *dcb_cfg;
682 struct i40e_hw *hw = &pf->hw;
683 u16 i, v;
684 u8 tc;
685
686 dcb_cfg = &hw->local_dcbx_config;
687
688 /* See if DCB enabled with PFC TC */
689 if (!(pf->flags & I40E_FLAG_DCB_ENABLED) ||
690 !(dcb_cfg->pfc.pfcenable)) {
691 i40e_update_link_xoff_rx(pf);
692 return;
693 }
694
695 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
696 u64 prio_xoff = nsd->priority_xoff_rx[i];
697 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
698 pf->stat_offsets_loaded,
699 &osd->priority_xoff_rx[i],
700 &nsd->priority_xoff_rx[i]);
701
702 /* No new PFC xoff rx */
703 if (!(nsd->priority_xoff_rx[i] - prio_xoff))
704 continue;
705 /* Get the TC for given priority */
706 tc = dcb_cfg->etscfg.prioritytable[i];
707 xoff[tc] = true;
708 }
709
710 /* Clear the __I40E_HANG_CHECK_ARMED bit for Tx rings */
711 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
712 struct i40e_vsi *vsi = pf->vsi[v];
713
714 if (!vsi)
715 continue;
716
717 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000718 struct i40e_ring *ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000719
720 tc = ring->dcb_tc;
721 if (xoff[tc])
722 clear_bit(__I40E_HANG_CHECK_ARMED,
723 &ring->state);
724 }
725 }
726}
727
728/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000729 * i40e_update_vsi_stats - Update the vsi statistics counters.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000730 * @vsi: the VSI to be updated
731 *
732 * There are a few instances where we store the same stat in a
733 * couple of different structs. This is partly because we have
734 * the netdev stats that need to be filled out, which is slightly
735 * different from the "eth_stats" defined by the chip and used in
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000736 * VF communications. We sort it out here.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000737 **/
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000738static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000739{
740 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000741 struct rtnl_link_stats64 *ons;
742 struct rtnl_link_stats64 *ns; /* netdev stats */
743 struct i40e_eth_stats *oes;
744 struct i40e_eth_stats *es; /* device's eth stats */
745 u32 tx_restart, tx_busy;
746 u32 rx_page, rx_buf;
747 u64 rx_p, rx_b;
748 u64 tx_p, tx_b;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000749 u16 q;
750
751 if (test_bit(__I40E_DOWN, &vsi->state) ||
752 test_bit(__I40E_CONFIG_BUSY, &pf->state))
753 return;
754
755 ns = i40e_get_vsi_stats_struct(vsi);
756 ons = &vsi->net_stats_offsets;
757 es = &vsi->eth_stats;
758 oes = &vsi->eth_stats_offsets;
759
760 /* Gather up the netdev and vsi stats that the driver collects
761 * on the fly during packet processing
762 */
763 rx_b = rx_p = 0;
764 tx_b = tx_p = 0;
765 tx_restart = tx_busy = 0;
766 rx_page = 0;
767 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000768 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000769 for (q = 0; q < vsi->num_queue_pairs; q++) {
770 struct i40e_ring *p;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000771 u64 bytes, packets;
772 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000773
Alexander Duyck980e9b12013-09-28 06:01:03 +0000774 /* locate Tx ring */
775 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000776
Alexander Duyck980e9b12013-09-28 06:01:03 +0000777 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700778 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000779 packets = p->stats.packets;
780 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700781 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000782 tx_b += bytes;
783 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000784 tx_restart += p->tx_stats.restart_queue;
785 tx_busy += p->tx_stats.tx_busy;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000786
787 /* Rx queue is part of the same block as Tx queue */
788 p = &p[1];
789 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700790 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000791 packets = p->stats.packets;
792 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700793 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000794 rx_b += bytes;
795 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000796 rx_buf += p->rx_stats.alloc_buff_failed;
797 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000798 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000799 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000800 vsi->tx_restart = tx_restart;
801 vsi->tx_busy = tx_busy;
802 vsi->rx_page_failed = rx_page;
803 vsi->rx_buf_failed = rx_buf;
804
805 ns->rx_packets = rx_p;
806 ns->rx_bytes = rx_b;
807 ns->tx_packets = tx_p;
808 ns->tx_bytes = tx_b;
809
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000810 /* update netdev stats from eth stats */
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000811 i40e_update_eth_stats(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000812 ons->tx_errors = oes->tx_errors;
813 ns->tx_errors = es->tx_errors;
814 ons->multicast = oes->rx_multicast;
815 ns->multicast = es->rx_multicast;
Shannon Nelson41a9e552014-04-23 04:50:20 +0000816 ons->rx_dropped = oes->rx_discards;
817 ns->rx_dropped = es->rx_discards;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000818 ons->tx_dropped = oes->tx_discards;
819 ns->tx_dropped = es->tx_discards;
820
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000821 /* pull in a couple PF stats if this is the main vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000822 if (vsi == pf->vsi[pf->lan_vsi]) {
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000823 ns->rx_crc_errors = pf->stats.crc_errors;
824 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
825 ns->rx_length_errors = pf->stats.rx_length_errors;
826 }
827}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000828
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000829/**
830 * i40e_update_pf_stats - Update the pf statistics counters.
831 * @pf: the PF to be updated
832 **/
833static void i40e_update_pf_stats(struct i40e_pf *pf)
834{
835 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
836 struct i40e_hw_port_stats *nsd = &pf->stats;
837 struct i40e_hw *hw = &pf->hw;
838 u32 val;
839 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000840
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000841 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
842 I40E_GLPRT_GORCL(hw->port),
843 pf->stat_offsets_loaded,
844 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
845 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
846 I40E_GLPRT_GOTCL(hw->port),
847 pf->stat_offsets_loaded,
848 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
849 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
850 pf->stat_offsets_loaded,
851 &osd->eth.rx_discards,
852 &nsd->eth.rx_discards);
853 i40e_stat_update32(hw, I40E_GLPRT_TDPC(hw->port),
854 pf->stat_offsets_loaded,
855 &osd->eth.tx_discards,
856 &nsd->eth.tx_discards);
857 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
858 I40E_GLPRT_MPRCL(hw->port),
859 pf->stat_offsets_loaded,
860 &osd->eth.rx_multicast,
861 &nsd->eth.rx_multicast);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000862
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000863 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
864 pf->stat_offsets_loaded,
865 &osd->tx_dropped_link_down,
866 &nsd->tx_dropped_link_down);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000867
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000868 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
869 pf->stat_offsets_loaded,
870 &osd->crc_errors, &nsd->crc_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000871
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000872 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
873 pf->stat_offsets_loaded,
874 &osd->illegal_bytes, &nsd->illegal_bytes);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000875
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000876 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
877 pf->stat_offsets_loaded,
878 &osd->mac_local_faults,
879 &nsd->mac_local_faults);
880 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
881 pf->stat_offsets_loaded,
882 &osd->mac_remote_faults,
883 &nsd->mac_remote_faults);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000884
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000885 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
886 pf->stat_offsets_loaded,
887 &osd->rx_length_errors,
888 &nsd->rx_length_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000889
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000890 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
891 pf->stat_offsets_loaded,
892 &osd->link_xon_rx, &nsd->link_xon_rx);
893 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
894 pf->stat_offsets_loaded,
895 &osd->link_xon_tx, &nsd->link_xon_tx);
896 i40e_update_prio_xoff_rx(pf); /* handles I40E_GLPRT_LXOFFRXC */
897 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
898 pf->stat_offsets_loaded,
899 &osd->link_xoff_tx, &nsd->link_xoff_tx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000900
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000901 for (i = 0; i < 8; i++) {
902 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000903 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000904 &osd->priority_xon_rx[i],
905 &nsd->priority_xon_rx[i]);
906 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000907 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000908 &osd->priority_xon_tx[i],
909 &nsd->priority_xon_tx[i]);
910 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000911 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000912 &osd->priority_xoff_tx[i],
913 &nsd->priority_xoff_tx[i]);
914 i40e_stat_update32(hw,
915 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000916 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000917 &osd->priority_xon_2_xoff[i],
918 &nsd->priority_xon_2_xoff[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000919 }
920
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000921 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
922 I40E_GLPRT_PRC64L(hw->port),
923 pf->stat_offsets_loaded,
924 &osd->rx_size_64, &nsd->rx_size_64);
925 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
926 I40E_GLPRT_PRC127L(hw->port),
927 pf->stat_offsets_loaded,
928 &osd->rx_size_127, &nsd->rx_size_127);
929 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
930 I40E_GLPRT_PRC255L(hw->port),
931 pf->stat_offsets_loaded,
932 &osd->rx_size_255, &nsd->rx_size_255);
933 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
934 I40E_GLPRT_PRC511L(hw->port),
935 pf->stat_offsets_loaded,
936 &osd->rx_size_511, &nsd->rx_size_511);
937 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
938 I40E_GLPRT_PRC1023L(hw->port),
939 pf->stat_offsets_loaded,
940 &osd->rx_size_1023, &nsd->rx_size_1023);
941 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
942 I40E_GLPRT_PRC1522L(hw->port),
943 pf->stat_offsets_loaded,
944 &osd->rx_size_1522, &nsd->rx_size_1522);
945 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
946 I40E_GLPRT_PRC9522L(hw->port),
947 pf->stat_offsets_loaded,
948 &osd->rx_size_big, &nsd->rx_size_big);
949
950 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
951 I40E_GLPRT_PTC64L(hw->port),
952 pf->stat_offsets_loaded,
953 &osd->tx_size_64, &nsd->tx_size_64);
954 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
955 I40E_GLPRT_PTC127L(hw->port),
956 pf->stat_offsets_loaded,
957 &osd->tx_size_127, &nsd->tx_size_127);
958 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
959 I40E_GLPRT_PTC255L(hw->port),
960 pf->stat_offsets_loaded,
961 &osd->tx_size_255, &nsd->tx_size_255);
962 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
963 I40E_GLPRT_PTC511L(hw->port),
964 pf->stat_offsets_loaded,
965 &osd->tx_size_511, &nsd->tx_size_511);
966 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
967 I40E_GLPRT_PTC1023L(hw->port),
968 pf->stat_offsets_loaded,
969 &osd->tx_size_1023, &nsd->tx_size_1023);
970 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
971 I40E_GLPRT_PTC1522L(hw->port),
972 pf->stat_offsets_loaded,
973 &osd->tx_size_1522, &nsd->tx_size_1522);
974 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
975 I40E_GLPRT_PTC9522L(hw->port),
976 pf->stat_offsets_loaded,
977 &osd->tx_size_big, &nsd->tx_size_big);
978
979 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
980 pf->stat_offsets_loaded,
981 &osd->rx_undersize, &nsd->rx_undersize);
982 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
983 pf->stat_offsets_loaded,
984 &osd->rx_fragments, &nsd->rx_fragments);
985 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
986 pf->stat_offsets_loaded,
987 &osd->rx_oversize, &nsd->rx_oversize);
988 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
989 pf->stat_offsets_loaded,
990 &osd->rx_jabber, &nsd->rx_jabber);
991
992 val = rd32(hw, I40E_PRTPM_EEE_STAT);
993 nsd->tx_lpi_status =
994 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
995 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
996 nsd->rx_lpi_status =
997 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
998 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
999 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1000 pf->stat_offsets_loaded,
1001 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1002 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1003 pf->stat_offsets_loaded,
1004 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1005
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001006 pf->stat_offsets_loaded = true;
1007}
1008
1009/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001010 * i40e_update_stats - Update the various statistics counters.
1011 * @vsi: the VSI to be updated
1012 *
1013 * Update the various stats for this VSI and its related entities.
1014 **/
1015void i40e_update_stats(struct i40e_vsi *vsi)
1016{
1017 struct i40e_pf *pf = vsi->back;
1018
1019 if (vsi == pf->vsi[pf->lan_vsi])
1020 i40e_update_pf_stats(pf);
1021
1022 i40e_update_vsi_stats(vsi);
1023}
1024
1025/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001026 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1027 * @vsi: the VSI to be searched
1028 * @macaddr: the MAC address
1029 * @vlan: the vlan
1030 * @is_vf: make sure its a vf filter, else doesn't matter
1031 * @is_netdev: make sure its a netdev filter, else doesn't matter
1032 *
1033 * Returns ptr to the filter object or NULL
1034 **/
1035static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1036 u8 *macaddr, s16 vlan,
1037 bool is_vf, bool is_netdev)
1038{
1039 struct i40e_mac_filter *f;
1040
1041 if (!vsi || !macaddr)
1042 return NULL;
1043
1044 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1045 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1046 (vlan == f->vlan) &&
1047 (!is_vf || f->is_vf) &&
1048 (!is_netdev || f->is_netdev))
1049 return f;
1050 }
1051 return NULL;
1052}
1053
1054/**
1055 * i40e_find_mac - Find a mac addr in the macvlan filters list
1056 * @vsi: the VSI to be searched
1057 * @macaddr: the MAC address we are searching for
1058 * @is_vf: make sure its a vf filter, else doesn't matter
1059 * @is_netdev: make sure its a netdev filter, else doesn't matter
1060 *
1061 * Returns the first filter with the provided MAC address or NULL if
1062 * MAC address was not found
1063 **/
1064struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, u8 *macaddr,
1065 bool is_vf, bool is_netdev)
1066{
1067 struct i40e_mac_filter *f;
1068
1069 if (!vsi || !macaddr)
1070 return NULL;
1071
1072 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1073 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1074 (!is_vf || f->is_vf) &&
1075 (!is_netdev || f->is_netdev))
1076 return f;
1077 }
1078 return NULL;
1079}
1080
1081/**
1082 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1083 * @vsi: the VSI to be searched
1084 *
1085 * Returns true if VSI is in vlan mode or false otherwise
1086 **/
1087bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1088{
1089 struct i40e_mac_filter *f;
1090
1091 /* Only -1 for all the filters denotes not in vlan mode
1092 * so we have to go through all the list in order to make sure
1093 */
1094 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1095 if (f->vlan >= 0)
1096 return true;
1097 }
1098
1099 return false;
1100}
1101
1102/**
1103 * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
1104 * @vsi: the VSI to be searched
1105 * @macaddr: the mac address to be filtered
1106 * @is_vf: true if it is a vf
1107 * @is_netdev: true if it is a netdev
1108 *
1109 * Goes through all the macvlan filters and adds a
1110 * macvlan filter for each unique vlan that already exists
1111 *
1112 * Returns first filter found on success, else NULL
1113 **/
1114struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi, u8 *macaddr,
1115 bool is_vf, bool is_netdev)
1116{
1117 struct i40e_mac_filter *f;
1118
1119 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1120 if (!i40e_find_filter(vsi, macaddr, f->vlan,
1121 is_vf, is_netdev)) {
1122 if (!i40e_add_filter(vsi, macaddr, f->vlan,
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -08001123 is_vf, is_netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001124 return NULL;
1125 }
1126 }
1127
1128 return list_first_entry_or_null(&vsi->mac_filter_list,
1129 struct i40e_mac_filter, list);
1130}
1131
1132/**
1133 * i40e_add_filter - Add a mac/vlan filter to the VSI
1134 * @vsi: the VSI to be searched
1135 * @macaddr: the MAC address
1136 * @vlan: the vlan
1137 * @is_vf: make sure its a vf filter, else doesn't matter
1138 * @is_netdev: make sure its a netdev filter, else doesn't matter
1139 *
1140 * Returns ptr to the filter object or NULL when no memory available.
1141 **/
1142struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1143 u8 *macaddr, s16 vlan,
1144 bool is_vf, bool is_netdev)
1145{
1146 struct i40e_mac_filter *f;
1147
1148 if (!vsi || !macaddr)
1149 return NULL;
1150
1151 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1152 if (!f) {
1153 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1154 if (!f)
1155 goto add_filter_out;
1156
1157 memcpy(f->macaddr, macaddr, ETH_ALEN);
1158 f->vlan = vlan;
1159 f->changed = true;
1160
1161 INIT_LIST_HEAD(&f->list);
1162 list_add(&f->list, &vsi->mac_filter_list);
1163 }
1164
1165 /* increment counter and add a new flag if needed */
1166 if (is_vf) {
1167 if (!f->is_vf) {
1168 f->is_vf = true;
1169 f->counter++;
1170 }
1171 } else if (is_netdev) {
1172 if (!f->is_netdev) {
1173 f->is_netdev = true;
1174 f->counter++;
1175 }
1176 } else {
1177 f->counter++;
1178 }
1179
1180 /* changed tells sync_filters_subtask to
1181 * push the filter down to the firmware
1182 */
1183 if (f->changed) {
1184 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1185 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1186 }
1187
1188add_filter_out:
1189 return f;
1190}
1191
1192/**
1193 * i40e_del_filter - Remove a mac/vlan filter from the VSI
1194 * @vsi: the VSI to be searched
1195 * @macaddr: the MAC address
1196 * @vlan: the vlan
1197 * @is_vf: make sure it's a vf filter, else doesn't matter
1198 * @is_netdev: make sure it's a netdev filter, else doesn't matter
1199 **/
1200void i40e_del_filter(struct i40e_vsi *vsi,
1201 u8 *macaddr, s16 vlan,
1202 bool is_vf, bool is_netdev)
1203{
1204 struct i40e_mac_filter *f;
1205
1206 if (!vsi || !macaddr)
1207 return;
1208
1209 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1210 if (!f || f->counter == 0)
1211 return;
1212
1213 if (is_vf) {
1214 if (f->is_vf) {
1215 f->is_vf = false;
1216 f->counter--;
1217 }
1218 } else if (is_netdev) {
1219 if (f->is_netdev) {
1220 f->is_netdev = false;
1221 f->counter--;
1222 }
1223 } else {
1224 /* make sure we don't remove a filter in use by vf or netdev */
1225 int min_f = 0;
1226 min_f += (f->is_vf ? 1 : 0);
1227 min_f += (f->is_netdev ? 1 : 0);
1228
1229 if (f->counter > min_f)
1230 f->counter--;
1231 }
1232
1233 /* counter == 0 tells sync_filters_subtask to
1234 * remove the filter from the firmware's list
1235 */
1236 if (f->counter == 0) {
1237 f->changed = true;
1238 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1239 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1240 }
1241}
1242
1243/**
1244 * i40e_set_mac - NDO callback to set mac address
1245 * @netdev: network interface device structure
1246 * @p: pointer to an address structure
1247 *
1248 * Returns 0 on success, negative on failure
1249 **/
1250static int i40e_set_mac(struct net_device *netdev, void *p)
1251{
1252 struct i40e_netdev_priv *np = netdev_priv(netdev);
1253 struct i40e_vsi *vsi = np->vsi;
1254 struct sockaddr *addr = p;
1255 struct i40e_mac_filter *f;
1256
1257 if (!is_valid_ether_addr(addr->sa_data))
1258 return -EADDRNOTAVAIL;
1259
1260 netdev_info(netdev, "set mac address=%pM\n", addr->sa_data);
1261
1262 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
1263 return 0;
1264
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001265 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1266 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1267 return -EADDRNOTAVAIL;
1268
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001269 if (vsi->type == I40E_VSI_MAIN) {
1270 i40e_status ret;
1271 ret = i40e_aq_mac_address_write(&vsi->back->hw,
1272 I40E_AQC_WRITE_TYPE_LAA_ONLY,
1273 addr->sa_data, NULL);
1274 if (ret) {
1275 netdev_info(netdev,
1276 "Addr change for Main VSI failed: %d\n",
1277 ret);
1278 return -EADDRNOTAVAIL;
1279 }
1280
1281 memcpy(vsi->back->hw.mac.addr, addr->sa_data, netdev->addr_len);
1282 }
1283
1284 /* In order to be sure to not drop any packets, add the new address
1285 * then delete the old one.
1286 */
1287 f = i40e_add_filter(vsi, addr->sa_data, I40E_VLAN_ANY, false, false);
1288 if (!f)
1289 return -ENOMEM;
1290
1291 i40e_sync_vsi_filters(vsi);
1292 i40e_del_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY, false, false);
1293 i40e_sync_vsi_filters(vsi);
1294
1295 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1296
1297 return 0;
1298}
1299
1300/**
1301 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1302 * @vsi: the VSI being setup
1303 * @ctxt: VSI context structure
1304 * @enabled_tc: Enabled TCs bitmap
1305 * @is_add: True if called before Add VSI
1306 *
1307 * Setup VSI queue mapping for enabled traffic classes.
1308 **/
1309static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1310 struct i40e_vsi_context *ctxt,
1311 u8 enabled_tc,
1312 bool is_add)
1313{
1314 struct i40e_pf *pf = vsi->back;
1315 u16 sections = 0;
1316 u8 netdev_tc = 0;
1317 u16 numtc = 0;
1318 u16 qcount;
1319 u8 offset;
1320 u16 qmap;
1321 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001322 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001323
1324 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1325 offset = 0;
1326
1327 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1328 /* Find numtc from enabled TC bitmap */
1329 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1330 if (enabled_tc & (1 << i)) /* TC is enabled */
1331 numtc++;
1332 }
1333 if (!numtc) {
1334 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1335 numtc = 1;
1336 }
1337 } else {
1338 /* At least TC0 is enabled in case of non-DCB case */
1339 numtc = 1;
1340 }
1341
1342 vsi->tc_config.numtc = numtc;
1343 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001344 /* Number of queues per enabled TC */
1345 num_tc_qps = rounddown_pow_of_two(vsi->alloc_queue_pairs/numtc);
1346 num_tc_qps = min_t(int, num_tc_qps, I40E_MAX_QUEUES_PER_TC);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001347
1348 /* Setup queue offset/count for all TCs for given VSI */
1349 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1350 /* See if the given TC is enabled for the given VSI */
1351 if (vsi->tc_config.enabled_tc & (1 << i)) { /* TC is enabled */
1352 int pow, num_qps;
1353
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001354 switch (vsi->type) {
1355 case I40E_VSI_MAIN:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001356 qcount = min_t(int, pf->rss_size, num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001357 break;
1358 case I40E_VSI_FDIR:
1359 case I40E_VSI_SRIOV:
1360 case I40E_VSI_VMDQ2:
1361 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001362 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001363 WARN_ON(i != 0);
1364 break;
1365 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001366 vsi->tc_config.tc_info[i].qoffset = offset;
1367 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001368
1369 /* find the power-of-2 of the number of queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001370 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001371 pow = 0;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001372 while (num_qps && ((1 << pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001373 pow++;
1374 num_qps >>= 1;
1375 }
1376
1377 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1378 qmap =
1379 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1380 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1381
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001382 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001383 } else {
1384 /* TC is not enabled so set the offset to
1385 * default queue and allocate one queue
1386 * for the given TC.
1387 */
1388 vsi->tc_config.tc_info[i].qoffset = 0;
1389 vsi->tc_config.tc_info[i].qcount = 1;
1390 vsi->tc_config.tc_info[i].netdev_tc = 0;
1391
1392 qmap = 0;
1393 }
1394 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1395 }
1396
1397 /* Set actual Tx/Rx queue pairs */
1398 vsi->num_queue_pairs = offset;
1399
1400 /* Scheduler section valid can only be set for ADD VSI */
1401 if (is_add) {
1402 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1403
1404 ctxt->info.up_enable_bits = enabled_tc;
1405 }
1406 if (vsi->type == I40E_VSI_SRIOV) {
1407 ctxt->info.mapping_flags |=
1408 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1409 for (i = 0; i < vsi->num_queue_pairs; i++)
1410 ctxt->info.queue_mapping[i] =
1411 cpu_to_le16(vsi->base_queue + i);
1412 } else {
1413 ctxt->info.mapping_flags |=
1414 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1415 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1416 }
1417 ctxt->info.valid_sections |= cpu_to_le16(sections);
1418}
1419
1420/**
1421 * i40e_set_rx_mode - NDO callback to set the netdev filters
1422 * @netdev: network interface device structure
1423 **/
1424static void i40e_set_rx_mode(struct net_device *netdev)
1425{
1426 struct i40e_netdev_priv *np = netdev_priv(netdev);
1427 struct i40e_mac_filter *f, *ftmp;
1428 struct i40e_vsi *vsi = np->vsi;
1429 struct netdev_hw_addr *uca;
1430 struct netdev_hw_addr *mca;
1431 struct netdev_hw_addr *ha;
1432
1433 /* add addr if not already in the filter list */
1434 netdev_for_each_uc_addr(uca, netdev) {
1435 if (!i40e_find_mac(vsi, uca->addr, false, true)) {
1436 if (i40e_is_vsi_in_vlan(vsi))
1437 i40e_put_mac_in_vlan(vsi, uca->addr,
1438 false, true);
1439 else
1440 i40e_add_filter(vsi, uca->addr, I40E_VLAN_ANY,
1441 false, true);
1442 }
1443 }
1444
1445 netdev_for_each_mc_addr(mca, netdev) {
1446 if (!i40e_find_mac(vsi, mca->addr, false, true)) {
1447 if (i40e_is_vsi_in_vlan(vsi))
1448 i40e_put_mac_in_vlan(vsi, mca->addr,
1449 false, true);
1450 else
1451 i40e_add_filter(vsi, mca->addr, I40E_VLAN_ANY,
1452 false, true);
1453 }
1454 }
1455
1456 /* remove filter if not in netdev list */
1457 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1458 bool found = false;
1459
1460 if (!f->is_netdev)
1461 continue;
1462
1463 if (is_multicast_ether_addr(f->macaddr)) {
1464 netdev_for_each_mc_addr(mca, netdev) {
1465 if (ether_addr_equal(mca->addr, f->macaddr)) {
1466 found = true;
1467 break;
1468 }
1469 }
1470 } else {
1471 netdev_for_each_uc_addr(uca, netdev) {
1472 if (ether_addr_equal(uca->addr, f->macaddr)) {
1473 found = true;
1474 break;
1475 }
1476 }
1477
1478 for_each_dev_addr(netdev, ha) {
1479 if (ether_addr_equal(ha->addr, f->macaddr)) {
1480 found = true;
1481 break;
1482 }
1483 }
1484 }
1485 if (!found)
1486 i40e_del_filter(
1487 vsi, f->macaddr, I40E_VLAN_ANY, false, true);
1488 }
1489
1490 /* check for other flag changes */
1491 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1492 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1493 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1494 }
1495}
1496
1497/**
1498 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1499 * @vsi: ptr to the VSI
1500 *
1501 * Push any outstanding VSI filter changes through the AdminQ.
1502 *
1503 * Returns 0 or error value
1504 **/
1505int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
1506{
1507 struct i40e_mac_filter *f, *ftmp;
1508 bool promisc_forced_on = false;
1509 bool add_happened = false;
1510 int filter_list_len = 0;
1511 u32 changed_flags = 0;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001512 i40e_status aq_ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001513 struct i40e_pf *pf;
1514 int num_add = 0;
1515 int num_del = 0;
1516 u16 cmd_flags;
1517
1518 /* empty array typed pointers, kcalloc later */
1519 struct i40e_aqc_add_macvlan_element_data *add_list;
1520 struct i40e_aqc_remove_macvlan_element_data *del_list;
1521
1522 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1523 usleep_range(1000, 2000);
1524 pf = vsi->back;
1525
1526 if (vsi->netdev) {
1527 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1528 vsi->current_netdev_flags = vsi->netdev->flags;
1529 }
1530
1531 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1532 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1533
1534 filter_list_len = pf->hw.aq.asq_buf_size /
1535 sizeof(struct i40e_aqc_remove_macvlan_element_data);
1536 del_list = kcalloc(filter_list_len,
1537 sizeof(struct i40e_aqc_remove_macvlan_element_data),
1538 GFP_KERNEL);
1539 if (!del_list)
1540 return -ENOMEM;
1541
1542 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1543 if (!f->changed)
1544 continue;
1545
1546 if (f->counter != 0)
1547 continue;
1548 f->changed = false;
1549 cmd_flags = 0;
1550
1551 /* add to delete list */
1552 memcpy(del_list[num_del].mac_addr,
1553 f->macaddr, ETH_ALEN);
1554 del_list[num_del].vlan_tag =
1555 cpu_to_le16((u16)(f->vlan ==
1556 I40E_VLAN_ANY ? 0 : f->vlan));
1557
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001558 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1559 del_list[num_del].flags = cmd_flags;
1560 num_del++;
1561
1562 /* unlink from filter list */
1563 list_del(&f->list);
1564 kfree(f);
1565
1566 /* flush a full buffer */
1567 if (num_del == filter_list_len) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001568 aq_ret = i40e_aq_remove_macvlan(&pf->hw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001569 vsi->seid, del_list, num_del,
1570 NULL);
1571 num_del = 0;
1572 memset(del_list, 0, sizeof(*del_list));
1573
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001574 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001575 dev_info(&pf->pdev->dev,
1576 "ignoring delete macvlan error, err %d, aq_err %d while flushing a full buffer\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001577 aq_ret,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001578 pf->hw.aq.asq_last_status);
1579 }
1580 }
1581 if (num_del) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001582 aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001583 del_list, num_del, NULL);
1584 num_del = 0;
1585
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001586 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001587 dev_info(&pf->pdev->dev,
1588 "ignoring delete macvlan error, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001589 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001590 }
1591
1592 kfree(del_list);
1593 del_list = NULL;
1594
1595 /* do all the adds now */
1596 filter_list_len = pf->hw.aq.asq_buf_size /
1597 sizeof(struct i40e_aqc_add_macvlan_element_data),
1598 add_list = kcalloc(filter_list_len,
1599 sizeof(struct i40e_aqc_add_macvlan_element_data),
1600 GFP_KERNEL);
1601 if (!add_list)
1602 return -ENOMEM;
1603
1604 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1605 if (!f->changed)
1606 continue;
1607
1608 if (f->counter == 0)
1609 continue;
1610 f->changed = false;
1611 add_happened = true;
1612 cmd_flags = 0;
1613
1614 /* add to add array */
1615 memcpy(add_list[num_add].mac_addr,
1616 f->macaddr, ETH_ALEN);
1617 add_list[num_add].vlan_tag =
1618 cpu_to_le16(
1619 (u16)(f->vlan == I40E_VLAN_ANY ? 0 : f->vlan));
1620 add_list[num_add].queue_number = 0;
1621
1622 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001623 add_list[num_add].flags = cpu_to_le16(cmd_flags);
1624 num_add++;
1625
1626 /* flush a full buffer */
1627 if (num_add == filter_list_len) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001628 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1629 add_list, num_add,
1630 NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001631 num_add = 0;
1632
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001633 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001634 break;
1635 memset(add_list, 0, sizeof(*add_list));
1636 }
1637 }
1638 if (num_add) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001639 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1640 add_list, num_add, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001641 num_add = 0;
1642 }
1643 kfree(add_list);
1644 add_list = NULL;
1645
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001646 if (add_happened && (!aq_ret)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001647 /* do nothing */;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001648 } else if (add_happened && (aq_ret)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001649 dev_info(&pf->pdev->dev,
1650 "add filter failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001651 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001652 if ((pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOSPC) &&
1653 !test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1654 &vsi->state)) {
1655 promisc_forced_on = true;
1656 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1657 &vsi->state);
1658 dev_info(&pf->pdev->dev, "promiscuous mode forced on\n");
1659 }
1660 }
1661 }
1662
1663 /* check for changes in promiscuous modes */
1664 if (changed_flags & IFF_ALLMULTI) {
1665 bool cur_multipromisc;
1666 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001667 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
1668 vsi->seid,
1669 cur_multipromisc,
1670 NULL);
1671 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001672 dev_info(&pf->pdev->dev,
1673 "set multi promisc failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001674 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001675 }
1676 if ((changed_flags & IFF_PROMISC) || promisc_forced_on) {
1677 bool cur_promisc;
1678 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
1679 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1680 &vsi->state));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001681 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(&vsi->back->hw,
1682 vsi->seid,
1683 cur_promisc, NULL);
1684 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001685 dev_info(&pf->pdev->dev,
1686 "set uni promisc failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001687 aq_ret, pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00001688 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
1689 vsi->seid,
1690 cur_promisc, NULL);
1691 if (aq_ret)
1692 dev_info(&pf->pdev->dev,
1693 "set brdcast promisc failed, err %d, aq_err %d\n",
1694 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001695 }
1696
1697 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
1698 return 0;
1699}
1700
1701/**
1702 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
1703 * @pf: board private structure
1704 **/
1705static void i40e_sync_filters_subtask(struct i40e_pf *pf)
1706{
1707 int v;
1708
1709 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
1710 return;
1711 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
1712
1713 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
1714 if (pf->vsi[v] &&
1715 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED))
1716 i40e_sync_vsi_filters(pf->vsi[v]);
1717 }
1718}
1719
1720/**
1721 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
1722 * @netdev: network interface device structure
1723 * @new_mtu: new value for maximum frame size
1724 *
1725 * Returns 0 on success, negative on failure
1726 **/
1727static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
1728{
1729 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg61a46a42014-04-23 04:50:05 +00001730 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001731 struct i40e_vsi *vsi = np->vsi;
1732
1733 /* MTU < 68 is an error and causes problems on some kernels */
1734 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
1735 return -EINVAL;
1736
1737 netdev_info(netdev, "changing MTU from %d to %d\n",
1738 netdev->mtu, new_mtu);
1739 netdev->mtu = new_mtu;
1740 if (netif_running(netdev))
1741 i40e_vsi_reinit_locked(vsi);
1742
1743 return 0;
1744}
1745
1746/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00001747 * i40e_ioctl - Access the hwtstamp interface
1748 * @netdev: network interface device structure
1749 * @ifr: interface request data
1750 * @cmd: ioctl command
1751 **/
1752int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1753{
1754 struct i40e_netdev_priv *np = netdev_priv(netdev);
1755 struct i40e_pf *pf = np->vsi->back;
1756
1757 switch (cmd) {
1758 case SIOCGHWTSTAMP:
1759 return i40e_ptp_get_ts_config(pf, ifr);
1760 case SIOCSHWTSTAMP:
1761 return i40e_ptp_set_ts_config(pf, ifr);
1762 default:
1763 return -EOPNOTSUPP;
1764 }
1765}
1766
1767/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001768 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
1769 * @vsi: the vsi being adjusted
1770 **/
1771void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
1772{
1773 struct i40e_vsi_context ctxt;
1774 i40e_status ret;
1775
1776 if ((vsi->info.valid_sections &
1777 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1778 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
1779 return; /* already enabled */
1780
1781 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1782 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1783 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
1784
1785 ctxt.seid = vsi->seid;
1786 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1787 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1788 if (ret) {
1789 dev_info(&vsi->back->pdev->dev,
1790 "%s: update vsi failed, aq_err=%d\n",
1791 __func__, vsi->back->hw.aq.asq_last_status);
1792 }
1793}
1794
1795/**
1796 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
1797 * @vsi: the vsi being adjusted
1798 **/
1799void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
1800{
1801 struct i40e_vsi_context ctxt;
1802 i40e_status ret;
1803
1804 if ((vsi->info.valid_sections &
1805 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1806 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
1807 I40E_AQ_VSI_PVLAN_EMOD_MASK))
1808 return; /* already disabled */
1809
1810 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1811 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1812 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
1813
1814 ctxt.seid = vsi->seid;
1815 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1816 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1817 if (ret) {
1818 dev_info(&vsi->back->pdev->dev,
1819 "%s: update vsi failed, aq_err=%d\n",
1820 __func__, vsi->back->hw.aq.asq_last_status);
1821 }
1822}
1823
1824/**
1825 * i40e_vlan_rx_register - Setup or shutdown vlan offload
1826 * @netdev: network interface to be adjusted
1827 * @features: netdev features to test if VLAN offload is enabled or not
1828 **/
1829static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
1830{
1831 struct i40e_netdev_priv *np = netdev_priv(netdev);
1832 struct i40e_vsi *vsi = np->vsi;
1833
1834 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1835 i40e_vlan_stripping_enable(vsi);
1836 else
1837 i40e_vlan_stripping_disable(vsi);
1838}
1839
1840/**
1841 * i40e_vsi_add_vlan - Add vsi membership for given vlan
1842 * @vsi: the vsi being configured
1843 * @vid: vlan id to be added (0 = untagged only , -1 = any)
1844 **/
1845int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
1846{
1847 struct i40e_mac_filter *f, *add_f;
1848 bool is_netdev, is_vf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001849
1850 is_vf = (vsi->type == I40E_VSI_SRIOV);
1851 is_netdev = !!(vsi->netdev);
1852
1853 if (is_netdev) {
1854 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid,
1855 is_vf, is_netdev);
1856 if (!add_f) {
1857 dev_info(&vsi->back->pdev->dev,
1858 "Could not add vlan filter %d for %pM\n",
1859 vid, vsi->netdev->dev_addr);
1860 return -ENOMEM;
1861 }
1862 }
1863
1864 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1865 add_f = i40e_add_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
1866 if (!add_f) {
1867 dev_info(&vsi->back->pdev->dev,
1868 "Could not add vlan filter %d for %pM\n",
1869 vid, f->macaddr);
1870 return -ENOMEM;
1871 }
1872 }
1873
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001874 /* Now if we add a vlan tag, make sure to check if it is the first
1875 * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
1876 * with 0, so we now accept untagged and specified tagged traffic
1877 * (and not any taged and untagged)
1878 */
1879 if (vid > 0) {
1880 if (is_netdev && i40e_find_filter(vsi, vsi->netdev->dev_addr,
1881 I40E_VLAN_ANY,
1882 is_vf, is_netdev)) {
1883 i40e_del_filter(vsi, vsi->netdev->dev_addr,
1884 I40E_VLAN_ANY, is_vf, is_netdev);
1885 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0,
1886 is_vf, is_netdev);
1887 if (!add_f) {
1888 dev_info(&vsi->back->pdev->dev,
1889 "Could not add filter 0 for %pM\n",
1890 vsi->netdev->dev_addr);
1891 return -ENOMEM;
1892 }
1893 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08001894 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001895
Greg Rose8d82a7c2014-01-13 16:13:04 -08001896 /* Do not assume that I40E_VLAN_ANY should be reset to VLAN 0 */
1897 if (vid > 0 && !vsi->info.pvid) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001898 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1899 if (i40e_find_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1900 is_vf, is_netdev)) {
1901 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1902 is_vf, is_netdev);
1903 add_f = i40e_add_filter(vsi, f->macaddr,
1904 0, is_vf, is_netdev);
1905 if (!add_f) {
1906 dev_info(&vsi->back->pdev->dev,
1907 "Could not add filter 0 for %pM\n",
1908 f->macaddr);
1909 return -ENOMEM;
1910 }
1911 }
1912 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001913 }
1914
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001915 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1916 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1917 return 0;
1918
1919 return i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001920}
1921
1922/**
1923 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
1924 * @vsi: the vsi being configured
1925 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001926 *
1927 * Return: 0 on success or negative otherwise
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001928 **/
1929int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
1930{
1931 struct net_device *netdev = vsi->netdev;
1932 struct i40e_mac_filter *f, *add_f;
1933 bool is_vf, is_netdev;
1934 int filter_count = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001935
1936 is_vf = (vsi->type == I40E_VSI_SRIOV);
1937 is_netdev = !!(netdev);
1938
1939 if (is_netdev)
1940 i40e_del_filter(vsi, netdev->dev_addr, vid, is_vf, is_netdev);
1941
1942 list_for_each_entry(f, &vsi->mac_filter_list, list)
1943 i40e_del_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
1944
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001945 /* go through all the filters for this VSI and if there is only
1946 * vid == 0 it means there are no other filters, so vid 0 must
1947 * be replaced with -1. This signifies that we should from now
1948 * on accept any traffic (with any tag present, or untagged)
1949 */
1950 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1951 if (is_netdev) {
1952 if (f->vlan &&
1953 ether_addr_equal(netdev->dev_addr, f->macaddr))
1954 filter_count++;
1955 }
1956
1957 if (f->vlan)
1958 filter_count++;
1959 }
1960
1961 if (!filter_count && is_netdev) {
1962 i40e_del_filter(vsi, netdev->dev_addr, 0, is_vf, is_netdev);
1963 f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
1964 is_vf, is_netdev);
1965 if (!f) {
1966 dev_info(&vsi->back->pdev->dev,
1967 "Could not add filter %d for %pM\n",
1968 I40E_VLAN_ANY, netdev->dev_addr);
1969 return -ENOMEM;
1970 }
1971 }
1972
1973 if (!filter_count) {
1974 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1975 i40e_del_filter(vsi, f->macaddr, 0, is_vf, is_netdev);
1976 add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1977 is_vf, is_netdev);
1978 if (!add_f) {
1979 dev_info(&vsi->back->pdev->dev,
1980 "Could not add filter %d for %pM\n",
1981 I40E_VLAN_ANY, f->macaddr);
1982 return -ENOMEM;
1983 }
1984 }
1985 }
1986
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001987 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1988 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1989 return 0;
1990
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001991 return i40e_sync_vsi_filters(vsi);
1992}
1993
1994/**
1995 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
1996 * @netdev: network interface to be adjusted
1997 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001998 *
1999 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002000 **/
2001static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2002 __always_unused __be16 proto, u16 vid)
2003{
2004 struct i40e_netdev_priv *np = netdev_priv(netdev);
2005 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002006 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002007
2008 if (vid > 4095)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002009 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002010
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002011 netdev_info(netdev, "adding %pM vid=%d\n", netdev->dev_addr, vid);
2012
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002013 /* If the network stack called us with vid = 0 then
2014 * it is asking to receive priority tagged packets with
2015 * vlan id 0. Our HW receives them by default when configured
2016 * to receive untagged packets so there is no need to add an
2017 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002018 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002019 if (vid)
2020 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002021
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002022 if (!ret && (vid < VLAN_N_VID))
2023 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002024
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002025 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002026}
2027
2028/**
2029 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2030 * @netdev: network interface to be adjusted
2031 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002032 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002033 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002034 **/
2035static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2036 __always_unused __be16 proto, u16 vid)
2037{
2038 struct i40e_netdev_priv *np = netdev_priv(netdev);
2039 struct i40e_vsi *vsi = np->vsi;
2040
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002041 netdev_info(netdev, "removing %pM vid=%d\n", netdev->dev_addr, vid);
2042
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002043 /* return code is ignored as there is nothing a user
2044 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002045 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002046 */
2047 i40e_vsi_kill_vlan(vsi, vid);
2048
2049 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002050
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002051 return 0;
2052}
2053
2054/**
2055 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2056 * @vsi: the vsi being brought back up
2057 **/
2058static void i40e_restore_vlan(struct i40e_vsi *vsi)
2059{
2060 u16 vid;
2061
2062 if (!vsi->netdev)
2063 return;
2064
2065 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2066
2067 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2068 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2069 vid);
2070}
2071
2072/**
2073 * i40e_vsi_add_pvid - Add pvid for the VSI
2074 * @vsi: the vsi being adjusted
2075 * @vid: the vlan id to set as a PVID
2076 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002077int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002078{
2079 struct i40e_vsi_context ctxt;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002080 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002081
2082 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2083 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002084 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2085 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002086 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002087
2088 ctxt.seid = vsi->seid;
2089 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002090 aq_ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2091 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002092 dev_info(&vsi->back->pdev->dev,
2093 "%s: update vsi failed, aq_err=%d\n",
2094 __func__, vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002095 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002096 }
2097
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002098 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002099}
2100
2101/**
2102 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2103 * @vsi: the vsi being adjusted
2104 *
2105 * Just use the vlan_rx_register() service to put it back to normal
2106 **/
2107void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2108{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002109 i40e_vlan_stripping_disable(vsi);
2110
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002111 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002112}
2113
2114/**
2115 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2116 * @vsi: ptr to the VSI
2117 *
2118 * If this function returns with an error, then it's possible one or
2119 * more of the rings is populated (while the rest are not). It is the
2120 * callers duty to clean those orphaned rings.
2121 *
2122 * Return 0 on success, negative on failure
2123 **/
2124static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2125{
2126 int i, err = 0;
2127
2128 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002129 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002130
2131 return err;
2132}
2133
2134/**
2135 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2136 * @vsi: ptr to the VSI
2137 *
2138 * Free VSI's transmit software resources
2139 **/
2140static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2141{
2142 int i;
2143
Greg Rose8e9dca52013-12-18 13:45:53 +00002144 if (!vsi->tx_rings)
2145 return;
2146
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002147 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002148 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002149 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002150}
2151
2152/**
2153 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2154 * @vsi: ptr to the VSI
2155 *
2156 * If this function returns with an error, then it's possible one or
2157 * more of the rings is populated (while the rest are not). It is the
2158 * callers duty to clean those orphaned rings.
2159 *
2160 * Return 0 on success, negative on failure
2161 **/
2162static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2163{
2164 int i, err = 0;
2165
2166 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002167 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002168 return err;
2169}
2170
2171/**
2172 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2173 * @vsi: ptr to the VSI
2174 *
2175 * Free all receive software resources
2176 **/
2177static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2178{
2179 int i;
2180
Greg Rose8e9dca52013-12-18 13:45:53 +00002181 if (!vsi->rx_rings)
2182 return;
2183
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002184 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002185 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002186 i40e_free_rx_resources(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002187}
2188
2189/**
2190 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2191 * @ring: The Tx ring to configure
2192 *
2193 * Configure the Tx descriptor ring in the HMC context.
2194 **/
2195static int i40e_configure_tx_ring(struct i40e_ring *ring)
2196{
2197 struct i40e_vsi *vsi = ring->vsi;
2198 u16 pf_q = vsi->base_queue + ring->queue_index;
2199 struct i40e_hw *hw = &vsi->back->hw;
2200 struct i40e_hmc_obj_txq tx_ctx;
2201 i40e_status err = 0;
2202 u32 qtx_ctl = 0;
2203
2204 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002205 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002206 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2207 ring->atr_count = 0;
2208 } else {
2209 ring->atr_sample_rate = 0;
2210 }
2211
2212 /* initialize XPS */
2213 if (ring->q_vector && ring->netdev &&
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08002214 vsi->tc_config.numtc <= 1 &&
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002215 !test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
2216 netif_set_xps_queue(ring->netdev,
2217 &ring->q_vector->affinity_mask,
2218 ring->queue_index);
2219
2220 /* clear the context structure first */
2221 memset(&tx_ctx, 0, sizeof(tx_ctx));
2222
2223 tx_ctx.new_context = 1;
2224 tx_ctx.base = (ring->dma / 128);
2225 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002226 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2227 I40E_FLAG_FD_ATR_ENABLED));
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002228 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002229 /* FDIR VSI tx ring can still use RS bit and writebacks */
2230 if (vsi->type != I40E_VSI_FDIR)
2231 tx_ctx.head_wb_ena = 1;
2232 tx_ctx.head_wb_addr = ring->dma +
2233 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002234
2235 /* As part of VSI creation/update, FW allocates certain
2236 * Tx arbitration queue sets for each TC enabled for
2237 * the VSI. The FW returns the handles to these queue
2238 * sets as part of the response buffer to Add VSI,
2239 * Update VSI, etc. AQ commands. It is expected that
2240 * these queue set handles be associated with the Tx
2241 * queues by the driver as part of the TX queue context
2242 * initialization. This has to be done regardless of
2243 * DCB as by default everything is mapped to TC0.
2244 */
2245 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2246 tx_ctx.rdylist_act = 0;
2247
2248 /* clear the context in the HMC */
2249 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2250 if (err) {
2251 dev_info(&vsi->back->pdev->dev,
2252 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2253 ring->queue_index, pf_q, err);
2254 return -ENOMEM;
2255 }
2256
2257 /* set the context in the HMC */
2258 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2259 if (err) {
2260 dev_info(&vsi->back->pdev->dev,
2261 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2262 ring->queue_index, pf_q, err);
2263 return -ENOMEM;
2264 }
2265
2266 /* Now associate this queue with this PCI function */
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002267 if (vsi->type == I40E_VSI_VMDQ2)
2268 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
2269 else
2270 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Shannon Nelson13fd9772013-09-28 07:14:19 +00002271 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2272 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002273 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2274 i40e_flush(hw);
2275
2276 clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
2277
2278 /* cache tail off for easier writes later */
2279 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2280
2281 return 0;
2282}
2283
2284/**
2285 * i40e_configure_rx_ring - Configure a receive ring context
2286 * @ring: The Rx ring to configure
2287 *
2288 * Configure the Rx descriptor ring in the HMC context.
2289 **/
2290static int i40e_configure_rx_ring(struct i40e_ring *ring)
2291{
2292 struct i40e_vsi *vsi = ring->vsi;
2293 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2294 u16 pf_q = vsi->base_queue + ring->queue_index;
2295 struct i40e_hw *hw = &vsi->back->hw;
2296 struct i40e_hmc_obj_rxq rx_ctx;
2297 i40e_status err = 0;
2298
2299 ring->state = 0;
2300
2301 /* clear the context structure first */
2302 memset(&rx_ctx, 0, sizeof(rx_ctx));
2303
2304 ring->rx_buf_len = vsi->rx_buf_len;
2305 ring->rx_hdr_len = vsi->rx_hdr_len;
2306
2307 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
2308 rx_ctx.hbuff = ring->rx_hdr_len >> I40E_RXQ_CTX_HBUFF_SHIFT;
2309
2310 rx_ctx.base = (ring->dma / 128);
2311 rx_ctx.qlen = ring->count;
2312
2313 if (vsi->back->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED) {
2314 set_ring_16byte_desc_enabled(ring);
2315 rx_ctx.dsize = 0;
2316 } else {
2317 rx_ctx.dsize = 1;
2318 }
2319
2320 rx_ctx.dtype = vsi->dtype;
2321 if (vsi->dtype) {
2322 set_ring_ps_enabled(ring);
2323 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
2324 I40E_RX_SPLIT_IP |
2325 I40E_RX_SPLIT_TCP_UDP |
2326 I40E_RX_SPLIT_SCTP;
2327 } else {
2328 rx_ctx.hsplit_0 = 0;
2329 }
2330
2331 rx_ctx.rxmax = min_t(u16, vsi->max_frame,
2332 (chain_len * ring->rx_buf_len));
2333 rx_ctx.tphrdesc_ena = 1;
2334 rx_ctx.tphwdesc_ena = 1;
2335 rx_ctx.tphdata_ena = 1;
2336 rx_ctx.tphhead_ena = 1;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00002337 if (hw->revision_id == 0)
2338 rx_ctx.lrxqthresh = 0;
2339 else
2340 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002341 rx_ctx.crcstrip = 1;
2342 rx_ctx.l2tsel = 1;
2343 rx_ctx.showiv = 1;
Catherine Sullivanacb36762014-03-06 09:02:30 +00002344 /* set the prefena field to 1 because the manual says to */
2345 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002346
2347 /* clear the context in the HMC */
2348 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2349 if (err) {
2350 dev_info(&vsi->back->pdev->dev,
2351 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2352 ring->queue_index, pf_q, err);
2353 return -ENOMEM;
2354 }
2355
2356 /* set the context in the HMC */
2357 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2358 if (err) {
2359 dev_info(&vsi->back->pdev->dev,
2360 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2361 ring->queue_index, pf_q, err);
2362 return -ENOMEM;
2363 }
2364
2365 /* cache tail for quicker writes, and clear the reg before use */
2366 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2367 writel(0, ring->tail);
2368
2369 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
2370
2371 return 0;
2372}
2373
2374/**
2375 * i40e_vsi_configure_tx - Configure the VSI for Tx
2376 * @vsi: VSI structure describing this set of rings and resources
2377 *
2378 * Configure the Tx VSI for operation.
2379 **/
2380static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2381{
2382 int err = 0;
2383 u16 i;
2384
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002385 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2386 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002387
2388 return err;
2389}
2390
2391/**
2392 * i40e_vsi_configure_rx - Configure the VSI for Rx
2393 * @vsi: the VSI being configured
2394 *
2395 * Configure the Rx VSI for operation.
2396 **/
2397static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
2398{
2399 int err = 0;
2400 u16 i;
2401
2402 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
2403 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
2404 + ETH_FCS_LEN + VLAN_HLEN;
2405 else
2406 vsi->max_frame = I40E_RXBUFFER_2048;
2407
2408 /* figure out correct receive buffer length */
2409 switch (vsi->back->flags & (I40E_FLAG_RX_1BUF_ENABLED |
2410 I40E_FLAG_RX_PS_ENABLED)) {
2411 case I40E_FLAG_RX_1BUF_ENABLED:
2412 vsi->rx_hdr_len = 0;
2413 vsi->rx_buf_len = vsi->max_frame;
2414 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2415 break;
2416 case I40E_FLAG_RX_PS_ENABLED:
2417 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2418 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2419 vsi->dtype = I40E_RX_DTYPE_HEADER_SPLIT;
2420 break;
2421 default:
2422 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2423 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2424 vsi->dtype = I40E_RX_DTYPE_SPLIT_ALWAYS;
2425 break;
2426 }
2427
2428 /* round up for the chip's needs */
2429 vsi->rx_hdr_len = ALIGN(vsi->rx_hdr_len,
2430 (1 << I40E_RXQ_CTX_HBUFF_SHIFT));
2431 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
2432 (1 << I40E_RXQ_CTX_DBUFF_SHIFT));
2433
2434 /* set up individual rings */
2435 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002436 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002437
2438 return err;
2439}
2440
2441/**
2442 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
2443 * @vsi: ptr to the VSI
2444 **/
2445static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
2446{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00002447 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002448 u16 qoffset, qcount;
2449 int i, n;
2450
2451 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED))
2452 return;
2453
2454 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
2455 if (!(vsi->tc_config.enabled_tc & (1 << n)))
2456 continue;
2457
2458 qoffset = vsi->tc_config.tc_info[n].qoffset;
2459 qcount = vsi->tc_config.tc_info[n].qcount;
2460 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00002461 rx_ring = vsi->rx_rings[i];
2462 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002463 rx_ring->dcb_tc = n;
2464 tx_ring->dcb_tc = n;
2465 }
2466 }
2467}
2468
2469/**
2470 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
2471 * @vsi: ptr to the VSI
2472 **/
2473static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
2474{
2475 if (vsi->netdev)
2476 i40e_set_rx_mode(vsi->netdev);
2477}
2478
2479/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00002480 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
2481 * @vsi: Pointer to the targeted VSI
2482 *
2483 * This function replays the hlist on the hw where all the SB Flow Director
2484 * filters were saved.
2485 **/
2486static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
2487{
2488 struct i40e_fdir_filter *filter;
2489 struct i40e_pf *pf = vsi->back;
2490 struct hlist_node *node;
2491
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00002492 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
2493 return;
2494
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00002495 hlist_for_each_entry_safe(filter, node,
2496 &pf->fdir_filter_list, fdir_node) {
2497 i40e_add_del_fdir(vsi, filter, true);
2498 }
2499}
2500
2501/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002502 * i40e_vsi_configure - Set up the VSI for action
2503 * @vsi: the VSI being configured
2504 **/
2505static int i40e_vsi_configure(struct i40e_vsi *vsi)
2506{
2507 int err;
2508
2509 i40e_set_vsi_rx_mode(vsi);
2510 i40e_restore_vlan(vsi);
2511 i40e_vsi_config_dcb_rings(vsi);
2512 err = i40e_vsi_configure_tx(vsi);
2513 if (!err)
2514 err = i40e_vsi_configure_rx(vsi);
2515
2516 return err;
2517}
2518
2519/**
2520 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
2521 * @vsi: the VSI being configured
2522 **/
2523static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
2524{
2525 struct i40e_pf *pf = vsi->back;
2526 struct i40e_q_vector *q_vector;
2527 struct i40e_hw *hw = &pf->hw;
2528 u16 vector;
2529 int i, q;
2530 u32 val;
2531 u32 qp;
2532
2533 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
2534 * and PFINT_LNKLSTn registers, e.g.:
2535 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
2536 */
2537 qp = vsi->base_queue;
2538 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00002539 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
2540 q_vector = vsi->q_vectors[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002541 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2542 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2543 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
2544 q_vector->rx.itr);
2545 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2546 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2547 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
2548 q_vector->tx.itr);
2549
2550 /* Linked list for the queuepairs assigned to this vector */
2551 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
2552 for (q = 0; q < q_vector->num_ringpairs; q++) {
2553 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2554 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2555 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
2556 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
2557 (I40E_QUEUE_TYPE_TX
2558 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
2559
2560 wr32(hw, I40E_QINT_RQCTL(qp), val);
2561
2562 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2563 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2564 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
2565 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
2566 (I40E_QUEUE_TYPE_RX
2567 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2568
2569 /* Terminate the linked list */
2570 if (q == (q_vector->num_ringpairs - 1))
2571 val |= (I40E_QUEUE_END_OF_LIST
2572 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2573
2574 wr32(hw, I40E_QINT_TQCTL(qp), val);
2575 qp++;
2576 }
2577 }
2578
2579 i40e_flush(hw);
2580}
2581
2582/**
2583 * i40e_enable_misc_int_causes - enable the non-queue interrupts
2584 * @hw: ptr to the hardware info
2585 **/
2586static void i40e_enable_misc_int_causes(struct i40e_hw *hw)
2587{
2588 u32 val;
2589
2590 /* clear things first */
2591 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
2592 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
2593
2594 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
2595 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
2596 I40E_PFINT_ICR0_ENA_GRST_MASK |
2597 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
2598 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002599 I40E_PFINT_ICR0_ENA_TIMESYNC_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002600 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
2601 I40E_PFINT_ICR0_ENA_VFLR_MASK |
2602 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2603
2604 wr32(hw, I40E_PFINT_ICR0_ENA, val);
2605
2606 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00002607 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
2608 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002609
2610 /* OTHER_ITR_IDX = 0 */
2611 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
2612}
2613
2614/**
2615 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
2616 * @vsi: the VSI being configured
2617 **/
2618static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
2619{
Alexander Duyck493fb302013-09-28 07:01:44 +00002620 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002621 struct i40e_pf *pf = vsi->back;
2622 struct i40e_hw *hw = &pf->hw;
2623 u32 val;
2624
2625 /* set the ITR configuration */
2626 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2627 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2628 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
2629 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2630 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2631 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
2632
2633 i40e_enable_misc_int_causes(hw);
2634
2635 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
2636 wr32(hw, I40E_PFINT_LNKLST0, 0);
2637
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00002638 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002639 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2640 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2641 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2642
2643 wr32(hw, I40E_QINT_RQCTL(0), val);
2644
2645 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2646 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2647 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2648
2649 wr32(hw, I40E_QINT_TQCTL(0), val);
2650 i40e_flush(hw);
2651}
2652
2653/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00002654 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
2655 * @pf: board private structure
2656 **/
2657void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
2658{
2659 struct i40e_hw *hw = &pf->hw;
2660
2661 wr32(hw, I40E_PFINT_DYN_CTL0,
2662 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2663 i40e_flush(hw);
2664}
2665
2666/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002667 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
2668 * @pf: board private structure
2669 **/
Shannon Nelson116a57d2013-09-28 07:13:59 +00002670void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002671{
2672 struct i40e_hw *hw = &pf->hw;
2673 u32 val;
2674
2675 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
2676 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
2677 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
2678
2679 wr32(hw, I40E_PFINT_DYN_CTL0, val);
2680 i40e_flush(hw);
2681}
2682
2683/**
2684 * i40e_irq_dynamic_enable - Enable default interrupt generation settings
2685 * @vsi: pointer to a vsi
2686 * @vector: enable a particular Hw Interrupt vector
2687 **/
2688void i40e_irq_dynamic_enable(struct i40e_vsi *vsi, int vector)
2689{
2690 struct i40e_pf *pf = vsi->back;
2691 struct i40e_hw *hw = &pf->hw;
2692 u32 val;
2693
2694 val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
2695 I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
2696 (I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2697 wr32(hw, I40E_PFINT_DYN_CTLN(vector - 1), val);
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00002698 /* skip the flush */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002699}
2700
2701/**
2702 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
2703 * @irq: interrupt number
2704 * @data: pointer to a q_vector
2705 **/
2706static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
2707{
2708 struct i40e_q_vector *q_vector = data;
2709
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002710 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002711 return IRQ_HANDLED;
2712
2713 napi_schedule(&q_vector->napi);
2714
2715 return IRQ_HANDLED;
2716}
2717
2718/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002719 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
2720 * @vsi: the VSI being configured
2721 * @basename: name for the vector
2722 *
2723 * Allocates MSI-X vectors and requests interrupts from the kernel.
2724 **/
2725static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
2726{
2727 int q_vectors = vsi->num_q_vectors;
2728 struct i40e_pf *pf = vsi->back;
2729 int base = vsi->base_vector;
2730 int rx_int_idx = 0;
2731 int tx_int_idx = 0;
2732 int vector, err;
2733
2734 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00002735 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002736
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002737 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002738 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2739 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
2740 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002741 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002742 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2743 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002744 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002745 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2746 "%s-%s-%d", basename, "tx", tx_int_idx++);
2747 } else {
2748 /* skip this unused q_vector */
2749 continue;
2750 }
2751 err = request_irq(pf->msix_entries[base + vector].vector,
2752 vsi->irq_handler,
2753 0,
2754 q_vector->name,
2755 q_vector);
2756 if (err) {
2757 dev_info(&pf->pdev->dev,
2758 "%s: request_irq failed, error: %d\n",
2759 __func__, err);
2760 goto free_queue_irqs;
2761 }
2762 /* assign the mask for this irq */
2763 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2764 &q_vector->affinity_mask);
2765 }
2766
2767 return 0;
2768
2769free_queue_irqs:
2770 while (vector) {
2771 vector--;
2772 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2773 NULL);
2774 free_irq(pf->msix_entries[base + vector].vector,
2775 &(vsi->q_vectors[vector]));
2776 }
2777 return err;
2778}
2779
2780/**
2781 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
2782 * @vsi: the VSI being un-configured
2783 **/
2784static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
2785{
2786 struct i40e_pf *pf = vsi->back;
2787 struct i40e_hw *hw = &pf->hw;
2788 int base = vsi->base_vector;
2789 int i;
2790
2791 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002792 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
2793 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002794 }
2795
2796 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2797 for (i = vsi->base_vector;
2798 i < (vsi->num_q_vectors + vsi->base_vector); i++)
2799 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
2800
2801 i40e_flush(hw);
2802 for (i = 0; i < vsi->num_q_vectors; i++)
2803 synchronize_irq(pf->msix_entries[i + base].vector);
2804 } else {
2805 /* Legacy and MSI mode - this stops all interrupt handling */
2806 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
2807 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
2808 i40e_flush(hw);
2809 synchronize_irq(pf->pdev->irq);
2810 }
2811}
2812
2813/**
2814 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
2815 * @vsi: the VSI being configured
2816 **/
2817static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
2818{
2819 struct i40e_pf *pf = vsi->back;
2820 int i;
2821
2822 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2823 for (i = vsi->base_vector;
2824 i < (vsi->num_q_vectors + vsi->base_vector); i++)
2825 i40e_irq_dynamic_enable(vsi, i);
2826 } else {
2827 i40e_irq_dynamic_enable_icr0(pf);
2828 }
2829
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00002830 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002831 return 0;
2832}
2833
2834/**
2835 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
2836 * @pf: board private structure
2837 **/
2838static void i40e_stop_misc_vector(struct i40e_pf *pf)
2839{
2840 /* Disable ICR 0 */
2841 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
2842 i40e_flush(&pf->hw);
2843}
2844
2845/**
2846 * i40e_intr - MSI/Legacy and non-queue interrupt handler
2847 * @irq: interrupt number
2848 * @data: pointer to a q_vector
2849 *
2850 * This is the handler used for all MSI/Legacy interrupts, and deals
2851 * with both queue and non-queue interrupts. This is also used in
2852 * MSIX mode to handle the non-queue interrupts.
2853 **/
2854static irqreturn_t i40e_intr(int irq, void *data)
2855{
2856 struct i40e_pf *pf = (struct i40e_pf *)data;
2857 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002858 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002859 u32 icr0, icr0_remaining;
2860 u32 val, ena_mask;
2861
2862 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002863 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002864
Shannon Nelson116a57d2013-09-28 07:13:59 +00002865 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
2866 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002867 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002868
Shannon Nelsoncd92e722013-11-16 10:00:44 +00002869 /* if interrupt but no bits showing, must be SWINT */
2870 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
2871 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
2872 pf->sw_int_count++;
2873
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002874 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
2875 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
2876
2877 /* temporarily disable queue cause for NAPI processing */
2878 u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
2879 qval &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
2880 wr32(hw, I40E_QINT_RQCTL(0), qval);
2881
2882 qval = rd32(hw, I40E_QINT_TQCTL(0));
2883 qval &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
2884 wr32(hw, I40E_QINT_TQCTL(0), qval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002885
2886 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck493fb302013-09-28 07:01:44 +00002887 napi_schedule(&pf->vsi[pf->lan_vsi]->q_vectors[0]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002888 }
2889
2890 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
2891 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2892 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
2893 }
2894
2895 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
2896 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
2897 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
2898 }
2899
2900 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
2901 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
2902 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
2903 }
2904
2905 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
2906 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
2907 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
2908 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
2909 val = rd32(hw, I40E_GLGEN_RSTAT);
2910 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
2911 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002912 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002913 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002914 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002915 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002916 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002917 pf->empr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002918 set_bit(__I40E_EMP_RESET_REQUESTED, &pf->state);
2919 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002920 }
2921
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002922 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
2923 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
2924 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
2925 }
2926
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002927 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
2928 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
2929
2930 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07002931 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002932 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002933 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002934 }
2935
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002936 /* If a critical error is pending we have no choice but to reset the
2937 * device.
2938 * Report and mask out any remaining unexpected interrupts.
2939 */
2940 icr0_remaining = icr0 & ena_mask;
2941 if (icr0_remaining) {
2942 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
2943 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002944 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002945 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00002946 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002947 dev_info(&pf->pdev->dev, "device will be reset\n");
2948 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
2949 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002950 }
2951 ena_mask &= ~icr0_remaining;
2952 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002953 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002954
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002955enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002956 /* re-enable interrupt causes */
2957 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002958 if (!test_bit(__I40E_DOWN, &pf->state)) {
2959 i40e_service_event_schedule(pf);
2960 i40e_irq_dynamic_enable_icr0(pf);
2961 }
2962
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002963 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002964}
2965
2966/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08002967 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
2968 * @tx_ring: tx ring to clean
2969 * @budget: how many cleans we're allowed
2970 *
2971 * Returns true if there's any budget left (e.g. the clean is finished)
2972 **/
2973static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
2974{
2975 struct i40e_vsi *vsi = tx_ring->vsi;
2976 u16 i = tx_ring->next_to_clean;
2977 struct i40e_tx_buffer *tx_buf;
2978 struct i40e_tx_desc *tx_desc;
2979
2980 tx_buf = &tx_ring->tx_bi[i];
2981 tx_desc = I40E_TX_DESC(tx_ring, i);
2982 i -= tx_ring->count;
2983
2984 do {
2985 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
2986
2987 /* if next_to_watch is not set then there is no work pending */
2988 if (!eop_desc)
2989 break;
2990
2991 /* prevent any other reads prior to eop_desc */
2992 read_barrier_depends();
2993
2994 /* if the descriptor isn't done, no work yet to do */
2995 if (!(eop_desc->cmd_type_offset_bsz &
2996 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
2997 break;
2998
2999 /* clear next_to_watch to prevent false hangs */
3000 tx_buf->next_to_watch = NULL;
3001
3002 /* unmap skb header data */
3003 dma_unmap_single(tx_ring->dev,
3004 dma_unmap_addr(tx_buf, dma),
3005 dma_unmap_len(tx_buf, len),
3006 DMA_TO_DEVICE);
3007
3008 dma_unmap_len_set(tx_buf, len, 0);
3009
3010
3011 /* move to the next desc and buffer to clean */
3012 tx_buf++;
3013 tx_desc++;
3014 i++;
3015 if (unlikely(!i)) {
3016 i -= tx_ring->count;
3017 tx_buf = tx_ring->tx_bi;
3018 tx_desc = I40E_TX_DESC(tx_ring, 0);
3019 }
3020
3021 /* update budget accounting */
3022 budget--;
3023 } while (likely(budget));
3024
3025 i += tx_ring->count;
3026 tx_ring->next_to_clean = i;
3027
3028 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
3029 i40e_irq_dynamic_enable(vsi,
3030 tx_ring->q_vector->v_idx + vsi->base_vector);
3031 }
3032 return budget > 0;
3033}
3034
3035/**
3036 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3037 * @irq: interrupt number
3038 * @data: pointer to a q_vector
3039 **/
3040static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3041{
3042 struct i40e_q_vector *q_vector = data;
3043 struct i40e_vsi *vsi;
3044
3045 if (!q_vector->tx.ring)
3046 return IRQ_HANDLED;
3047
3048 vsi = q_vector->tx.ring->vsi;
3049 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3050
3051 return IRQ_HANDLED;
3052}
3053
3054/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003055 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003056 * @vsi: the VSI being configured
3057 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003058 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003059 **/
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003060static void map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003061{
Alexander Duyck493fb302013-09-28 07:01:44 +00003062 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00003063 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3064 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003065
3066 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003067 tx_ring->next = q_vector->tx.ring;
3068 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003069 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003070
3071 rx_ring->q_vector = q_vector;
3072 rx_ring->next = q_vector->rx.ring;
3073 q_vector->rx.ring = rx_ring;
3074 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003075}
3076
3077/**
3078 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3079 * @vsi: the VSI being configured
3080 *
3081 * This function maps descriptor rings to the queue-specific vectors
3082 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3083 * one vector per queue pair, but on a constrained vector budget, we
3084 * group the queue pairs as "efficiently" as possible.
3085 **/
3086static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3087{
3088 int qp_remaining = vsi->num_queue_pairs;
3089 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003090 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003091 int v_start = 0;
3092 int qp_idx = 0;
3093
3094 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3095 * group them so there are multiple queues per vector.
3096 */
3097 for (; v_start < q_vectors && qp_remaining; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003098 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3099
3100 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3101
3102 q_vector->num_ringpairs = num_ringpairs;
3103
3104 q_vector->rx.count = 0;
3105 q_vector->tx.count = 0;
3106 q_vector->rx.ring = NULL;
3107 q_vector->tx.ring = NULL;
3108
3109 while (num_ringpairs--) {
3110 map_vector_to_qp(vsi, v_start, qp_idx);
3111 qp_idx++;
3112 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003113 }
3114 }
3115}
3116
3117/**
3118 * i40e_vsi_request_irq - Request IRQ from the OS
3119 * @vsi: the VSI being configured
3120 * @basename: name for the vector
3121 **/
3122static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3123{
3124 struct i40e_pf *pf = vsi->back;
3125 int err;
3126
3127 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3128 err = i40e_vsi_request_irq_msix(vsi, basename);
3129 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3130 err = request_irq(pf->pdev->irq, i40e_intr, 0,
3131 pf->misc_int_name, pf);
3132 else
3133 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
3134 pf->misc_int_name, pf);
3135
3136 if (err)
3137 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3138
3139 return err;
3140}
3141
3142#ifdef CONFIG_NET_POLL_CONTROLLER
3143/**
3144 * i40e_netpoll - A Polling 'interrupt'handler
3145 * @netdev: network interface device structure
3146 *
3147 * This is used by netconsole to send skbs without having to re-enable
3148 * interrupts. It's not called while the normal interrupt routine is executing.
3149 **/
3150static void i40e_netpoll(struct net_device *netdev)
3151{
3152 struct i40e_netdev_priv *np = netdev_priv(netdev);
3153 struct i40e_vsi *vsi = np->vsi;
3154 struct i40e_pf *pf = vsi->back;
3155 int i;
3156
3157 /* if interface is down do nothing */
3158 if (test_bit(__I40E_DOWN, &vsi->state))
3159 return;
3160
3161 pf->flags |= I40E_FLAG_IN_NETPOLL;
3162 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3163 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003164 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003165 } else {
3166 i40e_intr(pf->pdev->irq, netdev);
3167 }
3168 pf->flags &= ~I40E_FLAG_IN_NETPOLL;
3169}
3170#endif
3171
3172/**
3173 * i40e_vsi_control_tx - Start or stop a VSI's rings
3174 * @vsi: the VSI being configured
3175 * @enable: start or stop the rings
3176 **/
3177static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3178{
3179 struct i40e_pf *pf = vsi->back;
3180 struct i40e_hw *hw = &pf->hw;
3181 int i, j, pf_q;
3182 u32 tx_reg;
3183
3184 pf_q = vsi->base_queue;
3185 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003186 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003187 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003188 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3189 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3190 break;
3191 usleep_range(1000, 2000);
3192 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003193 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c122002014-03-14 07:32:29 +00003194 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00003195 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003196
3197 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003198 if (enable) {
3199 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003200 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003201 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003202 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003203 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003204
3205 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
3206
3207 /* wait for the change to finish */
3208 for (j = 0; j < 10; j++) {
3209 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Catherine Sullivan7c122002014-03-14 07:32:29 +00003210 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3211 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003212
3213 udelay(10);
3214 }
3215 if (j >= 10) {
3216 dev_info(&pf->pdev->dev, "Tx ring %d %sable timeout\n",
3217 pf_q, (enable ? "en" : "dis"));
3218 return -ETIMEDOUT;
3219 }
3220 }
3221
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003222 if (hw->revision_id == 0)
3223 mdelay(50);
3224
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003225 return 0;
3226}
3227
3228/**
3229 * i40e_vsi_control_rx - Start or stop a VSI's rings
3230 * @vsi: the VSI being configured
3231 * @enable: start or stop the rings
3232 **/
3233static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3234{
3235 struct i40e_pf *pf = vsi->back;
3236 struct i40e_hw *hw = &pf->hw;
3237 int i, j, pf_q;
3238 u32 rx_reg;
3239
3240 pf_q = vsi->base_queue;
3241 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003242 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003243 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003244 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
3245 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
3246 break;
3247 usleep_range(1000, 2000);
3248 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003249
Catherine Sullivan7c122002014-03-14 07:32:29 +00003250 /* Skip if the queue is already in the requested state */
3251 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3252 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003253
3254 /* turn on/off the queue */
3255 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08003256 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003257 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08003258 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003259 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
3260
3261 /* wait for the change to finish */
3262 for (j = 0; j < 10; j++) {
3263 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
3264
Catherine Sullivan7c122002014-03-14 07:32:29 +00003265 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3266 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003267
3268 udelay(10);
3269 }
3270 if (j >= 10) {
3271 dev_info(&pf->pdev->dev, "Rx ring %d %sable timeout\n",
3272 pf_q, (enable ? "en" : "dis"));
3273 return -ETIMEDOUT;
3274 }
3275 }
3276
3277 return 0;
3278}
3279
3280/**
3281 * i40e_vsi_control_rings - Start or stop a VSI's rings
3282 * @vsi: the VSI being configured
3283 * @enable: start or stop the rings
3284 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00003285int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003286{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003287 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003288
3289 /* do rx first for enable and last for disable */
3290 if (request) {
3291 ret = i40e_vsi_control_rx(vsi, request);
3292 if (ret)
3293 return ret;
3294 ret = i40e_vsi_control_tx(vsi, request);
3295 } else {
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003296 /* Ignore return value, we need to shutdown whatever we can */
3297 i40e_vsi_control_tx(vsi, request);
3298 i40e_vsi_control_rx(vsi, request);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003299 }
3300
3301 return ret;
3302}
3303
3304/**
3305 * i40e_vsi_free_irq - Free the irq association with the OS
3306 * @vsi: the VSI being configured
3307 **/
3308static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
3309{
3310 struct i40e_pf *pf = vsi->back;
3311 struct i40e_hw *hw = &pf->hw;
3312 int base = vsi->base_vector;
3313 u32 val, qp;
3314 int i;
3315
3316 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3317 if (!vsi->q_vectors)
3318 return;
3319
3320 for (i = 0; i < vsi->num_q_vectors; i++) {
3321 u16 vector = i + base;
3322
3323 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00003324 if (!vsi->q_vectors[i] ||
3325 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003326 continue;
3327
3328 /* clear the affinity_mask in the IRQ descriptor */
3329 irq_set_affinity_hint(pf->msix_entries[vector].vector,
3330 NULL);
3331 free_irq(pf->msix_entries[vector].vector,
Alexander Duyck493fb302013-09-28 07:01:44 +00003332 vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003333
3334 /* Tear down the interrupt queue link list
3335 *
3336 * We know that they come in pairs and always
3337 * the Rx first, then the Tx. To clear the
3338 * link list, stick the EOL value into the
3339 * next_q field of the registers.
3340 */
3341 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
3342 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3343 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3344 val |= I40E_QUEUE_END_OF_LIST
3345 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3346 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
3347
3348 while (qp != I40E_QUEUE_END_OF_LIST) {
3349 u32 next;
3350
3351 val = rd32(hw, I40E_QINT_RQCTL(qp));
3352
3353 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3354 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3355 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3356 I40E_QINT_RQCTL_INTEVENT_MASK);
3357
3358 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3359 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3360
3361 wr32(hw, I40E_QINT_RQCTL(qp), val);
3362
3363 val = rd32(hw, I40E_QINT_TQCTL(qp));
3364
3365 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
3366 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
3367
3368 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3369 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3370 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3371 I40E_QINT_TQCTL_INTEVENT_MASK);
3372
3373 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3374 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3375
3376 wr32(hw, I40E_QINT_TQCTL(qp), val);
3377 qp = next;
3378 }
3379 }
3380 } else {
3381 free_irq(pf->pdev->irq, pf);
3382
3383 val = rd32(hw, I40E_PFINT_LNKLST0);
3384 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3385 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3386 val |= I40E_QUEUE_END_OF_LIST
3387 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
3388 wr32(hw, I40E_PFINT_LNKLST0, val);
3389
3390 val = rd32(hw, I40E_QINT_RQCTL(qp));
3391 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3392 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3393 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3394 I40E_QINT_RQCTL_INTEVENT_MASK);
3395
3396 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3397 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3398
3399 wr32(hw, I40E_QINT_RQCTL(qp), val);
3400
3401 val = rd32(hw, I40E_QINT_TQCTL(qp));
3402
3403 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3404 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3405 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3406 I40E_QINT_TQCTL_INTEVENT_MASK);
3407
3408 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3409 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3410
3411 wr32(hw, I40E_QINT_TQCTL(qp), val);
3412 }
3413}
3414
3415/**
Alexander Duyck493fb302013-09-28 07:01:44 +00003416 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
3417 * @vsi: the VSI being configured
3418 * @v_idx: Index of vector to be freed
3419 *
3420 * This function frees the memory allocated to the q_vector. In addition if
3421 * NAPI is enabled it will delete any references to the NAPI struct prior
3422 * to freeing the q_vector.
3423 **/
3424static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
3425{
3426 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003427 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00003428
3429 if (!q_vector)
3430 return;
3431
3432 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003433 i40e_for_each_ring(ring, q_vector->tx)
3434 ring->q_vector = NULL;
3435
3436 i40e_for_each_ring(ring, q_vector->rx)
3437 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00003438
3439 /* only VSI w/ an associated netdev is set up w/ NAPI */
3440 if (vsi->netdev)
3441 netif_napi_del(&q_vector->napi);
3442
3443 vsi->q_vectors[v_idx] = NULL;
3444
3445 kfree_rcu(q_vector, rcu);
3446}
3447
3448/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003449 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
3450 * @vsi: the VSI being un-configured
3451 *
3452 * This frees the memory allocated to the q_vectors and
3453 * deletes references to the NAPI struct.
3454 **/
3455static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
3456{
3457 int v_idx;
3458
Alexander Duyck493fb302013-09-28 07:01:44 +00003459 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
3460 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003461}
3462
3463/**
3464 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
3465 * @pf: board private structure
3466 **/
3467static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
3468{
3469 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
3470 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3471 pci_disable_msix(pf->pdev);
3472 kfree(pf->msix_entries);
3473 pf->msix_entries = NULL;
3474 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
3475 pci_disable_msi(pf->pdev);
3476 }
3477 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
3478}
3479
3480/**
3481 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
3482 * @pf: board private structure
3483 *
3484 * We go through and clear interrupt specific resources and reset the structure
3485 * to pre-load conditions
3486 **/
3487static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
3488{
3489 int i;
3490
3491 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
3492 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
3493 if (pf->vsi[i])
3494 i40e_vsi_free_q_vectors(pf->vsi[i]);
3495 i40e_reset_interrupt_capability(pf);
3496}
3497
3498/**
3499 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
3500 * @vsi: the VSI being configured
3501 **/
3502static void i40e_napi_enable_all(struct i40e_vsi *vsi)
3503{
3504 int q_idx;
3505
3506 if (!vsi->netdev)
3507 return;
3508
3509 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003510 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003511}
3512
3513/**
3514 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
3515 * @vsi: the VSI being configured
3516 **/
3517static void i40e_napi_disable_all(struct i40e_vsi *vsi)
3518{
3519 int q_idx;
3520
3521 if (!vsi->netdev)
3522 return;
3523
3524 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003525 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003526}
3527
3528/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00003529 * i40e_vsi_close - Shut down a VSI
3530 * @vsi: the vsi to be quelled
3531 **/
3532static void i40e_vsi_close(struct i40e_vsi *vsi)
3533{
3534 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
3535 i40e_down(vsi);
3536 i40e_vsi_free_irq(vsi);
3537 i40e_vsi_free_tx_resources(vsi);
3538 i40e_vsi_free_rx_resources(vsi);
3539}
3540
3541/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003542 * i40e_quiesce_vsi - Pause a given VSI
3543 * @vsi: the VSI being paused
3544 **/
3545static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
3546{
3547 if (test_bit(__I40E_DOWN, &vsi->state))
3548 return;
3549
3550 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
3551 if (vsi->netdev && netif_running(vsi->netdev)) {
3552 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
3553 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00003554 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003555 }
3556}
3557
3558/**
3559 * i40e_unquiesce_vsi - Resume a given VSI
3560 * @vsi: the VSI being resumed
3561 **/
3562static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
3563{
3564 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
3565 return;
3566
3567 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
3568 if (vsi->netdev && netif_running(vsi->netdev))
3569 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
3570 else
Shannon Nelson8276f752014-03-14 07:32:27 +00003571 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003572}
3573
3574/**
3575 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
3576 * @pf: the PF
3577 **/
3578static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
3579{
3580 int v;
3581
3582 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
3583 if (pf->vsi[v])
3584 i40e_quiesce_vsi(pf->vsi[v]);
3585 }
3586}
3587
3588/**
3589 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
3590 * @pf: the PF
3591 **/
3592static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
3593{
3594 int v;
3595
3596 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
3597 if (pf->vsi[v])
3598 i40e_unquiesce_vsi(pf->vsi[v]);
3599 }
3600}
3601
3602/**
3603 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
3604 * @dcbcfg: the corresponding DCBx configuration structure
3605 *
3606 * Return the number of TCs from given DCBx configuration
3607 **/
3608static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
3609{
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003610 u8 num_tc = 0;
3611 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003612
3613 /* Scan the ETS Config Priority Table to find
3614 * traffic class enabled for a given priority
3615 * and use the traffic class index to get the
3616 * number of traffic classes enabled
3617 */
3618 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3619 if (dcbcfg->etscfg.prioritytable[i] > num_tc)
3620 num_tc = dcbcfg->etscfg.prioritytable[i];
3621 }
3622
3623 /* Traffic class index starts from zero so
3624 * increment to return the actual count
3625 */
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003626 return num_tc + 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003627}
3628
3629/**
3630 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
3631 * @dcbcfg: the corresponding DCBx configuration structure
3632 *
3633 * Query the current DCB configuration and return the number of
3634 * traffic classes enabled from the given DCBX config
3635 **/
3636static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
3637{
3638 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
3639 u8 enabled_tc = 1;
3640 u8 i;
3641
3642 for (i = 0; i < num_tc; i++)
3643 enabled_tc |= 1 << i;
3644
3645 return enabled_tc;
3646}
3647
3648/**
3649 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
3650 * @pf: PF being queried
3651 *
3652 * Return number of traffic classes enabled for the given PF
3653 **/
3654static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
3655{
3656 struct i40e_hw *hw = &pf->hw;
3657 u8 i, enabled_tc;
3658 u8 num_tc = 0;
3659 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3660
3661 /* If DCB is not enabled then always in single TC */
3662 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3663 return 1;
3664
3665 /* MFP mode return count of enabled TCs for this PF */
3666 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
3667 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3668 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3669 if (enabled_tc & (1 << i))
3670 num_tc++;
3671 }
3672 return num_tc;
3673 }
3674
3675 /* SFP mode will be enabled for all TCs on port */
3676 return i40e_dcb_get_num_tc(dcbcfg);
3677}
3678
3679/**
3680 * i40e_pf_get_default_tc - Get bitmap for first enabled TC
3681 * @pf: PF being queried
3682 *
3683 * Return a bitmap for first enabled traffic class for this PF.
3684 **/
3685static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
3686{
3687 u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3688 u8 i = 0;
3689
3690 if (!enabled_tc)
3691 return 0x1; /* TC0 */
3692
3693 /* Find the first enabled TC */
3694 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3695 if (enabled_tc & (1 << i))
3696 break;
3697 }
3698
3699 return 1 << i;
3700}
3701
3702/**
3703 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
3704 * @pf: PF being queried
3705 *
3706 * Return a bitmap for enabled traffic classes for this PF.
3707 **/
3708static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
3709{
3710 /* If DCB is not enabled for this PF then just return default TC */
3711 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3712 return i40e_pf_get_default_tc(pf);
3713
3714 /* MFP mode will have enabled TCs set by FW */
3715 if (pf->flags & I40E_FLAG_MFP_ENABLED)
3716 return pf->hw.func_caps.enabled_tcmap;
3717
3718 /* SFP mode we want PF to be enabled for all TCs */
3719 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
3720}
3721
3722/**
3723 * i40e_vsi_get_bw_info - Query VSI BW Information
3724 * @vsi: the VSI being queried
3725 *
3726 * Returns 0 on success, negative value on failure
3727 **/
3728static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
3729{
3730 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
3731 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
3732 struct i40e_pf *pf = vsi->back;
3733 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003734 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003735 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003736 int i;
3737
3738 /* Get the VSI level BW configuration */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003739 aq_ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
3740 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003741 dev_info(&pf->pdev->dev,
3742 "couldn't get pf vsi bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003743 aq_ret, pf->hw.aq.asq_last_status);
3744 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003745 }
3746
3747 /* Get the VSI level BW configuration per TC */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003748 aq_ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
Jesse Brandeburg6838b532014-01-14 00:49:52 -08003749 NULL);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003750 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003751 dev_info(&pf->pdev->dev,
3752 "couldn't get pf vsi ets bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003753 aq_ret, pf->hw.aq.asq_last_status);
3754 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003755 }
3756
3757 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
3758 dev_info(&pf->pdev->dev,
3759 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
3760 bw_config.tc_valid_bits,
3761 bw_ets_config.tc_valid_bits);
3762 /* Still continuing */
3763 }
3764
3765 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
3766 vsi->bw_max_quanta = bw_config.max_bw;
3767 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
3768 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
3769 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3770 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
3771 vsi->bw_ets_limit_credits[i] =
3772 le16_to_cpu(bw_ets_config.credits[i]);
3773 /* 3 bits out of 4 for each TC */
3774 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
3775 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003776
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003777 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003778}
3779
3780/**
3781 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
3782 * @vsi: the VSI being configured
3783 * @enabled_tc: TC bitmap
3784 * @bw_credits: BW shared credits per TC
3785 *
3786 * Returns 0 on success, negative value on failure
3787 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003788static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003789 u8 *bw_share)
3790{
3791 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003792 i40e_status aq_ret;
3793 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003794
3795 bw_data.tc_valid_bits = enabled_tc;
3796 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3797 bw_data.tc_bw_credits[i] = bw_share[i];
3798
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003799 aq_ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
3800 NULL);
3801 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003802 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00003803 "AQ command Config VSI BW allocation per TC failed = %d\n",
3804 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003805 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003806 }
3807
3808 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3809 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
3810
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003811 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003812}
3813
3814/**
3815 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
3816 * @vsi: the VSI being configured
3817 * @enabled_tc: TC map to be enabled
3818 *
3819 **/
3820static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3821{
3822 struct net_device *netdev = vsi->netdev;
3823 struct i40e_pf *pf = vsi->back;
3824 struct i40e_hw *hw = &pf->hw;
3825 u8 netdev_tc = 0;
3826 int i;
3827 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3828
3829 if (!netdev)
3830 return;
3831
3832 if (!enabled_tc) {
3833 netdev_reset_tc(netdev);
3834 return;
3835 }
3836
3837 /* Set up actual enabled TCs on the VSI */
3838 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
3839 return;
3840
3841 /* set per TC queues for the VSI */
3842 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3843 /* Only set TC queues for enabled tcs
3844 *
3845 * e.g. For a VSI that has TC0 and TC3 enabled the
3846 * enabled_tc bitmap would be 0x00001001; the driver
3847 * will set the numtc for netdev as 2 that will be
3848 * referenced by the netdev layer as TC 0 and 1.
3849 */
3850 if (vsi->tc_config.enabled_tc & (1 << i))
3851 netdev_set_tc_queue(netdev,
3852 vsi->tc_config.tc_info[i].netdev_tc,
3853 vsi->tc_config.tc_info[i].qcount,
3854 vsi->tc_config.tc_info[i].qoffset);
3855 }
3856
3857 /* Assign UP2TC map for the VSI */
3858 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3859 /* Get the actual TC# for the UP */
3860 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
3861 /* Get the mapped netdev TC# for the UP */
3862 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
3863 netdev_set_prio_tc_map(netdev, i, netdev_tc);
3864 }
3865}
3866
3867/**
3868 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
3869 * @vsi: the VSI being configured
3870 * @ctxt: the ctxt buffer returned from AQ VSI update param command
3871 **/
3872static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
3873 struct i40e_vsi_context *ctxt)
3874{
3875 /* copy just the sections touched not the entire info
3876 * since not all sections are valid as returned by
3877 * update vsi params
3878 */
3879 vsi->info.mapping_flags = ctxt->info.mapping_flags;
3880 memcpy(&vsi->info.queue_mapping,
3881 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
3882 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
3883 sizeof(vsi->info.tc_mapping));
3884}
3885
3886/**
3887 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
3888 * @vsi: VSI to be configured
3889 * @enabled_tc: TC bitmap
3890 *
3891 * This configures a particular VSI for TCs that are mapped to the
3892 * given TC bitmap. It uses default bandwidth share for TCs across
3893 * VSIs to configure TC for a particular VSI.
3894 *
3895 * NOTE:
3896 * It is expected that the VSI queues have been quisced before calling
3897 * this function.
3898 **/
3899static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3900{
3901 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
3902 struct i40e_vsi_context ctxt;
3903 int ret = 0;
3904 int i;
3905
3906 /* Check if enabled_tc is same as existing or new TCs */
3907 if (vsi->tc_config.enabled_tc == enabled_tc)
3908 return ret;
3909
3910 /* Enable ETS TCs with equal BW Share for now across all VSIs */
3911 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3912 if (enabled_tc & (1 << i))
3913 bw_share[i] = 1;
3914 }
3915
3916 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
3917 if (ret) {
3918 dev_info(&vsi->back->pdev->dev,
3919 "Failed configuring TC map %d for VSI %d\n",
3920 enabled_tc, vsi->seid);
3921 goto out;
3922 }
3923
3924 /* Update Queue Pairs Mapping for currently enabled UPs */
3925 ctxt.seid = vsi->seid;
3926 ctxt.pf_num = vsi->back->hw.pf_id;
3927 ctxt.vf_num = 0;
3928 ctxt.uplink_seid = vsi->uplink_seid;
3929 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
3930 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
3931
3932 /* Update the VSI after updating the VSI queue-mapping information */
3933 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3934 if (ret) {
3935 dev_info(&vsi->back->pdev->dev,
3936 "update vsi failed, aq_err=%d\n",
3937 vsi->back->hw.aq.asq_last_status);
3938 goto out;
3939 }
3940 /* update the local VSI info with updated queue map */
3941 i40e_vsi_update_queue_map(vsi, &ctxt);
3942 vsi->info.valid_sections = 0;
3943
3944 /* Update current VSI BW information */
3945 ret = i40e_vsi_get_bw_info(vsi);
3946 if (ret) {
3947 dev_info(&vsi->back->pdev->dev,
3948 "Failed updating vsi bw info, aq_err=%d\n",
3949 vsi->back->hw.aq.asq_last_status);
3950 goto out;
3951 }
3952
3953 /* Update the netdev TC setup */
3954 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
3955out:
3956 return ret;
3957}
3958
3959/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08003960 * i40e_veb_config_tc - Configure TCs for given VEB
3961 * @veb: given VEB
3962 * @enabled_tc: TC bitmap
3963 *
3964 * Configures given TC bitmap for VEB (switching) element
3965 **/
3966int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
3967{
3968 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
3969 struct i40e_pf *pf = veb->pf;
3970 int ret = 0;
3971 int i;
3972
3973 /* No TCs or already enabled TCs just return */
3974 if (!enabled_tc || veb->enabled_tc == enabled_tc)
3975 return ret;
3976
3977 bw_data.tc_valid_bits = enabled_tc;
3978 /* bw_data.absolute_credits is not set (relative) */
3979
3980 /* Enable ETS TCs with equal BW Share for now */
3981 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3982 if (enabled_tc & (1 << i))
3983 bw_data.tc_bw_share_credits[i] = 1;
3984 }
3985
3986 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
3987 &bw_data, NULL);
3988 if (ret) {
3989 dev_info(&pf->pdev->dev,
3990 "veb bw config failed, aq_err=%d\n",
3991 pf->hw.aq.asq_last_status);
3992 goto out;
3993 }
3994
3995 /* Update the BW information */
3996 ret = i40e_veb_get_bw_info(veb);
3997 if (ret) {
3998 dev_info(&pf->pdev->dev,
3999 "Failed getting veb bw config, aq_err=%d\n",
4000 pf->hw.aq.asq_last_status);
4001 }
4002
4003out:
4004 return ret;
4005}
4006
4007#ifdef CONFIG_I40E_DCB
4008/**
4009 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
4010 * @pf: PF struct
4011 *
4012 * Reconfigure VEB/VSIs on a given PF; it is assumed that
4013 * the caller would've quiesce all the VSIs before calling
4014 * this function
4015 **/
4016static void i40e_dcb_reconfigure(struct i40e_pf *pf)
4017{
4018 u8 tc_map = 0;
4019 int ret;
4020 u8 v;
4021
4022 /* Enable the TCs available on PF to all VEBs */
4023 tc_map = i40e_pf_get_tc_map(pf);
4024 for (v = 0; v < I40E_MAX_VEB; v++) {
4025 if (!pf->veb[v])
4026 continue;
4027 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
4028 if (ret) {
4029 dev_info(&pf->pdev->dev,
4030 "Failed configuring TC for VEB seid=%d\n",
4031 pf->veb[v]->seid);
4032 /* Will try to configure as many components */
4033 }
4034 }
4035
4036 /* Update each VSI */
4037 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4038 if (!pf->vsi[v])
4039 continue;
4040
4041 /* - Enable all TCs for the LAN VSI
4042 * - For all others keep them at TC0 for now
4043 */
4044 if (v == pf->lan_vsi)
4045 tc_map = i40e_pf_get_tc_map(pf);
4046 else
4047 tc_map = i40e_pf_get_default_tc(pf);
4048
4049 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
4050 if (ret) {
4051 dev_info(&pf->pdev->dev,
4052 "Failed configuring TC for VSI seid=%d\n",
4053 pf->vsi[v]->seid);
4054 /* Will try to configure as many components */
4055 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00004056 /* Re-configure VSI vectors based on updated TC map */
4057 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004058 if (pf->vsi[v]->netdev)
4059 i40e_dcbnl_set_all(pf->vsi[v]);
4060 }
4061 }
4062}
4063
4064/**
4065 * i40e_init_pf_dcb - Initialize DCB configuration
4066 * @pf: PF being configured
4067 *
4068 * Query the current DCB configuration and cache it
4069 * in the hardware structure
4070 **/
4071static int i40e_init_pf_dcb(struct i40e_pf *pf)
4072{
4073 struct i40e_hw *hw = &pf->hw;
4074 int err = 0;
4075
4076 if (pf->hw.func_caps.npar_enable)
4077 goto out;
4078
4079 /* Get the initial DCB configuration */
4080 err = i40e_init_dcb(hw);
4081 if (!err) {
4082 /* Device/Function is not DCBX capable */
4083 if ((!hw->func_caps.dcb) ||
4084 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
4085 dev_info(&pf->pdev->dev,
4086 "DCBX offload is not supported or is disabled for this PF.\n");
4087
4088 if (pf->flags & I40E_FLAG_MFP_ENABLED)
4089 goto out;
4090
4091 } else {
4092 /* When status is not DISABLED then DCBX in FW */
4093 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
4094 DCB_CAP_DCBX_VER_IEEE;
4095 pf->flags |= I40E_FLAG_DCB_ENABLED;
4096 }
Neerav Parikh014269f2014-04-01 07:11:48 +00004097 } else {
4098 dev_info(&pf->pdev->dev, "AQ Querying DCB configuration failed: %d\n",
4099 pf->hw.aq.asq_last_status);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004100 }
4101
4102out:
4103 return err;
4104}
4105#endif /* CONFIG_I40E_DCB */
4106
4107/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004108 * i40e_up_complete - Finish the last steps of bringing up a connection
4109 * @vsi: the VSI being configured
4110 **/
4111static int i40e_up_complete(struct i40e_vsi *vsi)
4112{
4113 struct i40e_pf *pf = vsi->back;
4114 int err;
4115
4116 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4117 i40e_vsi_configure_msix(vsi);
4118 else
4119 i40e_configure_msi_and_legacy(vsi);
4120
4121 /* start rings */
4122 err = i40e_vsi_control_rings(vsi, true);
4123 if (err)
4124 return err;
4125
4126 clear_bit(__I40E_DOWN, &vsi->state);
4127 i40e_napi_enable_all(vsi);
4128 i40e_vsi_enable_irq(vsi);
4129
4130 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
4131 (vsi->netdev)) {
Anjali Singhai6d779b42013-09-28 06:00:02 +00004132 netdev_info(vsi->netdev, "NIC Link is Up\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004133 netif_tx_start_all_queues(vsi->netdev);
4134 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00004135 } else if (vsi->netdev) {
4136 netdev_info(vsi->netdev, "NIC Link is Down\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004137 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00004138
4139 /* replay FDIR SB filters */
4140 if (vsi->type == I40E_VSI_FDIR)
4141 i40e_fdir_filter_restore(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004142 i40e_service_event_schedule(pf);
4143
4144 return 0;
4145}
4146
4147/**
4148 * i40e_vsi_reinit_locked - Reset the VSI
4149 * @vsi: the VSI being configured
4150 *
4151 * Rebuild the ring structs after some configuration
4152 * has changed, e.g. MTU size.
4153 **/
4154static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
4155{
4156 struct i40e_pf *pf = vsi->back;
4157
4158 WARN_ON(in_interrupt());
4159 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
4160 usleep_range(1000, 2000);
4161 i40e_down(vsi);
4162
4163 /* Give a VF some time to respond to the reset. The
4164 * two second wait is based upon the watchdog cycle in
4165 * the VF driver.
4166 */
4167 if (vsi->type == I40E_VSI_SRIOV)
4168 msleep(2000);
4169 i40e_up(vsi);
4170 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
4171}
4172
4173/**
4174 * i40e_up - Bring the connection back up after being down
4175 * @vsi: the VSI being configured
4176 **/
4177int i40e_up(struct i40e_vsi *vsi)
4178{
4179 int err;
4180
4181 err = i40e_vsi_configure(vsi);
4182 if (!err)
4183 err = i40e_up_complete(vsi);
4184
4185 return err;
4186}
4187
4188/**
4189 * i40e_down - Shutdown the connection processing
4190 * @vsi: the VSI being stopped
4191 **/
4192void i40e_down(struct i40e_vsi *vsi)
4193{
4194 int i;
4195
4196 /* It is assumed that the caller of this function
4197 * sets the vsi->state __I40E_DOWN bit.
4198 */
4199 if (vsi->netdev) {
4200 netif_carrier_off(vsi->netdev);
4201 netif_tx_disable(vsi->netdev);
4202 }
4203 i40e_vsi_disable_irq(vsi);
4204 i40e_vsi_control_rings(vsi, false);
4205 i40e_napi_disable_all(vsi);
4206
4207 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004208 i40e_clean_tx_ring(vsi->tx_rings[i]);
4209 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004210 }
4211}
4212
4213/**
4214 * i40e_setup_tc - configure multiple traffic classes
4215 * @netdev: net device to configure
4216 * @tc: number of traffic classes to enable
4217 **/
4218static int i40e_setup_tc(struct net_device *netdev, u8 tc)
4219{
4220 struct i40e_netdev_priv *np = netdev_priv(netdev);
4221 struct i40e_vsi *vsi = np->vsi;
4222 struct i40e_pf *pf = vsi->back;
4223 u8 enabled_tc = 0;
4224 int ret = -EINVAL;
4225 int i;
4226
4227 /* Check if DCB enabled to continue */
4228 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
4229 netdev_info(netdev, "DCB is not enabled for adapter\n");
4230 goto exit;
4231 }
4232
4233 /* Check if MFP enabled */
4234 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
4235 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
4236 goto exit;
4237 }
4238
4239 /* Check whether tc count is within enabled limit */
4240 if (tc > i40e_pf_get_num_tc(pf)) {
4241 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
4242 goto exit;
4243 }
4244
4245 /* Generate TC map for number of tc requested */
4246 for (i = 0; i < tc; i++)
4247 enabled_tc |= (1 << i);
4248
4249 /* Requesting same TC configuration as already enabled */
4250 if (enabled_tc == vsi->tc_config.enabled_tc)
4251 return 0;
4252
4253 /* Quiesce VSI queues */
4254 i40e_quiesce_vsi(vsi);
4255
4256 /* Configure VSI for enabled TCs */
4257 ret = i40e_vsi_config_tc(vsi, enabled_tc);
4258 if (ret) {
4259 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
4260 vsi->seid);
4261 goto exit;
4262 }
4263
4264 /* Unquiesce VSI */
4265 i40e_unquiesce_vsi(vsi);
4266
4267exit:
4268 return ret;
4269}
4270
4271/**
4272 * i40e_open - Called when a network interface is made active
4273 * @netdev: network interface device structure
4274 *
4275 * The open entry point is called when a network interface is made
4276 * active by the system (IFF_UP). At this point all resources needed
4277 * for transmit and receive operations are allocated, the interrupt
4278 * handler is registered with the OS, the netdev watchdog subtask is
4279 * enabled, and the stack is notified that the interface is ready.
4280 *
4281 * Returns 0 on success, negative value on failure
4282 **/
4283static int i40e_open(struct net_device *netdev)
4284{
4285 struct i40e_netdev_priv *np = netdev_priv(netdev);
4286 struct i40e_vsi *vsi = np->vsi;
4287 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004288 int err;
4289
Shannon Nelson4eb3f762014-03-06 08:59:58 +00004290 /* disallow open during test or if eeprom is broken */
4291 if (test_bit(__I40E_TESTING, &pf->state) ||
4292 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004293 return -EBUSY;
4294
4295 netif_carrier_off(netdev);
4296
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004297 err = i40e_vsi_open(vsi);
4298 if (err)
4299 return err;
4300
Jesse Brandeburg059dab62014-04-01 09:07:20 +00004301 /* configure global TSO hardware offload settings */
4302 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
4303 TCP_FLAG_FIN) >> 16);
4304 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
4305 TCP_FLAG_FIN |
4306 TCP_FLAG_CWR) >> 16);
4307 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
4308
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004309#ifdef CONFIG_I40E_VXLAN
4310 vxlan_get_rx_port(netdev);
4311#endif
4312
4313 return 0;
4314}
4315
4316/**
4317 * i40e_vsi_open -
4318 * @vsi: the VSI to open
4319 *
4320 * Finish initialization of the VSI.
4321 *
4322 * Returns 0 on success, negative value on failure
4323 **/
4324int i40e_vsi_open(struct i40e_vsi *vsi)
4325{
4326 struct i40e_pf *pf = vsi->back;
4327 char int_name[IFNAMSIZ];
4328 int err;
4329
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004330 /* allocate descriptors */
4331 err = i40e_vsi_setup_tx_resources(vsi);
4332 if (err)
4333 goto err_setup_tx;
4334 err = i40e_vsi_setup_rx_resources(vsi);
4335 if (err)
4336 goto err_setup_rx;
4337
4338 err = i40e_vsi_configure(vsi);
4339 if (err)
4340 goto err_setup_rx;
4341
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00004342 if (vsi->netdev) {
4343 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
4344 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
4345 err = i40e_vsi_request_irq(vsi, int_name);
4346 if (err)
4347 goto err_setup_rx;
4348
4349 /* Notify the stack of the actual queue counts. */
4350 err = netif_set_real_num_tx_queues(vsi->netdev,
4351 vsi->num_queue_pairs);
4352 if (err)
4353 goto err_set_queues;
4354
4355 err = netif_set_real_num_rx_queues(vsi->netdev,
4356 vsi->num_queue_pairs);
4357 if (err)
4358 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00004359
4360 } else if (vsi->type == I40E_VSI_FDIR) {
4361 snprintf(int_name, sizeof(int_name) - 1, "%s-fdir",
4362 dev_driver_string(&pf->pdev->dev));
4363 err = i40e_vsi_request_irq(vsi, int_name);
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00004364 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00004365 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004366 goto err_setup_rx;
4367 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004368
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004369 err = i40e_up_complete(vsi);
4370 if (err)
4371 goto err_up_complete;
4372
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004373 return 0;
4374
4375err_up_complete:
4376 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004377err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004378 i40e_vsi_free_irq(vsi);
4379err_setup_rx:
4380 i40e_vsi_free_rx_resources(vsi);
4381err_setup_tx:
4382 i40e_vsi_free_tx_resources(vsi);
4383 if (vsi == pf->vsi[pf->lan_vsi])
4384 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
4385
4386 return err;
4387}
4388
4389/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00004390 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
4391 * @pf: Pointer to pf
4392 *
4393 * This function destroys the hlist where all the Flow Director
4394 * filters were saved.
4395 **/
4396static void i40e_fdir_filter_exit(struct i40e_pf *pf)
4397{
4398 struct i40e_fdir_filter *filter;
4399 struct hlist_node *node2;
4400
4401 hlist_for_each_entry_safe(filter, node2,
4402 &pf->fdir_filter_list, fdir_node) {
4403 hlist_del(&filter->fdir_node);
4404 kfree(filter);
4405 }
4406 pf->fdir_pf_active_filters = 0;
4407}
4408
4409/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004410 * i40e_close - Disables a network interface
4411 * @netdev: network interface device structure
4412 *
4413 * The close entry point is called when an interface is de-activated
4414 * by the OS. The hardware is still under the driver's control, but
4415 * this netdev interface is disabled.
4416 *
4417 * Returns 0, this is not allowed to fail
4418 **/
4419static int i40e_close(struct net_device *netdev)
4420{
4421 struct i40e_netdev_priv *np = netdev_priv(netdev);
4422 struct i40e_vsi *vsi = np->vsi;
4423
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004424 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004425
4426 return 0;
4427}
4428
4429/**
4430 * i40e_do_reset - Start a PF or Core Reset sequence
4431 * @pf: board private structure
4432 * @reset_flags: which reset is requested
4433 *
4434 * The essential difference in resets is that the PF Reset
4435 * doesn't clear the packet buffers, doesn't reset the PE
4436 * firmware, and doesn't bother the other PFs on the chip.
4437 **/
4438void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
4439{
4440 u32 val;
4441
4442 WARN_ON(in_interrupt());
4443
Mitch Williams263fc482014-04-23 04:50:11 +00004444 if (i40e_check_asq_alive(&pf->hw))
4445 i40e_vc_notify_reset(pf);
4446
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004447 /* do the biggest reset indicated */
4448 if (reset_flags & (1 << __I40E_GLOBAL_RESET_REQUESTED)) {
4449
4450 /* Request a Global Reset
4451 *
4452 * This will start the chip's countdown to the actual full
4453 * chip reset event, and a warning interrupt to be sent
4454 * to all PFs, including the requestor. Our handler
4455 * for the warning interrupt will deal with the shutdown
4456 * and recovery of the switch setup.
4457 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004458 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004459 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4460 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
4461 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4462
4463 } else if (reset_flags & (1 << __I40E_CORE_RESET_REQUESTED)) {
4464
4465 /* Request a Core Reset
4466 *
4467 * Same as Global Reset, except does *not* include the MAC/PHY
4468 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004469 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004470 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4471 val |= I40E_GLGEN_RTRIG_CORER_MASK;
4472 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4473 i40e_flush(&pf->hw);
4474
Shannon Nelson7823fe32013-11-16 10:00:45 +00004475 } else if (reset_flags & (1 << __I40E_EMP_RESET_REQUESTED)) {
4476
4477 /* Request a Firmware Reset
4478 *
4479 * Same as Global reset, plus restarting the
4480 * embedded firmware engine.
4481 */
4482 /* enable EMP Reset */
4483 val = rd32(&pf->hw, I40E_GLGEN_RSTENA_EMP);
4484 val |= I40E_GLGEN_RSTENA_EMP_EMP_RST_ENA_MASK;
4485 wr32(&pf->hw, I40E_GLGEN_RSTENA_EMP, val);
4486
4487 /* force the reset */
4488 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4489 val |= I40E_GLGEN_RTRIG_EMPFWR_MASK;
4490 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4491 i40e_flush(&pf->hw);
4492
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004493 } else if (reset_flags & (1 << __I40E_PF_RESET_REQUESTED)) {
4494
4495 /* Request a PF Reset
4496 *
4497 * Resets only the PF-specific registers
4498 *
4499 * This goes directly to the tear-down and rebuild of
4500 * the switch, since we need to do all the recovery as
4501 * for the Core Reset.
4502 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004503 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004504 i40e_handle_reset_warning(pf);
4505
4506 } else if (reset_flags & (1 << __I40E_REINIT_REQUESTED)) {
4507 int v;
4508
4509 /* Find the VSI(s) that requested a re-init */
4510 dev_info(&pf->pdev->dev,
4511 "VSI reinit requested\n");
4512 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4513 struct i40e_vsi *vsi = pf->vsi[v];
4514 if (vsi != NULL &&
4515 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
4516 i40e_vsi_reinit_locked(pf->vsi[v]);
4517 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
4518 }
4519 }
4520
4521 /* no further action needed, so return now */
4522 return;
4523 } else {
4524 dev_info(&pf->pdev->dev,
4525 "bad reset request 0x%08x\n", reset_flags);
4526 return;
4527 }
4528}
4529
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004530#ifdef CONFIG_I40E_DCB
4531/**
4532 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
4533 * @pf: board private structure
4534 * @old_cfg: current DCB config
4535 * @new_cfg: new DCB config
4536 **/
4537bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
4538 struct i40e_dcbx_config *old_cfg,
4539 struct i40e_dcbx_config *new_cfg)
4540{
4541 bool need_reconfig = false;
4542
4543 /* Check if ETS configuration has changed */
4544 if (memcmp(&new_cfg->etscfg,
4545 &old_cfg->etscfg,
4546 sizeof(new_cfg->etscfg))) {
4547 /* If Priority Table has changed reconfig is needed */
4548 if (memcmp(&new_cfg->etscfg.prioritytable,
4549 &old_cfg->etscfg.prioritytable,
4550 sizeof(new_cfg->etscfg.prioritytable))) {
4551 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004552 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004553 }
4554
4555 if (memcmp(&new_cfg->etscfg.tcbwtable,
4556 &old_cfg->etscfg.tcbwtable,
4557 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004558 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004559
4560 if (memcmp(&new_cfg->etscfg.tsatable,
4561 &old_cfg->etscfg.tsatable,
4562 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004563 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004564 }
4565
4566 /* Check if PFC configuration has changed */
4567 if (memcmp(&new_cfg->pfc,
4568 &old_cfg->pfc,
4569 sizeof(new_cfg->pfc))) {
4570 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004571 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004572 }
4573
4574 /* Check if APP Table has changed */
4575 if (memcmp(&new_cfg->app,
4576 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05004577 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004578 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004579 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05004580 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004581
4582 return need_reconfig;
4583}
4584
4585/**
4586 * i40e_handle_lldp_event - Handle LLDP Change MIB event
4587 * @pf: board private structure
4588 * @e: event info posted on ARQ
4589 **/
4590static int i40e_handle_lldp_event(struct i40e_pf *pf,
4591 struct i40e_arq_event_info *e)
4592{
4593 struct i40e_aqc_lldp_get_mib *mib =
4594 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
4595 struct i40e_hw *hw = &pf->hw;
4596 struct i40e_dcbx_config *dcbx_cfg = &hw->local_dcbx_config;
4597 struct i40e_dcbx_config tmp_dcbx_cfg;
4598 bool need_reconfig = false;
4599 int ret = 0;
4600 u8 type;
4601
4602 /* Ignore if event is not for Nearest Bridge */
4603 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
4604 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
4605 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
4606 return ret;
4607
4608 /* Check MIB Type and return if event for Remote MIB update */
4609 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
4610 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
4611 /* Update the remote cached instance and return */
4612 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
4613 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
4614 &hw->remote_dcbx_config);
4615 goto exit;
4616 }
4617
4618 /* Convert/store the DCBX data from LLDPDU temporarily */
4619 memset(&tmp_dcbx_cfg, 0, sizeof(tmp_dcbx_cfg));
4620 ret = i40e_lldp_to_dcb_config(e->msg_buf, &tmp_dcbx_cfg);
4621 if (ret) {
4622 /* Error in LLDPDU parsing return */
4623 dev_info(&pf->pdev->dev, "Failed parsing LLDPDU from event buffer\n");
4624 goto exit;
4625 }
4626
4627 /* No change detected in DCBX configs */
4628 if (!memcmp(&tmp_dcbx_cfg, dcbx_cfg, sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004629 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004630 goto exit;
4631 }
4632
4633 need_reconfig = i40e_dcb_need_reconfig(pf, dcbx_cfg, &tmp_dcbx_cfg);
4634
4635 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg);
4636
4637 /* Overwrite the new configuration */
4638 *dcbx_cfg = tmp_dcbx_cfg;
4639
4640 if (!need_reconfig)
4641 goto exit;
4642
4643 /* Reconfiguration needed quiesce all VSIs */
4644 i40e_pf_quiesce_all_vsi(pf);
4645
4646 /* Changes in configuration update VEB/VSI */
4647 i40e_dcb_reconfigure(pf);
4648
4649 i40e_pf_unquiesce_all_vsi(pf);
4650exit:
4651 return ret;
4652}
4653#endif /* CONFIG_I40E_DCB */
4654
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004655/**
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004656 * i40e_do_reset_safe - Protected reset path for userland calls.
4657 * @pf: board private structure
4658 * @reset_flags: which reset is requested
4659 *
4660 **/
4661void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
4662{
4663 rtnl_lock();
4664 i40e_do_reset(pf, reset_flags);
4665 rtnl_unlock();
4666}
4667
4668/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004669 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
4670 * @pf: board private structure
4671 * @e: event info posted on ARQ
4672 *
4673 * Handler for LAN Queue Overflow Event generated by the firmware for PF
4674 * and VF queues
4675 **/
4676static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
4677 struct i40e_arq_event_info *e)
4678{
4679 struct i40e_aqc_lan_overflow *data =
4680 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
4681 u32 queue = le32_to_cpu(data->prtdcb_rupto);
4682 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
4683 struct i40e_hw *hw = &pf->hw;
4684 struct i40e_vf *vf;
4685 u16 vf_id;
4686
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004687 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
4688 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004689
4690 /* Queue belongs to VF, find the VF and issue VF reset */
4691 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
4692 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
4693 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
4694 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
4695 vf_id -= hw->func_caps.vf_base_id;
4696 vf = &pf->vf[vf_id];
4697 i40e_vc_notify_vf_reset(vf);
4698 /* Allow VF to process pending reset notification */
4699 msleep(20);
4700 i40e_reset_vf(vf, false);
4701 }
4702}
4703
4704/**
4705 * i40e_service_event_complete - Finish up the service event
4706 * @pf: board private structure
4707 **/
4708static void i40e_service_event_complete(struct i40e_pf *pf)
4709{
4710 BUG_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
4711
4712 /* flush memory to make sure state is correct before next watchog */
4713 smp_mb__before_clear_bit();
4714 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
4715}
4716
4717/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004718 * i40e_get_current_fd_count - Get the count of FD filters programmed in the HW
4719 * @pf: board private structure
4720 **/
4721int i40e_get_current_fd_count(struct i40e_pf *pf)
4722{
4723 int val, fcnt_prog;
4724 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
4725 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
4726 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
4727 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
4728 return fcnt_prog;
4729}
4730
4731/**
4732 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
4733 * @pf: board private structure
4734 **/
4735void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
4736{
4737 u32 fcnt_prog, fcnt_avail;
4738
4739 /* Check if, FD SB or ATR was auto disabled and if there is enough room
4740 * to re-enable
4741 */
4742 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4743 (pf->flags & I40E_FLAG_FD_SB_ENABLED))
4744 return;
4745 fcnt_prog = i40e_get_current_fd_count(pf);
Anjali Singhai Jain89132782014-04-09 05:59:01 +00004746 fcnt_avail = i40e_get_fd_cnt_all(pf);
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004747 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) {
4748 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
4749 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
4750 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
4751 dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
4752 }
4753 }
4754 /* Wait for some more space to be available to turn on ATR */
4755 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
4756 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4757 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
4758 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
4759 dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table now\n");
4760 }
4761 }
4762}
4763
4764/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004765 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
4766 * @pf: board private structure
4767 **/
4768static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
4769{
4770 if (!(pf->flags & I40E_FLAG_FDIR_REQUIRES_REINIT))
4771 return;
4772
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004773 /* if interface is down do nothing */
4774 if (test_bit(__I40E_DOWN, &pf->state))
4775 return;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004776 i40e_fdir_check_and_reenable(pf);
4777
4778 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4779 (pf->flags & I40E_FLAG_FD_SB_ENABLED))
4780 pf->flags &= ~I40E_FLAG_FDIR_REQUIRES_REINIT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004781}
4782
4783/**
4784 * i40e_vsi_link_event - notify VSI of a link event
4785 * @vsi: vsi to be notified
4786 * @link_up: link up or down
4787 **/
4788static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
4789{
4790 if (!vsi)
4791 return;
4792
4793 switch (vsi->type) {
4794 case I40E_VSI_MAIN:
4795 if (!vsi->netdev || !vsi->netdev_registered)
4796 break;
4797
4798 if (link_up) {
4799 netif_carrier_on(vsi->netdev);
4800 netif_tx_wake_all_queues(vsi->netdev);
4801 } else {
4802 netif_carrier_off(vsi->netdev);
4803 netif_tx_stop_all_queues(vsi->netdev);
4804 }
4805 break;
4806
4807 case I40E_VSI_SRIOV:
4808 break;
4809
4810 case I40E_VSI_VMDQ2:
4811 case I40E_VSI_CTRL:
4812 case I40E_VSI_MIRROR:
4813 default:
4814 /* there is no notification for other VSIs */
4815 break;
4816 }
4817}
4818
4819/**
4820 * i40e_veb_link_event - notify elements on the veb of a link event
4821 * @veb: veb to be notified
4822 * @link_up: link up or down
4823 **/
4824static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
4825{
4826 struct i40e_pf *pf;
4827 int i;
4828
4829 if (!veb || !veb->pf)
4830 return;
4831 pf = veb->pf;
4832
4833 /* depth first... */
4834 for (i = 0; i < I40E_MAX_VEB; i++)
4835 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
4836 i40e_veb_link_event(pf->veb[i], link_up);
4837
4838 /* ... now the local VSIs */
4839 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
4840 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
4841 i40e_vsi_link_event(pf->vsi[i], link_up);
4842}
4843
4844/**
4845 * i40e_link_event - Update netif_carrier status
4846 * @pf: board private structure
4847 **/
4848static void i40e_link_event(struct i40e_pf *pf)
4849{
4850 bool new_link, old_link;
4851
4852 new_link = (pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP);
4853 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
4854
4855 if (new_link == old_link)
4856 return;
4857
Anjali Singhai6d779b42013-09-28 06:00:02 +00004858 if (!test_bit(__I40E_DOWN, &pf->vsi[pf->lan_vsi]->state))
4859 netdev_info(pf->vsi[pf->lan_vsi]->netdev,
4860 "NIC Link is %s\n", (new_link ? "Up" : "Down"));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004861
4862 /* Notify the base of the switch tree connected to
4863 * the link. Floating VEBs are not notified.
4864 */
4865 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
4866 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
4867 else
4868 i40e_vsi_link_event(pf->vsi[pf->lan_vsi], new_link);
4869
4870 if (pf->vf)
4871 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00004872
4873 if (pf->flags & I40E_FLAG_PTP)
4874 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004875}
4876
4877/**
4878 * i40e_check_hang_subtask - Check for hung queues and dropped interrupts
4879 * @pf: board private structure
4880 *
4881 * Set the per-queue flags to request a check for stuck queues in the irq
4882 * clean functions, then force interrupts to be sure the irq clean is called.
4883 **/
4884static void i40e_check_hang_subtask(struct i40e_pf *pf)
4885{
4886 int i, v;
4887
4888 /* If we're down or resetting, just bail */
4889 if (test_bit(__I40E_CONFIG_BUSY, &pf->state))
4890 return;
4891
4892 /* for each VSI/netdev
4893 * for each Tx queue
4894 * set the check flag
4895 * for each q_vector
4896 * force an interrupt
4897 */
4898 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4899 struct i40e_vsi *vsi = pf->vsi[v];
4900 int armed = 0;
4901
4902 if (!pf->vsi[v] ||
4903 test_bit(__I40E_DOWN, &vsi->state) ||
4904 (vsi->netdev && !netif_carrier_ok(vsi->netdev)))
4905 continue;
4906
4907 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004908 set_check_for_tx_hang(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004909 if (test_bit(__I40E_HANG_CHECK_ARMED,
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004910 &vsi->tx_rings[i]->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004911 armed++;
4912 }
4913
4914 if (armed) {
4915 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
4916 wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0,
4917 (I40E_PFINT_DYN_CTL0_INTENA_MASK |
4918 I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK));
4919 } else {
4920 u16 vec = vsi->base_vector - 1;
4921 u32 val = (I40E_PFINT_DYN_CTLN_INTENA_MASK |
4922 I40E_PFINT_DYN_CTLN_SWINT_TRIG_MASK);
4923 for (i = 0; i < vsi->num_q_vectors; i++, vec++)
4924 wr32(&vsi->back->hw,
4925 I40E_PFINT_DYN_CTLN(vec), val);
4926 }
4927 i40e_flush(&vsi->back->hw);
4928 }
4929 }
4930}
4931
4932/**
4933 * i40e_watchdog_subtask - Check and bring link up
4934 * @pf: board private structure
4935 **/
4936static void i40e_watchdog_subtask(struct i40e_pf *pf)
4937{
4938 int i;
4939
4940 /* if interface is down do nothing */
4941 if (test_bit(__I40E_DOWN, &pf->state) ||
4942 test_bit(__I40E_CONFIG_BUSY, &pf->state))
4943 return;
4944
4945 /* Update the stats for active netdevs so the network stack
4946 * can look at updated numbers whenever it cares to
4947 */
4948 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
4949 if (pf->vsi[i] && pf->vsi[i]->netdev)
4950 i40e_update_stats(pf->vsi[i]);
4951
4952 /* Update the stats for the active switching components */
4953 for (i = 0; i < I40E_MAX_VEB; i++)
4954 if (pf->veb[i])
4955 i40e_update_veb_stats(pf->veb[i]);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00004956
4957 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004958}
4959
4960/**
4961 * i40e_reset_subtask - Set up for resetting the device and driver
4962 * @pf: board private structure
4963 **/
4964static void i40e_reset_subtask(struct i40e_pf *pf)
4965{
4966 u32 reset_flags = 0;
4967
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004968 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004969 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
4970 reset_flags |= (1 << __I40E_REINIT_REQUESTED);
4971 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
4972 }
4973 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
4974 reset_flags |= (1 << __I40E_PF_RESET_REQUESTED);
4975 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
4976 }
4977 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
4978 reset_flags |= (1 << __I40E_CORE_RESET_REQUESTED);
4979 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
4980 }
4981 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
4982 reset_flags |= (1 << __I40E_GLOBAL_RESET_REQUESTED);
4983 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
4984 }
4985
4986 /* If there's a recovery already waiting, it takes
4987 * precedence before starting a new reset sequence.
4988 */
4989 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
4990 i40e_handle_reset_warning(pf);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004991 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004992 }
4993
4994 /* If we're already down or resetting, just bail */
4995 if (reset_flags &&
4996 !test_bit(__I40E_DOWN, &pf->state) &&
4997 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
4998 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004999
5000unlock:
5001 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005002}
5003
5004/**
5005 * i40e_handle_link_event - Handle link event
5006 * @pf: board private structure
5007 * @e: event info posted on ARQ
5008 **/
5009static void i40e_handle_link_event(struct i40e_pf *pf,
5010 struct i40e_arq_event_info *e)
5011{
5012 struct i40e_hw *hw = &pf->hw;
5013 struct i40e_aqc_get_link_status *status =
5014 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
5015 struct i40e_link_status *hw_link_info = &hw->phy.link_info;
5016
5017 /* save off old link status information */
5018 memcpy(&pf->hw.phy.link_info_old, hw_link_info,
5019 sizeof(pf->hw.phy.link_info_old));
5020
5021 /* update link status */
5022 hw_link_info->phy_type = (enum i40e_aq_phy_type)status->phy_type;
5023 hw_link_info->link_speed = (enum i40e_aq_link_speed)status->link_speed;
5024 hw_link_info->link_info = status->link_info;
5025 hw_link_info->an_info = status->an_info;
5026 hw_link_info->ext_info = status->ext_info;
5027 hw_link_info->lse_enable =
5028 le16_to_cpu(status->command_flags) &
5029 I40E_AQ_LSE_ENABLE;
5030
5031 /* process the event */
5032 i40e_link_event(pf);
5033
5034 /* Do a new status request to re-enable LSE reporting
5035 * and load new status information into the hw struct,
5036 * then see if the status changed while processing the
5037 * initial event.
5038 */
5039 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
5040 i40e_link_event(pf);
5041}
5042
5043/**
5044 * i40e_clean_adminq_subtask - Clean the AdminQ rings
5045 * @pf: board private structure
5046 **/
5047static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
5048{
5049 struct i40e_arq_event_info event;
5050 struct i40e_hw *hw = &pf->hw;
5051 u16 pending, i = 0;
5052 i40e_status ret;
5053 u16 opcode;
5054 u32 val;
5055
5056 if (!test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state))
5057 return;
5058
Mitch Williams3197ce22013-11-28 06:39:39 +00005059 event.msg_size = I40E_MAX_AQ_BUF_SIZE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005060 event.msg_buf = kzalloc(event.msg_size, GFP_KERNEL);
5061 if (!event.msg_buf)
5062 return;
5063
5064 do {
Mitch Williams2f019122013-11-28 06:39:33 +00005065 event.msg_size = I40E_MAX_AQ_BUF_SIZE; /* reinit each time */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005066 ret = i40e_clean_arq_element(hw, &event, &pending);
5067 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
5068 dev_info(&pf->pdev->dev, "No ARQ event found\n");
5069 break;
5070 } else if (ret) {
5071 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
5072 break;
5073 }
5074
5075 opcode = le16_to_cpu(event.desc.opcode);
5076 switch (opcode) {
5077
5078 case i40e_aqc_opc_get_link_status:
5079 i40e_handle_link_event(pf, &event);
5080 break;
5081 case i40e_aqc_opc_send_msg_to_pf:
5082 ret = i40e_vc_process_vf_msg(pf,
5083 le16_to_cpu(event.desc.retval),
5084 le32_to_cpu(event.desc.cookie_high),
5085 le32_to_cpu(event.desc.cookie_low),
5086 event.msg_buf,
5087 event.msg_size);
5088 break;
5089 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005090 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005091#ifdef CONFIG_I40E_DCB
5092 rtnl_lock();
5093 ret = i40e_handle_lldp_event(pf, &event);
5094 rtnl_unlock();
5095#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005096 break;
5097 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005098 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005099 i40e_handle_lan_overflow_event(pf, &event);
5100 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00005101 case i40e_aqc_opc_send_msg_to_peer:
5102 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
5103 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005104 default:
5105 dev_info(&pf->pdev->dev,
Shannon Nelson0467bc92013-12-18 13:45:58 +00005106 "ARQ Error: Unknown event 0x%04x received\n",
5107 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005108 break;
5109 }
5110 } while (pending && (i++ < pf->adminq_work_limit));
5111
5112 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
5113 /* re-enable Admin queue interrupt cause */
5114 val = rd32(hw, I40E_PFINT_ICR0_ENA);
5115 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
5116 wr32(hw, I40E_PFINT_ICR0_ENA, val);
5117 i40e_flush(hw);
5118
5119 kfree(event.msg_buf);
5120}
5121
5122/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005123 * i40e_verify_eeprom - make sure eeprom is good to use
5124 * @pf: board private structure
5125 **/
5126static void i40e_verify_eeprom(struct i40e_pf *pf)
5127{
5128 int err;
5129
5130 err = i40e_diag_eeprom_test(&pf->hw);
5131 if (err) {
5132 /* retry in case of garbage read */
5133 err = i40e_diag_eeprom_test(&pf->hw);
5134 if (err) {
5135 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
5136 err);
5137 set_bit(__I40E_BAD_EEPROM, &pf->state);
5138 }
5139 }
5140
5141 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
5142 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
5143 clear_bit(__I40E_BAD_EEPROM, &pf->state);
5144 }
5145}
5146
5147/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005148 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
5149 * @veb: pointer to the VEB instance
5150 *
5151 * This is a recursive function that first builds the attached VSIs then
5152 * recurses in to build the next layer of VEB. We track the connections
5153 * through our own index numbers because the seid's from the HW could
5154 * change across the reset.
5155 **/
5156static int i40e_reconstitute_veb(struct i40e_veb *veb)
5157{
5158 struct i40e_vsi *ctl_vsi = NULL;
5159 struct i40e_pf *pf = veb->pf;
5160 int v, veb_idx;
5161 int ret;
5162
5163 /* build VSI that owns this VEB, temporarily attached to base VEB */
5164 for (v = 0; v < pf->hw.func_caps.num_vsis && !ctl_vsi; v++) {
5165 if (pf->vsi[v] &&
5166 pf->vsi[v]->veb_idx == veb->idx &&
5167 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
5168 ctl_vsi = pf->vsi[v];
5169 break;
5170 }
5171 }
5172 if (!ctl_vsi) {
5173 dev_info(&pf->pdev->dev,
5174 "missing owner VSI for veb_idx %d\n", veb->idx);
5175 ret = -ENOENT;
5176 goto end_reconstitute;
5177 }
5178 if (ctl_vsi != pf->vsi[pf->lan_vsi])
5179 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
5180 ret = i40e_add_vsi(ctl_vsi);
5181 if (ret) {
5182 dev_info(&pf->pdev->dev,
5183 "rebuild of owner VSI failed: %d\n", ret);
5184 goto end_reconstitute;
5185 }
5186 i40e_vsi_reset_stats(ctl_vsi);
5187
5188 /* create the VEB in the switch and move the VSI onto the VEB */
5189 ret = i40e_add_veb(veb, ctl_vsi);
5190 if (ret)
5191 goto end_reconstitute;
5192
5193 /* create the remaining VSIs attached to this VEB */
5194 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5195 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
5196 continue;
5197
5198 if (pf->vsi[v]->veb_idx == veb->idx) {
5199 struct i40e_vsi *vsi = pf->vsi[v];
5200 vsi->uplink_seid = veb->seid;
5201 ret = i40e_add_vsi(vsi);
5202 if (ret) {
5203 dev_info(&pf->pdev->dev,
5204 "rebuild of vsi_idx %d failed: %d\n",
5205 v, ret);
5206 goto end_reconstitute;
5207 }
5208 i40e_vsi_reset_stats(vsi);
5209 }
5210 }
5211
5212 /* create any VEBs attached to this VEB - RECURSION */
5213 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
5214 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
5215 pf->veb[veb_idx]->uplink_seid = veb->seid;
5216 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
5217 if (ret)
5218 break;
5219 }
5220 }
5221
5222end_reconstitute:
5223 return ret;
5224}
5225
5226/**
5227 * i40e_get_capabilities - get info about the HW
5228 * @pf: the PF struct
5229 **/
5230static int i40e_get_capabilities(struct i40e_pf *pf)
5231{
5232 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
5233 u16 data_size;
5234 int buf_len;
5235 int err;
5236
5237 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
5238 do {
5239 cap_buf = kzalloc(buf_len, GFP_KERNEL);
5240 if (!cap_buf)
5241 return -ENOMEM;
5242
5243 /* this loads the data into the hw struct for us */
5244 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
5245 &data_size,
5246 i40e_aqc_opc_list_func_capabilities,
5247 NULL);
5248 /* data loaded, buffer no longer needed */
5249 kfree(cap_buf);
5250
5251 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
5252 /* retry with a larger buffer */
5253 buf_len = data_size;
5254 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
5255 dev_info(&pf->pdev->dev,
5256 "capability discovery failed: aq=%d\n",
5257 pf->hw.aq.asq_last_status);
5258 return -ENODEV;
5259 }
5260 } while (err);
5261
Anjali Singhai Jainac71b7b2014-02-06 05:51:08 +00005262 if (((pf->hw.aq.fw_maj_ver == 2) && (pf->hw.aq.fw_min_ver < 22)) ||
5263 (pf->hw.aq.fw_maj_ver < 2)) {
5264 pf->hw.func_caps.num_msix_vectors++;
5265 pf->hw.func_caps.num_msix_vectors_vf++;
5266 }
5267
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005268 if (pf->hw.debug_mask & I40E_DEBUG_USER)
5269 dev_info(&pf->pdev->dev,
5270 "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",
5271 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
5272 pf->hw.func_caps.num_msix_vectors,
5273 pf->hw.func_caps.num_msix_vectors_vf,
5274 pf->hw.func_caps.fd_filters_guaranteed,
5275 pf->hw.func_caps.fd_filters_best_effort,
5276 pf->hw.func_caps.num_tx_qp,
5277 pf->hw.func_caps.num_vsis);
5278
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00005279#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
5280 + pf->hw.func_caps.num_vfs)
5281 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
5282 dev_info(&pf->pdev->dev,
5283 "got num_vsis %d, setting num_vsis to %d\n",
5284 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
5285 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
5286 }
5287
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005288 return 0;
5289}
5290
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005291static int i40e_vsi_clear(struct i40e_vsi *vsi);
5292
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005293/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005294 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005295 * @pf: board private structure
5296 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005297static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005298{
5299 struct i40e_vsi *vsi;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005300 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005301
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005302 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005303 return;
5304
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005305 /* find existing VSI and see if it needs configuring */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005306 vsi = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005307 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
5308 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005309 vsi = pf->vsi[i];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005310 break;
5311 }
5312 }
5313
5314 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005315 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005316 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
5317 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005318 if (!vsi) {
5319 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005320 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
5321 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005322 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005323 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005324
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005325 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005326}
5327
5328/**
5329 * i40e_fdir_teardown - release the Flow Director resources
5330 * @pf: board private structure
5331 **/
5332static void i40e_fdir_teardown(struct i40e_pf *pf)
5333{
5334 int i;
5335
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005336 i40e_fdir_filter_exit(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005337 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
5338 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
5339 i40e_vsi_release(pf->vsi[i]);
5340 break;
5341 }
5342 }
5343}
5344
5345/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005346 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005347 * @pf: board private structure
5348 *
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005349 * Close up the VFs and other things in prep for pf Reset.
5350 **/
5351static int i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005352{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005353 struct i40e_hw *hw = &pf->hw;
5354 i40e_status ret;
5355 u32 v;
5356
5357 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
5358 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005359 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005360
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005361 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005362
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005363 /* quiesce the VSIs and their queues that are not already DOWN */
5364 i40e_pf_quiesce_all_vsi(pf);
5365
5366 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5367 if (pf->vsi[v])
5368 pf->vsi[v]->seid = 0;
5369 }
5370
5371 i40e_shutdown_adminq(&pf->hw);
5372
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005373 /* call shutdown HMC */
5374 ret = i40e_shutdown_lan_hmc(hw);
5375 if (ret) {
5376 dev_info(&pf->pdev->dev, "shutdown_lan_hmc failed: %d\n", ret);
5377 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
5378 }
5379 return ret;
5380}
5381
5382/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00005383 * i40e_send_version - update firmware with driver version
5384 * @pf: PF struct
5385 */
5386static void i40e_send_version(struct i40e_pf *pf)
5387{
5388 struct i40e_driver_version dv;
5389
5390 dv.major_version = DRV_VERSION_MAJOR;
5391 dv.minor_version = DRV_VERSION_MINOR;
5392 dv.build_version = DRV_VERSION_BUILD;
5393 dv.subbuild_version = 0;
5394 strncpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
5395 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
5396}
5397
5398/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00005399 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005400 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005401 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005402 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005403static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005404{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005405 struct i40e_hw *hw = &pf->hw;
5406 i40e_status ret;
5407 u32 v;
5408
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005409 /* Now we wait for GRST to settle out.
5410 * We don't have to delete the VEBs or VSIs from the hw switch
5411 * because the reset will make them disappear.
5412 */
5413 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00005414 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005415 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00005416 goto end_core_reset;
5417 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005418 pf->pfr_count++;
5419
5420 if (test_bit(__I40E_DOWN, &pf->state))
5421 goto end_core_reset;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005422 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005423
5424 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
5425 ret = i40e_init_adminq(&pf->hw);
5426 if (ret) {
5427 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, %d\n", ret);
5428 goto end_core_reset;
5429 }
5430
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005431 /* re-verify the eeprom if we just had an EMP reset */
5432 if (test_bit(__I40E_EMP_RESET_REQUESTED, &pf->state)) {
5433 clear_bit(__I40E_EMP_RESET_REQUESTED, &pf->state);
5434 i40e_verify_eeprom(pf);
5435 }
5436
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005437 ret = i40e_get_capabilities(pf);
5438 if (ret) {
5439 dev_info(&pf->pdev->dev, "i40e_get_capabilities failed, %d\n",
5440 ret);
5441 goto end_core_reset;
5442 }
5443
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005444 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
5445 hw->func_caps.num_rx_qp,
5446 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
5447 if (ret) {
5448 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
5449 goto end_core_reset;
5450 }
5451 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
5452 if (ret) {
5453 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
5454 goto end_core_reset;
5455 }
5456
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005457#ifdef CONFIG_I40E_DCB
5458 ret = i40e_init_pf_dcb(pf);
5459 if (ret) {
5460 dev_info(&pf->pdev->dev, "init_pf_dcb failed: %d\n", ret);
5461 goto end_core_reset;
5462 }
5463#endif /* CONFIG_I40E_DCB */
5464
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005465 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005466 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005467 if (ret)
5468 goto end_core_reset;
5469
5470 /* Rebuild the VSIs and VEBs that existed before reset.
5471 * They are still in our local switch element arrays, so only
5472 * need to rebuild the switch model in the HW.
5473 *
5474 * If there were VEBs but the reconstitution failed, we'll try
5475 * try to recover minimal use by getting the basic PF VSI working.
5476 */
5477 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005478 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005479 /* find the one VEB connected to the MAC, and find orphans */
5480 for (v = 0; v < I40E_MAX_VEB; v++) {
5481 if (!pf->veb[v])
5482 continue;
5483
5484 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
5485 pf->veb[v]->uplink_seid == 0) {
5486 ret = i40e_reconstitute_veb(pf->veb[v]);
5487
5488 if (!ret)
5489 continue;
5490
5491 /* If Main VEB failed, we're in deep doodoo,
5492 * so give up rebuilding the switch and set up
5493 * for minimal rebuild of PF VSI.
5494 * If orphan failed, we'll report the error
5495 * but try to keep going.
5496 */
5497 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
5498 dev_info(&pf->pdev->dev,
5499 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
5500 ret);
5501 pf->vsi[pf->lan_vsi]->uplink_seid
5502 = pf->mac_seid;
5503 break;
5504 } else if (pf->veb[v]->uplink_seid == 0) {
5505 dev_info(&pf->pdev->dev,
5506 "rebuild of orphan VEB failed: %d\n",
5507 ret);
5508 }
5509 }
5510 }
5511 }
5512
5513 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
5514 dev_info(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
5515 /* no VEB, so rebuild only the Main VSI */
5516 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
5517 if (ret) {
5518 dev_info(&pf->pdev->dev,
5519 "rebuild of Main VSI failed: %d\n", ret);
5520 goto end_core_reset;
5521 }
5522 }
5523
5524 /* reinit the misc interrupt */
5525 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5526 ret = i40e_setup_misc_vector(pf);
5527
5528 /* restart the VSIs that were rebuilt and running before the reset */
5529 i40e_pf_unquiesce_all_vsi(pf);
5530
Mitch Williams69f64b22014-02-13 03:48:46 -08005531 if (pf->num_alloc_vfs) {
5532 for (v = 0; v < pf->num_alloc_vfs; v++)
5533 i40e_reset_vf(&pf->vf[v], true);
5534 }
5535
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005536 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00005537 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005538
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005539 dev_info(&pf->pdev->dev, "reset complete\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005540
5541end_core_reset:
5542 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
5543}
5544
5545/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005546 * i40e_handle_reset_warning - prep for the pf to reset, reset and rebuild
5547 * @pf: board private structure
5548 *
5549 * Close up the VFs and other things in prep for a Core Reset,
5550 * then get ready to rebuild the world.
5551 **/
5552static void i40e_handle_reset_warning(struct i40e_pf *pf)
5553{
5554 i40e_status ret;
5555
5556 ret = i40e_prep_for_reset(pf);
5557 if (!ret)
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005558 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005559}
5560
5561/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005562 * i40e_handle_mdd_event
5563 * @pf: pointer to the pf structure
5564 *
5565 * Called from the MDD irq handler to identify possibly malicious vfs
5566 **/
5567static void i40e_handle_mdd_event(struct i40e_pf *pf)
5568{
5569 struct i40e_hw *hw = &pf->hw;
5570 bool mdd_detected = false;
5571 struct i40e_vf *vf;
5572 u32 reg;
5573 int i;
5574
5575 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
5576 return;
5577
5578 /* find what triggered the MDD event */
5579 reg = rd32(hw, I40E_GL_MDET_TX);
5580 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
5581 u8 func = (reg & I40E_GL_MDET_TX_FUNCTION_MASK)
5582 >> I40E_GL_MDET_TX_FUNCTION_SHIFT;
5583 u8 event = (reg & I40E_GL_MDET_TX_EVENT_SHIFT)
5584 >> I40E_GL_MDET_TX_EVENT_SHIFT;
5585 u8 queue = (reg & I40E_GL_MDET_TX_QUEUE_MASK)
5586 >> I40E_GL_MDET_TX_QUEUE_SHIFT;
5587 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00005588 "Malicious Driver Detection event 0x%02x on TX queue %d of function 0x%02x\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005589 event, queue, func);
5590 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
5591 mdd_detected = true;
5592 }
5593 reg = rd32(hw, I40E_GL_MDET_RX);
5594 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
5595 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK)
5596 >> I40E_GL_MDET_RX_FUNCTION_SHIFT;
5597 u8 event = (reg & I40E_GL_MDET_RX_EVENT_SHIFT)
5598 >> I40E_GL_MDET_RX_EVENT_SHIFT;
5599 u8 queue = (reg & I40E_GL_MDET_RX_QUEUE_MASK)
5600 >> I40E_GL_MDET_RX_QUEUE_SHIFT;
5601 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00005602 "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005603 event, queue, func);
5604 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
5605 mdd_detected = true;
5606 }
5607
5608 /* see if one of the VFs needs its hand slapped */
5609 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
5610 vf = &(pf->vf[i]);
5611 reg = rd32(hw, I40E_VP_MDET_TX(i));
5612 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
5613 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
5614 vf->num_mdd_events++;
5615 dev_info(&pf->pdev->dev, "MDD TX event on VF %d\n", i);
5616 }
5617
5618 reg = rd32(hw, I40E_VP_MDET_RX(i));
5619 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
5620 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
5621 vf->num_mdd_events++;
5622 dev_info(&pf->pdev->dev, "MDD RX event on VF %d\n", i);
5623 }
5624
5625 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
5626 dev_info(&pf->pdev->dev,
5627 "Too many MDD events on VF %d, disabled\n", i);
5628 dev_info(&pf->pdev->dev,
5629 "Use PF Control I/F to re-enable the VF\n");
5630 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
5631 }
5632 }
5633
5634 /* re-enable mdd interrupt cause */
5635 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
5636 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
5637 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
5638 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
5639 i40e_flush(hw);
5640}
5641
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005642#ifdef CONFIG_I40E_VXLAN
5643/**
5644 * i40e_sync_vxlan_filters_subtask - Sync the VSI filter list with HW
5645 * @pf: board private structure
5646 **/
5647static void i40e_sync_vxlan_filters_subtask(struct i40e_pf *pf)
5648{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005649 struct i40e_hw *hw = &pf->hw;
5650 i40e_status ret;
5651 u8 filter_index;
5652 __be16 port;
5653 int i;
5654
5655 if (!(pf->flags & I40E_FLAG_VXLAN_FILTER_SYNC))
5656 return;
5657
5658 pf->flags &= ~I40E_FLAG_VXLAN_FILTER_SYNC;
5659
5660 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
5661 if (pf->pending_vxlan_bitmap & (1 << i)) {
5662 pf->pending_vxlan_bitmap &= ~(1 << i);
5663 port = pf->vxlan_ports[i];
5664 ret = port ?
5665 i40e_aq_add_udp_tunnel(hw, ntohs(port),
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005666 I40E_AQC_TUNNEL_TYPE_VXLAN,
5667 &filter_index, NULL)
5668 : i40e_aq_del_udp_tunnel(hw, i, NULL);
5669
5670 if (ret) {
5671 dev_info(&pf->pdev->dev, "Failed to execute AQ command for %s port %d with index %d\n",
5672 port ? "adding" : "deleting",
5673 ntohs(port), port ? i : i);
5674
5675 pf->vxlan_ports[i] = 0;
5676 } else {
5677 dev_info(&pf->pdev->dev, "%s port %d with AQ command with index %d\n",
5678 port ? "Added" : "Deleted",
5679 ntohs(port), port ? i : filter_index);
5680 }
5681 }
5682 }
5683}
5684
5685#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005686/**
5687 * i40e_service_task - Run the driver's async subtasks
5688 * @work: pointer to work_struct containing our data
5689 **/
5690static void i40e_service_task(struct work_struct *work)
5691{
5692 struct i40e_pf *pf = container_of(work,
5693 struct i40e_pf,
5694 service_task);
5695 unsigned long start_time = jiffies;
5696
5697 i40e_reset_subtask(pf);
5698 i40e_handle_mdd_event(pf);
5699 i40e_vc_process_vflr_event(pf);
5700 i40e_watchdog_subtask(pf);
5701 i40e_fdir_reinit_subtask(pf);
5702 i40e_check_hang_subtask(pf);
5703 i40e_sync_filters_subtask(pf);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005704#ifdef CONFIG_I40E_VXLAN
5705 i40e_sync_vxlan_filters_subtask(pf);
5706#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005707 i40e_clean_adminq_subtask(pf);
5708
5709 i40e_service_event_complete(pf);
5710
5711 /* If the tasks have taken longer than one timer cycle or there
5712 * is more work to be done, reschedule the service task now
5713 * rather than wait for the timer to tick again.
5714 */
5715 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
5716 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
5717 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
5718 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
5719 i40e_service_event_schedule(pf);
5720}
5721
5722/**
5723 * i40e_service_timer - timer callback
5724 * @data: pointer to PF struct
5725 **/
5726static void i40e_service_timer(unsigned long data)
5727{
5728 struct i40e_pf *pf = (struct i40e_pf *)data;
5729
5730 mod_timer(&pf->service_timer,
5731 round_jiffies(jiffies + pf->service_timer_period));
5732 i40e_service_event_schedule(pf);
5733}
5734
5735/**
5736 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
5737 * @vsi: the VSI being configured
5738 **/
5739static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
5740{
5741 struct i40e_pf *pf = vsi->back;
5742
5743 switch (vsi->type) {
5744 case I40E_VSI_MAIN:
5745 vsi->alloc_queue_pairs = pf->num_lan_qps;
5746 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5747 I40E_REQ_DESCRIPTOR_MULTIPLE);
5748 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5749 vsi->num_q_vectors = pf->num_lan_msix;
5750 else
5751 vsi->num_q_vectors = 1;
5752
5753 break;
5754
5755 case I40E_VSI_FDIR:
5756 vsi->alloc_queue_pairs = 1;
5757 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
5758 I40E_REQ_DESCRIPTOR_MULTIPLE);
5759 vsi->num_q_vectors = 1;
5760 break;
5761
5762 case I40E_VSI_VMDQ2:
5763 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
5764 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5765 I40E_REQ_DESCRIPTOR_MULTIPLE);
5766 vsi->num_q_vectors = pf->num_vmdq_msix;
5767 break;
5768
5769 case I40E_VSI_SRIOV:
5770 vsi->alloc_queue_pairs = pf->num_vf_qps;
5771 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5772 I40E_REQ_DESCRIPTOR_MULTIPLE);
5773 break;
5774
5775 default:
5776 WARN_ON(1);
5777 return -ENODATA;
5778 }
5779
5780 return 0;
5781}
5782
5783/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005784 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
5785 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005786 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005787 *
5788 * On error: returns error code (negative)
5789 * On success: returns 0
5790 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005791static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005792{
5793 int size;
5794 int ret = 0;
5795
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00005796 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005797 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
5798 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
5799 if (!vsi->tx_rings)
5800 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005801 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
5802
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005803 if (alloc_qvectors) {
5804 /* allocate memory for q_vector pointers */
5805 size = sizeof(struct i40e_q_vectors *) * vsi->num_q_vectors;
5806 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
5807 if (!vsi->q_vectors) {
5808 ret = -ENOMEM;
5809 goto err_vectors;
5810 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005811 }
5812 return ret;
5813
5814err_vectors:
5815 kfree(vsi->tx_rings);
5816 return ret;
5817}
5818
5819/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005820 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
5821 * @pf: board private structure
5822 * @type: type of VSI
5823 *
5824 * On error: returns error code (negative)
5825 * On success: returns vsi index in PF (positive)
5826 **/
5827static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
5828{
5829 int ret = -ENODEV;
5830 struct i40e_vsi *vsi;
5831 int vsi_idx;
5832 int i;
5833
5834 /* Need to protect the allocation of the VSIs at the PF level */
5835 mutex_lock(&pf->switch_mutex);
5836
5837 /* VSI list may be fragmented if VSI creation/destruction has
5838 * been happening. We can afford to do a quick scan to look
5839 * for any free VSIs in the list.
5840 *
5841 * find next empty vsi slot, looping back around if necessary
5842 */
5843 i = pf->next_vsi;
5844 while (i < pf->hw.func_caps.num_vsis && pf->vsi[i])
5845 i++;
5846 if (i >= pf->hw.func_caps.num_vsis) {
5847 i = 0;
5848 while (i < pf->next_vsi && pf->vsi[i])
5849 i++;
5850 }
5851
5852 if (i < pf->hw.func_caps.num_vsis && !pf->vsi[i]) {
5853 vsi_idx = i; /* Found one! */
5854 } else {
5855 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00005856 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005857 }
5858 pf->next_vsi = ++i;
5859
5860 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
5861 if (!vsi) {
5862 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00005863 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005864 }
5865 vsi->type = type;
5866 vsi->back = pf;
5867 set_bit(__I40E_DOWN, &vsi->state);
5868 vsi->flags = 0;
5869 vsi->idx = vsi_idx;
5870 vsi->rx_itr_setting = pf->rx_itr_default;
5871 vsi->tx_itr_setting = pf->tx_itr_default;
5872 vsi->netdev_registered = false;
5873 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
5874 INIT_LIST_HEAD(&vsi->mac_filter_list);
5875
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005876 ret = i40e_set_num_rings_in_vsi(vsi);
5877 if (ret)
5878 goto err_rings;
5879
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005880 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005881 if (ret)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005882 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00005883
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005884 /* Setup default MSIX irq handler for VSI */
5885 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
5886
5887 pf->vsi[vsi_idx] = vsi;
5888 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00005889 goto unlock_pf;
5890
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005891err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00005892 pf->next_vsi = i - 1;
5893 kfree(vsi);
5894unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005895 mutex_unlock(&pf->switch_mutex);
5896 return ret;
5897}
5898
5899/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005900 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
5901 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005902 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005903 *
5904 * On error: returns error code (negative)
5905 * On success: returns 0
5906 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005907static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005908{
5909 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005910 if (free_qvectors) {
5911 kfree(vsi->q_vectors);
5912 vsi->q_vectors = NULL;
5913 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005914 kfree(vsi->tx_rings);
5915 vsi->tx_rings = NULL;
5916 vsi->rx_rings = NULL;
5917}
5918
5919/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005920 * i40e_vsi_clear - Deallocate the VSI provided
5921 * @vsi: the VSI being un-configured
5922 **/
5923static int i40e_vsi_clear(struct i40e_vsi *vsi)
5924{
5925 struct i40e_pf *pf;
5926
5927 if (!vsi)
5928 return 0;
5929
5930 if (!vsi->back)
5931 goto free_vsi;
5932 pf = vsi->back;
5933
5934 mutex_lock(&pf->switch_mutex);
5935 if (!pf->vsi[vsi->idx]) {
5936 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
5937 vsi->idx, vsi->idx, vsi, vsi->type);
5938 goto unlock_vsi;
5939 }
5940
5941 if (pf->vsi[vsi->idx] != vsi) {
5942 dev_err(&pf->pdev->dev,
5943 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
5944 pf->vsi[vsi->idx]->idx,
5945 pf->vsi[vsi->idx],
5946 pf->vsi[vsi->idx]->type,
5947 vsi->idx, vsi, vsi->type);
5948 goto unlock_vsi;
5949 }
5950
5951 /* updates the pf for this cleared vsi */
5952 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
5953 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
5954
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005955 i40e_vsi_free_arrays(vsi, true);
Alexander Duyck493fb302013-09-28 07:01:44 +00005956
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005957 pf->vsi[vsi->idx] = NULL;
5958 if (vsi->idx < pf->next_vsi)
5959 pf->next_vsi = vsi->idx;
5960
5961unlock_vsi:
5962 mutex_unlock(&pf->switch_mutex);
5963free_vsi:
5964 kfree(vsi);
5965
5966 return 0;
5967}
5968
5969/**
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005970 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
5971 * @vsi: the VSI being cleaned
5972 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00005973static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005974{
5975 int i;
5976
Greg Rose8e9dca52013-12-18 13:45:53 +00005977 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00005978 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00005979 kfree_rcu(vsi->tx_rings[i], rcu);
5980 vsi->tx_rings[i] = NULL;
5981 vsi->rx_rings[i] = NULL;
5982 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00005983 }
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005984}
5985
5986/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005987 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
5988 * @vsi: the VSI being configured
5989 **/
5990static int i40e_alloc_rings(struct i40e_vsi *vsi)
5991{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00005992 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005993 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005994 int i;
5995
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005996 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00005997 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00005998 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005999 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
6000 if (!tx_ring)
6001 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006002
6003 tx_ring->queue_index = i;
6004 tx_ring->reg_idx = vsi->base_queue + i;
6005 tx_ring->ring_active = false;
6006 tx_ring->vsi = vsi;
6007 tx_ring->netdev = vsi->netdev;
6008 tx_ring->dev = &pf->pdev->dev;
6009 tx_ring->count = vsi->num_desc;
6010 tx_ring->size = 0;
6011 tx_ring->dcb_tc = 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006012 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006013
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006014 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006015 rx_ring->queue_index = i;
6016 rx_ring->reg_idx = vsi->base_queue + i;
6017 rx_ring->ring_active = false;
6018 rx_ring->vsi = vsi;
6019 rx_ring->netdev = vsi->netdev;
6020 rx_ring->dev = &pf->pdev->dev;
6021 rx_ring->count = vsi->num_desc;
6022 rx_ring->size = 0;
6023 rx_ring->dcb_tc = 0;
6024 if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
6025 set_ring_16byte_desc_enabled(rx_ring);
6026 else
6027 clear_ring_16byte_desc_enabled(rx_ring);
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006028 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006029 }
6030
6031 return 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006032
6033err_out:
6034 i40e_vsi_clear_rings(vsi);
6035 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006036}
6037
6038/**
6039 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
6040 * @pf: board private structure
6041 * @vectors: the number of MSI-X vectors to request
6042 *
6043 * Returns the number of vectors reserved, or error
6044 **/
6045static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
6046{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01006047 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
6048 I40E_MIN_MSIX, vectors);
6049 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006050 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01006051 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006052 vectors = 0;
6053 }
6054
Alexander Gordeev7b37f372014-02-18 11:11:42 +01006055 pf->num_msix_entries = vectors;
6056
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006057 return vectors;
6058}
6059
6060/**
6061 * i40e_init_msix - Setup the MSIX capability
6062 * @pf: board private structure
6063 *
6064 * Work with the OS to set up the MSIX vectors needed.
6065 *
6066 * Returns 0 on success, negative on failure
6067 **/
6068static int i40e_init_msix(struct i40e_pf *pf)
6069{
6070 i40e_status err = 0;
6071 struct i40e_hw *hw = &pf->hw;
6072 int v_budget, i;
6073 int vec;
6074
6075 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
6076 return -ENODEV;
6077
6078 /* The number of vectors we'll request will be comprised of:
6079 * - Add 1 for "other" cause for Admin Queue events, etc.
6080 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006081 * - Queues being used for RSS.
6082 * We don't need as many as max_rss_size vectors.
6083 * use rss_size instead in the calculation since that
6084 * is governed by number of cpus in the system.
6085 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006086 * - The number of VMDq pairs
6087 * Once we count this up, try the request.
6088 *
6089 * If we can't get what we want, we'll simplify to nearly nothing
6090 * and try again. If that still fails, we punt.
6091 */
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006092 pf->num_lan_msix = pf->num_lan_qps - (pf->rss_size_max - pf->rss_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006093 pf->num_vmdq_msix = pf->num_vmdq_qps;
6094 v_budget = 1 + pf->num_lan_msix;
6095 v_budget += (pf->num_vmdq_vsis * pf->num_vmdq_msix);
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006096 if (pf->flags & I40E_FLAG_FD_SB_ENABLED)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006097 v_budget++;
6098
6099 /* Scale down if necessary, and the rings will share vectors */
6100 v_budget = min_t(int, v_budget, hw->func_caps.num_msix_vectors);
6101
6102 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
6103 GFP_KERNEL);
6104 if (!pf->msix_entries)
6105 return -ENOMEM;
6106
6107 for (i = 0; i < v_budget; i++)
6108 pf->msix_entries[i].entry = i;
6109 vec = i40e_reserve_msix_vectors(pf, v_budget);
6110 if (vec < I40E_MIN_MSIX) {
6111 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
6112 kfree(pf->msix_entries);
6113 pf->msix_entries = NULL;
6114 return -ENODEV;
6115
6116 } else if (vec == I40E_MIN_MSIX) {
6117 /* Adjust for minimal MSIX use */
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006118 dev_info(&pf->pdev->dev, "Features disabled, not enough MSI-X vectors\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006119 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
6120 pf->num_vmdq_vsis = 0;
6121 pf->num_vmdq_qps = 0;
6122 pf->num_vmdq_msix = 0;
6123 pf->num_lan_qps = 1;
6124 pf->num_lan_msix = 1;
6125
6126 } else if (vec != v_budget) {
6127 /* Scale vector usage down */
6128 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
6129 vec--; /* reserve the misc vector */
6130
6131 /* partition out the remaining vectors */
6132 switch (vec) {
6133 case 2:
6134 pf->num_vmdq_vsis = 1;
6135 pf->num_lan_msix = 1;
6136 break;
6137 case 3:
6138 pf->num_vmdq_vsis = 1;
6139 pf->num_lan_msix = 2;
6140 break;
6141 default:
6142 pf->num_lan_msix = min_t(int, (vec / 2),
6143 pf->num_lan_qps);
6144 pf->num_vmdq_vsis = min_t(int, (vec - pf->num_lan_msix),
6145 I40E_DEFAULT_NUM_VMDQ_VSI);
6146 break;
6147 }
6148 }
6149
6150 return err;
6151}
6152
6153/**
Greg Rose90e04072014-03-06 08:59:57 +00006154 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00006155 * @vsi: the VSI being configured
6156 * @v_idx: index of the vector in the vsi struct
6157 *
6158 * We allocate one q_vector. If allocation fails we return -ENOMEM.
6159 **/
Greg Rose90e04072014-03-06 08:59:57 +00006160static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
Alexander Duyck493fb302013-09-28 07:01:44 +00006161{
6162 struct i40e_q_vector *q_vector;
6163
6164 /* allocate q_vector */
6165 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
6166 if (!q_vector)
6167 return -ENOMEM;
6168
6169 q_vector->vsi = vsi;
6170 q_vector->v_idx = v_idx;
6171 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
6172 if (vsi->netdev)
6173 netif_napi_add(vsi->netdev, &q_vector->napi,
6174 i40e_napi_poll, vsi->work_limit);
6175
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00006176 q_vector->rx.latency_range = I40E_LOW_LATENCY;
6177 q_vector->tx.latency_range = I40E_LOW_LATENCY;
6178
Alexander Duyck493fb302013-09-28 07:01:44 +00006179 /* tie q_vector and vsi together */
6180 vsi->q_vectors[v_idx] = q_vector;
6181
6182 return 0;
6183}
6184
6185/**
Greg Rose90e04072014-03-06 08:59:57 +00006186 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006187 * @vsi: the VSI being configured
6188 *
6189 * We allocate one q_vector per queue interrupt. If allocation fails we
6190 * return -ENOMEM.
6191 **/
Greg Rose90e04072014-03-06 08:59:57 +00006192static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006193{
6194 struct i40e_pf *pf = vsi->back;
6195 int v_idx, num_q_vectors;
Alexander Duyck493fb302013-09-28 07:01:44 +00006196 int err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006197
6198 /* if not MSIX, give the one vector only to the LAN VSI */
6199 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6200 num_q_vectors = vsi->num_q_vectors;
6201 else if (vsi == pf->vsi[pf->lan_vsi])
6202 num_q_vectors = 1;
6203 else
6204 return -EINVAL;
6205
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006206 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Greg Rose90e04072014-03-06 08:59:57 +00006207 err = i40e_vsi_alloc_q_vector(vsi, v_idx);
Alexander Duyck493fb302013-09-28 07:01:44 +00006208 if (err)
6209 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006210 }
6211
6212 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00006213
6214err_out:
6215 while (v_idx--)
6216 i40e_free_q_vector(vsi, v_idx);
6217
6218 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006219}
6220
6221/**
6222 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
6223 * @pf: board private structure to initialize
6224 **/
6225static void i40e_init_interrupt_scheme(struct i40e_pf *pf)
6226{
6227 int err = 0;
6228
6229 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
6230 err = i40e_init_msix(pf);
6231 if (err) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006232 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
6233 I40E_FLAG_RSS_ENABLED |
6234 I40E_FLAG_DCB_ENABLED |
6235 I40E_FLAG_SRIOV_ENABLED |
6236 I40E_FLAG_FD_SB_ENABLED |
6237 I40E_FLAG_FD_ATR_ENABLED |
6238 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006239
6240 /* rework the queue expectations without MSIX */
6241 i40e_determine_queue_usage(pf);
6242 }
6243 }
6244
6245 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
6246 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006247 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006248 err = pci_enable_msi(pf->pdev);
6249 if (err) {
Shannon Nelson958a3e32013-09-28 07:13:28 +00006250 dev_info(&pf->pdev->dev, "MSI init failed - %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006251 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
6252 }
6253 }
6254
Shannon Nelson958a3e32013-09-28 07:13:28 +00006255 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006256 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00006257
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006258 /* track first vector for misc interrupts */
6259 err = i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT-1);
6260}
6261
6262/**
6263 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
6264 * @pf: board private structure
6265 *
6266 * This sets up the handler for MSIX 0, which is used to manage the
6267 * non-queue interrupts, e.g. AdminQ and errors. This is not used
6268 * when in MSI or Legacy interrupt mode.
6269 **/
6270static int i40e_setup_misc_vector(struct i40e_pf *pf)
6271{
6272 struct i40e_hw *hw = &pf->hw;
6273 int err = 0;
6274
6275 /* Only request the irq if this is the first time through, and
6276 * not when we're rebuilding after a Reset
6277 */
6278 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
6279 err = request_irq(pf->msix_entries[0].vector,
6280 i40e_intr, 0, pf->misc_int_name, pf);
6281 if (err) {
6282 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006283 "request_irq for %s failed: %d\n",
6284 pf->misc_int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006285 return -EFAULT;
6286 }
6287 }
6288
6289 i40e_enable_misc_int_causes(hw);
6290
6291 /* associate no queues to the misc vector */
6292 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
6293 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
6294
6295 i40e_flush(hw);
6296
6297 i40e_irq_dynamic_enable_icr0(pf);
6298
6299 return err;
6300}
6301
6302/**
6303 * i40e_config_rss - Prepare for RSS if used
6304 * @pf: board private structure
6305 **/
6306static int i40e_config_rss(struct i40e_pf *pf)
6307{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006308 /* Set of random keys generated using kernel random number generator */
6309 static const u32 seed[I40E_PFQF_HKEY_MAX_INDEX + 1] = {0x41b01687,
6310 0x183cfd8c, 0xce880440, 0x580cbc3c, 0x35897377,
6311 0x328b25e1, 0x4fa98922, 0xb7d90c14, 0xd5bad70d,
6312 0xcd15a2c1, 0xe8580225, 0x4a1e9d11, 0xfe5731be};
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00006313 struct i40e_hw *hw = &pf->hw;
6314 u32 lut = 0;
6315 int i, j;
6316 u64 hena;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006317
6318 /* Fill out hash function seed */
6319 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
6320 wr32(hw, I40E_PFQF_HKEY(i), seed[i]);
6321
6322 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
6323 hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
6324 ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
Mitch Williams12dc4fe2013-11-28 06:39:32 +00006325 hena |= I40E_DEFAULT_RSS_HENA;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006326 wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
6327 wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
6328
6329 /* Populate the LUT with max no. of queues in round robin fashion */
6330 for (i = 0, j = 0; i < pf->hw.func_caps.rss_table_size; i++, j++) {
6331
6332 /* The assumption is that lan qp count will be the highest
6333 * qp count for any PF VSI that needs RSS.
6334 * If multiple VSIs need RSS support, all the qp counts
6335 * for those VSIs should be a power of 2 for RSS to work.
6336 * If LAN VSI is the only consumer for RSS then this requirement
6337 * is not necessary.
6338 */
6339 if (j == pf->rss_size)
6340 j = 0;
6341 /* lut = 4-byte sliding window of 4 lut entries */
6342 lut = (lut << 8) | (j &
6343 ((0x1 << pf->hw.func_caps.rss_table_entry_width) - 1));
6344 /* On i = 3, we have 4 entries in lut; write to the register */
6345 if ((i & 3) == 3)
6346 wr32(hw, I40E_PFQF_HLUT(i >> 2), lut);
6347 }
6348 i40e_flush(hw);
6349
6350 return 0;
6351}
6352
6353/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006354 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
6355 * @pf: board private structure
6356 * @queue_count: the requested queue count for rss.
6357 *
6358 * returns 0 if rss is not enabled, if enabled returns the final rss queue
6359 * count which may be different from the requested queue count.
6360 **/
6361int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
6362{
6363 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
6364 return 0;
6365
6366 queue_count = min_t(int, queue_count, pf->rss_size_max);
6367 queue_count = rounddown_pow_of_two(queue_count);
6368
6369 if (queue_count != pf->rss_size) {
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006370 i40e_prep_for_reset(pf);
6371
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006372 pf->rss_size = queue_count;
6373
6374 i40e_reset_and_rebuild(pf, true);
6375 i40e_config_rss(pf);
6376 }
6377 dev_info(&pf->pdev->dev, "RSS count: %d\n", pf->rss_size);
6378 return pf->rss_size;
6379}
6380
6381/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006382 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
6383 * @pf: board private structure to initialize
6384 *
6385 * i40e_sw_init initializes the Adapter private data structure.
6386 * Fields are initialized based on PCI device information and
6387 * OS network device settings (MTU size).
6388 **/
6389static int i40e_sw_init(struct i40e_pf *pf)
6390{
6391 int err = 0;
6392 int size;
6393
6394 pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
6395 (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
Shannon Nelson27599972013-11-16 10:00:43 +00006396 pf->hw.debug_mask = pf->msg_enable | I40E_DEBUG_DIAG;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006397 if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
6398 if (I40E_DEBUG_USER & debug)
6399 pf->hw.debug_mask = debug;
6400 pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
6401 I40E_DEFAULT_MSG_ENABLE);
6402 }
6403
6404 /* Set default capability flags */
6405 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
6406 I40E_FLAG_MSI_ENABLED |
6407 I40E_FLAG_MSIX_ENABLED |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006408 I40E_FLAG_RX_1BUF_ENABLED;
6409
Mitch Williamsca99eb92014-04-04 04:43:07 +00006410 /* Set default ITR */
6411 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
6412 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
6413
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006414 /* Depending on PF configurations, it is possible that the RSS
6415 * maximum might end up larger than the available queues
6416 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006417 pf->rss_size_max = 0x1 << pf->hw.func_caps.rss_table_entry_width;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006418 pf->rss_size_max = min_t(int, pf->rss_size_max,
6419 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006420 if (pf->hw.func_caps.rss) {
6421 pf->flags |= I40E_FLAG_RSS_ENABLED;
Jesse Brandeburgbf051a32013-11-26 10:49:17 +00006422 pf->rss_size = min_t(int, pf->rss_size_max, num_online_cpus());
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006423 pf->rss_size = rounddown_pow_of_two(pf->rss_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006424 } else {
6425 pf->rss_size = 1;
6426 }
6427
Catherine Sullivan2050bc62013-12-18 13:46:03 +00006428 /* MFP mode enabled */
6429 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.mfp_mode_1) {
6430 pf->flags |= I40E_FLAG_MFP_ENABLED;
6431 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
6432 }
6433
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006434 /* FW/NVM is not yet fixed in this regard */
6435 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
6436 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
6437 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
6438 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006439 if (!(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006440 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006441 } else {
6442 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00006443 "Flow Director Sideband mode Disabled in MFP mode\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006444 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006445 pf->fdir_pf_filter_count =
6446 pf->hw.func_caps.fd_filters_guaranteed;
6447 pf->hw.fdir_shared_filter_count =
6448 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006449 }
6450
6451 if (pf->hw.func_caps.vmdq) {
6452 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
6453 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
6454 pf->num_vmdq_qps = I40E_DEFAULT_QUEUES_PER_VMDQ;
6455 }
6456
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006457#ifdef CONFIG_PCI_IOV
6458 if (pf->hw.func_caps.num_vfs) {
6459 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
6460 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
6461 pf->num_req_vfs = min_t(int,
6462 pf->hw.func_caps.num_vfs,
6463 I40E_MAX_VF_COUNT);
6464 }
6465#endif /* CONFIG_PCI_IOV */
6466 pf->eeprom_version = 0xDEAD;
6467 pf->lan_veb = I40E_NO_VEB;
6468 pf->lan_vsi = I40E_NO_VSI;
6469
6470 /* set up queue assignment tracking */
6471 size = sizeof(struct i40e_lump_tracking)
6472 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
6473 pf->qp_pile = kzalloc(size, GFP_KERNEL);
6474 if (!pf->qp_pile) {
6475 err = -ENOMEM;
6476 goto sw_init_done;
6477 }
6478 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
6479 pf->qp_pile->search_hint = 0;
6480
6481 /* set up vector assignment tracking */
6482 size = sizeof(struct i40e_lump_tracking)
6483 + (sizeof(u16) * pf->hw.func_caps.num_msix_vectors);
6484 pf->irq_pile = kzalloc(size, GFP_KERNEL);
6485 if (!pf->irq_pile) {
6486 kfree(pf->qp_pile);
6487 err = -ENOMEM;
6488 goto sw_init_done;
6489 }
6490 pf->irq_pile->num_entries = pf->hw.func_caps.num_msix_vectors;
6491 pf->irq_pile->search_hint = 0;
6492
6493 mutex_init(&pf->switch_mutex);
6494
6495sw_init_done:
6496 return err;
6497}
6498
6499/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00006500 * i40e_set_ntuple - set the ntuple feature flag and take action
6501 * @pf: board private structure to initialize
6502 * @features: the feature set that the stack is suggesting
6503 *
6504 * returns a bool to indicate if reset needs to happen
6505 **/
6506bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
6507{
6508 bool need_reset = false;
6509
6510 /* Check if Flow Director n-tuple support was enabled or disabled. If
6511 * the state changed, we need to reset.
6512 */
6513 if (features & NETIF_F_NTUPLE) {
6514 /* Enable filters and mark for reset */
6515 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
6516 need_reset = true;
6517 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
6518 } else {
6519 /* turn off filters, mark for reset and clear SW filter list */
6520 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
6521 need_reset = true;
6522 i40e_fdir_filter_exit(pf);
6523 }
6524 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6525 /* if ATR was disabled it can be re-enabled. */
6526 if (!(pf->flags & I40E_FLAG_FD_ATR_ENABLED))
6527 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
6528 }
6529 return need_reset;
6530}
6531
6532/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006533 * i40e_set_features - set the netdev feature flags
6534 * @netdev: ptr to the netdev being adjusted
6535 * @features: the feature set that the stack is suggesting
6536 **/
6537static int i40e_set_features(struct net_device *netdev,
6538 netdev_features_t features)
6539{
6540 struct i40e_netdev_priv *np = netdev_priv(netdev);
6541 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00006542 struct i40e_pf *pf = vsi->back;
6543 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006544
6545 if (features & NETIF_F_HW_VLAN_CTAG_RX)
6546 i40e_vlan_stripping_enable(vsi);
6547 else
6548 i40e_vlan_stripping_disable(vsi);
6549
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00006550 need_reset = i40e_set_ntuple(pf, features);
6551
6552 if (need_reset)
6553 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
6554
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006555 return 0;
6556}
6557
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006558#ifdef CONFIG_I40E_VXLAN
6559/**
6560 * i40e_get_vxlan_port_idx - Lookup a possibly offloaded for Rx UDP port
6561 * @pf: board private structure
6562 * @port: The UDP port to look up
6563 *
6564 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
6565 **/
6566static u8 i40e_get_vxlan_port_idx(struct i40e_pf *pf, __be16 port)
6567{
6568 u8 i;
6569
6570 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
6571 if (pf->vxlan_ports[i] == port)
6572 return i;
6573 }
6574
6575 return i;
6576}
6577
6578/**
6579 * i40e_add_vxlan_port - Get notifications about VXLAN ports that come up
6580 * @netdev: This physical port's netdev
6581 * @sa_family: Socket Family that VXLAN is notifying us about
6582 * @port: New UDP port number that VXLAN started listening to
6583 **/
6584static void i40e_add_vxlan_port(struct net_device *netdev,
6585 sa_family_t sa_family, __be16 port)
6586{
6587 struct i40e_netdev_priv *np = netdev_priv(netdev);
6588 struct i40e_vsi *vsi = np->vsi;
6589 struct i40e_pf *pf = vsi->back;
6590 u8 next_idx;
6591 u8 idx;
6592
6593 if (sa_family == AF_INET6)
6594 return;
6595
6596 idx = i40e_get_vxlan_port_idx(pf, port);
6597
6598 /* Check if port already exists */
6599 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6600 netdev_info(netdev, "Port %d already offloaded\n", ntohs(port));
6601 return;
6602 }
6603
6604 /* Now check if there is space to add the new port */
6605 next_idx = i40e_get_vxlan_port_idx(pf, 0);
6606
6607 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6608 netdev_info(netdev, "Maximum number of UDP ports reached, not adding port %d\n",
6609 ntohs(port));
6610 return;
6611 }
6612
6613 /* New port: add it and mark its index in the bitmap */
6614 pf->vxlan_ports[next_idx] = port;
6615 pf->pending_vxlan_bitmap |= (1 << next_idx);
6616
6617 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
6618}
6619
6620/**
6621 * i40e_del_vxlan_port - Get notifications about VXLAN ports that go away
6622 * @netdev: This physical port's netdev
6623 * @sa_family: Socket Family that VXLAN is notifying us about
6624 * @port: UDP port number that VXLAN stopped listening to
6625 **/
6626static void i40e_del_vxlan_port(struct net_device *netdev,
6627 sa_family_t sa_family, __be16 port)
6628{
6629 struct i40e_netdev_priv *np = netdev_priv(netdev);
6630 struct i40e_vsi *vsi = np->vsi;
6631 struct i40e_pf *pf = vsi->back;
6632 u8 idx;
6633
6634 if (sa_family == AF_INET6)
6635 return;
6636
6637 idx = i40e_get_vxlan_port_idx(pf, port);
6638
6639 /* Check if port already exists */
6640 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6641 /* if port exists, set it to 0 (mark for deletion)
6642 * and make it pending
6643 */
6644 pf->vxlan_ports[idx] = 0;
6645
6646 pf->pending_vxlan_bitmap |= (1 << idx);
6647
6648 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
6649 } else {
6650 netdev_warn(netdev, "Port %d was not found, not deleting\n",
6651 ntohs(port));
6652 }
6653}
6654
6655#endif
Greg Rose4ba0dea2014-03-06 08:59:55 +00006656#ifdef HAVE_FDB_OPS
6657#ifdef USE_CONST_DEV_UC_CHAR
6658static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
6659 struct net_device *dev,
6660 const unsigned char *addr,
6661 u16 flags)
6662#else
6663static int i40e_ndo_fdb_add(struct ndmsg *ndm,
6664 struct net_device *dev,
6665 unsigned char *addr,
6666 u16 flags)
6667#endif
6668{
6669 struct i40e_netdev_priv *np = netdev_priv(dev);
6670 struct i40e_pf *pf = np->vsi->back;
6671 int err = 0;
6672
6673 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
6674 return -EOPNOTSUPP;
6675
6676 /* Hardware does not support aging addresses so if a
6677 * ndm_state is given only allow permanent addresses
6678 */
6679 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
6680 netdev_info(dev, "FDB only supports static addresses\n");
6681 return -EINVAL;
6682 }
6683
6684 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
6685 err = dev_uc_add_excl(dev, addr);
6686 else if (is_multicast_ether_addr(addr))
6687 err = dev_mc_add_excl(dev, addr);
6688 else
6689 err = -EINVAL;
6690
6691 /* Only return duplicate errors if NLM_F_EXCL is set */
6692 if (err == -EEXIST && !(flags & NLM_F_EXCL))
6693 err = 0;
6694
6695 return err;
6696}
6697
6698#ifndef USE_DEFAULT_FDB_DEL_DUMP
6699#ifdef USE_CONST_DEV_UC_CHAR
6700static int i40e_ndo_fdb_del(struct ndmsg *ndm,
6701 struct net_device *dev,
6702 const unsigned char *addr)
6703#else
6704static int i40e_ndo_fdb_del(struct ndmsg *ndm,
6705 struct net_device *dev,
6706 unsigned char *addr)
6707#endif
6708{
6709 struct i40e_netdev_priv *np = netdev_priv(dev);
6710 struct i40e_pf *pf = np->vsi->back;
6711 int err = -EOPNOTSUPP;
6712
6713 if (ndm->ndm_state & NUD_PERMANENT) {
6714 netdev_info(dev, "FDB only supports static addresses\n");
6715 return -EINVAL;
6716 }
6717
6718 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
6719 if (is_unicast_ether_addr(addr))
6720 err = dev_uc_del(dev, addr);
6721 else if (is_multicast_ether_addr(addr))
6722 err = dev_mc_del(dev, addr);
6723 else
6724 err = -EINVAL;
6725 }
6726
6727 return err;
6728}
6729
6730static int i40e_ndo_fdb_dump(struct sk_buff *skb,
6731 struct netlink_callback *cb,
6732 struct net_device *dev,
6733 int idx)
6734{
6735 struct i40e_netdev_priv *np = netdev_priv(dev);
6736 struct i40e_pf *pf = np->vsi->back;
6737
6738 if (pf->flags & I40E_FLAG_SRIOV_ENABLED)
6739 idx = ndo_dflt_fdb_dump(skb, cb, dev, idx);
6740
6741 return idx;
6742}
6743
6744#endif /* USE_DEFAULT_FDB_DEL_DUMP */
6745#endif /* HAVE_FDB_OPS */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006746static const struct net_device_ops i40e_netdev_ops = {
6747 .ndo_open = i40e_open,
6748 .ndo_stop = i40e_close,
6749 .ndo_start_xmit = i40e_lan_xmit_frame,
6750 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
6751 .ndo_set_rx_mode = i40e_set_rx_mode,
6752 .ndo_validate_addr = eth_validate_addr,
6753 .ndo_set_mac_address = i40e_set_mac,
6754 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006755 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006756 .ndo_tx_timeout = i40e_tx_timeout,
6757 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
6758 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
6759#ifdef CONFIG_NET_POLL_CONTROLLER
6760 .ndo_poll_controller = i40e_netpoll,
6761#endif
6762 .ndo_setup_tc = i40e_setup_tc,
6763 .ndo_set_features = i40e_set_features,
6764 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
6765 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04006766 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006767 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00006768 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006769#ifdef CONFIG_I40E_VXLAN
6770 .ndo_add_vxlan_port = i40e_add_vxlan_port,
6771 .ndo_del_vxlan_port = i40e_del_vxlan_port,
6772#endif
Greg Rose4ba0dea2014-03-06 08:59:55 +00006773#ifdef HAVE_FDB_OPS
6774 .ndo_fdb_add = i40e_ndo_fdb_add,
6775#ifndef USE_DEFAULT_FDB_DEL_DUMP
6776 .ndo_fdb_del = i40e_ndo_fdb_del,
6777 .ndo_fdb_dump = i40e_ndo_fdb_dump,
6778#endif
6779#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006780};
6781
6782/**
6783 * i40e_config_netdev - Setup the netdev flags
6784 * @vsi: the VSI being configured
6785 *
6786 * Returns 0 on success, negative value on failure
6787 **/
6788static int i40e_config_netdev(struct i40e_vsi *vsi)
6789{
Greg Rose1a103702013-11-28 06:42:39 +00006790 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006791 struct i40e_pf *pf = vsi->back;
6792 struct i40e_hw *hw = &pf->hw;
6793 struct i40e_netdev_priv *np;
6794 struct net_device *netdev;
6795 u8 mac_addr[ETH_ALEN];
6796 int etherdev_size;
6797
6798 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006799 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006800 if (!netdev)
6801 return -ENOMEM;
6802
6803 vsi->netdev = netdev;
6804 np = netdev_priv(netdev);
6805 np->vsi = vsi;
6806
Or Gerlitzd70e9412014-03-18 10:36:45 +02006807 netdev->hw_enc_features |= NETIF_F_IP_CSUM |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006808 NETIF_F_GSO_UDP_TUNNEL |
Or Gerlitzd70e9412014-03-18 10:36:45 +02006809 NETIF_F_TSO;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006810
6811 netdev->features = NETIF_F_SG |
6812 NETIF_F_IP_CSUM |
6813 NETIF_F_SCTP_CSUM |
6814 NETIF_F_HIGHDMA |
6815 NETIF_F_GSO_UDP_TUNNEL |
6816 NETIF_F_HW_VLAN_CTAG_TX |
6817 NETIF_F_HW_VLAN_CTAG_RX |
6818 NETIF_F_HW_VLAN_CTAG_FILTER |
6819 NETIF_F_IPV6_CSUM |
6820 NETIF_F_TSO |
Jesse Brandeburg059dab62014-04-01 09:07:20 +00006821 NETIF_F_TSO_ECN |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006822 NETIF_F_TSO6 |
6823 NETIF_F_RXCSUM |
6824 NETIF_F_RXHASH |
6825 0;
6826
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00006827 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
6828 netdev->features |= NETIF_F_NTUPLE;
6829
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006830 /* copy netdev features into list of user selectable features */
6831 netdev->hw_features |= netdev->features;
6832
6833 if (vsi->type == I40E_VSI_MAIN) {
6834 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
6835 memcpy(mac_addr, hw->mac.perm_addr, ETH_ALEN);
6836 } else {
6837 /* relate the VSI_VMDQ name to the VSI_MAIN name */
6838 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
6839 pf->vsi[pf->lan_vsi]->netdev->name);
6840 random_ether_addr(mac_addr);
6841 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
6842 }
Greg Rose1a103702013-11-28 06:42:39 +00006843 i40e_add_filter(vsi, brdcast, I40E_VLAN_ANY, false, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006844
6845 memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
6846 memcpy(netdev->perm_addr, mac_addr, ETH_ALEN);
6847 /* vlan gets same features (except vlan offload)
6848 * after any tweaks for specific VSI types
6849 */
6850 netdev->vlan_features = netdev->features & ~(NETIF_F_HW_VLAN_CTAG_TX |
6851 NETIF_F_HW_VLAN_CTAG_RX |
6852 NETIF_F_HW_VLAN_CTAG_FILTER);
6853 netdev->priv_flags |= IFF_UNICAST_FLT;
6854 netdev->priv_flags |= IFF_SUPP_NOFCS;
6855 /* Setup netdev TC information */
6856 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
6857
6858 netdev->netdev_ops = &i40e_netdev_ops;
6859 netdev->watchdog_timeo = 5 * HZ;
6860 i40e_set_ethtool_ops(netdev);
6861
6862 return 0;
6863}
6864
6865/**
6866 * i40e_vsi_delete - Delete a VSI from the switch
6867 * @vsi: the VSI being removed
6868 *
6869 * Returns 0 on success, negative value on failure
6870 **/
6871static void i40e_vsi_delete(struct i40e_vsi *vsi)
6872{
6873 /* remove default VSI is not allowed */
6874 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
6875 return;
6876
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006877 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006878}
6879
6880/**
6881 * i40e_add_vsi - Add a VSI to the switch
6882 * @vsi: the VSI being configured
6883 *
6884 * This initializes a VSI context depending on the VSI type to be added and
6885 * passes it down to the add_vsi aq command.
6886 **/
6887static int i40e_add_vsi(struct i40e_vsi *vsi)
6888{
6889 int ret = -ENODEV;
6890 struct i40e_mac_filter *f, *ftmp;
6891 struct i40e_pf *pf = vsi->back;
6892 struct i40e_hw *hw = &pf->hw;
6893 struct i40e_vsi_context ctxt;
6894 u8 enabled_tc = 0x1; /* TC0 enabled */
6895 int f_count = 0;
6896
6897 memset(&ctxt, 0, sizeof(ctxt));
6898 switch (vsi->type) {
6899 case I40E_VSI_MAIN:
6900 /* The PF's main VSI is already setup as part of the
6901 * device initialization, so we'll not bother with
6902 * the add_vsi call, but we will retrieve the current
6903 * VSI context.
6904 */
6905 ctxt.seid = pf->main_vsi_seid;
6906 ctxt.pf_num = pf->hw.pf_id;
6907 ctxt.vf_num = 0;
6908 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6909 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6910 if (ret) {
6911 dev_info(&pf->pdev->dev,
6912 "couldn't get pf vsi config, err %d, aq_err %d\n",
6913 ret, pf->hw.aq.asq_last_status);
6914 return -ENOENT;
6915 }
6916 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
6917 vsi->info.valid_sections = 0;
6918
6919 vsi->seid = ctxt.seid;
6920 vsi->id = ctxt.vsi_number;
6921
6922 enabled_tc = i40e_pf_get_tc_map(pf);
6923
6924 /* MFP mode setup queue map and update VSI */
6925 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
6926 memset(&ctxt, 0, sizeof(ctxt));
6927 ctxt.seid = pf->main_vsi_seid;
6928 ctxt.pf_num = pf->hw.pf_id;
6929 ctxt.vf_num = 0;
6930 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
6931 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
6932 if (ret) {
6933 dev_info(&pf->pdev->dev,
6934 "update vsi failed, aq_err=%d\n",
6935 pf->hw.aq.asq_last_status);
6936 ret = -ENOENT;
6937 goto err;
6938 }
6939 /* update the local VSI info queue map */
6940 i40e_vsi_update_queue_map(vsi, &ctxt);
6941 vsi->info.valid_sections = 0;
6942 } else {
6943 /* Default/Main VSI is only enabled for TC0
6944 * reconfigure it to enable all TCs that are
6945 * available on the port in SFP mode.
6946 */
6947 ret = i40e_vsi_config_tc(vsi, enabled_tc);
6948 if (ret) {
6949 dev_info(&pf->pdev->dev,
6950 "failed to configure TCs for main VSI tc_map 0x%08x, err %d, aq_err %d\n",
6951 enabled_tc, ret,
6952 pf->hw.aq.asq_last_status);
6953 ret = -ENOENT;
6954 }
6955 }
6956 break;
6957
6958 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006959 ctxt.pf_num = hw->pf_id;
6960 ctxt.vf_num = 0;
6961 ctxt.uplink_seid = vsi->uplink_seid;
6962 ctxt.connection_type = 0x1; /* regular data port */
6963 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006964 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006965 break;
6966
6967 case I40E_VSI_VMDQ2:
6968 ctxt.pf_num = hw->pf_id;
6969 ctxt.vf_num = 0;
6970 ctxt.uplink_seid = vsi->uplink_seid;
6971 ctxt.connection_type = 0x1; /* regular data port */
6972 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6973
6974 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6975
6976 /* This VSI is connected to VEB so the switch_id
6977 * should be set to zero by default.
6978 */
6979 ctxt.info.switch_id = 0;
6980 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
6981 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6982
6983 /* Setup the VSI tx/rx queue map for TC0 only for now */
6984 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
6985 break;
6986
6987 case I40E_VSI_SRIOV:
6988 ctxt.pf_num = hw->pf_id;
6989 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
6990 ctxt.uplink_seid = vsi->uplink_seid;
6991 ctxt.connection_type = 0x1; /* regular data port */
6992 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
6993
6994 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6995
6996 /* This VSI is connected to VEB so the switch_id
6997 * should be set to zero by default.
6998 */
6999 ctxt.info.switch_id = cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
7000
7001 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
7002 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
7003 /* Setup the VSI tx/rx queue map for TC0 only for now */
7004 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
7005 break;
7006
7007 default:
7008 return -ENODEV;
7009 }
7010
7011 if (vsi->type != I40E_VSI_MAIN) {
7012 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
7013 if (ret) {
7014 dev_info(&vsi->back->pdev->dev,
7015 "add vsi failed, aq_err=%d\n",
7016 vsi->back->hw.aq.asq_last_status);
7017 ret = -ENOENT;
7018 goto err;
7019 }
7020 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
7021 vsi->info.valid_sections = 0;
7022 vsi->seid = ctxt.seid;
7023 vsi->id = ctxt.vsi_number;
7024 }
7025
7026 /* If macvlan filters already exist, force them to get loaded */
7027 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
7028 f->changed = true;
7029 f_count++;
7030 }
7031 if (f_count) {
7032 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
7033 pf->flags |= I40E_FLAG_FILTER_SYNC;
7034 }
7035
7036 /* Update VSI BW information */
7037 ret = i40e_vsi_get_bw_info(vsi);
7038 if (ret) {
7039 dev_info(&pf->pdev->dev,
7040 "couldn't get vsi bw info, err %d, aq_err %d\n",
7041 ret, pf->hw.aq.asq_last_status);
7042 /* VSI is already added so not tearing that up */
7043 ret = 0;
7044 }
7045
7046err:
7047 return ret;
7048}
7049
7050/**
7051 * i40e_vsi_release - Delete a VSI and free its resources
7052 * @vsi: the VSI being removed
7053 *
7054 * Returns 0 on success or < 0 on error
7055 **/
7056int i40e_vsi_release(struct i40e_vsi *vsi)
7057{
7058 struct i40e_mac_filter *f, *ftmp;
7059 struct i40e_veb *veb = NULL;
7060 struct i40e_pf *pf;
7061 u16 uplink_seid;
7062 int i, n;
7063
7064 pf = vsi->back;
7065
7066 /* release of a VEB-owner or last VSI is not allowed */
7067 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
7068 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
7069 vsi->seid, vsi->uplink_seid);
7070 return -ENODEV;
7071 }
7072 if (vsi == pf->vsi[pf->lan_vsi] &&
7073 !test_bit(__I40E_DOWN, &pf->state)) {
7074 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
7075 return -ENODEV;
7076 }
7077
7078 uplink_seid = vsi->uplink_seid;
7079 if (vsi->type != I40E_VSI_SRIOV) {
7080 if (vsi->netdev_registered) {
7081 vsi->netdev_registered = false;
7082 if (vsi->netdev) {
7083 /* results in a call to i40e_close() */
7084 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007085 }
7086 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00007087 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007088 }
7089 i40e_vsi_disable_irq(vsi);
7090 }
7091
7092 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
7093 i40e_del_filter(vsi, f->macaddr, f->vlan,
7094 f->is_vf, f->is_netdev);
7095 i40e_sync_vsi_filters(vsi);
7096
7097 i40e_vsi_delete(vsi);
7098 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00007099 if (vsi->netdev) {
7100 free_netdev(vsi->netdev);
7101 vsi->netdev = NULL;
7102 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007103 i40e_vsi_clear_rings(vsi);
7104 i40e_vsi_clear(vsi);
7105
7106 /* If this was the last thing on the VEB, except for the
7107 * controlling VSI, remove the VEB, which puts the controlling
7108 * VSI onto the next level down in the switch.
7109 *
7110 * Well, okay, there's one more exception here: don't remove
7111 * the orphan VEBs yet. We'll wait for an explicit remove request
7112 * from up the network stack.
7113 */
7114 for (n = 0, i = 0; i < pf->hw.func_caps.num_vsis; i++) {
7115 if (pf->vsi[i] &&
7116 pf->vsi[i]->uplink_seid == uplink_seid &&
7117 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
7118 n++; /* count the VSIs */
7119 }
7120 }
7121 for (i = 0; i < I40E_MAX_VEB; i++) {
7122 if (!pf->veb[i])
7123 continue;
7124 if (pf->veb[i]->uplink_seid == uplink_seid)
7125 n++; /* count the VEBs */
7126 if (pf->veb[i]->seid == uplink_seid)
7127 veb = pf->veb[i];
7128 }
7129 if (n == 0 && veb && veb->uplink_seid != 0)
7130 i40e_veb_release(veb);
7131
7132 return 0;
7133}
7134
7135/**
7136 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
7137 * @vsi: ptr to the VSI
7138 *
7139 * This should only be called after i40e_vsi_mem_alloc() which allocates the
7140 * corresponding SW VSI structure and initializes num_queue_pairs for the
7141 * newly allocated VSI.
7142 *
7143 * Returns 0 on success or negative on failure
7144 **/
7145static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
7146{
7147 int ret = -ENOENT;
7148 struct i40e_pf *pf = vsi->back;
7149
Alexander Duyck493fb302013-09-28 07:01:44 +00007150 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007151 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
7152 vsi->seid);
7153 return -EEXIST;
7154 }
7155
7156 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00007157 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007158 vsi->seid, vsi->base_vector);
7159 return -EEXIST;
7160 }
7161
Greg Rose90e04072014-03-06 08:59:57 +00007162 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007163 if (ret) {
7164 dev_info(&pf->pdev->dev,
7165 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
7166 vsi->num_q_vectors, vsi->seid, ret);
7167 vsi->num_q_vectors = 0;
7168 goto vector_setup_out;
7169 }
7170
Shannon Nelson958a3e32013-09-28 07:13:28 +00007171 if (vsi->num_q_vectors)
7172 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
7173 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007174 if (vsi->base_vector < 0) {
7175 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00007176 "failed to get queue tracking for VSI %d, err=%d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007177 vsi->seid, vsi->base_vector);
7178 i40e_vsi_free_q_vectors(vsi);
7179 ret = -ENOENT;
7180 goto vector_setup_out;
7181 }
7182
7183vector_setup_out:
7184 return ret;
7185}
7186
7187/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007188 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
7189 * @vsi: pointer to the vsi.
7190 *
7191 * This re-allocates a vsi's queue resources.
7192 *
7193 * Returns pointer to the successfully allocated and configured VSI sw struct
7194 * on success, otherwise returns NULL on failure.
7195 **/
7196static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
7197{
7198 struct i40e_pf *pf = vsi->back;
7199 u8 enabled_tc;
7200 int ret;
7201
7202 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7203 i40e_vsi_clear_rings(vsi);
7204
7205 i40e_vsi_free_arrays(vsi, false);
7206 i40e_set_num_rings_in_vsi(vsi);
7207 ret = i40e_vsi_alloc_arrays(vsi, false);
7208 if (ret)
7209 goto err_vsi;
7210
7211 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
7212 if (ret < 0) {
7213 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
7214 vsi->seid, ret);
7215 goto err_vsi;
7216 }
7217 vsi->base_queue = ret;
7218
7219 /* Update the FW view of the VSI. Force a reset of TC and queue
7220 * layout configurations.
7221 */
7222 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
7223 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
7224 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
7225 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
7226
7227 /* assign it some queues */
7228 ret = i40e_alloc_rings(vsi);
7229 if (ret)
7230 goto err_rings;
7231
7232 /* map all of the rings to the q_vectors */
7233 i40e_vsi_map_rings_to_vectors(vsi);
7234 return vsi;
7235
7236err_rings:
7237 i40e_vsi_free_q_vectors(vsi);
7238 if (vsi->netdev_registered) {
7239 vsi->netdev_registered = false;
7240 unregister_netdev(vsi->netdev);
7241 free_netdev(vsi->netdev);
7242 vsi->netdev = NULL;
7243 }
7244 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
7245err_vsi:
7246 i40e_vsi_clear(vsi);
7247 return NULL;
7248}
7249
7250/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007251 * i40e_vsi_setup - Set up a VSI by a given type
7252 * @pf: board private structure
7253 * @type: VSI type
7254 * @uplink_seid: the switch element to link to
7255 * @param1: usage depends upon VSI type. For VF types, indicates VF id
7256 *
7257 * This allocates the sw VSI structure and its queue resources, then add a VSI
7258 * to the identified VEB.
7259 *
7260 * Returns pointer to the successfully allocated and configure VSI sw struct on
7261 * success, otherwise returns NULL on failure.
7262 **/
7263struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
7264 u16 uplink_seid, u32 param1)
7265{
7266 struct i40e_vsi *vsi = NULL;
7267 struct i40e_veb *veb = NULL;
7268 int ret, i;
7269 int v_idx;
7270
7271 /* The requested uplink_seid must be either
7272 * - the PF's port seid
7273 * no VEB is needed because this is the PF
7274 * or this is a Flow Director special case VSI
7275 * - seid of an existing VEB
7276 * - seid of a VSI that owns an existing VEB
7277 * - seid of a VSI that doesn't own a VEB
7278 * a new VEB is created and the VSI becomes the owner
7279 * - seid of the PF VSI, which is what creates the first VEB
7280 * this is a special case of the previous
7281 *
7282 * Find which uplink_seid we were given and create a new VEB if needed
7283 */
7284 for (i = 0; i < I40E_MAX_VEB; i++) {
7285 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
7286 veb = pf->veb[i];
7287 break;
7288 }
7289 }
7290
7291 if (!veb && uplink_seid != pf->mac_seid) {
7292
7293 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
7294 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
7295 vsi = pf->vsi[i];
7296 break;
7297 }
7298 }
7299 if (!vsi) {
7300 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
7301 uplink_seid);
7302 return NULL;
7303 }
7304
7305 if (vsi->uplink_seid == pf->mac_seid)
7306 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
7307 vsi->tc_config.enabled_tc);
7308 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
7309 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
7310 vsi->tc_config.enabled_tc);
7311
7312 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
7313 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
7314 veb = pf->veb[i];
7315 }
7316 if (!veb) {
7317 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
7318 return NULL;
7319 }
7320
7321 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
7322 uplink_seid = veb->seid;
7323 }
7324
7325 /* get vsi sw struct */
7326 v_idx = i40e_vsi_mem_alloc(pf, type);
7327 if (v_idx < 0)
7328 goto err_alloc;
7329 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007330 if (!vsi)
7331 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007332 vsi->type = type;
7333 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
7334
7335 if (type == I40E_VSI_MAIN)
7336 pf->lan_vsi = v_idx;
7337 else if (type == I40E_VSI_SRIOV)
7338 vsi->vf_id = param1;
7339 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007340 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
7341 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007342 if (ret < 0) {
7343 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
7344 vsi->seid, ret);
7345 goto err_vsi;
7346 }
7347 vsi->base_queue = ret;
7348
7349 /* get a VSI from the hardware */
7350 vsi->uplink_seid = uplink_seid;
7351 ret = i40e_add_vsi(vsi);
7352 if (ret)
7353 goto err_vsi;
7354
7355 switch (vsi->type) {
7356 /* setup the netdev if needed */
7357 case I40E_VSI_MAIN:
7358 case I40E_VSI_VMDQ2:
7359 ret = i40e_config_netdev(vsi);
7360 if (ret)
7361 goto err_netdev;
7362 ret = register_netdev(vsi->netdev);
7363 if (ret)
7364 goto err_netdev;
7365 vsi->netdev_registered = true;
7366 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007367#ifdef CONFIG_I40E_DCB
7368 /* Setup DCB netlink interface */
7369 i40e_dcbnl_setup(vsi);
7370#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007371 /* fall through */
7372
7373 case I40E_VSI_FDIR:
7374 /* set up vectors and rings if needed */
7375 ret = i40e_vsi_setup_vectors(vsi);
7376 if (ret)
7377 goto err_msix;
7378
7379 ret = i40e_alloc_rings(vsi);
7380 if (ret)
7381 goto err_rings;
7382
7383 /* map all of the rings to the q_vectors */
7384 i40e_vsi_map_rings_to_vectors(vsi);
7385
7386 i40e_vsi_reset_stats(vsi);
7387 break;
7388
7389 default:
7390 /* no netdev or rings for the other VSI types */
7391 break;
7392 }
7393
7394 return vsi;
7395
7396err_rings:
7397 i40e_vsi_free_q_vectors(vsi);
7398err_msix:
7399 if (vsi->netdev_registered) {
7400 vsi->netdev_registered = false;
7401 unregister_netdev(vsi->netdev);
7402 free_netdev(vsi->netdev);
7403 vsi->netdev = NULL;
7404 }
7405err_netdev:
7406 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
7407err_vsi:
7408 i40e_vsi_clear(vsi);
7409err_alloc:
7410 return NULL;
7411}
7412
7413/**
7414 * i40e_veb_get_bw_info - Query VEB BW information
7415 * @veb: the veb to query
7416 *
7417 * Query the Tx scheduler BW configuration data for given VEB
7418 **/
7419static int i40e_veb_get_bw_info(struct i40e_veb *veb)
7420{
7421 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
7422 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
7423 struct i40e_pf *pf = veb->pf;
7424 struct i40e_hw *hw = &pf->hw;
7425 u32 tc_bw_max;
7426 int ret = 0;
7427 int i;
7428
7429 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
7430 &bw_data, NULL);
7431 if (ret) {
7432 dev_info(&pf->pdev->dev,
7433 "query veb bw config failed, aq_err=%d\n",
7434 hw->aq.asq_last_status);
7435 goto out;
7436 }
7437
7438 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
7439 &ets_data, NULL);
7440 if (ret) {
7441 dev_info(&pf->pdev->dev,
7442 "query veb bw ets config failed, aq_err=%d\n",
7443 hw->aq.asq_last_status);
7444 goto out;
7445 }
7446
7447 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
7448 veb->bw_max_quanta = ets_data.tc_bw_max;
7449 veb->is_abs_credits = bw_data.absolute_credits_enable;
7450 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
7451 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
7452 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7453 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
7454 veb->bw_tc_limit_credits[i] =
7455 le16_to_cpu(bw_data.tc_bw_limits[i]);
7456 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
7457 }
7458
7459out:
7460 return ret;
7461}
7462
7463/**
7464 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
7465 * @pf: board private structure
7466 *
7467 * On error: returns error code (negative)
7468 * On success: returns vsi index in PF (positive)
7469 **/
7470static int i40e_veb_mem_alloc(struct i40e_pf *pf)
7471{
7472 int ret = -ENOENT;
7473 struct i40e_veb *veb;
7474 int i;
7475
7476 /* Need to protect the allocation of switch elements at the PF level */
7477 mutex_lock(&pf->switch_mutex);
7478
7479 /* VEB list may be fragmented if VEB creation/destruction has
7480 * been happening. We can afford to do a quick scan to look
7481 * for any free slots in the list.
7482 *
7483 * find next empty veb slot, looping back around if necessary
7484 */
7485 i = 0;
7486 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
7487 i++;
7488 if (i >= I40E_MAX_VEB) {
7489 ret = -ENOMEM;
7490 goto err_alloc_veb; /* out of VEB slots! */
7491 }
7492
7493 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
7494 if (!veb) {
7495 ret = -ENOMEM;
7496 goto err_alloc_veb;
7497 }
7498 veb->pf = pf;
7499 veb->idx = i;
7500 veb->enabled_tc = 1;
7501
7502 pf->veb[i] = veb;
7503 ret = i;
7504err_alloc_veb:
7505 mutex_unlock(&pf->switch_mutex);
7506 return ret;
7507}
7508
7509/**
7510 * i40e_switch_branch_release - Delete a branch of the switch tree
7511 * @branch: where to start deleting
7512 *
7513 * This uses recursion to find the tips of the branch to be
7514 * removed, deleting until we get back to and can delete this VEB.
7515 **/
7516static void i40e_switch_branch_release(struct i40e_veb *branch)
7517{
7518 struct i40e_pf *pf = branch->pf;
7519 u16 branch_seid = branch->seid;
7520 u16 veb_idx = branch->idx;
7521 int i;
7522
7523 /* release any VEBs on this VEB - RECURSION */
7524 for (i = 0; i < I40E_MAX_VEB; i++) {
7525 if (!pf->veb[i])
7526 continue;
7527 if (pf->veb[i]->uplink_seid == branch->seid)
7528 i40e_switch_branch_release(pf->veb[i]);
7529 }
7530
7531 /* Release the VSIs on this VEB, but not the owner VSI.
7532 *
7533 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
7534 * the VEB itself, so don't use (*branch) after this loop.
7535 */
7536 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
7537 if (!pf->vsi[i])
7538 continue;
7539 if (pf->vsi[i]->uplink_seid == branch_seid &&
7540 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
7541 i40e_vsi_release(pf->vsi[i]);
7542 }
7543 }
7544
7545 /* There's one corner case where the VEB might not have been
7546 * removed, so double check it here and remove it if needed.
7547 * This case happens if the veb was created from the debugfs
7548 * commands and no VSIs were added to it.
7549 */
7550 if (pf->veb[veb_idx])
7551 i40e_veb_release(pf->veb[veb_idx]);
7552}
7553
7554/**
7555 * i40e_veb_clear - remove veb struct
7556 * @veb: the veb to remove
7557 **/
7558static void i40e_veb_clear(struct i40e_veb *veb)
7559{
7560 if (!veb)
7561 return;
7562
7563 if (veb->pf) {
7564 struct i40e_pf *pf = veb->pf;
7565
7566 mutex_lock(&pf->switch_mutex);
7567 if (pf->veb[veb->idx] == veb)
7568 pf->veb[veb->idx] = NULL;
7569 mutex_unlock(&pf->switch_mutex);
7570 }
7571
7572 kfree(veb);
7573}
7574
7575/**
7576 * i40e_veb_release - Delete a VEB and free its resources
7577 * @veb: the VEB being removed
7578 **/
7579void i40e_veb_release(struct i40e_veb *veb)
7580{
7581 struct i40e_vsi *vsi = NULL;
7582 struct i40e_pf *pf;
7583 int i, n = 0;
7584
7585 pf = veb->pf;
7586
7587 /* find the remaining VSI and check for extras */
7588 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
7589 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
7590 n++;
7591 vsi = pf->vsi[i];
7592 }
7593 }
7594 if (n != 1) {
7595 dev_info(&pf->pdev->dev,
7596 "can't remove VEB %d with %d VSIs left\n",
7597 veb->seid, n);
7598 return;
7599 }
7600
7601 /* move the remaining VSI to uplink veb */
7602 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
7603 if (veb->uplink_seid) {
7604 vsi->uplink_seid = veb->uplink_seid;
7605 if (veb->uplink_seid == pf->mac_seid)
7606 vsi->veb_idx = I40E_NO_VEB;
7607 else
7608 vsi->veb_idx = veb->veb_idx;
7609 } else {
7610 /* floating VEB */
7611 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
7612 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
7613 }
7614
7615 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
7616 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007617}
7618
7619/**
7620 * i40e_add_veb - create the VEB in the switch
7621 * @veb: the VEB to be instantiated
7622 * @vsi: the controlling VSI
7623 **/
7624static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
7625{
Greg Rose56747262013-11-28 06:39:37 +00007626 bool is_default = false;
Kevin Scotte1c51b952013-11-20 10:02:51 +00007627 bool is_cloud = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007628 int ret;
7629
7630 /* get a VEB from the hardware */
7631 ret = i40e_aq_add_veb(&veb->pf->hw, veb->uplink_seid, vsi->seid,
Kevin Scotte1c51b952013-11-20 10:02:51 +00007632 veb->enabled_tc, is_default,
7633 is_cloud, &veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007634 if (ret) {
7635 dev_info(&veb->pf->pdev->dev,
7636 "couldn't add VEB, err %d, aq_err %d\n",
7637 ret, veb->pf->hw.aq.asq_last_status);
7638 return -EPERM;
7639 }
7640
7641 /* get statistics counter */
7642 ret = i40e_aq_get_veb_parameters(&veb->pf->hw, veb->seid, NULL, NULL,
7643 &veb->stats_idx, NULL, NULL, NULL);
7644 if (ret) {
7645 dev_info(&veb->pf->pdev->dev,
7646 "couldn't get VEB statistics idx, err %d, aq_err %d\n",
7647 ret, veb->pf->hw.aq.asq_last_status);
7648 return -EPERM;
7649 }
7650 ret = i40e_veb_get_bw_info(veb);
7651 if (ret) {
7652 dev_info(&veb->pf->pdev->dev,
7653 "couldn't get VEB bw info, err %d, aq_err %d\n",
7654 ret, veb->pf->hw.aq.asq_last_status);
7655 i40e_aq_delete_element(&veb->pf->hw, veb->seid, NULL);
7656 return -ENOENT;
7657 }
7658
7659 vsi->uplink_seid = veb->seid;
7660 vsi->veb_idx = veb->idx;
7661 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
7662
7663 return 0;
7664}
7665
7666/**
7667 * i40e_veb_setup - Set up a VEB
7668 * @pf: board private structure
7669 * @flags: VEB setup flags
7670 * @uplink_seid: the switch element to link to
7671 * @vsi_seid: the initial VSI seid
7672 * @enabled_tc: Enabled TC bit-map
7673 *
7674 * This allocates the sw VEB structure and links it into the switch
7675 * It is possible and legal for this to be a duplicate of an already
7676 * existing VEB. It is also possible for both uplink and vsi seids
7677 * to be zero, in order to create a floating VEB.
7678 *
7679 * Returns pointer to the successfully allocated VEB sw struct on
7680 * success, otherwise returns NULL on failure.
7681 **/
7682struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
7683 u16 uplink_seid, u16 vsi_seid,
7684 u8 enabled_tc)
7685{
7686 struct i40e_veb *veb, *uplink_veb = NULL;
7687 int vsi_idx, veb_idx;
7688 int ret;
7689
7690 /* if one seid is 0, the other must be 0 to create a floating relay */
7691 if ((uplink_seid == 0 || vsi_seid == 0) &&
7692 (uplink_seid + vsi_seid != 0)) {
7693 dev_info(&pf->pdev->dev,
7694 "one, not both seid's are 0: uplink=%d vsi=%d\n",
7695 uplink_seid, vsi_seid);
7696 return NULL;
7697 }
7698
7699 /* make sure there is such a vsi and uplink */
7700 for (vsi_idx = 0; vsi_idx < pf->hw.func_caps.num_vsis; vsi_idx++)
7701 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
7702 break;
7703 if (vsi_idx >= pf->hw.func_caps.num_vsis && vsi_seid != 0) {
7704 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
7705 vsi_seid);
7706 return NULL;
7707 }
7708
7709 if (uplink_seid && uplink_seid != pf->mac_seid) {
7710 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
7711 if (pf->veb[veb_idx] &&
7712 pf->veb[veb_idx]->seid == uplink_seid) {
7713 uplink_veb = pf->veb[veb_idx];
7714 break;
7715 }
7716 }
7717 if (!uplink_veb) {
7718 dev_info(&pf->pdev->dev,
7719 "uplink seid %d not found\n", uplink_seid);
7720 return NULL;
7721 }
7722 }
7723
7724 /* get veb sw struct */
7725 veb_idx = i40e_veb_mem_alloc(pf);
7726 if (veb_idx < 0)
7727 goto err_alloc;
7728 veb = pf->veb[veb_idx];
7729 veb->flags = flags;
7730 veb->uplink_seid = uplink_seid;
7731 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
7732 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
7733
7734 /* create the VEB in the switch */
7735 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
7736 if (ret)
7737 goto err_veb;
7738
7739 return veb;
7740
7741err_veb:
7742 i40e_veb_clear(veb);
7743err_alloc:
7744 return NULL;
7745}
7746
7747/**
7748 * i40e_setup_pf_switch_element - set pf vars based on switch type
7749 * @pf: board private structure
7750 * @ele: element we are building info from
7751 * @num_reported: total number of elements
7752 * @printconfig: should we print the contents
7753 *
7754 * helper function to assist in extracting a few useful SEID values.
7755 **/
7756static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
7757 struct i40e_aqc_switch_config_element_resp *ele,
7758 u16 num_reported, bool printconfig)
7759{
7760 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
7761 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
7762 u8 element_type = ele->element_type;
7763 u16 seid = le16_to_cpu(ele->seid);
7764
7765 if (printconfig)
7766 dev_info(&pf->pdev->dev,
7767 "type=%d seid=%d uplink=%d downlink=%d\n",
7768 element_type, seid, uplink_seid, downlink_seid);
7769
7770 switch (element_type) {
7771 case I40E_SWITCH_ELEMENT_TYPE_MAC:
7772 pf->mac_seid = seid;
7773 break;
7774 case I40E_SWITCH_ELEMENT_TYPE_VEB:
7775 /* Main VEB? */
7776 if (uplink_seid != pf->mac_seid)
7777 break;
7778 if (pf->lan_veb == I40E_NO_VEB) {
7779 int v;
7780
7781 /* find existing or else empty VEB */
7782 for (v = 0; v < I40E_MAX_VEB; v++) {
7783 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
7784 pf->lan_veb = v;
7785 break;
7786 }
7787 }
7788 if (pf->lan_veb == I40E_NO_VEB) {
7789 v = i40e_veb_mem_alloc(pf);
7790 if (v < 0)
7791 break;
7792 pf->lan_veb = v;
7793 }
7794 }
7795
7796 pf->veb[pf->lan_veb]->seid = seid;
7797 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
7798 pf->veb[pf->lan_veb]->pf = pf;
7799 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
7800 break;
7801 case I40E_SWITCH_ELEMENT_TYPE_VSI:
7802 if (num_reported != 1)
7803 break;
7804 /* This is immediately after a reset so we can assume this is
7805 * the PF's VSI
7806 */
7807 pf->mac_seid = uplink_seid;
7808 pf->pf_seid = downlink_seid;
7809 pf->main_vsi_seid = seid;
7810 if (printconfig)
7811 dev_info(&pf->pdev->dev,
7812 "pf_seid=%d main_vsi_seid=%d\n",
7813 pf->pf_seid, pf->main_vsi_seid);
7814 break;
7815 case I40E_SWITCH_ELEMENT_TYPE_PF:
7816 case I40E_SWITCH_ELEMENT_TYPE_VF:
7817 case I40E_SWITCH_ELEMENT_TYPE_EMP:
7818 case I40E_SWITCH_ELEMENT_TYPE_BMC:
7819 case I40E_SWITCH_ELEMENT_TYPE_PE:
7820 case I40E_SWITCH_ELEMENT_TYPE_PA:
7821 /* ignore these for now */
7822 break;
7823 default:
7824 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
7825 element_type, seid);
7826 break;
7827 }
7828}
7829
7830/**
7831 * i40e_fetch_switch_configuration - Get switch config from firmware
7832 * @pf: board private structure
7833 * @printconfig: should we print the contents
7834 *
7835 * Get the current switch configuration from the device and
7836 * extract a few useful SEID values.
7837 **/
7838int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
7839{
7840 struct i40e_aqc_get_switch_config_resp *sw_config;
7841 u16 next_seid = 0;
7842 int ret = 0;
7843 u8 *aq_buf;
7844 int i;
7845
7846 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
7847 if (!aq_buf)
7848 return -ENOMEM;
7849
7850 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
7851 do {
7852 u16 num_reported, num_total;
7853
7854 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
7855 I40E_AQ_LARGE_BUF,
7856 &next_seid, NULL);
7857 if (ret) {
7858 dev_info(&pf->pdev->dev,
7859 "get switch config failed %d aq_err=%x\n",
7860 ret, pf->hw.aq.asq_last_status);
7861 kfree(aq_buf);
7862 return -ENOENT;
7863 }
7864
7865 num_reported = le16_to_cpu(sw_config->header.num_reported);
7866 num_total = le16_to_cpu(sw_config->header.num_total);
7867
7868 if (printconfig)
7869 dev_info(&pf->pdev->dev,
7870 "header: %d reported %d total\n",
7871 num_reported, num_total);
7872
7873 if (num_reported) {
7874 int sz = sizeof(*sw_config) * num_reported;
7875
7876 kfree(pf->sw_config);
7877 pf->sw_config = kzalloc(sz, GFP_KERNEL);
7878 if (pf->sw_config)
7879 memcpy(pf->sw_config, sw_config, sz);
7880 }
7881
7882 for (i = 0; i < num_reported; i++) {
7883 struct i40e_aqc_switch_config_element_resp *ele =
7884 &sw_config->element[i];
7885
7886 i40e_setup_pf_switch_element(pf, ele, num_reported,
7887 printconfig);
7888 }
7889 } while (next_seid != 0);
7890
7891 kfree(aq_buf);
7892 return ret;
7893}
7894
7895/**
7896 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
7897 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007898 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007899 *
7900 * Returns 0 on success, negative value on failure
7901 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007902static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007903{
Jesse Brandeburg895106a2013-11-26 10:49:16 +00007904 u32 rxfc = 0, txfc = 0, rxfc_reg;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007905 int ret;
7906
7907 /* find out what's out there already */
7908 ret = i40e_fetch_switch_configuration(pf, false);
7909 if (ret) {
7910 dev_info(&pf->pdev->dev,
7911 "couldn't fetch switch config, err %d, aq_err %d\n",
7912 ret, pf->hw.aq.asq_last_status);
7913 return ret;
7914 }
7915 i40e_pf_reset_stats(pf);
7916
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007917 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007918 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007919 struct i40e_vsi *vsi = NULL;
7920 u16 uplink_seid;
7921
7922 /* Set up the PF VSI associated with the PF's main VSI
7923 * that is already in the HW switch
7924 */
7925 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
7926 uplink_seid = pf->veb[pf->lan_veb]->seid;
7927 else
7928 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007929 if (pf->lan_vsi == I40E_NO_VSI)
7930 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
7931 else if (reinit)
7932 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007933 if (!vsi) {
7934 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
7935 i40e_fdir_teardown(pf);
7936 return -EAGAIN;
7937 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007938 } else {
7939 /* force a reset of TC and queue layout configurations */
7940 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
7941 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
7942 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
7943 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
7944 }
7945 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
7946
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007947 i40e_fdir_sb_setup(pf);
7948
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007949 /* Setup static PF queue filter control settings */
7950 ret = i40e_setup_pf_filter_control(pf);
7951 if (ret) {
7952 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
7953 ret);
7954 /* Failure here should not stop continuing other steps */
7955 }
7956
7957 /* enable RSS in the HW, even for only one queue, as the stack can use
7958 * the hash
7959 */
7960 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
7961 i40e_config_rss(pf);
7962
7963 /* fill in link information and enable LSE reporting */
7964 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
7965 i40e_link_event(pf);
7966
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007967 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007968 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
7969 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007970 /* requested_mode is set in probe or by ethtool */
7971 if (!pf->fc_autoneg_status)
7972 goto no_autoneg;
7973
7974 if ((pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX) &&
7975 (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007976 pf->hw.fc.current_mode = I40E_FC_FULL;
7977 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX)
7978 pf->hw.fc.current_mode = I40E_FC_TX_PAUSE;
7979 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX)
7980 pf->hw.fc.current_mode = I40E_FC_RX_PAUSE;
7981 else
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007982 pf->hw.fc.current_mode = I40E_FC_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007983
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007984 /* sync the flow control settings with the auto-neg values */
7985 switch (pf->hw.fc.current_mode) {
7986 case I40E_FC_FULL:
7987 txfc = 1;
7988 rxfc = 1;
7989 break;
7990 case I40E_FC_TX_PAUSE:
7991 txfc = 1;
7992 rxfc = 0;
7993 break;
7994 case I40E_FC_RX_PAUSE:
7995 txfc = 0;
7996 rxfc = 1;
7997 break;
7998 case I40E_FC_NONE:
7999 case I40E_FC_DEFAULT:
8000 txfc = 0;
8001 rxfc = 0;
8002 break;
8003 case I40E_FC_PFC:
8004 /* TBD */
8005 break;
8006 /* no default case, we have to handle all possibilities here */
8007 }
8008
8009 wr32(&pf->hw, I40E_PRTDCB_FCCFG, txfc << I40E_PRTDCB_FCCFG_TFCE_SHIFT);
8010
8011 rxfc_reg = rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
8012 ~I40E_PRTDCB_MFLCN_RFCE_MASK;
8013 rxfc_reg |= (rxfc << I40E_PRTDCB_MFLCN_RFCE_SHIFT);
8014
8015 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rxfc_reg);
8016
8017 goto fc_complete;
8018
8019no_autoneg:
8020 /* disable L2 flow control, user can turn it on if they wish */
8021 wr32(&pf->hw, I40E_PRTDCB_FCCFG, 0);
8022 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
8023 ~I40E_PRTDCB_MFLCN_RFCE_MASK);
8024
8025fc_complete:
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008026 i40e_ptp_init(pf);
8027
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008028 return ret;
8029}
8030
8031/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008032 * i40e_determine_queue_usage - Work out queue distribution
8033 * @pf: board private structure
8034 **/
8035static void i40e_determine_queue_usage(struct i40e_pf *pf)
8036{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008037 int queues_left;
8038
8039 pf->num_lan_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008040
8041 /* Find the max queues to be put into basic use. We'll always be
8042 * using TC0, whether or not DCB is running, and TC0 will get the
8043 * big RSS set.
8044 */
8045 queues_left = pf->hw.func_caps.num_tx_qp;
8046
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008047 if ((queues_left == 1) ||
8048 !(pf->flags & I40E_FLAG_MSIX_ENABLED) ||
8049 !(pf->flags & (I40E_FLAG_RSS_ENABLED | I40E_FLAG_FD_SB_ENABLED |
8050 I40E_FLAG_DCB_ENABLED))) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008051 /* one qp for PF, no queues for anything else */
8052 queues_left = 0;
8053 pf->rss_size = pf->num_lan_qps = 1;
8054
8055 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008056 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
8057 I40E_FLAG_FD_SB_ENABLED |
8058 I40E_FLAG_FD_ATR_ENABLED |
8059 I40E_FLAG_DCB_ENABLED |
8060 I40E_FLAG_SRIOV_ENABLED |
8061 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008062 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008063 /* Not enough queues for all TCs */
8064 if ((pf->flags & I40E_FLAG_DCB_ENABLED) &&
8065 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
8066 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8067 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
8068 }
8069 pf->num_lan_qps = pf->rss_size_max;
8070 queues_left -= pf->num_lan_qps;
8071 }
8072
8073 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8074 if (queues_left > 1) {
8075 queues_left -= 1; /* save 1 queue for FD */
8076 } else {
8077 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8078 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
8079 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008080 }
8081
8082 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
8083 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008084 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
8085 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008086 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
8087 }
8088
8089 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
8090 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
8091 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
8092 (queues_left / pf->num_vmdq_qps));
8093 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
8094 }
8095
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008096 pf->queues_left = queues_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008097}
8098
8099/**
8100 * i40e_setup_pf_filter_control - Setup PF static filter control
8101 * @pf: PF to be setup
8102 *
8103 * i40e_setup_pf_filter_control sets up a pf's initial filter control
8104 * settings. If PE/FCoE are enabled then it will also set the per PF
8105 * based filter sizes required for them. It also enables Flow director,
8106 * ethertype and macvlan type filter settings for the pf.
8107 *
8108 * Returns 0 on success, negative on failure
8109 **/
8110static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
8111{
8112 struct i40e_filter_control_settings *settings = &pf->filter_settings;
8113
8114 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
8115
8116 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008117 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008118 settings->enable_fdir = true;
8119
8120 /* Ethtype and MACVLAN filters enabled for PF */
8121 settings->enable_ethtype = true;
8122 settings->enable_macvlan = true;
8123
8124 if (i40e_set_filter_control(&pf->hw, settings))
8125 return -ENOENT;
8126
8127 return 0;
8128}
8129
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008130#define INFO_STRING_LEN 255
8131static void i40e_print_features(struct i40e_pf *pf)
8132{
8133 struct i40e_hw *hw = &pf->hw;
8134 char *buf, *string;
8135
8136 string = kzalloc(INFO_STRING_LEN, GFP_KERNEL);
8137 if (!string) {
8138 dev_err(&pf->pdev->dev, "Features string allocation failed\n");
8139 return;
8140 }
8141
8142 buf = string;
8143
8144 buf += sprintf(string, "Features: PF-id[%d] ", hw->pf_id);
8145#ifdef CONFIG_PCI_IOV
8146 buf += sprintf(buf, "VFs: %d ", pf->num_req_vfs);
8147#endif
8148 buf += sprintf(buf, "VSIs: %d QP: %d ", pf->hw.func_caps.num_vsis,
8149 pf->vsi[pf->lan_vsi]->num_queue_pairs);
8150
8151 if (pf->flags & I40E_FLAG_RSS_ENABLED)
8152 buf += sprintf(buf, "RSS ");
8153 buf += sprintf(buf, "FDir ");
8154 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
8155 buf += sprintf(buf, "ATR ");
8156 if (pf->flags & I40E_FLAG_FD_SB_ENABLED)
8157 buf += sprintf(buf, "NTUPLE ");
8158 if (pf->flags & I40E_FLAG_DCB_ENABLED)
8159 buf += sprintf(buf, "DCB ");
8160 if (pf->flags & I40E_FLAG_PTP)
8161 buf += sprintf(buf, "PTP ");
8162
8163 BUG_ON(buf > (string + INFO_STRING_LEN));
8164 dev_info(&pf->pdev->dev, "%s\n", string);
8165 kfree(string);
8166}
8167
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008168/**
8169 * i40e_probe - Device initialization routine
8170 * @pdev: PCI device information struct
8171 * @ent: entry in i40e_pci_tbl
8172 *
8173 * i40e_probe initializes a pf identified by a pci_dev structure.
8174 * The OS initialization, configuring of the pf private structure,
8175 * and a hardware reset occur.
8176 *
8177 * Returns 0 on success, negative on failure
8178 **/
8179static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
8180{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008181 struct i40e_pf *pf;
8182 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008183 static u16 pfs_found;
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008184 u16 link_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008185 int err = 0;
8186 u32 len;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008187 u32 i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008188
8189 err = pci_enable_device_mem(pdev);
8190 if (err)
8191 return err;
8192
8193 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +00008194 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00008195 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00008196 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
8197 if (err) {
8198 dev_err(&pdev->dev,
8199 "DMA configuration failed: 0x%x\n", err);
8200 goto err_dma;
8201 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008202 }
8203
8204 /* set up pci connections */
8205 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
8206 IORESOURCE_MEM), i40e_driver_name);
8207 if (err) {
8208 dev_info(&pdev->dev,
8209 "pci_request_selected_regions failed %d\n", err);
8210 goto err_pci_reg;
8211 }
8212
8213 pci_enable_pcie_error_reporting(pdev);
8214 pci_set_master(pdev);
8215
8216 /* Now that we have a PCI connection, we need to do the
8217 * low level device setup. This is primarily setting up
8218 * the Admin Queue structures and then querying for the
8219 * device's current profile information.
8220 */
8221 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
8222 if (!pf) {
8223 err = -ENOMEM;
8224 goto err_pf_alloc;
8225 }
8226 pf->next_vsi = 0;
8227 pf->pdev = pdev;
8228 set_bit(__I40E_DOWN, &pf->state);
8229
8230 hw = &pf->hw;
8231 hw->back = pf;
8232 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
8233 pci_resource_len(pdev, 0));
8234 if (!hw->hw_addr) {
8235 err = -EIO;
8236 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
8237 (unsigned int)pci_resource_start(pdev, 0),
8238 (unsigned int)pci_resource_len(pdev, 0), err);
8239 goto err_ioremap;
8240 }
8241 hw->vendor_id = pdev->vendor;
8242 hw->device_id = pdev->device;
8243 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
8244 hw->subsystem_vendor_id = pdev->subsystem_vendor;
8245 hw->subsystem_device_id = pdev->subsystem_device;
8246 hw->bus.device = PCI_SLOT(pdev->devfn);
8247 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008248 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008249
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008250 /* do a special CORER for clearing PXE mode once at init */
8251 if (hw->revision_id == 0 &&
8252 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
8253 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
8254 i40e_flush(hw);
8255 msleep(200);
8256 pf->corer_count++;
8257
8258 i40e_clear_pxe_mode(hw);
8259 }
8260
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008261 /* Reset here to make sure all is clean and to define PF 'n' */
8262 err = i40e_pf_reset(hw);
8263 if (err) {
8264 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
8265 goto err_pf_reset;
8266 }
8267 pf->pfr_count++;
8268
8269 hw->aq.num_arq_entries = I40E_AQ_LEN;
8270 hw->aq.num_asq_entries = I40E_AQ_LEN;
8271 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
8272 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
8273 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
8274 snprintf(pf->misc_int_name, sizeof(pf->misc_int_name) - 1,
8275 "%s-pf%d:misc",
8276 dev_driver_string(&pf->pdev->dev), pf->hw.pf_id);
8277
8278 err = i40e_init_shared_code(hw);
8279 if (err) {
8280 dev_info(&pdev->dev, "init_shared_code failed: %d\n", err);
8281 goto err_pf_reset;
8282 }
8283
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008284 /* set up a default setting for link flow control */
8285 pf->hw.fc.requested_mode = I40E_FC_NONE;
8286
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008287 err = i40e_init_adminq(hw);
8288 dev_info(&pdev->dev, "%s\n", i40e_fw_version_str(hw));
8289 if (err) {
8290 dev_info(&pdev->dev,
8291 "init_adminq failed: %d expecting API %02x.%02x\n",
8292 err,
8293 I40E_FW_API_VERSION_MAJOR, I40E_FW_API_VERSION_MINOR);
8294 goto err_pf_reset;
8295 }
8296
Shannon Nelson4eb3f762014-03-06 08:59:58 +00008297 i40e_verify_eeprom(pf);
8298
Shannon Nelson6ff4ef82013-12-21 05:44:49 +00008299 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008300 err = i40e_get_capabilities(pf);
8301 if (err)
8302 goto err_adminq_setup;
8303
8304 err = i40e_sw_init(pf);
8305 if (err) {
8306 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
8307 goto err_sw_init;
8308 }
8309
8310 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
8311 hw->func_caps.num_rx_qp,
8312 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
8313 if (err) {
8314 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
8315 goto err_init_lan_hmc;
8316 }
8317
8318 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
8319 if (err) {
8320 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
8321 err = -ENOENT;
8322 goto err_configure_lan_hmc;
8323 }
8324
8325 i40e_get_mac_addr(hw, hw->mac.addr);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +00008326 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008327 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
8328 err = -EIO;
8329 goto err_mac_addr;
8330 }
8331 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
8332 memcpy(hw->mac.perm_addr, hw->mac.addr, ETH_ALEN);
8333
8334 pci_set_drvdata(pdev, pf);
8335 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008336#ifdef CONFIG_I40E_DCB
8337 err = i40e_init_pf_dcb(pf);
8338 if (err) {
8339 dev_info(&pdev->dev, "init_pf_dcb failed: %d\n", err);
8340 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh014269f2014-04-01 07:11:48 +00008341 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008342 }
8343#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008344
8345 /* set up periodic task facility */
8346 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
8347 pf->service_timer_period = HZ;
8348
8349 INIT_WORK(&pf->service_task, i40e_service_task);
8350 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
8351 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
8352 pf->link_check_timeout = jiffies;
8353
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008354 /* WoL defaults to disabled */
8355 pf->wol_en = false;
8356 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
8357
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008358 /* set up the main switch operations */
8359 i40e_determine_queue_usage(pf);
8360 i40e_init_interrupt_scheme(pf);
8361
8362 /* Set up the *vsi struct based on the number of VSIs in the HW,
8363 * and set up our local tracking of the MAIN PF vsi.
8364 */
8365 len = sizeof(struct i40e_vsi *) * pf->hw.func_caps.num_vsis;
8366 pf->vsi = kzalloc(len, GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +00008367 if (!pf->vsi) {
8368 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008369 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +00008370 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008371
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008372 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008373 if (err) {
8374 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
8375 goto err_vsis;
8376 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008377 /* if FDIR VSI was set up, start it now */
8378 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
8379 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
8380 i40e_vsi_open(pf->vsi[i]);
8381 break;
8382 }
8383 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008384
8385 /* The main driver is (mostly) up and happy. We need to set this state
8386 * before setting up the misc vector or we get a race and the vector
8387 * ends up disabled forever.
8388 */
8389 clear_bit(__I40E_DOWN, &pf->state);
8390
8391 /* In case of MSIX we are going to setup the misc vector right here
8392 * to handle admin queue events etc. In case of legacy and MSI
8393 * the misc functionality and queue processing is combined in
8394 * the same vector and that gets setup at open.
8395 */
8396 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
8397 err = i40e_setup_misc_vector(pf);
8398 if (err) {
8399 dev_info(&pdev->dev,
8400 "setup of misc vector failed: %d\n", err);
8401 goto err_vsis;
8402 }
8403 }
8404
Greg Rosedf805f62014-04-04 04:43:16 +00008405#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008406 /* prep for VF support */
8407 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +00008408 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
8409 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008410 u32 val;
8411
8412 /* disable link interrupts for VFs */
8413 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
8414 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
8415 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
8416 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -08008417
8418 if (pci_num_vf(pdev)) {
8419 dev_info(&pdev->dev,
8420 "Active VFs found, allocating resources.\n");
8421 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
8422 if (err)
8423 dev_info(&pdev->dev,
8424 "Error %d allocating resources for existing VFs\n",
8425 err);
8426 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008427 }
Greg Rosedf805f62014-04-04 04:43:16 +00008428#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008429
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008430 pfs_found++;
8431
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008432 i40e_dbg_pf_init(pf);
8433
8434 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00008435 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008436
8437 /* since everything's happy, start the service_task timer */
8438 mod_timer(&pf->service_timer,
8439 round_jiffies(jiffies + pf->service_timer_period));
8440
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008441 /* Get the negotiated link width and speed from PCI config space */
8442 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA, &link_status);
8443
8444 i40e_set_pci_config_data(hw, link_status);
8445
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00008446 dev_info(&pdev->dev, "PCI-Express: %s %s\n",
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008447 (hw->bus.speed == i40e_bus_speed_8000 ? "Speed 8.0GT/s" :
8448 hw->bus.speed == i40e_bus_speed_5000 ? "Speed 5.0GT/s" :
8449 hw->bus.speed == i40e_bus_speed_2500 ? "Speed 2.5GT/s" :
8450 "Unknown"),
8451 (hw->bus.width == i40e_bus_width_pcie_x8 ? "Width x8" :
8452 hw->bus.width == i40e_bus_width_pcie_x4 ? "Width x4" :
8453 hw->bus.width == i40e_bus_width_pcie_x2 ? "Width x2" :
8454 hw->bus.width == i40e_bus_width_pcie_x1 ? "Width x1" :
8455 "Unknown"));
8456
8457 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
8458 hw->bus.speed < i40e_bus_speed_8000) {
8459 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
8460 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
8461 }
8462
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008463 /* print a string summarizing features */
8464 i40e_print_features(pf);
8465
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008466 return 0;
8467
8468 /* Unwind what we've done if something failed in the setup */
8469err_vsis:
8470 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008471 i40e_clear_interrupt_scheme(pf);
8472 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +00008473err_switch_setup:
8474 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008475 del_timer_sync(&pf->service_timer);
8476err_mac_addr:
8477err_configure_lan_hmc:
8478 (void)i40e_shutdown_lan_hmc(hw);
8479err_init_lan_hmc:
8480 kfree(pf->qp_pile);
8481 kfree(pf->irq_pile);
8482err_sw_init:
8483err_adminq_setup:
8484 (void)i40e_shutdown_adminq(hw);
8485err_pf_reset:
8486 iounmap(hw->hw_addr);
8487err_ioremap:
8488 kfree(pf);
8489err_pf_alloc:
8490 pci_disable_pcie_error_reporting(pdev);
8491 pci_release_selected_regions(pdev,
8492 pci_select_bars(pdev, IORESOURCE_MEM));
8493err_pci_reg:
8494err_dma:
8495 pci_disable_device(pdev);
8496 return err;
8497}
8498
8499/**
8500 * i40e_remove - Device removal routine
8501 * @pdev: PCI device information struct
8502 *
8503 * i40e_remove is called by the PCI subsystem to alert the driver
8504 * that is should release a PCI device. This could be caused by a
8505 * Hot-Plug event, or because the driver is going to be removed from
8506 * memory.
8507 **/
8508static void i40e_remove(struct pci_dev *pdev)
8509{
8510 struct i40e_pf *pf = pci_get_drvdata(pdev);
8511 i40e_status ret_code;
8512 u32 reg;
8513 int i;
8514
8515 i40e_dbg_pf_exit(pf);
8516
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008517 i40e_ptp_stop(pf);
8518
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008519 /* no more scheduling of any task */
8520 set_bit(__I40E_DOWN, &pf->state);
8521 del_timer_sync(&pf->service_timer);
8522 cancel_work_sync(&pf->service_task);
8523
Mitch Williamseb2d80b2014-02-13 03:48:48 -08008524 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
8525 i40e_free_vfs(pf);
8526 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
8527 }
8528
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008529 i40e_fdir_teardown(pf);
8530
8531 /* If there is a switch structure or any orphans, remove them.
8532 * This will leave only the PF's VSI remaining.
8533 */
8534 for (i = 0; i < I40E_MAX_VEB; i++) {
8535 if (!pf->veb[i])
8536 continue;
8537
8538 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
8539 pf->veb[i]->uplink_seid == 0)
8540 i40e_switch_branch_release(pf->veb[i]);
8541 }
8542
8543 /* Now we can shutdown the PF's VSI, just before we kill
8544 * adminq and hmc.
8545 */
8546 if (pf->vsi[pf->lan_vsi])
8547 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
8548
8549 i40e_stop_misc_vector(pf);
8550 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
8551 synchronize_irq(pf->msix_entries[0].vector);
8552 free_irq(pf->msix_entries[0].vector, pf);
8553 }
8554
8555 /* shutdown and destroy the HMC */
8556 ret_code = i40e_shutdown_lan_hmc(&pf->hw);
8557 if (ret_code)
8558 dev_warn(&pdev->dev,
8559 "Failed to destroy the HMC resources: %d\n", ret_code);
8560
8561 /* shutdown the adminq */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008562 ret_code = i40e_shutdown_adminq(&pf->hw);
8563 if (ret_code)
8564 dev_warn(&pdev->dev,
8565 "Failed to destroy the Admin Queue resources: %d\n",
8566 ret_code);
8567
8568 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
8569 i40e_clear_interrupt_scheme(pf);
8570 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
8571 if (pf->vsi[i]) {
8572 i40e_vsi_clear_rings(pf->vsi[i]);
8573 i40e_vsi_clear(pf->vsi[i]);
8574 pf->vsi[i] = NULL;
8575 }
8576 }
8577
8578 for (i = 0; i < I40E_MAX_VEB; i++) {
8579 kfree(pf->veb[i]);
8580 pf->veb[i] = NULL;
8581 }
8582
8583 kfree(pf->qp_pile);
8584 kfree(pf->irq_pile);
8585 kfree(pf->sw_config);
8586 kfree(pf->vsi);
8587
8588 /* force a PF reset to clean anything leftover */
8589 reg = rd32(&pf->hw, I40E_PFGEN_CTRL);
8590 wr32(&pf->hw, I40E_PFGEN_CTRL, (reg | I40E_PFGEN_CTRL_PFSWR_MASK));
8591 i40e_flush(&pf->hw);
8592
8593 iounmap(pf->hw.hw_addr);
8594 kfree(pf);
8595 pci_release_selected_regions(pdev,
8596 pci_select_bars(pdev, IORESOURCE_MEM));
8597
8598 pci_disable_pcie_error_reporting(pdev);
8599 pci_disable_device(pdev);
8600}
8601
8602/**
8603 * i40e_pci_error_detected - warning that something funky happened in PCI land
8604 * @pdev: PCI device information struct
8605 *
8606 * Called to warn that something happened and the error handling steps
8607 * are in progress. Allows the driver to quiesce things, be ready for
8608 * remediation.
8609 **/
8610static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
8611 enum pci_channel_state error)
8612{
8613 struct i40e_pf *pf = pci_get_drvdata(pdev);
8614
8615 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
8616
8617 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008618 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
8619 rtnl_lock();
8620 i40e_prep_for_reset(pf);
8621 rtnl_unlock();
8622 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008623
8624 /* Request a slot reset */
8625 return PCI_ERS_RESULT_NEED_RESET;
8626}
8627
8628/**
8629 * i40e_pci_error_slot_reset - a PCI slot reset just happened
8630 * @pdev: PCI device information struct
8631 *
8632 * Called to find if the driver can work with the device now that
8633 * the pci slot has been reset. If a basic connection seems good
8634 * (registers are readable and have sane content) then return a
8635 * happy little PCI_ERS_RESULT_xxx.
8636 **/
8637static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
8638{
8639 struct i40e_pf *pf = pci_get_drvdata(pdev);
8640 pci_ers_result_t result;
8641 int err;
8642 u32 reg;
8643
8644 dev_info(&pdev->dev, "%s\n", __func__);
8645 if (pci_enable_device_mem(pdev)) {
8646 dev_info(&pdev->dev,
8647 "Cannot re-enable PCI device after reset.\n");
8648 result = PCI_ERS_RESULT_DISCONNECT;
8649 } else {
8650 pci_set_master(pdev);
8651 pci_restore_state(pdev);
8652 pci_save_state(pdev);
8653 pci_wake_from_d3(pdev, false);
8654
8655 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8656 if (reg == 0)
8657 result = PCI_ERS_RESULT_RECOVERED;
8658 else
8659 result = PCI_ERS_RESULT_DISCONNECT;
8660 }
8661
8662 err = pci_cleanup_aer_uncorrect_error_status(pdev);
8663 if (err) {
8664 dev_info(&pdev->dev,
8665 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
8666 err);
8667 /* non-fatal, continue */
8668 }
8669
8670 return result;
8671}
8672
8673/**
8674 * i40e_pci_error_resume - restart operations after PCI error recovery
8675 * @pdev: PCI device information struct
8676 *
8677 * Called to allow the driver to bring things back up after PCI error
8678 * and/or reset recovery has finished.
8679 **/
8680static void i40e_pci_error_resume(struct pci_dev *pdev)
8681{
8682 struct i40e_pf *pf = pci_get_drvdata(pdev);
8683
8684 dev_info(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008685 if (test_bit(__I40E_SUSPENDED, &pf->state))
8686 return;
8687
8688 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008689 i40e_handle_reset_warning(pf);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008690 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008691}
8692
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008693/**
8694 * i40e_shutdown - PCI callback for shutting down
8695 * @pdev: PCI device information struct
8696 **/
8697static void i40e_shutdown(struct pci_dev *pdev)
8698{
8699 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008700 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008701
8702 set_bit(__I40E_SUSPENDED, &pf->state);
8703 set_bit(__I40E_DOWN, &pf->state);
8704 rtnl_lock();
8705 i40e_prep_for_reset(pf);
8706 rtnl_unlock();
8707
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008708 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
8709 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
8710
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008711 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008712 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008713 pci_set_power_state(pdev, PCI_D3hot);
8714 }
8715}
8716
8717#ifdef CONFIG_PM
8718/**
8719 * i40e_suspend - PCI callback for moving to D3
8720 * @pdev: PCI device information struct
8721 **/
8722static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
8723{
8724 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008725 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008726
8727 set_bit(__I40E_SUSPENDED, &pf->state);
8728 set_bit(__I40E_DOWN, &pf->state);
8729 rtnl_lock();
8730 i40e_prep_for_reset(pf);
8731 rtnl_unlock();
8732
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008733 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
8734 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
8735
8736 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008737 pci_set_power_state(pdev, PCI_D3hot);
8738
8739 return 0;
8740}
8741
8742/**
8743 * i40e_resume - PCI callback for waking up from D3
8744 * @pdev: PCI device information struct
8745 **/
8746static int i40e_resume(struct pci_dev *pdev)
8747{
8748 struct i40e_pf *pf = pci_get_drvdata(pdev);
8749 u32 err;
8750
8751 pci_set_power_state(pdev, PCI_D0);
8752 pci_restore_state(pdev);
8753 /* pci_restore_state() clears dev->state_saves, so
8754 * call pci_save_state() again to restore it.
8755 */
8756 pci_save_state(pdev);
8757
8758 err = pci_enable_device_mem(pdev);
8759 if (err) {
8760 dev_err(&pdev->dev,
8761 "%s: Cannot enable PCI device from suspend\n",
8762 __func__);
8763 return err;
8764 }
8765 pci_set_master(pdev);
8766
8767 /* no wakeup events while running */
8768 pci_wake_from_d3(pdev, false);
8769
8770 /* handling the reset will rebuild the device state */
8771 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
8772 clear_bit(__I40E_DOWN, &pf->state);
8773 rtnl_lock();
8774 i40e_reset_and_rebuild(pf, false);
8775 rtnl_unlock();
8776 }
8777
8778 return 0;
8779}
8780
8781#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008782static const struct pci_error_handlers i40e_err_handler = {
8783 .error_detected = i40e_pci_error_detected,
8784 .slot_reset = i40e_pci_error_slot_reset,
8785 .resume = i40e_pci_error_resume,
8786};
8787
8788static struct pci_driver i40e_driver = {
8789 .name = i40e_driver_name,
8790 .id_table = i40e_pci_tbl,
8791 .probe = i40e_probe,
8792 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008793#ifdef CONFIG_PM
8794 .suspend = i40e_suspend,
8795 .resume = i40e_resume,
8796#endif
8797 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008798 .err_handler = &i40e_err_handler,
8799 .sriov_configure = i40e_pci_sriov_configure,
8800};
8801
8802/**
8803 * i40e_init_module - Driver registration routine
8804 *
8805 * i40e_init_module is the first routine called when the driver is
8806 * loaded. All it does is register with the PCI subsystem.
8807 **/
8808static int __init i40e_init_module(void)
8809{
8810 pr_info("%s: %s - version %s\n", i40e_driver_name,
8811 i40e_driver_string, i40e_driver_version_str);
8812 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
8813 i40e_dbg_init();
8814 return pci_register_driver(&i40e_driver);
8815}
8816module_init(i40e_init_module);
8817
8818/**
8819 * i40e_exit_module - Driver exit cleanup routine
8820 *
8821 * i40e_exit_module is called just before the driver is removed
8822 * from memory.
8823 **/
8824static void __exit i40e_exit_module(void)
8825{
8826 pci_unregister_driver(&i40e_driver);
8827 i40e_dbg_exit();
8828}
8829module_exit(i40e_exit_module);