blob: afc37d3b210151b7784b938cc51d81f3789d815b [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
Catherine Sullivane454d6b2014-04-23 04:50:17 +000041#define DRV_VERSION_MINOR 4
Catherine Sullivaneeb6b642014-05-10 04:49:15 +000042#define DRV_VERSION_BUILD 5
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},
Shannon Nelsonab600852014-01-17 15:36:39 -080070 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
71 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_A), 0},
72 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
73 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080074 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
75 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
76 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000077 /* required last entry */
78 {0, }
79};
80MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
81
82#define I40E_MAX_VF_COUNT 128
83static int debug = -1;
84module_param(debug, int, 0);
85MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
86
87MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
88MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
89MODULE_LICENSE("GPL");
90MODULE_VERSION(DRV_VERSION);
91
92/**
93 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
94 * @hw: pointer to the HW structure
95 * @mem: ptr to mem struct to fill out
96 * @size: size of memory requested
97 * @alignment: what to align the allocation to
98 **/
99int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
100 u64 size, u32 alignment)
101{
102 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
103
104 mem->size = ALIGN(size, alignment);
105 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
106 &mem->pa, GFP_KERNEL);
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000107 if (!mem->va)
108 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000109
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000110 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000111}
112
113/**
114 * i40e_free_dma_mem_d - OS specific memory free for shared code
115 * @hw: pointer to the HW structure
116 * @mem: ptr to mem struct to free
117 **/
118int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
119{
120 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
121
122 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
123 mem->va = NULL;
124 mem->pa = 0;
125 mem->size = 0;
126
127 return 0;
128}
129
130/**
131 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
132 * @hw: pointer to the HW structure
133 * @mem: ptr to mem struct to fill out
134 * @size: size of memory requested
135 **/
136int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
137 u32 size)
138{
139 mem->size = size;
140 mem->va = kzalloc(size, GFP_KERNEL);
141
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000142 if (!mem->va)
143 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000144
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000145 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000146}
147
148/**
149 * i40e_free_virt_mem_d - OS specific memory free for shared code
150 * @hw: pointer to the HW structure
151 * @mem: ptr to mem struct to free
152 **/
153int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
154{
155 /* it's ok to kfree a NULL pointer */
156 kfree(mem->va);
157 mem->va = NULL;
158 mem->size = 0;
159
160 return 0;
161}
162
163/**
164 * i40e_get_lump - find a lump of free generic resource
165 * @pf: board private structure
166 * @pile: the pile of resource to search
167 * @needed: the number of items needed
168 * @id: an owner id to stick on the items assigned
169 *
170 * Returns the base item index of the lump, or negative for error
171 *
172 * The search_hint trick and lack of advanced fit-finding only work
173 * because we're highly likely to have all the same size lump requests.
174 * Linear search time and any fragmentation should be minimal.
175 **/
176static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
177 u16 needed, u16 id)
178{
179 int ret = -ENOMEM;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000180 int i, j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000181
182 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
183 dev_info(&pf->pdev->dev,
184 "param err: pile=%p needed=%d id=0x%04x\n",
185 pile, needed, id);
186 return -EINVAL;
187 }
188
189 /* start the linear search with an imperfect hint */
190 i = pile->search_hint;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000191 while (i < pile->num_entries) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000192 /* skip already allocated entries */
193 if (pile->list[i] & I40E_PILE_VALID_BIT) {
194 i++;
195 continue;
196 }
197
198 /* do we have enough in this lump? */
199 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
200 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
201 break;
202 }
203
204 if (j == needed) {
205 /* there was enough, so assign it to the requestor */
206 for (j = 0; j < needed; j++)
207 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
208 ret = i;
209 pile->search_hint = i + j;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000210 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000211 } else {
212 /* not enough, so skip over it and continue looking */
213 i += j;
214 }
215 }
216
217 return ret;
218}
219
220/**
221 * i40e_put_lump - return a lump of generic resource
222 * @pile: the pile of resource to search
223 * @index: the base item index
224 * @id: the owner id of the items assigned
225 *
226 * Returns the count of items in the lump
227 **/
228static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
229{
230 int valid_id = (id | I40E_PILE_VALID_BIT);
231 int count = 0;
232 int i;
233
234 if (!pile || index >= pile->num_entries)
235 return -EINVAL;
236
237 for (i = index;
238 i < pile->num_entries && pile->list[i] == valid_id;
239 i++) {
240 pile->list[i] = 0;
241 count++;
242 }
243
244 if (count && index < pile->search_hint)
245 pile->search_hint = index;
246
247 return count;
248}
249
250/**
251 * i40e_service_event_schedule - Schedule the service task to wake up
252 * @pf: board private structure
253 *
254 * If not already scheduled, this puts the task into the work queue
255 **/
256static void i40e_service_event_schedule(struct i40e_pf *pf)
257{
258 if (!test_bit(__I40E_DOWN, &pf->state) &&
259 !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) &&
260 !test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
261 schedule_work(&pf->service_task);
262}
263
264/**
265 * i40e_tx_timeout - Respond to a Tx Hang
266 * @netdev: network interface device structure
267 *
268 * If any port has noticed a Tx timeout, it is likely that the whole
269 * device is munged, not just the one netdev port, so go for the full
270 * reset.
271 **/
272static void i40e_tx_timeout(struct net_device *netdev)
273{
274 struct i40e_netdev_priv *np = netdev_priv(netdev);
275 struct i40e_vsi *vsi = np->vsi;
276 struct i40e_pf *pf = vsi->back;
277
278 pf->tx_timeout_count++;
279
280 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
281 pf->tx_timeout_recovery_level = 0;
282 pf->tx_timeout_last_recovery = jiffies;
283 netdev_info(netdev, "tx_timeout recovery level %d\n",
284 pf->tx_timeout_recovery_level);
285
286 switch (pf->tx_timeout_recovery_level) {
287 case 0:
288 /* disable and re-enable queues for the VSI */
289 if (in_interrupt()) {
290 set_bit(__I40E_REINIT_REQUESTED, &pf->state);
291 set_bit(__I40E_REINIT_REQUESTED, &vsi->state);
292 } else {
293 i40e_vsi_reinit_locked(vsi);
294 }
295 break;
296 case 1:
297 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
298 break;
299 case 2:
300 set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
301 break;
302 case 3:
303 set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
304 break;
305 default:
306 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
Akeem G Abodunrine108b0e2014-02-13 03:48:43 -0800307 set_bit(__I40E_DOWN, &vsi->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000308 i40e_down(vsi);
309 break;
310 }
311 i40e_service_event_schedule(pf);
312 pf->tx_timeout_recovery_level++;
313}
314
315/**
316 * i40e_release_rx_desc - Store the new tail and head values
317 * @rx_ring: ring to bump
318 * @val: new head index
319 **/
320static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
321{
322 rx_ring->next_to_use = val;
323
324 /* Force memory writes to complete before letting h/w
325 * know there are new descriptors to fetch. (Only
326 * applicable for weak-ordered memory model archs,
327 * such as IA-64).
328 */
329 wmb();
330 writel(val, rx_ring->tail);
331}
332
333/**
334 * i40e_get_vsi_stats_struct - Get System Network Statistics
335 * @vsi: the VSI we care about
336 *
337 * Returns the address of the device statistics structure.
338 * The statistics are actually updated from the service task.
339 **/
340struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
341{
342 return &vsi->net_stats;
343}
344
345/**
346 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
347 * @netdev: network interface device structure
348 *
349 * Returns the address of the device statistics structure.
350 * The statistics are actually updated from the service task.
351 **/
352static struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
353 struct net_device *netdev,
Alexander Duyck980e9b12013-09-28 06:01:03 +0000354 struct rtnl_link_stats64 *stats)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000355{
356 struct i40e_netdev_priv *np = netdev_priv(netdev);
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +0000357 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000358 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000359 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
360 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000361
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000362 if (test_bit(__I40E_DOWN, &vsi->state))
363 return stats;
364
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800365 if (!vsi->tx_rings)
366 return stats;
367
Alexander Duyck980e9b12013-09-28 06:01:03 +0000368 rcu_read_lock();
369 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000370 u64 bytes, packets;
371 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000372
Alexander Duyck980e9b12013-09-28 06:01:03 +0000373 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
374 if (!tx_ring)
375 continue;
376
377 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700378 start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000379 packets = tx_ring->stats.packets;
380 bytes = tx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700381 } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000382
383 stats->tx_packets += packets;
384 stats->tx_bytes += bytes;
385 rx_ring = &tx_ring[1];
386
387 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700388 start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000389 packets = rx_ring->stats.packets;
390 bytes = rx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700391 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000392
393 stats->rx_packets += packets;
394 stats->rx_bytes += bytes;
395 }
396 rcu_read_unlock();
397
Akeem G Abodunrina5282f42014-05-10 04:49:03 +0000398 /* following stats updated by i40e_watchdog_subtask() */
Alexander Duyck980e9b12013-09-28 06:01:03 +0000399 stats->multicast = vsi_stats->multicast;
400 stats->tx_errors = vsi_stats->tx_errors;
401 stats->tx_dropped = vsi_stats->tx_dropped;
402 stats->rx_errors = vsi_stats->rx_errors;
403 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
404 stats->rx_length_errors = vsi_stats->rx_length_errors;
405
406 return stats;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000407}
408
409/**
410 * i40e_vsi_reset_stats - Resets all stats of the given vsi
411 * @vsi: the VSI to have its stats reset
412 **/
413void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
414{
415 struct rtnl_link_stats64 *ns;
416 int i;
417
418 if (!vsi)
419 return;
420
421 ns = i40e_get_vsi_stats_struct(vsi);
422 memset(ns, 0, sizeof(*ns));
423 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
424 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
425 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
Greg Rose8e9dca52013-12-18 13:45:53 +0000426 if (vsi->rx_rings && vsi->rx_rings[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000427 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000428 memset(&vsi->rx_rings[i]->stats, 0 ,
429 sizeof(vsi->rx_rings[i]->stats));
430 memset(&vsi->rx_rings[i]->rx_stats, 0 ,
431 sizeof(vsi->rx_rings[i]->rx_stats));
432 memset(&vsi->tx_rings[i]->stats, 0 ,
433 sizeof(vsi->tx_rings[i]->stats));
434 memset(&vsi->tx_rings[i]->tx_stats, 0,
435 sizeof(vsi->tx_rings[i]->tx_stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000436 }
Greg Rose8e9dca52013-12-18 13:45:53 +0000437 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000438 vsi->stat_offsets_loaded = false;
439}
440
441/**
442 * i40e_pf_reset_stats - Reset all of the stats for the given pf
443 * @pf: the PF to be reset
444 **/
445void i40e_pf_reset_stats(struct i40e_pf *pf)
446{
447 memset(&pf->stats, 0, sizeof(pf->stats));
448 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
449 pf->stat_offsets_loaded = false;
450}
451
452/**
453 * i40e_stat_update48 - read and update a 48 bit stat from the chip
454 * @hw: ptr to the hardware info
455 * @hireg: the high 32 bit reg to read
456 * @loreg: the low 32 bit reg to read
457 * @offset_loaded: has the initial offset been loaded yet
458 * @offset: ptr to current offset value
459 * @stat: ptr to the stat
460 *
461 * Since the device stats are not reset at PFReset, they likely will not
462 * be zeroed when the driver starts. We'll save the first values read
463 * and use them as offsets to be subtracted from the raw values in order
464 * to report stats that count from zero. In the process, we also manage
465 * the potential roll-over.
466 **/
467static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
468 bool offset_loaded, u64 *offset, u64 *stat)
469{
470 u64 new_data;
471
Shannon Nelsonab600852014-01-17 15:36:39 -0800472 if (hw->device_id == I40E_DEV_ID_QEMU) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000473 new_data = rd32(hw, loreg);
474 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
475 } else {
476 new_data = rd64(hw, loreg);
477 }
478 if (!offset_loaded)
479 *offset = new_data;
480 if (likely(new_data >= *offset))
481 *stat = new_data - *offset;
482 else
483 *stat = (new_data + ((u64)1 << 48)) - *offset;
484 *stat &= 0xFFFFFFFFFFFFULL;
485}
486
487/**
488 * i40e_stat_update32 - read and update a 32 bit stat from the chip
489 * @hw: ptr to the hardware info
490 * @reg: the hw reg to read
491 * @offset_loaded: has the initial offset been loaded yet
492 * @offset: ptr to current offset value
493 * @stat: ptr to the stat
494 **/
495static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
496 bool offset_loaded, u64 *offset, u64 *stat)
497{
498 u32 new_data;
499
500 new_data = rd32(hw, reg);
501 if (!offset_loaded)
502 *offset = new_data;
503 if (likely(new_data >= *offset))
504 *stat = (u32)(new_data - *offset);
505 else
506 *stat = (u32)((new_data + ((u64)1 << 32)) - *offset);
507}
508
509/**
510 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
511 * @vsi: the VSI to be updated
512 **/
513void i40e_update_eth_stats(struct i40e_vsi *vsi)
514{
515 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
516 struct i40e_pf *pf = vsi->back;
517 struct i40e_hw *hw = &pf->hw;
518 struct i40e_eth_stats *oes;
519 struct i40e_eth_stats *es; /* device's eth stats */
520
521 es = &vsi->eth_stats;
522 oes = &vsi->eth_stats_offsets;
523
524 /* Gather up the stats that the hw collects */
525 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
526 vsi->stat_offsets_loaded,
527 &oes->tx_errors, &es->tx_errors);
528 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
529 vsi->stat_offsets_loaded,
530 &oes->rx_discards, &es->rx_discards);
Shannon Nelson41a9e552014-04-23 04:50:20 +0000531 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
532 vsi->stat_offsets_loaded,
533 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
534 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
535 vsi->stat_offsets_loaded,
536 &oes->tx_errors, &es->tx_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000537
538 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
539 I40E_GLV_GORCL(stat_idx),
540 vsi->stat_offsets_loaded,
541 &oes->rx_bytes, &es->rx_bytes);
542 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
543 I40E_GLV_UPRCL(stat_idx),
544 vsi->stat_offsets_loaded,
545 &oes->rx_unicast, &es->rx_unicast);
546 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
547 I40E_GLV_MPRCL(stat_idx),
548 vsi->stat_offsets_loaded,
549 &oes->rx_multicast, &es->rx_multicast);
550 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
551 I40E_GLV_BPRCL(stat_idx),
552 vsi->stat_offsets_loaded,
553 &oes->rx_broadcast, &es->rx_broadcast);
554
555 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
556 I40E_GLV_GOTCL(stat_idx),
557 vsi->stat_offsets_loaded,
558 &oes->tx_bytes, &es->tx_bytes);
559 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
560 I40E_GLV_UPTCL(stat_idx),
561 vsi->stat_offsets_loaded,
562 &oes->tx_unicast, &es->tx_unicast);
563 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
564 I40E_GLV_MPTCL(stat_idx),
565 vsi->stat_offsets_loaded,
566 &oes->tx_multicast, &es->tx_multicast);
567 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
568 I40E_GLV_BPTCL(stat_idx),
569 vsi->stat_offsets_loaded,
570 &oes->tx_broadcast, &es->tx_broadcast);
571 vsi->stat_offsets_loaded = true;
572}
573
574/**
575 * i40e_update_veb_stats - Update Switch component statistics
576 * @veb: the VEB being updated
577 **/
578static void i40e_update_veb_stats(struct i40e_veb *veb)
579{
580 struct i40e_pf *pf = veb->pf;
581 struct i40e_hw *hw = &pf->hw;
582 struct i40e_eth_stats *oes;
583 struct i40e_eth_stats *es; /* device's eth stats */
584 int idx = 0;
585
586 idx = veb->stats_idx;
587 es = &veb->stats;
588 oes = &veb->stats_offsets;
589
590 /* Gather up the stats that the hw collects */
591 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
592 veb->stat_offsets_loaded,
593 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000594 if (hw->revision_id > 0)
595 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
596 veb->stat_offsets_loaded,
597 &oes->rx_unknown_protocol,
598 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000599 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
600 veb->stat_offsets_loaded,
601 &oes->rx_bytes, &es->rx_bytes);
602 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
603 veb->stat_offsets_loaded,
604 &oes->rx_unicast, &es->rx_unicast);
605 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
606 veb->stat_offsets_loaded,
607 &oes->rx_multicast, &es->rx_multicast);
608 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
609 veb->stat_offsets_loaded,
610 &oes->rx_broadcast, &es->rx_broadcast);
611
612 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
613 veb->stat_offsets_loaded,
614 &oes->tx_bytes, &es->tx_bytes);
615 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
616 veb->stat_offsets_loaded,
617 &oes->tx_unicast, &es->tx_unicast);
618 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
619 veb->stat_offsets_loaded,
620 &oes->tx_multicast, &es->tx_multicast);
621 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
622 veb->stat_offsets_loaded,
623 &oes->tx_broadcast, &es->tx_broadcast);
624 veb->stat_offsets_loaded = true;
625}
626
627/**
628 * i40e_update_link_xoff_rx - Update XOFF received in link flow control mode
629 * @pf: the corresponding PF
630 *
631 * Update the Rx XOFF counter (PAUSE frames) in link flow control mode
632 **/
633static void i40e_update_link_xoff_rx(struct i40e_pf *pf)
634{
635 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
636 struct i40e_hw_port_stats *nsd = &pf->stats;
637 struct i40e_hw *hw = &pf->hw;
638 u64 xoff = 0;
639 u16 i, v;
640
641 if ((hw->fc.current_mode != I40E_FC_FULL) &&
642 (hw->fc.current_mode != I40E_FC_RX_PAUSE))
643 return;
644
645 xoff = nsd->link_xoff_rx;
646 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
647 pf->stat_offsets_loaded,
648 &osd->link_xoff_rx, &nsd->link_xoff_rx);
649
650 /* No new LFC xoff rx */
651 if (!(nsd->link_xoff_rx - xoff))
652 return;
653
654 /* Clear the __I40E_HANG_CHECK_ARMED bit for all Tx rings */
Mitch Williams505682c2014-05-20 08:01:37 +0000655 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000656 struct i40e_vsi *vsi = pf->vsi[v];
657
Mitch Williamsddfda802014-05-10 04:49:10 +0000658 if (!vsi || !vsi->tx_rings[0])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000659 continue;
660
661 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000662 struct i40e_ring *ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000663 clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
664 }
665 }
666}
667
668/**
669 * i40e_update_prio_xoff_rx - Update XOFF received in PFC mode
670 * @pf: the corresponding PF
671 *
672 * Update the Rx XOFF counter (PAUSE frames) in PFC mode
673 **/
674static void i40e_update_prio_xoff_rx(struct i40e_pf *pf)
675{
676 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
677 struct i40e_hw_port_stats *nsd = &pf->stats;
678 bool xoff[I40E_MAX_TRAFFIC_CLASS] = {false};
679 struct i40e_dcbx_config *dcb_cfg;
680 struct i40e_hw *hw = &pf->hw;
681 u16 i, v;
682 u8 tc;
683
684 dcb_cfg = &hw->local_dcbx_config;
685
686 /* See if DCB enabled with PFC TC */
687 if (!(pf->flags & I40E_FLAG_DCB_ENABLED) ||
688 !(dcb_cfg->pfc.pfcenable)) {
689 i40e_update_link_xoff_rx(pf);
690 return;
691 }
692
693 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
694 u64 prio_xoff = nsd->priority_xoff_rx[i];
695 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
696 pf->stat_offsets_loaded,
697 &osd->priority_xoff_rx[i],
698 &nsd->priority_xoff_rx[i]);
699
700 /* No new PFC xoff rx */
701 if (!(nsd->priority_xoff_rx[i] - prio_xoff))
702 continue;
703 /* Get the TC for given priority */
704 tc = dcb_cfg->etscfg.prioritytable[i];
705 xoff[tc] = true;
706 }
707
708 /* Clear the __I40E_HANG_CHECK_ARMED bit for Tx rings */
Mitch Williams505682c2014-05-20 08:01:37 +0000709 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000710 struct i40e_vsi *vsi = pf->vsi[v];
711
Mitch Williamsddfda802014-05-10 04:49:10 +0000712 if (!vsi || !vsi->tx_rings[0])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000713 continue;
714
715 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000716 struct i40e_ring *ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000717
718 tc = ring->dcb_tc;
719 if (xoff[tc])
720 clear_bit(__I40E_HANG_CHECK_ARMED,
721 &ring->state);
722 }
723 }
724}
725
726/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000727 * i40e_update_vsi_stats - Update the vsi statistics counters.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000728 * @vsi: the VSI to be updated
729 *
730 * There are a few instances where we store the same stat in a
731 * couple of different structs. This is partly because we have
732 * the netdev stats that need to be filled out, which is slightly
733 * different from the "eth_stats" defined by the chip and used in
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000734 * VF communications. We sort it out here.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000735 **/
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000736static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000737{
738 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000739 struct rtnl_link_stats64 *ons;
740 struct rtnl_link_stats64 *ns; /* netdev stats */
741 struct i40e_eth_stats *oes;
742 struct i40e_eth_stats *es; /* device's eth stats */
743 u32 tx_restart, tx_busy;
744 u32 rx_page, rx_buf;
745 u64 rx_p, rx_b;
746 u64 tx_p, tx_b;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000747 u16 q;
748
749 if (test_bit(__I40E_DOWN, &vsi->state) ||
750 test_bit(__I40E_CONFIG_BUSY, &pf->state))
751 return;
752
753 ns = i40e_get_vsi_stats_struct(vsi);
754 ons = &vsi->net_stats_offsets;
755 es = &vsi->eth_stats;
756 oes = &vsi->eth_stats_offsets;
757
758 /* Gather up the netdev and vsi stats that the driver collects
759 * on the fly during packet processing
760 */
761 rx_b = rx_p = 0;
762 tx_b = tx_p = 0;
763 tx_restart = tx_busy = 0;
764 rx_page = 0;
765 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000766 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000767 for (q = 0; q < vsi->num_queue_pairs; q++) {
768 struct i40e_ring *p;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000769 u64 bytes, packets;
770 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000771
Alexander Duyck980e9b12013-09-28 06:01:03 +0000772 /* locate Tx ring */
773 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000774
Alexander Duyck980e9b12013-09-28 06:01:03 +0000775 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700776 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000777 packets = p->stats.packets;
778 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700779 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000780 tx_b += bytes;
781 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000782 tx_restart += p->tx_stats.restart_queue;
783 tx_busy += p->tx_stats.tx_busy;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000784
785 /* Rx queue is part of the same block as Tx queue */
786 p = &p[1];
787 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700788 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000789 packets = p->stats.packets;
790 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700791 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000792 rx_b += bytes;
793 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000794 rx_buf += p->rx_stats.alloc_buff_failed;
795 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000796 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000797 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000798 vsi->tx_restart = tx_restart;
799 vsi->tx_busy = tx_busy;
800 vsi->rx_page_failed = rx_page;
801 vsi->rx_buf_failed = rx_buf;
802
803 ns->rx_packets = rx_p;
804 ns->rx_bytes = rx_b;
805 ns->tx_packets = tx_p;
806 ns->tx_bytes = tx_b;
807
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000808 /* update netdev stats from eth stats */
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000809 i40e_update_eth_stats(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000810 ons->tx_errors = oes->tx_errors;
811 ns->tx_errors = es->tx_errors;
812 ons->multicast = oes->rx_multicast;
813 ns->multicast = es->rx_multicast;
Shannon Nelson41a9e552014-04-23 04:50:20 +0000814 ons->rx_dropped = oes->rx_discards;
815 ns->rx_dropped = es->rx_discards;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000816 ons->tx_dropped = oes->tx_discards;
817 ns->tx_dropped = es->tx_discards;
818
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000819 /* pull in a couple PF stats if this is the main vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000820 if (vsi == pf->vsi[pf->lan_vsi]) {
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000821 ns->rx_crc_errors = pf->stats.crc_errors;
822 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
823 ns->rx_length_errors = pf->stats.rx_length_errors;
824 }
825}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000826
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000827/**
828 * i40e_update_pf_stats - Update the pf statistics counters.
829 * @pf: the PF to be updated
830 **/
831static void i40e_update_pf_stats(struct i40e_pf *pf)
832{
833 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
834 struct i40e_hw_port_stats *nsd = &pf->stats;
835 struct i40e_hw *hw = &pf->hw;
836 u32 val;
837 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000838
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000839 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
840 I40E_GLPRT_GORCL(hw->port),
841 pf->stat_offsets_loaded,
842 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
843 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
844 I40E_GLPRT_GOTCL(hw->port),
845 pf->stat_offsets_loaded,
846 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
847 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
848 pf->stat_offsets_loaded,
849 &osd->eth.rx_discards,
850 &nsd->eth.rx_discards);
851 i40e_stat_update32(hw, I40E_GLPRT_TDPC(hw->port),
852 pf->stat_offsets_loaded,
853 &osd->eth.tx_discards,
854 &nsd->eth.tx_discards);
Shannon Nelson532d2832014-04-23 04:50:09 +0000855
856 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
857 I40E_GLPRT_UPRCL(hw->port),
858 pf->stat_offsets_loaded,
859 &osd->eth.rx_unicast,
860 &nsd->eth.rx_unicast);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000861 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
862 I40E_GLPRT_MPRCL(hw->port),
863 pf->stat_offsets_loaded,
864 &osd->eth.rx_multicast,
865 &nsd->eth.rx_multicast);
Shannon Nelson532d2832014-04-23 04:50:09 +0000866 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
867 I40E_GLPRT_BPRCL(hw->port),
868 pf->stat_offsets_loaded,
869 &osd->eth.rx_broadcast,
870 &nsd->eth.rx_broadcast);
871 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
872 I40E_GLPRT_UPTCL(hw->port),
873 pf->stat_offsets_loaded,
874 &osd->eth.tx_unicast,
875 &nsd->eth.tx_unicast);
876 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
877 I40E_GLPRT_MPTCL(hw->port),
878 pf->stat_offsets_loaded,
879 &osd->eth.tx_multicast,
880 &nsd->eth.tx_multicast);
881 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
882 I40E_GLPRT_BPTCL(hw->port),
883 pf->stat_offsets_loaded,
884 &osd->eth.tx_broadcast,
885 &nsd->eth.tx_broadcast);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000886
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000887 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
888 pf->stat_offsets_loaded,
889 &osd->tx_dropped_link_down,
890 &nsd->tx_dropped_link_down);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000891
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000892 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
893 pf->stat_offsets_loaded,
894 &osd->crc_errors, &nsd->crc_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000895
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000896 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
897 pf->stat_offsets_loaded,
898 &osd->illegal_bytes, &nsd->illegal_bytes);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000899
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000900 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
901 pf->stat_offsets_loaded,
902 &osd->mac_local_faults,
903 &nsd->mac_local_faults);
904 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
905 pf->stat_offsets_loaded,
906 &osd->mac_remote_faults,
907 &nsd->mac_remote_faults);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000908
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000909 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
910 pf->stat_offsets_loaded,
911 &osd->rx_length_errors,
912 &nsd->rx_length_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000913
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000914 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
915 pf->stat_offsets_loaded,
916 &osd->link_xon_rx, &nsd->link_xon_rx);
917 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
918 pf->stat_offsets_loaded,
919 &osd->link_xon_tx, &nsd->link_xon_tx);
920 i40e_update_prio_xoff_rx(pf); /* handles I40E_GLPRT_LXOFFRXC */
921 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
922 pf->stat_offsets_loaded,
923 &osd->link_xoff_tx, &nsd->link_xoff_tx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000924
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000925 for (i = 0; i < 8; i++) {
926 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000927 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000928 &osd->priority_xon_rx[i],
929 &nsd->priority_xon_rx[i]);
930 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000931 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000932 &osd->priority_xon_tx[i],
933 &nsd->priority_xon_tx[i]);
934 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000935 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000936 &osd->priority_xoff_tx[i],
937 &nsd->priority_xoff_tx[i]);
938 i40e_stat_update32(hw,
939 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000940 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000941 &osd->priority_xon_2_xoff[i],
942 &nsd->priority_xon_2_xoff[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000943 }
944
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000945 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
946 I40E_GLPRT_PRC64L(hw->port),
947 pf->stat_offsets_loaded,
948 &osd->rx_size_64, &nsd->rx_size_64);
949 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
950 I40E_GLPRT_PRC127L(hw->port),
951 pf->stat_offsets_loaded,
952 &osd->rx_size_127, &nsd->rx_size_127);
953 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
954 I40E_GLPRT_PRC255L(hw->port),
955 pf->stat_offsets_loaded,
956 &osd->rx_size_255, &nsd->rx_size_255);
957 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
958 I40E_GLPRT_PRC511L(hw->port),
959 pf->stat_offsets_loaded,
960 &osd->rx_size_511, &nsd->rx_size_511);
961 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
962 I40E_GLPRT_PRC1023L(hw->port),
963 pf->stat_offsets_loaded,
964 &osd->rx_size_1023, &nsd->rx_size_1023);
965 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
966 I40E_GLPRT_PRC1522L(hw->port),
967 pf->stat_offsets_loaded,
968 &osd->rx_size_1522, &nsd->rx_size_1522);
969 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
970 I40E_GLPRT_PRC9522L(hw->port),
971 pf->stat_offsets_loaded,
972 &osd->rx_size_big, &nsd->rx_size_big);
973
974 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
975 I40E_GLPRT_PTC64L(hw->port),
976 pf->stat_offsets_loaded,
977 &osd->tx_size_64, &nsd->tx_size_64);
978 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
979 I40E_GLPRT_PTC127L(hw->port),
980 pf->stat_offsets_loaded,
981 &osd->tx_size_127, &nsd->tx_size_127);
982 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
983 I40E_GLPRT_PTC255L(hw->port),
984 pf->stat_offsets_loaded,
985 &osd->tx_size_255, &nsd->tx_size_255);
986 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
987 I40E_GLPRT_PTC511L(hw->port),
988 pf->stat_offsets_loaded,
989 &osd->tx_size_511, &nsd->tx_size_511);
990 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
991 I40E_GLPRT_PTC1023L(hw->port),
992 pf->stat_offsets_loaded,
993 &osd->tx_size_1023, &nsd->tx_size_1023);
994 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
995 I40E_GLPRT_PTC1522L(hw->port),
996 pf->stat_offsets_loaded,
997 &osd->tx_size_1522, &nsd->tx_size_1522);
998 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
999 I40E_GLPRT_PTC9522L(hw->port),
1000 pf->stat_offsets_loaded,
1001 &osd->tx_size_big, &nsd->tx_size_big);
1002
1003 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1004 pf->stat_offsets_loaded,
1005 &osd->rx_undersize, &nsd->rx_undersize);
1006 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1007 pf->stat_offsets_loaded,
1008 &osd->rx_fragments, &nsd->rx_fragments);
1009 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1010 pf->stat_offsets_loaded,
1011 &osd->rx_oversize, &nsd->rx_oversize);
1012 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1013 pf->stat_offsets_loaded,
1014 &osd->rx_jabber, &nsd->rx_jabber);
1015
1016 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1017 nsd->tx_lpi_status =
1018 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1019 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1020 nsd->rx_lpi_status =
1021 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1022 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1023 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1024 pf->stat_offsets_loaded,
1025 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1026 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1027 pf->stat_offsets_loaded,
1028 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1029
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001030 pf->stat_offsets_loaded = true;
1031}
1032
1033/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001034 * i40e_update_stats - Update the various statistics counters.
1035 * @vsi: the VSI to be updated
1036 *
1037 * Update the various stats for this VSI and its related entities.
1038 **/
1039void i40e_update_stats(struct i40e_vsi *vsi)
1040{
1041 struct i40e_pf *pf = vsi->back;
1042
1043 if (vsi == pf->vsi[pf->lan_vsi])
1044 i40e_update_pf_stats(pf);
1045
1046 i40e_update_vsi_stats(vsi);
1047}
1048
1049/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001050 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1051 * @vsi: the VSI to be searched
1052 * @macaddr: the MAC address
1053 * @vlan: the vlan
1054 * @is_vf: make sure its a vf filter, else doesn't matter
1055 * @is_netdev: make sure its a netdev filter, else doesn't matter
1056 *
1057 * Returns ptr to the filter object or NULL
1058 **/
1059static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1060 u8 *macaddr, s16 vlan,
1061 bool is_vf, bool is_netdev)
1062{
1063 struct i40e_mac_filter *f;
1064
1065 if (!vsi || !macaddr)
1066 return NULL;
1067
1068 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1069 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1070 (vlan == f->vlan) &&
1071 (!is_vf || f->is_vf) &&
1072 (!is_netdev || f->is_netdev))
1073 return f;
1074 }
1075 return NULL;
1076}
1077
1078/**
1079 * i40e_find_mac - Find a mac addr in the macvlan filters list
1080 * @vsi: the VSI to be searched
1081 * @macaddr: the MAC address we are searching for
1082 * @is_vf: make sure its a vf filter, else doesn't matter
1083 * @is_netdev: make sure its a netdev filter, else doesn't matter
1084 *
1085 * Returns the first filter with the provided MAC address or NULL if
1086 * MAC address was not found
1087 **/
1088struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, u8 *macaddr,
1089 bool is_vf, bool is_netdev)
1090{
1091 struct i40e_mac_filter *f;
1092
1093 if (!vsi || !macaddr)
1094 return NULL;
1095
1096 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1097 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1098 (!is_vf || f->is_vf) &&
1099 (!is_netdev || f->is_netdev))
1100 return f;
1101 }
1102 return NULL;
1103}
1104
1105/**
1106 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1107 * @vsi: the VSI to be searched
1108 *
1109 * Returns true if VSI is in vlan mode or false otherwise
1110 **/
1111bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1112{
1113 struct i40e_mac_filter *f;
1114
1115 /* Only -1 for all the filters denotes not in vlan mode
1116 * so we have to go through all the list in order to make sure
1117 */
1118 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1119 if (f->vlan >= 0)
1120 return true;
1121 }
1122
1123 return false;
1124}
1125
1126/**
1127 * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
1128 * @vsi: the VSI to be searched
1129 * @macaddr: the mac address to be filtered
1130 * @is_vf: true if it is a vf
1131 * @is_netdev: true if it is a netdev
1132 *
1133 * Goes through all the macvlan filters and adds a
1134 * macvlan filter for each unique vlan that already exists
1135 *
1136 * Returns first filter found on success, else NULL
1137 **/
1138struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi, u8 *macaddr,
1139 bool is_vf, bool is_netdev)
1140{
1141 struct i40e_mac_filter *f;
1142
1143 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1144 if (!i40e_find_filter(vsi, macaddr, f->vlan,
1145 is_vf, is_netdev)) {
1146 if (!i40e_add_filter(vsi, macaddr, f->vlan,
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -08001147 is_vf, is_netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001148 return NULL;
1149 }
1150 }
1151
1152 return list_first_entry_or_null(&vsi->mac_filter_list,
1153 struct i40e_mac_filter, list);
1154}
1155
1156/**
1157 * i40e_add_filter - Add a mac/vlan filter to the VSI
1158 * @vsi: the VSI to be searched
1159 * @macaddr: the MAC address
1160 * @vlan: the vlan
1161 * @is_vf: make sure its a vf filter, else doesn't matter
1162 * @is_netdev: make sure its a netdev filter, else doesn't matter
1163 *
1164 * Returns ptr to the filter object or NULL when no memory available.
1165 **/
1166struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1167 u8 *macaddr, s16 vlan,
1168 bool is_vf, bool is_netdev)
1169{
1170 struct i40e_mac_filter *f;
1171
1172 if (!vsi || !macaddr)
1173 return NULL;
1174
1175 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1176 if (!f) {
1177 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1178 if (!f)
1179 goto add_filter_out;
1180
1181 memcpy(f->macaddr, macaddr, ETH_ALEN);
1182 f->vlan = vlan;
1183 f->changed = true;
1184
1185 INIT_LIST_HEAD(&f->list);
1186 list_add(&f->list, &vsi->mac_filter_list);
1187 }
1188
1189 /* increment counter and add a new flag if needed */
1190 if (is_vf) {
1191 if (!f->is_vf) {
1192 f->is_vf = true;
1193 f->counter++;
1194 }
1195 } else if (is_netdev) {
1196 if (!f->is_netdev) {
1197 f->is_netdev = true;
1198 f->counter++;
1199 }
1200 } else {
1201 f->counter++;
1202 }
1203
1204 /* changed tells sync_filters_subtask to
1205 * push the filter down to the firmware
1206 */
1207 if (f->changed) {
1208 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1209 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1210 }
1211
1212add_filter_out:
1213 return f;
1214}
1215
1216/**
1217 * i40e_del_filter - Remove a mac/vlan filter from the VSI
1218 * @vsi: the VSI to be searched
1219 * @macaddr: the MAC address
1220 * @vlan: the vlan
1221 * @is_vf: make sure it's a vf filter, else doesn't matter
1222 * @is_netdev: make sure it's a netdev filter, else doesn't matter
1223 **/
1224void i40e_del_filter(struct i40e_vsi *vsi,
1225 u8 *macaddr, s16 vlan,
1226 bool is_vf, bool is_netdev)
1227{
1228 struct i40e_mac_filter *f;
1229
1230 if (!vsi || !macaddr)
1231 return;
1232
1233 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1234 if (!f || f->counter == 0)
1235 return;
1236
1237 if (is_vf) {
1238 if (f->is_vf) {
1239 f->is_vf = false;
1240 f->counter--;
1241 }
1242 } else if (is_netdev) {
1243 if (f->is_netdev) {
1244 f->is_netdev = false;
1245 f->counter--;
1246 }
1247 } else {
1248 /* make sure we don't remove a filter in use by vf or netdev */
1249 int min_f = 0;
1250 min_f += (f->is_vf ? 1 : 0);
1251 min_f += (f->is_netdev ? 1 : 0);
1252
1253 if (f->counter > min_f)
1254 f->counter--;
1255 }
1256
1257 /* counter == 0 tells sync_filters_subtask to
1258 * remove the filter from the firmware's list
1259 */
1260 if (f->counter == 0) {
1261 f->changed = true;
1262 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1263 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1264 }
1265}
1266
1267/**
1268 * i40e_set_mac - NDO callback to set mac address
1269 * @netdev: network interface device structure
1270 * @p: pointer to an address structure
1271 *
1272 * Returns 0 on success, negative on failure
1273 **/
1274static int i40e_set_mac(struct net_device *netdev, void *p)
1275{
1276 struct i40e_netdev_priv *np = netdev_priv(netdev);
1277 struct i40e_vsi *vsi = np->vsi;
1278 struct sockaddr *addr = p;
1279 struct i40e_mac_filter *f;
1280
1281 if (!is_valid_ether_addr(addr->sa_data))
1282 return -EADDRNOTAVAIL;
1283
1284 netdev_info(netdev, "set mac address=%pM\n", addr->sa_data);
1285
1286 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
1287 return 0;
1288
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001289 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1290 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1291 return -EADDRNOTAVAIL;
1292
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001293 if (vsi->type == I40E_VSI_MAIN) {
1294 i40e_status ret;
1295 ret = i40e_aq_mac_address_write(&vsi->back->hw,
1296 I40E_AQC_WRITE_TYPE_LAA_ONLY,
1297 addr->sa_data, NULL);
1298 if (ret) {
1299 netdev_info(netdev,
1300 "Addr change for Main VSI failed: %d\n",
1301 ret);
1302 return -EADDRNOTAVAIL;
1303 }
1304
1305 memcpy(vsi->back->hw.mac.addr, addr->sa_data, netdev->addr_len);
1306 }
1307
1308 /* In order to be sure to not drop any packets, add the new address
1309 * then delete the old one.
1310 */
1311 f = i40e_add_filter(vsi, addr->sa_data, I40E_VLAN_ANY, false, false);
1312 if (!f)
1313 return -ENOMEM;
1314
1315 i40e_sync_vsi_filters(vsi);
1316 i40e_del_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY, false, false);
1317 i40e_sync_vsi_filters(vsi);
1318
1319 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1320
1321 return 0;
1322}
1323
1324/**
1325 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1326 * @vsi: the VSI being setup
1327 * @ctxt: VSI context structure
1328 * @enabled_tc: Enabled TCs bitmap
1329 * @is_add: True if called before Add VSI
1330 *
1331 * Setup VSI queue mapping for enabled traffic classes.
1332 **/
1333static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1334 struct i40e_vsi_context *ctxt,
1335 u8 enabled_tc,
1336 bool is_add)
1337{
1338 struct i40e_pf *pf = vsi->back;
1339 u16 sections = 0;
1340 u8 netdev_tc = 0;
1341 u16 numtc = 0;
1342 u16 qcount;
1343 u8 offset;
1344 u16 qmap;
1345 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001346 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001347
1348 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1349 offset = 0;
1350
1351 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1352 /* Find numtc from enabled TC bitmap */
1353 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1354 if (enabled_tc & (1 << i)) /* TC is enabled */
1355 numtc++;
1356 }
1357 if (!numtc) {
1358 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1359 numtc = 1;
1360 }
1361 } else {
1362 /* At least TC0 is enabled in case of non-DCB case */
1363 numtc = 1;
1364 }
1365
1366 vsi->tc_config.numtc = numtc;
1367 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001368 /* Number of queues per enabled TC */
1369 num_tc_qps = rounddown_pow_of_two(vsi->alloc_queue_pairs/numtc);
1370 num_tc_qps = min_t(int, num_tc_qps, I40E_MAX_QUEUES_PER_TC);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001371
1372 /* Setup queue offset/count for all TCs for given VSI */
1373 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1374 /* See if the given TC is enabled for the given VSI */
1375 if (vsi->tc_config.enabled_tc & (1 << i)) { /* TC is enabled */
1376 int pow, num_qps;
1377
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001378 switch (vsi->type) {
1379 case I40E_VSI_MAIN:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001380 qcount = min_t(int, pf->rss_size, num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001381 break;
1382 case I40E_VSI_FDIR:
1383 case I40E_VSI_SRIOV:
1384 case I40E_VSI_VMDQ2:
1385 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001386 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001387 WARN_ON(i != 0);
1388 break;
1389 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001390 vsi->tc_config.tc_info[i].qoffset = offset;
1391 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001392
1393 /* find the power-of-2 of the number of queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001394 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001395 pow = 0;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001396 while (num_qps && ((1 << pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001397 pow++;
1398 num_qps >>= 1;
1399 }
1400
1401 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1402 qmap =
1403 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1404 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1405
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001406 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001407 } else {
1408 /* TC is not enabled so set the offset to
1409 * default queue and allocate one queue
1410 * for the given TC.
1411 */
1412 vsi->tc_config.tc_info[i].qoffset = 0;
1413 vsi->tc_config.tc_info[i].qcount = 1;
1414 vsi->tc_config.tc_info[i].netdev_tc = 0;
1415
1416 qmap = 0;
1417 }
1418 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1419 }
1420
1421 /* Set actual Tx/Rx queue pairs */
1422 vsi->num_queue_pairs = offset;
1423
1424 /* Scheduler section valid can only be set for ADD VSI */
1425 if (is_add) {
1426 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1427
1428 ctxt->info.up_enable_bits = enabled_tc;
1429 }
1430 if (vsi->type == I40E_VSI_SRIOV) {
1431 ctxt->info.mapping_flags |=
1432 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1433 for (i = 0; i < vsi->num_queue_pairs; i++)
1434 ctxt->info.queue_mapping[i] =
1435 cpu_to_le16(vsi->base_queue + i);
1436 } else {
1437 ctxt->info.mapping_flags |=
1438 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1439 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1440 }
1441 ctxt->info.valid_sections |= cpu_to_le16(sections);
1442}
1443
1444/**
1445 * i40e_set_rx_mode - NDO callback to set the netdev filters
1446 * @netdev: network interface device structure
1447 **/
1448static void i40e_set_rx_mode(struct net_device *netdev)
1449{
1450 struct i40e_netdev_priv *np = netdev_priv(netdev);
1451 struct i40e_mac_filter *f, *ftmp;
1452 struct i40e_vsi *vsi = np->vsi;
1453 struct netdev_hw_addr *uca;
1454 struct netdev_hw_addr *mca;
1455 struct netdev_hw_addr *ha;
1456
1457 /* add addr if not already in the filter list */
1458 netdev_for_each_uc_addr(uca, netdev) {
1459 if (!i40e_find_mac(vsi, uca->addr, false, true)) {
1460 if (i40e_is_vsi_in_vlan(vsi))
1461 i40e_put_mac_in_vlan(vsi, uca->addr,
1462 false, true);
1463 else
1464 i40e_add_filter(vsi, uca->addr, I40E_VLAN_ANY,
1465 false, true);
1466 }
1467 }
1468
1469 netdev_for_each_mc_addr(mca, netdev) {
1470 if (!i40e_find_mac(vsi, mca->addr, false, true)) {
1471 if (i40e_is_vsi_in_vlan(vsi))
1472 i40e_put_mac_in_vlan(vsi, mca->addr,
1473 false, true);
1474 else
1475 i40e_add_filter(vsi, mca->addr, I40E_VLAN_ANY,
1476 false, true);
1477 }
1478 }
1479
1480 /* remove filter if not in netdev list */
1481 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1482 bool found = false;
1483
1484 if (!f->is_netdev)
1485 continue;
1486
1487 if (is_multicast_ether_addr(f->macaddr)) {
1488 netdev_for_each_mc_addr(mca, netdev) {
1489 if (ether_addr_equal(mca->addr, f->macaddr)) {
1490 found = true;
1491 break;
1492 }
1493 }
1494 } else {
1495 netdev_for_each_uc_addr(uca, netdev) {
1496 if (ether_addr_equal(uca->addr, f->macaddr)) {
1497 found = true;
1498 break;
1499 }
1500 }
1501
1502 for_each_dev_addr(netdev, ha) {
1503 if (ether_addr_equal(ha->addr, f->macaddr)) {
1504 found = true;
1505 break;
1506 }
1507 }
1508 }
1509 if (!found)
1510 i40e_del_filter(
1511 vsi, f->macaddr, I40E_VLAN_ANY, false, true);
1512 }
1513
1514 /* check for other flag changes */
1515 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1516 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1517 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1518 }
1519}
1520
1521/**
1522 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1523 * @vsi: ptr to the VSI
1524 *
1525 * Push any outstanding VSI filter changes through the AdminQ.
1526 *
1527 * Returns 0 or error value
1528 **/
1529int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
1530{
1531 struct i40e_mac_filter *f, *ftmp;
1532 bool promisc_forced_on = false;
1533 bool add_happened = false;
1534 int filter_list_len = 0;
1535 u32 changed_flags = 0;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001536 i40e_status aq_ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001537 struct i40e_pf *pf;
1538 int num_add = 0;
1539 int num_del = 0;
1540 u16 cmd_flags;
1541
1542 /* empty array typed pointers, kcalloc later */
1543 struct i40e_aqc_add_macvlan_element_data *add_list;
1544 struct i40e_aqc_remove_macvlan_element_data *del_list;
1545
1546 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1547 usleep_range(1000, 2000);
1548 pf = vsi->back;
1549
1550 if (vsi->netdev) {
1551 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1552 vsi->current_netdev_flags = vsi->netdev->flags;
1553 }
1554
1555 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1556 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1557
1558 filter_list_len = pf->hw.aq.asq_buf_size /
1559 sizeof(struct i40e_aqc_remove_macvlan_element_data);
1560 del_list = kcalloc(filter_list_len,
1561 sizeof(struct i40e_aqc_remove_macvlan_element_data),
1562 GFP_KERNEL);
1563 if (!del_list)
1564 return -ENOMEM;
1565
1566 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1567 if (!f->changed)
1568 continue;
1569
1570 if (f->counter != 0)
1571 continue;
1572 f->changed = false;
1573 cmd_flags = 0;
1574
1575 /* add to delete list */
1576 memcpy(del_list[num_del].mac_addr,
1577 f->macaddr, ETH_ALEN);
1578 del_list[num_del].vlan_tag =
1579 cpu_to_le16((u16)(f->vlan ==
1580 I40E_VLAN_ANY ? 0 : f->vlan));
1581
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001582 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1583 del_list[num_del].flags = cmd_flags;
1584 num_del++;
1585
1586 /* unlink from filter list */
1587 list_del(&f->list);
1588 kfree(f);
1589
1590 /* flush a full buffer */
1591 if (num_del == filter_list_len) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001592 aq_ret = i40e_aq_remove_macvlan(&pf->hw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001593 vsi->seid, del_list, num_del,
1594 NULL);
1595 num_del = 0;
1596 memset(del_list, 0, sizeof(*del_list));
1597
Shannon Nelsonfdfe9cb2014-05-20 08:01:39 +00001598 if (aq_ret &&
1599 pf->hw.aq.asq_last_status !=
1600 I40E_AQ_RC_ENOENT)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001601 dev_info(&pf->pdev->dev,
1602 "ignoring delete macvlan error, err %d, aq_err %d while flushing a full buffer\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001603 aq_ret,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001604 pf->hw.aq.asq_last_status);
1605 }
1606 }
1607 if (num_del) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001608 aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001609 del_list, num_del, NULL);
1610 num_del = 0;
1611
Shannon Nelsonfdfe9cb2014-05-20 08:01:39 +00001612 if (aq_ret &&
1613 pf->hw.aq.asq_last_status != I40E_AQ_RC_ENOENT)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001614 dev_info(&pf->pdev->dev,
1615 "ignoring delete macvlan error, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001616 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001617 }
1618
1619 kfree(del_list);
1620 del_list = NULL;
1621
1622 /* do all the adds now */
1623 filter_list_len = pf->hw.aq.asq_buf_size /
1624 sizeof(struct i40e_aqc_add_macvlan_element_data),
1625 add_list = kcalloc(filter_list_len,
1626 sizeof(struct i40e_aqc_add_macvlan_element_data),
1627 GFP_KERNEL);
1628 if (!add_list)
1629 return -ENOMEM;
1630
1631 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1632 if (!f->changed)
1633 continue;
1634
1635 if (f->counter == 0)
1636 continue;
1637 f->changed = false;
1638 add_happened = true;
1639 cmd_flags = 0;
1640
1641 /* add to add array */
1642 memcpy(add_list[num_add].mac_addr,
1643 f->macaddr, ETH_ALEN);
1644 add_list[num_add].vlan_tag =
1645 cpu_to_le16(
1646 (u16)(f->vlan == I40E_VLAN_ANY ? 0 : f->vlan));
1647 add_list[num_add].queue_number = 0;
1648
1649 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001650 add_list[num_add].flags = cpu_to_le16(cmd_flags);
1651 num_add++;
1652
1653 /* flush a full buffer */
1654 if (num_add == filter_list_len) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001655 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1656 add_list, num_add,
1657 NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001658 num_add = 0;
1659
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001660 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001661 break;
1662 memset(add_list, 0, sizeof(*add_list));
1663 }
1664 }
1665 if (num_add) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001666 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1667 add_list, num_add, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001668 num_add = 0;
1669 }
1670 kfree(add_list);
1671 add_list = NULL;
1672
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001673 if (add_happened && (!aq_ret)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001674 /* do nothing */;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001675 } else if (add_happened && (aq_ret)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001676 dev_info(&pf->pdev->dev,
1677 "add filter failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001678 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001679 if ((pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOSPC) &&
1680 !test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1681 &vsi->state)) {
1682 promisc_forced_on = true;
1683 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1684 &vsi->state);
1685 dev_info(&pf->pdev->dev, "promiscuous mode forced on\n");
1686 }
1687 }
1688 }
1689
1690 /* check for changes in promiscuous modes */
1691 if (changed_flags & IFF_ALLMULTI) {
1692 bool cur_multipromisc;
1693 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001694 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
1695 vsi->seid,
1696 cur_multipromisc,
1697 NULL);
1698 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001699 dev_info(&pf->pdev->dev,
1700 "set multi promisc failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001701 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001702 }
1703 if ((changed_flags & IFF_PROMISC) || promisc_forced_on) {
1704 bool cur_promisc;
1705 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
1706 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1707 &vsi->state));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001708 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(&vsi->back->hw,
1709 vsi->seid,
1710 cur_promisc, NULL);
1711 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001712 dev_info(&pf->pdev->dev,
1713 "set uni promisc failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001714 aq_ret, pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00001715 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
1716 vsi->seid,
1717 cur_promisc, NULL);
1718 if (aq_ret)
1719 dev_info(&pf->pdev->dev,
1720 "set brdcast promisc failed, err %d, aq_err %d\n",
1721 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001722 }
1723
1724 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
1725 return 0;
1726}
1727
1728/**
1729 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
1730 * @pf: board private structure
1731 **/
1732static void i40e_sync_filters_subtask(struct i40e_pf *pf)
1733{
1734 int v;
1735
1736 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
1737 return;
1738 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
1739
Mitch Williams505682c2014-05-20 08:01:37 +00001740 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001741 if (pf->vsi[v] &&
1742 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED))
1743 i40e_sync_vsi_filters(pf->vsi[v]);
1744 }
1745}
1746
1747/**
1748 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
1749 * @netdev: network interface device structure
1750 * @new_mtu: new value for maximum frame size
1751 *
1752 * Returns 0 on success, negative on failure
1753 **/
1754static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
1755{
1756 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg61a46a42014-04-23 04:50:05 +00001757 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001758 struct i40e_vsi *vsi = np->vsi;
1759
1760 /* MTU < 68 is an error and causes problems on some kernels */
1761 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
1762 return -EINVAL;
1763
1764 netdev_info(netdev, "changing MTU from %d to %d\n",
1765 netdev->mtu, new_mtu);
1766 netdev->mtu = new_mtu;
1767 if (netif_running(netdev))
1768 i40e_vsi_reinit_locked(vsi);
1769
1770 return 0;
1771}
1772
1773/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00001774 * i40e_ioctl - Access the hwtstamp interface
1775 * @netdev: network interface device structure
1776 * @ifr: interface request data
1777 * @cmd: ioctl command
1778 **/
1779int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1780{
1781 struct i40e_netdev_priv *np = netdev_priv(netdev);
1782 struct i40e_pf *pf = np->vsi->back;
1783
1784 switch (cmd) {
1785 case SIOCGHWTSTAMP:
1786 return i40e_ptp_get_ts_config(pf, ifr);
1787 case SIOCSHWTSTAMP:
1788 return i40e_ptp_set_ts_config(pf, ifr);
1789 default:
1790 return -EOPNOTSUPP;
1791 }
1792}
1793
1794/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001795 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
1796 * @vsi: the vsi being adjusted
1797 **/
1798void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
1799{
1800 struct i40e_vsi_context ctxt;
1801 i40e_status ret;
1802
1803 if ((vsi->info.valid_sections &
1804 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1805 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
1806 return; /* already enabled */
1807
1808 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1809 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1810 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
1811
1812 ctxt.seid = vsi->seid;
1813 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1814 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1815 if (ret) {
1816 dev_info(&vsi->back->pdev->dev,
1817 "%s: update vsi failed, aq_err=%d\n",
1818 __func__, vsi->back->hw.aq.asq_last_status);
1819 }
1820}
1821
1822/**
1823 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
1824 * @vsi: the vsi being adjusted
1825 **/
1826void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
1827{
1828 struct i40e_vsi_context ctxt;
1829 i40e_status ret;
1830
1831 if ((vsi->info.valid_sections &
1832 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1833 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
1834 I40E_AQ_VSI_PVLAN_EMOD_MASK))
1835 return; /* already disabled */
1836
1837 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1838 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1839 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
1840
1841 ctxt.seid = vsi->seid;
1842 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1843 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1844 if (ret) {
1845 dev_info(&vsi->back->pdev->dev,
1846 "%s: update vsi failed, aq_err=%d\n",
1847 __func__, vsi->back->hw.aq.asq_last_status);
1848 }
1849}
1850
1851/**
1852 * i40e_vlan_rx_register - Setup or shutdown vlan offload
1853 * @netdev: network interface to be adjusted
1854 * @features: netdev features to test if VLAN offload is enabled or not
1855 **/
1856static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
1857{
1858 struct i40e_netdev_priv *np = netdev_priv(netdev);
1859 struct i40e_vsi *vsi = np->vsi;
1860
1861 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1862 i40e_vlan_stripping_enable(vsi);
1863 else
1864 i40e_vlan_stripping_disable(vsi);
1865}
1866
1867/**
1868 * i40e_vsi_add_vlan - Add vsi membership for given vlan
1869 * @vsi: the vsi being configured
1870 * @vid: vlan id to be added (0 = untagged only , -1 = any)
1871 **/
1872int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
1873{
1874 struct i40e_mac_filter *f, *add_f;
1875 bool is_netdev, is_vf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001876
1877 is_vf = (vsi->type == I40E_VSI_SRIOV);
1878 is_netdev = !!(vsi->netdev);
1879
1880 if (is_netdev) {
1881 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid,
1882 is_vf, is_netdev);
1883 if (!add_f) {
1884 dev_info(&vsi->back->pdev->dev,
1885 "Could not add vlan filter %d for %pM\n",
1886 vid, vsi->netdev->dev_addr);
1887 return -ENOMEM;
1888 }
1889 }
1890
1891 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1892 add_f = i40e_add_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
1893 if (!add_f) {
1894 dev_info(&vsi->back->pdev->dev,
1895 "Could not add vlan filter %d for %pM\n",
1896 vid, f->macaddr);
1897 return -ENOMEM;
1898 }
1899 }
1900
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001901 /* Now if we add a vlan tag, make sure to check if it is the first
1902 * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
1903 * with 0, so we now accept untagged and specified tagged traffic
1904 * (and not any taged and untagged)
1905 */
1906 if (vid > 0) {
1907 if (is_netdev && i40e_find_filter(vsi, vsi->netdev->dev_addr,
1908 I40E_VLAN_ANY,
1909 is_vf, is_netdev)) {
1910 i40e_del_filter(vsi, vsi->netdev->dev_addr,
1911 I40E_VLAN_ANY, is_vf, is_netdev);
1912 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0,
1913 is_vf, is_netdev);
1914 if (!add_f) {
1915 dev_info(&vsi->back->pdev->dev,
1916 "Could not add filter 0 for %pM\n",
1917 vsi->netdev->dev_addr);
1918 return -ENOMEM;
1919 }
1920 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08001921 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001922
Greg Rose8d82a7c2014-01-13 16:13:04 -08001923 /* Do not assume that I40E_VLAN_ANY should be reset to VLAN 0 */
1924 if (vid > 0 && !vsi->info.pvid) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001925 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1926 if (i40e_find_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1927 is_vf, is_netdev)) {
1928 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1929 is_vf, is_netdev);
1930 add_f = i40e_add_filter(vsi, f->macaddr,
1931 0, is_vf, is_netdev);
1932 if (!add_f) {
1933 dev_info(&vsi->back->pdev->dev,
1934 "Could not add filter 0 for %pM\n",
1935 f->macaddr);
1936 return -ENOMEM;
1937 }
1938 }
1939 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001940 }
1941
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001942 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1943 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1944 return 0;
1945
1946 return i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001947}
1948
1949/**
1950 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
1951 * @vsi: the vsi being configured
1952 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001953 *
1954 * Return: 0 on success or negative otherwise
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001955 **/
1956int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
1957{
1958 struct net_device *netdev = vsi->netdev;
1959 struct i40e_mac_filter *f, *add_f;
1960 bool is_vf, is_netdev;
1961 int filter_count = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001962
1963 is_vf = (vsi->type == I40E_VSI_SRIOV);
1964 is_netdev = !!(netdev);
1965
1966 if (is_netdev)
1967 i40e_del_filter(vsi, netdev->dev_addr, vid, is_vf, is_netdev);
1968
1969 list_for_each_entry(f, &vsi->mac_filter_list, list)
1970 i40e_del_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
1971
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001972 /* go through all the filters for this VSI and if there is only
1973 * vid == 0 it means there are no other filters, so vid 0 must
1974 * be replaced with -1. This signifies that we should from now
1975 * on accept any traffic (with any tag present, or untagged)
1976 */
1977 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1978 if (is_netdev) {
1979 if (f->vlan &&
1980 ether_addr_equal(netdev->dev_addr, f->macaddr))
1981 filter_count++;
1982 }
1983
1984 if (f->vlan)
1985 filter_count++;
1986 }
1987
1988 if (!filter_count && is_netdev) {
1989 i40e_del_filter(vsi, netdev->dev_addr, 0, is_vf, is_netdev);
1990 f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
1991 is_vf, is_netdev);
1992 if (!f) {
1993 dev_info(&vsi->back->pdev->dev,
1994 "Could not add filter %d for %pM\n",
1995 I40E_VLAN_ANY, netdev->dev_addr);
1996 return -ENOMEM;
1997 }
1998 }
1999
2000 if (!filter_count) {
2001 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2002 i40e_del_filter(vsi, f->macaddr, 0, is_vf, is_netdev);
2003 add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2004 is_vf, is_netdev);
2005 if (!add_f) {
2006 dev_info(&vsi->back->pdev->dev,
2007 "Could not add filter %d for %pM\n",
2008 I40E_VLAN_ANY, f->macaddr);
2009 return -ENOMEM;
2010 }
2011 }
2012 }
2013
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00002014 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
2015 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
2016 return 0;
2017
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002018 return i40e_sync_vsi_filters(vsi);
2019}
2020
2021/**
2022 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2023 * @netdev: network interface to be adjusted
2024 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002025 *
2026 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002027 **/
2028static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2029 __always_unused __be16 proto, u16 vid)
2030{
2031 struct i40e_netdev_priv *np = netdev_priv(netdev);
2032 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002033 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002034
2035 if (vid > 4095)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002036 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002037
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002038 netdev_info(netdev, "adding %pM vid=%d\n", netdev->dev_addr, vid);
2039
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002040 /* If the network stack called us with vid = 0 then
2041 * it is asking to receive priority tagged packets with
2042 * vlan id 0. Our HW receives them by default when configured
2043 * to receive untagged packets so there is no need to add an
2044 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002045 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002046 if (vid)
2047 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002048
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002049 if (!ret && (vid < VLAN_N_VID))
2050 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002051
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002052 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002053}
2054
2055/**
2056 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2057 * @netdev: network interface to be adjusted
2058 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002059 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002060 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002061 **/
2062static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2063 __always_unused __be16 proto, u16 vid)
2064{
2065 struct i40e_netdev_priv *np = netdev_priv(netdev);
2066 struct i40e_vsi *vsi = np->vsi;
2067
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002068 netdev_info(netdev, "removing %pM vid=%d\n", netdev->dev_addr, vid);
2069
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002070 /* return code is ignored as there is nothing a user
2071 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002072 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002073 */
2074 i40e_vsi_kill_vlan(vsi, vid);
2075
2076 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002077
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002078 return 0;
2079}
2080
2081/**
2082 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2083 * @vsi: the vsi being brought back up
2084 **/
2085static void i40e_restore_vlan(struct i40e_vsi *vsi)
2086{
2087 u16 vid;
2088
2089 if (!vsi->netdev)
2090 return;
2091
2092 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2093
2094 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2095 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2096 vid);
2097}
2098
2099/**
2100 * i40e_vsi_add_pvid - Add pvid for the VSI
2101 * @vsi: the vsi being adjusted
2102 * @vid: the vlan id to set as a PVID
2103 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002104int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002105{
2106 struct i40e_vsi_context ctxt;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002107 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002108
2109 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2110 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002111 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2112 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002113 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002114
2115 ctxt.seid = vsi->seid;
2116 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002117 aq_ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2118 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002119 dev_info(&vsi->back->pdev->dev,
2120 "%s: update vsi failed, aq_err=%d\n",
2121 __func__, vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002122 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002123 }
2124
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002125 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002126}
2127
2128/**
2129 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2130 * @vsi: the vsi being adjusted
2131 *
2132 * Just use the vlan_rx_register() service to put it back to normal
2133 **/
2134void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2135{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002136 i40e_vlan_stripping_disable(vsi);
2137
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002138 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002139}
2140
2141/**
2142 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2143 * @vsi: ptr to the VSI
2144 *
2145 * If this function returns with an error, then it's possible one or
2146 * more of the rings is populated (while the rest are not). It is the
2147 * callers duty to clean those orphaned rings.
2148 *
2149 * Return 0 on success, negative on failure
2150 **/
2151static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2152{
2153 int i, err = 0;
2154
2155 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002156 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002157
2158 return err;
2159}
2160
2161/**
2162 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2163 * @vsi: ptr to the VSI
2164 *
2165 * Free VSI's transmit software resources
2166 **/
2167static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2168{
2169 int i;
2170
Greg Rose8e9dca52013-12-18 13:45:53 +00002171 if (!vsi->tx_rings)
2172 return;
2173
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002174 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002175 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002176 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002177}
2178
2179/**
2180 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2181 * @vsi: ptr to the VSI
2182 *
2183 * If this function returns with an error, then it's possible one or
2184 * more of the rings is populated (while the rest are not). It is the
2185 * callers duty to clean those orphaned rings.
2186 *
2187 * Return 0 on success, negative on failure
2188 **/
2189static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2190{
2191 int i, err = 0;
2192
2193 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002194 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002195 return err;
2196}
2197
2198/**
2199 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2200 * @vsi: ptr to the VSI
2201 *
2202 * Free all receive software resources
2203 **/
2204static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2205{
2206 int i;
2207
Greg Rose8e9dca52013-12-18 13:45:53 +00002208 if (!vsi->rx_rings)
2209 return;
2210
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002211 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002212 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002213 i40e_free_rx_resources(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002214}
2215
2216/**
2217 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2218 * @ring: The Tx ring to configure
2219 *
2220 * Configure the Tx descriptor ring in the HMC context.
2221 **/
2222static int i40e_configure_tx_ring(struct i40e_ring *ring)
2223{
2224 struct i40e_vsi *vsi = ring->vsi;
2225 u16 pf_q = vsi->base_queue + ring->queue_index;
2226 struct i40e_hw *hw = &vsi->back->hw;
2227 struct i40e_hmc_obj_txq tx_ctx;
2228 i40e_status err = 0;
2229 u32 qtx_ctl = 0;
2230
2231 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002232 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002233 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2234 ring->atr_count = 0;
2235 } else {
2236 ring->atr_sample_rate = 0;
2237 }
2238
2239 /* initialize XPS */
2240 if (ring->q_vector && ring->netdev &&
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08002241 vsi->tc_config.numtc <= 1 &&
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002242 !test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
2243 netif_set_xps_queue(ring->netdev,
2244 &ring->q_vector->affinity_mask,
2245 ring->queue_index);
2246
2247 /* clear the context structure first */
2248 memset(&tx_ctx, 0, sizeof(tx_ctx));
2249
2250 tx_ctx.new_context = 1;
2251 tx_ctx.base = (ring->dma / 128);
2252 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002253 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2254 I40E_FLAG_FD_ATR_ENABLED));
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002255 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002256 /* FDIR VSI tx ring can still use RS bit and writebacks */
2257 if (vsi->type != I40E_VSI_FDIR)
2258 tx_ctx.head_wb_ena = 1;
2259 tx_ctx.head_wb_addr = ring->dma +
2260 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002261
2262 /* As part of VSI creation/update, FW allocates certain
2263 * Tx arbitration queue sets for each TC enabled for
2264 * the VSI. The FW returns the handles to these queue
2265 * sets as part of the response buffer to Add VSI,
2266 * Update VSI, etc. AQ commands. It is expected that
2267 * these queue set handles be associated with the Tx
2268 * queues by the driver as part of the TX queue context
2269 * initialization. This has to be done regardless of
2270 * DCB as by default everything is mapped to TC0.
2271 */
2272 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2273 tx_ctx.rdylist_act = 0;
2274
2275 /* clear the context in the HMC */
2276 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2277 if (err) {
2278 dev_info(&vsi->back->pdev->dev,
2279 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2280 ring->queue_index, pf_q, err);
2281 return -ENOMEM;
2282 }
2283
2284 /* set the context in the HMC */
2285 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2286 if (err) {
2287 dev_info(&vsi->back->pdev->dev,
2288 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2289 ring->queue_index, pf_q, err);
2290 return -ENOMEM;
2291 }
2292
2293 /* Now associate this queue with this PCI function */
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002294 if (vsi->type == I40E_VSI_VMDQ2)
2295 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
2296 else
2297 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Shannon Nelson13fd9772013-09-28 07:14:19 +00002298 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2299 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002300 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2301 i40e_flush(hw);
2302
2303 clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
2304
2305 /* cache tail off for easier writes later */
2306 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2307
2308 return 0;
2309}
2310
2311/**
2312 * i40e_configure_rx_ring - Configure a receive ring context
2313 * @ring: The Rx ring to configure
2314 *
2315 * Configure the Rx descriptor ring in the HMC context.
2316 **/
2317static int i40e_configure_rx_ring(struct i40e_ring *ring)
2318{
2319 struct i40e_vsi *vsi = ring->vsi;
2320 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2321 u16 pf_q = vsi->base_queue + ring->queue_index;
2322 struct i40e_hw *hw = &vsi->back->hw;
2323 struct i40e_hmc_obj_rxq rx_ctx;
2324 i40e_status err = 0;
2325
2326 ring->state = 0;
2327
2328 /* clear the context structure first */
2329 memset(&rx_ctx, 0, sizeof(rx_ctx));
2330
2331 ring->rx_buf_len = vsi->rx_buf_len;
2332 ring->rx_hdr_len = vsi->rx_hdr_len;
2333
2334 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
2335 rx_ctx.hbuff = ring->rx_hdr_len >> I40E_RXQ_CTX_HBUFF_SHIFT;
2336
2337 rx_ctx.base = (ring->dma / 128);
2338 rx_ctx.qlen = ring->count;
2339
2340 if (vsi->back->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED) {
2341 set_ring_16byte_desc_enabled(ring);
2342 rx_ctx.dsize = 0;
2343 } else {
2344 rx_ctx.dsize = 1;
2345 }
2346
2347 rx_ctx.dtype = vsi->dtype;
2348 if (vsi->dtype) {
2349 set_ring_ps_enabled(ring);
2350 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
2351 I40E_RX_SPLIT_IP |
2352 I40E_RX_SPLIT_TCP_UDP |
2353 I40E_RX_SPLIT_SCTP;
2354 } else {
2355 rx_ctx.hsplit_0 = 0;
2356 }
2357
2358 rx_ctx.rxmax = min_t(u16, vsi->max_frame,
2359 (chain_len * ring->rx_buf_len));
2360 rx_ctx.tphrdesc_ena = 1;
2361 rx_ctx.tphwdesc_ena = 1;
2362 rx_ctx.tphdata_ena = 1;
2363 rx_ctx.tphhead_ena = 1;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00002364 if (hw->revision_id == 0)
2365 rx_ctx.lrxqthresh = 0;
2366 else
2367 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002368 rx_ctx.crcstrip = 1;
2369 rx_ctx.l2tsel = 1;
2370 rx_ctx.showiv = 1;
Catherine Sullivanacb36762014-03-06 09:02:30 +00002371 /* set the prefena field to 1 because the manual says to */
2372 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002373
2374 /* clear the context in the HMC */
2375 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2376 if (err) {
2377 dev_info(&vsi->back->pdev->dev,
2378 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2379 ring->queue_index, pf_q, err);
2380 return -ENOMEM;
2381 }
2382
2383 /* set the context in the HMC */
2384 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2385 if (err) {
2386 dev_info(&vsi->back->pdev->dev,
2387 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2388 ring->queue_index, pf_q, err);
2389 return -ENOMEM;
2390 }
2391
2392 /* cache tail for quicker writes, and clear the reg before use */
2393 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2394 writel(0, ring->tail);
2395
2396 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
2397
2398 return 0;
2399}
2400
2401/**
2402 * i40e_vsi_configure_tx - Configure the VSI for Tx
2403 * @vsi: VSI structure describing this set of rings and resources
2404 *
2405 * Configure the Tx VSI for operation.
2406 **/
2407static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2408{
2409 int err = 0;
2410 u16 i;
2411
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002412 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2413 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002414
2415 return err;
2416}
2417
2418/**
2419 * i40e_vsi_configure_rx - Configure the VSI for Rx
2420 * @vsi: the VSI being configured
2421 *
2422 * Configure the Rx VSI for operation.
2423 **/
2424static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
2425{
2426 int err = 0;
2427 u16 i;
2428
2429 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
2430 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
2431 + ETH_FCS_LEN + VLAN_HLEN;
2432 else
2433 vsi->max_frame = I40E_RXBUFFER_2048;
2434
2435 /* figure out correct receive buffer length */
2436 switch (vsi->back->flags & (I40E_FLAG_RX_1BUF_ENABLED |
2437 I40E_FLAG_RX_PS_ENABLED)) {
2438 case I40E_FLAG_RX_1BUF_ENABLED:
2439 vsi->rx_hdr_len = 0;
2440 vsi->rx_buf_len = vsi->max_frame;
2441 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2442 break;
2443 case I40E_FLAG_RX_PS_ENABLED:
2444 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2445 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2446 vsi->dtype = I40E_RX_DTYPE_HEADER_SPLIT;
2447 break;
2448 default:
2449 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2450 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2451 vsi->dtype = I40E_RX_DTYPE_SPLIT_ALWAYS;
2452 break;
2453 }
2454
2455 /* round up for the chip's needs */
2456 vsi->rx_hdr_len = ALIGN(vsi->rx_hdr_len,
2457 (1 << I40E_RXQ_CTX_HBUFF_SHIFT));
2458 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
2459 (1 << I40E_RXQ_CTX_DBUFF_SHIFT));
2460
2461 /* set up individual rings */
2462 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002463 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002464
2465 return err;
2466}
2467
2468/**
2469 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
2470 * @vsi: ptr to the VSI
2471 **/
2472static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
2473{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00002474 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002475 u16 qoffset, qcount;
2476 int i, n;
2477
2478 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED))
2479 return;
2480
2481 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
2482 if (!(vsi->tc_config.enabled_tc & (1 << n)))
2483 continue;
2484
2485 qoffset = vsi->tc_config.tc_info[n].qoffset;
2486 qcount = vsi->tc_config.tc_info[n].qcount;
2487 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00002488 rx_ring = vsi->rx_rings[i];
2489 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002490 rx_ring->dcb_tc = n;
2491 tx_ring->dcb_tc = n;
2492 }
2493 }
2494}
2495
2496/**
2497 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
2498 * @vsi: ptr to the VSI
2499 **/
2500static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
2501{
2502 if (vsi->netdev)
2503 i40e_set_rx_mode(vsi->netdev);
2504}
2505
2506/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00002507 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
2508 * @vsi: Pointer to the targeted VSI
2509 *
2510 * This function replays the hlist on the hw where all the SB Flow Director
2511 * filters were saved.
2512 **/
2513static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
2514{
2515 struct i40e_fdir_filter *filter;
2516 struct i40e_pf *pf = vsi->back;
2517 struct hlist_node *node;
2518
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00002519 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
2520 return;
2521
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00002522 hlist_for_each_entry_safe(filter, node,
2523 &pf->fdir_filter_list, fdir_node) {
2524 i40e_add_del_fdir(vsi, filter, true);
2525 }
2526}
2527
2528/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002529 * i40e_vsi_configure - Set up the VSI for action
2530 * @vsi: the VSI being configured
2531 **/
2532static int i40e_vsi_configure(struct i40e_vsi *vsi)
2533{
2534 int err;
2535
2536 i40e_set_vsi_rx_mode(vsi);
2537 i40e_restore_vlan(vsi);
2538 i40e_vsi_config_dcb_rings(vsi);
2539 err = i40e_vsi_configure_tx(vsi);
2540 if (!err)
2541 err = i40e_vsi_configure_rx(vsi);
2542
2543 return err;
2544}
2545
2546/**
2547 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
2548 * @vsi: the VSI being configured
2549 **/
2550static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
2551{
2552 struct i40e_pf *pf = vsi->back;
2553 struct i40e_q_vector *q_vector;
2554 struct i40e_hw *hw = &pf->hw;
2555 u16 vector;
2556 int i, q;
2557 u32 val;
2558 u32 qp;
2559
2560 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
2561 * and PFINT_LNKLSTn registers, e.g.:
2562 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
2563 */
2564 qp = vsi->base_queue;
2565 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00002566 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
2567 q_vector = vsi->q_vectors[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002568 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2569 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2570 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
2571 q_vector->rx.itr);
2572 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2573 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2574 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
2575 q_vector->tx.itr);
2576
2577 /* Linked list for the queuepairs assigned to this vector */
2578 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
2579 for (q = 0; q < q_vector->num_ringpairs; q++) {
2580 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2581 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2582 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
2583 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
2584 (I40E_QUEUE_TYPE_TX
2585 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
2586
2587 wr32(hw, I40E_QINT_RQCTL(qp), val);
2588
2589 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2590 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2591 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
2592 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
2593 (I40E_QUEUE_TYPE_RX
2594 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2595
2596 /* Terminate the linked list */
2597 if (q == (q_vector->num_ringpairs - 1))
2598 val |= (I40E_QUEUE_END_OF_LIST
2599 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2600
2601 wr32(hw, I40E_QINT_TQCTL(qp), val);
2602 qp++;
2603 }
2604 }
2605
2606 i40e_flush(hw);
2607}
2608
2609/**
2610 * i40e_enable_misc_int_causes - enable the non-queue interrupts
2611 * @hw: ptr to the hardware info
2612 **/
2613static void i40e_enable_misc_int_causes(struct i40e_hw *hw)
2614{
2615 u32 val;
2616
2617 /* clear things first */
2618 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
2619 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
2620
2621 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
2622 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
2623 I40E_PFINT_ICR0_ENA_GRST_MASK |
2624 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
2625 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002626 I40E_PFINT_ICR0_ENA_TIMESYNC_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002627 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
2628 I40E_PFINT_ICR0_ENA_VFLR_MASK |
2629 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2630
2631 wr32(hw, I40E_PFINT_ICR0_ENA, val);
2632
2633 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00002634 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
2635 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002636
2637 /* OTHER_ITR_IDX = 0 */
2638 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
2639}
2640
2641/**
2642 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
2643 * @vsi: the VSI being configured
2644 **/
2645static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
2646{
Alexander Duyck493fb302013-09-28 07:01:44 +00002647 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002648 struct i40e_pf *pf = vsi->back;
2649 struct i40e_hw *hw = &pf->hw;
2650 u32 val;
2651
2652 /* set the ITR configuration */
2653 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2654 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2655 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
2656 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2657 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2658 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
2659
2660 i40e_enable_misc_int_causes(hw);
2661
2662 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
2663 wr32(hw, I40E_PFINT_LNKLST0, 0);
2664
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00002665 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002666 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2667 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2668 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2669
2670 wr32(hw, I40E_QINT_RQCTL(0), val);
2671
2672 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2673 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2674 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2675
2676 wr32(hw, I40E_QINT_TQCTL(0), val);
2677 i40e_flush(hw);
2678}
2679
2680/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00002681 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
2682 * @pf: board private structure
2683 **/
2684void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
2685{
2686 struct i40e_hw *hw = &pf->hw;
2687
2688 wr32(hw, I40E_PFINT_DYN_CTL0,
2689 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2690 i40e_flush(hw);
2691}
2692
2693/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002694 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
2695 * @pf: board private structure
2696 **/
Shannon Nelson116a57d2013-09-28 07:13:59 +00002697void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002698{
2699 struct i40e_hw *hw = &pf->hw;
2700 u32 val;
2701
2702 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
2703 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
2704 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
2705
2706 wr32(hw, I40E_PFINT_DYN_CTL0, val);
2707 i40e_flush(hw);
2708}
2709
2710/**
2711 * i40e_irq_dynamic_enable - Enable default interrupt generation settings
2712 * @vsi: pointer to a vsi
2713 * @vector: enable a particular Hw Interrupt vector
2714 **/
2715void i40e_irq_dynamic_enable(struct i40e_vsi *vsi, int vector)
2716{
2717 struct i40e_pf *pf = vsi->back;
2718 struct i40e_hw *hw = &pf->hw;
2719 u32 val;
2720
2721 val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
2722 I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
2723 (I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2724 wr32(hw, I40E_PFINT_DYN_CTLN(vector - 1), val);
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00002725 /* skip the flush */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002726}
2727
2728/**
2729 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
2730 * @irq: interrupt number
2731 * @data: pointer to a q_vector
2732 **/
2733static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
2734{
2735 struct i40e_q_vector *q_vector = data;
2736
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 return IRQ_HANDLED;
2739
2740 napi_schedule(&q_vector->napi);
2741
2742 return IRQ_HANDLED;
2743}
2744
2745/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002746 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
2747 * @vsi: the VSI being configured
2748 * @basename: name for the vector
2749 *
2750 * Allocates MSI-X vectors and requests interrupts from the kernel.
2751 **/
2752static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
2753{
2754 int q_vectors = vsi->num_q_vectors;
2755 struct i40e_pf *pf = vsi->back;
2756 int base = vsi->base_vector;
2757 int rx_int_idx = 0;
2758 int tx_int_idx = 0;
2759 int vector, err;
2760
2761 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00002762 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002763
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002764 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002765 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2766 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
2767 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002768 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002769 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2770 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002771 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002772 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2773 "%s-%s-%d", basename, "tx", tx_int_idx++);
2774 } else {
2775 /* skip this unused q_vector */
2776 continue;
2777 }
2778 err = request_irq(pf->msix_entries[base + vector].vector,
2779 vsi->irq_handler,
2780 0,
2781 q_vector->name,
2782 q_vector);
2783 if (err) {
2784 dev_info(&pf->pdev->dev,
2785 "%s: request_irq failed, error: %d\n",
2786 __func__, err);
2787 goto free_queue_irqs;
2788 }
2789 /* assign the mask for this irq */
2790 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2791 &q_vector->affinity_mask);
2792 }
2793
Shannon Nelson63741842014-04-23 04:50:16 +00002794 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002795 return 0;
2796
2797free_queue_irqs:
2798 while (vector) {
2799 vector--;
2800 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2801 NULL);
2802 free_irq(pf->msix_entries[base + vector].vector,
2803 &(vsi->q_vectors[vector]));
2804 }
2805 return err;
2806}
2807
2808/**
2809 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
2810 * @vsi: the VSI being un-configured
2811 **/
2812static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
2813{
2814 struct i40e_pf *pf = vsi->back;
2815 struct i40e_hw *hw = &pf->hw;
2816 int base = vsi->base_vector;
2817 int i;
2818
2819 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002820 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
2821 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002822 }
2823
2824 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2825 for (i = vsi->base_vector;
2826 i < (vsi->num_q_vectors + vsi->base_vector); i++)
2827 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
2828
2829 i40e_flush(hw);
2830 for (i = 0; i < vsi->num_q_vectors; i++)
2831 synchronize_irq(pf->msix_entries[i + base].vector);
2832 } else {
2833 /* Legacy and MSI mode - this stops all interrupt handling */
2834 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
2835 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
2836 i40e_flush(hw);
2837 synchronize_irq(pf->pdev->irq);
2838 }
2839}
2840
2841/**
2842 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
2843 * @vsi: the VSI being configured
2844 **/
2845static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
2846{
2847 struct i40e_pf *pf = vsi->back;
2848 int i;
2849
2850 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2851 for (i = vsi->base_vector;
2852 i < (vsi->num_q_vectors + vsi->base_vector); i++)
2853 i40e_irq_dynamic_enable(vsi, i);
2854 } else {
2855 i40e_irq_dynamic_enable_icr0(pf);
2856 }
2857
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00002858 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002859 return 0;
2860}
2861
2862/**
2863 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
2864 * @pf: board private structure
2865 **/
2866static void i40e_stop_misc_vector(struct i40e_pf *pf)
2867{
2868 /* Disable ICR 0 */
2869 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
2870 i40e_flush(&pf->hw);
2871}
2872
2873/**
2874 * i40e_intr - MSI/Legacy and non-queue interrupt handler
2875 * @irq: interrupt number
2876 * @data: pointer to a q_vector
2877 *
2878 * This is the handler used for all MSI/Legacy interrupts, and deals
2879 * with both queue and non-queue interrupts. This is also used in
2880 * MSIX mode to handle the non-queue interrupts.
2881 **/
2882static irqreturn_t i40e_intr(int irq, void *data)
2883{
2884 struct i40e_pf *pf = (struct i40e_pf *)data;
2885 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002886 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002887 u32 icr0, icr0_remaining;
2888 u32 val, ena_mask;
2889
2890 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002891 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002892
Shannon Nelson116a57d2013-09-28 07:13:59 +00002893 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
2894 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002895 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002896
Shannon Nelsoncd92e722013-11-16 10:00:44 +00002897 /* if interrupt but no bits showing, must be SWINT */
2898 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
2899 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
2900 pf->sw_int_count++;
2901
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002902 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
2903 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
2904
2905 /* temporarily disable queue cause for NAPI processing */
2906 u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
2907 qval &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
2908 wr32(hw, I40E_QINT_RQCTL(0), qval);
2909
2910 qval = rd32(hw, I40E_QINT_TQCTL(0));
2911 qval &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
2912 wr32(hw, I40E_QINT_TQCTL(0), qval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002913
2914 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck493fb302013-09-28 07:01:44 +00002915 napi_schedule(&pf->vsi[pf->lan_vsi]->q_vectors[0]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002916 }
2917
2918 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
2919 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2920 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
2921 }
2922
2923 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
2924 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
2925 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
2926 }
2927
2928 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
2929 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
2930 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
2931 }
2932
2933 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
2934 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
2935 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
2936 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
2937 val = rd32(hw, I40E_GLGEN_RSTAT);
2938 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
2939 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002940 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002941 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002942 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002943 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002944 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002945 pf->empr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002946 set_bit(__I40E_EMP_RESET_REQUESTED, &pf->state);
2947 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002948 }
2949
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002950 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
2951 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
2952 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
2953 }
2954
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002955 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
2956 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
2957
2958 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07002959 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002960 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002961 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002962 }
2963
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002964 /* If a critical error is pending we have no choice but to reset the
2965 * device.
2966 * Report and mask out any remaining unexpected interrupts.
2967 */
2968 icr0_remaining = icr0 & ena_mask;
2969 if (icr0_remaining) {
2970 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
2971 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002972 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002973 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00002974 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002975 dev_info(&pf->pdev->dev, "device will be reset\n");
2976 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
2977 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002978 }
2979 ena_mask &= ~icr0_remaining;
2980 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002981 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002982
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002983enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002984 /* re-enable interrupt causes */
2985 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002986 if (!test_bit(__I40E_DOWN, &pf->state)) {
2987 i40e_service_event_schedule(pf);
2988 i40e_irq_dynamic_enable_icr0(pf);
2989 }
2990
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002991 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002992}
2993
2994/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08002995 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
2996 * @tx_ring: tx ring to clean
2997 * @budget: how many cleans we're allowed
2998 *
2999 * Returns true if there's any budget left (e.g. the clean is finished)
3000 **/
3001static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3002{
3003 struct i40e_vsi *vsi = tx_ring->vsi;
3004 u16 i = tx_ring->next_to_clean;
3005 struct i40e_tx_buffer *tx_buf;
3006 struct i40e_tx_desc *tx_desc;
3007
3008 tx_buf = &tx_ring->tx_bi[i];
3009 tx_desc = I40E_TX_DESC(tx_ring, i);
3010 i -= tx_ring->count;
3011
3012 do {
3013 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3014
3015 /* if next_to_watch is not set then there is no work pending */
3016 if (!eop_desc)
3017 break;
3018
3019 /* prevent any other reads prior to eop_desc */
3020 read_barrier_depends();
3021
3022 /* if the descriptor isn't done, no work yet to do */
3023 if (!(eop_desc->cmd_type_offset_bsz &
3024 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3025 break;
3026
3027 /* clear next_to_watch to prevent false hangs */
3028 tx_buf->next_to_watch = NULL;
3029
3030 /* unmap skb header data */
3031 dma_unmap_single(tx_ring->dev,
3032 dma_unmap_addr(tx_buf, dma),
3033 dma_unmap_len(tx_buf, len),
3034 DMA_TO_DEVICE);
3035
3036 dma_unmap_len_set(tx_buf, len, 0);
3037
3038
3039 /* move to the next desc and buffer to clean */
3040 tx_buf++;
3041 tx_desc++;
3042 i++;
3043 if (unlikely(!i)) {
3044 i -= tx_ring->count;
3045 tx_buf = tx_ring->tx_bi;
3046 tx_desc = I40E_TX_DESC(tx_ring, 0);
3047 }
3048
3049 /* update budget accounting */
3050 budget--;
3051 } while (likely(budget));
3052
3053 i += tx_ring->count;
3054 tx_ring->next_to_clean = i;
3055
3056 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
3057 i40e_irq_dynamic_enable(vsi,
3058 tx_ring->q_vector->v_idx + vsi->base_vector);
3059 }
3060 return budget > 0;
3061}
3062
3063/**
3064 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3065 * @irq: interrupt number
3066 * @data: pointer to a q_vector
3067 **/
3068static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3069{
3070 struct i40e_q_vector *q_vector = data;
3071 struct i40e_vsi *vsi;
3072
3073 if (!q_vector->tx.ring)
3074 return IRQ_HANDLED;
3075
3076 vsi = q_vector->tx.ring->vsi;
3077 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3078
3079 return IRQ_HANDLED;
3080}
3081
3082/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003083 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003084 * @vsi: the VSI being configured
3085 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003086 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003087 **/
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003088static void map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003089{
Alexander Duyck493fb302013-09-28 07:01:44 +00003090 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00003091 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3092 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003093
3094 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003095 tx_ring->next = q_vector->tx.ring;
3096 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003097 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003098
3099 rx_ring->q_vector = q_vector;
3100 rx_ring->next = q_vector->rx.ring;
3101 q_vector->rx.ring = rx_ring;
3102 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003103}
3104
3105/**
3106 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3107 * @vsi: the VSI being configured
3108 *
3109 * This function maps descriptor rings to the queue-specific vectors
3110 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3111 * one vector per queue pair, but on a constrained vector budget, we
3112 * group the queue pairs as "efficiently" as possible.
3113 **/
3114static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3115{
3116 int qp_remaining = vsi->num_queue_pairs;
3117 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003118 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003119 int v_start = 0;
3120 int qp_idx = 0;
3121
3122 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3123 * group them so there are multiple queues per vector.
3124 */
3125 for (; v_start < q_vectors && qp_remaining; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003126 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3127
3128 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3129
3130 q_vector->num_ringpairs = num_ringpairs;
3131
3132 q_vector->rx.count = 0;
3133 q_vector->tx.count = 0;
3134 q_vector->rx.ring = NULL;
3135 q_vector->tx.ring = NULL;
3136
3137 while (num_ringpairs--) {
3138 map_vector_to_qp(vsi, v_start, qp_idx);
3139 qp_idx++;
3140 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003141 }
3142 }
3143}
3144
3145/**
3146 * i40e_vsi_request_irq - Request IRQ from the OS
3147 * @vsi: the VSI being configured
3148 * @basename: name for the vector
3149 **/
3150static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3151{
3152 struct i40e_pf *pf = vsi->back;
3153 int err;
3154
3155 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3156 err = i40e_vsi_request_irq_msix(vsi, basename);
3157 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3158 err = request_irq(pf->pdev->irq, i40e_intr, 0,
3159 pf->misc_int_name, pf);
3160 else
3161 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
3162 pf->misc_int_name, pf);
3163
3164 if (err)
3165 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3166
3167 return err;
3168}
3169
3170#ifdef CONFIG_NET_POLL_CONTROLLER
3171/**
3172 * i40e_netpoll - A Polling 'interrupt'handler
3173 * @netdev: network interface device structure
3174 *
3175 * This is used by netconsole to send skbs without having to re-enable
3176 * interrupts. It's not called while the normal interrupt routine is executing.
3177 **/
3178static void i40e_netpoll(struct net_device *netdev)
3179{
3180 struct i40e_netdev_priv *np = netdev_priv(netdev);
3181 struct i40e_vsi *vsi = np->vsi;
3182 struct i40e_pf *pf = vsi->back;
3183 int i;
3184
3185 /* if interface is down do nothing */
3186 if (test_bit(__I40E_DOWN, &vsi->state))
3187 return;
3188
3189 pf->flags |= I40E_FLAG_IN_NETPOLL;
3190 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3191 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003192 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003193 } else {
3194 i40e_intr(pf->pdev->irq, netdev);
3195 }
3196 pf->flags &= ~I40E_FLAG_IN_NETPOLL;
3197}
3198#endif
3199
3200/**
3201 * i40e_vsi_control_tx - Start or stop a VSI's rings
3202 * @vsi: the VSI being configured
3203 * @enable: start or stop the rings
3204 **/
3205static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3206{
3207 struct i40e_pf *pf = vsi->back;
3208 struct i40e_hw *hw = &pf->hw;
3209 int i, j, pf_q;
3210 u32 tx_reg;
3211
3212 pf_q = vsi->base_queue;
3213 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Matt Jared351499a2014-04-23 04:50:03 +00003214
3215 /* warn the TX unit of coming changes */
3216 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
3217 if (!enable)
3218 udelay(10);
3219
Mitch Williams6c5ef622014-02-20 19:29:16 -08003220 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003221 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003222 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3223 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3224 break;
3225 usleep_range(1000, 2000);
3226 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003227 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c122002014-03-14 07:32:29 +00003228 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00003229 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003230
3231 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003232 if (enable) {
3233 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003234 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003235 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003236 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003237 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003238
3239 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
3240
3241 /* wait for the change to finish */
3242 for (j = 0; j < 10; j++) {
3243 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Catherine Sullivan7c122002014-03-14 07:32:29 +00003244 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3245 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003246
3247 udelay(10);
3248 }
3249 if (j >= 10) {
3250 dev_info(&pf->pdev->dev, "Tx ring %d %sable timeout\n",
3251 pf_q, (enable ? "en" : "dis"));
3252 return -ETIMEDOUT;
3253 }
3254 }
3255
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003256 if (hw->revision_id == 0)
3257 mdelay(50);
3258
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003259 return 0;
3260}
3261
3262/**
3263 * i40e_vsi_control_rx - Start or stop a VSI's rings
3264 * @vsi: the VSI being configured
3265 * @enable: start or stop the rings
3266 **/
3267static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3268{
3269 struct i40e_pf *pf = vsi->back;
3270 struct i40e_hw *hw = &pf->hw;
3271 int i, j, pf_q;
3272 u32 rx_reg;
3273
3274 pf_q = vsi->base_queue;
3275 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003276 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003277 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003278 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
3279 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
3280 break;
3281 usleep_range(1000, 2000);
3282 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003283
Catherine Sullivan7c122002014-03-14 07:32:29 +00003284 /* Skip if the queue is already in the requested state */
3285 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3286 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003287
3288 /* turn on/off the queue */
3289 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08003290 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003291 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08003292 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003293 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
3294
3295 /* wait for the change to finish */
3296 for (j = 0; j < 10; j++) {
3297 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
3298
Catherine Sullivan7c122002014-03-14 07:32:29 +00003299 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3300 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003301
3302 udelay(10);
3303 }
3304 if (j >= 10) {
3305 dev_info(&pf->pdev->dev, "Rx ring %d %sable timeout\n",
3306 pf_q, (enable ? "en" : "dis"));
3307 return -ETIMEDOUT;
3308 }
3309 }
3310
3311 return 0;
3312}
3313
3314/**
3315 * i40e_vsi_control_rings - Start or stop a VSI's rings
3316 * @vsi: the VSI being configured
3317 * @enable: start or stop the rings
3318 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00003319int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003320{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003321 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003322
3323 /* do rx first for enable and last for disable */
3324 if (request) {
3325 ret = i40e_vsi_control_rx(vsi, request);
3326 if (ret)
3327 return ret;
3328 ret = i40e_vsi_control_tx(vsi, request);
3329 } else {
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003330 /* Ignore return value, we need to shutdown whatever we can */
3331 i40e_vsi_control_tx(vsi, request);
3332 i40e_vsi_control_rx(vsi, request);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003333 }
3334
3335 return ret;
3336}
3337
3338/**
3339 * i40e_vsi_free_irq - Free the irq association with the OS
3340 * @vsi: the VSI being configured
3341 **/
3342static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
3343{
3344 struct i40e_pf *pf = vsi->back;
3345 struct i40e_hw *hw = &pf->hw;
3346 int base = vsi->base_vector;
3347 u32 val, qp;
3348 int i;
3349
3350 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3351 if (!vsi->q_vectors)
3352 return;
3353
Shannon Nelson63741842014-04-23 04:50:16 +00003354 if (!vsi->irqs_ready)
3355 return;
3356
3357 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003358 for (i = 0; i < vsi->num_q_vectors; i++) {
3359 u16 vector = i + base;
3360
3361 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00003362 if (!vsi->q_vectors[i] ||
3363 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003364 continue;
3365
3366 /* clear the affinity_mask in the IRQ descriptor */
3367 irq_set_affinity_hint(pf->msix_entries[vector].vector,
3368 NULL);
3369 free_irq(pf->msix_entries[vector].vector,
Alexander Duyck493fb302013-09-28 07:01:44 +00003370 vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003371
3372 /* Tear down the interrupt queue link list
3373 *
3374 * We know that they come in pairs and always
3375 * the Rx first, then the Tx. To clear the
3376 * link list, stick the EOL value into the
3377 * next_q field of the registers.
3378 */
3379 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
3380 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3381 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3382 val |= I40E_QUEUE_END_OF_LIST
3383 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3384 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
3385
3386 while (qp != I40E_QUEUE_END_OF_LIST) {
3387 u32 next;
3388
3389 val = rd32(hw, I40E_QINT_RQCTL(qp));
3390
3391 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3392 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3393 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3394 I40E_QINT_RQCTL_INTEVENT_MASK);
3395
3396 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3397 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3398
3399 wr32(hw, I40E_QINT_RQCTL(qp), val);
3400
3401 val = rd32(hw, I40E_QINT_TQCTL(qp));
3402
3403 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
3404 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
3405
3406 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3407 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3408 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3409 I40E_QINT_TQCTL_INTEVENT_MASK);
3410
3411 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3412 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3413
3414 wr32(hw, I40E_QINT_TQCTL(qp), val);
3415 qp = next;
3416 }
3417 }
3418 } else {
3419 free_irq(pf->pdev->irq, pf);
3420
3421 val = rd32(hw, I40E_PFINT_LNKLST0);
3422 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3423 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3424 val |= I40E_QUEUE_END_OF_LIST
3425 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
3426 wr32(hw, I40E_PFINT_LNKLST0, val);
3427
3428 val = rd32(hw, I40E_QINT_RQCTL(qp));
3429 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3430 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3431 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3432 I40E_QINT_RQCTL_INTEVENT_MASK);
3433
3434 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3435 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3436
3437 wr32(hw, I40E_QINT_RQCTL(qp), val);
3438
3439 val = rd32(hw, I40E_QINT_TQCTL(qp));
3440
3441 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3442 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3443 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3444 I40E_QINT_TQCTL_INTEVENT_MASK);
3445
3446 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3447 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3448
3449 wr32(hw, I40E_QINT_TQCTL(qp), val);
3450 }
3451}
3452
3453/**
Alexander Duyck493fb302013-09-28 07:01:44 +00003454 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
3455 * @vsi: the VSI being configured
3456 * @v_idx: Index of vector to be freed
3457 *
3458 * This function frees the memory allocated to the q_vector. In addition if
3459 * NAPI is enabled it will delete any references to the NAPI struct prior
3460 * to freeing the q_vector.
3461 **/
3462static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
3463{
3464 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003465 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00003466
3467 if (!q_vector)
3468 return;
3469
3470 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003471 i40e_for_each_ring(ring, q_vector->tx)
3472 ring->q_vector = NULL;
3473
3474 i40e_for_each_ring(ring, q_vector->rx)
3475 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00003476
3477 /* only VSI w/ an associated netdev is set up w/ NAPI */
3478 if (vsi->netdev)
3479 netif_napi_del(&q_vector->napi);
3480
3481 vsi->q_vectors[v_idx] = NULL;
3482
3483 kfree_rcu(q_vector, rcu);
3484}
3485
3486/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003487 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
3488 * @vsi: the VSI being un-configured
3489 *
3490 * This frees the memory allocated to the q_vectors and
3491 * deletes references to the NAPI struct.
3492 **/
3493static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
3494{
3495 int v_idx;
3496
Alexander Duyck493fb302013-09-28 07:01:44 +00003497 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
3498 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003499}
3500
3501/**
3502 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
3503 * @pf: board private structure
3504 **/
3505static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
3506{
3507 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
3508 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3509 pci_disable_msix(pf->pdev);
3510 kfree(pf->msix_entries);
3511 pf->msix_entries = NULL;
3512 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
3513 pci_disable_msi(pf->pdev);
3514 }
3515 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
3516}
3517
3518/**
3519 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
3520 * @pf: board private structure
3521 *
3522 * We go through and clear interrupt specific resources and reset the structure
3523 * to pre-load conditions
3524 **/
3525static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
3526{
3527 int i;
3528
3529 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00003530 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003531 if (pf->vsi[i])
3532 i40e_vsi_free_q_vectors(pf->vsi[i]);
3533 i40e_reset_interrupt_capability(pf);
3534}
3535
3536/**
3537 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
3538 * @vsi: the VSI being configured
3539 **/
3540static void i40e_napi_enable_all(struct i40e_vsi *vsi)
3541{
3542 int q_idx;
3543
3544 if (!vsi->netdev)
3545 return;
3546
3547 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003548 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003549}
3550
3551/**
3552 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
3553 * @vsi: the VSI being configured
3554 **/
3555static void i40e_napi_disable_all(struct i40e_vsi *vsi)
3556{
3557 int q_idx;
3558
3559 if (!vsi->netdev)
3560 return;
3561
3562 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003563 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003564}
3565
3566/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00003567 * i40e_vsi_close - Shut down a VSI
3568 * @vsi: the vsi to be quelled
3569 **/
3570static void i40e_vsi_close(struct i40e_vsi *vsi)
3571{
3572 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
3573 i40e_down(vsi);
3574 i40e_vsi_free_irq(vsi);
3575 i40e_vsi_free_tx_resources(vsi);
3576 i40e_vsi_free_rx_resources(vsi);
3577}
3578
3579/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003580 * i40e_quiesce_vsi - Pause a given VSI
3581 * @vsi: the VSI being paused
3582 **/
3583static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
3584{
3585 if (test_bit(__I40E_DOWN, &vsi->state))
3586 return;
3587
3588 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
3589 if (vsi->netdev && netif_running(vsi->netdev)) {
3590 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
3591 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00003592 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003593 }
3594}
3595
3596/**
3597 * i40e_unquiesce_vsi - Resume a given VSI
3598 * @vsi: the VSI being resumed
3599 **/
3600static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
3601{
3602 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
3603 return;
3604
3605 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
3606 if (vsi->netdev && netif_running(vsi->netdev))
3607 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
3608 else
Shannon Nelson8276f752014-03-14 07:32:27 +00003609 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003610}
3611
3612/**
3613 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
3614 * @pf: the PF
3615 **/
3616static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
3617{
3618 int v;
3619
Mitch Williams505682c2014-05-20 08:01:37 +00003620 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003621 if (pf->vsi[v])
3622 i40e_quiesce_vsi(pf->vsi[v]);
3623 }
3624}
3625
3626/**
3627 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
3628 * @pf: the PF
3629 **/
3630static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
3631{
3632 int v;
3633
Mitch Williams505682c2014-05-20 08:01:37 +00003634 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003635 if (pf->vsi[v])
3636 i40e_unquiesce_vsi(pf->vsi[v]);
3637 }
3638}
3639
3640/**
3641 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
3642 * @dcbcfg: the corresponding DCBx configuration structure
3643 *
3644 * Return the number of TCs from given DCBx configuration
3645 **/
3646static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
3647{
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003648 u8 num_tc = 0;
3649 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003650
3651 /* Scan the ETS Config Priority Table to find
3652 * traffic class enabled for a given priority
3653 * and use the traffic class index to get the
3654 * number of traffic classes enabled
3655 */
3656 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3657 if (dcbcfg->etscfg.prioritytable[i] > num_tc)
3658 num_tc = dcbcfg->etscfg.prioritytable[i];
3659 }
3660
3661 /* Traffic class index starts from zero so
3662 * increment to return the actual count
3663 */
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003664 return num_tc + 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003665}
3666
3667/**
3668 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
3669 * @dcbcfg: the corresponding DCBx configuration structure
3670 *
3671 * Query the current DCB configuration and return the number of
3672 * traffic classes enabled from the given DCBX config
3673 **/
3674static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
3675{
3676 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
3677 u8 enabled_tc = 1;
3678 u8 i;
3679
3680 for (i = 0; i < num_tc; i++)
3681 enabled_tc |= 1 << i;
3682
3683 return enabled_tc;
3684}
3685
3686/**
3687 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
3688 * @pf: PF being queried
3689 *
3690 * Return number of traffic classes enabled for the given PF
3691 **/
3692static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
3693{
3694 struct i40e_hw *hw = &pf->hw;
3695 u8 i, enabled_tc;
3696 u8 num_tc = 0;
3697 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3698
3699 /* If DCB is not enabled then always in single TC */
3700 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3701 return 1;
3702
3703 /* MFP mode return count of enabled TCs for this PF */
3704 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
3705 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3706 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3707 if (enabled_tc & (1 << i))
3708 num_tc++;
3709 }
3710 return num_tc;
3711 }
3712
3713 /* SFP mode will be enabled for all TCs on port */
3714 return i40e_dcb_get_num_tc(dcbcfg);
3715}
3716
3717/**
3718 * i40e_pf_get_default_tc - Get bitmap for first enabled TC
3719 * @pf: PF being queried
3720 *
3721 * Return a bitmap for first enabled traffic class for this PF.
3722 **/
3723static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
3724{
3725 u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3726 u8 i = 0;
3727
3728 if (!enabled_tc)
3729 return 0x1; /* TC0 */
3730
3731 /* Find the first enabled TC */
3732 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3733 if (enabled_tc & (1 << i))
3734 break;
3735 }
3736
3737 return 1 << i;
3738}
3739
3740/**
3741 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
3742 * @pf: PF being queried
3743 *
3744 * Return a bitmap for enabled traffic classes for this PF.
3745 **/
3746static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
3747{
3748 /* If DCB is not enabled for this PF then just return default TC */
3749 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3750 return i40e_pf_get_default_tc(pf);
3751
3752 /* MFP mode will have enabled TCs set by FW */
3753 if (pf->flags & I40E_FLAG_MFP_ENABLED)
3754 return pf->hw.func_caps.enabled_tcmap;
3755
3756 /* SFP mode we want PF to be enabled for all TCs */
3757 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
3758}
3759
3760/**
3761 * i40e_vsi_get_bw_info - Query VSI BW Information
3762 * @vsi: the VSI being queried
3763 *
3764 * Returns 0 on success, negative value on failure
3765 **/
3766static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
3767{
3768 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
3769 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
3770 struct i40e_pf *pf = vsi->back;
3771 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003772 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003773 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003774 int i;
3775
3776 /* Get the VSI level BW configuration */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003777 aq_ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
3778 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003779 dev_info(&pf->pdev->dev,
3780 "couldn't get pf vsi bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003781 aq_ret, pf->hw.aq.asq_last_status);
3782 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003783 }
3784
3785 /* Get the VSI level BW configuration per TC */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003786 aq_ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
Jesse Brandeburg6838b532014-01-14 00:49:52 -08003787 NULL);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003788 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003789 dev_info(&pf->pdev->dev,
3790 "couldn't get pf vsi ets bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003791 aq_ret, pf->hw.aq.asq_last_status);
3792 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003793 }
3794
3795 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
3796 dev_info(&pf->pdev->dev,
3797 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
3798 bw_config.tc_valid_bits,
3799 bw_ets_config.tc_valid_bits);
3800 /* Still continuing */
3801 }
3802
3803 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
3804 vsi->bw_max_quanta = bw_config.max_bw;
3805 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
3806 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
3807 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3808 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
3809 vsi->bw_ets_limit_credits[i] =
3810 le16_to_cpu(bw_ets_config.credits[i]);
3811 /* 3 bits out of 4 for each TC */
3812 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
3813 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003814
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003815 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003816}
3817
3818/**
3819 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
3820 * @vsi: the VSI being configured
3821 * @enabled_tc: TC bitmap
3822 * @bw_credits: BW shared credits per TC
3823 *
3824 * Returns 0 on success, negative value on failure
3825 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003826static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003827 u8 *bw_share)
3828{
3829 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003830 i40e_status aq_ret;
3831 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003832
3833 bw_data.tc_valid_bits = enabled_tc;
3834 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3835 bw_data.tc_bw_credits[i] = bw_share[i];
3836
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003837 aq_ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
3838 NULL);
3839 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003840 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00003841 "AQ command Config VSI BW allocation per TC failed = %d\n",
3842 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003843 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003844 }
3845
3846 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3847 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
3848
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003849 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003850}
3851
3852/**
3853 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
3854 * @vsi: the VSI being configured
3855 * @enabled_tc: TC map to be enabled
3856 *
3857 **/
3858static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3859{
3860 struct net_device *netdev = vsi->netdev;
3861 struct i40e_pf *pf = vsi->back;
3862 struct i40e_hw *hw = &pf->hw;
3863 u8 netdev_tc = 0;
3864 int i;
3865 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3866
3867 if (!netdev)
3868 return;
3869
3870 if (!enabled_tc) {
3871 netdev_reset_tc(netdev);
3872 return;
3873 }
3874
3875 /* Set up actual enabled TCs on the VSI */
3876 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
3877 return;
3878
3879 /* set per TC queues for the VSI */
3880 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3881 /* Only set TC queues for enabled tcs
3882 *
3883 * e.g. For a VSI that has TC0 and TC3 enabled the
3884 * enabled_tc bitmap would be 0x00001001; the driver
3885 * will set the numtc for netdev as 2 that will be
3886 * referenced by the netdev layer as TC 0 and 1.
3887 */
3888 if (vsi->tc_config.enabled_tc & (1 << i))
3889 netdev_set_tc_queue(netdev,
3890 vsi->tc_config.tc_info[i].netdev_tc,
3891 vsi->tc_config.tc_info[i].qcount,
3892 vsi->tc_config.tc_info[i].qoffset);
3893 }
3894
3895 /* Assign UP2TC map for the VSI */
3896 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3897 /* Get the actual TC# for the UP */
3898 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
3899 /* Get the mapped netdev TC# for the UP */
3900 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
3901 netdev_set_prio_tc_map(netdev, i, netdev_tc);
3902 }
3903}
3904
3905/**
3906 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
3907 * @vsi: the VSI being configured
3908 * @ctxt: the ctxt buffer returned from AQ VSI update param command
3909 **/
3910static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
3911 struct i40e_vsi_context *ctxt)
3912{
3913 /* copy just the sections touched not the entire info
3914 * since not all sections are valid as returned by
3915 * update vsi params
3916 */
3917 vsi->info.mapping_flags = ctxt->info.mapping_flags;
3918 memcpy(&vsi->info.queue_mapping,
3919 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
3920 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
3921 sizeof(vsi->info.tc_mapping));
3922}
3923
3924/**
3925 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
3926 * @vsi: VSI to be configured
3927 * @enabled_tc: TC bitmap
3928 *
3929 * This configures a particular VSI for TCs that are mapped to the
3930 * given TC bitmap. It uses default bandwidth share for TCs across
3931 * VSIs to configure TC for a particular VSI.
3932 *
3933 * NOTE:
3934 * It is expected that the VSI queues have been quisced before calling
3935 * this function.
3936 **/
3937static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3938{
3939 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
3940 struct i40e_vsi_context ctxt;
3941 int ret = 0;
3942 int i;
3943
3944 /* Check if enabled_tc is same as existing or new TCs */
3945 if (vsi->tc_config.enabled_tc == enabled_tc)
3946 return ret;
3947
3948 /* Enable ETS TCs with equal BW Share for now across all VSIs */
3949 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3950 if (enabled_tc & (1 << i))
3951 bw_share[i] = 1;
3952 }
3953
3954 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
3955 if (ret) {
3956 dev_info(&vsi->back->pdev->dev,
3957 "Failed configuring TC map %d for VSI %d\n",
3958 enabled_tc, vsi->seid);
3959 goto out;
3960 }
3961
3962 /* Update Queue Pairs Mapping for currently enabled UPs */
3963 ctxt.seid = vsi->seid;
3964 ctxt.pf_num = vsi->back->hw.pf_id;
3965 ctxt.vf_num = 0;
3966 ctxt.uplink_seid = vsi->uplink_seid;
3967 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
3968 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
3969
3970 /* Update the VSI after updating the VSI queue-mapping information */
3971 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3972 if (ret) {
3973 dev_info(&vsi->back->pdev->dev,
3974 "update vsi failed, aq_err=%d\n",
3975 vsi->back->hw.aq.asq_last_status);
3976 goto out;
3977 }
3978 /* update the local VSI info with updated queue map */
3979 i40e_vsi_update_queue_map(vsi, &ctxt);
3980 vsi->info.valid_sections = 0;
3981
3982 /* Update current VSI BW information */
3983 ret = i40e_vsi_get_bw_info(vsi);
3984 if (ret) {
3985 dev_info(&vsi->back->pdev->dev,
3986 "Failed updating vsi bw info, aq_err=%d\n",
3987 vsi->back->hw.aq.asq_last_status);
3988 goto out;
3989 }
3990
3991 /* Update the netdev TC setup */
3992 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
3993out:
3994 return ret;
3995}
3996
3997/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08003998 * i40e_veb_config_tc - Configure TCs for given VEB
3999 * @veb: given VEB
4000 * @enabled_tc: TC bitmap
4001 *
4002 * Configures given TC bitmap for VEB (switching) element
4003 **/
4004int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
4005{
4006 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
4007 struct i40e_pf *pf = veb->pf;
4008 int ret = 0;
4009 int i;
4010
4011 /* No TCs or already enabled TCs just return */
4012 if (!enabled_tc || veb->enabled_tc == enabled_tc)
4013 return ret;
4014
4015 bw_data.tc_valid_bits = enabled_tc;
4016 /* bw_data.absolute_credits is not set (relative) */
4017
4018 /* Enable ETS TCs with equal BW Share for now */
4019 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4020 if (enabled_tc & (1 << i))
4021 bw_data.tc_bw_share_credits[i] = 1;
4022 }
4023
4024 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
4025 &bw_data, NULL);
4026 if (ret) {
4027 dev_info(&pf->pdev->dev,
4028 "veb bw config failed, aq_err=%d\n",
4029 pf->hw.aq.asq_last_status);
4030 goto out;
4031 }
4032
4033 /* Update the BW information */
4034 ret = i40e_veb_get_bw_info(veb);
4035 if (ret) {
4036 dev_info(&pf->pdev->dev,
4037 "Failed getting veb bw config, aq_err=%d\n",
4038 pf->hw.aq.asq_last_status);
4039 }
4040
4041out:
4042 return ret;
4043}
4044
4045#ifdef CONFIG_I40E_DCB
4046/**
4047 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
4048 * @pf: PF struct
4049 *
4050 * Reconfigure VEB/VSIs on a given PF; it is assumed that
4051 * the caller would've quiesce all the VSIs before calling
4052 * this function
4053 **/
4054static void i40e_dcb_reconfigure(struct i40e_pf *pf)
4055{
4056 u8 tc_map = 0;
4057 int ret;
4058 u8 v;
4059
4060 /* Enable the TCs available on PF to all VEBs */
4061 tc_map = i40e_pf_get_tc_map(pf);
4062 for (v = 0; v < I40E_MAX_VEB; v++) {
4063 if (!pf->veb[v])
4064 continue;
4065 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
4066 if (ret) {
4067 dev_info(&pf->pdev->dev,
4068 "Failed configuring TC for VEB seid=%d\n",
4069 pf->veb[v]->seid);
4070 /* Will try to configure as many components */
4071 }
4072 }
4073
4074 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00004075 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004076 if (!pf->vsi[v])
4077 continue;
4078
4079 /* - Enable all TCs for the LAN VSI
4080 * - For all others keep them at TC0 for now
4081 */
4082 if (v == pf->lan_vsi)
4083 tc_map = i40e_pf_get_tc_map(pf);
4084 else
4085 tc_map = i40e_pf_get_default_tc(pf);
4086
4087 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
4088 if (ret) {
4089 dev_info(&pf->pdev->dev,
4090 "Failed configuring TC for VSI seid=%d\n",
4091 pf->vsi[v]->seid);
4092 /* Will try to configure as many components */
4093 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00004094 /* Re-configure VSI vectors based on updated TC map */
4095 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004096 if (pf->vsi[v]->netdev)
4097 i40e_dcbnl_set_all(pf->vsi[v]);
4098 }
4099 }
4100}
4101
4102/**
4103 * i40e_init_pf_dcb - Initialize DCB configuration
4104 * @pf: PF being configured
4105 *
4106 * Query the current DCB configuration and cache it
4107 * in the hardware structure
4108 **/
4109static int i40e_init_pf_dcb(struct i40e_pf *pf)
4110{
4111 struct i40e_hw *hw = &pf->hw;
4112 int err = 0;
4113
4114 if (pf->hw.func_caps.npar_enable)
4115 goto out;
4116
4117 /* Get the initial DCB configuration */
4118 err = i40e_init_dcb(hw);
4119 if (!err) {
4120 /* Device/Function is not DCBX capable */
4121 if ((!hw->func_caps.dcb) ||
4122 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
4123 dev_info(&pf->pdev->dev,
4124 "DCBX offload is not supported or is disabled for this PF.\n");
4125
4126 if (pf->flags & I40E_FLAG_MFP_ENABLED)
4127 goto out;
4128
4129 } else {
4130 /* When status is not DISABLED then DCBX in FW */
4131 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
4132 DCB_CAP_DCBX_VER_IEEE;
4133 pf->flags |= I40E_FLAG_DCB_ENABLED;
4134 }
Neerav Parikh014269f2014-04-01 07:11:48 +00004135 } else {
4136 dev_info(&pf->pdev->dev, "AQ Querying DCB configuration failed: %d\n",
4137 pf->hw.aq.asq_last_status);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004138 }
4139
4140out:
4141 return err;
4142}
4143#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004144#define SPEED_SIZE 14
4145#define FC_SIZE 8
4146/**
4147 * i40e_print_link_message - print link up or down
4148 * @vsi: the VSI for which link needs a message
4149 */
4150static void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
4151{
4152 char speed[SPEED_SIZE] = "Unknown";
4153 char fc[FC_SIZE] = "RX/TX";
4154
4155 if (!isup) {
4156 netdev_info(vsi->netdev, "NIC Link is Down\n");
4157 return;
4158 }
4159
4160 switch (vsi->back->hw.phy.link_info.link_speed) {
4161 case I40E_LINK_SPEED_40GB:
4162 strncpy(speed, "40 Gbps", SPEED_SIZE);
4163 break;
4164 case I40E_LINK_SPEED_10GB:
4165 strncpy(speed, "10 Gbps", SPEED_SIZE);
4166 break;
4167 case I40E_LINK_SPEED_1GB:
4168 strncpy(speed, "1000 Mbps", SPEED_SIZE);
4169 break;
4170 default:
4171 break;
4172 }
4173
4174 switch (vsi->back->hw.fc.current_mode) {
4175 case I40E_FC_FULL:
4176 strncpy(fc, "RX/TX", FC_SIZE);
4177 break;
4178 case I40E_FC_TX_PAUSE:
4179 strncpy(fc, "TX", FC_SIZE);
4180 break;
4181 case I40E_FC_RX_PAUSE:
4182 strncpy(fc, "RX", FC_SIZE);
4183 break;
4184 default:
4185 strncpy(fc, "None", FC_SIZE);
4186 break;
4187 }
4188
4189 netdev_info(vsi->netdev, "NIC Link is Up %s Full Duplex, Flow Control: %s\n",
4190 speed, fc);
4191}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004192
4193/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004194 * i40e_up_complete - Finish the last steps of bringing up a connection
4195 * @vsi: the VSI being configured
4196 **/
4197static int i40e_up_complete(struct i40e_vsi *vsi)
4198{
4199 struct i40e_pf *pf = vsi->back;
4200 int err;
4201
4202 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4203 i40e_vsi_configure_msix(vsi);
4204 else
4205 i40e_configure_msi_and_legacy(vsi);
4206
4207 /* start rings */
4208 err = i40e_vsi_control_rings(vsi, true);
4209 if (err)
4210 return err;
4211
4212 clear_bit(__I40E_DOWN, &vsi->state);
4213 i40e_napi_enable_all(vsi);
4214 i40e_vsi_enable_irq(vsi);
4215
4216 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
4217 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004218 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004219 netif_tx_start_all_queues(vsi->netdev);
4220 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00004221 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004222 i40e_print_link_message(vsi, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004223 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00004224
4225 /* replay FDIR SB filters */
4226 if (vsi->type == I40E_VSI_FDIR)
4227 i40e_fdir_filter_restore(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004228 i40e_service_event_schedule(pf);
4229
4230 return 0;
4231}
4232
4233/**
4234 * i40e_vsi_reinit_locked - Reset the VSI
4235 * @vsi: the VSI being configured
4236 *
4237 * Rebuild the ring structs after some configuration
4238 * has changed, e.g. MTU size.
4239 **/
4240static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
4241{
4242 struct i40e_pf *pf = vsi->back;
4243
4244 WARN_ON(in_interrupt());
4245 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
4246 usleep_range(1000, 2000);
4247 i40e_down(vsi);
4248
4249 /* Give a VF some time to respond to the reset. The
4250 * two second wait is based upon the watchdog cycle in
4251 * the VF driver.
4252 */
4253 if (vsi->type == I40E_VSI_SRIOV)
4254 msleep(2000);
4255 i40e_up(vsi);
4256 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
4257}
4258
4259/**
4260 * i40e_up - Bring the connection back up after being down
4261 * @vsi: the VSI being configured
4262 **/
4263int i40e_up(struct i40e_vsi *vsi)
4264{
4265 int err;
4266
4267 err = i40e_vsi_configure(vsi);
4268 if (!err)
4269 err = i40e_up_complete(vsi);
4270
4271 return err;
4272}
4273
4274/**
4275 * i40e_down - Shutdown the connection processing
4276 * @vsi: the VSI being stopped
4277 **/
4278void i40e_down(struct i40e_vsi *vsi)
4279{
4280 int i;
4281
4282 /* It is assumed that the caller of this function
4283 * sets the vsi->state __I40E_DOWN bit.
4284 */
4285 if (vsi->netdev) {
4286 netif_carrier_off(vsi->netdev);
4287 netif_tx_disable(vsi->netdev);
4288 }
4289 i40e_vsi_disable_irq(vsi);
4290 i40e_vsi_control_rings(vsi, false);
4291 i40e_napi_disable_all(vsi);
4292
4293 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004294 i40e_clean_tx_ring(vsi->tx_rings[i]);
4295 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004296 }
4297}
4298
4299/**
4300 * i40e_setup_tc - configure multiple traffic classes
4301 * @netdev: net device to configure
4302 * @tc: number of traffic classes to enable
4303 **/
4304static int i40e_setup_tc(struct net_device *netdev, u8 tc)
4305{
4306 struct i40e_netdev_priv *np = netdev_priv(netdev);
4307 struct i40e_vsi *vsi = np->vsi;
4308 struct i40e_pf *pf = vsi->back;
4309 u8 enabled_tc = 0;
4310 int ret = -EINVAL;
4311 int i;
4312
4313 /* Check if DCB enabled to continue */
4314 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
4315 netdev_info(netdev, "DCB is not enabled for adapter\n");
4316 goto exit;
4317 }
4318
4319 /* Check if MFP enabled */
4320 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
4321 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
4322 goto exit;
4323 }
4324
4325 /* Check whether tc count is within enabled limit */
4326 if (tc > i40e_pf_get_num_tc(pf)) {
4327 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
4328 goto exit;
4329 }
4330
4331 /* Generate TC map for number of tc requested */
4332 for (i = 0; i < tc; i++)
4333 enabled_tc |= (1 << i);
4334
4335 /* Requesting same TC configuration as already enabled */
4336 if (enabled_tc == vsi->tc_config.enabled_tc)
4337 return 0;
4338
4339 /* Quiesce VSI queues */
4340 i40e_quiesce_vsi(vsi);
4341
4342 /* Configure VSI for enabled TCs */
4343 ret = i40e_vsi_config_tc(vsi, enabled_tc);
4344 if (ret) {
4345 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
4346 vsi->seid);
4347 goto exit;
4348 }
4349
4350 /* Unquiesce VSI */
4351 i40e_unquiesce_vsi(vsi);
4352
4353exit:
4354 return ret;
4355}
4356
4357/**
4358 * i40e_open - Called when a network interface is made active
4359 * @netdev: network interface device structure
4360 *
4361 * The open entry point is called when a network interface is made
4362 * active by the system (IFF_UP). At this point all resources needed
4363 * for transmit and receive operations are allocated, the interrupt
4364 * handler is registered with the OS, the netdev watchdog subtask is
4365 * enabled, and the stack is notified that the interface is ready.
4366 *
4367 * Returns 0 on success, negative value on failure
4368 **/
4369static int i40e_open(struct net_device *netdev)
4370{
4371 struct i40e_netdev_priv *np = netdev_priv(netdev);
4372 struct i40e_vsi *vsi = np->vsi;
4373 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004374 int err;
4375
Shannon Nelson4eb3f762014-03-06 08:59:58 +00004376 /* disallow open during test or if eeprom is broken */
4377 if (test_bit(__I40E_TESTING, &pf->state) ||
4378 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004379 return -EBUSY;
4380
4381 netif_carrier_off(netdev);
4382
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004383 err = i40e_vsi_open(vsi);
4384 if (err)
4385 return err;
4386
Jesse Brandeburg059dab62014-04-01 09:07:20 +00004387 /* configure global TSO hardware offload settings */
4388 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
4389 TCP_FLAG_FIN) >> 16);
4390 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
4391 TCP_FLAG_FIN |
4392 TCP_FLAG_CWR) >> 16);
4393 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
4394
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004395#ifdef CONFIG_I40E_VXLAN
4396 vxlan_get_rx_port(netdev);
4397#endif
4398
4399 return 0;
4400}
4401
4402/**
4403 * i40e_vsi_open -
4404 * @vsi: the VSI to open
4405 *
4406 * Finish initialization of the VSI.
4407 *
4408 * Returns 0 on success, negative value on failure
4409 **/
4410int i40e_vsi_open(struct i40e_vsi *vsi)
4411{
4412 struct i40e_pf *pf = vsi->back;
4413 char int_name[IFNAMSIZ];
4414 int err;
4415
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004416 /* allocate descriptors */
4417 err = i40e_vsi_setup_tx_resources(vsi);
4418 if (err)
4419 goto err_setup_tx;
4420 err = i40e_vsi_setup_rx_resources(vsi);
4421 if (err)
4422 goto err_setup_rx;
4423
4424 err = i40e_vsi_configure(vsi);
4425 if (err)
4426 goto err_setup_rx;
4427
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00004428 if (vsi->netdev) {
4429 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
4430 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
4431 err = i40e_vsi_request_irq(vsi, int_name);
4432 if (err)
4433 goto err_setup_rx;
4434
4435 /* Notify the stack of the actual queue counts. */
4436 err = netif_set_real_num_tx_queues(vsi->netdev,
4437 vsi->num_queue_pairs);
4438 if (err)
4439 goto err_set_queues;
4440
4441 err = netif_set_real_num_rx_queues(vsi->netdev,
4442 vsi->num_queue_pairs);
4443 if (err)
4444 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00004445
4446 } else if (vsi->type == I40E_VSI_FDIR) {
4447 snprintf(int_name, sizeof(int_name) - 1, "%s-fdir",
4448 dev_driver_string(&pf->pdev->dev));
4449 err = i40e_vsi_request_irq(vsi, int_name);
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00004450 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00004451 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004452 goto err_setup_rx;
4453 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004454
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004455 err = i40e_up_complete(vsi);
4456 if (err)
4457 goto err_up_complete;
4458
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004459 return 0;
4460
4461err_up_complete:
4462 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004463err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004464 i40e_vsi_free_irq(vsi);
4465err_setup_rx:
4466 i40e_vsi_free_rx_resources(vsi);
4467err_setup_tx:
4468 i40e_vsi_free_tx_resources(vsi);
4469 if (vsi == pf->vsi[pf->lan_vsi])
4470 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
4471
4472 return err;
4473}
4474
4475/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00004476 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
4477 * @pf: Pointer to pf
4478 *
4479 * This function destroys the hlist where all the Flow Director
4480 * filters were saved.
4481 **/
4482static void i40e_fdir_filter_exit(struct i40e_pf *pf)
4483{
4484 struct i40e_fdir_filter *filter;
4485 struct hlist_node *node2;
4486
4487 hlist_for_each_entry_safe(filter, node2,
4488 &pf->fdir_filter_list, fdir_node) {
4489 hlist_del(&filter->fdir_node);
4490 kfree(filter);
4491 }
4492 pf->fdir_pf_active_filters = 0;
4493}
4494
4495/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004496 * i40e_close - Disables a network interface
4497 * @netdev: network interface device structure
4498 *
4499 * The close entry point is called when an interface is de-activated
4500 * by the OS. The hardware is still under the driver's control, but
4501 * this netdev interface is disabled.
4502 *
4503 * Returns 0, this is not allowed to fail
4504 **/
4505static int i40e_close(struct net_device *netdev)
4506{
4507 struct i40e_netdev_priv *np = netdev_priv(netdev);
4508 struct i40e_vsi *vsi = np->vsi;
4509
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004510 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004511
4512 return 0;
4513}
4514
4515/**
4516 * i40e_do_reset - Start a PF or Core Reset sequence
4517 * @pf: board private structure
4518 * @reset_flags: which reset is requested
4519 *
4520 * The essential difference in resets is that the PF Reset
4521 * doesn't clear the packet buffers, doesn't reset the PE
4522 * firmware, and doesn't bother the other PFs on the chip.
4523 **/
4524void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
4525{
4526 u32 val;
4527
4528 WARN_ON(in_interrupt());
4529
Mitch Williams263fc482014-04-23 04:50:11 +00004530 if (i40e_check_asq_alive(&pf->hw))
4531 i40e_vc_notify_reset(pf);
4532
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004533 /* do the biggest reset indicated */
4534 if (reset_flags & (1 << __I40E_GLOBAL_RESET_REQUESTED)) {
4535
4536 /* Request a Global Reset
4537 *
4538 * This will start the chip's countdown to the actual full
4539 * chip reset event, and a warning interrupt to be sent
4540 * to all PFs, including the requestor. Our handler
4541 * for the warning interrupt will deal with the shutdown
4542 * and recovery of the switch setup.
4543 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004544 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004545 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4546 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
4547 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4548
4549 } else if (reset_flags & (1 << __I40E_CORE_RESET_REQUESTED)) {
4550
4551 /* Request a Core Reset
4552 *
4553 * Same as Global Reset, except does *not* include the MAC/PHY
4554 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004555 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004556 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4557 val |= I40E_GLGEN_RTRIG_CORER_MASK;
4558 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4559 i40e_flush(&pf->hw);
4560
Shannon Nelson7823fe32013-11-16 10:00:45 +00004561 } else if (reset_flags & (1 << __I40E_EMP_RESET_REQUESTED)) {
4562
4563 /* Request a Firmware Reset
4564 *
4565 * Same as Global reset, plus restarting the
4566 * embedded firmware engine.
4567 */
4568 /* enable EMP Reset */
4569 val = rd32(&pf->hw, I40E_GLGEN_RSTENA_EMP);
4570 val |= I40E_GLGEN_RSTENA_EMP_EMP_RST_ENA_MASK;
4571 wr32(&pf->hw, I40E_GLGEN_RSTENA_EMP, val);
4572
4573 /* force the reset */
4574 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4575 val |= I40E_GLGEN_RTRIG_EMPFWR_MASK;
4576 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4577 i40e_flush(&pf->hw);
4578
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004579 } else if (reset_flags & (1 << __I40E_PF_RESET_REQUESTED)) {
4580
4581 /* Request a PF Reset
4582 *
4583 * Resets only the PF-specific registers
4584 *
4585 * This goes directly to the tear-down and rebuild of
4586 * the switch, since we need to do all the recovery as
4587 * for the Core Reset.
4588 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004589 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004590 i40e_handle_reset_warning(pf);
4591
4592 } else if (reset_flags & (1 << __I40E_REINIT_REQUESTED)) {
4593 int v;
4594
4595 /* Find the VSI(s) that requested a re-init */
4596 dev_info(&pf->pdev->dev,
4597 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00004598 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004599 struct i40e_vsi *vsi = pf->vsi[v];
4600 if (vsi != NULL &&
4601 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
4602 i40e_vsi_reinit_locked(pf->vsi[v]);
4603 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
4604 }
4605 }
4606
4607 /* no further action needed, so return now */
4608 return;
4609 } else {
4610 dev_info(&pf->pdev->dev,
4611 "bad reset request 0x%08x\n", reset_flags);
4612 return;
4613 }
4614}
4615
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004616#ifdef CONFIG_I40E_DCB
4617/**
4618 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
4619 * @pf: board private structure
4620 * @old_cfg: current DCB config
4621 * @new_cfg: new DCB config
4622 **/
4623bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
4624 struct i40e_dcbx_config *old_cfg,
4625 struct i40e_dcbx_config *new_cfg)
4626{
4627 bool need_reconfig = false;
4628
4629 /* Check if ETS configuration has changed */
4630 if (memcmp(&new_cfg->etscfg,
4631 &old_cfg->etscfg,
4632 sizeof(new_cfg->etscfg))) {
4633 /* If Priority Table has changed reconfig is needed */
4634 if (memcmp(&new_cfg->etscfg.prioritytable,
4635 &old_cfg->etscfg.prioritytable,
4636 sizeof(new_cfg->etscfg.prioritytable))) {
4637 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004638 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004639 }
4640
4641 if (memcmp(&new_cfg->etscfg.tcbwtable,
4642 &old_cfg->etscfg.tcbwtable,
4643 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004644 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004645
4646 if (memcmp(&new_cfg->etscfg.tsatable,
4647 &old_cfg->etscfg.tsatable,
4648 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004649 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004650 }
4651
4652 /* Check if PFC configuration has changed */
4653 if (memcmp(&new_cfg->pfc,
4654 &old_cfg->pfc,
4655 sizeof(new_cfg->pfc))) {
4656 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004657 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004658 }
4659
4660 /* Check if APP Table has changed */
4661 if (memcmp(&new_cfg->app,
4662 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05004663 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004664 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004665 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05004666 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004667
4668 return need_reconfig;
4669}
4670
4671/**
4672 * i40e_handle_lldp_event - Handle LLDP Change MIB event
4673 * @pf: board private structure
4674 * @e: event info posted on ARQ
4675 **/
4676static int i40e_handle_lldp_event(struct i40e_pf *pf,
4677 struct i40e_arq_event_info *e)
4678{
4679 struct i40e_aqc_lldp_get_mib *mib =
4680 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
4681 struct i40e_hw *hw = &pf->hw;
4682 struct i40e_dcbx_config *dcbx_cfg = &hw->local_dcbx_config;
4683 struct i40e_dcbx_config tmp_dcbx_cfg;
4684 bool need_reconfig = false;
4685 int ret = 0;
4686 u8 type;
4687
4688 /* Ignore if event is not for Nearest Bridge */
4689 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
4690 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
4691 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
4692 return ret;
4693
4694 /* Check MIB Type and return if event for Remote MIB update */
4695 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
4696 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
4697 /* Update the remote cached instance and return */
4698 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
4699 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
4700 &hw->remote_dcbx_config);
4701 goto exit;
4702 }
4703
4704 /* Convert/store the DCBX data from LLDPDU temporarily */
4705 memset(&tmp_dcbx_cfg, 0, sizeof(tmp_dcbx_cfg));
4706 ret = i40e_lldp_to_dcb_config(e->msg_buf, &tmp_dcbx_cfg);
4707 if (ret) {
4708 /* Error in LLDPDU parsing return */
4709 dev_info(&pf->pdev->dev, "Failed parsing LLDPDU from event buffer\n");
4710 goto exit;
4711 }
4712
4713 /* No change detected in DCBX configs */
4714 if (!memcmp(&tmp_dcbx_cfg, dcbx_cfg, sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004715 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004716 goto exit;
4717 }
4718
4719 need_reconfig = i40e_dcb_need_reconfig(pf, dcbx_cfg, &tmp_dcbx_cfg);
4720
4721 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg);
4722
4723 /* Overwrite the new configuration */
4724 *dcbx_cfg = tmp_dcbx_cfg;
4725
4726 if (!need_reconfig)
4727 goto exit;
4728
4729 /* Reconfiguration needed quiesce all VSIs */
4730 i40e_pf_quiesce_all_vsi(pf);
4731
4732 /* Changes in configuration update VEB/VSI */
4733 i40e_dcb_reconfigure(pf);
4734
4735 i40e_pf_unquiesce_all_vsi(pf);
4736exit:
4737 return ret;
4738}
4739#endif /* CONFIG_I40E_DCB */
4740
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004741/**
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004742 * i40e_do_reset_safe - Protected reset path for userland calls.
4743 * @pf: board private structure
4744 * @reset_flags: which reset is requested
4745 *
4746 **/
4747void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
4748{
4749 rtnl_lock();
4750 i40e_do_reset(pf, reset_flags);
4751 rtnl_unlock();
4752}
4753
4754/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004755 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
4756 * @pf: board private structure
4757 * @e: event info posted on ARQ
4758 *
4759 * Handler for LAN Queue Overflow Event generated by the firmware for PF
4760 * and VF queues
4761 **/
4762static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
4763 struct i40e_arq_event_info *e)
4764{
4765 struct i40e_aqc_lan_overflow *data =
4766 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
4767 u32 queue = le32_to_cpu(data->prtdcb_rupto);
4768 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
4769 struct i40e_hw *hw = &pf->hw;
4770 struct i40e_vf *vf;
4771 u16 vf_id;
4772
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004773 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
4774 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004775
4776 /* Queue belongs to VF, find the VF and issue VF reset */
4777 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
4778 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
4779 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
4780 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
4781 vf_id -= hw->func_caps.vf_base_id;
4782 vf = &pf->vf[vf_id];
4783 i40e_vc_notify_vf_reset(vf);
4784 /* Allow VF to process pending reset notification */
4785 msleep(20);
4786 i40e_reset_vf(vf, false);
4787 }
4788}
4789
4790/**
4791 * i40e_service_event_complete - Finish up the service event
4792 * @pf: board private structure
4793 **/
4794static void i40e_service_event_complete(struct i40e_pf *pf)
4795{
4796 BUG_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
4797
4798 /* flush memory to make sure state is correct before next watchog */
4799 smp_mb__before_clear_bit();
4800 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
4801}
4802
4803/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004804 * i40e_get_current_fd_count - Get the count of FD filters programmed in the HW
4805 * @pf: board private structure
4806 **/
4807int i40e_get_current_fd_count(struct i40e_pf *pf)
4808{
4809 int val, fcnt_prog;
4810 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
4811 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
4812 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
4813 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
4814 return fcnt_prog;
4815}
4816
4817/**
4818 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
4819 * @pf: board private structure
4820 **/
4821void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
4822{
4823 u32 fcnt_prog, fcnt_avail;
4824
4825 /* Check if, FD SB or ATR was auto disabled and if there is enough room
4826 * to re-enable
4827 */
4828 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4829 (pf->flags & I40E_FLAG_FD_SB_ENABLED))
4830 return;
4831 fcnt_prog = i40e_get_current_fd_count(pf);
Anjali Singhai Jain89132782014-04-09 05:59:01 +00004832 fcnt_avail = i40e_get_fd_cnt_all(pf);
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004833 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) {
4834 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
4835 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
4836 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
4837 dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
4838 }
4839 }
4840 /* Wait for some more space to be available to turn on ATR */
4841 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
4842 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4843 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
4844 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
4845 dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table now\n");
4846 }
4847 }
4848}
4849
4850/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004851 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
4852 * @pf: board private structure
4853 **/
4854static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
4855{
4856 if (!(pf->flags & I40E_FLAG_FDIR_REQUIRES_REINIT))
4857 return;
4858
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004859 /* if interface is down do nothing */
4860 if (test_bit(__I40E_DOWN, &pf->state))
4861 return;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004862 i40e_fdir_check_and_reenable(pf);
4863
4864 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4865 (pf->flags & I40E_FLAG_FD_SB_ENABLED))
4866 pf->flags &= ~I40E_FLAG_FDIR_REQUIRES_REINIT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004867}
4868
4869/**
4870 * i40e_vsi_link_event - notify VSI of a link event
4871 * @vsi: vsi to be notified
4872 * @link_up: link up or down
4873 **/
4874static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
4875{
4876 if (!vsi)
4877 return;
4878
4879 switch (vsi->type) {
4880 case I40E_VSI_MAIN:
4881 if (!vsi->netdev || !vsi->netdev_registered)
4882 break;
4883
4884 if (link_up) {
4885 netif_carrier_on(vsi->netdev);
4886 netif_tx_wake_all_queues(vsi->netdev);
4887 } else {
4888 netif_carrier_off(vsi->netdev);
4889 netif_tx_stop_all_queues(vsi->netdev);
4890 }
4891 break;
4892
4893 case I40E_VSI_SRIOV:
4894 break;
4895
4896 case I40E_VSI_VMDQ2:
4897 case I40E_VSI_CTRL:
4898 case I40E_VSI_MIRROR:
4899 default:
4900 /* there is no notification for other VSIs */
4901 break;
4902 }
4903}
4904
4905/**
4906 * i40e_veb_link_event - notify elements on the veb of a link event
4907 * @veb: veb to be notified
4908 * @link_up: link up or down
4909 **/
4910static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
4911{
4912 struct i40e_pf *pf;
4913 int i;
4914
4915 if (!veb || !veb->pf)
4916 return;
4917 pf = veb->pf;
4918
4919 /* depth first... */
4920 for (i = 0; i < I40E_MAX_VEB; i++)
4921 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
4922 i40e_veb_link_event(pf->veb[i], link_up);
4923
4924 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00004925 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004926 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
4927 i40e_vsi_link_event(pf->vsi[i], link_up);
4928}
4929
4930/**
4931 * i40e_link_event - Update netif_carrier status
4932 * @pf: board private structure
4933 **/
4934static void i40e_link_event(struct i40e_pf *pf)
4935{
4936 bool new_link, old_link;
4937
4938 new_link = (pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP);
4939 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
4940
4941 if (new_link == old_link)
4942 return;
Anjali Singhai6d779b42013-09-28 06:00:02 +00004943 if (!test_bit(__I40E_DOWN, &pf->vsi[pf->lan_vsi]->state))
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004944 i40e_print_link_message(pf->vsi[pf->lan_vsi], new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004945
4946 /* Notify the base of the switch tree connected to
4947 * the link. Floating VEBs are not notified.
4948 */
4949 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
4950 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
4951 else
4952 i40e_vsi_link_event(pf->vsi[pf->lan_vsi], new_link);
4953
4954 if (pf->vf)
4955 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00004956
4957 if (pf->flags & I40E_FLAG_PTP)
4958 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004959}
4960
4961/**
4962 * i40e_check_hang_subtask - Check for hung queues and dropped interrupts
4963 * @pf: board private structure
4964 *
4965 * Set the per-queue flags to request a check for stuck queues in the irq
4966 * clean functions, then force interrupts to be sure the irq clean is called.
4967 **/
4968static void i40e_check_hang_subtask(struct i40e_pf *pf)
4969{
4970 int i, v;
4971
4972 /* If we're down or resetting, just bail */
4973 if (test_bit(__I40E_CONFIG_BUSY, &pf->state))
4974 return;
4975
4976 /* for each VSI/netdev
4977 * for each Tx queue
4978 * set the check flag
4979 * for each q_vector
4980 * force an interrupt
4981 */
Mitch Williams505682c2014-05-20 08:01:37 +00004982 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004983 struct i40e_vsi *vsi = pf->vsi[v];
4984 int armed = 0;
4985
4986 if (!pf->vsi[v] ||
4987 test_bit(__I40E_DOWN, &vsi->state) ||
4988 (vsi->netdev && !netif_carrier_ok(vsi->netdev)))
4989 continue;
4990
4991 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004992 set_check_for_tx_hang(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004993 if (test_bit(__I40E_HANG_CHECK_ARMED,
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004994 &vsi->tx_rings[i]->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004995 armed++;
4996 }
4997
4998 if (armed) {
4999 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
5000 wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0,
5001 (I40E_PFINT_DYN_CTL0_INTENA_MASK |
5002 I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK));
5003 } else {
5004 u16 vec = vsi->base_vector - 1;
5005 u32 val = (I40E_PFINT_DYN_CTLN_INTENA_MASK |
5006 I40E_PFINT_DYN_CTLN_SWINT_TRIG_MASK);
5007 for (i = 0; i < vsi->num_q_vectors; i++, vec++)
5008 wr32(&vsi->back->hw,
5009 I40E_PFINT_DYN_CTLN(vec), val);
5010 }
5011 i40e_flush(&vsi->back->hw);
5012 }
5013 }
5014}
5015
5016/**
5017 * i40e_watchdog_subtask - Check and bring link up
5018 * @pf: board private structure
5019 **/
5020static void i40e_watchdog_subtask(struct i40e_pf *pf)
5021{
5022 int i;
5023
5024 /* if interface is down do nothing */
5025 if (test_bit(__I40E_DOWN, &pf->state) ||
5026 test_bit(__I40E_CONFIG_BUSY, &pf->state))
5027 return;
5028
5029 /* Update the stats for active netdevs so the network stack
5030 * can look at updated numbers whenever it cares to
5031 */
Mitch Williams505682c2014-05-20 08:01:37 +00005032 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005033 if (pf->vsi[i] && pf->vsi[i]->netdev)
5034 i40e_update_stats(pf->vsi[i]);
5035
5036 /* Update the stats for the active switching components */
5037 for (i = 0; i < I40E_MAX_VEB; i++)
5038 if (pf->veb[i])
5039 i40e_update_veb_stats(pf->veb[i]);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00005040
5041 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005042}
5043
5044/**
5045 * i40e_reset_subtask - Set up for resetting the device and driver
5046 * @pf: board private structure
5047 **/
5048static void i40e_reset_subtask(struct i40e_pf *pf)
5049{
5050 u32 reset_flags = 0;
5051
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005052 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005053 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
5054 reset_flags |= (1 << __I40E_REINIT_REQUESTED);
5055 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
5056 }
5057 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
5058 reset_flags |= (1 << __I40E_PF_RESET_REQUESTED);
5059 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5060 }
5061 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
5062 reset_flags |= (1 << __I40E_CORE_RESET_REQUESTED);
5063 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
5064 }
5065 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
5066 reset_flags |= (1 << __I40E_GLOBAL_RESET_REQUESTED);
5067 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
5068 }
5069
5070 /* If there's a recovery already waiting, it takes
5071 * precedence before starting a new reset sequence.
5072 */
5073 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
5074 i40e_handle_reset_warning(pf);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005075 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005076 }
5077
5078 /* If we're already down or resetting, just bail */
5079 if (reset_flags &&
5080 !test_bit(__I40E_DOWN, &pf->state) &&
5081 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
5082 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005083
5084unlock:
5085 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005086}
5087
5088/**
5089 * i40e_handle_link_event - Handle link event
5090 * @pf: board private structure
5091 * @e: event info posted on ARQ
5092 **/
5093static void i40e_handle_link_event(struct i40e_pf *pf,
5094 struct i40e_arq_event_info *e)
5095{
5096 struct i40e_hw *hw = &pf->hw;
5097 struct i40e_aqc_get_link_status *status =
5098 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
5099 struct i40e_link_status *hw_link_info = &hw->phy.link_info;
5100
5101 /* save off old link status information */
5102 memcpy(&pf->hw.phy.link_info_old, hw_link_info,
5103 sizeof(pf->hw.phy.link_info_old));
5104
5105 /* update link status */
5106 hw_link_info->phy_type = (enum i40e_aq_phy_type)status->phy_type;
5107 hw_link_info->link_speed = (enum i40e_aq_link_speed)status->link_speed;
5108 hw_link_info->link_info = status->link_info;
5109 hw_link_info->an_info = status->an_info;
5110 hw_link_info->ext_info = status->ext_info;
5111 hw_link_info->lse_enable =
5112 le16_to_cpu(status->command_flags) &
5113 I40E_AQ_LSE_ENABLE;
5114
5115 /* process the event */
5116 i40e_link_event(pf);
5117
5118 /* Do a new status request to re-enable LSE reporting
5119 * and load new status information into the hw struct,
5120 * then see if the status changed while processing the
5121 * initial event.
5122 */
5123 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
5124 i40e_link_event(pf);
5125}
5126
5127/**
5128 * i40e_clean_adminq_subtask - Clean the AdminQ rings
5129 * @pf: board private structure
5130 **/
5131static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
5132{
5133 struct i40e_arq_event_info event;
5134 struct i40e_hw *hw = &pf->hw;
5135 u16 pending, i = 0;
5136 i40e_status ret;
5137 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00005138 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005139 u32 val;
5140
5141 if (!test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state))
5142 return;
5143
Shannon Nelson86df2422014-05-20 08:01:35 +00005144 /* check for error indications */
5145 val = rd32(&pf->hw, pf->hw.aq.arq.len);
5146 oldval = val;
5147 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
5148 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
5149 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
5150 }
5151 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
5152 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
5153 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
5154 }
5155 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
5156 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
5157 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
5158 }
5159 if (oldval != val)
5160 wr32(&pf->hw, pf->hw.aq.arq.len, val);
5161
5162 val = rd32(&pf->hw, pf->hw.aq.asq.len);
5163 oldval = val;
5164 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
5165 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
5166 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
5167 }
5168 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
5169 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
5170 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
5171 }
5172 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
5173 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
5174 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
5175 }
5176 if (oldval != val)
5177 wr32(&pf->hw, pf->hw.aq.asq.len, val);
5178
Mitch Williams3197ce22013-11-28 06:39:39 +00005179 event.msg_size = I40E_MAX_AQ_BUF_SIZE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005180 event.msg_buf = kzalloc(event.msg_size, GFP_KERNEL);
5181 if (!event.msg_buf)
5182 return;
5183
5184 do {
Mitch Williams2f019122013-11-28 06:39:33 +00005185 event.msg_size = I40E_MAX_AQ_BUF_SIZE; /* reinit each time */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005186 ret = i40e_clean_arq_element(hw, &event, &pending);
5187 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
5188 dev_info(&pf->pdev->dev, "No ARQ event found\n");
5189 break;
5190 } else if (ret) {
5191 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
5192 break;
5193 }
5194
5195 opcode = le16_to_cpu(event.desc.opcode);
5196 switch (opcode) {
5197
5198 case i40e_aqc_opc_get_link_status:
5199 i40e_handle_link_event(pf, &event);
5200 break;
5201 case i40e_aqc_opc_send_msg_to_pf:
5202 ret = i40e_vc_process_vf_msg(pf,
5203 le16_to_cpu(event.desc.retval),
5204 le32_to_cpu(event.desc.cookie_high),
5205 le32_to_cpu(event.desc.cookie_low),
5206 event.msg_buf,
5207 event.msg_size);
5208 break;
5209 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005210 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005211#ifdef CONFIG_I40E_DCB
5212 rtnl_lock();
5213 ret = i40e_handle_lldp_event(pf, &event);
5214 rtnl_unlock();
5215#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005216 break;
5217 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005218 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005219 i40e_handle_lan_overflow_event(pf, &event);
5220 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00005221 case i40e_aqc_opc_send_msg_to_peer:
5222 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
5223 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005224 default:
5225 dev_info(&pf->pdev->dev,
Shannon Nelson0467bc92013-12-18 13:45:58 +00005226 "ARQ Error: Unknown event 0x%04x received\n",
5227 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005228 break;
5229 }
5230 } while (pending && (i++ < pf->adminq_work_limit));
5231
5232 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
5233 /* re-enable Admin queue interrupt cause */
5234 val = rd32(hw, I40E_PFINT_ICR0_ENA);
5235 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
5236 wr32(hw, I40E_PFINT_ICR0_ENA, val);
5237 i40e_flush(hw);
5238
5239 kfree(event.msg_buf);
5240}
5241
5242/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005243 * i40e_verify_eeprom - make sure eeprom is good to use
5244 * @pf: board private structure
5245 **/
5246static void i40e_verify_eeprom(struct i40e_pf *pf)
5247{
5248 int err;
5249
5250 err = i40e_diag_eeprom_test(&pf->hw);
5251 if (err) {
5252 /* retry in case of garbage read */
5253 err = i40e_diag_eeprom_test(&pf->hw);
5254 if (err) {
5255 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
5256 err);
5257 set_bit(__I40E_BAD_EEPROM, &pf->state);
5258 }
5259 }
5260
5261 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
5262 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
5263 clear_bit(__I40E_BAD_EEPROM, &pf->state);
5264 }
5265}
5266
5267/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005268 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
5269 * @veb: pointer to the VEB instance
5270 *
5271 * This is a recursive function that first builds the attached VSIs then
5272 * recurses in to build the next layer of VEB. We track the connections
5273 * through our own index numbers because the seid's from the HW could
5274 * change across the reset.
5275 **/
5276static int i40e_reconstitute_veb(struct i40e_veb *veb)
5277{
5278 struct i40e_vsi *ctl_vsi = NULL;
5279 struct i40e_pf *pf = veb->pf;
5280 int v, veb_idx;
5281 int ret;
5282
5283 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00005284 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005285 if (pf->vsi[v] &&
5286 pf->vsi[v]->veb_idx == veb->idx &&
5287 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
5288 ctl_vsi = pf->vsi[v];
5289 break;
5290 }
5291 }
5292 if (!ctl_vsi) {
5293 dev_info(&pf->pdev->dev,
5294 "missing owner VSI for veb_idx %d\n", veb->idx);
5295 ret = -ENOENT;
5296 goto end_reconstitute;
5297 }
5298 if (ctl_vsi != pf->vsi[pf->lan_vsi])
5299 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
5300 ret = i40e_add_vsi(ctl_vsi);
5301 if (ret) {
5302 dev_info(&pf->pdev->dev,
5303 "rebuild of owner VSI failed: %d\n", ret);
5304 goto end_reconstitute;
5305 }
5306 i40e_vsi_reset_stats(ctl_vsi);
5307
5308 /* create the VEB in the switch and move the VSI onto the VEB */
5309 ret = i40e_add_veb(veb, ctl_vsi);
5310 if (ret)
5311 goto end_reconstitute;
5312
5313 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00005314 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005315 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
5316 continue;
5317
5318 if (pf->vsi[v]->veb_idx == veb->idx) {
5319 struct i40e_vsi *vsi = pf->vsi[v];
5320 vsi->uplink_seid = veb->seid;
5321 ret = i40e_add_vsi(vsi);
5322 if (ret) {
5323 dev_info(&pf->pdev->dev,
5324 "rebuild of vsi_idx %d failed: %d\n",
5325 v, ret);
5326 goto end_reconstitute;
5327 }
5328 i40e_vsi_reset_stats(vsi);
5329 }
5330 }
5331
5332 /* create any VEBs attached to this VEB - RECURSION */
5333 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
5334 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
5335 pf->veb[veb_idx]->uplink_seid = veb->seid;
5336 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
5337 if (ret)
5338 break;
5339 }
5340 }
5341
5342end_reconstitute:
5343 return ret;
5344}
5345
5346/**
5347 * i40e_get_capabilities - get info about the HW
5348 * @pf: the PF struct
5349 **/
5350static int i40e_get_capabilities(struct i40e_pf *pf)
5351{
5352 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
5353 u16 data_size;
5354 int buf_len;
5355 int err;
5356
5357 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
5358 do {
5359 cap_buf = kzalloc(buf_len, GFP_KERNEL);
5360 if (!cap_buf)
5361 return -ENOMEM;
5362
5363 /* this loads the data into the hw struct for us */
5364 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
5365 &data_size,
5366 i40e_aqc_opc_list_func_capabilities,
5367 NULL);
5368 /* data loaded, buffer no longer needed */
5369 kfree(cap_buf);
5370
5371 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
5372 /* retry with a larger buffer */
5373 buf_len = data_size;
5374 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
5375 dev_info(&pf->pdev->dev,
5376 "capability discovery failed: aq=%d\n",
5377 pf->hw.aq.asq_last_status);
5378 return -ENODEV;
5379 }
5380 } while (err);
5381
Anjali Singhai Jainac71b7b2014-02-06 05:51:08 +00005382 if (((pf->hw.aq.fw_maj_ver == 2) && (pf->hw.aq.fw_min_ver < 22)) ||
5383 (pf->hw.aq.fw_maj_ver < 2)) {
5384 pf->hw.func_caps.num_msix_vectors++;
5385 pf->hw.func_caps.num_msix_vectors_vf++;
5386 }
5387
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005388 if (pf->hw.debug_mask & I40E_DEBUG_USER)
5389 dev_info(&pf->pdev->dev,
5390 "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",
5391 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
5392 pf->hw.func_caps.num_msix_vectors,
5393 pf->hw.func_caps.num_msix_vectors_vf,
5394 pf->hw.func_caps.fd_filters_guaranteed,
5395 pf->hw.func_caps.fd_filters_best_effort,
5396 pf->hw.func_caps.num_tx_qp,
5397 pf->hw.func_caps.num_vsis);
5398
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00005399#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
5400 + pf->hw.func_caps.num_vfs)
5401 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
5402 dev_info(&pf->pdev->dev,
5403 "got num_vsis %d, setting num_vsis to %d\n",
5404 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
5405 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
5406 }
5407
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005408 return 0;
5409}
5410
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005411static int i40e_vsi_clear(struct i40e_vsi *vsi);
5412
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005413/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005414 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005415 * @pf: board private structure
5416 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005417static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005418{
5419 struct i40e_vsi *vsi;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005420 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005421
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005422 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005423 return;
5424
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005425 /* find existing VSI and see if it needs configuring */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005426 vsi = NULL;
Mitch Williams505682c2014-05-20 08:01:37 +00005427 for (i = 0; i < pf->num_alloc_vsi; i++) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005428 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005429 vsi = pf->vsi[i];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005430 break;
5431 }
5432 }
5433
5434 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005435 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005436 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
5437 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005438 if (!vsi) {
5439 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005440 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
5441 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005442 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005443 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005444
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005445 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005446}
5447
5448/**
5449 * i40e_fdir_teardown - release the Flow Director resources
5450 * @pf: board private structure
5451 **/
5452static void i40e_fdir_teardown(struct i40e_pf *pf)
5453{
5454 int i;
5455
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005456 i40e_fdir_filter_exit(pf);
Mitch Williams505682c2014-05-20 08:01:37 +00005457 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005458 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
5459 i40e_vsi_release(pf->vsi[i]);
5460 break;
5461 }
5462 }
5463}
5464
5465/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005466 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005467 * @pf: board private structure
5468 *
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005469 * Close up the VFs and other things in prep for pf Reset.
5470 **/
5471static int i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005472{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005473 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00005474 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005475 u32 v;
5476
5477 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
5478 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005479 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005480
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005481 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005482
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005483 /* quiesce the VSIs and their queues that are not already DOWN */
5484 i40e_pf_quiesce_all_vsi(pf);
5485
Mitch Williams505682c2014-05-20 08:01:37 +00005486 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005487 if (pf->vsi[v])
5488 pf->vsi[v]->seid = 0;
5489 }
5490
5491 i40e_shutdown_adminq(&pf->hw);
5492
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005493 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00005494 if (hw->hmc.hmc_obj) {
5495 ret = i40e_shutdown_lan_hmc(hw);
5496 if (ret) {
5497 dev_warn(&pf->pdev->dev,
5498 "shutdown_lan_hmc failed: %d\n", ret);
5499 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
5500 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005501 }
5502 return ret;
5503}
5504
5505/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00005506 * i40e_send_version - update firmware with driver version
5507 * @pf: PF struct
5508 */
5509static void i40e_send_version(struct i40e_pf *pf)
5510{
5511 struct i40e_driver_version dv;
5512
5513 dv.major_version = DRV_VERSION_MAJOR;
5514 dv.minor_version = DRV_VERSION_MINOR;
5515 dv.build_version = DRV_VERSION_BUILD;
5516 dv.subbuild_version = 0;
5517 strncpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
5518 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
5519}
5520
5521/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00005522 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005523 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005524 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005525 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005526static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005527{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005528 struct i40e_hw *hw = &pf->hw;
5529 i40e_status ret;
5530 u32 v;
5531
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005532 /* Now we wait for GRST to settle out.
5533 * We don't have to delete the VEBs or VSIs from the hw switch
5534 * because the reset will make them disappear.
5535 */
5536 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00005537 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005538 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00005539 goto end_core_reset;
5540 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005541 pf->pfr_count++;
5542
5543 if (test_bit(__I40E_DOWN, &pf->state))
5544 goto end_core_reset;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005545 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005546
5547 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
5548 ret = i40e_init_adminq(&pf->hw);
5549 if (ret) {
5550 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, %d\n", ret);
5551 goto end_core_reset;
5552 }
5553
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005554 /* re-verify the eeprom if we just had an EMP reset */
5555 if (test_bit(__I40E_EMP_RESET_REQUESTED, &pf->state)) {
5556 clear_bit(__I40E_EMP_RESET_REQUESTED, &pf->state);
5557 i40e_verify_eeprom(pf);
5558 }
5559
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00005560 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005561 ret = i40e_get_capabilities(pf);
5562 if (ret) {
5563 dev_info(&pf->pdev->dev, "i40e_get_capabilities failed, %d\n",
5564 ret);
5565 goto end_core_reset;
5566 }
5567
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005568 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
5569 hw->func_caps.num_rx_qp,
5570 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
5571 if (ret) {
5572 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
5573 goto end_core_reset;
5574 }
5575 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
5576 if (ret) {
5577 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
5578 goto end_core_reset;
5579 }
5580
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005581#ifdef CONFIG_I40E_DCB
5582 ret = i40e_init_pf_dcb(pf);
5583 if (ret) {
5584 dev_info(&pf->pdev->dev, "init_pf_dcb failed: %d\n", ret);
5585 goto end_core_reset;
5586 }
5587#endif /* CONFIG_I40E_DCB */
5588
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005589 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005590 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005591 if (ret)
5592 goto end_core_reset;
5593
5594 /* Rebuild the VSIs and VEBs that existed before reset.
5595 * They are still in our local switch element arrays, so only
5596 * need to rebuild the switch model in the HW.
5597 *
5598 * If there were VEBs but the reconstitution failed, we'll try
5599 * try to recover minimal use by getting the basic PF VSI working.
5600 */
5601 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005602 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005603 /* find the one VEB connected to the MAC, and find orphans */
5604 for (v = 0; v < I40E_MAX_VEB; v++) {
5605 if (!pf->veb[v])
5606 continue;
5607
5608 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
5609 pf->veb[v]->uplink_seid == 0) {
5610 ret = i40e_reconstitute_veb(pf->veb[v]);
5611
5612 if (!ret)
5613 continue;
5614
5615 /* If Main VEB failed, we're in deep doodoo,
5616 * so give up rebuilding the switch and set up
5617 * for minimal rebuild of PF VSI.
5618 * If orphan failed, we'll report the error
5619 * but try to keep going.
5620 */
5621 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
5622 dev_info(&pf->pdev->dev,
5623 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
5624 ret);
5625 pf->vsi[pf->lan_vsi]->uplink_seid
5626 = pf->mac_seid;
5627 break;
5628 } else if (pf->veb[v]->uplink_seid == 0) {
5629 dev_info(&pf->pdev->dev,
5630 "rebuild of orphan VEB failed: %d\n",
5631 ret);
5632 }
5633 }
5634 }
5635 }
5636
5637 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
5638 dev_info(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
5639 /* no VEB, so rebuild only the Main VSI */
5640 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
5641 if (ret) {
5642 dev_info(&pf->pdev->dev,
5643 "rebuild of Main VSI failed: %d\n", ret);
5644 goto end_core_reset;
5645 }
5646 }
5647
5648 /* reinit the misc interrupt */
5649 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5650 ret = i40e_setup_misc_vector(pf);
5651
5652 /* restart the VSIs that were rebuilt and running before the reset */
5653 i40e_pf_unquiesce_all_vsi(pf);
5654
Mitch Williams69f64b22014-02-13 03:48:46 -08005655 if (pf->num_alloc_vfs) {
5656 for (v = 0; v < pf->num_alloc_vfs; v++)
5657 i40e_reset_vf(&pf->vf[v], true);
5658 }
5659
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005660 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00005661 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005662
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005663end_core_reset:
5664 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
5665}
5666
5667/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005668 * i40e_handle_reset_warning - prep for the pf to reset, reset and rebuild
5669 * @pf: board private structure
5670 *
5671 * Close up the VFs and other things in prep for a Core Reset,
5672 * then get ready to rebuild the world.
5673 **/
5674static void i40e_handle_reset_warning(struct i40e_pf *pf)
5675{
5676 i40e_status ret;
5677
5678 ret = i40e_prep_for_reset(pf);
5679 if (!ret)
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005680 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005681}
5682
5683/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005684 * i40e_handle_mdd_event
5685 * @pf: pointer to the pf structure
5686 *
5687 * Called from the MDD irq handler to identify possibly malicious vfs
5688 **/
5689static void i40e_handle_mdd_event(struct i40e_pf *pf)
5690{
5691 struct i40e_hw *hw = &pf->hw;
5692 bool mdd_detected = false;
5693 struct i40e_vf *vf;
5694 u32 reg;
5695 int i;
5696
5697 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
5698 return;
5699
5700 /* find what triggered the MDD event */
5701 reg = rd32(hw, I40E_GL_MDET_TX);
5702 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
5703 u8 func = (reg & I40E_GL_MDET_TX_FUNCTION_MASK)
5704 >> I40E_GL_MDET_TX_FUNCTION_SHIFT;
5705 u8 event = (reg & I40E_GL_MDET_TX_EVENT_SHIFT)
5706 >> I40E_GL_MDET_TX_EVENT_SHIFT;
5707 u8 queue = (reg & I40E_GL_MDET_TX_QUEUE_MASK)
5708 >> I40E_GL_MDET_TX_QUEUE_SHIFT;
5709 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00005710 "Malicious Driver Detection event 0x%02x on TX queue %d of function 0x%02x\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005711 event, queue, func);
5712 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
5713 mdd_detected = true;
5714 }
5715 reg = rd32(hw, I40E_GL_MDET_RX);
5716 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
5717 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK)
5718 >> I40E_GL_MDET_RX_FUNCTION_SHIFT;
5719 u8 event = (reg & I40E_GL_MDET_RX_EVENT_SHIFT)
5720 >> I40E_GL_MDET_RX_EVENT_SHIFT;
5721 u8 queue = (reg & I40E_GL_MDET_RX_QUEUE_MASK)
5722 >> I40E_GL_MDET_RX_QUEUE_SHIFT;
5723 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00005724 "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005725 event, queue, func);
5726 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
5727 mdd_detected = true;
5728 }
5729
5730 /* see if one of the VFs needs its hand slapped */
5731 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
5732 vf = &(pf->vf[i]);
5733 reg = rd32(hw, I40E_VP_MDET_TX(i));
5734 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
5735 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
5736 vf->num_mdd_events++;
5737 dev_info(&pf->pdev->dev, "MDD TX event on VF %d\n", i);
5738 }
5739
5740 reg = rd32(hw, I40E_VP_MDET_RX(i));
5741 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
5742 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
5743 vf->num_mdd_events++;
5744 dev_info(&pf->pdev->dev, "MDD RX event on VF %d\n", i);
5745 }
5746
5747 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
5748 dev_info(&pf->pdev->dev,
5749 "Too many MDD events on VF %d, disabled\n", i);
5750 dev_info(&pf->pdev->dev,
5751 "Use PF Control I/F to re-enable the VF\n");
5752 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
5753 }
5754 }
5755
5756 /* re-enable mdd interrupt cause */
5757 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
5758 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
5759 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
5760 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
5761 i40e_flush(hw);
5762}
5763
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005764#ifdef CONFIG_I40E_VXLAN
5765/**
5766 * i40e_sync_vxlan_filters_subtask - Sync the VSI filter list with HW
5767 * @pf: board private structure
5768 **/
5769static void i40e_sync_vxlan_filters_subtask(struct i40e_pf *pf)
5770{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005771 struct i40e_hw *hw = &pf->hw;
5772 i40e_status ret;
5773 u8 filter_index;
5774 __be16 port;
5775 int i;
5776
5777 if (!(pf->flags & I40E_FLAG_VXLAN_FILTER_SYNC))
5778 return;
5779
5780 pf->flags &= ~I40E_FLAG_VXLAN_FILTER_SYNC;
5781
5782 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
5783 if (pf->pending_vxlan_bitmap & (1 << i)) {
5784 pf->pending_vxlan_bitmap &= ~(1 << i);
5785 port = pf->vxlan_ports[i];
5786 ret = port ?
5787 i40e_aq_add_udp_tunnel(hw, ntohs(port),
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005788 I40E_AQC_TUNNEL_TYPE_VXLAN,
5789 &filter_index, NULL)
5790 : i40e_aq_del_udp_tunnel(hw, i, NULL);
5791
5792 if (ret) {
5793 dev_info(&pf->pdev->dev, "Failed to execute AQ command for %s port %d with index %d\n",
5794 port ? "adding" : "deleting",
5795 ntohs(port), port ? i : i);
5796
5797 pf->vxlan_ports[i] = 0;
5798 } else {
5799 dev_info(&pf->pdev->dev, "%s port %d with AQ command with index %d\n",
5800 port ? "Added" : "Deleted",
5801 ntohs(port), port ? i : filter_index);
5802 }
5803 }
5804 }
5805}
5806
5807#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005808/**
5809 * i40e_service_task - Run the driver's async subtasks
5810 * @work: pointer to work_struct containing our data
5811 **/
5812static void i40e_service_task(struct work_struct *work)
5813{
5814 struct i40e_pf *pf = container_of(work,
5815 struct i40e_pf,
5816 service_task);
5817 unsigned long start_time = jiffies;
5818
5819 i40e_reset_subtask(pf);
5820 i40e_handle_mdd_event(pf);
5821 i40e_vc_process_vflr_event(pf);
5822 i40e_watchdog_subtask(pf);
5823 i40e_fdir_reinit_subtask(pf);
5824 i40e_check_hang_subtask(pf);
5825 i40e_sync_filters_subtask(pf);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005826#ifdef CONFIG_I40E_VXLAN
5827 i40e_sync_vxlan_filters_subtask(pf);
5828#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005829 i40e_clean_adminq_subtask(pf);
5830
5831 i40e_service_event_complete(pf);
5832
5833 /* If the tasks have taken longer than one timer cycle or there
5834 * is more work to be done, reschedule the service task now
5835 * rather than wait for the timer to tick again.
5836 */
5837 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
5838 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
5839 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
5840 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
5841 i40e_service_event_schedule(pf);
5842}
5843
5844/**
5845 * i40e_service_timer - timer callback
5846 * @data: pointer to PF struct
5847 **/
5848static void i40e_service_timer(unsigned long data)
5849{
5850 struct i40e_pf *pf = (struct i40e_pf *)data;
5851
5852 mod_timer(&pf->service_timer,
5853 round_jiffies(jiffies + pf->service_timer_period));
5854 i40e_service_event_schedule(pf);
5855}
5856
5857/**
5858 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
5859 * @vsi: the VSI being configured
5860 **/
5861static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
5862{
5863 struct i40e_pf *pf = vsi->back;
5864
5865 switch (vsi->type) {
5866 case I40E_VSI_MAIN:
5867 vsi->alloc_queue_pairs = pf->num_lan_qps;
5868 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5869 I40E_REQ_DESCRIPTOR_MULTIPLE);
5870 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5871 vsi->num_q_vectors = pf->num_lan_msix;
5872 else
5873 vsi->num_q_vectors = 1;
5874
5875 break;
5876
5877 case I40E_VSI_FDIR:
5878 vsi->alloc_queue_pairs = 1;
5879 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
5880 I40E_REQ_DESCRIPTOR_MULTIPLE);
5881 vsi->num_q_vectors = 1;
5882 break;
5883
5884 case I40E_VSI_VMDQ2:
5885 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
5886 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5887 I40E_REQ_DESCRIPTOR_MULTIPLE);
5888 vsi->num_q_vectors = pf->num_vmdq_msix;
5889 break;
5890
5891 case I40E_VSI_SRIOV:
5892 vsi->alloc_queue_pairs = pf->num_vf_qps;
5893 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5894 I40E_REQ_DESCRIPTOR_MULTIPLE);
5895 break;
5896
5897 default:
5898 WARN_ON(1);
5899 return -ENODATA;
5900 }
5901
5902 return 0;
5903}
5904
5905/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005906 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
5907 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005908 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
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 int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005914{
5915 int size;
5916 int ret = 0;
5917
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00005918 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005919 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
5920 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
5921 if (!vsi->tx_rings)
5922 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005923 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
5924
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005925 if (alloc_qvectors) {
5926 /* allocate memory for q_vector pointers */
5927 size = sizeof(struct i40e_q_vectors *) * vsi->num_q_vectors;
5928 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
5929 if (!vsi->q_vectors) {
5930 ret = -ENOMEM;
5931 goto err_vectors;
5932 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005933 }
5934 return ret;
5935
5936err_vectors:
5937 kfree(vsi->tx_rings);
5938 return ret;
5939}
5940
5941/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005942 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
5943 * @pf: board private structure
5944 * @type: type of VSI
5945 *
5946 * On error: returns error code (negative)
5947 * On success: returns vsi index in PF (positive)
5948 **/
5949static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
5950{
5951 int ret = -ENODEV;
5952 struct i40e_vsi *vsi;
5953 int vsi_idx;
5954 int i;
5955
5956 /* Need to protect the allocation of the VSIs at the PF level */
5957 mutex_lock(&pf->switch_mutex);
5958
5959 /* VSI list may be fragmented if VSI creation/destruction has
5960 * been happening. We can afford to do a quick scan to look
5961 * for any free VSIs in the list.
5962 *
5963 * find next empty vsi slot, looping back around if necessary
5964 */
5965 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00005966 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005967 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00005968 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005969 i = 0;
5970 while (i < pf->next_vsi && pf->vsi[i])
5971 i++;
5972 }
5973
Mitch Williams505682c2014-05-20 08:01:37 +00005974 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005975 vsi_idx = i; /* Found one! */
5976 } else {
5977 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00005978 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005979 }
5980 pf->next_vsi = ++i;
5981
5982 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
5983 if (!vsi) {
5984 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00005985 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005986 }
5987 vsi->type = type;
5988 vsi->back = pf;
5989 set_bit(__I40E_DOWN, &vsi->state);
5990 vsi->flags = 0;
5991 vsi->idx = vsi_idx;
5992 vsi->rx_itr_setting = pf->rx_itr_default;
5993 vsi->tx_itr_setting = pf->tx_itr_default;
5994 vsi->netdev_registered = false;
5995 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
5996 INIT_LIST_HEAD(&vsi->mac_filter_list);
Shannon Nelson63741842014-04-23 04:50:16 +00005997 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005998
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005999 ret = i40e_set_num_rings_in_vsi(vsi);
6000 if (ret)
6001 goto err_rings;
6002
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006003 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006004 if (ret)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006005 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00006006
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006007 /* Setup default MSIX irq handler for VSI */
6008 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
6009
6010 pf->vsi[vsi_idx] = vsi;
6011 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00006012 goto unlock_pf;
6013
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006014err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00006015 pf->next_vsi = i - 1;
6016 kfree(vsi);
6017unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006018 mutex_unlock(&pf->switch_mutex);
6019 return ret;
6020}
6021
6022/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006023 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
6024 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006025 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006026 *
6027 * On error: returns error code (negative)
6028 * On success: returns 0
6029 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006030static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006031{
6032 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006033 if (free_qvectors) {
6034 kfree(vsi->q_vectors);
6035 vsi->q_vectors = NULL;
6036 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006037 kfree(vsi->tx_rings);
6038 vsi->tx_rings = NULL;
6039 vsi->rx_rings = NULL;
6040}
6041
6042/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006043 * i40e_vsi_clear - Deallocate the VSI provided
6044 * @vsi: the VSI being un-configured
6045 **/
6046static int i40e_vsi_clear(struct i40e_vsi *vsi)
6047{
6048 struct i40e_pf *pf;
6049
6050 if (!vsi)
6051 return 0;
6052
6053 if (!vsi->back)
6054 goto free_vsi;
6055 pf = vsi->back;
6056
6057 mutex_lock(&pf->switch_mutex);
6058 if (!pf->vsi[vsi->idx]) {
6059 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
6060 vsi->idx, vsi->idx, vsi, vsi->type);
6061 goto unlock_vsi;
6062 }
6063
6064 if (pf->vsi[vsi->idx] != vsi) {
6065 dev_err(&pf->pdev->dev,
6066 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
6067 pf->vsi[vsi->idx]->idx,
6068 pf->vsi[vsi->idx],
6069 pf->vsi[vsi->idx]->type,
6070 vsi->idx, vsi, vsi->type);
6071 goto unlock_vsi;
6072 }
6073
6074 /* updates the pf for this cleared vsi */
6075 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
6076 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
6077
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006078 i40e_vsi_free_arrays(vsi, true);
Alexander Duyck493fb302013-09-28 07:01:44 +00006079
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006080 pf->vsi[vsi->idx] = NULL;
6081 if (vsi->idx < pf->next_vsi)
6082 pf->next_vsi = vsi->idx;
6083
6084unlock_vsi:
6085 mutex_unlock(&pf->switch_mutex);
6086free_vsi:
6087 kfree(vsi);
6088
6089 return 0;
6090}
6091
6092/**
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006093 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
6094 * @vsi: the VSI being cleaned
6095 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00006096static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006097{
6098 int i;
6099
Greg Rose8e9dca52013-12-18 13:45:53 +00006100 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00006101 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00006102 kfree_rcu(vsi->tx_rings[i], rcu);
6103 vsi->tx_rings[i] = NULL;
6104 vsi->rx_rings[i] = NULL;
6105 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00006106 }
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006107}
6108
6109/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006110 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
6111 * @vsi: the VSI being configured
6112 **/
6113static int i40e_alloc_rings(struct i40e_vsi *vsi)
6114{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00006115 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006116 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006117 int i;
6118
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006119 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00006120 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00006121 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006122 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
6123 if (!tx_ring)
6124 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006125
6126 tx_ring->queue_index = i;
6127 tx_ring->reg_idx = vsi->base_queue + i;
6128 tx_ring->ring_active = false;
6129 tx_ring->vsi = vsi;
6130 tx_ring->netdev = vsi->netdev;
6131 tx_ring->dev = &pf->pdev->dev;
6132 tx_ring->count = vsi->num_desc;
6133 tx_ring->size = 0;
6134 tx_ring->dcb_tc = 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006135 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006136
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006137 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006138 rx_ring->queue_index = i;
6139 rx_ring->reg_idx = vsi->base_queue + i;
6140 rx_ring->ring_active = false;
6141 rx_ring->vsi = vsi;
6142 rx_ring->netdev = vsi->netdev;
6143 rx_ring->dev = &pf->pdev->dev;
6144 rx_ring->count = vsi->num_desc;
6145 rx_ring->size = 0;
6146 rx_ring->dcb_tc = 0;
6147 if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
6148 set_ring_16byte_desc_enabled(rx_ring);
6149 else
6150 clear_ring_16byte_desc_enabled(rx_ring);
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006151 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006152 }
6153
6154 return 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006155
6156err_out:
6157 i40e_vsi_clear_rings(vsi);
6158 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006159}
6160
6161/**
6162 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
6163 * @pf: board private structure
6164 * @vectors: the number of MSI-X vectors to request
6165 *
6166 * Returns the number of vectors reserved, or error
6167 **/
6168static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
6169{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01006170 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
6171 I40E_MIN_MSIX, vectors);
6172 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006173 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01006174 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006175 vectors = 0;
6176 }
6177
6178 return vectors;
6179}
6180
6181/**
6182 * i40e_init_msix - Setup the MSIX capability
6183 * @pf: board private structure
6184 *
6185 * Work with the OS to set up the MSIX vectors needed.
6186 *
6187 * Returns 0 on success, negative on failure
6188 **/
6189static int i40e_init_msix(struct i40e_pf *pf)
6190{
6191 i40e_status err = 0;
6192 struct i40e_hw *hw = &pf->hw;
6193 int v_budget, i;
6194 int vec;
6195
6196 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
6197 return -ENODEV;
6198
6199 /* The number of vectors we'll request will be comprised of:
6200 * - Add 1 for "other" cause for Admin Queue events, etc.
6201 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006202 * - Queues being used for RSS.
6203 * We don't need as many as max_rss_size vectors.
6204 * use rss_size instead in the calculation since that
6205 * is governed by number of cpus in the system.
6206 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006207 * - The number of VMDq pairs
6208 * Once we count this up, try the request.
6209 *
6210 * If we can't get what we want, we'll simplify to nearly nothing
6211 * and try again. If that still fails, we punt.
6212 */
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006213 pf->num_lan_msix = pf->num_lan_qps - (pf->rss_size_max - pf->rss_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006214 pf->num_vmdq_msix = pf->num_vmdq_qps;
6215 v_budget = 1 + pf->num_lan_msix;
6216 v_budget += (pf->num_vmdq_vsis * pf->num_vmdq_msix);
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006217 if (pf->flags & I40E_FLAG_FD_SB_ENABLED)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006218 v_budget++;
6219
6220 /* Scale down if necessary, and the rings will share vectors */
6221 v_budget = min_t(int, v_budget, hw->func_caps.num_msix_vectors);
6222
6223 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
6224 GFP_KERNEL);
6225 if (!pf->msix_entries)
6226 return -ENOMEM;
6227
6228 for (i = 0; i < v_budget; i++)
6229 pf->msix_entries[i].entry = i;
6230 vec = i40e_reserve_msix_vectors(pf, v_budget);
6231 if (vec < I40E_MIN_MSIX) {
6232 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
6233 kfree(pf->msix_entries);
6234 pf->msix_entries = NULL;
6235 return -ENODEV;
6236
6237 } else if (vec == I40E_MIN_MSIX) {
6238 /* Adjust for minimal MSIX use */
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006239 dev_info(&pf->pdev->dev, "Features disabled, not enough MSI-X vectors\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006240 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
6241 pf->num_vmdq_vsis = 0;
6242 pf->num_vmdq_qps = 0;
6243 pf->num_vmdq_msix = 0;
6244 pf->num_lan_qps = 1;
6245 pf->num_lan_msix = 1;
6246
6247 } else if (vec != v_budget) {
6248 /* Scale vector usage down */
6249 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
6250 vec--; /* reserve the misc vector */
6251
6252 /* partition out the remaining vectors */
6253 switch (vec) {
6254 case 2:
6255 pf->num_vmdq_vsis = 1;
6256 pf->num_lan_msix = 1;
6257 break;
6258 case 3:
6259 pf->num_vmdq_vsis = 1;
6260 pf->num_lan_msix = 2;
6261 break;
6262 default:
6263 pf->num_lan_msix = min_t(int, (vec / 2),
6264 pf->num_lan_qps);
6265 pf->num_vmdq_vsis = min_t(int, (vec - pf->num_lan_msix),
6266 I40E_DEFAULT_NUM_VMDQ_VSI);
6267 break;
6268 }
6269 }
6270
6271 return err;
6272}
6273
6274/**
Greg Rose90e04072014-03-06 08:59:57 +00006275 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00006276 * @vsi: the VSI being configured
6277 * @v_idx: index of the vector in the vsi struct
6278 *
6279 * We allocate one q_vector. If allocation fails we return -ENOMEM.
6280 **/
Greg Rose90e04072014-03-06 08:59:57 +00006281static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
Alexander Duyck493fb302013-09-28 07:01:44 +00006282{
6283 struct i40e_q_vector *q_vector;
6284
6285 /* allocate q_vector */
6286 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
6287 if (!q_vector)
6288 return -ENOMEM;
6289
6290 q_vector->vsi = vsi;
6291 q_vector->v_idx = v_idx;
6292 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
6293 if (vsi->netdev)
6294 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00006295 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00006296
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00006297 q_vector->rx.latency_range = I40E_LOW_LATENCY;
6298 q_vector->tx.latency_range = I40E_LOW_LATENCY;
6299
Alexander Duyck493fb302013-09-28 07:01:44 +00006300 /* tie q_vector and vsi together */
6301 vsi->q_vectors[v_idx] = q_vector;
6302
6303 return 0;
6304}
6305
6306/**
Greg Rose90e04072014-03-06 08:59:57 +00006307 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006308 * @vsi: the VSI being configured
6309 *
6310 * We allocate one q_vector per queue interrupt. If allocation fails we
6311 * return -ENOMEM.
6312 **/
Greg Rose90e04072014-03-06 08:59:57 +00006313static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006314{
6315 struct i40e_pf *pf = vsi->back;
6316 int v_idx, num_q_vectors;
Alexander Duyck493fb302013-09-28 07:01:44 +00006317 int err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006318
6319 /* if not MSIX, give the one vector only to the LAN VSI */
6320 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6321 num_q_vectors = vsi->num_q_vectors;
6322 else if (vsi == pf->vsi[pf->lan_vsi])
6323 num_q_vectors = 1;
6324 else
6325 return -EINVAL;
6326
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006327 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Greg Rose90e04072014-03-06 08:59:57 +00006328 err = i40e_vsi_alloc_q_vector(vsi, v_idx);
Alexander Duyck493fb302013-09-28 07:01:44 +00006329 if (err)
6330 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006331 }
6332
6333 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00006334
6335err_out:
6336 while (v_idx--)
6337 i40e_free_q_vector(vsi, v_idx);
6338
6339 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006340}
6341
6342/**
6343 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
6344 * @pf: board private structure to initialize
6345 **/
6346static void i40e_init_interrupt_scheme(struct i40e_pf *pf)
6347{
6348 int err = 0;
6349
6350 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
6351 err = i40e_init_msix(pf);
6352 if (err) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006353 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
6354 I40E_FLAG_RSS_ENABLED |
6355 I40E_FLAG_DCB_ENABLED |
6356 I40E_FLAG_SRIOV_ENABLED |
6357 I40E_FLAG_FD_SB_ENABLED |
6358 I40E_FLAG_FD_ATR_ENABLED |
6359 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006360
6361 /* rework the queue expectations without MSIX */
6362 i40e_determine_queue_usage(pf);
6363 }
6364 }
6365
6366 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
6367 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006368 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006369 err = pci_enable_msi(pf->pdev);
6370 if (err) {
Shannon Nelson958a3e32013-09-28 07:13:28 +00006371 dev_info(&pf->pdev->dev, "MSI init failed - %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006372 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
6373 }
6374 }
6375
Shannon Nelson958a3e32013-09-28 07:13:28 +00006376 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006377 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00006378
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006379 /* track first vector for misc interrupts */
6380 err = i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT-1);
6381}
6382
6383/**
6384 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
6385 * @pf: board private structure
6386 *
6387 * This sets up the handler for MSIX 0, which is used to manage the
6388 * non-queue interrupts, e.g. AdminQ and errors. This is not used
6389 * when in MSI or Legacy interrupt mode.
6390 **/
6391static int i40e_setup_misc_vector(struct i40e_pf *pf)
6392{
6393 struct i40e_hw *hw = &pf->hw;
6394 int err = 0;
6395
6396 /* Only request the irq if this is the first time through, and
6397 * not when we're rebuilding after a Reset
6398 */
6399 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
6400 err = request_irq(pf->msix_entries[0].vector,
6401 i40e_intr, 0, pf->misc_int_name, pf);
6402 if (err) {
6403 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006404 "request_irq for %s failed: %d\n",
6405 pf->misc_int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006406 return -EFAULT;
6407 }
6408 }
6409
6410 i40e_enable_misc_int_causes(hw);
6411
6412 /* associate no queues to the misc vector */
6413 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
6414 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
6415
6416 i40e_flush(hw);
6417
6418 i40e_irq_dynamic_enable_icr0(pf);
6419
6420 return err;
6421}
6422
6423/**
6424 * i40e_config_rss - Prepare for RSS if used
6425 * @pf: board private structure
6426 **/
6427static int i40e_config_rss(struct i40e_pf *pf)
6428{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006429 /* Set of random keys generated using kernel random number generator */
6430 static const u32 seed[I40E_PFQF_HKEY_MAX_INDEX + 1] = {0x41b01687,
6431 0x183cfd8c, 0xce880440, 0x580cbc3c, 0x35897377,
6432 0x328b25e1, 0x4fa98922, 0xb7d90c14, 0xd5bad70d,
6433 0xcd15a2c1, 0xe8580225, 0x4a1e9d11, 0xfe5731be};
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00006434 struct i40e_hw *hw = &pf->hw;
6435 u32 lut = 0;
6436 int i, j;
6437 u64 hena;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006438
6439 /* Fill out hash function seed */
6440 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
6441 wr32(hw, I40E_PFQF_HKEY(i), seed[i]);
6442
6443 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
6444 hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
6445 ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
Mitch Williams12dc4fe2013-11-28 06:39:32 +00006446 hena |= I40E_DEFAULT_RSS_HENA;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006447 wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
6448 wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
6449
6450 /* Populate the LUT with max no. of queues in round robin fashion */
6451 for (i = 0, j = 0; i < pf->hw.func_caps.rss_table_size; i++, j++) {
6452
6453 /* The assumption is that lan qp count will be the highest
6454 * qp count for any PF VSI that needs RSS.
6455 * If multiple VSIs need RSS support, all the qp counts
6456 * for those VSIs should be a power of 2 for RSS to work.
6457 * If LAN VSI is the only consumer for RSS then this requirement
6458 * is not necessary.
6459 */
6460 if (j == pf->rss_size)
6461 j = 0;
6462 /* lut = 4-byte sliding window of 4 lut entries */
6463 lut = (lut << 8) | (j &
6464 ((0x1 << pf->hw.func_caps.rss_table_entry_width) - 1));
6465 /* On i = 3, we have 4 entries in lut; write to the register */
6466 if ((i & 3) == 3)
6467 wr32(hw, I40E_PFQF_HLUT(i >> 2), lut);
6468 }
6469 i40e_flush(hw);
6470
6471 return 0;
6472}
6473
6474/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006475 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
6476 * @pf: board private structure
6477 * @queue_count: the requested queue count for rss.
6478 *
6479 * returns 0 if rss is not enabled, if enabled returns the final rss queue
6480 * count which may be different from the requested queue count.
6481 **/
6482int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
6483{
6484 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
6485 return 0;
6486
6487 queue_count = min_t(int, queue_count, pf->rss_size_max);
6488 queue_count = rounddown_pow_of_two(queue_count);
6489
6490 if (queue_count != pf->rss_size) {
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006491 i40e_prep_for_reset(pf);
6492
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006493 pf->rss_size = queue_count;
6494
6495 i40e_reset_and_rebuild(pf, true);
6496 i40e_config_rss(pf);
6497 }
6498 dev_info(&pf->pdev->dev, "RSS count: %d\n", pf->rss_size);
6499 return pf->rss_size;
6500}
6501
6502/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006503 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
6504 * @pf: board private structure to initialize
6505 *
6506 * i40e_sw_init initializes the Adapter private data structure.
6507 * Fields are initialized based on PCI device information and
6508 * OS network device settings (MTU size).
6509 **/
6510static int i40e_sw_init(struct i40e_pf *pf)
6511{
6512 int err = 0;
6513 int size;
6514
6515 pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
6516 (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
Shannon Nelson27599972013-11-16 10:00:43 +00006517 pf->hw.debug_mask = pf->msg_enable | I40E_DEBUG_DIAG;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006518 if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
6519 if (I40E_DEBUG_USER & debug)
6520 pf->hw.debug_mask = debug;
6521 pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
6522 I40E_DEFAULT_MSG_ENABLE);
6523 }
6524
6525 /* Set default capability flags */
6526 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
6527 I40E_FLAG_MSI_ENABLED |
6528 I40E_FLAG_MSIX_ENABLED |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006529 I40E_FLAG_RX_1BUF_ENABLED;
6530
Mitch Williamsca99eb92014-04-04 04:43:07 +00006531 /* Set default ITR */
6532 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
6533 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
6534
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006535 /* Depending on PF configurations, it is possible that the RSS
6536 * maximum might end up larger than the available queues
6537 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006538 pf->rss_size_max = 0x1 << pf->hw.func_caps.rss_table_entry_width;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006539 pf->rss_size_max = min_t(int, pf->rss_size_max,
6540 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006541 if (pf->hw.func_caps.rss) {
6542 pf->flags |= I40E_FLAG_RSS_ENABLED;
Jesse Brandeburgbf051a32013-11-26 10:49:17 +00006543 pf->rss_size = min_t(int, pf->rss_size_max, num_online_cpus());
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006544 pf->rss_size = rounddown_pow_of_two(pf->rss_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006545 } else {
6546 pf->rss_size = 1;
6547 }
6548
Catherine Sullivan2050bc62013-12-18 13:46:03 +00006549 /* MFP mode enabled */
6550 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.mfp_mode_1) {
6551 pf->flags |= I40E_FLAG_MFP_ENABLED;
6552 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
6553 }
6554
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006555 /* FW/NVM is not yet fixed in this regard */
6556 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
6557 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
6558 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
6559 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006560 if (!(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006561 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006562 } else {
6563 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00006564 "Flow Director Sideband mode Disabled in MFP mode\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006565 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006566 pf->fdir_pf_filter_count =
6567 pf->hw.func_caps.fd_filters_guaranteed;
6568 pf->hw.fdir_shared_filter_count =
6569 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006570 }
6571
6572 if (pf->hw.func_caps.vmdq) {
6573 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
6574 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
6575 pf->num_vmdq_qps = I40E_DEFAULT_QUEUES_PER_VMDQ;
6576 }
6577
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006578#ifdef CONFIG_PCI_IOV
6579 if (pf->hw.func_caps.num_vfs) {
6580 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
6581 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
6582 pf->num_req_vfs = min_t(int,
6583 pf->hw.func_caps.num_vfs,
6584 I40E_MAX_VF_COUNT);
6585 }
6586#endif /* CONFIG_PCI_IOV */
6587 pf->eeprom_version = 0xDEAD;
6588 pf->lan_veb = I40E_NO_VEB;
6589 pf->lan_vsi = I40E_NO_VSI;
6590
6591 /* set up queue assignment tracking */
6592 size = sizeof(struct i40e_lump_tracking)
6593 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
6594 pf->qp_pile = kzalloc(size, GFP_KERNEL);
6595 if (!pf->qp_pile) {
6596 err = -ENOMEM;
6597 goto sw_init_done;
6598 }
6599 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
6600 pf->qp_pile->search_hint = 0;
6601
6602 /* set up vector assignment tracking */
6603 size = sizeof(struct i40e_lump_tracking)
6604 + (sizeof(u16) * pf->hw.func_caps.num_msix_vectors);
6605 pf->irq_pile = kzalloc(size, GFP_KERNEL);
6606 if (!pf->irq_pile) {
6607 kfree(pf->qp_pile);
6608 err = -ENOMEM;
6609 goto sw_init_done;
6610 }
6611 pf->irq_pile->num_entries = pf->hw.func_caps.num_msix_vectors;
6612 pf->irq_pile->search_hint = 0;
6613
6614 mutex_init(&pf->switch_mutex);
6615
6616sw_init_done:
6617 return err;
6618}
6619
6620/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00006621 * i40e_set_ntuple - set the ntuple feature flag and take action
6622 * @pf: board private structure to initialize
6623 * @features: the feature set that the stack is suggesting
6624 *
6625 * returns a bool to indicate if reset needs to happen
6626 **/
6627bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
6628{
6629 bool need_reset = false;
6630
6631 /* Check if Flow Director n-tuple support was enabled or disabled. If
6632 * the state changed, we need to reset.
6633 */
6634 if (features & NETIF_F_NTUPLE) {
6635 /* Enable filters and mark for reset */
6636 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
6637 need_reset = true;
6638 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
6639 } else {
6640 /* turn off filters, mark for reset and clear SW filter list */
6641 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
6642 need_reset = true;
6643 i40e_fdir_filter_exit(pf);
6644 }
6645 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6646 /* if ATR was disabled it can be re-enabled. */
6647 if (!(pf->flags & I40E_FLAG_FD_ATR_ENABLED))
6648 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
6649 }
6650 return need_reset;
6651}
6652
6653/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006654 * i40e_set_features - set the netdev feature flags
6655 * @netdev: ptr to the netdev being adjusted
6656 * @features: the feature set that the stack is suggesting
6657 **/
6658static int i40e_set_features(struct net_device *netdev,
6659 netdev_features_t features)
6660{
6661 struct i40e_netdev_priv *np = netdev_priv(netdev);
6662 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00006663 struct i40e_pf *pf = vsi->back;
6664 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006665
6666 if (features & NETIF_F_HW_VLAN_CTAG_RX)
6667 i40e_vlan_stripping_enable(vsi);
6668 else
6669 i40e_vlan_stripping_disable(vsi);
6670
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00006671 need_reset = i40e_set_ntuple(pf, features);
6672
6673 if (need_reset)
6674 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
6675
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006676 return 0;
6677}
6678
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006679#ifdef CONFIG_I40E_VXLAN
6680/**
6681 * i40e_get_vxlan_port_idx - Lookup a possibly offloaded for Rx UDP port
6682 * @pf: board private structure
6683 * @port: The UDP port to look up
6684 *
6685 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
6686 **/
6687static u8 i40e_get_vxlan_port_idx(struct i40e_pf *pf, __be16 port)
6688{
6689 u8 i;
6690
6691 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
6692 if (pf->vxlan_ports[i] == port)
6693 return i;
6694 }
6695
6696 return i;
6697}
6698
6699/**
6700 * i40e_add_vxlan_port - Get notifications about VXLAN ports that come up
6701 * @netdev: This physical port's netdev
6702 * @sa_family: Socket Family that VXLAN is notifying us about
6703 * @port: New UDP port number that VXLAN started listening to
6704 **/
6705static void i40e_add_vxlan_port(struct net_device *netdev,
6706 sa_family_t sa_family, __be16 port)
6707{
6708 struct i40e_netdev_priv *np = netdev_priv(netdev);
6709 struct i40e_vsi *vsi = np->vsi;
6710 struct i40e_pf *pf = vsi->back;
6711 u8 next_idx;
6712 u8 idx;
6713
6714 if (sa_family == AF_INET6)
6715 return;
6716
6717 idx = i40e_get_vxlan_port_idx(pf, port);
6718
6719 /* Check if port already exists */
6720 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6721 netdev_info(netdev, "Port %d already offloaded\n", ntohs(port));
6722 return;
6723 }
6724
6725 /* Now check if there is space to add the new port */
6726 next_idx = i40e_get_vxlan_port_idx(pf, 0);
6727
6728 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6729 netdev_info(netdev, "Maximum number of UDP ports reached, not adding port %d\n",
6730 ntohs(port));
6731 return;
6732 }
6733
6734 /* New port: add it and mark its index in the bitmap */
6735 pf->vxlan_ports[next_idx] = port;
6736 pf->pending_vxlan_bitmap |= (1 << next_idx);
6737
6738 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
6739}
6740
6741/**
6742 * i40e_del_vxlan_port - Get notifications about VXLAN ports that go away
6743 * @netdev: This physical port's netdev
6744 * @sa_family: Socket Family that VXLAN is notifying us about
6745 * @port: UDP port number that VXLAN stopped listening to
6746 **/
6747static void i40e_del_vxlan_port(struct net_device *netdev,
6748 sa_family_t sa_family, __be16 port)
6749{
6750 struct i40e_netdev_priv *np = netdev_priv(netdev);
6751 struct i40e_vsi *vsi = np->vsi;
6752 struct i40e_pf *pf = vsi->back;
6753 u8 idx;
6754
6755 if (sa_family == AF_INET6)
6756 return;
6757
6758 idx = i40e_get_vxlan_port_idx(pf, port);
6759
6760 /* Check if port already exists */
6761 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6762 /* if port exists, set it to 0 (mark for deletion)
6763 * and make it pending
6764 */
6765 pf->vxlan_ports[idx] = 0;
6766
6767 pf->pending_vxlan_bitmap |= (1 << idx);
6768
6769 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
6770 } else {
6771 netdev_warn(netdev, "Port %d was not found, not deleting\n",
6772 ntohs(port));
6773 }
6774}
6775
6776#endif
Greg Rose4ba0dea2014-03-06 08:59:55 +00006777#ifdef HAVE_FDB_OPS
6778#ifdef USE_CONST_DEV_UC_CHAR
6779static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
6780 struct net_device *dev,
6781 const unsigned char *addr,
6782 u16 flags)
6783#else
6784static int i40e_ndo_fdb_add(struct ndmsg *ndm,
6785 struct net_device *dev,
6786 unsigned char *addr,
6787 u16 flags)
6788#endif
6789{
6790 struct i40e_netdev_priv *np = netdev_priv(dev);
6791 struct i40e_pf *pf = np->vsi->back;
6792 int err = 0;
6793
6794 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
6795 return -EOPNOTSUPP;
6796
6797 /* Hardware does not support aging addresses so if a
6798 * ndm_state is given only allow permanent addresses
6799 */
6800 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
6801 netdev_info(dev, "FDB only supports static addresses\n");
6802 return -EINVAL;
6803 }
6804
6805 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
6806 err = dev_uc_add_excl(dev, addr);
6807 else if (is_multicast_ether_addr(addr))
6808 err = dev_mc_add_excl(dev, addr);
6809 else
6810 err = -EINVAL;
6811
6812 /* Only return duplicate errors if NLM_F_EXCL is set */
6813 if (err == -EEXIST && !(flags & NLM_F_EXCL))
6814 err = 0;
6815
6816 return err;
6817}
6818
6819#ifndef USE_DEFAULT_FDB_DEL_DUMP
6820#ifdef USE_CONST_DEV_UC_CHAR
6821static int i40e_ndo_fdb_del(struct ndmsg *ndm,
6822 struct net_device *dev,
6823 const unsigned char *addr)
6824#else
6825static int i40e_ndo_fdb_del(struct ndmsg *ndm,
6826 struct net_device *dev,
6827 unsigned char *addr)
6828#endif
6829{
6830 struct i40e_netdev_priv *np = netdev_priv(dev);
6831 struct i40e_pf *pf = np->vsi->back;
6832 int err = -EOPNOTSUPP;
6833
6834 if (ndm->ndm_state & NUD_PERMANENT) {
6835 netdev_info(dev, "FDB only supports static addresses\n");
6836 return -EINVAL;
6837 }
6838
6839 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
6840 if (is_unicast_ether_addr(addr))
6841 err = dev_uc_del(dev, addr);
6842 else if (is_multicast_ether_addr(addr))
6843 err = dev_mc_del(dev, addr);
6844 else
6845 err = -EINVAL;
6846 }
6847
6848 return err;
6849}
6850
6851static int i40e_ndo_fdb_dump(struct sk_buff *skb,
6852 struct netlink_callback *cb,
6853 struct net_device *dev,
6854 int idx)
6855{
6856 struct i40e_netdev_priv *np = netdev_priv(dev);
6857 struct i40e_pf *pf = np->vsi->back;
6858
6859 if (pf->flags & I40E_FLAG_SRIOV_ENABLED)
6860 idx = ndo_dflt_fdb_dump(skb, cb, dev, idx);
6861
6862 return idx;
6863}
6864
6865#endif /* USE_DEFAULT_FDB_DEL_DUMP */
6866#endif /* HAVE_FDB_OPS */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006867static const struct net_device_ops i40e_netdev_ops = {
6868 .ndo_open = i40e_open,
6869 .ndo_stop = i40e_close,
6870 .ndo_start_xmit = i40e_lan_xmit_frame,
6871 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
6872 .ndo_set_rx_mode = i40e_set_rx_mode,
6873 .ndo_validate_addr = eth_validate_addr,
6874 .ndo_set_mac_address = i40e_set_mac,
6875 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006876 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006877 .ndo_tx_timeout = i40e_tx_timeout,
6878 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
6879 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
6880#ifdef CONFIG_NET_POLL_CONTROLLER
6881 .ndo_poll_controller = i40e_netpoll,
6882#endif
6883 .ndo_setup_tc = i40e_setup_tc,
6884 .ndo_set_features = i40e_set_features,
6885 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
6886 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04006887 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006888 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00006889 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Mitch Williamsc674d122014-05-20 08:01:40 +00006890 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofck,
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006891#ifdef CONFIG_I40E_VXLAN
6892 .ndo_add_vxlan_port = i40e_add_vxlan_port,
6893 .ndo_del_vxlan_port = i40e_del_vxlan_port,
6894#endif
Greg Rose4ba0dea2014-03-06 08:59:55 +00006895#ifdef HAVE_FDB_OPS
6896 .ndo_fdb_add = i40e_ndo_fdb_add,
6897#ifndef USE_DEFAULT_FDB_DEL_DUMP
6898 .ndo_fdb_del = i40e_ndo_fdb_del,
6899 .ndo_fdb_dump = i40e_ndo_fdb_dump,
6900#endif
6901#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006902};
6903
6904/**
6905 * i40e_config_netdev - Setup the netdev flags
6906 * @vsi: the VSI being configured
6907 *
6908 * Returns 0 on success, negative value on failure
6909 **/
6910static int i40e_config_netdev(struct i40e_vsi *vsi)
6911{
Greg Rose1a103702013-11-28 06:42:39 +00006912 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006913 struct i40e_pf *pf = vsi->back;
6914 struct i40e_hw *hw = &pf->hw;
6915 struct i40e_netdev_priv *np;
6916 struct net_device *netdev;
6917 u8 mac_addr[ETH_ALEN];
6918 int etherdev_size;
6919
6920 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006921 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006922 if (!netdev)
6923 return -ENOMEM;
6924
6925 vsi->netdev = netdev;
6926 np = netdev_priv(netdev);
6927 np->vsi = vsi;
6928
Or Gerlitzd70e9412014-03-18 10:36:45 +02006929 netdev->hw_enc_features |= NETIF_F_IP_CSUM |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006930 NETIF_F_GSO_UDP_TUNNEL |
Or Gerlitzd70e9412014-03-18 10:36:45 +02006931 NETIF_F_TSO;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006932
6933 netdev->features = NETIF_F_SG |
6934 NETIF_F_IP_CSUM |
6935 NETIF_F_SCTP_CSUM |
6936 NETIF_F_HIGHDMA |
6937 NETIF_F_GSO_UDP_TUNNEL |
6938 NETIF_F_HW_VLAN_CTAG_TX |
6939 NETIF_F_HW_VLAN_CTAG_RX |
6940 NETIF_F_HW_VLAN_CTAG_FILTER |
6941 NETIF_F_IPV6_CSUM |
6942 NETIF_F_TSO |
Jesse Brandeburg059dab62014-04-01 09:07:20 +00006943 NETIF_F_TSO_ECN |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006944 NETIF_F_TSO6 |
6945 NETIF_F_RXCSUM |
6946 NETIF_F_RXHASH |
6947 0;
6948
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00006949 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
6950 netdev->features |= NETIF_F_NTUPLE;
6951
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006952 /* copy netdev features into list of user selectable features */
6953 netdev->hw_features |= netdev->features;
6954
6955 if (vsi->type == I40E_VSI_MAIN) {
6956 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
6957 memcpy(mac_addr, hw->mac.perm_addr, ETH_ALEN);
6958 } else {
6959 /* relate the VSI_VMDQ name to the VSI_MAIN name */
6960 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
6961 pf->vsi[pf->lan_vsi]->netdev->name);
6962 random_ether_addr(mac_addr);
6963 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
6964 }
Greg Rose1a103702013-11-28 06:42:39 +00006965 i40e_add_filter(vsi, brdcast, I40E_VLAN_ANY, false, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006966
6967 memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
6968 memcpy(netdev->perm_addr, mac_addr, ETH_ALEN);
6969 /* vlan gets same features (except vlan offload)
6970 * after any tweaks for specific VSI types
6971 */
6972 netdev->vlan_features = netdev->features & ~(NETIF_F_HW_VLAN_CTAG_TX |
6973 NETIF_F_HW_VLAN_CTAG_RX |
6974 NETIF_F_HW_VLAN_CTAG_FILTER);
6975 netdev->priv_flags |= IFF_UNICAST_FLT;
6976 netdev->priv_flags |= IFF_SUPP_NOFCS;
6977 /* Setup netdev TC information */
6978 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
6979
6980 netdev->netdev_ops = &i40e_netdev_ops;
6981 netdev->watchdog_timeo = 5 * HZ;
6982 i40e_set_ethtool_ops(netdev);
6983
6984 return 0;
6985}
6986
6987/**
6988 * i40e_vsi_delete - Delete a VSI from the switch
6989 * @vsi: the VSI being removed
6990 *
6991 * Returns 0 on success, negative value on failure
6992 **/
6993static void i40e_vsi_delete(struct i40e_vsi *vsi)
6994{
6995 /* remove default VSI is not allowed */
6996 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
6997 return;
6998
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006999 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007000}
7001
7002/**
7003 * i40e_add_vsi - Add a VSI to the switch
7004 * @vsi: the VSI being configured
7005 *
7006 * This initializes a VSI context depending on the VSI type to be added and
7007 * passes it down to the add_vsi aq command.
7008 **/
7009static int i40e_add_vsi(struct i40e_vsi *vsi)
7010{
7011 int ret = -ENODEV;
7012 struct i40e_mac_filter *f, *ftmp;
7013 struct i40e_pf *pf = vsi->back;
7014 struct i40e_hw *hw = &pf->hw;
7015 struct i40e_vsi_context ctxt;
7016 u8 enabled_tc = 0x1; /* TC0 enabled */
7017 int f_count = 0;
7018
7019 memset(&ctxt, 0, sizeof(ctxt));
7020 switch (vsi->type) {
7021 case I40E_VSI_MAIN:
7022 /* The PF's main VSI is already setup as part of the
7023 * device initialization, so we'll not bother with
7024 * the add_vsi call, but we will retrieve the current
7025 * VSI context.
7026 */
7027 ctxt.seid = pf->main_vsi_seid;
7028 ctxt.pf_num = pf->hw.pf_id;
7029 ctxt.vf_num = 0;
7030 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
7031 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
7032 if (ret) {
7033 dev_info(&pf->pdev->dev,
7034 "couldn't get pf vsi config, err %d, aq_err %d\n",
7035 ret, pf->hw.aq.asq_last_status);
7036 return -ENOENT;
7037 }
7038 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
7039 vsi->info.valid_sections = 0;
7040
7041 vsi->seid = ctxt.seid;
7042 vsi->id = ctxt.vsi_number;
7043
7044 enabled_tc = i40e_pf_get_tc_map(pf);
7045
7046 /* MFP mode setup queue map and update VSI */
7047 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
7048 memset(&ctxt, 0, sizeof(ctxt));
7049 ctxt.seid = pf->main_vsi_seid;
7050 ctxt.pf_num = pf->hw.pf_id;
7051 ctxt.vf_num = 0;
7052 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
7053 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7054 if (ret) {
7055 dev_info(&pf->pdev->dev,
7056 "update vsi failed, aq_err=%d\n",
7057 pf->hw.aq.asq_last_status);
7058 ret = -ENOENT;
7059 goto err;
7060 }
7061 /* update the local VSI info queue map */
7062 i40e_vsi_update_queue_map(vsi, &ctxt);
7063 vsi->info.valid_sections = 0;
7064 } else {
7065 /* Default/Main VSI is only enabled for TC0
7066 * reconfigure it to enable all TCs that are
7067 * available on the port in SFP mode.
7068 */
7069 ret = i40e_vsi_config_tc(vsi, enabled_tc);
7070 if (ret) {
7071 dev_info(&pf->pdev->dev,
7072 "failed to configure TCs for main VSI tc_map 0x%08x, err %d, aq_err %d\n",
7073 enabled_tc, ret,
7074 pf->hw.aq.asq_last_status);
7075 ret = -ENOENT;
7076 }
7077 }
7078 break;
7079
7080 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007081 ctxt.pf_num = hw->pf_id;
7082 ctxt.vf_num = 0;
7083 ctxt.uplink_seid = vsi->uplink_seid;
7084 ctxt.connection_type = 0x1; /* regular data port */
7085 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007086 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007087 break;
7088
7089 case I40E_VSI_VMDQ2:
7090 ctxt.pf_num = hw->pf_id;
7091 ctxt.vf_num = 0;
7092 ctxt.uplink_seid = vsi->uplink_seid;
7093 ctxt.connection_type = 0x1; /* regular data port */
7094 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
7095
7096 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
7097
7098 /* This VSI is connected to VEB so the switch_id
7099 * should be set to zero by default.
7100 */
7101 ctxt.info.switch_id = 0;
7102 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
7103 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
7104
7105 /* Setup the VSI tx/rx queue map for TC0 only for now */
7106 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
7107 break;
7108
7109 case I40E_VSI_SRIOV:
7110 ctxt.pf_num = hw->pf_id;
7111 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
7112 ctxt.uplink_seid = vsi->uplink_seid;
7113 ctxt.connection_type = 0x1; /* regular data port */
7114 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
7115
7116 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
7117
7118 /* This VSI is connected to VEB so the switch_id
7119 * should be set to zero by default.
7120 */
7121 ctxt.info.switch_id = cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
7122
7123 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
7124 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00007125 if (pf->vf[vsi->vf_id].spoofchk) {
7126 ctxt.info.valid_sections |=
7127 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
7128 ctxt.info.sec_flags |=
7129 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
7130 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
7131 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007132 /* Setup the VSI tx/rx queue map for TC0 only for now */
7133 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
7134 break;
7135
7136 default:
7137 return -ENODEV;
7138 }
7139
7140 if (vsi->type != I40E_VSI_MAIN) {
7141 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
7142 if (ret) {
7143 dev_info(&vsi->back->pdev->dev,
7144 "add vsi failed, aq_err=%d\n",
7145 vsi->back->hw.aq.asq_last_status);
7146 ret = -ENOENT;
7147 goto err;
7148 }
7149 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
7150 vsi->info.valid_sections = 0;
7151 vsi->seid = ctxt.seid;
7152 vsi->id = ctxt.vsi_number;
7153 }
7154
7155 /* If macvlan filters already exist, force them to get loaded */
7156 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
7157 f->changed = true;
7158 f_count++;
7159 }
7160 if (f_count) {
7161 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
7162 pf->flags |= I40E_FLAG_FILTER_SYNC;
7163 }
7164
7165 /* Update VSI BW information */
7166 ret = i40e_vsi_get_bw_info(vsi);
7167 if (ret) {
7168 dev_info(&pf->pdev->dev,
7169 "couldn't get vsi bw info, err %d, aq_err %d\n",
7170 ret, pf->hw.aq.asq_last_status);
7171 /* VSI is already added so not tearing that up */
7172 ret = 0;
7173 }
7174
7175err:
7176 return ret;
7177}
7178
7179/**
7180 * i40e_vsi_release - Delete a VSI and free its resources
7181 * @vsi: the VSI being removed
7182 *
7183 * Returns 0 on success or < 0 on error
7184 **/
7185int i40e_vsi_release(struct i40e_vsi *vsi)
7186{
7187 struct i40e_mac_filter *f, *ftmp;
7188 struct i40e_veb *veb = NULL;
7189 struct i40e_pf *pf;
7190 u16 uplink_seid;
7191 int i, n;
7192
7193 pf = vsi->back;
7194
7195 /* release of a VEB-owner or last VSI is not allowed */
7196 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
7197 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
7198 vsi->seid, vsi->uplink_seid);
7199 return -ENODEV;
7200 }
7201 if (vsi == pf->vsi[pf->lan_vsi] &&
7202 !test_bit(__I40E_DOWN, &pf->state)) {
7203 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
7204 return -ENODEV;
7205 }
7206
7207 uplink_seid = vsi->uplink_seid;
7208 if (vsi->type != I40E_VSI_SRIOV) {
7209 if (vsi->netdev_registered) {
7210 vsi->netdev_registered = false;
7211 if (vsi->netdev) {
7212 /* results in a call to i40e_close() */
7213 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007214 }
7215 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00007216 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007217 }
7218 i40e_vsi_disable_irq(vsi);
7219 }
7220
7221 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
7222 i40e_del_filter(vsi, f->macaddr, f->vlan,
7223 f->is_vf, f->is_netdev);
7224 i40e_sync_vsi_filters(vsi);
7225
7226 i40e_vsi_delete(vsi);
7227 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00007228 if (vsi->netdev) {
7229 free_netdev(vsi->netdev);
7230 vsi->netdev = NULL;
7231 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007232 i40e_vsi_clear_rings(vsi);
7233 i40e_vsi_clear(vsi);
7234
7235 /* If this was the last thing on the VEB, except for the
7236 * controlling VSI, remove the VEB, which puts the controlling
7237 * VSI onto the next level down in the switch.
7238 *
7239 * Well, okay, there's one more exception here: don't remove
7240 * the orphan VEBs yet. We'll wait for an explicit remove request
7241 * from up the network stack.
7242 */
Mitch Williams505682c2014-05-20 08:01:37 +00007243 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007244 if (pf->vsi[i] &&
7245 pf->vsi[i]->uplink_seid == uplink_seid &&
7246 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
7247 n++; /* count the VSIs */
7248 }
7249 }
7250 for (i = 0; i < I40E_MAX_VEB; i++) {
7251 if (!pf->veb[i])
7252 continue;
7253 if (pf->veb[i]->uplink_seid == uplink_seid)
7254 n++; /* count the VEBs */
7255 if (pf->veb[i]->seid == uplink_seid)
7256 veb = pf->veb[i];
7257 }
7258 if (n == 0 && veb && veb->uplink_seid != 0)
7259 i40e_veb_release(veb);
7260
7261 return 0;
7262}
7263
7264/**
7265 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
7266 * @vsi: ptr to the VSI
7267 *
7268 * This should only be called after i40e_vsi_mem_alloc() which allocates the
7269 * corresponding SW VSI structure and initializes num_queue_pairs for the
7270 * newly allocated VSI.
7271 *
7272 * Returns 0 on success or negative on failure
7273 **/
7274static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
7275{
7276 int ret = -ENOENT;
7277 struct i40e_pf *pf = vsi->back;
7278
Alexander Duyck493fb302013-09-28 07:01:44 +00007279 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007280 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
7281 vsi->seid);
7282 return -EEXIST;
7283 }
7284
7285 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00007286 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007287 vsi->seid, vsi->base_vector);
7288 return -EEXIST;
7289 }
7290
Greg Rose90e04072014-03-06 08:59:57 +00007291 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007292 if (ret) {
7293 dev_info(&pf->pdev->dev,
7294 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
7295 vsi->num_q_vectors, vsi->seid, ret);
7296 vsi->num_q_vectors = 0;
7297 goto vector_setup_out;
7298 }
7299
Shannon Nelson958a3e32013-09-28 07:13:28 +00007300 if (vsi->num_q_vectors)
7301 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
7302 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007303 if (vsi->base_vector < 0) {
7304 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00007305 "failed to get queue tracking for VSI %d, err=%d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007306 vsi->seid, vsi->base_vector);
7307 i40e_vsi_free_q_vectors(vsi);
7308 ret = -ENOENT;
7309 goto vector_setup_out;
7310 }
7311
7312vector_setup_out:
7313 return ret;
7314}
7315
7316/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007317 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
7318 * @vsi: pointer to the vsi.
7319 *
7320 * This re-allocates a vsi's queue resources.
7321 *
7322 * Returns pointer to the successfully allocated and configured VSI sw struct
7323 * on success, otherwise returns NULL on failure.
7324 **/
7325static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
7326{
7327 struct i40e_pf *pf = vsi->back;
7328 u8 enabled_tc;
7329 int ret;
7330
7331 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7332 i40e_vsi_clear_rings(vsi);
7333
7334 i40e_vsi_free_arrays(vsi, false);
7335 i40e_set_num_rings_in_vsi(vsi);
7336 ret = i40e_vsi_alloc_arrays(vsi, false);
7337 if (ret)
7338 goto err_vsi;
7339
7340 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
7341 if (ret < 0) {
7342 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
7343 vsi->seid, ret);
7344 goto err_vsi;
7345 }
7346 vsi->base_queue = ret;
7347
7348 /* Update the FW view of the VSI. Force a reset of TC and queue
7349 * layout configurations.
7350 */
7351 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
7352 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
7353 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
7354 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
7355
7356 /* assign it some queues */
7357 ret = i40e_alloc_rings(vsi);
7358 if (ret)
7359 goto err_rings;
7360
7361 /* map all of the rings to the q_vectors */
7362 i40e_vsi_map_rings_to_vectors(vsi);
7363 return vsi;
7364
7365err_rings:
7366 i40e_vsi_free_q_vectors(vsi);
7367 if (vsi->netdev_registered) {
7368 vsi->netdev_registered = false;
7369 unregister_netdev(vsi->netdev);
7370 free_netdev(vsi->netdev);
7371 vsi->netdev = NULL;
7372 }
7373 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
7374err_vsi:
7375 i40e_vsi_clear(vsi);
7376 return NULL;
7377}
7378
7379/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007380 * i40e_vsi_setup - Set up a VSI by a given type
7381 * @pf: board private structure
7382 * @type: VSI type
7383 * @uplink_seid: the switch element to link to
7384 * @param1: usage depends upon VSI type. For VF types, indicates VF id
7385 *
7386 * This allocates the sw VSI structure and its queue resources, then add a VSI
7387 * to the identified VEB.
7388 *
7389 * Returns pointer to the successfully allocated and configure VSI sw struct on
7390 * success, otherwise returns NULL on failure.
7391 **/
7392struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
7393 u16 uplink_seid, u32 param1)
7394{
7395 struct i40e_vsi *vsi = NULL;
7396 struct i40e_veb *veb = NULL;
7397 int ret, i;
7398 int v_idx;
7399
7400 /* The requested uplink_seid must be either
7401 * - the PF's port seid
7402 * no VEB is needed because this is the PF
7403 * or this is a Flow Director special case VSI
7404 * - seid of an existing VEB
7405 * - seid of a VSI that owns an existing VEB
7406 * - seid of a VSI that doesn't own a VEB
7407 * a new VEB is created and the VSI becomes the owner
7408 * - seid of the PF VSI, which is what creates the first VEB
7409 * this is a special case of the previous
7410 *
7411 * Find which uplink_seid we were given and create a new VEB if needed
7412 */
7413 for (i = 0; i < I40E_MAX_VEB; i++) {
7414 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
7415 veb = pf->veb[i];
7416 break;
7417 }
7418 }
7419
7420 if (!veb && uplink_seid != pf->mac_seid) {
7421
Mitch Williams505682c2014-05-20 08:01:37 +00007422 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007423 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
7424 vsi = pf->vsi[i];
7425 break;
7426 }
7427 }
7428 if (!vsi) {
7429 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
7430 uplink_seid);
7431 return NULL;
7432 }
7433
7434 if (vsi->uplink_seid == pf->mac_seid)
7435 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
7436 vsi->tc_config.enabled_tc);
7437 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
7438 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
7439 vsi->tc_config.enabled_tc);
7440
7441 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
7442 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
7443 veb = pf->veb[i];
7444 }
7445 if (!veb) {
7446 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
7447 return NULL;
7448 }
7449
7450 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
7451 uplink_seid = veb->seid;
7452 }
7453
7454 /* get vsi sw struct */
7455 v_idx = i40e_vsi_mem_alloc(pf, type);
7456 if (v_idx < 0)
7457 goto err_alloc;
7458 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007459 if (!vsi)
7460 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007461 vsi->type = type;
7462 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
7463
7464 if (type == I40E_VSI_MAIN)
7465 pf->lan_vsi = v_idx;
7466 else if (type == I40E_VSI_SRIOV)
7467 vsi->vf_id = param1;
7468 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007469 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
7470 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007471 if (ret < 0) {
7472 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
7473 vsi->seid, ret);
7474 goto err_vsi;
7475 }
7476 vsi->base_queue = ret;
7477
7478 /* get a VSI from the hardware */
7479 vsi->uplink_seid = uplink_seid;
7480 ret = i40e_add_vsi(vsi);
7481 if (ret)
7482 goto err_vsi;
7483
7484 switch (vsi->type) {
7485 /* setup the netdev if needed */
7486 case I40E_VSI_MAIN:
7487 case I40E_VSI_VMDQ2:
7488 ret = i40e_config_netdev(vsi);
7489 if (ret)
7490 goto err_netdev;
7491 ret = register_netdev(vsi->netdev);
7492 if (ret)
7493 goto err_netdev;
7494 vsi->netdev_registered = true;
7495 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007496#ifdef CONFIG_I40E_DCB
7497 /* Setup DCB netlink interface */
7498 i40e_dcbnl_setup(vsi);
7499#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007500 /* fall through */
7501
7502 case I40E_VSI_FDIR:
7503 /* set up vectors and rings if needed */
7504 ret = i40e_vsi_setup_vectors(vsi);
7505 if (ret)
7506 goto err_msix;
7507
7508 ret = i40e_alloc_rings(vsi);
7509 if (ret)
7510 goto err_rings;
7511
7512 /* map all of the rings to the q_vectors */
7513 i40e_vsi_map_rings_to_vectors(vsi);
7514
7515 i40e_vsi_reset_stats(vsi);
7516 break;
7517
7518 default:
7519 /* no netdev or rings for the other VSI types */
7520 break;
7521 }
7522
7523 return vsi;
7524
7525err_rings:
7526 i40e_vsi_free_q_vectors(vsi);
7527err_msix:
7528 if (vsi->netdev_registered) {
7529 vsi->netdev_registered = false;
7530 unregister_netdev(vsi->netdev);
7531 free_netdev(vsi->netdev);
7532 vsi->netdev = NULL;
7533 }
7534err_netdev:
7535 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
7536err_vsi:
7537 i40e_vsi_clear(vsi);
7538err_alloc:
7539 return NULL;
7540}
7541
7542/**
7543 * i40e_veb_get_bw_info - Query VEB BW information
7544 * @veb: the veb to query
7545 *
7546 * Query the Tx scheduler BW configuration data for given VEB
7547 **/
7548static int i40e_veb_get_bw_info(struct i40e_veb *veb)
7549{
7550 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
7551 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
7552 struct i40e_pf *pf = veb->pf;
7553 struct i40e_hw *hw = &pf->hw;
7554 u32 tc_bw_max;
7555 int ret = 0;
7556 int i;
7557
7558 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
7559 &bw_data, NULL);
7560 if (ret) {
7561 dev_info(&pf->pdev->dev,
7562 "query veb bw config failed, aq_err=%d\n",
7563 hw->aq.asq_last_status);
7564 goto out;
7565 }
7566
7567 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
7568 &ets_data, NULL);
7569 if (ret) {
7570 dev_info(&pf->pdev->dev,
7571 "query veb bw ets config failed, aq_err=%d\n",
7572 hw->aq.asq_last_status);
7573 goto out;
7574 }
7575
7576 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
7577 veb->bw_max_quanta = ets_data.tc_bw_max;
7578 veb->is_abs_credits = bw_data.absolute_credits_enable;
7579 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
7580 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
7581 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7582 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
7583 veb->bw_tc_limit_credits[i] =
7584 le16_to_cpu(bw_data.tc_bw_limits[i]);
7585 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
7586 }
7587
7588out:
7589 return ret;
7590}
7591
7592/**
7593 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
7594 * @pf: board private structure
7595 *
7596 * On error: returns error code (negative)
7597 * On success: returns vsi index in PF (positive)
7598 **/
7599static int i40e_veb_mem_alloc(struct i40e_pf *pf)
7600{
7601 int ret = -ENOENT;
7602 struct i40e_veb *veb;
7603 int i;
7604
7605 /* Need to protect the allocation of switch elements at the PF level */
7606 mutex_lock(&pf->switch_mutex);
7607
7608 /* VEB list may be fragmented if VEB creation/destruction has
7609 * been happening. We can afford to do a quick scan to look
7610 * for any free slots in the list.
7611 *
7612 * find next empty veb slot, looping back around if necessary
7613 */
7614 i = 0;
7615 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
7616 i++;
7617 if (i >= I40E_MAX_VEB) {
7618 ret = -ENOMEM;
7619 goto err_alloc_veb; /* out of VEB slots! */
7620 }
7621
7622 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
7623 if (!veb) {
7624 ret = -ENOMEM;
7625 goto err_alloc_veb;
7626 }
7627 veb->pf = pf;
7628 veb->idx = i;
7629 veb->enabled_tc = 1;
7630
7631 pf->veb[i] = veb;
7632 ret = i;
7633err_alloc_veb:
7634 mutex_unlock(&pf->switch_mutex);
7635 return ret;
7636}
7637
7638/**
7639 * i40e_switch_branch_release - Delete a branch of the switch tree
7640 * @branch: where to start deleting
7641 *
7642 * This uses recursion to find the tips of the branch to be
7643 * removed, deleting until we get back to and can delete this VEB.
7644 **/
7645static void i40e_switch_branch_release(struct i40e_veb *branch)
7646{
7647 struct i40e_pf *pf = branch->pf;
7648 u16 branch_seid = branch->seid;
7649 u16 veb_idx = branch->idx;
7650 int i;
7651
7652 /* release any VEBs on this VEB - RECURSION */
7653 for (i = 0; i < I40E_MAX_VEB; i++) {
7654 if (!pf->veb[i])
7655 continue;
7656 if (pf->veb[i]->uplink_seid == branch->seid)
7657 i40e_switch_branch_release(pf->veb[i]);
7658 }
7659
7660 /* Release the VSIs on this VEB, but not the owner VSI.
7661 *
7662 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
7663 * the VEB itself, so don't use (*branch) after this loop.
7664 */
Mitch Williams505682c2014-05-20 08:01:37 +00007665 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007666 if (!pf->vsi[i])
7667 continue;
7668 if (pf->vsi[i]->uplink_seid == branch_seid &&
7669 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
7670 i40e_vsi_release(pf->vsi[i]);
7671 }
7672 }
7673
7674 /* There's one corner case where the VEB might not have been
7675 * removed, so double check it here and remove it if needed.
7676 * This case happens if the veb was created from the debugfs
7677 * commands and no VSIs were added to it.
7678 */
7679 if (pf->veb[veb_idx])
7680 i40e_veb_release(pf->veb[veb_idx]);
7681}
7682
7683/**
7684 * i40e_veb_clear - remove veb struct
7685 * @veb: the veb to remove
7686 **/
7687static void i40e_veb_clear(struct i40e_veb *veb)
7688{
7689 if (!veb)
7690 return;
7691
7692 if (veb->pf) {
7693 struct i40e_pf *pf = veb->pf;
7694
7695 mutex_lock(&pf->switch_mutex);
7696 if (pf->veb[veb->idx] == veb)
7697 pf->veb[veb->idx] = NULL;
7698 mutex_unlock(&pf->switch_mutex);
7699 }
7700
7701 kfree(veb);
7702}
7703
7704/**
7705 * i40e_veb_release - Delete a VEB and free its resources
7706 * @veb: the VEB being removed
7707 **/
7708void i40e_veb_release(struct i40e_veb *veb)
7709{
7710 struct i40e_vsi *vsi = NULL;
7711 struct i40e_pf *pf;
7712 int i, n = 0;
7713
7714 pf = veb->pf;
7715
7716 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +00007717 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007718 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
7719 n++;
7720 vsi = pf->vsi[i];
7721 }
7722 }
7723 if (n != 1) {
7724 dev_info(&pf->pdev->dev,
7725 "can't remove VEB %d with %d VSIs left\n",
7726 veb->seid, n);
7727 return;
7728 }
7729
7730 /* move the remaining VSI to uplink veb */
7731 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
7732 if (veb->uplink_seid) {
7733 vsi->uplink_seid = veb->uplink_seid;
7734 if (veb->uplink_seid == pf->mac_seid)
7735 vsi->veb_idx = I40E_NO_VEB;
7736 else
7737 vsi->veb_idx = veb->veb_idx;
7738 } else {
7739 /* floating VEB */
7740 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
7741 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
7742 }
7743
7744 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
7745 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007746}
7747
7748/**
7749 * i40e_add_veb - create the VEB in the switch
7750 * @veb: the VEB to be instantiated
7751 * @vsi: the controlling VSI
7752 **/
7753static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
7754{
Greg Rose56747262013-11-28 06:39:37 +00007755 bool is_default = false;
Kevin Scotte1c51b952013-11-20 10:02:51 +00007756 bool is_cloud = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007757 int ret;
7758
7759 /* get a VEB from the hardware */
7760 ret = i40e_aq_add_veb(&veb->pf->hw, veb->uplink_seid, vsi->seid,
Kevin Scotte1c51b952013-11-20 10:02:51 +00007761 veb->enabled_tc, is_default,
7762 is_cloud, &veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007763 if (ret) {
7764 dev_info(&veb->pf->pdev->dev,
7765 "couldn't add VEB, err %d, aq_err %d\n",
7766 ret, veb->pf->hw.aq.asq_last_status);
7767 return -EPERM;
7768 }
7769
7770 /* get statistics counter */
7771 ret = i40e_aq_get_veb_parameters(&veb->pf->hw, veb->seid, NULL, NULL,
7772 &veb->stats_idx, NULL, NULL, NULL);
7773 if (ret) {
7774 dev_info(&veb->pf->pdev->dev,
7775 "couldn't get VEB statistics idx, err %d, aq_err %d\n",
7776 ret, veb->pf->hw.aq.asq_last_status);
7777 return -EPERM;
7778 }
7779 ret = i40e_veb_get_bw_info(veb);
7780 if (ret) {
7781 dev_info(&veb->pf->pdev->dev,
7782 "couldn't get VEB bw info, err %d, aq_err %d\n",
7783 ret, veb->pf->hw.aq.asq_last_status);
7784 i40e_aq_delete_element(&veb->pf->hw, veb->seid, NULL);
7785 return -ENOENT;
7786 }
7787
7788 vsi->uplink_seid = veb->seid;
7789 vsi->veb_idx = veb->idx;
7790 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
7791
7792 return 0;
7793}
7794
7795/**
7796 * i40e_veb_setup - Set up a VEB
7797 * @pf: board private structure
7798 * @flags: VEB setup flags
7799 * @uplink_seid: the switch element to link to
7800 * @vsi_seid: the initial VSI seid
7801 * @enabled_tc: Enabled TC bit-map
7802 *
7803 * This allocates the sw VEB structure and links it into the switch
7804 * It is possible and legal for this to be a duplicate of an already
7805 * existing VEB. It is also possible for both uplink and vsi seids
7806 * to be zero, in order to create a floating VEB.
7807 *
7808 * Returns pointer to the successfully allocated VEB sw struct on
7809 * success, otherwise returns NULL on failure.
7810 **/
7811struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
7812 u16 uplink_seid, u16 vsi_seid,
7813 u8 enabled_tc)
7814{
7815 struct i40e_veb *veb, *uplink_veb = NULL;
7816 int vsi_idx, veb_idx;
7817 int ret;
7818
7819 /* if one seid is 0, the other must be 0 to create a floating relay */
7820 if ((uplink_seid == 0 || vsi_seid == 0) &&
7821 (uplink_seid + vsi_seid != 0)) {
7822 dev_info(&pf->pdev->dev,
7823 "one, not both seid's are 0: uplink=%d vsi=%d\n",
7824 uplink_seid, vsi_seid);
7825 return NULL;
7826 }
7827
7828 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +00007829 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007830 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
7831 break;
Mitch Williams505682c2014-05-20 08:01:37 +00007832 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007833 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
7834 vsi_seid);
7835 return NULL;
7836 }
7837
7838 if (uplink_seid && uplink_seid != pf->mac_seid) {
7839 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
7840 if (pf->veb[veb_idx] &&
7841 pf->veb[veb_idx]->seid == uplink_seid) {
7842 uplink_veb = pf->veb[veb_idx];
7843 break;
7844 }
7845 }
7846 if (!uplink_veb) {
7847 dev_info(&pf->pdev->dev,
7848 "uplink seid %d not found\n", uplink_seid);
7849 return NULL;
7850 }
7851 }
7852
7853 /* get veb sw struct */
7854 veb_idx = i40e_veb_mem_alloc(pf);
7855 if (veb_idx < 0)
7856 goto err_alloc;
7857 veb = pf->veb[veb_idx];
7858 veb->flags = flags;
7859 veb->uplink_seid = uplink_seid;
7860 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
7861 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
7862
7863 /* create the VEB in the switch */
7864 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
7865 if (ret)
7866 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +00007867 if (vsi_idx == pf->lan_vsi)
7868 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007869
7870 return veb;
7871
7872err_veb:
7873 i40e_veb_clear(veb);
7874err_alloc:
7875 return NULL;
7876}
7877
7878/**
7879 * i40e_setup_pf_switch_element - set pf vars based on switch type
7880 * @pf: board private structure
7881 * @ele: element we are building info from
7882 * @num_reported: total number of elements
7883 * @printconfig: should we print the contents
7884 *
7885 * helper function to assist in extracting a few useful SEID values.
7886 **/
7887static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
7888 struct i40e_aqc_switch_config_element_resp *ele,
7889 u16 num_reported, bool printconfig)
7890{
7891 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
7892 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
7893 u8 element_type = ele->element_type;
7894 u16 seid = le16_to_cpu(ele->seid);
7895
7896 if (printconfig)
7897 dev_info(&pf->pdev->dev,
7898 "type=%d seid=%d uplink=%d downlink=%d\n",
7899 element_type, seid, uplink_seid, downlink_seid);
7900
7901 switch (element_type) {
7902 case I40E_SWITCH_ELEMENT_TYPE_MAC:
7903 pf->mac_seid = seid;
7904 break;
7905 case I40E_SWITCH_ELEMENT_TYPE_VEB:
7906 /* Main VEB? */
7907 if (uplink_seid != pf->mac_seid)
7908 break;
7909 if (pf->lan_veb == I40E_NO_VEB) {
7910 int v;
7911
7912 /* find existing or else empty VEB */
7913 for (v = 0; v < I40E_MAX_VEB; v++) {
7914 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
7915 pf->lan_veb = v;
7916 break;
7917 }
7918 }
7919 if (pf->lan_veb == I40E_NO_VEB) {
7920 v = i40e_veb_mem_alloc(pf);
7921 if (v < 0)
7922 break;
7923 pf->lan_veb = v;
7924 }
7925 }
7926
7927 pf->veb[pf->lan_veb]->seid = seid;
7928 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
7929 pf->veb[pf->lan_veb]->pf = pf;
7930 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
7931 break;
7932 case I40E_SWITCH_ELEMENT_TYPE_VSI:
7933 if (num_reported != 1)
7934 break;
7935 /* This is immediately after a reset so we can assume this is
7936 * the PF's VSI
7937 */
7938 pf->mac_seid = uplink_seid;
7939 pf->pf_seid = downlink_seid;
7940 pf->main_vsi_seid = seid;
7941 if (printconfig)
7942 dev_info(&pf->pdev->dev,
7943 "pf_seid=%d main_vsi_seid=%d\n",
7944 pf->pf_seid, pf->main_vsi_seid);
7945 break;
7946 case I40E_SWITCH_ELEMENT_TYPE_PF:
7947 case I40E_SWITCH_ELEMENT_TYPE_VF:
7948 case I40E_SWITCH_ELEMENT_TYPE_EMP:
7949 case I40E_SWITCH_ELEMENT_TYPE_BMC:
7950 case I40E_SWITCH_ELEMENT_TYPE_PE:
7951 case I40E_SWITCH_ELEMENT_TYPE_PA:
7952 /* ignore these for now */
7953 break;
7954 default:
7955 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
7956 element_type, seid);
7957 break;
7958 }
7959}
7960
7961/**
7962 * i40e_fetch_switch_configuration - Get switch config from firmware
7963 * @pf: board private structure
7964 * @printconfig: should we print the contents
7965 *
7966 * Get the current switch configuration from the device and
7967 * extract a few useful SEID values.
7968 **/
7969int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
7970{
7971 struct i40e_aqc_get_switch_config_resp *sw_config;
7972 u16 next_seid = 0;
7973 int ret = 0;
7974 u8 *aq_buf;
7975 int i;
7976
7977 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
7978 if (!aq_buf)
7979 return -ENOMEM;
7980
7981 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
7982 do {
7983 u16 num_reported, num_total;
7984
7985 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
7986 I40E_AQ_LARGE_BUF,
7987 &next_seid, NULL);
7988 if (ret) {
7989 dev_info(&pf->pdev->dev,
7990 "get switch config failed %d aq_err=%x\n",
7991 ret, pf->hw.aq.asq_last_status);
7992 kfree(aq_buf);
7993 return -ENOENT;
7994 }
7995
7996 num_reported = le16_to_cpu(sw_config->header.num_reported);
7997 num_total = le16_to_cpu(sw_config->header.num_total);
7998
7999 if (printconfig)
8000 dev_info(&pf->pdev->dev,
8001 "header: %d reported %d total\n",
8002 num_reported, num_total);
8003
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008004 for (i = 0; i < num_reported; i++) {
8005 struct i40e_aqc_switch_config_element_resp *ele =
8006 &sw_config->element[i];
8007
8008 i40e_setup_pf_switch_element(pf, ele, num_reported,
8009 printconfig);
8010 }
8011 } while (next_seid != 0);
8012
8013 kfree(aq_buf);
8014 return ret;
8015}
8016
8017/**
8018 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
8019 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008020 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008021 *
8022 * Returns 0 on success, negative value on failure
8023 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008024static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008025{
Jesse Brandeburg895106a2013-11-26 10:49:16 +00008026 u32 rxfc = 0, txfc = 0, rxfc_reg;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008027 int ret;
8028
8029 /* find out what's out there already */
8030 ret = i40e_fetch_switch_configuration(pf, false);
8031 if (ret) {
8032 dev_info(&pf->pdev->dev,
8033 "couldn't fetch switch config, err %d, aq_err %d\n",
8034 ret, pf->hw.aq.asq_last_status);
8035 return ret;
8036 }
8037 i40e_pf_reset_stats(pf);
8038
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008039 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008040 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008041 struct i40e_vsi *vsi = NULL;
8042 u16 uplink_seid;
8043
8044 /* Set up the PF VSI associated with the PF's main VSI
8045 * that is already in the HW switch
8046 */
8047 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
8048 uplink_seid = pf->veb[pf->lan_veb]->seid;
8049 else
8050 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008051 if (pf->lan_vsi == I40E_NO_VSI)
8052 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
8053 else if (reinit)
8054 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008055 if (!vsi) {
8056 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
8057 i40e_fdir_teardown(pf);
8058 return -EAGAIN;
8059 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008060 } else {
8061 /* force a reset of TC and queue layout configurations */
8062 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
8063 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
8064 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
8065 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
8066 }
8067 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
8068
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008069 i40e_fdir_sb_setup(pf);
8070
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008071 /* Setup static PF queue filter control settings */
8072 ret = i40e_setup_pf_filter_control(pf);
8073 if (ret) {
8074 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
8075 ret);
8076 /* Failure here should not stop continuing other steps */
8077 }
8078
8079 /* enable RSS in the HW, even for only one queue, as the stack can use
8080 * the hash
8081 */
8082 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
8083 i40e_config_rss(pf);
8084
8085 /* fill in link information and enable LSE reporting */
8086 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
8087 i40e_link_event(pf);
8088
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008089 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008090 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
8091 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008092 /* requested_mode is set in probe or by ethtool */
8093 if (!pf->fc_autoneg_status)
8094 goto no_autoneg;
8095
8096 if ((pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX) &&
8097 (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008098 pf->hw.fc.current_mode = I40E_FC_FULL;
8099 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX)
8100 pf->hw.fc.current_mode = I40E_FC_TX_PAUSE;
8101 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX)
8102 pf->hw.fc.current_mode = I40E_FC_RX_PAUSE;
8103 else
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008104 pf->hw.fc.current_mode = I40E_FC_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008105
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008106 /* sync the flow control settings with the auto-neg values */
8107 switch (pf->hw.fc.current_mode) {
8108 case I40E_FC_FULL:
8109 txfc = 1;
8110 rxfc = 1;
8111 break;
8112 case I40E_FC_TX_PAUSE:
8113 txfc = 1;
8114 rxfc = 0;
8115 break;
8116 case I40E_FC_RX_PAUSE:
8117 txfc = 0;
8118 rxfc = 1;
8119 break;
8120 case I40E_FC_NONE:
8121 case I40E_FC_DEFAULT:
8122 txfc = 0;
8123 rxfc = 0;
8124 break;
8125 case I40E_FC_PFC:
8126 /* TBD */
8127 break;
8128 /* no default case, we have to handle all possibilities here */
8129 }
8130
8131 wr32(&pf->hw, I40E_PRTDCB_FCCFG, txfc << I40E_PRTDCB_FCCFG_TFCE_SHIFT);
8132
8133 rxfc_reg = rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
8134 ~I40E_PRTDCB_MFLCN_RFCE_MASK;
8135 rxfc_reg |= (rxfc << I40E_PRTDCB_MFLCN_RFCE_SHIFT);
8136
8137 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rxfc_reg);
8138
8139 goto fc_complete;
8140
8141no_autoneg:
8142 /* disable L2 flow control, user can turn it on if they wish */
8143 wr32(&pf->hw, I40E_PRTDCB_FCCFG, 0);
8144 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
8145 ~I40E_PRTDCB_MFLCN_RFCE_MASK);
8146
8147fc_complete:
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008148 i40e_ptp_init(pf);
8149
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008150 return ret;
8151}
8152
8153/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008154 * i40e_determine_queue_usage - Work out queue distribution
8155 * @pf: board private structure
8156 **/
8157static void i40e_determine_queue_usage(struct i40e_pf *pf)
8158{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008159 int queues_left;
8160
8161 pf->num_lan_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008162
8163 /* Find the max queues to be put into basic use. We'll always be
8164 * using TC0, whether or not DCB is running, and TC0 will get the
8165 * big RSS set.
8166 */
8167 queues_left = pf->hw.func_caps.num_tx_qp;
8168
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008169 if ((queues_left == 1) ||
8170 !(pf->flags & I40E_FLAG_MSIX_ENABLED) ||
8171 !(pf->flags & (I40E_FLAG_RSS_ENABLED | I40E_FLAG_FD_SB_ENABLED |
8172 I40E_FLAG_DCB_ENABLED))) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008173 /* one qp for PF, no queues for anything else */
8174 queues_left = 0;
8175 pf->rss_size = pf->num_lan_qps = 1;
8176
8177 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008178 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
8179 I40E_FLAG_FD_SB_ENABLED |
8180 I40E_FLAG_FD_ATR_ENABLED |
8181 I40E_FLAG_DCB_ENABLED |
8182 I40E_FLAG_SRIOV_ENABLED |
8183 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008184 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008185 /* Not enough queues for all TCs */
8186 if ((pf->flags & I40E_FLAG_DCB_ENABLED) &&
8187 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
8188 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8189 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
8190 }
8191 pf->num_lan_qps = pf->rss_size_max;
8192 queues_left -= pf->num_lan_qps;
8193 }
8194
8195 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8196 if (queues_left > 1) {
8197 queues_left -= 1; /* save 1 queue for FD */
8198 } else {
8199 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8200 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
8201 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008202 }
8203
8204 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
8205 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008206 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
8207 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008208 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
8209 }
8210
8211 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
8212 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
8213 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
8214 (queues_left / pf->num_vmdq_qps));
8215 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
8216 }
8217
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008218 pf->queues_left = queues_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008219}
8220
8221/**
8222 * i40e_setup_pf_filter_control - Setup PF static filter control
8223 * @pf: PF to be setup
8224 *
8225 * i40e_setup_pf_filter_control sets up a pf's initial filter control
8226 * settings. If PE/FCoE are enabled then it will also set the per PF
8227 * based filter sizes required for them. It also enables Flow director,
8228 * ethertype and macvlan type filter settings for the pf.
8229 *
8230 * Returns 0 on success, negative on failure
8231 **/
8232static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
8233{
8234 struct i40e_filter_control_settings *settings = &pf->filter_settings;
8235
8236 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
8237
8238 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008239 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008240 settings->enable_fdir = true;
8241
8242 /* Ethtype and MACVLAN filters enabled for PF */
8243 settings->enable_ethtype = true;
8244 settings->enable_macvlan = true;
8245
8246 if (i40e_set_filter_control(&pf->hw, settings))
8247 return -ENOENT;
8248
8249 return 0;
8250}
8251
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008252#define INFO_STRING_LEN 255
8253static void i40e_print_features(struct i40e_pf *pf)
8254{
8255 struct i40e_hw *hw = &pf->hw;
8256 char *buf, *string;
8257
8258 string = kzalloc(INFO_STRING_LEN, GFP_KERNEL);
8259 if (!string) {
8260 dev_err(&pf->pdev->dev, "Features string allocation failed\n");
8261 return;
8262 }
8263
8264 buf = string;
8265
8266 buf += sprintf(string, "Features: PF-id[%d] ", hw->pf_id);
8267#ifdef CONFIG_PCI_IOV
8268 buf += sprintf(buf, "VFs: %d ", pf->num_req_vfs);
8269#endif
8270 buf += sprintf(buf, "VSIs: %d QP: %d ", pf->hw.func_caps.num_vsis,
8271 pf->vsi[pf->lan_vsi]->num_queue_pairs);
8272
8273 if (pf->flags & I40E_FLAG_RSS_ENABLED)
8274 buf += sprintf(buf, "RSS ");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008275 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +00008276 buf += sprintf(buf, "FD_ATR ");
8277 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8278 buf += sprintf(buf, "FD_SB ");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008279 buf += sprintf(buf, "NTUPLE ");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +00008280 }
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008281 if (pf->flags & I40E_FLAG_DCB_ENABLED)
8282 buf += sprintf(buf, "DCB ");
8283 if (pf->flags & I40E_FLAG_PTP)
8284 buf += sprintf(buf, "PTP ");
8285
8286 BUG_ON(buf > (string + INFO_STRING_LEN));
8287 dev_info(&pf->pdev->dev, "%s\n", string);
8288 kfree(string);
8289}
8290
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008291/**
8292 * i40e_probe - Device initialization routine
8293 * @pdev: PCI device information struct
8294 * @ent: entry in i40e_pci_tbl
8295 *
8296 * i40e_probe initializes a pf identified by a pci_dev structure.
8297 * The OS initialization, configuring of the pf private structure,
8298 * and a hardware reset occur.
8299 *
8300 * Returns 0 on success, negative on failure
8301 **/
8302static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
8303{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008304 struct i40e_pf *pf;
8305 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008306 static u16 pfs_found;
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008307 u16 link_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008308 int err = 0;
8309 u32 len;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008310 u32 i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008311
8312 err = pci_enable_device_mem(pdev);
8313 if (err)
8314 return err;
8315
8316 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +00008317 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00008318 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00008319 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
8320 if (err) {
8321 dev_err(&pdev->dev,
8322 "DMA configuration failed: 0x%x\n", err);
8323 goto err_dma;
8324 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008325 }
8326
8327 /* set up pci connections */
8328 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
8329 IORESOURCE_MEM), i40e_driver_name);
8330 if (err) {
8331 dev_info(&pdev->dev,
8332 "pci_request_selected_regions failed %d\n", err);
8333 goto err_pci_reg;
8334 }
8335
8336 pci_enable_pcie_error_reporting(pdev);
8337 pci_set_master(pdev);
8338
8339 /* Now that we have a PCI connection, we need to do the
8340 * low level device setup. This is primarily setting up
8341 * the Admin Queue structures and then querying for the
8342 * device's current profile information.
8343 */
8344 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
8345 if (!pf) {
8346 err = -ENOMEM;
8347 goto err_pf_alloc;
8348 }
8349 pf->next_vsi = 0;
8350 pf->pdev = pdev;
8351 set_bit(__I40E_DOWN, &pf->state);
8352
8353 hw = &pf->hw;
8354 hw->back = pf;
8355 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
8356 pci_resource_len(pdev, 0));
8357 if (!hw->hw_addr) {
8358 err = -EIO;
8359 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
8360 (unsigned int)pci_resource_start(pdev, 0),
8361 (unsigned int)pci_resource_len(pdev, 0), err);
8362 goto err_ioremap;
8363 }
8364 hw->vendor_id = pdev->vendor;
8365 hw->device_id = pdev->device;
8366 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
8367 hw->subsystem_vendor_id = pdev->subsystem_vendor;
8368 hw->subsystem_device_id = pdev->subsystem_device;
8369 hw->bus.device = PCI_SLOT(pdev->devfn);
8370 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008371 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008372
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008373 /* do a special CORER for clearing PXE mode once at init */
8374 if (hw->revision_id == 0 &&
8375 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
8376 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
8377 i40e_flush(hw);
8378 msleep(200);
8379 pf->corer_count++;
8380
8381 i40e_clear_pxe_mode(hw);
8382 }
8383
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008384 /* Reset here to make sure all is clean and to define PF 'n' */
8385 err = i40e_pf_reset(hw);
8386 if (err) {
8387 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
8388 goto err_pf_reset;
8389 }
8390 pf->pfr_count++;
8391
8392 hw->aq.num_arq_entries = I40E_AQ_LEN;
8393 hw->aq.num_asq_entries = I40E_AQ_LEN;
8394 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
8395 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
8396 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
8397 snprintf(pf->misc_int_name, sizeof(pf->misc_int_name) - 1,
8398 "%s-pf%d:misc",
8399 dev_driver_string(&pf->pdev->dev), pf->hw.pf_id);
8400
8401 err = i40e_init_shared_code(hw);
8402 if (err) {
8403 dev_info(&pdev->dev, "init_shared_code failed: %d\n", err);
8404 goto err_pf_reset;
8405 }
8406
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008407 /* set up a default setting for link flow control */
8408 pf->hw.fc.requested_mode = I40E_FC_NONE;
8409
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008410 err = i40e_init_adminq(hw);
8411 dev_info(&pdev->dev, "%s\n", i40e_fw_version_str(hw));
8412 if (err) {
8413 dev_info(&pdev->dev,
8414 "init_adminq failed: %d expecting API %02x.%02x\n",
8415 err,
8416 I40E_FW_API_VERSION_MAJOR, I40E_FW_API_VERSION_MINOR);
8417 goto err_pf_reset;
8418 }
8419
Shannon Nelson4eb3f762014-03-06 08:59:58 +00008420 i40e_verify_eeprom(pf);
8421
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +00008422 /* Rev 0 hardware was never productized */
8423 if (hw->revision_id < 1)
8424 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");
8425
Shannon Nelson6ff4ef82013-12-21 05:44:49 +00008426 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008427 err = i40e_get_capabilities(pf);
8428 if (err)
8429 goto err_adminq_setup;
8430
8431 err = i40e_sw_init(pf);
8432 if (err) {
8433 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
8434 goto err_sw_init;
8435 }
8436
8437 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
8438 hw->func_caps.num_rx_qp,
8439 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
8440 if (err) {
8441 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
8442 goto err_init_lan_hmc;
8443 }
8444
8445 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
8446 if (err) {
8447 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
8448 err = -ENOENT;
8449 goto err_configure_lan_hmc;
8450 }
8451
8452 i40e_get_mac_addr(hw, hw->mac.addr);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +00008453 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008454 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
8455 err = -EIO;
8456 goto err_mac_addr;
8457 }
8458 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
8459 memcpy(hw->mac.perm_addr, hw->mac.addr, ETH_ALEN);
8460
8461 pci_set_drvdata(pdev, pf);
8462 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008463#ifdef CONFIG_I40E_DCB
8464 err = i40e_init_pf_dcb(pf);
8465 if (err) {
8466 dev_info(&pdev->dev, "init_pf_dcb failed: %d\n", err);
8467 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh014269f2014-04-01 07:11:48 +00008468 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008469 }
8470#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008471
8472 /* set up periodic task facility */
8473 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
8474 pf->service_timer_period = HZ;
8475
8476 INIT_WORK(&pf->service_task, i40e_service_task);
8477 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
8478 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
8479 pf->link_check_timeout = jiffies;
8480
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008481 /* WoL defaults to disabled */
8482 pf->wol_en = false;
8483 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
8484
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008485 /* set up the main switch operations */
8486 i40e_determine_queue_usage(pf);
8487 i40e_init_interrupt_scheme(pf);
8488
Mitch Williams505682c2014-05-20 08:01:37 +00008489 /* The number of VSIs reported by the FW is the minimum guaranteed
8490 * to us; HW supports far more and we share the remaining pool with
8491 * the other PFs. We allocate space for more than the guarantee with
8492 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008493 */
Mitch Williams505682c2014-05-20 08:01:37 +00008494 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
8495 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
8496 else
8497 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
8498
8499 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
8500 len = sizeof(struct i40e_vsi *) * pf->num_alloc_vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008501 pf->vsi = kzalloc(len, GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +00008502 if (!pf->vsi) {
8503 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008504 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +00008505 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008506
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008507 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008508 if (err) {
8509 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
8510 goto err_vsis;
8511 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008512 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +00008513 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008514 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
8515 i40e_vsi_open(pf->vsi[i]);
8516 break;
8517 }
8518 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008519
8520 /* The main driver is (mostly) up and happy. We need to set this state
8521 * before setting up the misc vector or we get a race and the vector
8522 * ends up disabled forever.
8523 */
8524 clear_bit(__I40E_DOWN, &pf->state);
8525
8526 /* In case of MSIX we are going to setup the misc vector right here
8527 * to handle admin queue events etc. In case of legacy and MSI
8528 * the misc functionality and queue processing is combined in
8529 * the same vector and that gets setup at open.
8530 */
8531 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
8532 err = i40e_setup_misc_vector(pf);
8533 if (err) {
8534 dev_info(&pdev->dev,
8535 "setup of misc vector failed: %d\n", err);
8536 goto err_vsis;
8537 }
8538 }
8539
Greg Rosedf805f62014-04-04 04:43:16 +00008540#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008541 /* prep for VF support */
8542 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +00008543 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
8544 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008545 u32 val;
8546
8547 /* disable link interrupts for VFs */
8548 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
8549 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
8550 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
8551 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -08008552
8553 if (pci_num_vf(pdev)) {
8554 dev_info(&pdev->dev,
8555 "Active VFs found, allocating resources.\n");
8556 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
8557 if (err)
8558 dev_info(&pdev->dev,
8559 "Error %d allocating resources for existing VFs\n",
8560 err);
8561 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008562 }
Greg Rosedf805f62014-04-04 04:43:16 +00008563#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008564
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008565 pfs_found++;
8566
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008567 i40e_dbg_pf_init(pf);
8568
8569 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00008570 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008571
8572 /* since everything's happy, start the service_task timer */
8573 mod_timer(&pf->service_timer,
8574 round_jiffies(jiffies + pf->service_timer_period));
8575
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008576 /* Get the negotiated link width and speed from PCI config space */
8577 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA, &link_status);
8578
8579 i40e_set_pci_config_data(hw, link_status);
8580
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00008581 dev_info(&pdev->dev, "PCI-Express: %s %s\n",
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008582 (hw->bus.speed == i40e_bus_speed_8000 ? "Speed 8.0GT/s" :
8583 hw->bus.speed == i40e_bus_speed_5000 ? "Speed 5.0GT/s" :
8584 hw->bus.speed == i40e_bus_speed_2500 ? "Speed 2.5GT/s" :
8585 "Unknown"),
8586 (hw->bus.width == i40e_bus_width_pcie_x8 ? "Width x8" :
8587 hw->bus.width == i40e_bus_width_pcie_x4 ? "Width x4" :
8588 hw->bus.width == i40e_bus_width_pcie_x2 ? "Width x2" :
8589 hw->bus.width == i40e_bus_width_pcie_x1 ? "Width x1" :
8590 "Unknown"));
8591
8592 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
8593 hw->bus.speed < i40e_bus_speed_8000) {
8594 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
8595 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
8596 }
8597
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008598 /* print a string summarizing features */
8599 i40e_print_features(pf);
8600
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008601 return 0;
8602
8603 /* Unwind what we've done if something failed in the setup */
8604err_vsis:
8605 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008606 i40e_clear_interrupt_scheme(pf);
8607 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +00008608err_switch_setup:
8609 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008610 del_timer_sync(&pf->service_timer);
8611err_mac_addr:
8612err_configure_lan_hmc:
8613 (void)i40e_shutdown_lan_hmc(hw);
8614err_init_lan_hmc:
8615 kfree(pf->qp_pile);
8616 kfree(pf->irq_pile);
8617err_sw_init:
8618err_adminq_setup:
8619 (void)i40e_shutdown_adminq(hw);
8620err_pf_reset:
8621 iounmap(hw->hw_addr);
8622err_ioremap:
8623 kfree(pf);
8624err_pf_alloc:
8625 pci_disable_pcie_error_reporting(pdev);
8626 pci_release_selected_regions(pdev,
8627 pci_select_bars(pdev, IORESOURCE_MEM));
8628err_pci_reg:
8629err_dma:
8630 pci_disable_device(pdev);
8631 return err;
8632}
8633
8634/**
8635 * i40e_remove - Device removal routine
8636 * @pdev: PCI device information struct
8637 *
8638 * i40e_remove is called by the PCI subsystem to alert the driver
8639 * that is should release a PCI device. This could be caused by a
8640 * Hot-Plug event, or because the driver is going to be removed from
8641 * memory.
8642 **/
8643static void i40e_remove(struct pci_dev *pdev)
8644{
8645 struct i40e_pf *pf = pci_get_drvdata(pdev);
8646 i40e_status ret_code;
8647 u32 reg;
8648 int i;
8649
8650 i40e_dbg_pf_exit(pf);
8651
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008652 i40e_ptp_stop(pf);
8653
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008654 /* no more scheduling of any task */
8655 set_bit(__I40E_DOWN, &pf->state);
8656 del_timer_sync(&pf->service_timer);
8657 cancel_work_sync(&pf->service_task);
8658
Mitch Williamseb2d80b2014-02-13 03:48:48 -08008659 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
8660 i40e_free_vfs(pf);
8661 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
8662 }
8663
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008664 i40e_fdir_teardown(pf);
8665
8666 /* If there is a switch structure or any orphans, remove them.
8667 * This will leave only the PF's VSI remaining.
8668 */
8669 for (i = 0; i < I40E_MAX_VEB; i++) {
8670 if (!pf->veb[i])
8671 continue;
8672
8673 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
8674 pf->veb[i]->uplink_seid == 0)
8675 i40e_switch_branch_release(pf->veb[i]);
8676 }
8677
8678 /* Now we can shutdown the PF's VSI, just before we kill
8679 * adminq and hmc.
8680 */
8681 if (pf->vsi[pf->lan_vsi])
8682 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
8683
8684 i40e_stop_misc_vector(pf);
8685 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
8686 synchronize_irq(pf->msix_entries[0].vector);
8687 free_irq(pf->msix_entries[0].vector, pf);
8688 }
8689
8690 /* shutdown and destroy the HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00008691 if (pf->hw.hmc.hmc_obj) {
8692 ret_code = i40e_shutdown_lan_hmc(&pf->hw);
8693 if (ret_code)
8694 dev_warn(&pdev->dev,
8695 "Failed to destroy the HMC resources: %d\n",
8696 ret_code);
8697 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008698
8699 /* shutdown the adminq */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008700 ret_code = i40e_shutdown_adminq(&pf->hw);
8701 if (ret_code)
8702 dev_warn(&pdev->dev,
8703 "Failed to destroy the Admin Queue resources: %d\n",
8704 ret_code);
8705
8706 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
8707 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +00008708 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008709 if (pf->vsi[i]) {
8710 i40e_vsi_clear_rings(pf->vsi[i]);
8711 i40e_vsi_clear(pf->vsi[i]);
8712 pf->vsi[i] = NULL;
8713 }
8714 }
8715
8716 for (i = 0; i < I40E_MAX_VEB; i++) {
8717 kfree(pf->veb[i]);
8718 pf->veb[i] = NULL;
8719 }
8720
8721 kfree(pf->qp_pile);
8722 kfree(pf->irq_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008723 kfree(pf->vsi);
8724
8725 /* force a PF reset to clean anything leftover */
8726 reg = rd32(&pf->hw, I40E_PFGEN_CTRL);
8727 wr32(&pf->hw, I40E_PFGEN_CTRL, (reg | I40E_PFGEN_CTRL_PFSWR_MASK));
8728 i40e_flush(&pf->hw);
8729
8730 iounmap(pf->hw.hw_addr);
8731 kfree(pf);
8732 pci_release_selected_regions(pdev,
8733 pci_select_bars(pdev, IORESOURCE_MEM));
8734
8735 pci_disable_pcie_error_reporting(pdev);
8736 pci_disable_device(pdev);
8737}
8738
8739/**
8740 * i40e_pci_error_detected - warning that something funky happened in PCI land
8741 * @pdev: PCI device information struct
8742 *
8743 * Called to warn that something happened and the error handling steps
8744 * are in progress. Allows the driver to quiesce things, be ready for
8745 * remediation.
8746 **/
8747static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
8748 enum pci_channel_state error)
8749{
8750 struct i40e_pf *pf = pci_get_drvdata(pdev);
8751
8752 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
8753
8754 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008755 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
8756 rtnl_lock();
8757 i40e_prep_for_reset(pf);
8758 rtnl_unlock();
8759 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008760
8761 /* Request a slot reset */
8762 return PCI_ERS_RESULT_NEED_RESET;
8763}
8764
8765/**
8766 * i40e_pci_error_slot_reset - a PCI slot reset just happened
8767 * @pdev: PCI device information struct
8768 *
8769 * Called to find if the driver can work with the device now that
8770 * the pci slot has been reset. If a basic connection seems good
8771 * (registers are readable and have sane content) then return a
8772 * happy little PCI_ERS_RESULT_xxx.
8773 **/
8774static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
8775{
8776 struct i40e_pf *pf = pci_get_drvdata(pdev);
8777 pci_ers_result_t result;
8778 int err;
8779 u32 reg;
8780
8781 dev_info(&pdev->dev, "%s\n", __func__);
8782 if (pci_enable_device_mem(pdev)) {
8783 dev_info(&pdev->dev,
8784 "Cannot re-enable PCI device after reset.\n");
8785 result = PCI_ERS_RESULT_DISCONNECT;
8786 } else {
8787 pci_set_master(pdev);
8788 pci_restore_state(pdev);
8789 pci_save_state(pdev);
8790 pci_wake_from_d3(pdev, false);
8791
8792 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8793 if (reg == 0)
8794 result = PCI_ERS_RESULT_RECOVERED;
8795 else
8796 result = PCI_ERS_RESULT_DISCONNECT;
8797 }
8798
8799 err = pci_cleanup_aer_uncorrect_error_status(pdev);
8800 if (err) {
8801 dev_info(&pdev->dev,
8802 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
8803 err);
8804 /* non-fatal, continue */
8805 }
8806
8807 return result;
8808}
8809
8810/**
8811 * i40e_pci_error_resume - restart operations after PCI error recovery
8812 * @pdev: PCI device information struct
8813 *
8814 * Called to allow the driver to bring things back up after PCI error
8815 * and/or reset recovery has finished.
8816 **/
8817static void i40e_pci_error_resume(struct pci_dev *pdev)
8818{
8819 struct i40e_pf *pf = pci_get_drvdata(pdev);
8820
8821 dev_info(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008822 if (test_bit(__I40E_SUSPENDED, &pf->state))
8823 return;
8824
8825 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008826 i40e_handle_reset_warning(pf);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008827 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008828}
8829
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008830/**
8831 * i40e_shutdown - PCI callback for shutting down
8832 * @pdev: PCI device information struct
8833 **/
8834static void i40e_shutdown(struct pci_dev *pdev)
8835{
8836 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008837 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008838
8839 set_bit(__I40E_SUSPENDED, &pf->state);
8840 set_bit(__I40E_DOWN, &pf->state);
8841 rtnl_lock();
8842 i40e_prep_for_reset(pf);
8843 rtnl_unlock();
8844
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008845 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
8846 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
8847
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008848 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008849 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008850 pci_set_power_state(pdev, PCI_D3hot);
8851 }
8852}
8853
8854#ifdef CONFIG_PM
8855/**
8856 * i40e_suspend - PCI callback for moving to D3
8857 * @pdev: PCI device information struct
8858 **/
8859static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
8860{
8861 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008862 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008863
8864 set_bit(__I40E_SUSPENDED, &pf->state);
8865 set_bit(__I40E_DOWN, &pf->state);
8866 rtnl_lock();
8867 i40e_prep_for_reset(pf);
8868 rtnl_unlock();
8869
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008870 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
8871 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
8872
8873 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008874 pci_set_power_state(pdev, PCI_D3hot);
8875
8876 return 0;
8877}
8878
8879/**
8880 * i40e_resume - PCI callback for waking up from D3
8881 * @pdev: PCI device information struct
8882 **/
8883static int i40e_resume(struct pci_dev *pdev)
8884{
8885 struct i40e_pf *pf = pci_get_drvdata(pdev);
8886 u32 err;
8887
8888 pci_set_power_state(pdev, PCI_D0);
8889 pci_restore_state(pdev);
8890 /* pci_restore_state() clears dev->state_saves, so
8891 * call pci_save_state() again to restore it.
8892 */
8893 pci_save_state(pdev);
8894
8895 err = pci_enable_device_mem(pdev);
8896 if (err) {
8897 dev_err(&pdev->dev,
8898 "%s: Cannot enable PCI device from suspend\n",
8899 __func__);
8900 return err;
8901 }
8902 pci_set_master(pdev);
8903
8904 /* no wakeup events while running */
8905 pci_wake_from_d3(pdev, false);
8906
8907 /* handling the reset will rebuild the device state */
8908 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
8909 clear_bit(__I40E_DOWN, &pf->state);
8910 rtnl_lock();
8911 i40e_reset_and_rebuild(pf, false);
8912 rtnl_unlock();
8913 }
8914
8915 return 0;
8916}
8917
8918#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008919static const struct pci_error_handlers i40e_err_handler = {
8920 .error_detected = i40e_pci_error_detected,
8921 .slot_reset = i40e_pci_error_slot_reset,
8922 .resume = i40e_pci_error_resume,
8923};
8924
8925static struct pci_driver i40e_driver = {
8926 .name = i40e_driver_name,
8927 .id_table = i40e_pci_tbl,
8928 .probe = i40e_probe,
8929 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008930#ifdef CONFIG_PM
8931 .suspend = i40e_suspend,
8932 .resume = i40e_resume,
8933#endif
8934 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008935 .err_handler = &i40e_err_handler,
8936 .sriov_configure = i40e_pci_sriov_configure,
8937};
8938
8939/**
8940 * i40e_init_module - Driver registration routine
8941 *
8942 * i40e_init_module is the first routine called when the driver is
8943 * loaded. All it does is register with the PCI subsystem.
8944 **/
8945static int __init i40e_init_module(void)
8946{
8947 pr_info("%s: %s - version %s\n", i40e_driver_name,
8948 i40e_driver_string, i40e_driver_version_str);
8949 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
8950 i40e_dbg_init();
8951 return pci_register_driver(&i40e_driver);
8952}
8953module_init(i40e_init_module);
8954
8955/**
8956 * i40e_exit_module - Driver exit cleanup routine
8957 *
8958 * i40e_exit_module is called just before the driver is removed
8959 * from memory.
8960 **/
8961static void __exit i40e_exit_module(void)
8962{
8963 pci_unregister_driver(&i40e_driver);
8964 i40e_dbg_exit();
8965}
8966module_exit(i40e_exit_module);