blob: 726ecebeb02a810989de0359a3a63e99da465cf7 [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++) {
Matt Jared351499a2014-04-23 04:50:03 +00003186
3187 /* warn the TX unit of coming changes */
3188 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
3189 if (!enable)
3190 udelay(10);
3191
Mitch Williams6c5ef622014-02-20 19:29:16 -08003192 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003193 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003194 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3195 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3196 break;
3197 usleep_range(1000, 2000);
3198 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003199 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c122002014-03-14 07:32:29 +00003200 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00003201 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003202
3203 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003204 if (enable) {
3205 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003206 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003207 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003208 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003209 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003210
3211 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
3212
3213 /* wait for the change to finish */
3214 for (j = 0; j < 10; j++) {
3215 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Catherine Sullivan7c122002014-03-14 07:32:29 +00003216 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3217 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003218
3219 udelay(10);
3220 }
3221 if (j >= 10) {
3222 dev_info(&pf->pdev->dev, "Tx ring %d %sable timeout\n",
3223 pf_q, (enable ? "en" : "dis"));
3224 return -ETIMEDOUT;
3225 }
3226 }
3227
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003228 if (hw->revision_id == 0)
3229 mdelay(50);
3230
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003231 return 0;
3232}
3233
3234/**
3235 * i40e_vsi_control_rx - Start or stop a VSI's rings
3236 * @vsi: the VSI being configured
3237 * @enable: start or stop the rings
3238 **/
3239static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3240{
3241 struct i40e_pf *pf = vsi->back;
3242 struct i40e_hw *hw = &pf->hw;
3243 int i, j, pf_q;
3244 u32 rx_reg;
3245
3246 pf_q = vsi->base_queue;
3247 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003248 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003249 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003250 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
3251 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
3252 break;
3253 usleep_range(1000, 2000);
3254 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003255
Catherine Sullivan7c122002014-03-14 07:32:29 +00003256 /* Skip if the queue is already in the requested state */
3257 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3258 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003259
3260 /* turn on/off the queue */
3261 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08003262 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003263 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08003264 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003265 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
3266
3267 /* wait for the change to finish */
3268 for (j = 0; j < 10; j++) {
3269 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
3270
Catherine Sullivan7c122002014-03-14 07:32:29 +00003271 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3272 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003273
3274 udelay(10);
3275 }
3276 if (j >= 10) {
3277 dev_info(&pf->pdev->dev, "Rx ring %d %sable timeout\n",
3278 pf_q, (enable ? "en" : "dis"));
3279 return -ETIMEDOUT;
3280 }
3281 }
3282
3283 return 0;
3284}
3285
3286/**
3287 * i40e_vsi_control_rings - Start or stop a VSI's rings
3288 * @vsi: the VSI being configured
3289 * @enable: start or stop the rings
3290 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00003291int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003292{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003293 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003294
3295 /* do rx first for enable and last for disable */
3296 if (request) {
3297 ret = i40e_vsi_control_rx(vsi, request);
3298 if (ret)
3299 return ret;
3300 ret = i40e_vsi_control_tx(vsi, request);
3301 } else {
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003302 /* Ignore return value, we need to shutdown whatever we can */
3303 i40e_vsi_control_tx(vsi, request);
3304 i40e_vsi_control_rx(vsi, request);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003305 }
3306
3307 return ret;
3308}
3309
3310/**
3311 * i40e_vsi_free_irq - Free the irq association with the OS
3312 * @vsi: the VSI being configured
3313 **/
3314static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
3315{
3316 struct i40e_pf *pf = vsi->back;
3317 struct i40e_hw *hw = &pf->hw;
3318 int base = vsi->base_vector;
3319 u32 val, qp;
3320 int i;
3321
3322 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3323 if (!vsi->q_vectors)
3324 return;
3325
3326 for (i = 0; i < vsi->num_q_vectors; i++) {
3327 u16 vector = i + base;
3328
3329 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00003330 if (!vsi->q_vectors[i] ||
3331 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003332 continue;
3333
3334 /* clear the affinity_mask in the IRQ descriptor */
3335 irq_set_affinity_hint(pf->msix_entries[vector].vector,
3336 NULL);
3337 free_irq(pf->msix_entries[vector].vector,
Alexander Duyck493fb302013-09-28 07:01:44 +00003338 vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003339
3340 /* Tear down the interrupt queue link list
3341 *
3342 * We know that they come in pairs and always
3343 * the Rx first, then the Tx. To clear the
3344 * link list, stick the EOL value into the
3345 * next_q field of the registers.
3346 */
3347 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
3348 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3349 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3350 val |= I40E_QUEUE_END_OF_LIST
3351 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3352 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
3353
3354 while (qp != I40E_QUEUE_END_OF_LIST) {
3355 u32 next;
3356
3357 val = rd32(hw, I40E_QINT_RQCTL(qp));
3358
3359 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3360 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3361 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3362 I40E_QINT_RQCTL_INTEVENT_MASK);
3363
3364 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3365 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3366
3367 wr32(hw, I40E_QINT_RQCTL(qp), val);
3368
3369 val = rd32(hw, I40E_QINT_TQCTL(qp));
3370
3371 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
3372 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
3373
3374 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3375 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3376 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3377 I40E_QINT_TQCTL_INTEVENT_MASK);
3378
3379 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3380 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3381
3382 wr32(hw, I40E_QINT_TQCTL(qp), val);
3383 qp = next;
3384 }
3385 }
3386 } else {
3387 free_irq(pf->pdev->irq, pf);
3388
3389 val = rd32(hw, I40E_PFINT_LNKLST0);
3390 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3391 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3392 val |= I40E_QUEUE_END_OF_LIST
3393 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
3394 wr32(hw, I40E_PFINT_LNKLST0, val);
3395
3396 val = rd32(hw, I40E_QINT_RQCTL(qp));
3397 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3398 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3399 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3400 I40E_QINT_RQCTL_INTEVENT_MASK);
3401
3402 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3403 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3404
3405 wr32(hw, I40E_QINT_RQCTL(qp), val);
3406
3407 val = rd32(hw, I40E_QINT_TQCTL(qp));
3408
3409 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3410 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3411 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3412 I40E_QINT_TQCTL_INTEVENT_MASK);
3413
3414 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3415 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3416
3417 wr32(hw, I40E_QINT_TQCTL(qp), val);
3418 }
3419}
3420
3421/**
Alexander Duyck493fb302013-09-28 07:01:44 +00003422 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
3423 * @vsi: the VSI being configured
3424 * @v_idx: Index of vector to be freed
3425 *
3426 * This function frees the memory allocated to the q_vector. In addition if
3427 * NAPI is enabled it will delete any references to the NAPI struct prior
3428 * to freeing the q_vector.
3429 **/
3430static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
3431{
3432 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003433 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00003434
3435 if (!q_vector)
3436 return;
3437
3438 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003439 i40e_for_each_ring(ring, q_vector->tx)
3440 ring->q_vector = NULL;
3441
3442 i40e_for_each_ring(ring, q_vector->rx)
3443 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00003444
3445 /* only VSI w/ an associated netdev is set up w/ NAPI */
3446 if (vsi->netdev)
3447 netif_napi_del(&q_vector->napi);
3448
3449 vsi->q_vectors[v_idx] = NULL;
3450
3451 kfree_rcu(q_vector, rcu);
3452}
3453
3454/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003455 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
3456 * @vsi: the VSI being un-configured
3457 *
3458 * This frees the memory allocated to the q_vectors and
3459 * deletes references to the NAPI struct.
3460 **/
3461static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
3462{
3463 int v_idx;
3464
Alexander Duyck493fb302013-09-28 07:01:44 +00003465 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
3466 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003467}
3468
3469/**
3470 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
3471 * @pf: board private structure
3472 **/
3473static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
3474{
3475 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
3476 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3477 pci_disable_msix(pf->pdev);
3478 kfree(pf->msix_entries);
3479 pf->msix_entries = NULL;
3480 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
3481 pci_disable_msi(pf->pdev);
3482 }
3483 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
3484}
3485
3486/**
3487 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
3488 * @pf: board private structure
3489 *
3490 * We go through and clear interrupt specific resources and reset the structure
3491 * to pre-load conditions
3492 **/
3493static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
3494{
3495 int i;
3496
3497 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
3498 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
3499 if (pf->vsi[i])
3500 i40e_vsi_free_q_vectors(pf->vsi[i]);
3501 i40e_reset_interrupt_capability(pf);
3502}
3503
3504/**
3505 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
3506 * @vsi: the VSI being configured
3507 **/
3508static void i40e_napi_enable_all(struct i40e_vsi *vsi)
3509{
3510 int q_idx;
3511
3512 if (!vsi->netdev)
3513 return;
3514
3515 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003516 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003517}
3518
3519/**
3520 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
3521 * @vsi: the VSI being configured
3522 **/
3523static void i40e_napi_disable_all(struct i40e_vsi *vsi)
3524{
3525 int q_idx;
3526
3527 if (!vsi->netdev)
3528 return;
3529
3530 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003531 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003532}
3533
3534/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00003535 * i40e_vsi_close - Shut down a VSI
3536 * @vsi: the vsi to be quelled
3537 **/
3538static void i40e_vsi_close(struct i40e_vsi *vsi)
3539{
3540 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
3541 i40e_down(vsi);
3542 i40e_vsi_free_irq(vsi);
3543 i40e_vsi_free_tx_resources(vsi);
3544 i40e_vsi_free_rx_resources(vsi);
3545}
3546
3547/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003548 * i40e_quiesce_vsi - Pause a given VSI
3549 * @vsi: the VSI being paused
3550 **/
3551static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
3552{
3553 if (test_bit(__I40E_DOWN, &vsi->state))
3554 return;
3555
3556 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
3557 if (vsi->netdev && netif_running(vsi->netdev)) {
3558 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
3559 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00003560 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003561 }
3562}
3563
3564/**
3565 * i40e_unquiesce_vsi - Resume a given VSI
3566 * @vsi: the VSI being resumed
3567 **/
3568static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
3569{
3570 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
3571 return;
3572
3573 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
3574 if (vsi->netdev && netif_running(vsi->netdev))
3575 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
3576 else
Shannon Nelson8276f752014-03-14 07:32:27 +00003577 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003578}
3579
3580/**
3581 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
3582 * @pf: the PF
3583 **/
3584static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
3585{
3586 int v;
3587
3588 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
3589 if (pf->vsi[v])
3590 i40e_quiesce_vsi(pf->vsi[v]);
3591 }
3592}
3593
3594/**
3595 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
3596 * @pf: the PF
3597 **/
3598static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
3599{
3600 int v;
3601
3602 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
3603 if (pf->vsi[v])
3604 i40e_unquiesce_vsi(pf->vsi[v]);
3605 }
3606}
3607
3608/**
3609 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
3610 * @dcbcfg: the corresponding DCBx configuration structure
3611 *
3612 * Return the number of TCs from given DCBx configuration
3613 **/
3614static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
3615{
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003616 u8 num_tc = 0;
3617 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003618
3619 /* Scan the ETS Config Priority Table to find
3620 * traffic class enabled for a given priority
3621 * and use the traffic class index to get the
3622 * number of traffic classes enabled
3623 */
3624 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3625 if (dcbcfg->etscfg.prioritytable[i] > num_tc)
3626 num_tc = dcbcfg->etscfg.prioritytable[i];
3627 }
3628
3629 /* Traffic class index starts from zero so
3630 * increment to return the actual count
3631 */
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003632 return num_tc + 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003633}
3634
3635/**
3636 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
3637 * @dcbcfg: the corresponding DCBx configuration structure
3638 *
3639 * Query the current DCB configuration and return the number of
3640 * traffic classes enabled from the given DCBX config
3641 **/
3642static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
3643{
3644 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
3645 u8 enabled_tc = 1;
3646 u8 i;
3647
3648 for (i = 0; i < num_tc; i++)
3649 enabled_tc |= 1 << i;
3650
3651 return enabled_tc;
3652}
3653
3654/**
3655 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
3656 * @pf: PF being queried
3657 *
3658 * Return number of traffic classes enabled for the given PF
3659 **/
3660static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
3661{
3662 struct i40e_hw *hw = &pf->hw;
3663 u8 i, enabled_tc;
3664 u8 num_tc = 0;
3665 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3666
3667 /* If DCB is not enabled then always in single TC */
3668 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3669 return 1;
3670
3671 /* MFP mode return count of enabled TCs for this PF */
3672 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
3673 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3674 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3675 if (enabled_tc & (1 << i))
3676 num_tc++;
3677 }
3678 return num_tc;
3679 }
3680
3681 /* SFP mode will be enabled for all TCs on port */
3682 return i40e_dcb_get_num_tc(dcbcfg);
3683}
3684
3685/**
3686 * i40e_pf_get_default_tc - Get bitmap for first enabled TC
3687 * @pf: PF being queried
3688 *
3689 * Return a bitmap for first enabled traffic class for this PF.
3690 **/
3691static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
3692{
3693 u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3694 u8 i = 0;
3695
3696 if (!enabled_tc)
3697 return 0x1; /* TC0 */
3698
3699 /* Find the first enabled TC */
3700 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3701 if (enabled_tc & (1 << i))
3702 break;
3703 }
3704
3705 return 1 << i;
3706}
3707
3708/**
3709 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
3710 * @pf: PF being queried
3711 *
3712 * Return a bitmap for enabled traffic classes for this PF.
3713 **/
3714static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
3715{
3716 /* If DCB is not enabled for this PF then just return default TC */
3717 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3718 return i40e_pf_get_default_tc(pf);
3719
3720 /* MFP mode will have enabled TCs set by FW */
3721 if (pf->flags & I40E_FLAG_MFP_ENABLED)
3722 return pf->hw.func_caps.enabled_tcmap;
3723
3724 /* SFP mode we want PF to be enabled for all TCs */
3725 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
3726}
3727
3728/**
3729 * i40e_vsi_get_bw_info - Query VSI BW Information
3730 * @vsi: the VSI being queried
3731 *
3732 * Returns 0 on success, negative value on failure
3733 **/
3734static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
3735{
3736 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
3737 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
3738 struct i40e_pf *pf = vsi->back;
3739 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003740 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003741 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003742 int i;
3743
3744 /* Get the VSI level BW configuration */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003745 aq_ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
3746 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003747 dev_info(&pf->pdev->dev,
3748 "couldn't get pf vsi bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003749 aq_ret, pf->hw.aq.asq_last_status);
3750 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003751 }
3752
3753 /* Get the VSI level BW configuration per TC */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003754 aq_ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
Jesse Brandeburg6838b532014-01-14 00:49:52 -08003755 NULL);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003756 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003757 dev_info(&pf->pdev->dev,
3758 "couldn't get pf vsi ets bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003759 aq_ret, pf->hw.aq.asq_last_status);
3760 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003761 }
3762
3763 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
3764 dev_info(&pf->pdev->dev,
3765 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
3766 bw_config.tc_valid_bits,
3767 bw_ets_config.tc_valid_bits);
3768 /* Still continuing */
3769 }
3770
3771 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
3772 vsi->bw_max_quanta = bw_config.max_bw;
3773 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
3774 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
3775 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3776 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
3777 vsi->bw_ets_limit_credits[i] =
3778 le16_to_cpu(bw_ets_config.credits[i]);
3779 /* 3 bits out of 4 for each TC */
3780 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
3781 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003782
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003783 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003784}
3785
3786/**
3787 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
3788 * @vsi: the VSI being configured
3789 * @enabled_tc: TC bitmap
3790 * @bw_credits: BW shared credits per TC
3791 *
3792 * Returns 0 on success, negative value on failure
3793 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003794static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003795 u8 *bw_share)
3796{
3797 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003798 i40e_status aq_ret;
3799 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003800
3801 bw_data.tc_valid_bits = enabled_tc;
3802 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3803 bw_data.tc_bw_credits[i] = bw_share[i];
3804
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003805 aq_ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
3806 NULL);
3807 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003808 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00003809 "AQ command Config VSI BW allocation per TC failed = %d\n",
3810 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003811 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003812 }
3813
3814 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3815 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
3816
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003817 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003818}
3819
3820/**
3821 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
3822 * @vsi: the VSI being configured
3823 * @enabled_tc: TC map to be enabled
3824 *
3825 **/
3826static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3827{
3828 struct net_device *netdev = vsi->netdev;
3829 struct i40e_pf *pf = vsi->back;
3830 struct i40e_hw *hw = &pf->hw;
3831 u8 netdev_tc = 0;
3832 int i;
3833 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3834
3835 if (!netdev)
3836 return;
3837
3838 if (!enabled_tc) {
3839 netdev_reset_tc(netdev);
3840 return;
3841 }
3842
3843 /* Set up actual enabled TCs on the VSI */
3844 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
3845 return;
3846
3847 /* set per TC queues for the VSI */
3848 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3849 /* Only set TC queues for enabled tcs
3850 *
3851 * e.g. For a VSI that has TC0 and TC3 enabled the
3852 * enabled_tc bitmap would be 0x00001001; the driver
3853 * will set the numtc for netdev as 2 that will be
3854 * referenced by the netdev layer as TC 0 and 1.
3855 */
3856 if (vsi->tc_config.enabled_tc & (1 << i))
3857 netdev_set_tc_queue(netdev,
3858 vsi->tc_config.tc_info[i].netdev_tc,
3859 vsi->tc_config.tc_info[i].qcount,
3860 vsi->tc_config.tc_info[i].qoffset);
3861 }
3862
3863 /* Assign UP2TC map for the VSI */
3864 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3865 /* Get the actual TC# for the UP */
3866 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
3867 /* Get the mapped netdev TC# for the UP */
3868 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
3869 netdev_set_prio_tc_map(netdev, i, netdev_tc);
3870 }
3871}
3872
3873/**
3874 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
3875 * @vsi: the VSI being configured
3876 * @ctxt: the ctxt buffer returned from AQ VSI update param command
3877 **/
3878static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
3879 struct i40e_vsi_context *ctxt)
3880{
3881 /* copy just the sections touched not the entire info
3882 * since not all sections are valid as returned by
3883 * update vsi params
3884 */
3885 vsi->info.mapping_flags = ctxt->info.mapping_flags;
3886 memcpy(&vsi->info.queue_mapping,
3887 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
3888 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
3889 sizeof(vsi->info.tc_mapping));
3890}
3891
3892/**
3893 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
3894 * @vsi: VSI to be configured
3895 * @enabled_tc: TC bitmap
3896 *
3897 * This configures a particular VSI for TCs that are mapped to the
3898 * given TC bitmap. It uses default bandwidth share for TCs across
3899 * VSIs to configure TC for a particular VSI.
3900 *
3901 * NOTE:
3902 * It is expected that the VSI queues have been quisced before calling
3903 * this function.
3904 **/
3905static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3906{
3907 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
3908 struct i40e_vsi_context ctxt;
3909 int ret = 0;
3910 int i;
3911
3912 /* Check if enabled_tc is same as existing or new TCs */
3913 if (vsi->tc_config.enabled_tc == enabled_tc)
3914 return ret;
3915
3916 /* Enable ETS TCs with equal BW Share for now across all VSIs */
3917 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3918 if (enabled_tc & (1 << i))
3919 bw_share[i] = 1;
3920 }
3921
3922 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
3923 if (ret) {
3924 dev_info(&vsi->back->pdev->dev,
3925 "Failed configuring TC map %d for VSI %d\n",
3926 enabled_tc, vsi->seid);
3927 goto out;
3928 }
3929
3930 /* Update Queue Pairs Mapping for currently enabled UPs */
3931 ctxt.seid = vsi->seid;
3932 ctxt.pf_num = vsi->back->hw.pf_id;
3933 ctxt.vf_num = 0;
3934 ctxt.uplink_seid = vsi->uplink_seid;
3935 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
3936 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
3937
3938 /* Update the VSI after updating the VSI queue-mapping information */
3939 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3940 if (ret) {
3941 dev_info(&vsi->back->pdev->dev,
3942 "update vsi failed, aq_err=%d\n",
3943 vsi->back->hw.aq.asq_last_status);
3944 goto out;
3945 }
3946 /* update the local VSI info with updated queue map */
3947 i40e_vsi_update_queue_map(vsi, &ctxt);
3948 vsi->info.valid_sections = 0;
3949
3950 /* Update current VSI BW information */
3951 ret = i40e_vsi_get_bw_info(vsi);
3952 if (ret) {
3953 dev_info(&vsi->back->pdev->dev,
3954 "Failed updating vsi bw info, aq_err=%d\n",
3955 vsi->back->hw.aq.asq_last_status);
3956 goto out;
3957 }
3958
3959 /* Update the netdev TC setup */
3960 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
3961out:
3962 return ret;
3963}
3964
3965/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08003966 * i40e_veb_config_tc - Configure TCs for given VEB
3967 * @veb: given VEB
3968 * @enabled_tc: TC bitmap
3969 *
3970 * Configures given TC bitmap for VEB (switching) element
3971 **/
3972int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
3973{
3974 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
3975 struct i40e_pf *pf = veb->pf;
3976 int ret = 0;
3977 int i;
3978
3979 /* No TCs or already enabled TCs just return */
3980 if (!enabled_tc || veb->enabled_tc == enabled_tc)
3981 return ret;
3982
3983 bw_data.tc_valid_bits = enabled_tc;
3984 /* bw_data.absolute_credits is not set (relative) */
3985
3986 /* Enable ETS TCs with equal BW Share for now */
3987 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3988 if (enabled_tc & (1 << i))
3989 bw_data.tc_bw_share_credits[i] = 1;
3990 }
3991
3992 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
3993 &bw_data, NULL);
3994 if (ret) {
3995 dev_info(&pf->pdev->dev,
3996 "veb bw config failed, aq_err=%d\n",
3997 pf->hw.aq.asq_last_status);
3998 goto out;
3999 }
4000
4001 /* Update the BW information */
4002 ret = i40e_veb_get_bw_info(veb);
4003 if (ret) {
4004 dev_info(&pf->pdev->dev,
4005 "Failed getting veb bw config, aq_err=%d\n",
4006 pf->hw.aq.asq_last_status);
4007 }
4008
4009out:
4010 return ret;
4011}
4012
4013#ifdef CONFIG_I40E_DCB
4014/**
4015 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
4016 * @pf: PF struct
4017 *
4018 * Reconfigure VEB/VSIs on a given PF; it is assumed that
4019 * the caller would've quiesce all the VSIs before calling
4020 * this function
4021 **/
4022static void i40e_dcb_reconfigure(struct i40e_pf *pf)
4023{
4024 u8 tc_map = 0;
4025 int ret;
4026 u8 v;
4027
4028 /* Enable the TCs available on PF to all VEBs */
4029 tc_map = i40e_pf_get_tc_map(pf);
4030 for (v = 0; v < I40E_MAX_VEB; v++) {
4031 if (!pf->veb[v])
4032 continue;
4033 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
4034 if (ret) {
4035 dev_info(&pf->pdev->dev,
4036 "Failed configuring TC for VEB seid=%d\n",
4037 pf->veb[v]->seid);
4038 /* Will try to configure as many components */
4039 }
4040 }
4041
4042 /* Update each VSI */
4043 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4044 if (!pf->vsi[v])
4045 continue;
4046
4047 /* - Enable all TCs for the LAN VSI
4048 * - For all others keep them at TC0 for now
4049 */
4050 if (v == pf->lan_vsi)
4051 tc_map = i40e_pf_get_tc_map(pf);
4052 else
4053 tc_map = i40e_pf_get_default_tc(pf);
4054
4055 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
4056 if (ret) {
4057 dev_info(&pf->pdev->dev,
4058 "Failed configuring TC for VSI seid=%d\n",
4059 pf->vsi[v]->seid);
4060 /* Will try to configure as many components */
4061 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00004062 /* Re-configure VSI vectors based on updated TC map */
4063 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004064 if (pf->vsi[v]->netdev)
4065 i40e_dcbnl_set_all(pf->vsi[v]);
4066 }
4067 }
4068}
4069
4070/**
4071 * i40e_init_pf_dcb - Initialize DCB configuration
4072 * @pf: PF being configured
4073 *
4074 * Query the current DCB configuration and cache it
4075 * in the hardware structure
4076 **/
4077static int i40e_init_pf_dcb(struct i40e_pf *pf)
4078{
4079 struct i40e_hw *hw = &pf->hw;
4080 int err = 0;
4081
4082 if (pf->hw.func_caps.npar_enable)
4083 goto out;
4084
4085 /* Get the initial DCB configuration */
4086 err = i40e_init_dcb(hw);
4087 if (!err) {
4088 /* Device/Function is not DCBX capable */
4089 if ((!hw->func_caps.dcb) ||
4090 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
4091 dev_info(&pf->pdev->dev,
4092 "DCBX offload is not supported or is disabled for this PF.\n");
4093
4094 if (pf->flags & I40E_FLAG_MFP_ENABLED)
4095 goto out;
4096
4097 } else {
4098 /* When status is not DISABLED then DCBX in FW */
4099 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
4100 DCB_CAP_DCBX_VER_IEEE;
4101 pf->flags |= I40E_FLAG_DCB_ENABLED;
4102 }
Neerav Parikh014269f2014-04-01 07:11:48 +00004103 } else {
4104 dev_info(&pf->pdev->dev, "AQ Querying DCB configuration failed: %d\n",
4105 pf->hw.aq.asq_last_status);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004106 }
4107
4108out:
4109 return err;
4110}
4111#endif /* CONFIG_I40E_DCB */
4112
4113/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004114 * i40e_up_complete - Finish the last steps of bringing up a connection
4115 * @vsi: the VSI being configured
4116 **/
4117static int i40e_up_complete(struct i40e_vsi *vsi)
4118{
4119 struct i40e_pf *pf = vsi->back;
4120 int err;
4121
4122 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4123 i40e_vsi_configure_msix(vsi);
4124 else
4125 i40e_configure_msi_and_legacy(vsi);
4126
4127 /* start rings */
4128 err = i40e_vsi_control_rings(vsi, true);
4129 if (err)
4130 return err;
4131
4132 clear_bit(__I40E_DOWN, &vsi->state);
4133 i40e_napi_enable_all(vsi);
4134 i40e_vsi_enable_irq(vsi);
4135
4136 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
4137 (vsi->netdev)) {
Anjali Singhai6d779b42013-09-28 06:00:02 +00004138 netdev_info(vsi->netdev, "NIC Link is Up\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004139 netif_tx_start_all_queues(vsi->netdev);
4140 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00004141 } else if (vsi->netdev) {
4142 netdev_info(vsi->netdev, "NIC Link is Down\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004143 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00004144
4145 /* replay FDIR SB filters */
4146 if (vsi->type == I40E_VSI_FDIR)
4147 i40e_fdir_filter_restore(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004148 i40e_service_event_schedule(pf);
4149
4150 return 0;
4151}
4152
4153/**
4154 * i40e_vsi_reinit_locked - Reset the VSI
4155 * @vsi: the VSI being configured
4156 *
4157 * Rebuild the ring structs after some configuration
4158 * has changed, e.g. MTU size.
4159 **/
4160static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
4161{
4162 struct i40e_pf *pf = vsi->back;
4163
4164 WARN_ON(in_interrupt());
4165 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
4166 usleep_range(1000, 2000);
4167 i40e_down(vsi);
4168
4169 /* Give a VF some time to respond to the reset. The
4170 * two second wait is based upon the watchdog cycle in
4171 * the VF driver.
4172 */
4173 if (vsi->type == I40E_VSI_SRIOV)
4174 msleep(2000);
4175 i40e_up(vsi);
4176 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
4177}
4178
4179/**
4180 * i40e_up - Bring the connection back up after being down
4181 * @vsi: the VSI being configured
4182 **/
4183int i40e_up(struct i40e_vsi *vsi)
4184{
4185 int err;
4186
4187 err = i40e_vsi_configure(vsi);
4188 if (!err)
4189 err = i40e_up_complete(vsi);
4190
4191 return err;
4192}
4193
4194/**
4195 * i40e_down - Shutdown the connection processing
4196 * @vsi: the VSI being stopped
4197 **/
4198void i40e_down(struct i40e_vsi *vsi)
4199{
4200 int i;
4201
4202 /* It is assumed that the caller of this function
4203 * sets the vsi->state __I40E_DOWN bit.
4204 */
4205 if (vsi->netdev) {
4206 netif_carrier_off(vsi->netdev);
4207 netif_tx_disable(vsi->netdev);
4208 }
4209 i40e_vsi_disable_irq(vsi);
4210 i40e_vsi_control_rings(vsi, false);
4211 i40e_napi_disable_all(vsi);
4212
4213 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004214 i40e_clean_tx_ring(vsi->tx_rings[i]);
4215 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004216 }
4217}
4218
4219/**
4220 * i40e_setup_tc - configure multiple traffic classes
4221 * @netdev: net device to configure
4222 * @tc: number of traffic classes to enable
4223 **/
4224static int i40e_setup_tc(struct net_device *netdev, u8 tc)
4225{
4226 struct i40e_netdev_priv *np = netdev_priv(netdev);
4227 struct i40e_vsi *vsi = np->vsi;
4228 struct i40e_pf *pf = vsi->back;
4229 u8 enabled_tc = 0;
4230 int ret = -EINVAL;
4231 int i;
4232
4233 /* Check if DCB enabled to continue */
4234 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
4235 netdev_info(netdev, "DCB is not enabled for adapter\n");
4236 goto exit;
4237 }
4238
4239 /* Check if MFP enabled */
4240 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
4241 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
4242 goto exit;
4243 }
4244
4245 /* Check whether tc count is within enabled limit */
4246 if (tc > i40e_pf_get_num_tc(pf)) {
4247 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
4248 goto exit;
4249 }
4250
4251 /* Generate TC map for number of tc requested */
4252 for (i = 0; i < tc; i++)
4253 enabled_tc |= (1 << i);
4254
4255 /* Requesting same TC configuration as already enabled */
4256 if (enabled_tc == vsi->tc_config.enabled_tc)
4257 return 0;
4258
4259 /* Quiesce VSI queues */
4260 i40e_quiesce_vsi(vsi);
4261
4262 /* Configure VSI for enabled TCs */
4263 ret = i40e_vsi_config_tc(vsi, enabled_tc);
4264 if (ret) {
4265 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
4266 vsi->seid);
4267 goto exit;
4268 }
4269
4270 /* Unquiesce VSI */
4271 i40e_unquiesce_vsi(vsi);
4272
4273exit:
4274 return ret;
4275}
4276
4277/**
4278 * i40e_open - Called when a network interface is made active
4279 * @netdev: network interface device structure
4280 *
4281 * The open entry point is called when a network interface is made
4282 * active by the system (IFF_UP). At this point all resources needed
4283 * for transmit and receive operations are allocated, the interrupt
4284 * handler is registered with the OS, the netdev watchdog subtask is
4285 * enabled, and the stack is notified that the interface is ready.
4286 *
4287 * Returns 0 on success, negative value on failure
4288 **/
4289static int i40e_open(struct net_device *netdev)
4290{
4291 struct i40e_netdev_priv *np = netdev_priv(netdev);
4292 struct i40e_vsi *vsi = np->vsi;
4293 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004294 int err;
4295
Shannon Nelson4eb3f762014-03-06 08:59:58 +00004296 /* disallow open during test or if eeprom is broken */
4297 if (test_bit(__I40E_TESTING, &pf->state) ||
4298 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004299 return -EBUSY;
4300
4301 netif_carrier_off(netdev);
4302
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004303 err = i40e_vsi_open(vsi);
4304 if (err)
4305 return err;
4306
Jesse Brandeburg059dab62014-04-01 09:07:20 +00004307 /* configure global TSO hardware offload settings */
4308 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
4309 TCP_FLAG_FIN) >> 16);
4310 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
4311 TCP_FLAG_FIN |
4312 TCP_FLAG_CWR) >> 16);
4313 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
4314
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004315#ifdef CONFIG_I40E_VXLAN
4316 vxlan_get_rx_port(netdev);
4317#endif
4318
4319 return 0;
4320}
4321
4322/**
4323 * i40e_vsi_open -
4324 * @vsi: the VSI to open
4325 *
4326 * Finish initialization of the VSI.
4327 *
4328 * Returns 0 on success, negative value on failure
4329 **/
4330int i40e_vsi_open(struct i40e_vsi *vsi)
4331{
4332 struct i40e_pf *pf = vsi->back;
4333 char int_name[IFNAMSIZ];
4334 int err;
4335
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004336 /* allocate descriptors */
4337 err = i40e_vsi_setup_tx_resources(vsi);
4338 if (err)
4339 goto err_setup_tx;
4340 err = i40e_vsi_setup_rx_resources(vsi);
4341 if (err)
4342 goto err_setup_rx;
4343
4344 err = i40e_vsi_configure(vsi);
4345 if (err)
4346 goto err_setup_rx;
4347
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00004348 if (vsi->netdev) {
4349 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
4350 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
4351 err = i40e_vsi_request_irq(vsi, int_name);
4352 if (err)
4353 goto err_setup_rx;
4354
4355 /* Notify the stack of the actual queue counts. */
4356 err = netif_set_real_num_tx_queues(vsi->netdev,
4357 vsi->num_queue_pairs);
4358 if (err)
4359 goto err_set_queues;
4360
4361 err = netif_set_real_num_rx_queues(vsi->netdev,
4362 vsi->num_queue_pairs);
4363 if (err)
4364 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00004365
4366 } else if (vsi->type == I40E_VSI_FDIR) {
4367 snprintf(int_name, sizeof(int_name) - 1, "%s-fdir",
4368 dev_driver_string(&pf->pdev->dev));
4369 err = i40e_vsi_request_irq(vsi, int_name);
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00004370 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00004371 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004372 goto err_setup_rx;
4373 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004374
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004375 err = i40e_up_complete(vsi);
4376 if (err)
4377 goto err_up_complete;
4378
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004379 return 0;
4380
4381err_up_complete:
4382 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004383err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004384 i40e_vsi_free_irq(vsi);
4385err_setup_rx:
4386 i40e_vsi_free_rx_resources(vsi);
4387err_setup_tx:
4388 i40e_vsi_free_tx_resources(vsi);
4389 if (vsi == pf->vsi[pf->lan_vsi])
4390 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
4391
4392 return err;
4393}
4394
4395/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00004396 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
4397 * @pf: Pointer to pf
4398 *
4399 * This function destroys the hlist where all the Flow Director
4400 * filters were saved.
4401 **/
4402static void i40e_fdir_filter_exit(struct i40e_pf *pf)
4403{
4404 struct i40e_fdir_filter *filter;
4405 struct hlist_node *node2;
4406
4407 hlist_for_each_entry_safe(filter, node2,
4408 &pf->fdir_filter_list, fdir_node) {
4409 hlist_del(&filter->fdir_node);
4410 kfree(filter);
4411 }
4412 pf->fdir_pf_active_filters = 0;
4413}
4414
4415/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004416 * i40e_close - Disables a network interface
4417 * @netdev: network interface device structure
4418 *
4419 * The close entry point is called when an interface is de-activated
4420 * by the OS. The hardware is still under the driver's control, but
4421 * this netdev interface is disabled.
4422 *
4423 * Returns 0, this is not allowed to fail
4424 **/
4425static int i40e_close(struct net_device *netdev)
4426{
4427 struct i40e_netdev_priv *np = netdev_priv(netdev);
4428 struct i40e_vsi *vsi = np->vsi;
4429
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004430 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004431
4432 return 0;
4433}
4434
4435/**
4436 * i40e_do_reset - Start a PF or Core Reset sequence
4437 * @pf: board private structure
4438 * @reset_flags: which reset is requested
4439 *
4440 * The essential difference in resets is that the PF Reset
4441 * doesn't clear the packet buffers, doesn't reset the PE
4442 * firmware, and doesn't bother the other PFs on the chip.
4443 **/
4444void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
4445{
4446 u32 val;
4447
4448 WARN_ON(in_interrupt());
4449
Mitch Williams263fc482014-04-23 04:50:11 +00004450 if (i40e_check_asq_alive(&pf->hw))
4451 i40e_vc_notify_reset(pf);
4452
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004453 /* do the biggest reset indicated */
4454 if (reset_flags & (1 << __I40E_GLOBAL_RESET_REQUESTED)) {
4455
4456 /* Request a Global Reset
4457 *
4458 * This will start the chip's countdown to the actual full
4459 * chip reset event, and a warning interrupt to be sent
4460 * to all PFs, including the requestor. Our handler
4461 * for the warning interrupt will deal with the shutdown
4462 * and recovery of the switch setup.
4463 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004464 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004465 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4466 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
4467 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4468
4469 } else if (reset_flags & (1 << __I40E_CORE_RESET_REQUESTED)) {
4470
4471 /* Request a Core Reset
4472 *
4473 * Same as Global Reset, except does *not* include the MAC/PHY
4474 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004475 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004476 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4477 val |= I40E_GLGEN_RTRIG_CORER_MASK;
4478 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4479 i40e_flush(&pf->hw);
4480
Shannon Nelson7823fe32013-11-16 10:00:45 +00004481 } else if (reset_flags & (1 << __I40E_EMP_RESET_REQUESTED)) {
4482
4483 /* Request a Firmware Reset
4484 *
4485 * Same as Global reset, plus restarting the
4486 * embedded firmware engine.
4487 */
4488 /* enable EMP Reset */
4489 val = rd32(&pf->hw, I40E_GLGEN_RSTENA_EMP);
4490 val |= I40E_GLGEN_RSTENA_EMP_EMP_RST_ENA_MASK;
4491 wr32(&pf->hw, I40E_GLGEN_RSTENA_EMP, val);
4492
4493 /* force the reset */
4494 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4495 val |= I40E_GLGEN_RTRIG_EMPFWR_MASK;
4496 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4497 i40e_flush(&pf->hw);
4498
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004499 } else if (reset_flags & (1 << __I40E_PF_RESET_REQUESTED)) {
4500
4501 /* Request a PF Reset
4502 *
4503 * Resets only the PF-specific registers
4504 *
4505 * This goes directly to the tear-down and rebuild of
4506 * the switch, since we need to do all the recovery as
4507 * for the Core Reset.
4508 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004509 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004510 i40e_handle_reset_warning(pf);
4511
4512 } else if (reset_flags & (1 << __I40E_REINIT_REQUESTED)) {
4513 int v;
4514
4515 /* Find the VSI(s) that requested a re-init */
4516 dev_info(&pf->pdev->dev,
4517 "VSI reinit requested\n");
4518 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4519 struct i40e_vsi *vsi = pf->vsi[v];
4520 if (vsi != NULL &&
4521 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
4522 i40e_vsi_reinit_locked(pf->vsi[v]);
4523 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
4524 }
4525 }
4526
4527 /* no further action needed, so return now */
4528 return;
4529 } else {
4530 dev_info(&pf->pdev->dev,
4531 "bad reset request 0x%08x\n", reset_flags);
4532 return;
4533 }
4534}
4535
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004536#ifdef CONFIG_I40E_DCB
4537/**
4538 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
4539 * @pf: board private structure
4540 * @old_cfg: current DCB config
4541 * @new_cfg: new DCB config
4542 **/
4543bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
4544 struct i40e_dcbx_config *old_cfg,
4545 struct i40e_dcbx_config *new_cfg)
4546{
4547 bool need_reconfig = false;
4548
4549 /* Check if ETS configuration has changed */
4550 if (memcmp(&new_cfg->etscfg,
4551 &old_cfg->etscfg,
4552 sizeof(new_cfg->etscfg))) {
4553 /* If Priority Table has changed reconfig is needed */
4554 if (memcmp(&new_cfg->etscfg.prioritytable,
4555 &old_cfg->etscfg.prioritytable,
4556 sizeof(new_cfg->etscfg.prioritytable))) {
4557 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004558 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004559 }
4560
4561 if (memcmp(&new_cfg->etscfg.tcbwtable,
4562 &old_cfg->etscfg.tcbwtable,
4563 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004564 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004565
4566 if (memcmp(&new_cfg->etscfg.tsatable,
4567 &old_cfg->etscfg.tsatable,
4568 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004569 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004570 }
4571
4572 /* Check if PFC configuration has changed */
4573 if (memcmp(&new_cfg->pfc,
4574 &old_cfg->pfc,
4575 sizeof(new_cfg->pfc))) {
4576 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004577 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004578 }
4579
4580 /* Check if APP Table has changed */
4581 if (memcmp(&new_cfg->app,
4582 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05004583 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004584 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004585 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05004586 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004587
4588 return need_reconfig;
4589}
4590
4591/**
4592 * i40e_handle_lldp_event - Handle LLDP Change MIB event
4593 * @pf: board private structure
4594 * @e: event info posted on ARQ
4595 **/
4596static int i40e_handle_lldp_event(struct i40e_pf *pf,
4597 struct i40e_arq_event_info *e)
4598{
4599 struct i40e_aqc_lldp_get_mib *mib =
4600 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
4601 struct i40e_hw *hw = &pf->hw;
4602 struct i40e_dcbx_config *dcbx_cfg = &hw->local_dcbx_config;
4603 struct i40e_dcbx_config tmp_dcbx_cfg;
4604 bool need_reconfig = false;
4605 int ret = 0;
4606 u8 type;
4607
4608 /* Ignore if event is not for Nearest Bridge */
4609 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
4610 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
4611 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
4612 return ret;
4613
4614 /* Check MIB Type and return if event for Remote MIB update */
4615 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
4616 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
4617 /* Update the remote cached instance and return */
4618 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
4619 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
4620 &hw->remote_dcbx_config);
4621 goto exit;
4622 }
4623
4624 /* Convert/store the DCBX data from LLDPDU temporarily */
4625 memset(&tmp_dcbx_cfg, 0, sizeof(tmp_dcbx_cfg));
4626 ret = i40e_lldp_to_dcb_config(e->msg_buf, &tmp_dcbx_cfg);
4627 if (ret) {
4628 /* Error in LLDPDU parsing return */
4629 dev_info(&pf->pdev->dev, "Failed parsing LLDPDU from event buffer\n");
4630 goto exit;
4631 }
4632
4633 /* No change detected in DCBX configs */
4634 if (!memcmp(&tmp_dcbx_cfg, dcbx_cfg, sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004635 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004636 goto exit;
4637 }
4638
4639 need_reconfig = i40e_dcb_need_reconfig(pf, dcbx_cfg, &tmp_dcbx_cfg);
4640
4641 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg);
4642
4643 /* Overwrite the new configuration */
4644 *dcbx_cfg = tmp_dcbx_cfg;
4645
4646 if (!need_reconfig)
4647 goto exit;
4648
4649 /* Reconfiguration needed quiesce all VSIs */
4650 i40e_pf_quiesce_all_vsi(pf);
4651
4652 /* Changes in configuration update VEB/VSI */
4653 i40e_dcb_reconfigure(pf);
4654
4655 i40e_pf_unquiesce_all_vsi(pf);
4656exit:
4657 return ret;
4658}
4659#endif /* CONFIG_I40E_DCB */
4660
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004661/**
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004662 * i40e_do_reset_safe - Protected reset path for userland calls.
4663 * @pf: board private structure
4664 * @reset_flags: which reset is requested
4665 *
4666 **/
4667void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
4668{
4669 rtnl_lock();
4670 i40e_do_reset(pf, reset_flags);
4671 rtnl_unlock();
4672}
4673
4674/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004675 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
4676 * @pf: board private structure
4677 * @e: event info posted on ARQ
4678 *
4679 * Handler for LAN Queue Overflow Event generated by the firmware for PF
4680 * and VF queues
4681 **/
4682static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
4683 struct i40e_arq_event_info *e)
4684{
4685 struct i40e_aqc_lan_overflow *data =
4686 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
4687 u32 queue = le32_to_cpu(data->prtdcb_rupto);
4688 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
4689 struct i40e_hw *hw = &pf->hw;
4690 struct i40e_vf *vf;
4691 u16 vf_id;
4692
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004693 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
4694 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004695
4696 /* Queue belongs to VF, find the VF and issue VF reset */
4697 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
4698 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
4699 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
4700 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
4701 vf_id -= hw->func_caps.vf_base_id;
4702 vf = &pf->vf[vf_id];
4703 i40e_vc_notify_vf_reset(vf);
4704 /* Allow VF to process pending reset notification */
4705 msleep(20);
4706 i40e_reset_vf(vf, false);
4707 }
4708}
4709
4710/**
4711 * i40e_service_event_complete - Finish up the service event
4712 * @pf: board private structure
4713 **/
4714static void i40e_service_event_complete(struct i40e_pf *pf)
4715{
4716 BUG_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
4717
4718 /* flush memory to make sure state is correct before next watchog */
4719 smp_mb__before_clear_bit();
4720 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
4721}
4722
4723/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004724 * i40e_get_current_fd_count - Get the count of FD filters programmed in the HW
4725 * @pf: board private structure
4726 **/
4727int i40e_get_current_fd_count(struct i40e_pf *pf)
4728{
4729 int val, fcnt_prog;
4730 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
4731 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
4732 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
4733 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
4734 return fcnt_prog;
4735}
4736
4737/**
4738 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
4739 * @pf: board private structure
4740 **/
4741void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
4742{
4743 u32 fcnt_prog, fcnt_avail;
4744
4745 /* Check if, FD SB or ATR was auto disabled and if there is enough room
4746 * to re-enable
4747 */
4748 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4749 (pf->flags & I40E_FLAG_FD_SB_ENABLED))
4750 return;
4751 fcnt_prog = i40e_get_current_fd_count(pf);
Anjali Singhai Jain89132782014-04-09 05:59:01 +00004752 fcnt_avail = i40e_get_fd_cnt_all(pf);
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004753 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) {
4754 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
4755 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
4756 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
4757 dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
4758 }
4759 }
4760 /* Wait for some more space to be available to turn on ATR */
4761 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
4762 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4763 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
4764 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
4765 dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table now\n");
4766 }
4767 }
4768}
4769
4770/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004771 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
4772 * @pf: board private structure
4773 **/
4774static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
4775{
4776 if (!(pf->flags & I40E_FLAG_FDIR_REQUIRES_REINIT))
4777 return;
4778
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004779 /* if interface is down do nothing */
4780 if (test_bit(__I40E_DOWN, &pf->state))
4781 return;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004782 i40e_fdir_check_and_reenable(pf);
4783
4784 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4785 (pf->flags & I40E_FLAG_FD_SB_ENABLED))
4786 pf->flags &= ~I40E_FLAG_FDIR_REQUIRES_REINIT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004787}
4788
4789/**
4790 * i40e_vsi_link_event - notify VSI of a link event
4791 * @vsi: vsi to be notified
4792 * @link_up: link up or down
4793 **/
4794static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
4795{
4796 if (!vsi)
4797 return;
4798
4799 switch (vsi->type) {
4800 case I40E_VSI_MAIN:
4801 if (!vsi->netdev || !vsi->netdev_registered)
4802 break;
4803
4804 if (link_up) {
4805 netif_carrier_on(vsi->netdev);
4806 netif_tx_wake_all_queues(vsi->netdev);
4807 } else {
4808 netif_carrier_off(vsi->netdev);
4809 netif_tx_stop_all_queues(vsi->netdev);
4810 }
4811 break;
4812
4813 case I40E_VSI_SRIOV:
4814 break;
4815
4816 case I40E_VSI_VMDQ2:
4817 case I40E_VSI_CTRL:
4818 case I40E_VSI_MIRROR:
4819 default:
4820 /* there is no notification for other VSIs */
4821 break;
4822 }
4823}
4824
4825/**
4826 * i40e_veb_link_event - notify elements on the veb of a link event
4827 * @veb: veb to be notified
4828 * @link_up: link up or down
4829 **/
4830static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
4831{
4832 struct i40e_pf *pf;
4833 int i;
4834
4835 if (!veb || !veb->pf)
4836 return;
4837 pf = veb->pf;
4838
4839 /* depth first... */
4840 for (i = 0; i < I40E_MAX_VEB; i++)
4841 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
4842 i40e_veb_link_event(pf->veb[i], link_up);
4843
4844 /* ... now the local VSIs */
4845 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
4846 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
4847 i40e_vsi_link_event(pf->vsi[i], link_up);
4848}
4849
4850/**
4851 * i40e_link_event - Update netif_carrier status
4852 * @pf: board private structure
4853 **/
4854static void i40e_link_event(struct i40e_pf *pf)
4855{
4856 bool new_link, old_link;
4857
4858 new_link = (pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP);
4859 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
4860
4861 if (new_link == old_link)
4862 return;
4863
Anjali Singhai6d779b42013-09-28 06:00:02 +00004864 if (!test_bit(__I40E_DOWN, &pf->vsi[pf->lan_vsi]->state))
4865 netdev_info(pf->vsi[pf->lan_vsi]->netdev,
4866 "NIC Link is %s\n", (new_link ? "Up" : "Down"));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004867
4868 /* Notify the base of the switch tree connected to
4869 * the link. Floating VEBs are not notified.
4870 */
4871 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
4872 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
4873 else
4874 i40e_vsi_link_event(pf->vsi[pf->lan_vsi], new_link);
4875
4876 if (pf->vf)
4877 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00004878
4879 if (pf->flags & I40E_FLAG_PTP)
4880 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004881}
4882
4883/**
4884 * i40e_check_hang_subtask - Check for hung queues and dropped interrupts
4885 * @pf: board private structure
4886 *
4887 * Set the per-queue flags to request a check for stuck queues in the irq
4888 * clean functions, then force interrupts to be sure the irq clean is called.
4889 **/
4890static void i40e_check_hang_subtask(struct i40e_pf *pf)
4891{
4892 int i, v;
4893
4894 /* If we're down or resetting, just bail */
4895 if (test_bit(__I40E_CONFIG_BUSY, &pf->state))
4896 return;
4897
4898 /* for each VSI/netdev
4899 * for each Tx queue
4900 * set the check flag
4901 * for each q_vector
4902 * force an interrupt
4903 */
4904 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4905 struct i40e_vsi *vsi = pf->vsi[v];
4906 int armed = 0;
4907
4908 if (!pf->vsi[v] ||
4909 test_bit(__I40E_DOWN, &vsi->state) ||
4910 (vsi->netdev && !netif_carrier_ok(vsi->netdev)))
4911 continue;
4912
4913 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004914 set_check_for_tx_hang(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004915 if (test_bit(__I40E_HANG_CHECK_ARMED,
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004916 &vsi->tx_rings[i]->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004917 armed++;
4918 }
4919
4920 if (armed) {
4921 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
4922 wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0,
4923 (I40E_PFINT_DYN_CTL0_INTENA_MASK |
4924 I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK));
4925 } else {
4926 u16 vec = vsi->base_vector - 1;
4927 u32 val = (I40E_PFINT_DYN_CTLN_INTENA_MASK |
4928 I40E_PFINT_DYN_CTLN_SWINT_TRIG_MASK);
4929 for (i = 0; i < vsi->num_q_vectors; i++, vec++)
4930 wr32(&vsi->back->hw,
4931 I40E_PFINT_DYN_CTLN(vec), val);
4932 }
4933 i40e_flush(&vsi->back->hw);
4934 }
4935 }
4936}
4937
4938/**
4939 * i40e_watchdog_subtask - Check and bring link up
4940 * @pf: board private structure
4941 **/
4942static void i40e_watchdog_subtask(struct i40e_pf *pf)
4943{
4944 int i;
4945
4946 /* if interface is down do nothing */
4947 if (test_bit(__I40E_DOWN, &pf->state) ||
4948 test_bit(__I40E_CONFIG_BUSY, &pf->state))
4949 return;
4950
4951 /* Update the stats for active netdevs so the network stack
4952 * can look at updated numbers whenever it cares to
4953 */
4954 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
4955 if (pf->vsi[i] && pf->vsi[i]->netdev)
4956 i40e_update_stats(pf->vsi[i]);
4957
4958 /* Update the stats for the active switching components */
4959 for (i = 0; i < I40E_MAX_VEB; i++)
4960 if (pf->veb[i])
4961 i40e_update_veb_stats(pf->veb[i]);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00004962
4963 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004964}
4965
4966/**
4967 * i40e_reset_subtask - Set up for resetting the device and driver
4968 * @pf: board private structure
4969 **/
4970static void i40e_reset_subtask(struct i40e_pf *pf)
4971{
4972 u32 reset_flags = 0;
4973
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004974 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004975 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
4976 reset_flags |= (1 << __I40E_REINIT_REQUESTED);
4977 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
4978 }
4979 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
4980 reset_flags |= (1 << __I40E_PF_RESET_REQUESTED);
4981 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
4982 }
4983 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
4984 reset_flags |= (1 << __I40E_CORE_RESET_REQUESTED);
4985 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
4986 }
4987 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
4988 reset_flags |= (1 << __I40E_GLOBAL_RESET_REQUESTED);
4989 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
4990 }
4991
4992 /* If there's a recovery already waiting, it takes
4993 * precedence before starting a new reset sequence.
4994 */
4995 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
4996 i40e_handle_reset_warning(pf);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004997 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004998 }
4999
5000 /* If we're already down or resetting, just bail */
5001 if (reset_flags &&
5002 !test_bit(__I40E_DOWN, &pf->state) &&
5003 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
5004 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005005
5006unlock:
5007 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005008}
5009
5010/**
5011 * i40e_handle_link_event - Handle link event
5012 * @pf: board private structure
5013 * @e: event info posted on ARQ
5014 **/
5015static void i40e_handle_link_event(struct i40e_pf *pf,
5016 struct i40e_arq_event_info *e)
5017{
5018 struct i40e_hw *hw = &pf->hw;
5019 struct i40e_aqc_get_link_status *status =
5020 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
5021 struct i40e_link_status *hw_link_info = &hw->phy.link_info;
5022
5023 /* save off old link status information */
5024 memcpy(&pf->hw.phy.link_info_old, hw_link_info,
5025 sizeof(pf->hw.phy.link_info_old));
5026
5027 /* update link status */
5028 hw_link_info->phy_type = (enum i40e_aq_phy_type)status->phy_type;
5029 hw_link_info->link_speed = (enum i40e_aq_link_speed)status->link_speed;
5030 hw_link_info->link_info = status->link_info;
5031 hw_link_info->an_info = status->an_info;
5032 hw_link_info->ext_info = status->ext_info;
5033 hw_link_info->lse_enable =
5034 le16_to_cpu(status->command_flags) &
5035 I40E_AQ_LSE_ENABLE;
5036
5037 /* process the event */
5038 i40e_link_event(pf);
5039
5040 /* Do a new status request to re-enable LSE reporting
5041 * and load new status information into the hw struct,
5042 * then see if the status changed while processing the
5043 * initial event.
5044 */
5045 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
5046 i40e_link_event(pf);
5047}
5048
5049/**
5050 * i40e_clean_adminq_subtask - Clean the AdminQ rings
5051 * @pf: board private structure
5052 **/
5053static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
5054{
5055 struct i40e_arq_event_info event;
5056 struct i40e_hw *hw = &pf->hw;
5057 u16 pending, i = 0;
5058 i40e_status ret;
5059 u16 opcode;
5060 u32 val;
5061
5062 if (!test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state))
5063 return;
5064
Mitch Williams3197ce22013-11-28 06:39:39 +00005065 event.msg_size = I40E_MAX_AQ_BUF_SIZE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005066 event.msg_buf = kzalloc(event.msg_size, GFP_KERNEL);
5067 if (!event.msg_buf)
5068 return;
5069
5070 do {
Mitch Williams2f019122013-11-28 06:39:33 +00005071 event.msg_size = I40E_MAX_AQ_BUF_SIZE; /* reinit each time */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005072 ret = i40e_clean_arq_element(hw, &event, &pending);
5073 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
5074 dev_info(&pf->pdev->dev, "No ARQ event found\n");
5075 break;
5076 } else if (ret) {
5077 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
5078 break;
5079 }
5080
5081 opcode = le16_to_cpu(event.desc.opcode);
5082 switch (opcode) {
5083
5084 case i40e_aqc_opc_get_link_status:
5085 i40e_handle_link_event(pf, &event);
5086 break;
5087 case i40e_aqc_opc_send_msg_to_pf:
5088 ret = i40e_vc_process_vf_msg(pf,
5089 le16_to_cpu(event.desc.retval),
5090 le32_to_cpu(event.desc.cookie_high),
5091 le32_to_cpu(event.desc.cookie_low),
5092 event.msg_buf,
5093 event.msg_size);
5094 break;
5095 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005096 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005097#ifdef CONFIG_I40E_DCB
5098 rtnl_lock();
5099 ret = i40e_handle_lldp_event(pf, &event);
5100 rtnl_unlock();
5101#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005102 break;
5103 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005104 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005105 i40e_handle_lan_overflow_event(pf, &event);
5106 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00005107 case i40e_aqc_opc_send_msg_to_peer:
5108 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
5109 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005110 default:
5111 dev_info(&pf->pdev->dev,
Shannon Nelson0467bc92013-12-18 13:45:58 +00005112 "ARQ Error: Unknown event 0x%04x received\n",
5113 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005114 break;
5115 }
5116 } while (pending && (i++ < pf->adminq_work_limit));
5117
5118 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
5119 /* re-enable Admin queue interrupt cause */
5120 val = rd32(hw, I40E_PFINT_ICR0_ENA);
5121 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
5122 wr32(hw, I40E_PFINT_ICR0_ENA, val);
5123 i40e_flush(hw);
5124
5125 kfree(event.msg_buf);
5126}
5127
5128/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005129 * i40e_verify_eeprom - make sure eeprom is good to use
5130 * @pf: board private structure
5131 **/
5132static void i40e_verify_eeprom(struct i40e_pf *pf)
5133{
5134 int err;
5135
5136 err = i40e_diag_eeprom_test(&pf->hw);
5137 if (err) {
5138 /* retry in case of garbage read */
5139 err = i40e_diag_eeprom_test(&pf->hw);
5140 if (err) {
5141 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
5142 err);
5143 set_bit(__I40E_BAD_EEPROM, &pf->state);
5144 }
5145 }
5146
5147 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
5148 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
5149 clear_bit(__I40E_BAD_EEPROM, &pf->state);
5150 }
5151}
5152
5153/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005154 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
5155 * @veb: pointer to the VEB instance
5156 *
5157 * This is a recursive function that first builds the attached VSIs then
5158 * recurses in to build the next layer of VEB. We track the connections
5159 * through our own index numbers because the seid's from the HW could
5160 * change across the reset.
5161 **/
5162static int i40e_reconstitute_veb(struct i40e_veb *veb)
5163{
5164 struct i40e_vsi *ctl_vsi = NULL;
5165 struct i40e_pf *pf = veb->pf;
5166 int v, veb_idx;
5167 int ret;
5168
5169 /* build VSI that owns this VEB, temporarily attached to base VEB */
5170 for (v = 0; v < pf->hw.func_caps.num_vsis && !ctl_vsi; v++) {
5171 if (pf->vsi[v] &&
5172 pf->vsi[v]->veb_idx == veb->idx &&
5173 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
5174 ctl_vsi = pf->vsi[v];
5175 break;
5176 }
5177 }
5178 if (!ctl_vsi) {
5179 dev_info(&pf->pdev->dev,
5180 "missing owner VSI for veb_idx %d\n", veb->idx);
5181 ret = -ENOENT;
5182 goto end_reconstitute;
5183 }
5184 if (ctl_vsi != pf->vsi[pf->lan_vsi])
5185 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
5186 ret = i40e_add_vsi(ctl_vsi);
5187 if (ret) {
5188 dev_info(&pf->pdev->dev,
5189 "rebuild of owner VSI failed: %d\n", ret);
5190 goto end_reconstitute;
5191 }
5192 i40e_vsi_reset_stats(ctl_vsi);
5193
5194 /* create the VEB in the switch and move the VSI onto the VEB */
5195 ret = i40e_add_veb(veb, ctl_vsi);
5196 if (ret)
5197 goto end_reconstitute;
5198
5199 /* create the remaining VSIs attached to this VEB */
5200 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5201 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
5202 continue;
5203
5204 if (pf->vsi[v]->veb_idx == veb->idx) {
5205 struct i40e_vsi *vsi = pf->vsi[v];
5206 vsi->uplink_seid = veb->seid;
5207 ret = i40e_add_vsi(vsi);
5208 if (ret) {
5209 dev_info(&pf->pdev->dev,
5210 "rebuild of vsi_idx %d failed: %d\n",
5211 v, ret);
5212 goto end_reconstitute;
5213 }
5214 i40e_vsi_reset_stats(vsi);
5215 }
5216 }
5217
5218 /* create any VEBs attached to this VEB - RECURSION */
5219 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
5220 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
5221 pf->veb[veb_idx]->uplink_seid = veb->seid;
5222 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
5223 if (ret)
5224 break;
5225 }
5226 }
5227
5228end_reconstitute:
5229 return ret;
5230}
5231
5232/**
5233 * i40e_get_capabilities - get info about the HW
5234 * @pf: the PF struct
5235 **/
5236static int i40e_get_capabilities(struct i40e_pf *pf)
5237{
5238 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
5239 u16 data_size;
5240 int buf_len;
5241 int err;
5242
5243 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
5244 do {
5245 cap_buf = kzalloc(buf_len, GFP_KERNEL);
5246 if (!cap_buf)
5247 return -ENOMEM;
5248
5249 /* this loads the data into the hw struct for us */
5250 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
5251 &data_size,
5252 i40e_aqc_opc_list_func_capabilities,
5253 NULL);
5254 /* data loaded, buffer no longer needed */
5255 kfree(cap_buf);
5256
5257 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
5258 /* retry with a larger buffer */
5259 buf_len = data_size;
5260 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
5261 dev_info(&pf->pdev->dev,
5262 "capability discovery failed: aq=%d\n",
5263 pf->hw.aq.asq_last_status);
5264 return -ENODEV;
5265 }
5266 } while (err);
5267
Anjali Singhai Jainac71b7b2014-02-06 05:51:08 +00005268 if (((pf->hw.aq.fw_maj_ver == 2) && (pf->hw.aq.fw_min_ver < 22)) ||
5269 (pf->hw.aq.fw_maj_ver < 2)) {
5270 pf->hw.func_caps.num_msix_vectors++;
5271 pf->hw.func_caps.num_msix_vectors_vf++;
5272 }
5273
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005274 if (pf->hw.debug_mask & I40E_DEBUG_USER)
5275 dev_info(&pf->pdev->dev,
5276 "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",
5277 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
5278 pf->hw.func_caps.num_msix_vectors,
5279 pf->hw.func_caps.num_msix_vectors_vf,
5280 pf->hw.func_caps.fd_filters_guaranteed,
5281 pf->hw.func_caps.fd_filters_best_effort,
5282 pf->hw.func_caps.num_tx_qp,
5283 pf->hw.func_caps.num_vsis);
5284
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00005285#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
5286 + pf->hw.func_caps.num_vfs)
5287 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
5288 dev_info(&pf->pdev->dev,
5289 "got num_vsis %d, setting num_vsis to %d\n",
5290 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
5291 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
5292 }
5293
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005294 return 0;
5295}
5296
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005297static int i40e_vsi_clear(struct i40e_vsi *vsi);
5298
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005299/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005300 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005301 * @pf: board private structure
5302 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005303static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005304{
5305 struct i40e_vsi *vsi;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005306 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005307
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005308 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005309 return;
5310
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005311 /* find existing VSI and see if it needs configuring */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005312 vsi = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005313 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
5314 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005315 vsi = pf->vsi[i];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005316 break;
5317 }
5318 }
5319
5320 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005321 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005322 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
5323 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005324 if (!vsi) {
5325 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005326 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
5327 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005328 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005329 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005330
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005331 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005332}
5333
5334/**
5335 * i40e_fdir_teardown - release the Flow Director resources
5336 * @pf: board private structure
5337 **/
5338static void i40e_fdir_teardown(struct i40e_pf *pf)
5339{
5340 int i;
5341
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005342 i40e_fdir_filter_exit(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005343 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
5344 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
5345 i40e_vsi_release(pf->vsi[i]);
5346 break;
5347 }
5348 }
5349}
5350
5351/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005352 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005353 * @pf: board private structure
5354 *
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005355 * Close up the VFs and other things in prep for pf Reset.
5356 **/
5357static int i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005358{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005359 struct i40e_hw *hw = &pf->hw;
5360 i40e_status ret;
5361 u32 v;
5362
5363 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
5364 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005365 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005366
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005367 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005368
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005369 /* quiesce the VSIs and their queues that are not already DOWN */
5370 i40e_pf_quiesce_all_vsi(pf);
5371
5372 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5373 if (pf->vsi[v])
5374 pf->vsi[v]->seid = 0;
5375 }
5376
5377 i40e_shutdown_adminq(&pf->hw);
5378
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005379 /* call shutdown HMC */
5380 ret = i40e_shutdown_lan_hmc(hw);
5381 if (ret) {
5382 dev_info(&pf->pdev->dev, "shutdown_lan_hmc failed: %d\n", ret);
5383 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
5384 }
5385 return ret;
5386}
5387
5388/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00005389 * i40e_send_version - update firmware with driver version
5390 * @pf: PF struct
5391 */
5392static void i40e_send_version(struct i40e_pf *pf)
5393{
5394 struct i40e_driver_version dv;
5395
5396 dv.major_version = DRV_VERSION_MAJOR;
5397 dv.minor_version = DRV_VERSION_MINOR;
5398 dv.build_version = DRV_VERSION_BUILD;
5399 dv.subbuild_version = 0;
5400 strncpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
5401 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
5402}
5403
5404/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00005405 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005406 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005407 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005408 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005409static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005410{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005411 struct i40e_hw *hw = &pf->hw;
5412 i40e_status ret;
5413 u32 v;
5414
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005415 /* Now we wait for GRST to settle out.
5416 * We don't have to delete the VEBs or VSIs from the hw switch
5417 * because the reset will make them disappear.
5418 */
5419 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00005420 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005421 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00005422 goto end_core_reset;
5423 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005424 pf->pfr_count++;
5425
5426 if (test_bit(__I40E_DOWN, &pf->state))
5427 goto end_core_reset;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005428 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005429
5430 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
5431 ret = i40e_init_adminq(&pf->hw);
5432 if (ret) {
5433 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, %d\n", ret);
5434 goto end_core_reset;
5435 }
5436
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005437 /* re-verify the eeprom if we just had an EMP reset */
5438 if (test_bit(__I40E_EMP_RESET_REQUESTED, &pf->state)) {
5439 clear_bit(__I40E_EMP_RESET_REQUESTED, &pf->state);
5440 i40e_verify_eeprom(pf);
5441 }
5442
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005443 ret = i40e_get_capabilities(pf);
5444 if (ret) {
5445 dev_info(&pf->pdev->dev, "i40e_get_capabilities failed, %d\n",
5446 ret);
5447 goto end_core_reset;
5448 }
5449
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005450 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
5451 hw->func_caps.num_rx_qp,
5452 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
5453 if (ret) {
5454 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
5455 goto end_core_reset;
5456 }
5457 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
5458 if (ret) {
5459 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
5460 goto end_core_reset;
5461 }
5462
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005463#ifdef CONFIG_I40E_DCB
5464 ret = i40e_init_pf_dcb(pf);
5465 if (ret) {
5466 dev_info(&pf->pdev->dev, "init_pf_dcb failed: %d\n", ret);
5467 goto end_core_reset;
5468 }
5469#endif /* CONFIG_I40E_DCB */
5470
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005471 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005472 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005473 if (ret)
5474 goto end_core_reset;
5475
5476 /* Rebuild the VSIs and VEBs that existed before reset.
5477 * They are still in our local switch element arrays, so only
5478 * need to rebuild the switch model in the HW.
5479 *
5480 * If there were VEBs but the reconstitution failed, we'll try
5481 * try to recover minimal use by getting the basic PF VSI working.
5482 */
5483 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005484 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005485 /* find the one VEB connected to the MAC, and find orphans */
5486 for (v = 0; v < I40E_MAX_VEB; v++) {
5487 if (!pf->veb[v])
5488 continue;
5489
5490 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
5491 pf->veb[v]->uplink_seid == 0) {
5492 ret = i40e_reconstitute_veb(pf->veb[v]);
5493
5494 if (!ret)
5495 continue;
5496
5497 /* If Main VEB failed, we're in deep doodoo,
5498 * so give up rebuilding the switch and set up
5499 * for minimal rebuild of PF VSI.
5500 * If orphan failed, we'll report the error
5501 * but try to keep going.
5502 */
5503 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
5504 dev_info(&pf->pdev->dev,
5505 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
5506 ret);
5507 pf->vsi[pf->lan_vsi]->uplink_seid
5508 = pf->mac_seid;
5509 break;
5510 } else if (pf->veb[v]->uplink_seid == 0) {
5511 dev_info(&pf->pdev->dev,
5512 "rebuild of orphan VEB failed: %d\n",
5513 ret);
5514 }
5515 }
5516 }
5517 }
5518
5519 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
5520 dev_info(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
5521 /* no VEB, so rebuild only the Main VSI */
5522 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
5523 if (ret) {
5524 dev_info(&pf->pdev->dev,
5525 "rebuild of Main VSI failed: %d\n", ret);
5526 goto end_core_reset;
5527 }
5528 }
5529
5530 /* reinit the misc interrupt */
5531 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5532 ret = i40e_setup_misc_vector(pf);
5533
5534 /* restart the VSIs that were rebuilt and running before the reset */
5535 i40e_pf_unquiesce_all_vsi(pf);
5536
Mitch Williams69f64b22014-02-13 03:48:46 -08005537 if (pf->num_alloc_vfs) {
5538 for (v = 0; v < pf->num_alloc_vfs; v++)
5539 i40e_reset_vf(&pf->vf[v], true);
5540 }
5541
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005542 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00005543 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005544
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005545 dev_info(&pf->pdev->dev, "reset complete\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005546
5547end_core_reset:
5548 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
5549}
5550
5551/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005552 * i40e_handle_reset_warning - prep for the pf to reset, reset and rebuild
5553 * @pf: board private structure
5554 *
5555 * Close up the VFs and other things in prep for a Core Reset,
5556 * then get ready to rebuild the world.
5557 **/
5558static void i40e_handle_reset_warning(struct i40e_pf *pf)
5559{
5560 i40e_status ret;
5561
5562 ret = i40e_prep_for_reset(pf);
5563 if (!ret)
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005564 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005565}
5566
5567/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005568 * i40e_handle_mdd_event
5569 * @pf: pointer to the pf structure
5570 *
5571 * Called from the MDD irq handler to identify possibly malicious vfs
5572 **/
5573static void i40e_handle_mdd_event(struct i40e_pf *pf)
5574{
5575 struct i40e_hw *hw = &pf->hw;
5576 bool mdd_detected = false;
5577 struct i40e_vf *vf;
5578 u32 reg;
5579 int i;
5580
5581 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
5582 return;
5583
5584 /* find what triggered the MDD event */
5585 reg = rd32(hw, I40E_GL_MDET_TX);
5586 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
5587 u8 func = (reg & I40E_GL_MDET_TX_FUNCTION_MASK)
5588 >> I40E_GL_MDET_TX_FUNCTION_SHIFT;
5589 u8 event = (reg & I40E_GL_MDET_TX_EVENT_SHIFT)
5590 >> I40E_GL_MDET_TX_EVENT_SHIFT;
5591 u8 queue = (reg & I40E_GL_MDET_TX_QUEUE_MASK)
5592 >> I40E_GL_MDET_TX_QUEUE_SHIFT;
5593 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00005594 "Malicious Driver Detection event 0x%02x on TX queue %d of function 0x%02x\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005595 event, queue, func);
5596 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
5597 mdd_detected = true;
5598 }
5599 reg = rd32(hw, I40E_GL_MDET_RX);
5600 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
5601 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK)
5602 >> I40E_GL_MDET_RX_FUNCTION_SHIFT;
5603 u8 event = (reg & I40E_GL_MDET_RX_EVENT_SHIFT)
5604 >> I40E_GL_MDET_RX_EVENT_SHIFT;
5605 u8 queue = (reg & I40E_GL_MDET_RX_QUEUE_MASK)
5606 >> I40E_GL_MDET_RX_QUEUE_SHIFT;
5607 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00005608 "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005609 event, queue, func);
5610 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
5611 mdd_detected = true;
5612 }
5613
5614 /* see if one of the VFs needs its hand slapped */
5615 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
5616 vf = &(pf->vf[i]);
5617 reg = rd32(hw, I40E_VP_MDET_TX(i));
5618 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
5619 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
5620 vf->num_mdd_events++;
5621 dev_info(&pf->pdev->dev, "MDD TX event on VF %d\n", i);
5622 }
5623
5624 reg = rd32(hw, I40E_VP_MDET_RX(i));
5625 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
5626 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
5627 vf->num_mdd_events++;
5628 dev_info(&pf->pdev->dev, "MDD RX event on VF %d\n", i);
5629 }
5630
5631 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
5632 dev_info(&pf->pdev->dev,
5633 "Too many MDD events on VF %d, disabled\n", i);
5634 dev_info(&pf->pdev->dev,
5635 "Use PF Control I/F to re-enable the VF\n");
5636 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
5637 }
5638 }
5639
5640 /* re-enable mdd interrupt cause */
5641 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
5642 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
5643 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
5644 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
5645 i40e_flush(hw);
5646}
5647
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005648#ifdef CONFIG_I40E_VXLAN
5649/**
5650 * i40e_sync_vxlan_filters_subtask - Sync the VSI filter list with HW
5651 * @pf: board private structure
5652 **/
5653static void i40e_sync_vxlan_filters_subtask(struct i40e_pf *pf)
5654{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005655 struct i40e_hw *hw = &pf->hw;
5656 i40e_status ret;
5657 u8 filter_index;
5658 __be16 port;
5659 int i;
5660
5661 if (!(pf->flags & I40E_FLAG_VXLAN_FILTER_SYNC))
5662 return;
5663
5664 pf->flags &= ~I40E_FLAG_VXLAN_FILTER_SYNC;
5665
5666 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
5667 if (pf->pending_vxlan_bitmap & (1 << i)) {
5668 pf->pending_vxlan_bitmap &= ~(1 << i);
5669 port = pf->vxlan_ports[i];
5670 ret = port ?
5671 i40e_aq_add_udp_tunnel(hw, ntohs(port),
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005672 I40E_AQC_TUNNEL_TYPE_VXLAN,
5673 &filter_index, NULL)
5674 : i40e_aq_del_udp_tunnel(hw, i, NULL);
5675
5676 if (ret) {
5677 dev_info(&pf->pdev->dev, "Failed to execute AQ command for %s port %d with index %d\n",
5678 port ? "adding" : "deleting",
5679 ntohs(port), port ? i : i);
5680
5681 pf->vxlan_ports[i] = 0;
5682 } else {
5683 dev_info(&pf->pdev->dev, "%s port %d with AQ command with index %d\n",
5684 port ? "Added" : "Deleted",
5685 ntohs(port), port ? i : filter_index);
5686 }
5687 }
5688 }
5689}
5690
5691#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005692/**
5693 * i40e_service_task - Run the driver's async subtasks
5694 * @work: pointer to work_struct containing our data
5695 **/
5696static void i40e_service_task(struct work_struct *work)
5697{
5698 struct i40e_pf *pf = container_of(work,
5699 struct i40e_pf,
5700 service_task);
5701 unsigned long start_time = jiffies;
5702
5703 i40e_reset_subtask(pf);
5704 i40e_handle_mdd_event(pf);
5705 i40e_vc_process_vflr_event(pf);
5706 i40e_watchdog_subtask(pf);
5707 i40e_fdir_reinit_subtask(pf);
5708 i40e_check_hang_subtask(pf);
5709 i40e_sync_filters_subtask(pf);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005710#ifdef CONFIG_I40E_VXLAN
5711 i40e_sync_vxlan_filters_subtask(pf);
5712#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005713 i40e_clean_adminq_subtask(pf);
5714
5715 i40e_service_event_complete(pf);
5716
5717 /* If the tasks have taken longer than one timer cycle or there
5718 * is more work to be done, reschedule the service task now
5719 * rather than wait for the timer to tick again.
5720 */
5721 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
5722 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
5723 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
5724 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
5725 i40e_service_event_schedule(pf);
5726}
5727
5728/**
5729 * i40e_service_timer - timer callback
5730 * @data: pointer to PF struct
5731 **/
5732static void i40e_service_timer(unsigned long data)
5733{
5734 struct i40e_pf *pf = (struct i40e_pf *)data;
5735
5736 mod_timer(&pf->service_timer,
5737 round_jiffies(jiffies + pf->service_timer_period));
5738 i40e_service_event_schedule(pf);
5739}
5740
5741/**
5742 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
5743 * @vsi: the VSI being configured
5744 **/
5745static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
5746{
5747 struct i40e_pf *pf = vsi->back;
5748
5749 switch (vsi->type) {
5750 case I40E_VSI_MAIN:
5751 vsi->alloc_queue_pairs = pf->num_lan_qps;
5752 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5753 I40E_REQ_DESCRIPTOR_MULTIPLE);
5754 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5755 vsi->num_q_vectors = pf->num_lan_msix;
5756 else
5757 vsi->num_q_vectors = 1;
5758
5759 break;
5760
5761 case I40E_VSI_FDIR:
5762 vsi->alloc_queue_pairs = 1;
5763 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
5764 I40E_REQ_DESCRIPTOR_MULTIPLE);
5765 vsi->num_q_vectors = 1;
5766 break;
5767
5768 case I40E_VSI_VMDQ2:
5769 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
5770 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5771 I40E_REQ_DESCRIPTOR_MULTIPLE);
5772 vsi->num_q_vectors = pf->num_vmdq_msix;
5773 break;
5774
5775 case I40E_VSI_SRIOV:
5776 vsi->alloc_queue_pairs = pf->num_vf_qps;
5777 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5778 I40E_REQ_DESCRIPTOR_MULTIPLE);
5779 break;
5780
5781 default:
5782 WARN_ON(1);
5783 return -ENODATA;
5784 }
5785
5786 return 0;
5787}
5788
5789/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005790 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
5791 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005792 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005793 *
5794 * On error: returns error code (negative)
5795 * On success: returns 0
5796 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005797static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005798{
5799 int size;
5800 int ret = 0;
5801
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00005802 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005803 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
5804 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
5805 if (!vsi->tx_rings)
5806 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005807 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
5808
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005809 if (alloc_qvectors) {
5810 /* allocate memory for q_vector pointers */
5811 size = sizeof(struct i40e_q_vectors *) * vsi->num_q_vectors;
5812 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
5813 if (!vsi->q_vectors) {
5814 ret = -ENOMEM;
5815 goto err_vectors;
5816 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005817 }
5818 return ret;
5819
5820err_vectors:
5821 kfree(vsi->tx_rings);
5822 return ret;
5823}
5824
5825/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005826 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
5827 * @pf: board private structure
5828 * @type: type of VSI
5829 *
5830 * On error: returns error code (negative)
5831 * On success: returns vsi index in PF (positive)
5832 **/
5833static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
5834{
5835 int ret = -ENODEV;
5836 struct i40e_vsi *vsi;
5837 int vsi_idx;
5838 int i;
5839
5840 /* Need to protect the allocation of the VSIs at the PF level */
5841 mutex_lock(&pf->switch_mutex);
5842
5843 /* VSI list may be fragmented if VSI creation/destruction has
5844 * been happening. We can afford to do a quick scan to look
5845 * for any free VSIs in the list.
5846 *
5847 * find next empty vsi slot, looping back around if necessary
5848 */
5849 i = pf->next_vsi;
5850 while (i < pf->hw.func_caps.num_vsis && pf->vsi[i])
5851 i++;
5852 if (i >= pf->hw.func_caps.num_vsis) {
5853 i = 0;
5854 while (i < pf->next_vsi && pf->vsi[i])
5855 i++;
5856 }
5857
5858 if (i < pf->hw.func_caps.num_vsis && !pf->vsi[i]) {
5859 vsi_idx = i; /* Found one! */
5860 } else {
5861 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00005862 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005863 }
5864 pf->next_vsi = ++i;
5865
5866 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
5867 if (!vsi) {
5868 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00005869 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005870 }
5871 vsi->type = type;
5872 vsi->back = pf;
5873 set_bit(__I40E_DOWN, &vsi->state);
5874 vsi->flags = 0;
5875 vsi->idx = vsi_idx;
5876 vsi->rx_itr_setting = pf->rx_itr_default;
5877 vsi->tx_itr_setting = pf->tx_itr_default;
5878 vsi->netdev_registered = false;
5879 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
5880 INIT_LIST_HEAD(&vsi->mac_filter_list);
5881
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005882 ret = i40e_set_num_rings_in_vsi(vsi);
5883 if (ret)
5884 goto err_rings;
5885
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005886 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005887 if (ret)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005888 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00005889
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005890 /* Setup default MSIX irq handler for VSI */
5891 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
5892
5893 pf->vsi[vsi_idx] = vsi;
5894 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00005895 goto unlock_pf;
5896
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005897err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00005898 pf->next_vsi = i - 1;
5899 kfree(vsi);
5900unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005901 mutex_unlock(&pf->switch_mutex);
5902 return ret;
5903}
5904
5905/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005906 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
5907 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005908 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005909 *
5910 * On error: returns error code (negative)
5911 * On success: returns 0
5912 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005913static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005914{
5915 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005916 if (free_qvectors) {
5917 kfree(vsi->q_vectors);
5918 vsi->q_vectors = NULL;
5919 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005920 kfree(vsi->tx_rings);
5921 vsi->tx_rings = NULL;
5922 vsi->rx_rings = NULL;
5923}
5924
5925/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005926 * i40e_vsi_clear - Deallocate the VSI provided
5927 * @vsi: the VSI being un-configured
5928 **/
5929static int i40e_vsi_clear(struct i40e_vsi *vsi)
5930{
5931 struct i40e_pf *pf;
5932
5933 if (!vsi)
5934 return 0;
5935
5936 if (!vsi->back)
5937 goto free_vsi;
5938 pf = vsi->back;
5939
5940 mutex_lock(&pf->switch_mutex);
5941 if (!pf->vsi[vsi->idx]) {
5942 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
5943 vsi->idx, vsi->idx, vsi, vsi->type);
5944 goto unlock_vsi;
5945 }
5946
5947 if (pf->vsi[vsi->idx] != vsi) {
5948 dev_err(&pf->pdev->dev,
5949 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
5950 pf->vsi[vsi->idx]->idx,
5951 pf->vsi[vsi->idx],
5952 pf->vsi[vsi->idx]->type,
5953 vsi->idx, vsi, vsi->type);
5954 goto unlock_vsi;
5955 }
5956
5957 /* updates the pf for this cleared vsi */
5958 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
5959 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
5960
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005961 i40e_vsi_free_arrays(vsi, true);
Alexander Duyck493fb302013-09-28 07:01:44 +00005962
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005963 pf->vsi[vsi->idx] = NULL;
5964 if (vsi->idx < pf->next_vsi)
5965 pf->next_vsi = vsi->idx;
5966
5967unlock_vsi:
5968 mutex_unlock(&pf->switch_mutex);
5969free_vsi:
5970 kfree(vsi);
5971
5972 return 0;
5973}
5974
5975/**
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005976 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
5977 * @vsi: the VSI being cleaned
5978 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00005979static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005980{
5981 int i;
5982
Greg Rose8e9dca52013-12-18 13:45:53 +00005983 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00005984 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00005985 kfree_rcu(vsi->tx_rings[i], rcu);
5986 vsi->tx_rings[i] = NULL;
5987 vsi->rx_rings[i] = NULL;
5988 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00005989 }
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005990}
5991
5992/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005993 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
5994 * @vsi: the VSI being configured
5995 **/
5996static int i40e_alloc_rings(struct i40e_vsi *vsi)
5997{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00005998 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005999 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006000 int i;
6001
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006002 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00006003 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00006004 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006005 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
6006 if (!tx_ring)
6007 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006008
6009 tx_ring->queue_index = i;
6010 tx_ring->reg_idx = vsi->base_queue + i;
6011 tx_ring->ring_active = false;
6012 tx_ring->vsi = vsi;
6013 tx_ring->netdev = vsi->netdev;
6014 tx_ring->dev = &pf->pdev->dev;
6015 tx_ring->count = vsi->num_desc;
6016 tx_ring->size = 0;
6017 tx_ring->dcb_tc = 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006018 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006019
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006020 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006021 rx_ring->queue_index = i;
6022 rx_ring->reg_idx = vsi->base_queue + i;
6023 rx_ring->ring_active = false;
6024 rx_ring->vsi = vsi;
6025 rx_ring->netdev = vsi->netdev;
6026 rx_ring->dev = &pf->pdev->dev;
6027 rx_ring->count = vsi->num_desc;
6028 rx_ring->size = 0;
6029 rx_ring->dcb_tc = 0;
6030 if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
6031 set_ring_16byte_desc_enabled(rx_ring);
6032 else
6033 clear_ring_16byte_desc_enabled(rx_ring);
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006034 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006035 }
6036
6037 return 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006038
6039err_out:
6040 i40e_vsi_clear_rings(vsi);
6041 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006042}
6043
6044/**
6045 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
6046 * @pf: board private structure
6047 * @vectors: the number of MSI-X vectors to request
6048 *
6049 * Returns the number of vectors reserved, or error
6050 **/
6051static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
6052{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01006053 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
6054 I40E_MIN_MSIX, vectors);
6055 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006056 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01006057 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006058 vectors = 0;
6059 }
6060
Alexander Gordeev7b37f372014-02-18 11:11:42 +01006061 pf->num_msix_entries = vectors;
6062
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006063 return vectors;
6064}
6065
6066/**
6067 * i40e_init_msix - Setup the MSIX capability
6068 * @pf: board private structure
6069 *
6070 * Work with the OS to set up the MSIX vectors needed.
6071 *
6072 * Returns 0 on success, negative on failure
6073 **/
6074static int i40e_init_msix(struct i40e_pf *pf)
6075{
6076 i40e_status err = 0;
6077 struct i40e_hw *hw = &pf->hw;
6078 int v_budget, i;
6079 int vec;
6080
6081 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
6082 return -ENODEV;
6083
6084 /* The number of vectors we'll request will be comprised of:
6085 * - Add 1 for "other" cause for Admin Queue events, etc.
6086 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006087 * - Queues being used for RSS.
6088 * We don't need as many as max_rss_size vectors.
6089 * use rss_size instead in the calculation since that
6090 * is governed by number of cpus in the system.
6091 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006092 * - The number of VMDq pairs
6093 * Once we count this up, try the request.
6094 *
6095 * If we can't get what we want, we'll simplify to nearly nothing
6096 * and try again. If that still fails, we punt.
6097 */
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006098 pf->num_lan_msix = pf->num_lan_qps - (pf->rss_size_max - pf->rss_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006099 pf->num_vmdq_msix = pf->num_vmdq_qps;
6100 v_budget = 1 + pf->num_lan_msix;
6101 v_budget += (pf->num_vmdq_vsis * pf->num_vmdq_msix);
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006102 if (pf->flags & I40E_FLAG_FD_SB_ENABLED)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006103 v_budget++;
6104
6105 /* Scale down if necessary, and the rings will share vectors */
6106 v_budget = min_t(int, v_budget, hw->func_caps.num_msix_vectors);
6107
6108 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
6109 GFP_KERNEL);
6110 if (!pf->msix_entries)
6111 return -ENOMEM;
6112
6113 for (i = 0; i < v_budget; i++)
6114 pf->msix_entries[i].entry = i;
6115 vec = i40e_reserve_msix_vectors(pf, v_budget);
6116 if (vec < I40E_MIN_MSIX) {
6117 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
6118 kfree(pf->msix_entries);
6119 pf->msix_entries = NULL;
6120 return -ENODEV;
6121
6122 } else if (vec == I40E_MIN_MSIX) {
6123 /* Adjust for minimal MSIX use */
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006124 dev_info(&pf->pdev->dev, "Features disabled, not enough MSI-X vectors\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006125 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
6126 pf->num_vmdq_vsis = 0;
6127 pf->num_vmdq_qps = 0;
6128 pf->num_vmdq_msix = 0;
6129 pf->num_lan_qps = 1;
6130 pf->num_lan_msix = 1;
6131
6132 } else if (vec != v_budget) {
6133 /* Scale vector usage down */
6134 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
6135 vec--; /* reserve the misc vector */
6136
6137 /* partition out the remaining vectors */
6138 switch (vec) {
6139 case 2:
6140 pf->num_vmdq_vsis = 1;
6141 pf->num_lan_msix = 1;
6142 break;
6143 case 3:
6144 pf->num_vmdq_vsis = 1;
6145 pf->num_lan_msix = 2;
6146 break;
6147 default:
6148 pf->num_lan_msix = min_t(int, (vec / 2),
6149 pf->num_lan_qps);
6150 pf->num_vmdq_vsis = min_t(int, (vec - pf->num_lan_msix),
6151 I40E_DEFAULT_NUM_VMDQ_VSI);
6152 break;
6153 }
6154 }
6155
6156 return err;
6157}
6158
6159/**
Greg Rose90e04072014-03-06 08:59:57 +00006160 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00006161 * @vsi: the VSI being configured
6162 * @v_idx: index of the vector in the vsi struct
6163 *
6164 * We allocate one q_vector. If allocation fails we return -ENOMEM.
6165 **/
Greg Rose90e04072014-03-06 08:59:57 +00006166static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
Alexander Duyck493fb302013-09-28 07:01:44 +00006167{
6168 struct i40e_q_vector *q_vector;
6169
6170 /* allocate q_vector */
6171 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
6172 if (!q_vector)
6173 return -ENOMEM;
6174
6175 q_vector->vsi = vsi;
6176 q_vector->v_idx = v_idx;
6177 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
6178 if (vsi->netdev)
6179 netif_napi_add(vsi->netdev, &q_vector->napi,
6180 i40e_napi_poll, vsi->work_limit);
6181
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00006182 q_vector->rx.latency_range = I40E_LOW_LATENCY;
6183 q_vector->tx.latency_range = I40E_LOW_LATENCY;
6184
Alexander Duyck493fb302013-09-28 07:01:44 +00006185 /* tie q_vector and vsi together */
6186 vsi->q_vectors[v_idx] = q_vector;
6187
6188 return 0;
6189}
6190
6191/**
Greg Rose90e04072014-03-06 08:59:57 +00006192 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006193 * @vsi: the VSI being configured
6194 *
6195 * We allocate one q_vector per queue interrupt. If allocation fails we
6196 * return -ENOMEM.
6197 **/
Greg Rose90e04072014-03-06 08:59:57 +00006198static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006199{
6200 struct i40e_pf *pf = vsi->back;
6201 int v_idx, num_q_vectors;
Alexander Duyck493fb302013-09-28 07:01:44 +00006202 int err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006203
6204 /* if not MSIX, give the one vector only to the LAN VSI */
6205 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6206 num_q_vectors = vsi->num_q_vectors;
6207 else if (vsi == pf->vsi[pf->lan_vsi])
6208 num_q_vectors = 1;
6209 else
6210 return -EINVAL;
6211
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006212 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Greg Rose90e04072014-03-06 08:59:57 +00006213 err = i40e_vsi_alloc_q_vector(vsi, v_idx);
Alexander Duyck493fb302013-09-28 07:01:44 +00006214 if (err)
6215 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006216 }
6217
6218 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00006219
6220err_out:
6221 while (v_idx--)
6222 i40e_free_q_vector(vsi, v_idx);
6223
6224 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006225}
6226
6227/**
6228 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
6229 * @pf: board private structure to initialize
6230 **/
6231static void i40e_init_interrupt_scheme(struct i40e_pf *pf)
6232{
6233 int err = 0;
6234
6235 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
6236 err = i40e_init_msix(pf);
6237 if (err) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006238 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
6239 I40E_FLAG_RSS_ENABLED |
6240 I40E_FLAG_DCB_ENABLED |
6241 I40E_FLAG_SRIOV_ENABLED |
6242 I40E_FLAG_FD_SB_ENABLED |
6243 I40E_FLAG_FD_ATR_ENABLED |
6244 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006245
6246 /* rework the queue expectations without MSIX */
6247 i40e_determine_queue_usage(pf);
6248 }
6249 }
6250
6251 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
6252 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006253 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006254 err = pci_enable_msi(pf->pdev);
6255 if (err) {
Shannon Nelson958a3e32013-09-28 07:13:28 +00006256 dev_info(&pf->pdev->dev, "MSI init failed - %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006257 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
6258 }
6259 }
6260
Shannon Nelson958a3e32013-09-28 07:13:28 +00006261 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006262 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00006263
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006264 /* track first vector for misc interrupts */
6265 err = i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT-1);
6266}
6267
6268/**
6269 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
6270 * @pf: board private structure
6271 *
6272 * This sets up the handler for MSIX 0, which is used to manage the
6273 * non-queue interrupts, e.g. AdminQ and errors. This is not used
6274 * when in MSI or Legacy interrupt mode.
6275 **/
6276static int i40e_setup_misc_vector(struct i40e_pf *pf)
6277{
6278 struct i40e_hw *hw = &pf->hw;
6279 int err = 0;
6280
6281 /* Only request the irq if this is the first time through, and
6282 * not when we're rebuilding after a Reset
6283 */
6284 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
6285 err = request_irq(pf->msix_entries[0].vector,
6286 i40e_intr, 0, pf->misc_int_name, pf);
6287 if (err) {
6288 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006289 "request_irq for %s failed: %d\n",
6290 pf->misc_int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006291 return -EFAULT;
6292 }
6293 }
6294
6295 i40e_enable_misc_int_causes(hw);
6296
6297 /* associate no queues to the misc vector */
6298 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
6299 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
6300
6301 i40e_flush(hw);
6302
6303 i40e_irq_dynamic_enable_icr0(pf);
6304
6305 return err;
6306}
6307
6308/**
6309 * i40e_config_rss - Prepare for RSS if used
6310 * @pf: board private structure
6311 **/
6312static int i40e_config_rss(struct i40e_pf *pf)
6313{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006314 /* Set of random keys generated using kernel random number generator */
6315 static const u32 seed[I40E_PFQF_HKEY_MAX_INDEX + 1] = {0x41b01687,
6316 0x183cfd8c, 0xce880440, 0x580cbc3c, 0x35897377,
6317 0x328b25e1, 0x4fa98922, 0xb7d90c14, 0xd5bad70d,
6318 0xcd15a2c1, 0xe8580225, 0x4a1e9d11, 0xfe5731be};
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00006319 struct i40e_hw *hw = &pf->hw;
6320 u32 lut = 0;
6321 int i, j;
6322 u64 hena;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006323
6324 /* Fill out hash function seed */
6325 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
6326 wr32(hw, I40E_PFQF_HKEY(i), seed[i]);
6327
6328 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
6329 hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
6330 ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
Mitch Williams12dc4fe2013-11-28 06:39:32 +00006331 hena |= I40E_DEFAULT_RSS_HENA;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006332 wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
6333 wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
6334
6335 /* Populate the LUT with max no. of queues in round robin fashion */
6336 for (i = 0, j = 0; i < pf->hw.func_caps.rss_table_size; i++, j++) {
6337
6338 /* The assumption is that lan qp count will be the highest
6339 * qp count for any PF VSI that needs RSS.
6340 * If multiple VSIs need RSS support, all the qp counts
6341 * for those VSIs should be a power of 2 for RSS to work.
6342 * If LAN VSI is the only consumer for RSS then this requirement
6343 * is not necessary.
6344 */
6345 if (j == pf->rss_size)
6346 j = 0;
6347 /* lut = 4-byte sliding window of 4 lut entries */
6348 lut = (lut << 8) | (j &
6349 ((0x1 << pf->hw.func_caps.rss_table_entry_width) - 1));
6350 /* On i = 3, we have 4 entries in lut; write to the register */
6351 if ((i & 3) == 3)
6352 wr32(hw, I40E_PFQF_HLUT(i >> 2), lut);
6353 }
6354 i40e_flush(hw);
6355
6356 return 0;
6357}
6358
6359/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006360 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
6361 * @pf: board private structure
6362 * @queue_count: the requested queue count for rss.
6363 *
6364 * returns 0 if rss is not enabled, if enabled returns the final rss queue
6365 * count which may be different from the requested queue count.
6366 **/
6367int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
6368{
6369 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
6370 return 0;
6371
6372 queue_count = min_t(int, queue_count, pf->rss_size_max);
6373 queue_count = rounddown_pow_of_two(queue_count);
6374
6375 if (queue_count != pf->rss_size) {
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006376 i40e_prep_for_reset(pf);
6377
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006378 pf->rss_size = queue_count;
6379
6380 i40e_reset_and_rebuild(pf, true);
6381 i40e_config_rss(pf);
6382 }
6383 dev_info(&pf->pdev->dev, "RSS count: %d\n", pf->rss_size);
6384 return pf->rss_size;
6385}
6386
6387/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006388 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
6389 * @pf: board private structure to initialize
6390 *
6391 * i40e_sw_init initializes the Adapter private data structure.
6392 * Fields are initialized based on PCI device information and
6393 * OS network device settings (MTU size).
6394 **/
6395static int i40e_sw_init(struct i40e_pf *pf)
6396{
6397 int err = 0;
6398 int size;
6399
6400 pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
6401 (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
Shannon Nelson27599972013-11-16 10:00:43 +00006402 pf->hw.debug_mask = pf->msg_enable | I40E_DEBUG_DIAG;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006403 if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
6404 if (I40E_DEBUG_USER & debug)
6405 pf->hw.debug_mask = debug;
6406 pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
6407 I40E_DEFAULT_MSG_ENABLE);
6408 }
6409
6410 /* Set default capability flags */
6411 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
6412 I40E_FLAG_MSI_ENABLED |
6413 I40E_FLAG_MSIX_ENABLED |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006414 I40E_FLAG_RX_1BUF_ENABLED;
6415
Mitch Williamsca99eb92014-04-04 04:43:07 +00006416 /* Set default ITR */
6417 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
6418 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
6419
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006420 /* Depending on PF configurations, it is possible that the RSS
6421 * maximum might end up larger than the available queues
6422 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006423 pf->rss_size_max = 0x1 << pf->hw.func_caps.rss_table_entry_width;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006424 pf->rss_size_max = min_t(int, pf->rss_size_max,
6425 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006426 if (pf->hw.func_caps.rss) {
6427 pf->flags |= I40E_FLAG_RSS_ENABLED;
Jesse Brandeburgbf051a32013-11-26 10:49:17 +00006428 pf->rss_size = min_t(int, pf->rss_size_max, num_online_cpus());
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006429 pf->rss_size = rounddown_pow_of_two(pf->rss_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006430 } else {
6431 pf->rss_size = 1;
6432 }
6433
Catherine Sullivan2050bc62013-12-18 13:46:03 +00006434 /* MFP mode enabled */
6435 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.mfp_mode_1) {
6436 pf->flags |= I40E_FLAG_MFP_ENABLED;
6437 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
6438 }
6439
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006440 /* FW/NVM is not yet fixed in this regard */
6441 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
6442 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
6443 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
6444 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006445 if (!(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006446 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006447 } else {
6448 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00006449 "Flow Director Sideband mode Disabled in MFP mode\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006450 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006451 pf->fdir_pf_filter_count =
6452 pf->hw.func_caps.fd_filters_guaranteed;
6453 pf->hw.fdir_shared_filter_count =
6454 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006455 }
6456
6457 if (pf->hw.func_caps.vmdq) {
6458 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
6459 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
6460 pf->num_vmdq_qps = I40E_DEFAULT_QUEUES_PER_VMDQ;
6461 }
6462
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006463#ifdef CONFIG_PCI_IOV
6464 if (pf->hw.func_caps.num_vfs) {
6465 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
6466 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
6467 pf->num_req_vfs = min_t(int,
6468 pf->hw.func_caps.num_vfs,
6469 I40E_MAX_VF_COUNT);
6470 }
6471#endif /* CONFIG_PCI_IOV */
6472 pf->eeprom_version = 0xDEAD;
6473 pf->lan_veb = I40E_NO_VEB;
6474 pf->lan_vsi = I40E_NO_VSI;
6475
6476 /* set up queue assignment tracking */
6477 size = sizeof(struct i40e_lump_tracking)
6478 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
6479 pf->qp_pile = kzalloc(size, GFP_KERNEL);
6480 if (!pf->qp_pile) {
6481 err = -ENOMEM;
6482 goto sw_init_done;
6483 }
6484 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
6485 pf->qp_pile->search_hint = 0;
6486
6487 /* set up vector assignment tracking */
6488 size = sizeof(struct i40e_lump_tracking)
6489 + (sizeof(u16) * pf->hw.func_caps.num_msix_vectors);
6490 pf->irq_pile = kzalloc(size, GFP_KERNEL);
6491 if (!pf->irq_pile) {
6492 kfree(pf->qp_pile);
6493 err = -ENOMEM;
6494 goto sw_init_done;
6495 }
6496 pf->irq_pile->num_entries = pf->hw.func_caps.num_msix_vectors;
6497 pf->irq_pile->search_hint = 0;
6498
6499 mutex_init(&pf->switch_mutex);
6500
6501sw_init_done:
6502 return err;
6503}
6504
6505/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00006506 * i40e_set_ntuple - set the ntuple feature flag and take action
6507 * @pf: board private structure to initialize
6508 * @features: the feature set that the stack is suggesting
6509 *
6510 * returns a bool to indicate if reset needs to happen
6511 **/
6512bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
6513{
6514 bool need_reset = false;
6515
6516 /* Check if Flow Director n-tuple support was enabled or disabled. If
6517 * the state changed, we need to reset.
6518 */
6519 if (features & NETIF_F_NTUPLE) {
6520 /* Enable filters and mark for reset */
6521 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
6522 need_reset = true;
6523 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
6524 } else {
6525 /* turn off filters, mark for reset and clear SW filter list */
6526 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
6527 need_reset = true;
6528 i40e_fdir_filter_exit(pf);
6529 }
6530 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6531 /* if ATR was disabled it can be re-enabled. */
6532 if (!(pf->flags & I40E_FLAG_FD_ATR_ENABLED))
6533 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
6534 }
6535 return need_reset;
6536}
6537
6538/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006539 * i40e_set_features - set the netdev feature flags
6540 * @netdev: ptr to the netdev being adjusted
6541 * @features: the feature set that the stack is suggesting
6542 **/
6543static int i40e_set_features(struct net_device *netdev,
6544 netdev_features_t features)
6545{
6546 struct i40e_netdev_priv *np = netdev_priv(netdev);
6547 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00006548 struct i40e_pf *pf = vsi->back;
6549 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006550
6551 if (features & NETIF_F_HW_VLAN_CTAG_RX)
6552 i40e_vlan_stripping_enable(vsi);
6553 else
6554 i40e_vlan_stripping_disable(vsi);
6555
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00006556 need_reset = i40e_set_ntuple(pf, features);
6557
6558 if (need_reset)
6559 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
6560
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006561 return 0;
6562}
6563
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006564#ifdef CONFIG_I40E_VXLAN
6565/**
6566 * i40e_get_vxlan_port_idx - Lookup a possibly offloaded for Rx UDP port
6567 * @pf: board private structure
6568 * @port: The UDP port to look up
6569 *
6570 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
6571 **/
6572static u8 i40e_get_vxlan_port_idx(struct i40e_pf *pf, __be16 port)
6573{
6574 u8 i;
6575
6576 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
6577 if (pf->vxlan_ports[i] == port)
6578 return i;
6579 }
6580
6581 return i;
6582}
6583
6584/**
6585 * i40e_add_vxlan_port - Get notifications about VXLAN ports that come up
6586 * @netdev: This physical port's netdev
6587 * @sa_family: Socket Family that VXLAN is notifying us about
6588 * @port: New UDP port number that VXLAN started listening to
6589 **/
6590static void i40e_add_vxlan_port(struct net_device *netdev,
6591 sa_family_t sa_family, __be16 port)
6592{
6593 struct i40e_netdev_priv *np = netdev_priv(netdev);
6594 struct i40e_vsi *vsi = np->vsi;
6595 struct i40e_pf *pf = vsi->back;
6596 u8 next_idx;
6597 u8 idx;
6598
6599 if (sa_family == AF_INET6)
6600 return;
6601
6602 idx = i40e_get_vxlan_port_idx(pf, port);
6603
6604 /* Check if port already exists */
6605 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6606 netdev_info(netdev, "Port %d already offloaded\n", ntohs(port));
6607 return;
6608 }
6609
6610 /* Now check if there is space to add the new port */
6611 next_idx = i40e_get_vxlan_port_idx(pf, 0);
6612
6613 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6614 netdev_info(netdev, "Maximum number of UDP ports reached, not adding port %d\n",
6615 ntohs(port));
6616 return;
6617 }
6618
6619 /* New port: add it and mark its index in the bitmap */
6620 pf->vxlan_ports[next_idx] = port;
6621 pf->pending_vxlan_bitmap |= (1 << next_idx);
6622
6623 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
6624}
6625
6626/**
6627 * i40e_del_vxlan_port - Get notifications about VXLAN ports that go away
6628 * @netdev: This physical port's netdev
6629 * @sa_family: Socket Family that VXLAN is notifying us about
6630 * @port: UDP port number that VXLAN stopped listening to
6631 **/
6632static void i40e_del_vxlan_port(struct net_device *netdev,
6633 sa_family_t sa_family, __be16 port)
6634{
6635 struct i40e_netdev_priv *np = netdev_priv(netdev);
6636 struct i40e_vsi *vsi = np->vsi;
6637 struct i40e_pf *pf = vsi->back;
6638 u8 idx;
6639
6640 if (sa_family == AF_INET6)
6641 return;
6642
6643 idx = i40e_get_vxlan_port_idx(pf, port);
6644
6645 /* Check if port already exists */
6646 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6647 /* if port exists, set it to 0 (mark for deletion)
6648 * and make it pending
6649 */
6650 pf->vxlan_ports[idx] = 0;
6651
6652 pf->pending_vxlan_bitmap |= (1 << idx);
6653
6654 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
6655 } else {
6656 netdev_warn(netdev, "Port %d was not found, not deleting\n",
6657 ntohs(port));
6658 }
6659}
6660
6661#endif
Greg Rose4ba0dea2014-03-06 08:59:55 +00006662#ifdef HAVE_FDB_OPS
6663#ifdef USE_CONST_DEV_UC_CHAR
6664static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
6665 struct net_device *dev,
6666 const unsigned char *addr,
6667 u16 flags)
6668#else
6669static int i40e_ndo_fdb_add(struct ndmsg *ndm,
6670 struct net_device *dev,
6671 unsigned char *addr,
6672 u16 flags)
6673#endif
6674{
6675 struct i40e_netdev_priv *np = netdev_priv(dev);
6676 struct i40e_pf *pf = np->vsi->back;
6677 int err = 0;
6678
6679 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
6680 return -EOPNOTSUPP;
6681
6682 /* Hardware does not support aging addresses so if a
6683 * ndm_state is given only allow permanent addresses
6684 */
6685 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
6686 netdev_info(dev, "FDB only supports static addresses\n");
6687 return -EINVAL;
6688 }
6689
6690 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
6691 err = dev_uc_add_excl(dev, addr);
6692 else if (is_multicast_ether_addr(addr))
6693 err = dev_mc_add_excl(dev, addr);
6694 else
6695 err = -EINVAL;
6696
6697 /* Only return duplicate errors if NLM_F_EXCL is set */
6698 if (err == -EEXIST && !(flags & NLM_F_EXCL))
6699 err = 0;
6700
6701 return err;
6702}
6703
6704#ifndef USE_DEFAULT_FDB_DEL_DUMP
6705#ifdef USE_CONST_DEV_UC_CHAR
6706static int i40e_ndo_fdb_del(struct ndmsg *ndm,
6707 struct net_device *dev,
6708 const unsigned char *addr)
6709#else
6710static int i40e_ndo_fdb_del(struct ndmsg *ndm,
6711 struct net_device *dev,
6712 unsigned char *addr)
6713#endif
6714{
6715 struct i40e_netdev_priv *np = netdev_priv(dev);
6716 struct i40e_pf *pf = np->vsi->back;
6717 int err = -EOPNOTSUPP;
6718
6719 if (ndm->ndm_state & NUD_PERMANENT) {
6720 netdev_info(dev, "FDB only supports static addresses\n");
6721 return -EINVAL;
6722 }
6723
6724 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
6725 if (is_unicast_ether_addr(addr))
6726 err = dev_uc_del(dev, addr);
6727 else if (is_multicast_ether_addr(addr))
6728 err = dev_mc_del(dev, addr);
6729 else
6730 err = -EINVAL;
6731 }
6732
6733 return err;
6734}
6735
6736static int i40e_ndo_fdb_dump(struct sk_buff *skb,
6737 struct netlink_callback *cb,
6738 struct net_device *dev,
6739 int idx)
6740{
6741 struct i40e_netdev_priv *np = netdev_priv(dev);
6742 struct i40e_pf *pf = np->vsi->back;
6743
6744 if (pf->flags & I40E_FLAG_SRIOV_ENABLED)
6745 idx = ndo_dflt_fdb_dump(skb, cb, dev, idx);
6746
6747 return idx;
6748}
6749
6750#endif /* USE_DEFAULT_FDB_DEL_DUMP */
6751#endif /* HAVE_FDB_OPS */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006752static const struct net_device_ops i40e_netdev_ops = {
6753 .ndo_open = i40e_open,
6754 .ndo_stop = i40e_close,
6755 .ndo_start_xmit = i40e_lan_xmit_frame,
6756 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
6757 .ndo_set_rx_mode = i40e_set_rx_mode,
6758 .ndo_validate_addr = eth_validate_addr,
6759 .ndo_set_mac_address = i40e_set_mac,
6760 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006761 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006762 .ndo_tx_timeout = i40e_tx_timeout,
6763 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
6764 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
6765#ifdef CONFIG_NET_POLL_CONTROLLER
6766 .ndo_poll_controller = i40e_netpoll,
6767#endif
6768 .ndo_setup_tc = i40e_setup_tc,
6769 .ndo_set_features = i40e_set_features,
6770 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
6771 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04006772 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006773 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00006774 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006775#ifdef CONFIG_I40E_VXLAN
6776 .ndo_add_vxlan_port = i40e_add_vxlan_port,
6777 .ndo_del_vxlan_port = i40e_del_vxlan_port,
6778#endif
Greg Rose4ba0dea2014-03-06 08:59:55 +00006779#ifdef HAVE_FDB_OPS
6780 .ndo_fdb_add = i40e_ndo_fdb_add,
6781#ifndef USE_DEFAULT_FDB_DEL_DUMP
6782 .ndo_fdb_del = i40e_ndo_fdb_del,
6783 .ndo_fdb_dump = i40e_ndo_fdb_dump,
6784#endif
6785#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006786};
6787
6788/**
6789 * i40e_config_netdev - Setup the netdev flags
6790 * @vsi: the VSI being configured
6791 *
6792 * Returns 0 on success, negative value on failure
6793 **/
6794static int i40e_config_netdev(struct i40e_vsi *vsi)
6795{
Greg Rose1a103702013-11-28 06:42:39 +00006796 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006797 struct i40e_pf *pf = vsi->back;
6798 struct i40e_hw *hw = &pf->hw;
6799 struct i40e_netdev_priv *np;
6800 struct net_device *netdev;
6801 u8 mac_addr[ETH_ALEN];
6802 int etherdev_size;
6803
6804 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006805 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006806 if (!netdev)
6807 return -ENOMEM;
6808
6809 vsi->netdev = netdev;
6810 np = netdev_priv(netdev);
6811 np->vsi = vsi;
6812
Or Gerlitzd70e9412014-03-18 10:36:45 +02006813 netdev->hw_enc_features |= NETIF_F_IP_CSUM |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006814 NETIF_F_GSO_UDP_TUNNEL |
Or Gerlitzd70e9412014-03-18 10:36:45 +02006815 NETIF_F_TSO;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006816
6817 netdev->features = NETIF_F_SG |
6818 NETIF_F_IP_CSUM |
6819 NETIF_F_SCTP_CSUM |
6820 NETIF_F_HIGHDMA |
6821 NETIF_F_GSO_UDP_TUNNEL |
6822 NETIF_F_HW_VLAN_CTAG_TX |
6823 NETIF_F_HW_VLAN_CTAG_RX |
6824 NETIF_F_HW_VLAN_CTAG_FILTER |
6825 NETIF_F_IPV6_CSUM |
6826 NETIF_F_TSO |
Jesse Brandeburg059dab62014-04-01 09:07:20 +00006827 NETIF_F_TSO_ECN |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006828 NETIF_F_TSO6 |
6829 NETIF_F_RXCSUM |
6830 NETIF_F_RXHASH |
6831 0;
6832
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00006833 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
6834 netdev->features |= NETIF_F_NTUPLE;
6835
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006836 /* copy netdev features into list of user selectable features */
6837 netdev->hw_features |= netdev->features;
6838
6839 if (vsi->type == I40E_VSI_MAIN) {
6840 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
6841 memcpy(mac_addr, hw->mac.perm_addr, ETH_ALEN);
6842 } else {
6843 /* relate the VSI_VMDQ name to the VSI_MAIN name */
6844 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
6845 pf->vsi[pf->lan_vsi]->netdev->name);
6846 random_ether_addr(mac_addr);
6847 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
6848 }
Greg Rose1a103702013-11-28 06:42:39 +00006849 i40e_add_filter(vsi, brdcast, I40E_VLAN_ANY, false, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006850
6851 memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
6852 memcpy(netdev->perm_addr, mac_addr, ETH_ALEN);
6853 /* vlan gets same features (except vlan offload)
6854 * after any tweaks for specific VSI types
6855 */
6856 netdev->vlan_features = netdev->features & ~(NETIF_F_HW_VLAN_CTAG_TX |
6857 NETIF_F_HW_VLAN_CTAG_RX |
6858 NETIF_F_HW_VLAN_CTAG_FILTER);
6859 netdev->priv_flags |= IFF_UNICAST_FLT;
6860 netdev->priv_flags |= IFF_SUPP_NOFCS;
6861 /* Setup netdev TC information */
6862 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
6863
6864 netdev->netdev_ops = &i40e_netdev_ops;
6865 netdev->watchdog_timeo = 5 * HZ;
6866 i40e_set_ethtool_ops(netdev);
6867
6868 return 0;
6869}
6870
6871/**
6872 * i40e_vsi_delete - Delete a VSI from the switch
6873 * @vsi: the VSI being removed
6874 *
6875 * Returns 0 on success, negative value on failure
6876 **/
6877static void i40e_vsi_delete(struct i40e_vsi *vsi)
6878{
6879 /* remove default VSI is not allowed */
6880 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
6881 return;
6882
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006883 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006884}
6885
6886/**
6887 * i40e_add_vsi - Add a VSI to the switch
6888 * @vsi: the VSI being configured
6889 *
6890 * This initializes a VSI context depending on the VSI type to be added and
6891 * passes it down to the add_vsi aq command.
6892 **/
6893static int i40e_add_vsi(struct i40e_vsi *vsi)
6894{
6895 int ret = -ENODEV;
6896 struct i40e_mac_filter *f, *ftmp;
6897 struct i40e_pf *pf = vsi->back;
6898 struct i40e_hw *hw = &pf->hw;
6899 struct i40e_vsi_context ctxt;
6900 u8 enabled_tc = 0x1; /* TC0 enabled */
6901 int f_count = 0;
6902
6903 memset(&ctxt, 0, sizeof(ctxt));
6904 switch (vsi->type) {
6905 case I40E_VSI_MAIN:
6906 /* The PF's main VSI is already setup as part of the
6907 * device initialization, so we'll not bother with
6908 * the add_vsi call, but we will retrieve the current
6909 * VSI context.
6910 */
6911 ctxt.seid = pf->main_vsi_seid;
6912 ctxt.pf_num = pf->hw.pf_id;
6913 ctxt.vf_num = 0;
6914 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6915 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6916 if (ret) {
6917 dev_info(&pf->pdev->dev,
6918 "couldn't get pf vsi config, err %d, aq_err %d\n",
6919 ret, pf->hw.aq.asq_last_status);
6920 return -ENOENT;
6921 }
6922 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
6923 vsi->info.valid_sections = 0;
6924
6925 vsi->seid = ctxt.seid;
6926 vsi->id = ctxt.vsi_number;
6927
6928 enabled_tc = i40e_pf_get_tc_map(pf);
6929
6930 /* MFP mode setup queue map and update VSI */
6931 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
6932 memset(&ctxt, 0, sizeof(ctxt));
6933 ctxt.seid = pf->main_vsi_seid;
6934 ctxt.pf_num = pf->hw.pf_id;
6935 ctxt.vf_num = 0;
6936 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
6937 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
6938 if (ret) {
6939 dev_info(&pf->pdev->dev,
6940 "update vsi failed, aq_err=%d\n",
6941 pf->hw.aq.asq_last_status);
6942 ret = -ENOENT;
6943 goto err;
6944 }
6945 /* update the local VSI info queue map */
6946 i40e_vsi_update_queue_map(vsi, &ctxt);
6947 vsi->info.valid_sections = 0;
6948 } else {
6949 /* Default/Main VSI is only enabled for TC0
6950 * reconfigure it to enable all TCs that are
6951 * available on the port in SFP mode.
6952 */
6953 ret = i40e_vsi_config_tc(vsi, enabled_tc);
6954 if (ret) {
6955 dev_info(&pf->pdev->dev,
6956 "failed to configure TCs for main VSI tc_map 0x%08x, err %d, aq_err %d\n",
6957 enabled_tc, ret,
6958 pf->hw.aq.asq_last_status);
6959 ret = -ENOENT;
6960 }
6961 }
6962 break;
6963
6964 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006965 ctxt.pf_num = hw->pf_id;
6966 ctxt.vf_num = 0;
6967 ctxt.uplink_seid = vsi->uplink_seid;
6968 ctxt.connection_type = 0x1; /* regular data port */
6969 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006970 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006971 break;
6972
6973 case I40E_VSI_VMDQ2:
6974 ctxt.pf_num = hw->pf_id;
6975 ctxt.vf_num = 0;
6976 ctxt.uplink_seid = vsi->uplink_seid;
6977 ctxt.connection_type = 0x1; /* regular data port */
6978 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6979
6980 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6981
6982 /* This VSI is connected to VEB so the switch_id
6983 * should be set to zero by default.
6984 */
6985 ctxt.info.switch_id = 0;
6986 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
6987 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6988
6989 /* Setup the VSI tx/rx queue map for TC0 only for now */
6990 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
6991 break;
6992
6993 case I40E_VSI_SRIOV:
6994 ctxt.pf_num = hw->pf_id;
6995 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
6996 ctxt.uplink_seid = vsi->uplink_seid;
6997 ctxt.connection_type = 0x1; /* regular data port */
6998 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
6999
7000 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
7001
7002 /* This VSI is connected to VEB so the switch_id
7003 * should be set to zero by default.
7004 */
7005 ctxt.info.switch_id = cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
7006
7007 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
7008 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
7009 /* Setup the VSI tx/rx queue map for TC0 only for now */
7010 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
7011 break;
7012
7013 default:
7014 return -ENODEV;
7015 }
7016
7017 if (vsi->type != I40E_VSI_MAIN) {
7018 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
7019 if (ret) {
7020 dev_info(&vsi->back->pdev->dev,
7021 "add vsi failed, aq_err=%d\n",
7022 vsi->back->hw.aq.asq_last_status);
7023 ret = -ENOENT;
7024 goto err;
7025 }
7026 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
7027 vsi->info.valid_sections = 0;
7028 vsi->seid = ctxt.seid;
7029 vsi->id = ctxt.vsi_number;
7030 }
7031
7032 /* If macvlan filters already exist, force them to get loaded */
7033 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
7034 f->changed = true;
7035 f_count++;
7036 }
7037 if (f_count) {
7038 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
7039 pf->flags |= I40E_FLAG_FILTER_SYNC;
7040 }
7041
7042 /* Update VSI BW information */
7043 ret = i40e_vsi_get_bw_info(vsi);
7044 if (ret) {
7045 dev_info(&pf->pdev->dev,
7046 "couldn't get vsi bw info, err %d, aq_err %d\n",
7047 ret, pf->hw.aq.asq_last_status);
7048 /* VSI is already added so not tearing that up */
7049 ret = 0;
7050 }
7051
7052err:
7053 return ret;
7054}
7055
7056/**
7057 * i40e_vsi_release - Delete a VSI and free its resources
7058 * @vsi: the VSI being removed
7059 *
7060 * Returns 0 on success or < 0 on error
7061 **/
7062int i40e_vsi_release(struct i40e_vsi *vsi)
7063{
7064 struct i40e_mac_filter *f, *ftmp;
7065 struct i40e_veb *veb = NULL;
7066 struct i40e_pf *pf;
7067 u16 uplink_seid;
7068 int i, n;
7069
7070 pf = vsi->back;
7071
7072 /* release of a VEB-owner or last VSI is not allowed */
7073 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
7074 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
7075 vsi->seid, vsi->uplink_seid);
7076 return -ENODEV;
7077 }
7078 if (vsi == pf->vsi[pf->lan_vsi] &&
7079 !test_bit(__I40E_DOWN, &pf->state)) {
7080 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
7081 return -ENODEV;
7082 }
7083
7084 uplink_seid = vsi->uplink_seid;
7085 if (vsi->type != I40E_VSI_SRIOV) {
7086 if (vsi->netdev_registered) {
7087 vsi->netdev_registered = false;
7088 if (vsi->netdev) {
7089 /* results in a call to i40e_close() */
7090 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007091 }
7092 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00007093 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007094 }
7095 i40e_vsi_disable_irq(vsi);
7096 }
7097
7098 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
7099 i40e_del_filter(vsi, f->macaddr, f->vlan,
7100 f->is_vf, f->is_netdev);
7101 i40e_sync_vsi_filters(vsi);
7102
7103 i40e_vsi_delete(vsi);
7104 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00007105 if (vsi->netdev) {
7106 free_netdev(vsi->netdev);
7107 vsi->netdev = NULL;
7108 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007109 i40e_vsi_clear_rings(vsi);
7110 i40e_vsi_clear(vsi);
7111
7112 /* If this was the last thing on the VEB, except for the
7113 * controlling VSI, remove the VEB, which puts the controlling
7114 * VSI onto the next level down in the switch.
7115 *
7116 * Well, okay, there's one more exception here: don't remove
7117 * the orphan VEBs yet. We'll wait for an explicit remove request
7118 * from up the network stack.
7119 */
7120 for (n = 0, i = 0; i < pf->hw.func_caps.num_vsis; i++) {
7121 if (pf->vsi[i] &&
7122 pf->vsi[i]->uplink_seid == uplink_seid &&
7123 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
7124 n++; /* count the VSIs */
7125 }
7126 }
7127 for (i = 0; i < I40E_MAX_VEB; i++) {
7128 if (!pf->veb[i])
7129 continue;
7130 if (pf->veb[i]->uplink_seid == uplink_seid)
7131 n++; /* count the VEBs */
7132 if (pf->veb[i]->seid == uplink_seid)
7133 veb = pf->veb[i];
7134 }
7135 if (n == 0 && veb && veb->uplink_seid != 0)
7136 i40e_veb_release(veb);
7137
7138 return 0;
7139}
7140
7141/**
7142 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
7143 * @vsi: ptr to the VSI
7144 *
7145 * This should only be called after i40e_vsi_mem_alloc() which allocates the
7146 * corresponding SW VSI structure and initializes num_queue_pairs for the
7147 * newly allocated VSI.
7148 *
7149 * Returns 0 on success or negative on failure
7150 **/
7151static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
7152{
7153 int ret = -ENOENT;
7154 struct i40e_pf *pf = vsi->back;
7155
Alexander Duyck493fb302013-09-28 07:01:44 +00007156 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007157 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
7158 vsi->seid);
7159 return -EEXIST;
7160 }
7161
7162 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00007163 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007164 vsi->seid, vsi->base_vector);
7165 return -EEXIST;
7166 }
7167
Greg Rose90e04072014-03-06 08:59:57 +00007168 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007169 if (ret) {
7170 dev_info(&pf->pdev->dev,
7171 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
7172 vsi->num_q_vectors, vsi->seid, ret);
7173 vsi->num_q_vectors = 0;
7174 goto vector_setup_out;
7175 }
7176
Shannon Nelson958a3e32013-09-28 07:13:28 +00007177 if (vsi->num_q_vectors)
7178 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
7179 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007180 if (vsi->base_vector < 0) {
7181 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00007182 "failed to get queue tracking for VSI %d, err=%d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007183 vsi->seid, vsi->base_vector);
7184 i40e_vsi_free_q_vectors(vsi);
7185 ret = -ENOENT;
7186 goto vector_setup_out;
7187 }
7188
7189vector_setup_out:
7190 return ret;
7191}
7192
7193/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007194 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
7195 * @vsi: pointer to the vsi.
7196 *
7197 * This re-allocates a vsi's queue resources.
7198 *
7199 * Returns pointer to the successfully allocated and configured VSI sw struct
7200 * on success, otherwise returns NULL on failure.
7201 **/
7202static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
7203{
7204 struct i40e_pf *pf = vsi->back;
7205 u8 enabled_tc;
7206 int ret;
7207
7208 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7209 i40e_vsi_clear_rings(vsi);
7210
7211 i40e_vsi_free_arrays(vsi, false);
7212 i40e_set_num_rings_in_vsi(vsi);
7213 ret = i40e_vsi_alloc_arrays(vsi, false);
7214 if (ret)
7215 goto err_vsi;
7216
7217 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
7218 if (ret < 0) {
7219 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
7220 vsi->seid, ret);
7221 goto err_vsi;
7222 }
7223 vsi->base_queue = ret;
7224
7225 /* Update the FW view of the VSI. Force a reset of TC and queue
7226 * layout configurations.
7227 */
7228 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
7229 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
7230 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
7231 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
7232
7233 /* assign it some queues */
7234 ret = i40e_alloc_rings(vsi);
7235 if (ret)
7236 goto err_rings;
7237
7238 /* map all of the rings to the q_vectors */
7239 i40e_vsi_map_rings_to_vectors(vsi);
7240 return vsi;
7241
7242err_rings:
7243 i40e_vsi_free_q_vectors(vsi);
7244 if (vsi->netdev_registered) {
7245 vsi->netdev_registered = false;
7246 unregister_netdev(vsi->netdev);
7247 free_netdev(vsi->netdev);
7248 vsi->netdev = NULL;
7249 }
7250 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
7251err_vsi:
7252 i40e_vsi_clear(vsi);
7253 return NULL;
7254}
7255
7256/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007257 * i40e_vsi_setup - Set up a VSI by a given type
7258 * @pf: board private structure
7259 * @type: VSI type
7260 * @uplink_seid: the switch element to link to
7261 * @param1: usage depends upon VSI type. For VF types, indicates VF id
7262 *
7263 * This allocates the sw VSI structure and its queue resources, then add a VSI
7264 * to the identified VEB.
7265 *
7266 * Returns pointer to the successfully allocated and configure VSI sw struct on
7267 * success, otherwise returns NULL on failure.
7268 **/
7269struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
7270 u16 uplink_seid, u32 param1)
7271{
7272 struct i40e_vsi *vsi = NULL;
7273 struct i40e_veb *veb = NULL;
7274 int ret, i;
7275 int v_idx;
7276
7277 /* The requested uplink_seid must be either
7278 * - the PF's port seid
7279 * no VEB is needed because this is the PF
7280 * or this is a Flow Director special case VSI
7281 * - seid of an existing VEB
7282 * - seid of a VSI that owns an existing VEB
7283 * - seid of a VSI that doesn't own a VEB
7284 * a new VEB is created and the VSI becomes the owner
7285 * - seid of the PF VSI, which is what creates the first VEB
7286 * this is a special case of the previous
7287 *
7288 * Find which uplink_seid we were given and create a new VEB if needed
7289 */
7290 for (i = 0; i < I40E_MAX_VEB; i++) {
7291 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
7292 veb = pf->veb[i];
7293 break;
7294 }
7295 }
7296
7297 if (!veb && uplink_seid != pf->mac_seid) {
7298
7299 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
7300 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
7301 vsi = pf->vsi[i];
7302 break;
7303 }
7304 }
7305 if (!vsi) {
7306 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
7307 uplink_seid);
7308 return NULL;
7309 }
7310
7311 if (vsi->uplink_seid == pf->mac_seid)
7312 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
7313 vsi->tc_config.enabled_tc);
7314 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
7315 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
7316 vsi->tc_config.enabled_tc);
7317
7318 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
7319 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
7320 veb = pf->veb[i];
7321 }
7322 if (!veb) {
7323 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
7324 return NULL;
7325 }
7326
7327 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
7328 uplink_seid = veb->seid;
7329 }
7330
7331 /* get vsi sw struct */
7332 v_idx = i40e_vsi_mem_alloc(pf, type);
7333 if (v_idx < 0)
7334 goto err_alloc;
7335 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007336 if (!vsi)
7337 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007338 vsi->type = type;
7339 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
7340
7341 if (type == I40E_VSI_MAIN)
7342 pf->lan_vsi = v_idx;
7343 else if (type == I40E_VSI_SRIOV)
7344 vsi->vf_id = param1;
7345 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007346 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
7347 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007348 if (ret < 0) {
7349 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
7350 vsi->seid, ret);
7351 goto err_vsi;
7352 }
7353 vsi->base_queue = ret;
7354
7355 /* get a VSI from the hardware */
7356 vsi->uplink_seid = uplink_seid;
7357 ret = i40e_add_vsi(vsi);
7358 if (ret)
7359 goto err_vsi;
7360
7361 switch (vsi->type) {
7362 /* setup the netdev if needed */
7363 case I40E_VSI_MAIN:
7364 case I40E_VSI_VMDQ2:
7365 ret = i40e_config_netdev(vsi);
7366 if (ret)
7367 goto err_netdev;
7368 ret = register_netdev(vsi->netdev);
7369 if (ret)
7370 goto err_netdev;
7371 vsi->netdev_registered = true;
7372 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007373#ifdef CONFIG_I40E_DCB
7374 /* Setup DCB netlink interface */
7375 i40e_dcbnl_setup(vsi);
7376#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007377 /* fall through */
7378
7379 case I40E_VSI_FDIR:
7380 /* set up vectors and rings if needed */
7381 ret = i40e_vsi_setup_vectors(vsi);
7382 if (ret)
7383 goto err_msix;
7384
7385 ret = i40e_alloc_rings(vsi);
7386 if (ret)
7387 goto err_rings;
7388
7389 /* map all of the rings to the q_vectors */
7390 i40e_vsi_map_rings_to_vectors(vsi);
7391
7392 i40e_vsi_reset_stats(vsi);
7393 break;
7394
7395 default:
7396 /* no netdev or rings for the other VSI types */
7397 break;
7398 }
7399
7400 return vsi;
7401
7402err_rings:
7403 i40e_vsi_free_q_vectors(vsi);
7404err_msix:
7405 if (vsi->netdev_registered) {
7406 vsi->netdev_registered = false;
7407 unregister_netdev(vsi->netdev);
7408 free_netdev(vsi->netdev);
7409 vsi->netdev = NULL;
7410 }
7411err_netdev:
7412 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
7413err_vsi:
7414 i40e_vsi_clear(vsi);
7415err_alloc:
7416 return NULL;
7417}
7418
7419/**
7420 * i40e_veb_get_bw_info - Query VEB BW information
7421 * @veb: the veb to query
7422 *
7423 * Query the Tx scheduler BW configuration data for given VEB
7424 **/
7425static int i40e_veb_get_bw_info(struct i40e_veb *veb)
7426{
7427 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
7428 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
7429 struct i40e_pf *pf = veb->pf;
7430 struct i40e_hw *hw = &pf->hw;
7431 u32 tc_bw_max;
7432 int ret = 0;
7433 int i;
7434
7435 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
7436 &bw_data, NULL);
7437 if (ret) {
7438 dev_info(&pf->pdev->dev,
7439 "query veb bw config failed, aq_err=%d\n",
7440 hw->aq.asq_last_status);
7441 goto out;
7442 }
7443
7444 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
7445 &ets_data, NULL);
7446 if (ret) {
7447 dev_info(&pf->pdev->dev,
7448 "query veb bw ets config failed, aq_err=%d\n",
7449 hw->aq.asq_last_status);
7450 goto out;
7451 }
7452
7453 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
7454 veb->bw_max_quanta = ets_data.tc_bw_max;
7455 veb->is_abs_credits = bw_data.absolute_credits_enable;
7456 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
7457 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
7458 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7459 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
7460 veb->bw_tc_limit_credits[i] =
7461 le16_to_cpu(bw_data.tc_bw_limits[i]);
7462 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
7463 }
7464
7465out:
7466 return ret;
7467}
7468
7469/**
7470 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
7471 * @pf: board private structure
7472 *
7473 * On error: returns error code (negative)
7474 * On success: returns vsi index in PF (positive)
7475 **/
7476static int i40e_veb_mem_alloc(struct i40e_pf *pf)
7477{
7478 int ret = -ENOENT;
7479 struct i40e_veb *veb;
7480 int i;
7481
7482 /* Need to protect the allocation of switch elements at the PF level */
7483 mutex_lock(&pf->switch_mutex);
7484
7485 /* VEB list may be fragmented if VEB creation/destruction has
7486 * been happening. We can afford to do a quick scan to look
7487 * for any free slots in the list.
7488 *
7489 * find next empty veb slot, looping back around if necessary
7490 */
7491 i = 0;
7492 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
7493 i++;
7494 if (i >= I40E_MAX_VEB) {
7495 ret = -ENOMEM;
7496 goto err_alloc_veb; /* out of VEB slots! */
7497 }
7498
7499 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
7500 if (!veb) {
7501 ret = -ENOMEM;
7502 goto err_alloc_veb;
7503 }
7504 veb->pf = pf;
7505 veb->idx = i;
7506 veb->enabled_tc = 1;
7507
7508 pf->veb[i] = veb;
7509 ret = i;
7510err_alloc_veb:
7511 mutex_unlock(&pf->switch_mutex);
7512 return ret;
7513}
7514
7515/**
7516 * i40e_switch_branch_release - Delete a branch of the switch tree
7517 * @branch: where to start deleting
7518 *
7519 * This uses recursion to find the tips of the branch to be
7520 * removed, deleting until we get back to and can delete this VEB.
7521 **/
7522static void i40e_switch_branch_release(struct i40e_veb *branch)
7523{
7524 struct i40e_pf *pf = branch->pf;
7525 u16 branch_seid = branch->seid;
7526 u16 veb_idx = branch->idx;
7527 int i;
7528
7529 /* release any VEBs on this VEB - RECURSION */
7530 for (i = 0; i < I40E_MAX_VEB; i++) {
7531 if (!pf->veb[i])
7532 continue;
7533 if (pf->veb[i]->uplink_seid == branch->seid)
7534 i40e_switch_branch_release(pf->veb[i]);
7535 }
7536
7537 /* Release the VSIs on this VEB, but not the owner VSI.
7538 *
7539 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
7540 * the VEB itself, so don't use (*branch) after this loop.
7541 */
7542 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
7543 if (!pf->vsi[i])
7544 continue;
7545 if (pf->vsi[i]->uplink_seid == branch_seid &&
7546 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
7547 i40e_vsi_release(pf->vsi[i]);
7548 }
7549 }
7550
7551 /* There's one corner case where the VEB might not have been
7552 * removed, so double check it here and remove it if needed.
7553 * This case happens if the veb was created from the debugfs
7554 * commands and no VSIs were added to it.
7555 */
7556 if (pf->veb[veb_idx])
7557 i40e_veb_release(pf->veb[veb_idx]);
7558}
7559
7560/**
7561 * i40e_veb_clear - remove veb struct
7562 * @veb: the veb to remove
7563 **/
7564static void i40e_veb_clear(struct i40e_veb *veb)
7565{
7566 if (!veb)
7567 return;
7568
7569 if (veb->pf) {
7570 struct i40e_pf *pf = veb->pf;
7571
7572 mutex_lock(&pf->switch_mutex);
7573 if (pf->veb[veb->idx] == veb)
7574 pf->veb[veb->idx] = NULL;
7575 mutex_unlock(&pf->switch_mutex);
7576 }
7577
7578 kfree(veb);
7579}
7580
7581/**
7582 * i40e_veb_release - Delete a VEB and free its resources
7583 * @veb: the VEB being removed
7584 **/
7585void i40e_veb_release(struct i40e_veb *veb)
7586{
7587 struct i40e_vsi *vsi = NULL;
7588 struct i40e_pf *pf;
7589 int i, n = 0;
7590
7591 pf = veb->pf;
7592
7593 /* find the remaining VSI and check for extras */
7594 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
7595 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
7596 n++;
7597 vsi = pf->vsi[i];
7598 }
7599 }
7600 if (n != 1) {
7601 dev_info(&pf->pdev->dev,
7602 "can't remove VEB %d with %d VSIs left\n",
7603 veb->seid, n);
7604 return;
7605 }
7606
7607 /* move the remaining VSI to uplink veb */
7608 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
7609 if (veb->uplink_seid) {
7610 vsi->uplink_seid = veb->uplink_seid;
7611 if (veb->uplink_seid == pf->mac_seid)
7612 vsi->veb_idx = I40E_NO_VEB;
7613 else
7614 vsi->veb_idx = veb->veb_idx;
7615 } else {
7616 /* floating VEB */
7617 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
7618 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
7619 }
7620
7621 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
7622 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007623}
7624
7625/**
7626 * i40e_add_veb - create the VEB in the switch
7627 * @veb: the VEB to be instantiated
7628 * @vsi: the controlling VSI
7629 **/
7630static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
7631{
Greg Rose56747262013-11-28 06:39:37 +00007632 bool is_default = false;
Kevin Scotte1c51b952013-11-20 10:02:51 +00007633 bool is_cloud = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007634 int ret;
7635
7636 /* get a VEB from the hardware */
7637 ret = i40e_aq_add_veb(&veb->pf->hw, veb->uplink_seid, vsi->seid,
Kevin Scotte1c51b952013-11-20 10:02:51 +00007638 veb->enabled_tc, is_default,
7639 is_cloud, &veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007640 if (ret) {
7641 dev_info(&veb->pf->pdev->dev,
7642 "couldn't add VEB, err %d, aq_err %d\n",
7643 ret, veb->pf->hw.aq.asq_last_status);
7644 return -EPERM;
7645 }
7646
7647 /* get statistics counter */
7648 ret = i40e_aq_get_veb_parameters(&veb->pf->hw, veb->seid, NULL, NULL,
7649 &veb->stats_idx, NULL, NULL, NULL);
7650 if (ret) {
7651 dev_info(&veb->pf->pdev->dev,
7652 "couldn't get VEB statistics idx, err %d, aq_err %d\n",
7653 ret, veb->pf->hw.aq.asq_last_status);
7654 return -EPERM;
7655 }
7656 ret = i40e_veb_get_bw_info(veb);
7657 if (ret) {
7658 dev_info(&veb->pf->pdev->dev,
7659 "couldn't get VEB bw info, err %d, aq_err %d\n",
7660 ret, veb->pf->hw.aq.asq_last_status);
7661 i40e_aq_delete_element(&veb->pf->hw, veb->seid, NULL);
7662 return -ENOENT;
7663 }
7664
7665 vsi->uplink_seid = veb->seid;
7666 vsi->veb_idx = veb->idx;
7667 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
7668
7669 return 0;
7670}
7671
7672/**
7673 * i40e_veb_setup - Set up a VEB
7674 * @pf: board private structure
7675 * @flags: VEB setup flags
7676 * @uplink_seid: the switch element to link to
7677 * @vsi_seid: the initial VSI seid
7678 * @enabled_tc: Enabled TC bit-map
7679 *
7680 * This allocates the sw VEB structure and links it into the switch
7681 * It is possible and legal for this to be a duplicate of an already
7682 * existing VEB. It is also possible for both uplink and vsi seids
7683 * to be zero, in order to create a floating VEB.
7684 *
7685 * Returns pointer to the successfully allocated VEB sw struct on
7686 * success, otherwise returns NULL on failure.
7687 **/
7688struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
7689 u16 uplink_seid, u16 vsi_seid,
7690 u8 enabled_tc)
7691{
7692 struct i40e_veb *veb, *uplink_veb = NULL;
7693 int vsi_idx, veb_idx;
7694 int ret;
7695
7696 /* if one seid is 0, the other must be 0 to create a floating relay */
7697 if ((uplink_seid == 0 || vsi_seid == 0) &&
7698 (uplink_seid + vsi_seid != 0)) {
7699 dev_info(&pf->pdev->dev,
7700 "one, not both seid's are 0: uplink=%d vsi=%d\n",
7701 uplink_seid, vsi_seid);
7702 return NULL;
7703 }
7704
7705 /* make sure there is such a vsi and uplink */
7706 for (vsi_idx = 0; vsi_idx < pf->hw.func_caps.num_vsis; vsi_idx++)
7707 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
7708 break;
7709 if (vsi_idx >= pf->hw.func_caps.num_vsis && vsi_seid != 0) {
7710 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
7711 vsi_seid);
7712 return NULL;
7713 }
7714
7715 if (uplink_seid && uplink_seid != pf->mac_seid) {
7716 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
7717 if (pf->veb[veb_idx] &&
7718 pf->veb[veb_idx]->seid == uplink_seid) {
7719 uplink_veb = pf->veb[veb_idx];
7720 break;
7721 }
7722 }
7723 if (!uplink_veb) {
7724 dev_info(&pf->pdev->dev,
7725 "uplink seid %d not found\n", uplink_seid);
7726 return NULL;
7727 }
7728 }
7729
7730 /* get veb sw struct */
7731 veb_idx = i40e_veb_mem_alloc(pf);
7732 if (veb_idx < 0)
7733 goto err_alloc;
7734 veb = pf->veb[veb_idx];
7735 veb->flags = flags;
7736 veb->uplink_seid = uplink_seid;
7737 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
7738 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
7739
7740 /* create the VEB in the switch */
7741 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
7742 if (ret)
7743 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +00007744 if (vsi_idx == pf->lan_vsi)
7745 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007746
7747 return veb;
7748
7749err_veb:
7750 i40e_veb_clear(veb);
7751err_alloc:
7752 return NULL;
7753}
7754
7755/**
7756 * i40e_setup_pf_switch_element - set pf vars based on switch type
7757 * @pf: board private structure
7758 * @ele: element we are building info from
7759 * @num_reported: total number of elements
7760 * @printconfig: should we print the contents
7761 *
7762 * helper function to assist in extracting a few useful SEID values.
7763 **/
7764static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
7765 struct i40e_aqc_switch_config_element_resp *ele,
7766 u16 num_reported, bool printconfig)
7767{
7768 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
7769 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
7770 u8 element_type = ele->element_type;
7771 u16 seid = le16_to_cpu(ele->seid);
7772
7773 if (printconfig)
7774 dev_info(&pf->pdev->dev,
7775 "type=%d seid=%d uplink=%d downlink=%d\n",
7776 element_type, seid, uplink_seid, downlink_seid);
7777
7778 switch (element_type) {
7779 case I40E_SWITCH_ELEMENT_TYPE_MAC:
7780 pf->mac_seid = seid;
7781 break;
7782 case I40E_SWITCH_ELEMENT_TYPE_VEB:
7783 /* Main VEB? */
7784 if (uplink_seid != pf->mac_seid)
7785 break;
7786 if (pf->lan_veb == I40E_NO_VEB) {
7787 int v;
7788
7789 /* find existing or else empty VEB */
7790 for (v = 0; v < I40E_MAX_VEB; v++) {
7791 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
7792 pf->lan_veb = v;
7793 break;
7794 }
7795 }
7796 if (pf->lan_veb == I40E_NO_VEB) {
7797 v = i40e_veb_mem_alloc(pf);
7798 if (v < 0)
7799 break;
7800 pf->lan_veb = v;
7801 }
7802 }
7803
7804 pf->veb[pf->lan_veb]->seid = seid;
7805 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
7806 pf->veb[pf->lan_veb]->pf = pf;
7807 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
7808 break;
7809 case I40E_SWITCH_ELEMENT_TYPE_VSI:
7810 if (num_reported != 1)
7811 break;
7812 /* This is immediately after a reset so we can assume this is
7813 * the PF's VSI
7814 */
7815 pf->mac_seid = uplink_seid;
7816 pf->pf_seid = downlink_seid;
7817 pf->main_vsi_seid = seid;
7818 if (printconfig)
7819 dev_info(&pf->pdev->dev,
7820 "pf_seid=%d main_vsi_seid=%d\n",
7821 pf->pf_seid, pf->main_vsi_seid);
7822 break;
7823 case I40E_SWITCH_ELEMENT_TYPE_PF:
7824 case I40E_SWITCH_ELEMENT_TYPE_VF:
7825 case I40E_SWITCH_ELEMENT_TYPE_EMP:
7826 case I40E_SWITCH_ELEMENT_TYPE_BMC:
7827 case I40E_SWITCH_ELEMENT_TYPE_PE:
7828 case I40E_SWITCH_ELEMENT_TYPE_PA:
7829 /* ignore these for now */
7830 break;
7831 default:
7832 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
7833 element_type, seid);
7834 break;
7835 }
7836}
7837
7838/**
7839 * i40e_fetch_switch_configuration - Get switch config from firmware
7840 * @pf: board private structure
7841 * @printconfig: should we print the contents
7842 *
7843 * Get the current switch configuration from the device and
7844 * extract a few useful SEID values.
7845 **/
7846int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
7847{
7848 struct i40e_aqc_get_switch_config_resp *sw_config;
7849 u16 next_seid = 0;
7850 int ret = 0;
7851 u8 *aq_buf;
7852 int i;
7853
7854 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
7855 if (!aq_buf)
7856 return -ENOMEM;
7857
7858 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
7859 do {
7860 u16 num_reported, num_total;
7861
7862 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
7863 I40E_AQ_LARGE_BUF,
7864 &next_seid, NULL);
7865 if (ret) {
7866 dev_info(&pf->pdev->dev,
7867 "get switch config failed %d aq_err=%x\n",
7868 ret, pf->hw.aq.asq_last_status);
7869 kfree(aq_buf);
7870 return -ENOENT;
7871 }
7872
7873 num_reported = le16_to_cpu(sw_config->header.num_reported);
7874 num_total = le16_to_cpu(sw_config->header.num_total);
7875
7876 if (printconfig)
7877 dev_info(&pf->pdev->dev,
7878 "header: %d reported %d total\n",
7879 num_reported, num_total);
7880
7881 if (num_reported) {
7882 int sz = sizeof(*sw_config) * num_reported;
7883
7884 kfree(pf->sw_config);
7885 pf->sw_config = kzalloc(sz, GFP_KERNEL);
7886 if (pf->sw_config)
7887 memcpy(pf->sw_config, sw_config, sz);
7888 }
7889
7890 for (i = 0; i < num_reported; i++) {
7891 struct i40e_aqc_switch_config_element_resp *ele =
7892 &sw_config->element[i];
7893
7894 i40e_setup_pf_switch_element(pf, ele, num_reported,
7895 printconfig);
7896 }
7897 } while (next_seid != 0);
7898
7899 kfree(aq_buf);
7900 return ret;
7901}
7902
7903/**
7904 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
7905 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007906 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007907 *
7908 * Returns 0 on success, negative value on failure
7909 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007910static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007911{
Jesse Brandeburg895106a2013-11-26 10:49:16 +00007912 u32 rxfc = 0, txfc = 0, rxfc_reg;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007913 int ret;
7914
7915 /* find out what's out there already */
7916 ret = i40e_fetch_switch_configuration(pf, false);
7917 if (ret) {
7918 dev_info(&pf->pdev->dev,
7919 "couldn't fetch switch config, err %d, aq_err %d\n",
7920 ret, pf->hw.aq.asq_last_status);
7921 return ret;
7922 }
7923 i40e_pf_reset_stats(pf);
7924
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007925 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007926 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007927 struct i40e_vsi *vsi = NULL;
7928 u16 uplink_seid;
7929
7930 /* Set up the PF VSI associated with the PF's main VSI
7931 * that is already in the HW switch
7932 */
7933 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
7934 uplink_seid = pf->veb[pf->lan_veb]->seid;
7935 else
7936 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007937 if (pf->lan_vsi == I40E_NO_VSI)
7938 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
7939 else if (reinit)
7940 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007941 if (!vsi) {
7942 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
7943 i40e_fdir_teardown(pf);
7944 return -EAGAIN;
7945 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007946 } else {
7947 /* force a reset of TC and queue layout configurations */
7948 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
7949 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
7950 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
7951 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
7952 }
7953 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
7954
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007955 i40e_fdir_sb_setup(pf);
7956
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007957 /* Setup static PF queue filter control settings */
7958 ret = i40e_setup_pf_filter_control(pf);
7959 if (ret) {
7960 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
7961 ret);
7962 /* Failure here should not stop continuing other steps */
7963 }
7964
7965 /* enable RSS in the HW, even for only one queue, as the stack can use
7966 * the hash
7967 */
7968 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
7969 i40e_config_rss(pf);
7970
7971 /* fill in link information and enable LSE reporting */
7972 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
7973 i40e_link_event(pf);
7974
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007975 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007976 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
7977 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007978 /* requested_mode is set in probe or by ethtool */
7979 if (!pf->fc_autoneg_status)
7980 goto no_autoneg;
7981
7982 if ((pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX) &&
7983 (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007984 pf->hw.fc.current_mode = I40E_FC_FULL;
7985 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX)
7986 pf->hw.fc.current_mode = I40E_FC_TX_PAUSE;
7987 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX)
7988 pf->hw.fc.current_mode = I40E_FC_RX_PAUSE;
7989 else
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007990 pf->hw.fc.current_mode = I40E_FC_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007991
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007992 /* sync the flow control settings with the auto-neg values */
7993 switch (pf->hw.fc.current_mode) {
7994 case I40E_FC_FULL:
7995 txfc = 1;
7996 rxfc = 1;
7997 break;
7998 case I40E_FC_TX_PAUSE:
7999 txfc = 1;
8000 rxfc = 0;
8001 break;
8002 case I40E_FC_RX_PAUSE:
8003 txfc = 0;
8004 rxfc = 1;
8005 break;
8006 case I40E_FC_NONE:
8007 case I40E_FC_DEFAULT:
8008 txfc = 0;
8009 rxfc = 0;
8010 break;
8011 case I40E_FC_PFC:
8012 /* TBD */
8013 break;
8014 /* no default case, we have to handle all possibilities here */
8015 }
8016
8017 wr32(&pf->hw, I40E_PRTDCB_FCCFG, txfc << I40E_PRTDCB_FCCFG_TFCE_SHIFT);
8018
8019 rxfc_reg = rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
8020 ~I40E_PRTDCB_MFLCN_RFCE_MASK;
8021 rxfc_reg |= (rxfc << I40E_PRTDCB_MFLCN_RFCE_SHIFT);
8022
8023 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rxfc_reg);
8024
8025 goto fc_complete;
8026
8027no_autoneg:
8028 /* disable L2 flow control, user can turn it on if they wish */
8029 wr32(&pf->hw, I40E_PRTDCB_FCCFG, 0);
8030 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
8031 ~I40E_PRTDCB_MFLCN_RFCE_MASK);
8032
8033fc_complete:
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008034 i40e_ptp_init(pf);
8035
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008036 return ret;
8037}
8038
8039/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008040 * i40e_determine_queue_usage - Work out queue distribution
8041 * @pf: board private structure
8042 **/
8043static void i40e_determine_queue_usage(struct i40e_pf *pf)
8044{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008045 int queues_left;
8046
8047 pf->num_lan_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008048
8049 /* Find the max queues to be put into basic use. We'll always be
8050 * using TC0, whether or not DCB is running, and TC0 will get the
8051 * big RSS set.
8052 */
8053 queues_left = pf->hw.func_caps.num_tx_qp;
8054
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008055 if ((queues_left == 1) ||
8056 !(pf->flags & I40E_FLAG_MSIX_ENABLED) ||
8057 !(pf->flags & (I40E_FLAG_RSS_ENABLED | I40E_FLAG_FD_SB_ENABLED |
8058 I40E_FLAG_DCB_ENABLED))) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008059 /* one qp for PF, no queues for anything else */
8060 queues_left = 0;
8061 pf->rss_size = pf->num_lan_qps = 1;
8062
8063 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008064 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
8065 I40E_FLAG_FD_SB_ENABLED |
8066 I40E_FLAG_FD_ATR_ENABLED |
8067 I40E_FLAG_DCB_ENABLED |
8068 I40E_FLAG_SRIOV_ENABLED |
8069 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008070 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008071 /* Not enough queues for all TCs */
8072 if ((pf->flags & I40E_FLAG_DCB_ENABLED) &&
8073 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
8074 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8075 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
8076 }
8077 pf->num_lan_qps = pf->rss_size_max;
8078 queues_left -= pf->num_lan_qps;
8079 }
8080
8081 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8082 if (queues_left > 1) {
8083 queues_left -= 1; /* save 1 queue for FD */
8084 } else {
8085 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8086 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
8087 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008088 }
8089
8090 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
8091 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008092 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
8093 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008094 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
8095 }
8096
8097 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
8098 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
8099 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
8100 (queues_left / pf->num_vmdq_qps));
8101 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
8102 }
8103
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008104 pf->queues_left = queues_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008105}
8106
8107/**
8108 * i40e_setup_pf_filter_control - Setup PF static filter control
8109 * @pf: PF to be setup
8110 *
8111 * i40e_setup_pf_filter_control sets up a pf's initial filter control
8112 * settings. If PE/FCoE are enabled then it will also set the per PF
8113 * based filter sizes required for them. It also enables Flow director,
8114 * ethertype and macvlan type filter settings for the pf.
8115 *
8116 * Returns 0 on success, negative on failure
8117 **/
8118static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
8119{
8120 struct i40e_filter_control_settings *settings = &pf->filter_settings;
8121
8122 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
8123
8124 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008125 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008126 settings->enable_fdir = true;
8127
8128 /* Ethtype and MACVLAN filters enabled for PF */
8129 settings->enable_ethtype = true;
8130 settings->enable_macvlan = true;
8131
8132 if (i40e_set_filter_control(&pf->hw, settings))
8133 return -ENOENT;
8134
8135 return 0;
8136}
8137
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008138#define INFO_STRING_LEN 255
8139static void i40e_print_features(struct i40e_pf *pf)
8140{
8141 struct i40e_hw *hw = &pf->hw;
8142 char *buf, *string;
8143
8144 string = kzalloc(INFO_STRING_LEN, GFP_KERNEL);
8145 if (!string) {
8146 dev_err(&pf->pdev->dev, "Features string allocation failed\n");
8147 return;
8148 }
8149
8150 buf = string;
8151
8152 buf += sprintf(string, "Features: PF-id[%d] ", hw->pf_id);
8153#ifdef CONFIG_PCI_IOV
8154 buf += sprintf(buf, "VFs: %d ", pf->num_req_vfs);
8155#endif
8156 buf += sprintf(buf, "VSIs: %d QP: %d ", pf->hw.func_caps.num_vsis,
8157 pf->vsi[pf->lan_vsi]->num_queue_pairs);
8158
8159 if (pf->flags & I40E_FLAG_RSS_ENABLED)
8160 buf += sprintf(buf, "RSS ");
8161 buf += sprintf(buf, "FDir ");
8162 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
8163 buf += sprintf(buf, "ATR ");
8164 if (pf->flags & I40E_FLAG_FD_SB_ENABLED)
8165 buf += sprintf(buf, "NTUPLE ");
8166 if (pf->flags & I40E_FLAG_DCB_ENABLED)
8167 buf += sprintf(buf, "DCB ");
8168 if (pf->flags & I40E_FLAG_PTP)
8169 buf += sprintf(buf, "PTP ");
8170
8171 BUG_ON(buf > (string + INFO_STRING_LEN));
8172 dev_info(&pf->pdev->dev, "%s\n", string);
8173 kfree(string);
8174}
8175
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008176/**
8177 * i40e_probe - Device initialization routine
8178 * @pdev: PCI device information struct
8179 * @ent: entry in i40e_pci_tbl
8180 *
8181 * i40e_probe initializes a pf identified by a pci_dev structure.
8182 * The OS initialization, configuring of the pf private structure,
8183 * and a hardware reset occur.
8184 *
8185 * Returns 0 on success, negative on failure
8186 **/
8187static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
8188{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008189 struct i40e_pf *pf;
8190 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008191 static u16 pfs_found;
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008192 u16 link_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008193 int err = 0;
8194 u32 len;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008195 u32 i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008196
8197 err = pci_enable_device_mem(pdev);
8198 if (err)
8199 return err;
8200
8201 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +00008202 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00008203 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00008204 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
8205 if (err) {
8206 dev_err(&pdev->dev,
8207 "DMA configuration failed: 0x%x\n", err);
8208 goto err_dma;
8209 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008210 }
8211
8212 /* set up pci connections */
8213 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
8214 IORESOURCE_MEM), i40e_driver_name);
8215 if (err) {
8216 dev_info(&pdev->dev,
8217 "pci_request_selected_regions failed %d\n", err);
8218 goto err_pci_reg;
8219 }
8220
8221 pci_enable_pcie_error_reporting(pdev);
8222 pci_set_master(pdev);
8223
8224 /* Now that we have a PCI connection, we need to do the
8225 * low level device setup. This is primarily setting up
8226 * the Admin Queue structures and then querying for the
8227 * device's current profile information.
8228 */
8229 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
8230 if (!pf) {
8231 err = -ENOMEM;
8232 goto err_pf_alloc;
8233 }
8234 pf->next_vsi = 0;
8235 pf->pdev = pdev;
8236 set_bit(__I40E_DOWN, &pf->state);
8237
8238 hw = &pf->hw;
8239 hw->back = pf;
8240 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
8241 pci_resource_len(pdev, 0));
8242 if (!hw->hw_addr) {
8243 err = -EIO;
8244 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
8245 (unsigned int)pci_resource_start(pdev, 0),
8246 (unsigned int)pci_resource_len(pdev, 0), err);
8247 goto err_ioremap;
8248 }
8249 hw->vendor_id = pdev->vendor;
8250 hw->device_id = pdev->device;
8251 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
8252 hw->subsystem_vendor_id = pdev->subsystem_vendor;
8253 hw->subsystem_device_id = pdev->subsystem_device;
8254 hw->bus.device = PCI_SLOT(pdev->devfn);
8255 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008256 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008257
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008258 /* do a special CORER for clearing PXE mode once at init */
8259 if (hw->revision_id == 0 &&
8260 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
8261 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
8262 i40e_flush(hw);
8263 msleep(200);
8264 pf->corer_count++;
8265
8266 i40e_clear_pxe_mode(hw);
8267 }
8268
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008269 /* Reset here to make sure all is clean and to define PF 'n' */
8270 err = i40e_pf_reset(hw);
8271 if (err) {
8272 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
8273 goto err_pf_reset;
8274 }
8275 pf->pfr_count++;
8276
8277 hw->aq.num_arq_entries = I40E_AQ_LEN;
8278 hw->aq.num_asq_entries = I40E_AQ_LEN;
8279 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
8280 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
8281 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
8282 snprintf(pf->misc_int_name, sizeof(pf->misc_int_name) - 1,
8283 "%s-pf%d:misc",
8284 dev_driver_string(&pf->pdev->dev), pf->hw.pf_id);
8285
8286 err = i40e_init_shared_code(hw);
8287 if (err) {
8288 dev_info(&pdev->dev, "init_shared_code failed: %d\n", err);
8289 goto err_pf_reset;
8290 }
8291
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008292 /* set up a default setting for link flow control */
8293 pf->hw.fc.requested_mode = I40E_FC_NONE;
8294
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008295 err = i40e_init_adminq(hw);
8296 dev_info(&pdev->dev, "%s\n", i40e_fw_version_str(hw));
8297 if (err) {
8298 dev_info(&pdev->dev,
8299 "init_adminq failed: %d expecting API %02x.%02x\n",
8300 err,
8301 I40E_FW_API_VERSION_MAJOR, I40E_FW_API_VERSION_MINOR);
8302 goto err_pf_reset;
8303 }
8304
Shannon Nelson4eb3f762014-03-06 08:59:58 +00008305 i40e_verify_eeprom(pf);
8306
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +00008307 /* Rev 0 hardware was never productized */
8308 if (hw->revision_id < 1)
8309 dev_warn(&pdev->dev, "This device is a pre-production adapter/LOM. Please be aware there may be issues with your hardware. If you are experiencing problems please contact your Intel or hardware representative who provided you with this hardware.\n");
8310
Shannon Nelson6ff4ef82013-12-21 05:44:49 +00008311 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008312 err = i40e_get_capabilities(pf);
8313 if (err)
8314 goto err_adminq_setup;
8315
8316 err = i40e_sw_init(pf);
8317 if (err) {
8318 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
8319 goto err_sw_init;
8320 }
8321
8322 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
8323 hw->func_caps.num_rx_qp,
8324 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
8325 if (err) {
8326 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
8327 goto err_init_lan_hmc;
8328 }
8329
8330 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
8331 if (err) {
8332 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
8333 err = -ENOENT;
8334 goto err_configure_lan_hmc;
8335 }
8336
8337 i40e_get_mac_addr(hw, hw->mac.addr);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +00008338 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008339 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
8340 err = -EIO;
8341 goto err_mac_addr;
8342 }
8343 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
8344 memcpy(hw->mac.perm_addr, hw->mac.addr, ETH_ALEN);
8345
8346 pci_set_drvdata(pdev, pf);
8347 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008348#ifdef CONFIG_I40E_DCB
8349 err = i40e_init_pf_dcb(pf);
8350 if (err) {
8351 dev_info(&pdev->dev, "init_pf_dcb failed: %d\n", err);
8352 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh014269f2014-04-01 07:11:48 +00008353 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008354 }
8355#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008356
8357 /* set up periodic task facility */
8358 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
8359 pf->service_timer_period = HZ;
8360
8361 INIT_WORK(&pf->service_task, i40e_service_task);
8362 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
8363 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
8364 pf->link_check_timeout = jiffies;
8365
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008366 /* WoL defaults to disabled */
8367 pf->wol_en = false;
8368 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
8369
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008370 /* set up the main switch operations */
8371 i40e_determine_queue_usage(pf);
8372 i40e_init_interrupt_scheme(pf);
8373
8374 /* Set up the *vsi struct based on the number of VSIs in the HW,
8375 * and set up our local tracking of the MAIN PF vsi.
8376 */
8377 len = sizeof(struct i40e_vsi *) * pf->hw.func_caps.num_vsis;
8378 pf->vsi = kzalloc(len, GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +00008379 if (!pf->vsi) {
8380 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008381 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +00008382 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008383
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008384 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008385 if (err) {
8386 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
8387 goto err_vsis;
8388 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008389 /* if FDIR VSI was set up, start it now */
8390 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
8391 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
8392 i40e_vsi_open(pf->vsi[i]);
8393 break;
8394 }
8395 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008396
8397 /* The main driver is (mostly) up and happy. We need to set this state
8398 * before setting up the misc vector or we get a race and the vector
8399 * ends up disabled forever.
8400 */
8401 clear_bit(__I40E_DOWN, &pf->state);
8402
8403 /* In case of MSIX we are going to setup the misc vector right here
8404 * to handle admin queue events etc. In case of legacy and MSI
8405 * the misc functionality and queue processing is combined in
8406 * the same vector and that gets setup at open.
8407 */
8408 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
8409 err = i40e_setup_misc_vector(pf);
8410 if (err) {
8411 dev_info(&pdev->dev,
8412 "setup of misc vector failed: %d\n", err);
8413 goto err_vsis;
8414 }
8415 }
8416
Greg Rosedf805f62014-04-04 04:43:16 +00008417#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008418 /* prep for VF support */
8419 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +00008420 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
8421 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008422 u32 val;
8423
8424 /* disable link interrupts for VFs */
8425 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
8426 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
8427 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
8428 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -08008429
8430 if (pci_num_vf(pdev)) {
8431 dev_info(&pdev->dev,
8432 "Active VFs found, allocating resources.\n");
8433 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
8434 if (err)
8435 dev_info(&pdev->dev,
8436 "Error %d allocating resources for existing VFs\n",
8437 err);
8438 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008439 }
Greg Rosedf805f62014-04-04 04:43:16 +00008440#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008441
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008442 pfs_found++;
8443
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008444 i40e_dbg_pf_init(pf);
8445
8446 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00008447 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008448
8449 /* since everything's happy, start the service_task timer */
8450 mod_timer(&pf->service_timer,
8451 round_jiffies(jiffies + pf->service_timer_period));
8452
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008453 /* Get the negotiated link width and speed from PCI config space */
8454 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA, &link_status);
8455
8456 i40e_set_pci_config_data(hw, link_status);
8457
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00008458 dev_info(&pdev->dev, "PCI-Express: %s %s\n",
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008459 (hw->bus.speed == i40e_bus_speed_8000 ? "Speed 8.0GT/s" :
8460 hw->bus.speed == i40e_bus_speed_5000 ? "Speed 5.0GT/s" :
8461 hw->bus.speed == i40e_bus_speed_2500 ? "Speed 2.5GT/s" :
8462 "Unknown"),
8463 (hw->bus.width == i40e_bus_width_pcie_x8 ? "Width x8" :
8464 hw->bus.width == i40e_bus_width_pcie_x4 ? "Width x4" :
8465 hw->bus.width == i40e_bus_width_pcie_x2 ? "Width x2" :
8466 hw->bus.width == i40e_bus_width_pcie_x1 ? "Width x1" :
8467 "Unknown"));
8468
8469 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
8470 hw->bus.speed < i40e_bus_speed_8000) {
8471 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
8472 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
8473 }
8474
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008475 /* print a string summarizing features */
8476 i40e_print_features(pf);
8477
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008478 return 0;
8479
8480 /* Unwind what we've done if something failed in the setup */
8481err_vsis:
8482 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008483 i40e_clear_interrupt_scheme(pf);
8484 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +00008485err_switch_setup:
8486 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008487 del_timer_sync(&pf->service_timer);
8488err_mac_addr:
8489err_configure_lan_hmc:
8490 (void)i40e_shutdown_lan_hmc(hw);
8491err_init_lan_hmc:
8492 kfree(pf->qp_pile);
8493 kfree(pf->irq_pile);
8494err_sw_init:
8495err_adminq_setup:
8496 (void)i40e_shutdown_adminq(hw);
8497err_pf_reset:
8498 iounmap(hw->hw_addr);
8499err_ioremap:
8500 kfree(pf);
8501err_pf_alloc:
8502 pci_disable_pcie_error_reporting(pdev);
8503 pci_release_selected_regions(pdev,
8504 pci_select_bars(pdev, IORESOURCE_MEM));
8505err_pci_reg:
8506err_dma:
8507 pci_disable_device(pdev);
8508 return err;
8509}
8510
8511/**
8512 * i40e_remove - Device removal routine
8513 * @pdev: PCI device information struct
8514 *
8515 * i40e_remove is called by the PCI subsystem to alert the driver
8516 * that is should release a PCI device. This could be caused by a
8517 * Hot-Plug event, or because the driver is going to be removed from
8518 * memory.
8519 **/
8520static void i40e_remove(struct pci_dev *pdev)
8521{
8522 struct i40e_pf *pf = pci_get_drvdata(pdev);
8523 i40e_status ret_code;
8524 u32 reg;
8525 int i;
8526
8527 i40e_dbg_pf_exit(pf);
8528
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008529 i40e_ptp_stop(pf);
8530
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008531 /* no more scheduling of any task */
8532 set_bit(__I40E_DOWN, &pf->state);
8533 del_timer_sync(&pf->service_timer);
8534 cancel_work_sync(&pf->service_task);
8535
Mitch Williamseb2d80b2014-02-13 03:48:48 -08008536 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
8537 i40e_free_vfs(pf);
8538 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
8539 }
8540
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008541 i40e_fdir_teardown(pf);
8542
8543 /* If there is a switch structure or any orphans, remove them.
8544 * This will leave only the PF's VSI remaining.
8545 */
8546 for (i = 0; i < I40E_MAX_VEB; i++) {
8547 if (!pf->veb[i])
8548 continue;
8549
8550 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
8551 pf->veb[i]->uplink_seid == 0)
8552 i40e_switch_branch_release(pf->veb[i]);
8553 }
8554
8555 /* Now we can shutdown the PF's VSI, just before we kill
8556 * adminq and hmc.
8557 */
8558 if (pf->vsi[pf->lan_vsi])
8559 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
8560
8561 i40e_stop_misc_vector(pf);
8562 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
8563 synchronize_irq(pf->msix_entries[0].vector);
8564 free_irq(pf->msix_entries[0].vector, pf);
8565 }
8566
8567 /* shutdown and destroy the HMC */
8568 ret_code = i40e_shutdown_lan_hmc(&pf->hw);
8569 if (ret_code)
8570 dev_warn(&pdev->dev,
8571 "Failed to destroy the HMC resources: %d\n", ret_code);
8572
8573 /* shutdown the adminq */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008574 ret_code = i40e_shutdown_adminq(&pf->hw);
8575 if (ret_code)
8576 dev_warn(&pdev->dev,
8577 "Failed to destroy the Admin Queue resources: %d\n",
8578 ret_code);
8579
8580 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
8581 i40e_clear_interrupt_scheme(pf);
8582 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
8583 if (pf->vsi[i]) {
8584 i40e_vsi_clear_rings(pf->vsi[i]);
8585 i40e_vsi_clear(pf->vsi[i]);
8586 pf->vsi[i] = NULL;
8587 }
8588 }
8589
8590 for (i = 0; i < I40E_MAX_VEB; i++) {
8591 kfree(pf->veb[i]);
8592 pf->veb[i] = NULL;
8593 }
8594
8595 kfree(pf->qp_pile);
8596 kfree(pf->irq_pile);
8597 kfree(pf->sw_config);
8598 kfree(pf->vsi);
8599
8600 /* force a PF reset to clean anything leftover */
8601 reg = rd32(&pf->hw, I40E_PFGEN_CTRL);
8602 wr32(&pf->hw, I40E_PFGEN_CTRL, (reg | I40E_PFGEN_CTRL_PFSWR_MASK));
8603 i40e_flush(&pf->hw);
8604
8605 iounmap(pf->hw.hw_addr);
8606 kfree(pf);
8607 pci_release_selected_regions(pdev,
8608 pci_select_bars(pdev, IORESOURCE_MEM));
8609
8610 pci_disable_pcie_error_reporting(pdev);
8611 pci_disable_device(pdev);
8612}
8613
8614/**
8615 * i40e_pci_error_detected - warning that something funky happened in PCI land
8616 * @pdev: PCI device information struct
8617 *
8618 * Called to warn that something happened and the error handling steps
8619 * are in progress. Allows the driver to quiesce things, be ready for
8620 * remediation.
8621 **/
8622static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
8623 enum pci_channel_state error)
8624{
8625 struct i40e_pf *pf = pci_get_drvdata(pdev);
8626
8627 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
8628
8629 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008630 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
8631 rtnl_lock();
8632 i40e_prep_for_reset(pf);
8633 rtnl_unlock();
8634 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008635
8636 /* Request a slot reset */
8637 return PCI_ERS_RESULT_NEED_RESET;
8638}
8639
8640/**
8641 * i40e_pci_error_slot_reset - a PCI slot reset just happened
8642 * @pdev: PCI device information struct
8643 *
8644 * Called to find if the driver can work with the device now that
8645 * the pci slot has been reset. If a basic connection seems good
8646 * (registers are readable and have sane content) then return a
8647 * happy little PCI_ERS_RESULT_xxx.
8648 **/
8649static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
8650{
8651 struct i40e_pf *pf = pci_get_drvdata(pdev);
8652 pci_ers_result_t result;
8653 int err;
8654 u32 reg;
8655
8656 dev_info(&pdev->dev, "%s\n", __func__);
8657 if (pci_enable_device_mem(pdev)) {
8658 dev_info(&pdev->dev,
8659 "Cannot re-enable PCI device after reset.\n");
8660 result = PCI_ERS_RESULT_DISCONNECT;
8661 } else {
8662 pci_set_master(pdev);
8663 pci_restore_state(pdev);
8664 pci_save_state(pdev);
8665 pci_wake_from_d3(pdev, false);
8666
8667 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8668 if (reg == 0)
8669 result = PCI_ERS_RESULT_RECOVERED;
8670 else
8671 result = PCI_ERS_RESULT_DISCONNECT;
8672 }
8673
8674 err = pci_cleanup_aer_uncorrect_error_status(pdev);
8675 if (err) {
8676 dev_info(&pdev->dev,
8677 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
8678 err);
8679 /* non-fatal, continue */
8680 }
8681
8682 return result;
8683}
8684
8685/**
8686 * i40e_pci_error_resume - restart operations after PCI error recovery
8687 * @pdev: PCI device information struct
8688 *
8689 * Called to allow the driver to bring things back up after PCI error
8690 * and/or reset recovery has finished.
8691 **/
8692static void i40e_pci_error_resume(struct pci_dev *pdev)
8693{
8694 struct i40e_pf *pf = pci_get_drvdata(pdev);
8695
8696 dev_info(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008697 if (test_bit(__I40E_SUSPENDED, &pf->state))
8698 return;
8699
8700 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008701 i40e_handle_reset_warning(pf);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008702 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008703}
8704
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008705/**
8706 * i40e_shutdown - PCI callback for shutting down
8707 * @pdev: PCI device information struct
8708 **/
8709static void i40e_shutdown(struct pci_dev *pdev)
8710{
8711 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008712 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008713
8714 set_bit(__I40E_SUSPENDED, &pf->state);
8715 set_bit(__I40E_DOWN, &pf->state);
8716 rtnl_lock();
8717 i40e_prep_for_reset(pf);
8718 rtnl_unlock();
8719
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008720 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
8721 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
8722
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008723 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008724 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008725 pci_set_power_state(pdev, PCI_D3hot);
8726 }
8727}
8728
8729#ifdef CONFIG_PM
8730/**
8731 * i40e_suspend - PCI callback for moving to D3
8732 * @pdev: PCI device information struct
8733 **/
8734static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
8735{
8736 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008737 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008738
8739 set_bit(__I40E_SUSPENDED, &pf->state);
8740 set_bit(__I40E_DOWN, &pf->state);
8741 rtnl_lock();
8742 i40e_prep_for_reset(pf);
8743 rtnl_unlock();
8744
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008745 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
8746 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
8747
8748 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008749 pci_set_power_state(pdev, PCI_D3hot);
8750
8751 return 0;
8752}
8753
8754/**
8755 * i40e_resume - PCI callback for waking up from D3
8756 * @pdev: PCI device information struct
8757 **/
8758static int i40e_resume(struct pci_dev *pdev)
8759{
8760 struct i40e_pf *pf = pci_get_drvdata(pdev);
8761 u32 err;
8762
8763 pci_set_power_state(pdev, PCI_D0);
8764 pci_restore_state(pdev);
8765 /* pci_restore_state() clears dev->state_saves, so
8766 * call pci_save_state() again to restore it.
8767 */
8768 pci_save_state(pdev);
8769
8770 err = pci_enable_device_mem(pdev);
8771 if (err) {
8772 dev_err(&pdev->dev,
8773 "%s: Cannot enable PCI device from suspend\n",
8774 __func__);
8775 return err;
8776 }
8777 pci_set_master(pdev);
8778
8779 /* no wakeup events while running */
8780 pci_wake_from_d3(pdev, false);
8781
8782 /* handling the reset will rebuild the device state */
8783 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
8784 clear_bit(__I40E_DOWN, &pf->state);
8785 rtnl_lock();
8786 i40e_reset_and_rebuild(pf, false);
8787 rtnl_unlock();
8788 }
8789
8790 return 0;
8791}
8792
8793#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008794static const struct pci_error_handlers i40e_err_handler = {
8795 .error_detected = i40e_pci_error_detected,
8796 .slot_reset = i40e_pci_error_slot_reset,
8797 .resume = i40e_pci_error_resume,
8798};
8799
8800static struct pci_driver i40e_driver = {
8801 .name = i40e_driver_name,
8802 .id_table = i40e_pci_tbl,
8803 .probe = i40e_probe,
8804 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008805#ifdef CONFIG_PM
8806 .suspend = i40e_suspend,
8807 .resume = i40e_resume,
8808#endif
8809 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008810 .err_handler = &i40e_err_handler,
8811 .sriov_configure = i40e_pci_sriov_configure,
8812};
8813
8814/**
8815 * i40e_init_module - Driver registration routine
8816 *
8817 * i40e_init_module is the first routine called when the driver is
8818 * loaded. All it does is register with the PCI subsystem.
8819 **/
8820static int __init i40e_init_module(void)
8821{
8822 pr_info("%s: %s - version %s\n", i40e_driver_name,
8823 i40e_driver_string, i40e_driver_version_str);
8824 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
8825 i40e_dbg_init();
8826 return pci_register_driver(&i40e_driver);
8827}
8828module_init(i40e_init_module);
8829
8830/**
8831 * i40e_exit_module - Driver exit cleanup routine
8832 *
8833 * i40e_exit_module is called just before the driver is removed
8834 * from memory.
8835 **/
8836static void __exit i40e_exit_module(void)
8837{
8838 pci_unregister_driver(&i40e_driver);
8839 i40e_dbg_exit();
8840}
8841module_exit(i40e_exit_module);