blob: 211684ce113a6667ce0c46b3ef947a031cc2239d [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
Shannon Nelsone8607ef2014-05-20 08:01:47 +000042#define DRV_VERSION_BUILD 7
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/**
Greg Rose8c27d422014-05-22 06:31:56 +00001157 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1158 * @vsi: the PF Main VSI - inappropriate for any other VSI
1159 * @macaddr: the MAC address
1160 **/
1161static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1162{
1163 struct i40e_aqc_remove_macvlan_element_data element;
1164 struct i40e_pf *pf = vsi->back;
1165 i40e_status aq_ret;
1166
1167 /* Only appropriate for the PF main VSI */
1168 if (vsi->type != I40E_VSI_MAIN)
1169 return;
1170
1171 ether_addr_copy(element.mac_addr, macaddr);
1172 element.vlan_tag = 0;
1173 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1174 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1175 aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1176 if (aq_ret)
1177 dev_err(&pf->pdev->dev, "Could not remove default MAC-VLAN\n");
1178}
1179
1180/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001181 * i40e_add_filter - Add a mac/vlan filter to the VSI
1182 * @vsi: the VSI to be searched
1183 * @macaddr: the MAC address
1184 * @vlan: the vlan
1185 * @is_vf: make sure its a vf filter, else doesn't matter
1186 * @is_netdev: make sure its a netdev filter, else doesn't matter
1187 *
1188 * Returns ptr to the filter object or NULL when no memory available.
1189 **/
1190struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1191 u8 *macaddr, s16 vlan,
1192 bool is_vf, bool is_netdev)
1193{
1194 struct i40e_mac_filter *f;
1195
1196 if (!vsi || !macaddr)
1197 return NULL;
1198
1199 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1200 if (!f) {
1201 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1202 if (!f)
1203 goto add_filter_out;
1204
1205 memcpy(f->macaddr, macaddr, ETH_ALEN);
1206 f->vlan = vlan;
1207 f->changed = true;
1208
1209 INIT_LIST_HEAD(&f->list);
1210 list_add(&f->list, &vsi->mac_filter_list);
1211 }
1212
1213 /* increment counter and add a new flag if needed */
1214 if (is_vf) {
1215 if (!f->is_vf) {
1216 f->is_vf = true;
1217 f->counter++;
1218 }
1219 } else if (is_netdev) {
1220 if (!f->is_netdev) {
1221 f->is_netdev = true;
1222 f->counter++;
1223 }
1224 } else {
1225 f->counter++;
1226 }
1227
1228 /* changed tells sync_filters_subtask to
1229 * push the filter down to the firmware
1230 */
1231 if (f->changed) {
1232 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1233 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1234 }
1235
1236add_filter_out:
1237 return f;
1238}
1239
1240/**
1241 * i40e_del_filter - Remove a mac/vlan filter from the VSI
1242 * @vsi: the VSI to be searched
1243 * @macaddr: the MAC address
1244 * @vlan: the vlan
1245 * @is_vf: make sure it's a vf filter, else doesn't matter
1246 * @is_netdev: make sure it's a netdev filter, else doesn't matter
1247 **/
1248void i40e_del_filter(struct i40e_vsi *vsi,
1249 u8 *macaddr, s16 vlan,
1250 bool is_vf, bool is_netdev)
1251{
1252 struct i40e_mac_filter *f;
1253
1254 if (!vsi || !macaddr)
1255 return;
1256
1257 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1258 if (!f || f->counter == 0)
1259 return;
1260
1261 if (is_vf) {
1262 if (f->is_vf) {
1263 f->is_vf = false;
1264 f->counter--;
1265 }
1266 } else if (is_netdev) {
1267 if (f->is_netdev) {
1268 f->is_netdev = false;
1269 f->counter--;
1270 }
1271 } else {
1272 /* make sure we don't remove a filter in use by vf or netdev */
1273 int min_f = 0;
1274 min_f += (f->is_vf ? 1 : 0);
1275 min_f += (f->is_netdev ? 1 : 0);
1276
1277 if (f->counter > min_f)
1278 f->counter--;
1279 }
1280
1281 /* counter == 0 tells sync_filters_subtask to
1282 * remove the filter from the firmware's list
1283 */
1284 if (f->counter == 0) {
1285 f->changed = true;
1286 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1287 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1288 }
1289}
1290
1291/**
1292 * i40e_set_mac - NDO callback to set mac address
1293 * @netdev: network interface device structure
1294 * @p: pointer to an address structure
1295 *
1296 * Returns 0 on success, negative on failure
1297 **/
1298static int i40e_set_mac(struct net_device *netdev, void *p)
1299{
1300 struct i40e_netdev_priv *np = netdev_priv(netdev);
1301 struct i40e_vsi *vsi = np->vsi;
1302 struct sockaddr *addr = p;
1303 struct i40e_mac_filter *f;
1304
1305 if (!is_valid_ether_addr(addr->sa_data))
1306 return -EADDRNOTAVAIL;
1307
1308 netdev_info(netdev, "set mac address=%pM\n", addr->sa_data);
1309
1310 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
1311 return 0;
1312
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001313 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1314 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1315 return -EADDRNOTAVAIL;
1316
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001317 if (vsi->type == I40E_VSI_MAIN) {
1318 i40e_status ret;
1319 ret = i40e_aq_mac_address_write(&vsi->back->hw,
1320 I40E_AQC_WRITE_TYPE_LAA_ONLY,
1321 addr->sa_data, NULL);
1322 if (ret) {
1323 netdev_info(netdev,
1324 "Addr change for Main VSI failed: %d\n",
1325 ret);
1326 return -EADDRNOTAVAIL;
1327 }
1328
1329 memcpy(vsi->back->hw.mac.addr, addr->sa_data, netdev->addr_len);
1330 }
1331
1332 /* In order to be sure to not drop any packets, add the new address
1333 * then delete the old one.
1334 */
1335 f = i40e_add_filter(vsi, addr->sa_data, I40E_VLAN_ANY, false, false);
1336 if (!f)
1337 return -ENOMEM;
1338
1339 i40e_sync_vsi_filters(vsi);
1340 i40e_del_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY, false, false);
1341 i40e_sync_vsi_filters(vsi);
1342
1343 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1344
1345 return 0;
1346}
1347
1348/**
1349 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1350 * @vsi: the VSI being setup
1351 * @ctxt: VSI context structure
1352 * @enabled_tc: Enabled TCs bitmap
1353 * @is_add: True if called before Add VSI
1354 *
1355 * Setup VSI queue mapping for enabled traffic classes.
1356 **/
1357static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1358 struct i40e_vsi_context *ctxt,
1359 u8 enabled_tc,
1360 bool is_add)
1361{
1362 struct i40e_pf *pf = vsi->back;
1363 u16 sections = 0;
1364 u8 netdev_tc = 0;
1365 u16 numtc = 0;
1366 u16 qcount;
1367 u8 offset;
1368 u16 qmap;
1369 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001370 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001371
1372 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1373 offset = 0;
1374
1375 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1376 /* Find numtc from enabled TC bitmap */
1377 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1378 if (enabled_tc & (1 << i)) /* TC is enabled */
1379 numtc++;
1380 }
1381 if (!numtc) {
1382 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1383 numtc = 1;
1384 }
1385 } else {
1386 /* At least TC0 is enabled in case of non-DCB case */
1387 numtc = 1;
1388 }
1389
1390 vsi->tc_config.numtc = numtc;
1391 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001392 /* Number of queues per enabled TC */
Anjali Singhai Jaineb051af2014-05-20 08:01:46 +00001393 num_tc_qps = vsi->alloc_queue_pairs/numtc;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001394 num_tc_qps = min_t(int, num_tc_qps, I40E_MAX_QUEUES_PER_TC);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001395
1396 /* Setup queue offset/count for all TCs for given VSI */
1397 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1398 /* See if the given TC is enabled for the given VSI */
1399 if (vsi->tc_config.enabled_tc & (1 << i)) { /* TC is enabled */
1400 int pow, num_qps;
1401
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001402 switch (vsi->type) {
1403 case I40E_VSI_MAIN:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001404 qcount = min_t(int, pf->rss_size, num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001405 break;
1406 case I40E_VSI_FDIR:
1407 case I40E_VSI_SRIOV:
1408 case I40E_VSI_VMDQ2:
1409 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001410 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001411 WARN_ON(i != 0);
1412 break;
1413 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001414 vsi->tc_config.tc_info[i].qoffset = offset;
1415 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001416
1417 /* find the power-of-2 of the number of queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001418 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001419 pow = 0;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001420 while (num_qps && ((1 << pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001421 pow++;
1422 num_qps >>= 1;
1423 }
1424
1425 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1426 qmap =
1427 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1428 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1429
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001430 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001431 } else {
1432 /* TC is not enabled so set the offset to
1433 * default queue and allocate one queue
1434 * for the given TC.
1435 */
1436 vsi->tc_config.tc_info[i].qoffset = 0;
1437 vsi->tc_config.tc_info[i].qcount = 1;
1438 vsi->tc_config.tc_info[i].netdev_tc = 0;
1439
1440 qmap = 0;
1441 }
1442 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1443 }
1444
1445 /* Set actual Tx/Rx queue pairs */
1446 vsi->num_queue_pairs = offset;
1447
1448 /* Scheduler section valid can only be set for ADD VSI */
1449 if (is_add) {
1450 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1451
1452 ctxt->info.up_enable_bits = enabled_tc;
1453 }
1454 if (vsi->type == I40E_VSI_SRIOV) {
1455 ctxt->info.mapping_flags |=
1456 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1457 for (i = 0; i < vsi->num_queue_pairs; i++)
1458 ctxt->info.queue_mapping[i] =
1459 cpu_to_le16(vsi->base_queue + i);
1460 } else {
1461 ctxt->info.mapping_flags |=
1462 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1463 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1464 }
1465 ctxt->info.valid_sections |= cpu_to_le16(sections);
1466}
1467
1468/**
1469 * i40e_set_rx_mode - NDO callback to set the netdev filters
1470 * @netdev: network interface device structure
1471 **/
1472static void i40e_set_rx_mode(struct net_device *netdev)
1473{
1474 struct i40e_netdev_priv *np = netdev_priv(netdev);
1475 struct i40e_mac_filter *f, *ftmp;
1476 struct i40e_vsi *vsi = np->vsi;
1477 struct netdev_hw_addr *uca;
1478 struct netdev_hw_addr *mca;
1479 struct netdev_hw_addr *ha;
1480
1481 /* add addr if not already in the filter list */
1482 netdev_for_each_uc_addr(uca, netdev) {
1483 if (!i40e_find_mac(vsi, uca->addr, false, true)) {
1484 if (i40e_is_vsi_in_vlan(vsi))
1485 i40e_put_mac_in_vlan(vsi, uca->addr,
1486 false, true);
1487 else
1488 i40e_add_filter(vsi, uca->addr, I40E_VLAN_ANY,
1489 false, true);
1490 }
1491 }
1492
1493 netdev_for_each_mc_addr(mca, netdev) {
1494 if (!i40e_find_mac(vsi, mca->addr, false, true)) {
1495 if (i40e_is_vsi_in_vlan(vsi))
1496 i40e_put_mac_in_vlan(vsi, mca->addr,
1497 false, true);
1498 else
1499 i40e_add_filter(vsi, mca->addr, I40E_VLAN_ANY,
1500 false, true);
1501 }
1502 }
1503
1504 /* remove filter if not in netdev list */
1505 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1506 bool found = false;
1507
1508 if (!f->is_netdev)
1509 continue;
1510
1511 if (is_multicast_ether_addr(f->macaddr)) {
1512 netdev_for_each_mc_addr(mca, netdev) {
1513 if (ether_addr_equal(mca->addr, f->macaddr)) {
1514 found = true;
1515 break;
1516 }
1517 }
1518 } else {
1519 netdev_for_each_uc_addr(uca, netdev) {
1520 if (ether_addr_equal(uca->addr, f->macaddr)) {
1521 found = true;
1522 break;
1523 }
1524 }
1525
1526 for_each_dev_addr(netdev, ha) {
1527 if (ether_addr_equal(ha->addr, f->macaddr)) {
1528 found = true;
1529 break;
1530 }
1531 }
1532 }
1533 if (!found)
1534 i40e_del_filter(
1535 vsi, f->macaddr, I40E_VLAN_ANY, false, true);
1536 }
1537
1538 /* check for other flag changes */
1539 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1540 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1541 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1542 }
1543}
1544
1545/**
1546 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1547 * @vsi: ptr to the VSI
1548 *
1549 * Push any outstanding VSI filter changes through the AdminQ.
1550 *
1551 * Returns 0 or error value
1552 **/
1553int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
1554{
1555 struct i40e_mac_filter *f, *ftmp;
1556 bool promisc_forced_on = false;
1557 bool add_happened = false;
1558 int filter_list_len = 0;
1559 u32 changed_flags = 0;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001560 i40e_status aq_ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001561 struct i40e_pf *pf;
1562 int num_add = 0;
1563 int num_del = 0;
1564 u16 cmd_flags;
1565
1566 /* empty array typed pointers, kcalloc later */
1567 struct i40e_aqc_add_macvlan_element_data *add_list;
1568 struct i40e_aqc_remove_macvlan_element_data *del_list;
1569
1570 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1571 usleep_range(1000, 2000);
1572 pf = vsi->back;
1573
1574 if (vsi->netdev) {
1575 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1576 vsi->current_netdev_flags = vsi->netdev->flags;
1577 }
1578
1579 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1580 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1581
1582 filter_list_len = pf->hw.aq.asq_buf_size /
1583 sizeof(struct i40e_aqc_remove_macvlan_element_data);
1584 del_list = kcalloc(filter_list_len,
1585 sizeof(struct i40e_aqc_remove_macvlan_element_data),
1586 GFP_KERNEL);
1587 if (!del_list)
1588 return -ENOMEM;
1589
1590 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1591 if (!f->changed)
1592 continue;
1593
1594 if (f->counter != 0)
1595 continue;
1596 f->changed = false;
1597 cmd_flags = 0;
1598
1599 /* add to delete list */
1600 memcpy(del_list[num_del].mac_addr,
1601 f->macaddr, ETH_ALEN);
1602 del_list[num_del].vlan_tag =
1603 cpu_to_le16((u16)(f->vlan ==
1604 I40E_VLAN_ANY ? 0 : f->vlan));
1605
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001606 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1607 del_list[num_del].flags = cmd_flags;
1608 num_del++;
1609
1610 /* unlink from filter list */
1611 list_del(&f->list);
1612 kfree(f);
1613
1614 /* flush a full buffer */
1615 if (num_del == filter_list_len) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001616 aq_ret = i40e_aq_remove_macvlan(&pf->hw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001617 vsi->seid, del_list, num_del,
1618 NULL);
1619 num_del = 0;
1620 memset(del_list, 0, sizeof(*del_list));
1621
Shannon Nelsonfdfe9cb2014-05-20 08:01:39 +00001622 if (aq_ret &&
1623 pf->hw.aq.asq_last_status !=
1624 I40E_AQ_RC_ENOENT)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001625 dev_info(&pf->pdev->dev,
1626 "ignoring delete macvlan error, err %d, aq_err %d while flushing a full buffer\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001627 aq_ret,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001628 pf->hw.aq.asq_last_status);
1629 }
1630 }
1631 if (num_del) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001632 aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001633 del_list, num_del, NULL);
1634 num_del = 0;
1635
Shannon Nelsonfdfe9cb2014-05-20 08:01:39 +00001636 if (aq_ret &&
1637 pf->hw.aq.asq_last_status != I40E_AQ_RC_ENOENT)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001638 dev_info(&pf->pdev->dev,
1639 "ignoring delete macvlan error, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001640 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001641 }
1642
1643 kfree(del_list);
1644 del_list = NULL;
1645
1646 /* do all the adds now */
1647 filter_list_len = pf->hw.aq.asq_buf_size /
1648 sizeof(struct i40e_aqc_add_macvlan_element_data),
1649 add_list = kcalloc(filter_list_len,
1650 sizeof(struct i40e_aqc_add_macvlan_element_data),
1651 GFP_KERNEL);
1652 if (!add_list)
1653 return -ENOMEM;
1654
1655 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1656 if (!f->changed)
1657 continue;
1658
1659 if (f->counter == 0)
1660 continue;
1661 f->changed = false;
1662 add_happened = true;
1663 cmd_flags = 0;
1664
1665 /* add to add array */
1666 memcpy(add_list[num_add].mac_addr,
1667 f->macaddr, ETH_ALEN);
1668 add_list[num_add].vlan_tag =
1669 cpu_to_le16(
1670 (u16)(f->vlan == I40E_VLAN_ANY ? 0 : f->vlan));
1671 add_list[num_add].queue_number = 0;
1672
1673 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001674 add_list[num_add].flags = cpu_to_le16(cmd_flags);
1675 num_add++;
1676
1677 /* flush a full buffer */
1678 if (num_add == filter_list_len) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001679 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1680 add_list, num_add,
1681 NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001682 num_add = 0;
1683
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001684 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001685 break;
1686 memset(add_list, 0, sizeof(*add_list));
1687 }
1688 }
1689 if (num_add) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001690 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1691 add_list, num_add, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001692 num_add = 0;
1693 }
1694 kfree(add_list);
1695 add_list = NULL;
1696
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001697 if (add_happened && (!aq_ret)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001698 /* do nothing */;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001699 } else if (add_happened && (aq_ret)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001700 dev_info(&pf->pdev->dev,
1701 "add filter failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001702 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001703 if ((pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOSPC) &&
1704 !test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1705 &vsi->state)) {
1706 promisc_forced_on = true;
1707 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1708 &vsi->state);
1709 dev_info(&pf->pdev->dev, "promiscuous mode forced on\n");
1710 }
1711 }
1712 }
1713
1714 /* check for changes in promiscuous modes */
1715 if (changed_flags & IFF_ALLMULTI) {
1716 bool cur_multipromisc;
1717 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001718 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
1719 vsi->seid,
1720 cur_multipromisc,
1721 NULL);
1722 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001723 dev_info(&pf->pdev->dev,
1724 "set multi promisc failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001725 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001726 }
1727 if ((changed_flags & IFF_PROMISC) || promisc_forced_on) {
1728 bool cur_promisc;
1729 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
1730 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1731 &vsi->state));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001732 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(&vsi->back->hw,
1733 vsi->seid,
1734 cur_promisc, NULL);
1735 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001736 dev_info(&pf->pdev->dev,
1737 "set uni promisc failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001738 aq_ret, pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00001739 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
1740 vsi->seid,
1741 cur_promisc, NULL);
1742 if (aq_ret)
1743 dev_info(&pf->pdev->dev,
1744 "set brdcast promisc failed, err %d, aq_err %d\n",
1745 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001746 }
1747
1748 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
1749 return 0;
1750}
1751
1752/**
1753 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
1754 * @pf: board private structure
1755 **/
1756static void i40e_sync_filters_subtask(struct i40e_pf *pf)
1757{
1758 int v;
1759
1760 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
1761 return;
1762 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
1763
Mitch Williams505682c2014-05-20 08:01:37 +00001764 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001765 if (pf->vsi[v] &&
1766 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED))
1767 i40e_sync_vsi_filters(pf->vsi[v]);
1768 }
1769}
1770
1771/**
1772 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
1773 * @netdev: network interface device structure
1774 * @new_mtu: new value for maximum frame size
1775 *
1776 * Returns 0 on success, negative on failure
1777 **/
1778static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
1779{
1780 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg61a46a42014-04-23 04:50:05 +00001781 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001782 struct i40e_vsi *vsi = np->vsi;
1783
1784 /* MTU < 68 is an error and causes problems on some kernels */
1785 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
1786 return -EINVAL;
1787
1788 netdev_info(netdev, "changing MTU from %d to %d\n",
1789 netdev->mtu, new_mtu);
1790 netdev->mtu = new_mtu;
1791 if (netif_running(netdev))
1792 i40e_vsi_reinit_locked(vsi);
1793
1794 return 0;
1795}
1796
1797/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00001798 * i40e_ioctl - Access the hwtstamp interface
1799 * @netdev: network interface device structure
1800 * @ifr: interface request data
1801 * @cmd: ioctl command
1802 **/
1803int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1804{
1805 struct i40e_netdev_priv *np = netdev_priv(netdev);
1806 struct i40e_pf *pf = np->vsi->back;
1807
1808 switch (cmd) {
1809 case SIOCGHWTSTAMP:
1810 return i40e_ptp_get_ts_config(pf, ifr);
1811 case SIOCSHWTSTAMP:
1812 return i40e_ptp_set_ts_config(pf, ifr);
1813 default:
1814 return -EOPNOTSUPP;
1815 }
1816}
1817
1818/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001819 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
1820 * @vsi: the vsi being adjusted
1821 **/
1822void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
1823{
1824 struct i40e_vsi_context ctxt;
1825 i40e_status ret;
1826
1827 if ((vsi->info.valid_sections &
1828 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1829 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
1830 return; /* already enabled */
1831
1832 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1833 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1834 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
1835
1836 ctxt.seid = vsi->seid;
1837 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1838 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1839 if (ret) {
1840 dev_info(&vsi->back->pdev->dev,
1841 "%s: update vsi failed, aq_err=%d\n",
1842 __func__, vsi->back->hw.aq.asq_last_status);
1843 }
1844}
1845
1846/**
1847 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
1848 * @vsi: the vsi being adjusted
1849 **/
1850void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
1851{
1852 struct i40e_vsi_context ctxt;
1853 i40e_status ret;
1854
1855 if ((vsi->info.valid_sections &
1856 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1857 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
1858 I40E_AQ_VSI_PVLAN_EMOD_MASK))
1859 return; /* already disabled */
1860
1861 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1862 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1863 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
1864
1865 ctxt.seid = vsi->seid;
1866 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1867 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1868 if (ret) {
1869 dev_info(&vsi->back->pdev->dev,
1870 "%s: update vsi failed, aq_err=%d\n",
1871 __func__, vsi->back->hw.aq.asq_last_status);
1872 }
1873}
1874
1875/**
1876 * i40e_vlan_rx_register - Setup or shutdown vlan offload
1877 * @netdev: network interface to be adjusted
1878 * @features: netdev features to test if VLAN offload is enabled or not
1879 **/
1880static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
1881{
1882 struct i40e_netdev_priv *np = netdev_priv(netdev);
1883 struct i40e_vsi *vsi = np->vsi;
1884
1885 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1886 i40e_vlan_stripping_enable(vsi);
1887 else
1888 i40e_vlan_stripping_disable(vsi);
1889}
1890
1891/**
1892 * i40e_vsi_add_vlan - Add vsi membership for given vlan
1893 * @vsi: the vsi being configured
1894 * @vid: vlan id to be added (0 = untagged only , -1 = any)
1895 **/
1896int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
1897{
1898 struct i40e_mac_filter *f, *add_f;
1899 bool is_netdev, is_vf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001900
1901 is_vf = (vsi->type == I40E_VSI_SRIOV);
1902 is_netdev = !!(vsi->netdev);
1903
1904 if (is_netdev) {
1905 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid,
1906 is_vf, is_netdev);
1907 if (!add_f) {
1908 dev_info(&vsi->back->pdev->dev,
1909 "Could not add vlan filter %d for %pM\n",
1910 vid, vsi->netdev->dev_addr);
1911 return -ENOMEM;
1912 }
1913 }
1914
1915 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1916 add_f = i40e_add_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
1917 if (!add_f) {
1918 dev_info(&vsi->back->pdev->dev,
1919 "Could not add vlan filter %d for %pM\n",
1920 vid, f->macaddr);
1921 return -ENOMEM;
1922 }
1923 }
1924
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001925 /* Now if we add a vlan tag, make sure to check if it is the first
1926 * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
1927 * with 0, so we now accept untagged and specified tagged traffic
1928 * (and not any taged and untagged)
1929 */
1930 if (vid > 0) {
1931 if (is_netdev && i40e_find_filter(vsi, vsi->netdev->dev_addr,
1932 I40E_VLAN_ANY,
1933 is_vf, is_netdev)) {
1934 i40e_del_filter(vsi, vsi->netdev->dev_addr,
1935 I40E_VLAN_ANY, is_vf, is_netdev);
1936 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0,
1937 is_vf, is_netdev);
1938 if (!add_f) {
1939 dev_info(&vsi->back->pdev->dev,
1940 "Could not add filter 0 for %pM\n",
1941 vsi->netdev->dev_addr);
1942 return -ENOMEM;
1943 }
1944 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08001945 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001946
Greg Rose8d82a7c2014-01-13 16:13:04 -08001947 /* Do not assume that I40E_VLAN_ANY should be reset to VLAN 0 */
1948 if (vid > 0 && !vsi->info.pvid) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001949 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1950 if (i40e_find_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1951 is_vf, is_netdev)) {
1952 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1953 is_vf, is_netdev);
1954 add_f = i40e_add_filter(vsi, f->macaddr,
1955 0, is_vf, is_netdev);
1956 if (!add_f) {
1957 dev_info(&vsi->back->pdev->dev,
1958 "Could not add filter 0 for %pM\n",
1959 f->macaddr);
1960 return -ENOMEM;
1961 }
1962 }
1963 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001964 }
1965
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001966 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1967 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1968 return 0;
1969
1970 return i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001971}
1972
1973/**
1974 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
1975 * @vsi: the vsi being configured
1976 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001977 *
1978 * Return: 0 on success or negative otherwise
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001979 **/
1980int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
1981{
1982 struct net_device *netdev = vsi->netdev;
1983 struct i40e_mac_filter *f, *add_f;
1984 bool is_vf, is_netdev;
1985 int filter_count = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001986
1987 is_vf = (vsi->type == I40E_VSI_SRIOV);
1988 is_netdev = !!(netdev);
1989
1990 if (is_netdev)
1991 i40e_del_filter(vsi, netdev->dev_addr, vid, is_vf, is_netdev);
1992
1993 list_for_each_entry(f, &vsi->mac_filter_list, list)
1994 i40e_del_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
1995
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001996 /* go through all the filters for this VSI and if there is only
1997 * vid == 0 it means there are no other filters, so vid 0 must
1998 * be replaced with -1. This signifies that we should from now
1999 * on accept any traffic (with any tag present, or untagged)
2000 */
2001 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2002 if (is_netdev) {
2003 if (f->vlan &&
2004 ether_addr_equal(netdev->dev_addr, f->macaddr))
2005 filter_count++;
2006 }
2007
2008 if (f->vlan)
2009 filter_count++;
2010 }
2011
2012 if (!filter_count && is_netdev) {
2013 i40e_del_filter(vsi, netdev->dev_addr, 0, is_vf, is_netdev);
2014 f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
2015 is_vf, is_netdev);
2016 if (!f) {
2017 dev_info(&vsi->back->pdev->dev,
2018 "Could not add filter %d for %pM\n",
2019 I40E_VLAN_ANY, netdev->dev_addr);
2020 return -ENOMEM;
2021 }
2022 }
2023
2024 if (!filter_count) {
2025 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2026 i40e_del_filter(vsi, f->macaddr, 0, is_vf, is_netdev);
2027 add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2028 is_vf, is_netdev);
2029 if (!add_f) {
2030 dev_info(&vsi->back->pdev->dev,
2031 "Could not add filter %d for %pM\n",
2032 I40E_VLAN_ANY, f->macaddr);
2033 return -ENOMEM;
2034 }
2035 }
2036 }
2037
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00002038 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
2039 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
2040 return 0;
2041
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002042 return i40e_sync_vsi_filters(vsi);
2043}
2044
2045/**
2046 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2047 * @netdev: network interface to be adjusted
2048 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002049 *
2050 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002051 **/
2052static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2053 __always_unused __be16 proto, u16 vid)
2054{
2055 struct i40e_netdev_priv *np = netdev_priv(netdev);
2056 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002057 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002058
2059 if (vid > 4095)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002060 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002061
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002062 netdev_info(netdev, "adding %pM vid=%d\n", netdev->dev_addr, vid);
2063
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002064 /* If the network stack called us with vid = 0 then
2065 * it is asking to receive priority tagged packets with
2066 * vlan id 0. Our HW receives them by default when configured
2067 * to receive untagged packets so there is no need to add an
2068 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002069 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002070 if (vid)
2071 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002072
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002073 if (!ret && (vid < VLAN_N_VID))
2074 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002075
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002076 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002077}
2078
2079/**
2080 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2081 * @netdev: network interface to be adjusted
2082 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002083 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002084 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002085 **/
2086static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2087 __always_unused __be16 proto, u16 vid)
2088{
2089 struct i40e_netdev_priv *np = netdev_priv(netdev);
2090 struct i40e_vsi *vsi = np->vsi;
2091
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002092 netdev_info(netdev, "removing %pM vid=%d\n", netdev->dev_addr, vid);
2093
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002094 /* return code is ignored as there is nothing a user
2095 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002096 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002097 */
2098 i40e_vsi_kill_vlan(vsi, vid);
2099
2100 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002101
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002102 return 0;
2103}
2104
2105/**
2106 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2107 * @vsi: the vsi being brought back up
2108 **/
2109static void i40e_restore_vlan(struct i40e_vsi *vsi)
2110{
2111 u16 vid;
2112
2113 if (!vsi->netdev)
2114 return;
2115
2116 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2117
2118 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2119 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2120 vid);
2121}
2122
2123/**
2124 * i40e_vsi_add_pvid - Add pvid for the VSI
2125 * @vsi: the vsi being adjusted
2126 * @vid: the vlan id to set as a PVID
2127 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002128int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002129{
2130 struct i40e_vsi_context ctxt;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002131 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002132
2133 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2134 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002135 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2136 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002137 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002138
2139 ctxt.seid = vsi->seid;
2140 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002141 aq_ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2142 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002143 dev_info(&vsi->back->pdev->dev,
2144 "%s: update vsi failed, aq_err=%d\n",
2145 __func__, vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002146 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002147 }
2148
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002149 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002150}
2151
2152/**
2153 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2154 * @vsi: the vsi being adjusted
2155 *
2156 * Just use the vlan_rx_register() service to put it back to normal
2157 **/
2158void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2159{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002160 i40e_vlan_stripping_disable(vsi);
2161
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002162 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002163}
2164
2165/**
2166 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2167 * @vsi: ptr to the VSI
2168 *
2169 * If this function returns with an error, then it's possible one or
2170 * more of the rings is populated (while the rest are not). It is the
2171 * callers duty to clean those orphaned rings.
2172 *
2173 * Return 0 on success, negative on failure
2174 **/
2175static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2176{
2177 int i, err = 0;
2178
2179 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002180 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002181
2182 return err;
2183}
2184
2185/**
2186 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2187 * @vsi: ptr to the VSI
2188 *
2189 * Free VSI's transmit software resources
2190 **/
2191static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2192{
2193 int i;
2194
Greg Rose8e9dca52013-12-18 13:45:53 +00002195 if (!vsi->tx_rings)
2196 return;
2197
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002198 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002199 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002200 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002201}
2202
2203/**
2204 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2205 * @vsi: ptr to the VSI
2206 *
2207 * If this function returns with an error, then it's possible one or
2208 * more of the rings is populated (while the rest are not). It is the
2209 * callers duty to clean those orphaned rings.
2210 *
2211 * Return 0 on success, negative on failure
2212 **/
2213static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2214{
2215 int i, err = 0;
2216
2217 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002218 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002219 return err;
2220}
2221
2222/**
2223 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2224 * @vsi: ptr to the VSI
2225 *
2226 * Free all receive software resources
2227 **/
2228static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2229{
2230 int i;
2231
Greg Rose8e9dca52013-12-18 13:45:53 +00002232 if (!vsi->rx_rings)
2233 return;
2234
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002235 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002236 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002237 i40e_free_rx_resources(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002238}
2239
2240/**
2241 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2242 * @ring: The Tx ring to configure
2243 *
2244 * Configure the Tx descriptor ring in the HMC context.
2245 **/
2246static int i40e_configure_tx_ring(struct i40e_ring *ring)
2247{
2248 struct i40e_vsi *vsi = ring->vsi;
2249 u16 pf_q = vsi->base_queue + ring->queue_index;
2250 struct i40e_hw *hw = &vsi->back->hw;
2251 struct i40e_hmc_obj_txq tx_ctx;
2252 i40e_status err = 0;
2253 u32 qtx_ctl = 0;
2254
2255 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002256 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002257 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2258 ring->atr_count = 0;
2259 } else {
2260 ring->atr_sample_rate = 0;
2261 }
2262
2263 /* initialize XPS */
2264 if (ring->q_vector && ring->netdev &&
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08002265 vsi->tc_config.numtc <= 1 &&
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002266 !test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
2267 netif_set_xps_queue(ring->netdev,
2268 &ring->q_vector->affinity_mask,
2269 ring->queue_index);
2270
2271 /* clear the context structure first */
2272 memset(&tx_ctx, 0, sizeof(tx_ctx));
2273
2274 tx_ctx.new_context = 1;
2275 tx_ctx.base = (ring->dma / 128);
2276 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002277 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2278 I40E_FLAG_FD_ATR_ENABLED));
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002279 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002280 /* FDIR VSI tx ring can still use RS bit and writebacks */
2281 if (vsi->type != I40E_VSI_FDIR)
2282 tx_ctx.head_wb_ena = 1;
2283 tx_ctx.head_wb_addr = ring->dma +
2284 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002285
2286 /* As part of VSI creation/update, FW allocates certain
2287 * Tx arbitration queue sets for each TC enabled for
2288 * the VSI. The FW returns the handles to these queue
2289 * sets as part of the response buffer to Add VSI,
2290 * Update VSI, etc. AQ commands. It is expected that
2291 * these queue set handles be associated with the Tx
2292 * queues by the driver as part of the TX queue context
2293 * initialization. This has to be done regardless of
2294 * DCB as by default everything is mapped to TC0.
2295 */
2296 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2297 tx_ctx.rdylist_act = 0;
2298
2299 /* clear the context in the HMC */
2300 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2301 if (err) {
2302 dev_info(&vsi->back->pdev->dev,
2303 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2304 ring->queue_index, pf_q, err);
2305 return -ENOMEM;
2306 }
2307
2308 /* set the context in the HMC */
2309 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2310 if (err) {
2311 dev_info(&vsi->back->pdev->dev,
2312 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2313 ring->queue_index, pf_q, err);
2314 return -ENOMEM;
2315 }
2316
2317 /* Now associate this queue with this PCI function */
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002318 if (vsi->type == I40E_VSI_VMDQ2)
2319 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
2320 else
2321 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Shannon Nelson13fd9772013-09-28 07:14:19 +00002322 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2323 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002324 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2325 i40e_flush(hw);
2326
2327 clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
2328
2329 /* cache tail off for easier writes later */
2330 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2331
2332 return 0;
2333}
2334
2335/**
2336 * i40e_configure_rx_ring - Configure a receive ring context
2337 * @ring: The Rx ring to configure
2338 *
2339 * Configure the Rx descriptor ring in the HMC context.
2340 **/
2341static int i40e_configure_rx_ring(struct i40e_ring *ring)
2342{
2343 struct i40e_vsi *vsi = ring->vsi;
2344 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2345 u16 pf_q = vsi->base_queue + ring->queue_index;
2346 struct i40e_hw *hw = &vsi->back->hw;
2347 struct i40e_hmc_obj_rxq rx_ctx;
2348 i40e_status err = 0;
2349
2350 ring->state = 0;
2351
2352 /* clear the context structure first */
2353 memset(&rx_ctx, 0, sizeof(rx_ctx));
2354
2355 ring->rx_buf_len = vsi->rx_buf_len;
2356 ring->rx_hdr_len = vsi->rx_hdr_len;
2357
2358 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
2359 rx_ctx.hbuff = ring->rx_hdr_len >> I40E_RXQ_CTX_HBUFF_SHIFT;
2360
2361 rx_ctx.base = (ring->dma / 128);
2362 rx_ctx.qlen = ring->count;
2363
2364 if (vsi->back->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED) {
2365 set_ring_16byte_desc_enabled(ring);
2366 rx_ctx.dsize = 0;
2367 } else {
2368 rx_ctx.dsize = 1;
2369 }
2370
2371 rx_ctx.dtype = vsi->dtype;
2372 if (vsi->dtype) {
2373 set_ring_ps_enabled(ring);
2374 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
2375 I40E_RX_SPLIT_IP |
2376 I40E_RX_SPLIT_TCP_UDP |
2377 I40E_RX_SPLIT_SCTP;
2378 } else {
2379 rx_ctx.hsplit_0 = 0;
2380 }
2381
2382 rx_ctx.rxmax = min_t(u16, vsi->max_frame,
2383 (chain_len * ring->rx_buf_len));
2384 rx_ctx.tphrdesc_ena = 1;
2385 rx_ctx.tphwdesc_ena = 1;
2386 rx_ctx.tphdata_ena = 1;
2387 rx_ctx.tphhead_ena = 1;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00002388 if (hw->revision_id == 0)
2389 rx_ctx.lrxqthresh = 0;
2390 else
2391 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002392 rx_ctx.crcstrip = 1;
2393 rx_ctx.l2tsel = 1;
2394 rx_ctx.showiv = 1;
Catherine Sullivanacb36762014-03-06 09:02:30 +00002395 /* set the prefena field to 1 because the manual says to */
2396 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002397
2398 /* clear the context in the HMC */
2399 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2400 if (err) {
2401 dev_info(&vsi->back->pdev->dev,
2402 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2403 ring->queue_index, pf_q, err);
2404 return -ENOMEM;
2405 }
2406
2407 /* set the context in the HMC */
2408 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2409 if (err) {
2410 dev_info(&vsi->back->pdev->dev,
2411 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2412 ring->queue_index, pf_q, err);
2413 return -ENOMEM;
2414 }
2415
2416 /* cache tail for quicker writes, and clear the reg before use */
2417 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2418 writel(0, ring->tail);
2419
2420 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
2421
2422 return 0;
2423}
2424
2425/**
2426 * i40e_vsi_configure_tx - Configure the VSI for Tx
2427 * @vsi: VSI structure describing this set of rings and resources
2428 *
2429 * Configure the Tx VSI for operation.
2430 **/
2431static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2432{
2433 int err = 0;
2434 u16 i;
2435
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002436 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2437 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002438
2439 return err;
2440}
2441
2442/**
2443 * i40e_vsi_configure_rx - Configure the VSI for Rx
2444 * @vsi: the VSI being configured
2445 *
2446 * Configure the Rx VSI for operation.
2447 **/
2448static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
2449{
2450 int err = 0;
2451 u16 i;
2452
2453 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
2454 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
2455 + ETH_FCS_LEN + VLAN_HLEN;
2456 else
2457 vsi->max_frame = I40E_RXBUFFER_2048;
2458
2459 /* figure out correct receive buffer length */
2460 switch (vsi->back->flags & (I40E_FLAG_RX_1BUF_ENABLED |
2461 I40E_FLAG_RX_PS_ENABLED)) {
2462 case I40E_FLAG_RX_1BUF_ENABLED:
2463 vsi->rx_hdr_len = 0;
2464 vsi->rx_buf_len = vsi->max_frame;
2465 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2466 break;
2467 case I40E_FLAG_RX_PS_ENABLED:
2468 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2469 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2470 vsi->dtype = I40E_RX_DTYPE_HEADER_SPLIT;
2471 break;
2472 default:
2473 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2474 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2475 vsi->dtype = I40E_RX_DTYPE_SPLIT_ALWAYS;
2476 break;
2477 }
2478
2479 /* round up for the chip's needs */
2480 vsi->rx_hdr_len = ALIGN(vsi->rx_hdr_len,
2481 (1 << I40E_RXQ_CTX_HBUFF_SHIFT));
2482 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
2483 (1 << I40E_RXQ_CTX_DBUFF_SHIFT));
2484
2485 /* set up individual rings */
2486 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002487 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002488
2489 return err;
2490}
2491
2492/**
2493 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
2494 * @vsi: ptr to the VSI
2495 **/
2496static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
2497{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00002498 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002499 u16 qoffset, qcount;
2500 int i, n;
2501
2502 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED))
2503 return;
2504
2505 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
2506 if (!(vsi->tc_config.enabled_tc & (1 << n)))
2507 continue;
2508
2509 qoffset = vsi->tc_config.tc_info[n].qoffset;
2510 qcount = vsi->tc_config.tc_info[n].qcount;
2511 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00002512 rx_ring = vsi->rx_rings[i];
2513 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002514 rx_ring->dcb_tc = n;
2515 tx_ring->dcb_tc = n;
2516 }
2517 }
2518}
2519
2520/**
2521 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
2522 * @vsi: ptr to the VSI
2523 **/
2524static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
2525{
2526 if (vsi->netdev)
2527 i40e_set_rx_mode(vsi->netdev);
2528}
2529
2530/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00002531 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
2532 * @vsi: Pointer to the targeted VSI
2533 *
2534 * This function replays the hlist on the hw where all the SB Flow Director
2535 * filters were saved.
2536 **/
2537static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
2538{
2539 struct i40e_fdir_filter *filter;
2540 struct i40e_pf *pf = vsi->back;
2541 struct hlist_node *node;
2542
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00002543 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
2544 return;
2545
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00002546 hlist_for_each_entry_safe(filter, node,
2547 &pf->fdir_filter_list, fdir_node) {
2548 i40e_add_del_fdir(vsi, filter, true);
2549 }
2550}
2551
2552/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002553 * i40e_vsi_configure - Set up the VSI for action
2554 * @vsi: the VSI being configured
2555 **/
2556static int i40e_vsi_configure(struct i40e_vsi *vsi)
2557{
2558 int err;
2559
2560 i40e_set_vsi_rx_mode(vsi);
2561 i40e_restore_vlan(vsi);
2562 i40e_vsi_config_dcb_rings(vsi);
2563 err = i40e_vsi_configure_tx(vsi);
2564 if (!err)
2565 err = i40e_vsi_configure_rx(vsi);
2566
2567 return err;
2568}
2569
2570/**
2571 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
2572 * @vsi: the VSI being configured
2573 **/
2574static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
2575{
2576 struct i40e_pf *pf = vsi->back;
2577 struct i40e_q_vector *q_vector;
2578 struct i40e_hw *hw = &pf->hw;
2579 u16 vector;
2580 int i, q;
2581 u32 val;
2582 u32 qp;
2583
2584 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
2585 * and PFINT_LNKLSTn registers, e.g.:
2586 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
2587 */
2588 qp = vsi->base_queue;
2589 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00002590 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
2591 q_vector = vsi->q_vectors[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002592 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2593 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2594 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
2595 q_vector->rx.itr);
2596 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2597 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2598 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
2599 q_vector->tx.itr);
2600
2601 /* Linked list for the queuepairs assigned to this vector */
2602 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
2603 for (q = 0; q < q_vector->num_ringpairs; q++) {
2604 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2605 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2606 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
2607 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
2608 (I40E_QUEUE_TYPE_TX
2609 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
2610
2611 wr32(hw, I40E_QINT_RQCTL(qp), val);
2612
2613 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2614 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2615 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
2616 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
2617 (I40E_QUEUE_TYPE_RX
2618 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2619
2620 /* Terminate the linked list */
2621 if (q == (q_vector->num_ringpairs - 1))
2622 val |= (I40E_QUEUE_END_OF_LIST
2623 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2624
2625 wr32(hw, I40E_QINT_TQCTL(qp), val);
2626 qp++;
2627 }
2628 }
2629
2630 i40e_flush(hw);
2631}
2632
2633/**
2634 * i40e_enable_misc_int_causes - enable the non-queue interrupts
2635 * @hw: ptr to the hardware info
2636 **/
2637static void i40e_enable_misc_int_causes(struct i40e_hw *hw)
2638{
2639 u32 val;
2640
2641 /* clear things first */
2642 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
2643 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
2644
2645 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
2646 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
2647 I40E_PFINT_ICR0_ENA_GRST_MASK |
2648 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
2649 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002650 I40E_PFINT_ICR0_ENA_TIMESYNC_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002651 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
2652 I40E_PFINT_ICR0_ENA_VFLR_MASK |
2653 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2654
2655 wr32(hw, I40E_PFINT_ICR0_ENA, val);
2656
2657 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00002658 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
2659 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002660
2661 /* OTHER_ITR_IDX = 0 */
2662 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
2663}
2664
2665/**
2666 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
2667 * @vsi: the VSI being configured
2668 **/
2669static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
2670{
Alexander Duyck493fb302013-09-28 07:01:44 +00002671 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002672 struct i40e_pf *pf = vsi->back;
2673 struct i40e_hw *hw = &pf->hw;
2674 u32 val;
2675
2676 /* set the ITR configuration */
2677 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2678 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2679 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
2680 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2681 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2682 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
2683
2684 i40e_enable_misc_int_causes(hw);
2685
2686 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
2687 wr32(hw, I40E_PFINT_LNKLST0, 0);
2688
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00002689 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002690 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2691 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2692 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2693
2694 wr32(hw, I40E_QINT_RQCTL(0), val);
2695
2696 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2697 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2698 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2699
2700 wr32(hw, I40E_QINT_TQCTL(0), val);
2701 i40e_flush(hw);
2702}
2703
2704/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00002705 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
2706 * @pf: board private structure
2707 **/
2708void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
2709{
2710 struct i40e_hw *hw = &pf->hw;
2711
2712 wr32(hw, I40E_PFINT_DYN_CTL0,
2713 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2714 i40e_flush(hw);
2715}
2716
2717/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002718 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
2719 * @pf: board private structure
2720 **/
Shannon Nelson116a57d2013-09-28 07:13:59 +00002721void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002722{
2723 struct i40e_hw *hw = &pf->hw;
2724 u32 val;
2725
2726 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
2727 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
2728 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
2729
2730 wr32(hw, I40E_PFINT_DYN_CTL0, val);
2731 i40e_flush(hw);
2732}
2733
2734/**
2735 * i40e_irq_dynamic_enable - Enable default interrupt generation settings
2736 * @vsi: pointer to a vsi
2737 * @vector: enable a particular Hw Interrupt vector
2738 **/
2739void i40e_irq_dynamic_enable(struct i40e_vsi *vsi, int vector)
2740{
2741 struct i40e_pf *pf = vsi->back;
2742 struct i40e_hw *hw = &pf->hw;
2743 u32 val;
2744
2745 val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
2746 I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
2747 (I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2748 wr32(hw, I40E_PFINT_DYN_CTLN(vector - 1), val);
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00002749 /* skip the flush */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002750}
2751
2752/**
2753 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
2754 * @irq: interrupt number
2755 * @data: pointer to a q_vector
2756 **/
2757static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
2758{
2759 struct i40e_q_vector *q_vector = data;
2760
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002761 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002762 return IRQ_HANDLED;
2763
2764 napi_schedule(&q_vector->napi);
2765
2766 return IRQ_HANDLED;
2767}
2768
2769/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002770 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
2771 * @vsi: the VSI being configured
2772 * @basename: name for the vector
2773 *
2774 * Allocates MSI-X vectors and requests interrupts from the kernel.
2775 **/
2776static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
2777{
2778 int q_vectors = vsi->num_q_vectors;
2779 struct i40e_pf *pf = vsi->back;
2780 int base = vsi->base_vector;
2781 int rx_int_idx = 0;
2782 int tx_int_idx = 0;
2783 int vector, err;
2784
2785 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00002786 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002787
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002788 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002789 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2790 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
2791 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002792 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002793 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2794 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002795 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002796 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2797 "%s-%s-%d", basename, "tx", tx_int_idx++);
2798 } else {
2799 /* skip this unused q_vector */
2800 continue;
2801 }
2802 err = request_irq(pf->msix_entries[base + vector].vector,
2803 vsi->irq_handler,
2804 0,
2805 q_vector->name,
2806 q_vector);
2807 if (err) {
2808 dev_info(&pf->pdev->dev,
2809 "%s: request_irq failed, error: %d\n",
2810 __func__, err);
2811 goto free_queue_irqs;
2812 }
2813 /* assign the mask for this irq */
2814 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2815 &q_vector->affinity_mask);
2816 }
2817
Shannon Nelson63741842014-04-23 04:50:16 +00002818 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002819 return 0;
2820
2821free_queue_irqs:
2822 while (vector) {
2823 vector--;
2824 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2825 NULL);
2826 free_irq(pf->msix_entries[base + vector].vector,
2827 &(vsi->q_vectors[vector]));
2828 }
2829 return err;
2830}
2831
2832/**
2833 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
2834 * @vsi: the VSI being un-configured
2835 **/
2836static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
2837{
2838 struct i40e_pf *pf = vsi->back;
2839 struct i40e_hw *hw = &pf->hw;
2840 int base = vsi->base_vector;
2841 int i;
2842
2843 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002844 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
2845 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002846 }
2847
2848 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2849 for (i = vsi->base_vector;
2850 i < (vsi->num_q_vectors + vsi->base_vector); i++)
2851 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
2852
2853 i40e_flush(hw);
2854 for (i = 0; i < vsi->num_q_vectors; i++)
2855 synchronize_irq(pf->msix_entries[i + base].vector);
2856 } else {
2857 /* Legacy and MSI mode - this stops all interrupt handling */
2858 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
2859 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
2860 i40e_flush(hw);
2861 synchronize_irq(pf->pdev->irq);
2862 }
2863}
2864
2865/**
2866 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
2867 * @vsi: the VSI being configured
2868 **/
2869static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
2870{
2871 struct i40e_pf *pf = vsi->back;
2872 int i;
2873
2874 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2875 for (i = vsi->base_vector;
2876 i < (vsi->num_q_vectors + vsi->base_vector); i++)
2877 i40e_irq_dynamic_enable(vsi, i);
2878 } else {
2879 i40e_irq_dynamic_enable_icr0(pf);
2880 }
2881
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00002882 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002883 return 0;
2884}
2885
2886/**
2887 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
2888 * @pf: board private structure
2889 **/
2890static void i40e_stop_misc_vector(struct i40e_pf *pf)
2891{
2892 /* Disable ICR 0 */
2893 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
2894 i40e_flush(&pf->hw);
2895}
2896
2897/**
2898 * i40e_intr - MSI/Legacy and non-queue interrupt handler
2899 * @irq: interrupt number
2900 * @data: pointer to a q_vector
2901 *
2902 * This is the handler used for all MSI/Legacy interrupts, and deals
2903 * with both queue and non-queue interrupts. This is also used in
2904 * MSIX mode to handle the non-queue interrupts.
2905 **/
2906static irqreturn_t i40e_intr(int irq, void *data)
2907{
2908 struct i40e_pf *pf = (struct i40e_pf *)data;
2909 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002910 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002911 u32 icr0, icr0_remaining;
2912 u32 val, ena_mask;
2913
2914 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002915 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002916
Shannon Nelson116a57d2013-09-28 07:13:59 +00002917 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
2918 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002919 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002920
Shannon Nelsoncd92e722013-11-16 10:00:44 +00002921 /* if interrupt but no bits showing, must be SWINT */
2922 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
2923 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
2924 pf->sw_int_count++;
2925
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002926 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
2927 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
2928
2929 /* temporarily disable queue cause for NAPI processing */
2930 u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
2931 qval &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
2932 wr32(hw, I40E_QINT_RQCTL(0), qval);
2933
2934 qval = rd32(hw, I40E_QINT_TQCTL(0));
2935 qval &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
2936 wr32(hw, I40E_QINT_TQCTL(0), qval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002937
2938 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck493fb302013-09-28 07:01:44 +00002939 napi_schedule(&pf->vsi[pf->lan_vsi]->q_vectors[0]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002940 }
2941
2942 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
2943 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2944 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
2945 }
2946
2947 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
2948 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
2949 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
2950 }
2951
2952 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
2953 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
2954 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
2955 }
2956
2957 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
2958 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
2959 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
2960 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
2961 val = rd32(hw, I40E_GLGEN_RSTAT);
2962 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
2963 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002964 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002965 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002966 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002967 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002968 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002969 pf->empr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002970 set_bit(__I40E_EMP_RESET_REQUESTED, &pf->state);
2971 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002972 }
2973
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002974 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
2975 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
2976 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
2977 }
2978
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002979 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
2980 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
2981
2982 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07002983 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002984 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002985 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002986 }
2987
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002988 /* If a critical error is pending we have no choice but to reset the
2989 * device.
2990 * Report and mask out any remaining unexpected interrupts.
2991 */
2992 icr0_remaining = icr0 & ena_mask;
2993 if (icr0_remaining) {
2994 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
2995 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002996 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002997 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00002998 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002999 dev_info(&pf->pdev->dev, "device will be reset\n");
3000 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
3001 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003002 }
3003 ena_mask &= ~icr0_remaining;
3004 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003005 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003006
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003007enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003008 /* re-enable interrupt causes */
3009 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003010 if (!test_bit(__I40E_DOWN, &pf->state)) {
3011 i40e_service_event_schedule(pf);
3012 i40e_irq_dynamic_enable_icr0(pf);
3013 }
3014
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003015 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003016}
3017
3018/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003019 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3020 * @tx_ring: tx ring to clean
3021 * @budget: how many cleans we're allowed
3022 *
3023 * Returns true if there's any budget left (e.g. the clean is finished)
3024 **/
3025static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3026{
3027 struct i40e_vsi *vsi = tx_ring->vsi;
3028 u16 i = tx_ring->next_to_clean;
3029 struct i40e_tx_buffer *tx_buf;
3030 struct i40e_tx_desc *tx_desc;
3031
3032 tx_buf = &tx_ring->tx_bi[i];
3033 tx_desc = I40E_TX_DESC(tx_ring, i);
3034 i -= tx_ring->count;
3035
3036 do {
3037 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3038
3039 /* if next_to_watch is not set then there is no work pending */
3040 if (!eop_desc)
3041 break;
3042
3043 /* prevent any other reads prior to eop_desc */
3044 read_barrier_depends();
3045
3046 /* if the descriptor isn't done, no work yet to do */
3047 if (!(eop_desc->cmd_type_offset_bsz &
3048 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3049 break;
3050
3051 /* clear next_to_watch to prevent false hangs */
3052 tx_buf->next_to_watch = NULL;
3053
3054 /* unmap skb header data */
3055 dma_unmap_single(tx_ring->dev,
3056 dma_unmap_addr(tx_buf, dma),
3057 dma_unmap_len(tx_buf, len),
3058 DMA_TO_DEVICE);
3059
3060 dma_unmap_len_set(tx_buf, len, 0);
3061
3062
3063 /* move to the next desc and buffer to clean */
3064 tx_buf++;
3065 tx_desc++;
3066 i++;
3067 if (unlikely(!i)) {
3068 i -= tx_ring->count;
3069 tx_buf = tx_ring->tx_bi;
3070 tx_desc = I40E_TX_DESC(tx_ring, 0);
3071 }
3072
3073 /* update budget accounting */
3074 budget--;
3075 } while (likely(budget));
3076
3077 i += tx_ring->count;
3078 tx_ring->next_to_clean = i;
3079
3080 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
3081 i40e_irq_dynamic_enable(vsi,
3082 tx_ring->q_vector->v_idx + vsi->base_vector);
3083 }
3084 return budget > 0;
3085}
3086
3087/**
3088 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3089 * @irq: interrupt number
3090 * @data: pointer to a q_vector
3091 **/
3092static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3093{
3094 struct i40e_q_vector *q_vector = data;
3095 struct i40e_vsi *vsi;
3096
3097 if (!q_vector->tx.ring)
3098 return IRQ_HANDLED;
3099
3100 vsi = q_vector->tx.ring->vsi;
3101 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3102
3103 return IRQ_HANDLED;
3104}
3105
3106/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003107 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003108 * @vsi: the VSI being configured
3109 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003110 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003111 **/
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003112static void map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003113{
Alexander Duyck493fb302013-09-28 07:01:44 +00003114 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00003115 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3116 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003117
3118 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003119 tx_ring->next = q_vector->tx.ring;
3120 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003121 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003122
3123 rx_ring->q_vector = q_vector;
3124 rx_ring->next = q_vector->rx.ring;
3125 q_vector->rx.ring = rx_ring;
3126 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003127}
3128
3129/**
3130 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3131 * @vsi: the VSI being configured
3132 *
3133 * This function maps descriptor rings to the queue-specific vectors
3134 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3135 * one vector per queue pair, but on a constrained vector budget, we
3136 * group the queue pairs as "efficiently" as possible.
3137 **/
3138static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3139{
3140 int qp_remaining = vsi->num_queue_pairs;
3141 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003142 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003143 int v_start = 0;
3144 int qp_idx = 0;
3145
3146 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3147 * group them so there are multiple queues per vector.
3148 */
3149 for (; v_start < q_vectors && qp_remaining; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003150 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3151
3152 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3153
3154 q_vector->num_ringpairs = num_ringpairs;
3155
3156 q_vector->rx.count = 0;
3157 q_vector->tx.count = 0;
3158 q_vector->rx.ring = NULL;
3159 q_vector->tx.ring = NULL;
3160
3161 while (num_ringpairs--) {
3162 map_vector_to_qp(vsi, v_start, qp_idx);
3163 qp_idx++;
3164 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003165 }
3166 }
3167}
3168
3169/**
3170 * i40e_vsi_request_irq - Request IRQ from the OS
3171 * @vsi: the VSI being configured
3172 * @basename: name for the vector
3173 **/
3174static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3175{
3176 struct i40e_pf *pf = vsi->back;
3177 int err;
3178
3179 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3180 err = i40e_vsi_request_irq_msix(vsi, basename);
3181 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3182 err = request_irq(pf->pdev->irq, i40e_intr, 0,
3183 pf->misc_int_name, pf);
3184 else
3185 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
3186 pf->misc_int_name, pf);
3187
3188 if (err)
3189 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3190
3191 return err;
3192}
3193
3194#ifdef CONFIG_NET_POLL_CONTROLLER
3195/**
3196 * i40e_netpoll - A Polling 'interrupt'handler
3197 * @netdev: network interface device structure
3198 *
3199 * This is used by netconsole to send skbs without having to re-enable
3200 * interrupts. It's not called while the normal interrupt routine is executing.
3201 **/
3202static void i40e_netpoll(struct net_device *netdev)
3203{
3204 struct i40e_netdev_priv *np = netdev_priv(netdev);
3205 struct i40e_vsi *vsi = np->vsi;
3206 struct i40e_pf *pf = vsi->back;
3207 int i;
3208
3209 /* if interface is down do nothing */
3210 if (test_bit(__I40E_DOWN, &vsi->state))
3211 return;
3212
3213 pf->flags |= I40E_FLAG_IN_NETPOLL;
3214 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3215 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003216 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003217 } else {
3218 i40e_intr(pf->pdev->irq, netdev);
3219 }
3220 pf->flags &= ~I40E_FLAG_IN_NETPOLL;
3221}
3222#endif
3223
3224/**
3225 * i40e_vsi_control_tx - Start or stop a VSI's rings
3226 * @vsi: the VSI being configured
3227 * @enable: start or stop the rings
3228 **/
3229static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3230{
3231 struct i40e_pf *pf = vsi->back;
3232 struct i40e_hw *hw = &pf->hw;
3233 int i, j, pf_q;
3234 u32 tx_reg;
3235
3236 pf_q = vsi->base_queue;
3237 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Matt Jared351499a2014-04-23 04:50:03 +00003238
3239 /* warn the TX unit of coming changes */
3240 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
3241 if (!enable)
3242 udelay(10);
3243
Mitch Williams6c5ef622014-02-20 19:29:16 -08003244 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003245 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003246 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3247 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3248 break;
3249 usleep_range(1000, 2000);
3250 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003251 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c122002014-03-14 07:32:29 +00003252 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00003253 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003254
3255 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003256 if (enable) {
3257 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003258 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003259 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003260 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003261 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003262
3263 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
3264
3265 /* wait for the change to finish */
3266 for (j = 0; j < 10; j++) {
3267 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Catherine Sullivan7c122002014-03-14 07:32:29 +00003268 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3269 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003270
3271 udelay(10);
3272 }
3273 if (j >= 10) {
3274 dev_info(&pf->pdev->dev, "Tx ring %d %sable timeout\n",
3275 pf_q, (enable ? "en" : "dis"));
3276 return -ETIMEDOUT;
3277 }
3278 }
3279
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003280 if (hw->revision_id == 0)
3281 mdelay(50);
3282
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003283 return 0;
3284}
3285
3286/**
3287 * i40e_vsi_control_rx - Start or stop a VSI's rings
3288 * @vsi: the VSI being configured
3289 * @enable: start or stop the rings
3290 **/
3291static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3292{
3293 struct i40e_pf *pf = vsi->back;
3294 struct i40e_hw *hw = &pf->hw;
3295 int i, j, pf_q;
3296 u32 rx_reg;
3297
3298 pf_q = vsi->base_queue;
3299 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003300 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003301 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003302 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
3303 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
3304 break;
3305 usleep_range(1000, 2000);
3306 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003307
Catherine Sullivan7c122002014-03-14 07:32:29 +00003308 /* Skip if the queue is already in the requested state */
3309 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3310 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003311
3312 /* turn on/off the queue */
3313 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08003314 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003315 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08003316 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003317 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
3318
3319 /* wait for the change to finish */
3320 for (j = 0; j < 10; j++) {
3321 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
3322
Catherine Sullivan7c122002014-03-14 07:32:29 +00003323 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3324 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003325
3326 udelay(10);
3327 }
3328 if (j >= 10) {
3329 dev_info(&pf->pdev->dev, "Rx ring %d %sable timeout\n",
3330 pf_q, (enable ? "en" : "dis"));
3331 return -ETIMEDOUT;
3332 }
3333 }
3334
3335 return 0;
3336}
3337
3338/**
3339 * i40e_vsi_control_rings - Start or stop a VSI's rings
3340 * @vsi: the VSI being configured
3341 * @enable: start or stop the rings
3342 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00003343int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003344{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003345 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003346
3347 /* do rx first for enable and last for disable */
3348 if (request) {
3349 ret = i40e_vsi_control_rx(vsi, request);
3350 if (ret)
3351 return ret;
3352 ret = i40e_vsi_control_tx(vsi, request);
3353 } else {
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003354 /* Ignore return value, we need to shutdown whatever we can */
3355 i40e_vsi_control_tx(vsi, request);
3356 i40e_vsi_control_rx(vsi, request);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003357 }
3358
3359 return ret;
3360}
3361
3362/**
3363 * i40e_vsi_free_irq - Free the irq association with the OS
3364 * @vsi: the VSI being configured
3365 **/
3366static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
3367{
3368 struct i40e_pf *pf = vsi->back;
3369 struct i40e_hw *hw = &pf->hw;
3370 int base = vsi->base_vector;
3371 u32 val, qp;
3372 int i;
3373
3374 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3375 if (!vsi->q_vectors)
3376 return;
3377
Shannon Nelson63741842014-04-23 04:50:16 +00003378 if (!vsi->irqs_ready)
3379 return;
3380
3381 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003382 for (i = 0; i < vsi->num_q_vectors; i++) {
3383 u16 vector = i + base;
3384
3385 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00003386 if (!vsi->q_vectors[i] ||
3387 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003388 continue;
3389
3390 /* clear the affinity_mask in the IRQ descriptor */
3391 irq_set_affinity_hint(pf->msix_entries[vector].vector,
3392 NULL);
3393 free_irq(pf->msix_entries[vector].vector,
Alexander Duyck493fb302013-09-28 07:01:44 +00003394 vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003395
3396 /* Tear down the interrupt queue link list
3397 *
3398 * We know that they come in pairs and always
3399 * the Rx first, then the Tx. To clear the
3400 * link list, stick the EOL value into the
3401 * next_q field of the registers.
3402 */
3403 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
3404 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3405 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3406 val |= I40E_QUEUE_END_OF_LIST
3407 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3408 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
3409
3410 while (qp != I40E_QUEUE_END_OF_LIST) {
3411 u32 next;
3412
3413 val = rd32(hw, I40E_QINT_RQCTL(qp));
3414
3415 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3416 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3417 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3418 I40E_QINT_RQCTL_INTEVENT_MASK);
3419
3420 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3421 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3422
3423 wr32(hw, I40E_QINT_RQCTL(qp), val);
3424
3425 val = rd32(hw, I40E_QINT_TQCTL(qp));
3426
3427 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
3428 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
3429
3430 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3431 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3432 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3433 I40E_QINT_TQCTL_INTEVENT_MASK);
3434
3435 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3436 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3437
3438 wr32(hw, I40E_QINT_TQCTL(qp), val);
3439 qp = next;
3440 }
3441 }
3442 } else {
3443 free_irq(pf->pdev->irq, pf);
3444
3445 val = rd32(hw, I40E_PFINT_LNKLST0);
3446 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3447 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3448 val |= I40E_QUEUE_END_OF_LIST
3449 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
3450 wr32(hw, I40E_PFINT_LNKLST0, val);
3451
3452 val = rd32(hw, I40E_QINT_RQCTL(qp));
3453 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3454 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3455 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3456 I40E_QINT_RQCTL_INTEVENT_MASK);
3457
3458 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3459 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3460
3461 wr32(hw, I40E_QINT_RQCTL(qp), val);
3462
3463 val = rd32(hw, I40E_QINT_TQCTL(qp));
3464
3465 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3466 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3467 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3468 I40E_QINT_TQCTL_INTEVENT_MASK);
3469
3470 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3471 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3472
3473 wr32(hw, I40E_QINT_TQCTL(qp), val);
3474 }
3475}
3476
3477/**
Alexander Duyck493fb302013-09-28 07:01:44 +00003478 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
3479 * @vsi: the VSI being configured
3480 * @v_idx: Index of vector to be freed
3481 *
3482 * This function frees the memory allocated to the q_vector. In addition if
3483 * NAPI is enabled it will delete any references to the NAPI struct prior
3484 * to freeing the q_vector.
3485 **/
3486static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
3487{
3488 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003489 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00003490
3491 if (!q_vector)
3492 return;
3493
3494 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003495 i40e_for_each_ring(ring, q_vector->tx)
3496 ring->q_vector = NULL;
3497
3498 i40e_for_each_ring(ring, q_vector->rx)
3499 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00003500
3501 /* only VSI w/ an associated netdev is set up w/ NAPI */
3502 if (vsi->netdev)
3503 netif_napi_del(&q_vector->napi);
3504
3505 vsi->q_vectors[v_idx] = NULL;
3506
3507 kfree_rcu(q_vector, rcu);
3508}
3509
3510/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003511 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
3512 * @vsi: the VSI being un-configured
3513 *
3514 * This frees the memory allocated to the q_vectors and
3515 * deletes references to the NAPI struct.
3516 **/
3517static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
3518{
3519 int v_idx;
3520
Alexander Duyck493fb302013-09-28 07:01:44 +00003521 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
3522 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003523}
3524
3525/**
3526 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
3527 * @pf: board private structure
3528 **/
3529static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
3530{
3531 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
3532 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3533 pci_disable_msix(pf->pdev);
3534 kfree(pf->msix_entries);
3535 pf->msix_entries = NULL;
3536 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
3537 pci_disable_msi(pf->pdev);
3538 }
3539 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
3540}
3541
3542/**
3543 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
3544 * @pf: board private structure
3545 *
3546 * We go through and clear interrupt specific resources and reset the structure
3547 * to pre-load conditions
3548 **/
3549static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
3550{
3551 int i;
3552
3553 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00003554 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003555 if (pf->vsi[i])
3556 i40e_vsi_free_q_vectors(pf->vsi[i]);
3557 i40e_reset_interrupt_capability(pf);
3558}
3559
3560/**
3561 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
3562 * @vsi: the VSI being configured
3563 **/
3564static void i40e_napi_enable_all(struct i40e_vsi *vsi)
3565{
3566 int q_idx;
3567
3568 if (!vsi->netdev)
3569 return;
3570
3571 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003572 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003573}
3574
3575/**
3576 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
3577 * @vsi: the VSI being configured
3578 **/
3579static void i40e_napi_disable_all(struct i40e_vsi *vsi)
3580{
3581 int q_idx;
3582
3583 if (!vsi->netdev)
3584 return;
3585
3586 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003587 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003588}
3589
3590/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00003591 * i40e_vsi_close - Shut down a VSI
3592 * @vsi: the vsi to be quelled
3593 **/
3594static void i40e_vsi_close(struct i40e_vsi *vsi)
3595{
3596 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
3597 i40e_down(vsi);
3598 i40e_vsi_free_irq(vsi);
3599 i40e_vsi_free_tx_resources(vsi);
3600 i40e_vsi_free_rx_resources(vsi);
3601}
3602
3603/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003604 * i40e_quiesce_vsi - Pause a given VSI
3605 * @vsi: the VSI being paused
3606 **/
3607static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
3608{
3609 if (test_bit(__I40E_DOWN, &vsi->state))
3610 return;
3611
3612 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
3613 if (vsi->netdev && netif_running(vsi->netdev)) {
3614 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
3615 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00003616 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003617 }
3618}
3619
3620/**
3621 * i40e_unquiesce_vsi - Resume a given VSI
3622 * @vsi: the VSI being resumed
3623 **/
3624static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
3625{
3626 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
3627 return;
3628
3629 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
3630 if (vsi->netdev && netif_running(vsi->netdev))
3631 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
3632 else
Shannon Nelson8276f752014-03-14 07:32:27 +00003633 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003634}
3635
3636/**
3637 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
3638 * @pf: the PF
3639 **/
3640static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
3641{
3642 int v;
3643
Mitch Williams505682c2014-05-20 08:01:37 +00003644 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003645 if (pf->vsi[v])
3646 i40e_quiesce_vsi(pf->vsi[v]);
3647 }
3648}
3649
3650/**
3651 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
3652 * @pf: the PF
3653 **/
3654static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
3655{
3656 int v;
3657
Mitch Williams505682c2014-05-20 08:01:37 +00003658 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003659 if (pf->vsi[v])
3660 i40e_unquiesce_vsi(pf->vsi[v]);
3661 }
3662}
3663
3664/**
3665 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
3666 * @dcbcfg: the corresponding DCBx configuration structure
3667 *
3668 * Return the number of TCs from given DCBx configuration
3669 **/
3670static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
3671{
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003672 u8 num_tc = 0;
3673 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003674
3675 /* Scan the ETS Config Priority Table to find
3676 * traffic class enabled for a given priority
3677 * and use the traffic class index to get the
3678 * number of traffic classes enabled
3679 */
3680 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3681 if (dcbcfg->etscfg.prioritytable[i] > num_tc)
3682 num_tc = dcbcfg->etscfg.prioritytable[i];
3683 }
3684
3685 /* Traffic class index starts from zero so
3686 * increment to return the actual count
3687 */
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003688 return num_tc + 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003689}
3690
3691/**
3692 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
3693 * @dcbcfg: the corresponding DCBx configuration structure
3694 *
3695 * Query the current DCB configuration and return the number of
3696 * traffic classes enabled from the given DCBX config
3697 **/
3698static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
3699{
3700 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
3701 u8 enabled_tc = 1;
3702 u8 i;
3703
3704 for (i = 0; i < num_tc; i++)
3705 enabled_tc |= 1 << i;
3706
3707 return enabled_tc;
3708}
3709
3710/**
3711 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
3712 * @pf: PF being queried
3713 *
3714 * Return number of traffic classes enabled for the given PF
3715 **/
3716static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
3717{
3718 struct i40e_hw *hw = &pf->hw;
3719 u8 i, enabled_tc;
3720 u8 num_tc = 0;
3721 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3722
3723 /* If DCB is not enabled then always in single TC */
3724 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3725 return 1;
3726
3727 /* MFP mode return count of enabled TCs for this PF */
3728 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
3729 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3730 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3731 if (enabled_tc & (1 << i))
3732 num_tc++;
3733 }
3734 return num_tc;
3735 }
3736
3737 /* SFP mode will be enabled for all TCs on port */
3738 return i40e_dcb_get_num_tc(dcbcfg);
3739}
3740
3741/**
3742 * i40e_pf_get_default_tc - Get bitmap for first enabled TC
3743 * @pf: PF being queried
3744 *
3745 * Return a bitmap for first enabled traffic class for this PF.
3746 **/
3747static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
3748{
3749 u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3750 u8 i = 0;
3751
3752 if (!enabled_tc)
3753 return 0x1; /* TC0 */
3754
3755 /* Find the first enabled TC */
3756 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3757 if (enabled_tc & (1 << i))
3758 break;
3759 }
3760
3761 return 1 << i;
3762}
3763
3764/**
3765 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
3766 * @pf: PF being queried
3767 *
3768 * Return a bitmap for enabled traffic classes for this PF.
3769 **/
3770static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
3771{
3772 /* If DCB is not enabled for this PF then just return default TC */
3773 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3774 return i40e_pf_get_default_tc(pf);
3775
3776 /* MFP mode will have enabled TCs set by FW */
3777 if (pf->flags & I40E_FLAG_MFP_ENABLED)
3778 return pf->hw.func_caps.enabled_tcmap;
3779
3780 /* SFP mode we want PF to be enabled for all TCs */
3781 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
3782}
3783
3784/**
3785 * i40e_vsi_get_bw_info - Query VSI BW Information
3786 * @vsi: the VSI being queried
3787 *
3788 * Returns 0 on success, negative value on failure
3789 **/
3790static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
3791{
3792 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
3793 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
3794 struct i40e_pf *pf = vsi->back;
3795 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003796 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003797 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003798 int i;
3799
3800 /* Get the VSI level BW configuration */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003801 aq_ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
3802 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003803 dev_info(&pf->pdev->dev,
3804 "couldn't get pf vsi bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003805 aq_ret, pf->hw.aq.asq_last_status);
3806 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003807 }
3808
3809 /* Get the VSI level BW configuration per TC */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003810 aq_ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
Jesse Brandeburg6838b532014-01-14 00:49:52 -08003811 NULL);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003812 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003813 dev_info(&pf->pdev->dev,
3814 "couldn't get pf vsi ets bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003815 aq_ret, pf->hw.aq.asq_last_status);
3816 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003817 }
3818
3819 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
3820 dev_info(&pf->pdev->dev,
3821 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
3822 bw_config.tc_valid_bits,
3823 bw_ets_config.tc_valid_bits);
3824 /* Still continuing */
3825 }
3826
3827 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
3828 vsi->bw_max_quanta = bw_config.max_bw;
3829 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
3830 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
3831 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3832 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
3833 vsi->bw_ets_limit_credits[i] =
3834 le16_to_cpu(bw_ets_config.credits[i]);
3835 /* 3 bits out of 4 for each TC */
3836 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
3837 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003838
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003839 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003840}
3841
3842/**
3843 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
3844 * @vsi: the VSI being configured
3845 * @enabled_tc: TC bitmap
3846 * @bw_credits: BW shared credits per TC
3847 *
3848 * Returns 0 on success, negative value on failure
3849 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003850static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003851 u8 *bw_share)
3852{
3853 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003854 i40e_status aq_ret;
3855 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003856
3857 bw_data.tc_valid_bits = enabled_tc;
3858 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3859 bw_data.tc_bw_credits[i] = bw_share[i];
3860
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003861 aq_ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
3862 NULL);
3863 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003864 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00003865 "AQ command Config VSI BW allocation per TC failed = %d\n",
3866 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003867 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003868 }
3869
3870 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3871 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
3872
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003873 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003874}
3875
3876/**
3877 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
3878 * @vsi: the VSI being configured
3879 * @enabled_tc: TC map to be enabled
3880 *
3881 **/
3882static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3883{
3884 struct net_device *netdev = vsi->netdev;
3885 struct i40e_pf *pf = vsi->back;
3886 struct i40e_hw *hw = &pf->hw;
3887 u8 netdev_tc = 0;
3888 int i;
3889 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3890
3891 if (!netdev)
3892 return;
3893
3894 if (!enabled_tc) {
3895 netdev_reset_tc(netdev);
3896 return;
3897 }
3898
3899 /* Set up actual enabled TCs on the VSI */
3900 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
3901 return;
3902
3903 /* set per TC queues for the VSI */
3904 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3905 /* Only set TC queues for enabled tcs
3906 *
3907 * e.g. For a VSI that has TC0 and TC3 enabled the
3908 * enabled_tc bitmap would be 0x00001001; the driver
3909 * will set the numtc for netdev as 2 that will be
3910 * referenced by the netdev layer as TC 0 and 1.
3911 */
3912 if (vsi->tc_config.enabled_tc & (1 << i))
3913 netdev_set_tc_queue(netdev,
3914 vsi->tc_config.tc_info[i].netdev_tc,
3915 vsi->tc_config.tc_info[i].qcount,
3916 vsi->tc_config.tc_info[i].qoffset);
3917 }
3918
3919 /* Assign UP2TC map for the VSI */
3920 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3921 /* Get the actual TC# for the UP */
3922 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
3923 /* Get the mapped netdev TC# for the UP */
3924 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
3925 netdev_set_prio_tc_map(netdev, i, netdev_tc);
3926 }
3927}
3928
3929/**
3930 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
3931 * @vsi: the VSI being configured
3932 * @ctxt: the ctxt buffer returned from AQ VSI update param command
3933 **/
3934static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
3935 struct i40e_vsi_context *ctxt)
3936{
3937 /* copy just the sections touched not the entire info
3938 * since not all sections are valid as returned by
3939 * update vsi params
3940 */
3941 vsi->info.mapping_flags = ctxt->info.mapping_flags;
3942 memcpy(&vsi->info.queue_mapping,
3943 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
3944 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
3945 sizeof(vsi->info.tc_mapping));
3946}
3947
3948/**
3949 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
3950 * @vsi: VSI to be configured
3951 * @enabled_tc: TC bitmap
3952 *
3953 * This configures a particular VSI for TCs that are mapped to the
3954 * given TC bitmap. It uses default bandwidth share for TCs across
3955 * VSIs to configure TC for a particular VSI.
3956 *
3957 * NOTE:
3958 * It is expected that the VSI queues have been quisced before calling
3959 * this function.
3960 **/
3961static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3962{
3963 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
3964 struct i40e_vsi_context ctxt;
3965 int ret = 0;
3966 int i;
3967
3968 /* Check if enabled_tc is same as existing or new TCs */
3969 if (vsi->tc_config.enabled_tc == enabled_tc)
3970 return ret;
3971
3972 /* Enable ETS TCs with equal BW Share for now across all VSIs */
3973 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3974 if (enabled_tc & (1 << i))
3975 bw_share[i] = 1;
3976 }
3977
3978 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
3979 if (ret) {
3980 dev_info(&vsi->back->pdev->dev,
3981 "Failed configuring TC map %d for VSI %d\n",
3982 enabled_tc, vsi->seid);
3983 goto out;
3984 }
3985
3986 /* Update Queue Pairs Mapping for currently enabled UPs */
3987 ctxt.seid = vsi->seid;
3988 ctxt.pf_num = vsi->back->hw.pf_id;
3989 ctxt.vf_num = 0;
3990 ctxt.uplink_seid = vsi->uplink_seid;
3991 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
3992 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
3993
3994 /* Update the VSI after updating the VSI queue-mapping information */
3995 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3996 if (ret) {
3997 dev_info(&vsi->back->pdev->dev,
3998 "update vsi failed, aq_err=%d\n",
3999 vsi->back->hw.aq.asq_last_status);
4000 goto out;
4001 }
4002 /* update the local VSI info with updated queue map */
4003 i40e_vsi_update_queue_map(vsi, &ctxt);
4004 vsi->info.valid_sections = 0;
4005
4006 /* Update current VSI BW information */
4007 ret = i40e_vsi_get_bw_info(vsi);
4008 if (ret) {
4009 dev_info(&vsi->back->pdev->dev,
4010 "Failed updating vsi bw info, aq_err=%d\n",
4011 vsi->back->hw.aq.asq_last_status);
4012 goto out;
4013 }
4014
4015 /* Update the netdev TC setup */
4016 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
4017out:
4018 return ret;
4019}
4020
4021/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004022 * i40e_veb_config_tc - Configure TCs for given VEB
4023 * @veb: given VEB
4024 * @enabled_tc: TC bitmap
4025 *
4026 * Configures given TC bitmap for VEB (switching) element
4027 **/
4028int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
4029{
4030 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
4031 struct i40e_pf *pf = veb->pf;
4032 int ret = 0;
4033 int i;
4034
4035 /* No TCs or already enabled TCs just return */
4036 if (!enabled_tc || veb->enabled_tc == enabled_tc)
4037 return ret;
4038
4039 bw_data.tc_valid_bits = enabled_tc;
4040 /* bw_data.absolute_credits is not set (relative) */
4041
4042 /* Enable ETS TCs with equal BW Share for now */
4043 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4044 if (enabled_tc & (1 << i))
4045 bw_data.tc_bw_share_credits[i] = 1;
4046 }
4047
4048 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
4049 &bw_data, NULL);
4050 if (ret) {
4051 dev_info(&pf->pdev->dev,
4052 "veb bw config failed, aq_err=%d\n",
4053 pf->hw.aq.asq_last_status);
4054 goto out;
4055 }
4056
4057 /* Update the BW information */
4058 ret = i40e_veb_get_bw_info(veb);
4059 if (ret) {
4060 dev_info(&pf->pdev->dev,
4061 "Failed getting veb bw config, aq_err=%d\n",
4062 pf->hw.aq.asq_last_status);
4063 }
4064
4065out:
4066 return ret;
4067}
4068
4069#ifdef CONFIG_I40E_DCB
4070/**
4071 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
4072 * @pf: PF struct
4073 *
4074 * Reconfigure VEB/VSIs on a given PF; it is assumed that
4075 * the caller would've quiesce all the VSIs before calling
4076 * this function
4077 **/
4078static void i40e_dcb_reconfigure(struct i40e_pf *pf)
4079{
4080 u8 tc_map = 0;
4081 int ret;
4082 u8 v;
4083
4084 /* Enable the TCs available on PF to all VEBs */
4085 tc_map = i40e_pf_get_tc_map(pf);
4086 for (v = 0; v < I40E_MAX_VEB; v++) {
4087 if (!pf->veb[v])
4088 continue;
4089 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
4090 if (ret) {
4091 dev_info(&pf->pdev->dev,
4092 "Failed configuring TC for VEB seid=%d\n",
4093 pf->veb[v]->seid);
4094 /* Will try to configure as many components */
4095 }
4096 }
4097
4098 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00004099 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004100 if (!pf->vsi[v])
4101 continue;
4102
4103 /* - Enable all TCs for the LAN VSI
4104 * - For all others keep them at TC0 for now
4105 */
4106 if (v == pf->lan_vsi)
4107 tc_map = i40e_pf_get_tc_map(pf);
4108 else
4109 tc_map = i40e_pf_get_default_tc(pf);
4110
4111 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
4112 if (ret) {
4113 dev_info(&pf->pdev->dev,
4114 "Failed configuring TC for VSI seid=%d\n",
4115 pf->vsi[v]->seid);
4116 /* Will try to configure as many components */
4117 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00004118 /* Re-configure VSI vectors based on updated TC map */
4119 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004120 if (pf->vsi[v]->netdev)
4121 i40e_dcbnl_set_all(pf->vsi[v]);
4122 }
4123 }
4124}
4125
4126/**
4127 * i40e_init_pf_dcb - Initialize DCB configuration
4128 * @pf: PF being configured
4129 *
4130 * Query the current DCB configuration and cache it
4131 * in the hardware structure
4132 **/
4133static int i40e_init_pf_dcb(struct i40e_pf *pf)
4134{
4135 struct i40e_hw *hw = &pf->hw;
4136 int err = 0;
4137
4138 if (pf->hw.func_caps.npar_enable)
4139 goto out;
4140
4141 /* Get the initial DCB configuration */
4142 err = i40e_init_dcb(hw);
4143 if (!err) {
4144 /* Device/Function is not DCBX capable */
4145 if ((!hw->func_caps.dcb) ||
4146 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
4147 dev_info(&pf->pdev->dev,
4148 "DCBX offload is not supported or is disabled for this PF.\n");
4149
4150 if (pf->flags & I40E_FLAG_MFP_ENABLED)
4151 goto out;
4152
4153 } else {
4154 /* When status is not DISABLED then DCBX in FW */
4155 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
4156 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00004157
4158 pf->flags |= I40E_FLAG_DCB_CAPABLE;
4159 /* Enable DCB tagging only when more than one TC */
4160 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
4161 pf->flags |= I40E_FLAG_DCB_ENABLED;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004162 }
Neerav Parikh014269f2014-04-01 07:11:48 +00004163 } else {
4164 dev_info(&pf->pdev->dev, "AQ Querying DCB configuration failed: %d\n",
4165 pf->hw.aq.asq_last_status);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004166 }
4167
4168out:
4169 return err;
4170}
4171#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004172#define SPEED_SIZE 14
4173#define FC_SIZE 8
4174/**
4175 * i40e_print_link_message - print link up or down
4176 * @vsi: the VSI for which link needs a message
4177 */
4178static void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
4179{
4180 char speed[SPEED_SIZE] = "Unknown";
4181 char fc[FC_SIZE] = "RX/TX";
4182
4183 if (!isup) {
4184 netdev_info(vsi->netdev, "NIC Link is Down\n");
4185 return;
4186 }
4187
4188 switch (vsi->back->hw.phy.link_info.link_speed) {
4189 case I40E_LINK_SPEED_40GB:
4190 strncpy(speed, "40 Gbps", SPEED_SIZE);
4191 break;
4192 case I40E_LINK_SPEED_10GB:
4193 strncpy(speed, "10 Gbps", SPEED_SIZE);
4194 break;
4195 case I40E_LINK_SPEED_1GB:
4196 strncpy(speed, "1000 Mbps", SPEED_SIZE);
4197 break;
4198 default:
4199 break;
4200 }
4201
4202 switch (vsi->back->hw.fc.current_mode) {
4203 case I40E_FC_FULL:
4204 strncpy(fc, "RX/TX", FC_SIZE);
4205 break;
4206 case I40E_FC_TX_PAUSE:
4207 strncpy(fc, "TX", FC_SIZE);
4208 break;
4209 case I40E_FC_RX_PAUSE:
4210 strncpy(fc, "RX", FC_SIZE);
4211 break;
4212 default:
4213 strncpy(fc, "None", FC_SIZE);
4214 break;
4215 }
4216
4217 netdev_info(vsi->netdev, "NIC Link is Up %s Full Duplex, Flow Control: %s\n",
4218 speed, fc);
4219}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004220
4221/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004222 * i40e_up_complete - Finish the last steps of bringing up a connection
4223 * @vsi: the VSI being configured
4224 **/
4225static int i40e_up_complete(struct i40e_vsi *vsi)
4226{
4227 struct i40e_pf *pf = vsi->back;
4228 int err;
4229
4230 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4231 i40e_vsi_configure_msix(vsi);
4232 else
4233 i40e_configure_msi_and_legacy(vsi);
4234
4235 /* start rings */
4236 err = i40e_vsi_control_rings(vsi, true);
4237 if (err)
4238 return err;
4239
4240 clear_bit(__I40E_DOWN, &vsi->state);
4241 i40e_napi_enable_all(vsi);
4242 i40e_vsi_enable_irq(vsi);
4243
4244 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
4245 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004246 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004247 netif_tx_start_all_queues(vsi->netdev);
4248 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00004249 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004250 i40e_print_link_message(vsi, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004251 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00004252
4253 /* replay FDIR SB filters */
4254 if (vsi->type == I40E_VSI_FDIR)
4255 i40e_fdir_filter_restore(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004256 i40e_service_event_schedule(pf);
4257
4258 return 0;
4259}
4260
4261/**
4262 * i40e_vsi_reinit_locked - Reset the VSI
4263 * @vsi: the VSI being configured
4264 *
4265 * Rebuild the ring structs after some configuration
4266 * has changed, e.g. MTU size.
4267 **/
4268static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
4269{
4270 struct i40e_pf *pf = vsi->back;
4271
4272 WARN_ON(in_interrupt());
4273 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
4274 usleep_range(1000, 2000);
4275 i40e_down(vsi);
4276
4277 /* Give a VF some time to respond to the reset. The
4278 * two second wait is based upon the watchdog cycle in
4279 * the VF driver.
4280 */
4281 if (vsi->type == I40E_VSI_SRIOV)
4282 msleep(2000);
4283 i40e_up(vsi);
4284 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
4285}
4286
4287/**
4288 * i40e_up - Bring the connection back up after being down
4289 * @vsi: the VSI being configured
4290 **/
4291int i40e_up(struct i40e_vsi *vsi)
4292{
4293 int err;
4294
4295 err = i40e_vsi_configure(vsi);
4296 if (!err)
4297 err = i40e_up_complete(vsi);
4298
4299 return err;
4300}
4301
4302/**
4303 * i40e_down - Shutdown the connection processing
4304 * @vsi: the VSI being stopped
4305 **/
4306void i40e_down(struct i40e_vsi *vsi)
4307{
4308 int i;
4309
4310 /* It is assumed that the caller of this function
4311 * sets the vsi->state __I40E_DOWN bit.
4312 */
4313 if (vsi->netdev) {
4314 netif_carrier_off(vsi->netdev);
4315 netif_tx_disable(vsi->netdev);
4316 }
4317 i40e_vsi_disable_irq(vsi);
4318 i40e_vsi_control_rings(vsi, false);
4319 i40e_napi_disable_all(vsi);
4320
4321 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004322 i40e_clean_tx_ring(vsi->tx_rings[i]);
4323 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004324 }
4325}
4326
4327/**
4328 * i40e_setup_tc - configure multiple traffic classes
4329 * @netdev: net device to configure
4330 * @tc: number of traffic classes to enable
4331 **/
4332static int i40e_setup_tc(struct net_device *netdev, u8 tc)
4333{
4334 struct i40e_netdev_priv *np = netdev_priv(netdev);
4335 struct i40e_vsi *vsi = np->vsi;
4336 struct i40e_pf *pf = vsi->back;
4337 u8 enabled_tc = 0;
4338 int ret = -EINVAL;
4339 int i;
4340
4341 /* Check if DCB enabled to continue */
4342 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
4343 netdev_info(netdev, "DCB is not enabled for adapter\n");
4344 goto exit;
4345 }
4346
4347 /* Check if MFP enabled */
4348 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
4349 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
4350 goto exit;
4351 }
4352
4353 /* Check whether tc count is within enabled limit */
4354 if (tc > i40e_pf_get_num_tc(pf)) {
4355 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
4356 goto exit;
4357 }
4358
4359 /* Generate TC map for number of tc requested */
4360 for (i = 0; i < tc; i++)
4361 enabled_tc |= (1 << i);
4362
4363 /* Requesting same TC configuration as already enabled */
4364 if (enabled_tc == vsi->tc_config.enabled_tc)
4365 return 0;
4366
4367 /* Quiesce VSI queues */
4368 i40e_quiesce_vsi(vsi);
4369
4370 /* Configure VSI for enabled TCs */
4371 ret = i40e_vsi_config_tc(vsi, enabled_tc);
4372 if (ret) {
4373 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
4374 vsi->seid);
4375 goto exit;
4376 }
4377
4378 /* Unquiesce VSI */
4379 i40e_unquiesce_vsi(vsi);
4380
4381exit:
4382 return ret;
4383}
4384
4385/**
4386 * i40e_open - Called when a network interface is made active
4387 * @netdev: network interface device structure
4388 *
4389 * The open entry point is called when a network interface is made
4390 * active by the system (IFF_UP). At this point all resources needed
4391 * for transmit and receive operations are allocated, the interrupt
4392 * handler is registered with the OS, the netdev watchdog subtask is
4393 * enabled, and the stack is notified that the interface is ready.
4394 *
4395 * Returns 0 on success, negative value on failure
4396 **/
4397static int i40e_open(struct net_device *netdev)
4398{
4399 struct i40e_netdev_priv *np = netdev_priv(netdev);
4400 struct i40e_vsi *vsi = np->vsi;
4401 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004402 int err;
4403
Shannon Nelson4eb3f762014-03-06 08:59:58 +00004404 /* disallow open during test or if eeprom is broken */
4405 if (test_bit(__I40E_TESTING, &pf->state) ||
4406 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004407 return -EBUSY;
4408
4409 netif_carrier_off(netdev);
4410
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004411 err = i40e_vsi_open(vsi);
4412 if (err)
4413 return err;
4414
Jesse Brandeburg059dab62014-04-01 09:07:20 +00004415 /* configure global TSO hardware offload settings */
4416 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
4417 TCP_FLAG_FIN) >> 16);
4418 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
4419 TCP_FLAG_FIN |
4420 TCP_FLAG_CWR) >> 16);
4421 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
4422
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004423#ifdef CONFIG_I40E_VXLAN
4424 vxlan_get_rx_port(netdev);
4425#endif
4426
4427 return 0;
4428}
4429
4430/**
4431 * i40e_vsi_open -
4432 * @vsi: the VSI to open
4433 *
4434 * Finish initialization of the VSI.
4435 *
4436 * Returns 0 on success, negative value on failure
4437 **/
4438int i40e_vsi_open(struct i40e_vsi *vsi)
4439{
4440 struct i40e_pf *pf = vsi->back;
4441 char int_name[IFNAMSIZ];
4442 int err;
4443
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004444 /* allocate descriptors */
4445 err = i40e_vsi_setup_tx_resources(vsi);
4446 if (err)
4447 goto err_setup_tx;
4448 err = i40e_vsi_setup_rx_resources(vsi);
4449 if (err)
4450 goto err_setup_rx;
4451
4452 err = i40e_vsi_configure(vsi);
4453 if (err)
4454 goto err_setup_rx;
4455
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00004456 if (vsi->netdev) {
4457 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
4458 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
4459 err = i40e_vsi_request_irq(vsi, int_name);
4460 if (err)
4461 goto err_setup_rx;
4462
4463 /* Notify the stack of the actual queue counts. */
4464 err = netif_set_real_num_tx_queues(vsi->netdev,
4465 vsi->num_queue_pairs);
4466 if (err)
4467 goto err_set_queues;
4468
4469 err = netif_set_real_num_rx_queues(vsi->netdev,
4470 vsi->num_queue_pairs);
4471 if (err)
4472 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00004473
4474 } else if (vsi->type == I40E_VSI_FDIR) {
4475 snprintf(int_name, sizeof(int_name) - 1, "%s-fdir",
4476 dev_driver_string(&pf->pdev->dev));
4477 err = i40e_vsi_request_irq(vsi, int_name);
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00004478 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00004479 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004480 goto err_setup_rx;
4481 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004482
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004483 err = i40e_up_complete(vsi);
4484 if (err)
4485 goto err_up_complete;
4486
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004487 return 0;
4488
4489err_up_complete:
4490 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004491err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004492 i40e_vsi_free_irq(vsi);
4493err_setup_rx:
4494 i40e_vsi_free_rx_resources(vsi);
4495err_setup_tx:
4496 i40e_vsi_free_tx_resources(vsi);
4497 if (vsi == pf->vsi[pf->lan_vsi])
4498 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
4499
4500 return err;
4501}
4502
4503/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00004504 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
4505 * @pf: Pointer to pf
4506 *
4507 * This function destroys the hlist where all the Flow Director
4508 * filters were saved.
4509 **/
4510static void i40e_fdir_filter_exit(struct i40e_pf *pf)
4511{
4512 struct i40e_fdir_filter *filter;
4513 struct hlist_node *node2;
4514
4515 hlist_for_each_entry_safe(filter, node2,
4516 &pf->fdir_filter_list, fdir_node) {
4517 hlist_del(&filter->fdir_node);
4518 kfree(filter);
4519 }
4520 pf->fdir_pf_active_filters = 0;
4521}
4522
4523/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004524 * i40e_close - Disables a network interface
4525 * @netdev: network interface device structure
4526 *
4527 * The close entry point is called when an interface is de-activated
4528 * by the OS. The hardware is still under the driver's control, but
4529 * this netdev interface is disabled.
4530 *
4531 * Returns 0, this is not allowed to fail
4532 **/
4533static int i40e_close(struct net_device *netdev)
4534{
4535 struct i40e_netdev_priv *np = netdev_priv(netdev);
4536 struct i40e_vsi *vsi = np->vsi;
4537
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004538 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004539
4540 return 0;
4541}
4542
4543/**
4544 * i40e_do_reset - Start a PF or Core Reset sequence
4545 * @pf: board private structure
4546 * @reset_flags: which reset is requested
4547 *
4548 * The essential difference in resets is that the PF Reset
4549 * doesn't clear the packet buffers, doesn't reset the PE
4550 * firmware, and doesn't bother the other PFs on the chip.
4551 **/
4552void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
4553{
4554 u32 val;
4555
4556 WARN_ON(in_interrupt());
4557
Mitch Williams263fc482014-04-23 04:50:11 +00004558 if (i40e_check_asq_alive(&pf->hw))
4559 i40e_vc_notify_reset(pf);
4560
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004561 /* do the biggest reset indicated */
4562 if (reset_flags & (1 << __I40E_GLOBAL_RESET_REQUESTED)) {
4563
4564 /* Request a Global Reset
4565 *
4566 * This will start the chip's countdown to the actual full
4567 * chip reset event, and a warning interrupt to be sent
4568 * to all PFs, including the requestor. Our handler
4569 * for the warning interrupt will deal with the shutdown
4570 * and recovery of the switch setup.
4571 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004572 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004573 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4574 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
4575 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4576
4577 } else if (reset_flags & (1 << __I40E_CORE_RESET_REQUESTED)) {
4578
4579 /* Request a Core Reset
4580 *
4581 * Same as Global Reset, except does *not* include the MAC/PHY
4582 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004583 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004584 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4585 val |= I40E_GLGEN_RTRIG_CORER_MASK;
4586 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4587 i40e_flush(&pf->hw);
4588
Shannon Nelson7823fe32013-11-16 10:00:45 +00004589 } else if (reset_flags & (1 << __I40E_EMP_RESET_REQUESTED)) {
4590
4591 /* Request a Firmware Reset
4592 *
4593 * Same as Global reset, plus restarting the
4594 * embedded firmware engine.
4595 */
4596 /* enable EMP Reset */
4597 val = rd32(&pf->hw, I40E_GLGEN_RSTENA_EMP);
4598 val |= I40E_GLGEN_RSTENA_EMP_EMP_RST_ENA_MASK;
4599 wr32(&pf->hw, I40E_GLGEN_RSTENA_EMP, val);
4600
4601 /* force the reset */
4602 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4603 val |= I40E_GLGEN_RTRIG_EMPFWR_MASK;
4604 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4605 i40e_flush(&pf->hw);
4606
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004607 } else if (reset_flags & (1 << __I40E_PF_RESET_REQUESTED)) {
4608
4609 /* Request a PF Reset
4610 *
4611 * Resets only the PF-specific registers
4612 *
4613 * This goes directly to the tear-down and rebuild of
4614 * the switch, since we need to do all the recovery as
4615 * for the Core Reset.
4616 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004617 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004618 i40e_handle_reset_warning(pf);
4619
4620 } else if (reset_flags & (1 << __I40E_REINIT_REQUESTED)) {
4621 int v;
4622
4623 /* Find the VSI(s) that requested a re-init */
4624 dev_info(&pf->pdev->dev,
4625 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00004626 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004627 struct i40e_vsi *vsi = pf->vsi[v];
4628 if (vsi != NULL &&
4629 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
4630 i40e_vsi_reinit_locked(pf->vsi[v]);
4631 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
4632 }
4633 }
4634
4635 /* no further action needed, so return now */
4636 return;
4637 } else {
4638 dev_info(&pf->pdev->dev,
4639 "bad reset request 0x%08x\n", reset_flags);
4640 return;
4641 }
4642}
4643
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004644#ifdef CONFIG_I40E_DCB
4645/**
4646 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
4647 * @pf: board private structure
4648 * @old_cfg: current DCB config
4649 * @new_cfg: new DCB config
4650 **/
4651bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
4652 struct i40e_dcbx_config *old_cfg,
4653 struct i40e_dcbx_config *new_cfg)
4654{
4655 bool need_reconfig = false;
4656
4657 /* Check if ETS configuration has changed */
4658 if (memcmp(&new_cfg->etscfg,
4659 &old_cfg->etscfg,
4660 sizeof(new_cfg->etscfg))) {
4661 /* If Priority Table has changed reconfig is needed */
4662 if (memcmp(&new_cfg->etscfg.prioritytable,
4663 &old_cfg->etscfg.prioritytable,
4664 sizeof(new_cfg->etscfg.prioritytable))) {
4665 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004666 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004667 }
4668
4669 if (memcmp(&new_cfg->etscfg.tcbwtable,
4670 &old_cfg->etscfg.tcbwtable,
4671 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004672 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004673
4674 if (memcmp(&new_cfg->etscfg.tsatable,
4675 &old_cfg->etscfg.tsatable,
4676 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004677 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004678 }
4679
4680 /* Check if PFC configuration has changed */
4681 if (memcmp(&new_cfg->pfc,
4682 &old_cfg->pfc,
4683 sizeof(new_cfg->pfc))) {
4684 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004685 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004686 }
4687
4688 /* Check if APP Table has changed */
4689 if (memcmp(&new_cfg->app,
4690 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05004691 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004692 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004693 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05004694 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004695
4696 return need_reconfig;
4697}
4698
4699/**
4700 * i40e_handle_lldp_event - Handle LLDP Change MIB event
4701 * @pf: board private structure
4702 * @e: event info posted on ARQ
4703 **/
4704static int i40e_handle_lldp_event(struct i40e_pf *pf,
4705 struct i40e_arq_event_info *e)
4706{
4707 struct i40e_aqc_lldp_get_mib *mib =
4708 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
4709 struct i40e_hw *hw = &pf->hw;
4710 struct i40e_dcbx_config *dcbx_cfg = &hw->local_dcbx_config;
4711 struct i40e_dcbx_config tmp_dcbx_cfg;
4712 bool need_reconfig = false;
4713 int ret = 0;
4714 u8 type;
4715
Neerav Parikh4d9b6042014-05-22 06:31:51 +00004716 /* Not DCB capable or capability disabled */
4717 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
4718 return ret;
4719
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004720 /* Ignore if event is not for Nearest Bridge */
4721 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
4722 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
4723 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
4724 return ret;
4725
4726 /* Check MIB Type and return if event for Remote MIB update */
4727 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
4728 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
4729 /* Update the remote cached instance and return */
4730 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
4731 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
4732 &hw->remote_dcbx_config);
4733 goto exit;
4734 }
4735
4736 /* Convert/store the DCBX data from LLDPDU temporarily */
4737 memset(&tmp_dcbx_cfg, 0, sizeof(tmp_dcbx_cfg));
4738 ret = i40e_lldp_to_dcb_config(e->msg_buf, &tmp_dcbx_cfg);
4739 if (ret) {
4740 /* Error in LLDPDU parsing return */
4741 dev_info(&pf->pdev->dev, "Failed parsing LLDPDU from event buffer\n");
4742 goto exit;
4743 }
4744
4745 /* No change detected in DCBX configs */
4746 if (!memcmp(&tmp_dcbx_cfg, dcbx_cfg, sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004747 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004748 goto exit;
4749 }
4750
4751 need_reconfig = i40e_dcb_need_reconfig(pf, dcbx_cfg, &tmp_dcbx_cfg);
4752
4753 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg);
4754
4755 /* Overwrite the new configuration */
4756 *dcbx_cfg = tmp_dcbx_cfg;
4757
4758 if (!need_reconfig)
4759 goto exit;
4760
Neerav Parikh4d9b6042014-05-22 06:31:51 +00004761 /* Enable DCB tagging only when more than one TC */
4762 if (i40e_dcb_get_num_tc(dcbx_cfg) > 1)
4763 pf->flags |= I40E_FLAG_DCB_ENABLED;
4764 else
4765 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
4766
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004767 /* Reconfiguration needed quiesce all VSIs */
4768 i40e_pf_quiesce_all_vsi(pf);
4769
4770 /* Changes in configuration update VEB/VSI */
4771 i40e_dcb_reconfigure(pf);
4772
4773 i40e_pf_unquiesce_all_vsi(pf);
4774exit:
4775 return ret;
4776}
4777#endif /* CONFIG_I40E_DCB */
4778
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004779/**
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004780 * i40e_do_reset_safe - Protected reset path for userland calls.
4781 * @pf: board private structure
4782 * @reset_flags: which reset is requested
4783 *
4784 **/
4785void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
4786{
4787 rtnl_lock();
4788 i40e_do_reset(pf, reset_flags);
4789 rtnl_unlock();
4790}
4791
4792/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004793 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
4794 * @pf: board private structure
4795 * @e: event info posted on ARQ
4796 *
4797 * Handler for LAN Queue Overflow Event generated by the firmware for PF
4798 * and VF queues
4799 **/
4800static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
4801 struct i40e_arq_event_info *e)
4802{
4803 struct i40e_aqc_lan_overflow *data =
4804 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
4805 u32 queue = le32_to_cpu(data->prtdcb_rupto);
4806 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
4807 struct i40e_hw *hw = &pf->hw;
4808 struct i40e_vf *vf;
4809 u16 vf_id;
4810
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004811 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
4812 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004813
4814 /* Queue belongs to VF, find the VF and issue VF reset */
4815 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
4816 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
4817 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
4818 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
4819 vf_id -= hw->func_caps.vf_base_id;
4820 vf = &pf->vf[vf_id];
4821 i40e_vc_notify_vf_reset(vf);
4822 /* Allow VF to process pending reset notification */
4823 msleep(20);
4824 i40e_reset_vf(vf, false);
4825 }
4826}
4827
4828/**
4829 * i40e_service_event_complete - Finish up the service event
4830 * @pf: board private structure
4831 **/
4832static void i40e_service_event_complete(struct i40e_pf *pf)
4833{
4834 BUG_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
4835
4836 /* flush memory to make sure state is correct before next watchog */
4837 smp_mb__before_clear_bit();
4838 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
4839}
4840
4841/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004842 * i40e_get_current_fd_count - Get the count of FD filters programmed in the HW
4843 * @pf: board private structure
4844 **/
4845int i40e_get_current_fd_count(struct i40e_pf *pf)
4846{
4847 int val, fcnt_prog;
4848 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
4849 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
4850 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
4851 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
4852 return fcnt_prog;
4853}
4854
4855/**
4856 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
4857 * @pf: board private structure
4858 **/
4859void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
4860{
4861 u32 fcnt_prog, fcnt_avail;
4862
4863 /* Check if, FD SB or ATR was auto disabled and if there is enough room
4864 * to re-enable
4865 */
4866 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4867 (pf->flags & I40E_FLAG_FD_SB_ENABLED))
4868 return;
4869 fcnt_prog = i40e_get_current_fd_count(pf);
Anjali Singhai Jain89132782014-04-09 05:59:01 +00004870 fcnt_avail = i40e_get_fd_cnt_all(pf);
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004871 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) {
4872 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
4873 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
4874 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
4875 dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
4876 }
4877 }
4878 /* Wait for some more space to be available to turn on ATR */
4879 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
4880 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4881 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
4882 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
4883 dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table now\n");
4884 }
4885 }
4886}
4887
4888/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004889 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
4890 * @pf: board private structure
4891 **/
4892static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
4893{
4894 if (!(pf->flags & I40E_FLAG_FDIR_REQUIRES_REINIT))
4895 return;
4896
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004897 /* if interface is down do nothing */
4898 if (test_bit(__I40E_DOWN, &pf->state))
4899 return;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004900 i40e_fdir_check_and_reenable(pf);
4901
4902 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4903 (pf->flags & I40E_FLAG_FD_SB_ENABLED))
4904 pf->flags &= ~I40E_FLAG_FDIR_REQUIRES_REINIT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004905}
4906
4907/**
4908 * i40e_vsi_link_event - notify VSI of a link event
4909 * @vsi: vsi to be notified
4910 * @link_up: link up or down
4911 **/
4912static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
4913{
4914 if (!vsi)
4915 return;
4916
4917 switch (vsi->type) {
4918 case I40E_VSI_MAIN:
4919 if (!vsi->netdev || !vsi->netdev_registered)
4920 break;
4921
4922 if (link_up) {
4923 netif_carrier_on(vsi->netdev);
4924 netif_tx_wake_all_queues(vsi->netdev);
4925 } else {
4926 netif_carrier_off(vsi->netdev);
4927 netif_tx_stop_all_queues(vsi->netdev);
4928 }
4929 break;
4930
4931 case I40E_VSI_SRIOV:
4932 break;
4933
4934 case I40E_VSI_VMDQ2:
4935 case I40E_VSI_CTRL:
4936 case I40E_VSI_MIRROR:
4937 default:
4938 /* there is no notification for other VSIs */
4939 break;
4940 }
4941}
4942
4943/**
4944 * i40e_veb_link_event - notify elements on the veb of a link event
4945 * @veb: veb to be notified
4946 * @link_up: link up or down
4947 **/
4948static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
4949{
4950 struct i40e_pf *pf;
4951 int i;
4952
4953 if (!veb || !veb->pf)
4954 return;
4955 pf = veb->pf;
4956
4957 /* depth first... */
4958 for (i = 0; i < I40E_MAX_VEB; i++)
4959 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
4960 i40e_veb_link_event(pf->veb[i], link_up);
4961
4962 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00004963 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004964 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
4965 i40e_vsi_link_event(pf->vsi[i], link_up);
4966}
4967
4968/**
4969 * i40e_link_event - Update netif_carrier status
4970 * @pf: board private structure
4971 **/
4972static void i40e_link_event(struct i40e_pf *pf)
4973{
4974 bool new_link, old_link;
4975
4976 new_link = (pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP);
4977 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
4978
4979 if (new_link == old_link)
4980 return;
Anjali Singhai6d779b42013-09-28 06:00:02 +00004981 if (!test_bit(__I40E_DOWN, &pf->vsi[pf->lan_vsi]->state))
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004982 i40e_print_link_message(pf->vsi[pf->lan_vsi], new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004983
4984 /* Notify the base of the switch tree connected to
4985 * the link. Floating VEBs are not notified.
4986 */
4987 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
4988 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
4989 else
4990 i40e_vsi_link_event(pf->vsi[pf->lan_vsi], new_link);
4991
4992 if (pf->vf)
4993 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00004994
4995 if (pf->flags & I40E_FLAG_PTP)
4996 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004997}
4998
4999/**
5000 * i40e_check_hang_subtask - Check for hung queues and dropped interrupts
5001 * @pf: board private structure
5002 *
5003 * Set the per-queue flags to request a check for stuck queues in the irq
5004 * clean functions, then force interrupts to be sure the irq clean is called.
5005 **/
5006static void i40e_check_hang_subtask(struct i40e_pf *pf)
5007{
5008 int i, v;
5009
5010 /* If we're down or resetting, just bail */
5011 if (test_bit(__I40E_CONFIG_BUSY, &pf->state))
5012 return;
5013
5014 /* for each VSI/netdev
5015 * for each Tx queue
5016 * set the check flag
5017 * for each q_vector
5018 * force an interrupt
5019 */
Mitch Williams505682c2014-05-20 08:01:37 +00005020 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005021 struct i40e_vsi *vsi = pf->vsi[v];
5022 int armed = 0;
5023
5024 if (!pf->vsi[v] ||
5025 test_bit(__I40E_DOWN, &vsi->state) ||
5026 (vsi->netdev && !netif_carrier_ok(vsi->netdev)))
5027 continue;
5028
5029 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005030 set_check_for_tx_hang(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005031 if (test_bit(__I40E_HANG_CHECK_ARMED,
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005032 &vsi->tx_rings[i]->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005033 armed++;
5034 }
5035
5036 if (armed) {
5037 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
5038 wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0,
5039 (I40E_PFINT_DYN_CTL0_INTENA_MASK |
5040 I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK));
5041 } else {
5042 u16 vec = vsi->base_vector - 1;
5043 u32 val = (I40E_PFINT_DYN_CTLN_INTENA_MASK |
5044 I40E_PFINT_DYN_CTLN_SWINT_TRIG_MASK);
5045 for (i = 0; i < vsi->num_q_vectors; i++, vec++)
5046 wr32(&vsi->back->hw,
5047 I40E_PFINT_DYN_CTLN(vec), val);
5048 }
5049 i40e_flush(&vsi->back->hw);
5050 }
5051 }
5052}
5053
5054/**
5055 * i40e_watchdog_subtask - Check and bring link up
5056 * @pf: board private structure
5057 **/
5058static void i40e_watchdog_subtask(struct i40e_pf *pf)
5059{
5060 int i;
5061
5062 /* if interface is down do nothing */
5063 if (test_bit(__I40E_DOWN, &pf->state) ||
5064 test_bit(__I40E_CONFIG_BUSY, &pf->state))
5065 return;
5066
5067 /* Update the stats for active netdevs so the network stack
5068 * can look at updated numbers whenever it cares to
5069 */
Mitch Williams505682c2014-05-20 08:01:37 +00005070 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005071 if (pf->vsi[i] && pf->vsi[i]->netdev)
5072 i40e_update_stats(pf->vsi[i]);
5073
5074 /* Update the stats for the active switching components */
5075 for (i = 0; i < I40E_MAX_VEB; i++)
5076 if (pf->veb[i])
5077 i40e_update_veb_stats(pf->veb[i]);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00005078
5079 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005080}
5081
5082/**
5083 * i40e_reset_subtask - Set up for resetting the device and driver
5084 * @pf: board private structure
5085 **/
5086static void i40e_reset_subtask(struct i40e_pf *pf)
5087{
5088 u32 reset_flags = 0;
5089
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005090 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005091 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
5092 reset_flags |= (1 << __I40E_REINIT_REQUESTED);
5093 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
5094 }
5095 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
5096 reset_flags |= (1 << __I40E_PF_RESET_REQUESTED);
5097 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5098 }
5099 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
5100 reset_flags |= (1 << __I40E_CORE_RESET_REQUESTED);
5101 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
5102 }
5103 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
5104 reset_flags |= (1 << __I40E_GLOBAL_RESET_REQUESTED);
5105 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
5106 }
5107
5108 /* If there's a recovery already waiting, it takes
5109 * precedence before starting a new reset sequence.
5110 */
5111 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
5112 i40e_handle_reset_warning(pf);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005113 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005114 }
5115
5116 /* If we're already down or resetting, just bail */
5117 if (reset_flags &&
5118 !test_bit(__I40E_DOWN, &pf->state) &&
5119 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
5120 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005121
5122unlock:
5123 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005124}
5125
5126/**
5127 * i40e_handle_link_event - Handle link event
5128 * @pf: board private structure
5129 * @e: event info posted on ARQ
5130 **/
5131static void i40e_handle_link_event(struct i40e_pf *pf,
5132 struct i40e_arq_event_info *e)
5133{
5134 struct i40e_hw *hw = &pf->hw;
5135 struct i40e_aqc_get_link_status *status =
5136 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
5137 struct i40e_link_status *hw_link_info = &hw->phy.link_info;
5138
5139 /* save off old link status information */
5140 memcpy(&pf->hw.phy.link_info_old, hw_link_info,
5141 sizeof(pf->hw.phy.link_info_old));
5142
5143 /* update link status */
5144 hw_link_info->phy_type = (enum i40e_aq_phy_type)status->phy_type;
5145 hw_link_info->link_speed = (enum i40e_aq_link_speed)status->link_speed;
5146 hw_link_info->link_info = status->link_info;
5147 hw_link_info->an_info = status->an_info;
5148 hw_link_info->ext_info = status->ext_info;
5149 hw_link_info->lse_enable =
5150 le16_to_cpu(status->command_flags) &
5151 I40E_AQ_LSE_ENABLE;
5152
5153 /* process the event */
5154 i40e_link_event(pf);
5155
5156 /* Do a new status request to re-enable LSE reporting
5157 * and load new status information into the hw struct,
5158 * then see if the status changed while processing the
5159 * initial event.
5160 */
5161 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
5162 i40e_link_event(pf);
5163}
5164
5165/**
5166 * i40e_clean_adminq_subtask - Clean the AdminQ rings
5167 * @pf: board private structure
5168 **/
5169static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
5170{
5171 struct i40e_arq_event_info event;
5172 struct i40e_hw *hw = &pf->hw;
5173 u16 pending, i = 0;
5174 i40e_status ret;
5175 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00005176 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005177 u32 val;
5178
5179 if (!test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state))
5180 return;
5181
Shannon Nelson86df2422014-05-20 08:01:35 +00005182 /* check for error indications */
5183 val = rd32(&pf->hw, pf->hw.aq.arq.len);
5184 oldval = val;
5185 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
5186 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
5187 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
5188 }
5189 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
5190 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
5191 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
5192 }
5193 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
5194 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
5195 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
5196 }
5197 if (oldval != val)
5198 wr32(&pf->hw, pf->hw.aq.arq.len, val);
5199
5200 val = rd32(&pf->hw, pf->hw.aq.asq.len);
5201 oldval = val;
5202 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
5203 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
5204 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
5205 }
5206 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
5207 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
5208 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
5209 }
5210 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
5211 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
5212 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
5213 }
5214 if (oldval != val)
5215 wr32(&pf->hw, pf->hw.aq.asq.len, val);
5216
Mitch Williams3197ce22013-11-28 06:39:39 +00005217 event.msg_size = I40E_MAX_AQ_BUF_SIZE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005218 event.msg_buf = kzalloc(event.msg_size, GFP_KERNEL);
5219 if (!event.msg_buf)
5220 return;
5221
5222 do {
Mitch Williams2f019122013-11-28 06:39:33 +00005223 event.msg_size = I40E_MAX_AQ_BUF_SIZE; /* reinit each time */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005224 ret = i40e_clean_arq_element(hw, &event, &pending);
5225 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
5226 dev_info(&pf->pdev->dev, "No ARQ event found\n");
5227 break;
5228 } else if (ret) {
5229 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
5230 break;
5231 }
5232
5233 opcode = le16_to_cpu(event.desc.opcode);
5234 switch (opcode) {
5235
5236 case i40e_aqc_opc_get_link_status:
5237 i40e_handle_link_event(pf, &event);
5238 break;
5239 case i40e_aqc_opc_send_msg_to_pf:
5240 ret = i40e_vc_process_vf_msg(pf,
5241 le16_to_cpu(event.desc.retval),
5242 le32_to_cpu(event.desc.cookie_high),
5243 le32_to_cpu(event.desc.cookie_low),
5244 event.msg_buf,
5245 event.msg_size);
5246 break;
5247 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005248 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005249#ifdef CONFIG_I40E_DCB
5250 rtnl_lock();
5251 ret = i40e_handle_lldp_event(pf, &event);
5252 rtnl_unlock();
5253#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005254 break;
5255 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005256 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005257 i40e_handle_lan_overflow_event(pf, &event);
5258 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00005259 case i40e_aqc_opc_send_msg_to_peer:
5260 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
5261 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005262 default:
5263 dev_info(&pf->pdev->dev,
Shannon Nelson0467bc92013-12-18 13:45:58 +00005264 "ARQ Error: Unknown event 0x%04x received\n",
5265 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005266 break;
5267 }
5268 } while (pending && (i++ < pf->adminq_work_limit));
5269
5270 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
5271 /* re-enable Admin queue interrupt cause */
5272 val = rd32(hw, I40E_PFINT_ICR0_ENA);
5273 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
5274 wr32(hw, I40E_PFINT_ICR0_ENA, val);
5275 i40e_flush(hw);
5276
5277 kfree(event.msg_buf);
5278}
5279
5280/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005281 * i40e_verify_eeprom - make sure eeprom is good to use
5282 * @pf: board private structure
5283 **/
5284static void i40e_verify_eeprom(struct i40e_pf *pf)
5285{
5286 int err;
5287
5288 err = i40e_diag_eeprom_test(&pf->hw);
5289 if (err) {
5290 /* retry in case of garbage read */
5291 err = i40e_diag_eeprom_test(&pf->hw);
5292 if (err) {
5293 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
5294 err);
5295 set_bit(__I40E_BAD_EEPROM, &pf->state);
5296 }
5297 }
5298
5299 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
5300 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
5301 clear_bit(__I40E_BAD_EEPROM, &pf->state);
5302 }
5303}
5304
5305/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005306 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
5307 * @veb: pointer to the VEB instance
5308 *
5309 * This is a recursive function that first builds the attached VSIs then
5310 * recurses in to build the next layer of VEB. We track the connections
5311 * through our own index numbers because the seid's from the HW could
5312 * change across the reset.
5313 **/
5314static int i40e_reconstitute_veb(struct i40e_veb *veb)
5315{
5316 struct i40e_vsi *ctl_vsi = NULL;
5317 struct i40e_pf *pf = veb->pf;
5318 int v, veb_idx;
5319 int ret;
5320
5321 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00005322 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005323 if (pf->vsi[v] &&
5324 pf->vsi[v]->veb_idx == veb->idx &&
5325 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
5326 ctl_vsi = pf->vsi[v];
5327 break;
5328 }
5329 }
5330 if (!ctl_vsi) {
5331 dev_info(&pf->pdev->dev,
5332 "missing owner VSI for veb_idx %d\n", veb->idx);
5333 ret = -ENOENT;
5334 goto end_reconstitute;
5335 }
5336 if (ctl_vsi != pf->vsi[pf->lan_vsi])
5337 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
5338 ret = i40e_add_vsi(ctl_vsi);
5339 if (ret) {
5340 dev_info(&pf->pdev->dev,
5341 "rebuild of owner VSI failed: %d\n", ret);
5342 goto end_reconstitute;
5343 }
5344 i40e_vsi_reset_stats(ctl_vsi);
5345
5346 /* create the VEB in the switch and move the VSI onto the VEB */
5347 ret = i40e_add_veb(veb, ctl_vsi);
5348 if (ret)
5349 goto end_reconstitute;
5350
5351 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00005352 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005353 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
5354 continue;
5355
5356 if (pf->vsi[v]->veb_idx == veb->idx) {
5357 struct i40e_vsi *vsi = pf->vsi[v];
5358 vsi->uplink_seid = veb->seid;
5359 ret = i40e_add_vsi(vsi);
5360 if (ret) {
5361 dev_info(&pf->pdev->dev,
5362 "rebuild of vsi_idx %d failed: %d\n",
5363 v, ret);
5364 goto end_reconstitute;
5365 }
5366 i40e_vsi_reset_stats(vsi);
5367 }
5368 }
5369
5370 /* create any VEBs attached to this VEB - RECURSION */
5371 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
5372 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
5373 pf->veb[veb_idx]->uplink_seid = veb->seid;
5374 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
5375 if (ret)
5376 break;
5377 }
5378 }
5379
5380end_reconstitute:
5381 return ret;
5382}
5383
5384/**
5385 * i40e_get_capabilities - get info about the HW
5386 * @pf: the PF struct
5387 **/
5388static int i40e_get_capabilities(struct i40e_pf *pf)
5389{
5390 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
5391 u16 data_size;
5392 int buf_len;
5393 int err;
5394
5395 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
5396 do {
5397 cap_buf = kzalloc(buf_len, GFP_KERNEL);
5398 if (!cap_buf)
5399 return -ENOMEM;
5400
5401 /* this loads the data into the hw struct for us */
5402 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
5403 &data_size,
5404 i40e_aqc_opc_list_func_capabilities,
5405 NULL);
5406 /* data loaded, buffer no longer needed */
5407 kfree(cap_buf);
5408
5409 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
5410 /* retry with a larger buffer */
5411 buf_len = data_size;
5412 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
5413 dev_info(&pf->pdev->dev,
5414 "capability discovery failed: aq=%d\n",
5415 pf->hw.aq.asq_last_status);
5416 return -ENODEV;
5417 }
5418 } while (err);
5419
Anjali Singhai Jainac71b7b2014-02-06 05:51:08 +00005420 if (((pf->hw.aq.fw_maj_ver == 2) && (pf->hw.aq.fw_min_ver < 22)) ||
5421 (pf->hw.aq.fw_maj_ver < 2)) {
5422 pf->hw.func_caps.num_msix_vectors++;
5423 pf->hw.func_caps.num_msix_vectors_vf++;
5424 }
5425
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005426 if (pf->hw.debug_mask & I40E_DEBUG_USER)
5427 dev_info(&pf->pdev->dev,
5428 "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",
5429 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
5430 pf->hw.func_caps.num_msix_vectors,
5431 pf->hw.func_caps.num_msix_vectors_vf,
5432 pf->hw.func_caps.fd_filters_guaranteed,
5433 pf->hw.func_caps.fd_filters_best_effort,
5434 pf->hw.func_caps.num_tx_qp,
5435 pf->hw.func_caps.num_vsis);
5436
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00005437#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
5438 + pf->hw.func_caps.num_vfs)
5439 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
5440 dev_info(&pf->pdev->dev,
5441 "got num_vsis %d, setting num_vsis to %d\n",
5442 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
5443 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
5444 }
5445
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005446 return 0;
5447}
5448
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005449static int i40e_vsi_clear(struct i40e_vsi *vsi);
5450
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005451/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005452 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005453 * @pf: board private structure
5454 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005455static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005456{
5457 struct i40e_vsi *vsi;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005458 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005459
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005460 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005461 return;
5462
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005463 /* find existing VSI and see if it needs configuring */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005464 vsi = NULL;
Mitch Williams505682c2014-05-20 08:01:37 +00005465 for (i = 0; i < pf->num_alloc_vsi; i++) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005466 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005467 vsi = pf->vsi[i];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005468 break;
5469 }
5470 }
5471
5472 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005473 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005474 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
5475 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005476 if (!vsi) {
5477 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005478 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
5479 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005480 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005481 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005482
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005483 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005484}
5485
5486/**
5487 * i40e_fdir_teardown - release the Flow Director resources
5488 * @pf: board private structure
5489 **/
5490static void i40e_fdir_teardown(struct i40e_pf *pf)
5491{
5492 int i;
5493
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005494 i40e_fdir_filter_exit(pf);
Mitch Williams505682c2014-05-20 08:01:37 +00005495 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005496 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
5497 i40e_vsi_release(pf->vsi[i]);
5498 break;
5499 }
5500 }
5501}
5502
5503/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005504 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005505 * @pf: board private structure
5506 *
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005507 * Close up the VFs and other things in prep for pf Reset.
5508 **/
5509static int i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005510{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005511 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00005512 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005513 u32 v;
5514
5515 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
5516 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005517 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005518
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005519 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005520
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005521 /* quiesce the VSIs and their queues that are not already DOWN */
5522 i40e_pf_quiesce_all_vsi(pf);
5523
Mitch Williams505682c2014-05-20 08:01:37 +00005524 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005525 if (pf->vsi[v])
5526 pf->vsi[v]->seid = 0;
5527 }
5528
5529 i40e_shutdown_adminq(&pf->hw);
5530
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005531 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00005532 if (hw->hmc.hmc_obj) {
5533 ret = i40e_shutdown_lan_hmc(hw);
5534 if (ret) {
5535 dev_warn(&pf->pdev->dev,
5536 "shutdown_lan_hmc failed: %d\n", ret);
5537 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
5538 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005539 }
5540 return ret;
5541}
5542
5543/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00005544 * i40e_send_version - update firmware with driver version
5545 * @pf: PF struct
5546 */
5547static void i40e_send_version(struct i40e_pf *pf)
5548{
5549 struct i40e_driver_version dv;
5550
5551 dv.major_version = DRV_VERSION_MAJOR;
5552 dv.minor_version = DRV_VERSION_MINOR;
5553 dv.build_version = DRV_VERSION_BUILD;
5554 dv.subbuild_version = 0;
5555 strncpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
5556 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
5557}
5558
5559/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00005560 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005561 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005562 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005563 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005564static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005565{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005566 struct i40e_hw *hw = &pf->hw;
5567 i40e_status ret;
5568 u32 v;
5569
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005570 /* Now we wait for GRST to settle out.
5571 * We don't have to delete the VEBs or VSIs from the hw switch
5572 * because the reset will make them disappear.
5573 */
5574 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00005575 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005576 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00005577 goto end_core_reset;
5578 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005579 pf->pfr_count++;
5580
5581 if (test_bit(__I40E_DOWN, &pf->state))
5582 goto end_core_reset;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005583 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005584
5585 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
5586 ret = i40e_init_adminq(&pf->hw);
5587 if (ret) {
5588 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, %d\n", ret);
5589 goto end_core_reset;
5590 }
5591
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005592 /* re-verify the eeprom if we just had an EMP reset */
5593 if (test_bit(__I40E_EMP_RESET_REQUESTED, &pf->state)) {
5594 clear_bit(__I40E_EMP_RESET_REQUESTED, &pf->state);
5595 i40e_verify_eeprom(pf);
5596 }
5597
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00005598 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005599 ret = i40e_get_capabilities(pf);
5600 if (ret) {
5601 dev_info(&pf->pdev->dev, "i40e_get_capabilities failed, %d\n",
5602 ret);
5603 goto end_core_reset;
5604 }
5605
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005606 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
5607 hw->func_caps.num_rx_qp,
5608 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
5609 if (ret) {
5610 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
5611 goto end_core_reset;
5612 }
5613 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
5614 if (ret) {
5615 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
5616 goto end_core_reset;
5617 }
5618
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005619#ifdef CONFIG_I40E_DCB
5620 ret = i40e_init_pf_dcb(pf);
5621 if (ret) {
5622 dev_info(&pf->pdev->dev, "init_pf_dcb failed: %d\n", ret);
5623 goto end_core_reset;
5624 }
5625#endif /* CONFIG_I40E_DCB */
5626
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005627 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005628 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005629 if (ret)
5630 goto end_core_reset;
5631
5632 /* Rebuild the VSIs and VEBs that existed before reset.
5633 * They are still in our local switch element arrays, so only
5634 * need to rebuild the switch model in the HW.
5635 *
5636 * If there were VEBs but the reconstitution failed, we'll try
5637 * try to recover minimal use by getting the basic PF VSI working.
5638 */
5639 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005640 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005641 /* find the one VEB connected to the MAC, and find orphans */
5642 for (v = 0; v < I40E_MAX_VEB; v++) {
5643 if (!pf->veb[v])
5644 continue;
5645
5646 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
5647 pf->veb[v]->uplink_seid == 0) {
5648 ret = i40e_reconstitute_veb(pf->veb[v]);
5649
5650 if (!ret)
5651 continue;
5652
5653 /* If Main VEB failed, we're in deep doodoo,
5654 * so give up rebuilding the switch and set up
5655 * for minimal rebuild of PF VSI.
5656 * If orphan failed, we'll report the error
5657 * but try to keep going.
5658 */
5659 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
5660 dev_info(&pf->pdev->dev,
5661 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
5662 ret);
5663 pf->vsi[pf->lan_vsi]->uplink_seid
5664 = pf->mac_seid;
5665 break;
5666 } else if (pf->veb[v]->uplink_seid == 0) {
5667 dev_info(&pf->pdev->dev,
5668 "rebuild of orphan VEB failed: %d\n",
5669 ret);
5670 }
5671 }
5672 }
5673 }
5674
5675 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
5676 dev_info(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
5677 /* no VEB, so rebuild only the Main VSI */
5678 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
5679 if (ret) {
5680 dev_info(&pf->pdev->dev,
5681 "rebuild of Main VSI failed: %d\n", ret);
5682 goto end_core_reset;
5683 }
5684 }
5685
5686 /* reinit the misc interrupt */
5687 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5688 ret = i40e_setup_misc_vector(pf);
5689
5690 /* restart the VSIs that were rebuilt and running before the reset */
5691 i40e_pf_unquiesce_all_vsi(pf);
5692
Mitch Williams69f64b22014-02-13 03:48:46 -08005693 if (pf->num_alloc_vfs) {
5694 for (v = 0; v < pf->num_alloc_vfs; v++)
5695 i40e_reset_vf(&pf->vf[v], true);
5696 }
5697
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005698 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00005699 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005700
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005701end_core_reset:
5702 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
5703}
5704
5705/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005706 * i40e_handle_reset_warning - prep for the pf to reset, reset and rebuild
5707 * @pf: board private structure
5708 *
5709 * Close up the VFs and other things in prep for a Core Reset,
5710 * then get ready to rebuild the world.
5711 **/
5712static void i40e_handle_reset_warning(struct i40e_pf *pf)
5713{
5714 i40e_status ret;
5715
5716 ret = i40e_prep_for_reset(pf);
5717 if (!ret)
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005718 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005719}
5720
5721/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005722 * i40e_handle_mdd_event
5723 * @pf: pointer to the pf structure
5724 *
5725 * Called from the MDD irq handler to identify possibly malicious vfs
5726 **/
5727static void i40e_handle_mdd_event(struct i40e_pf *pf)
5728{
5729 struct i40e_hw *hw = &pf->hw;
5730 bool mdd_detected = false;
5731 struct i40e_vf *vf;
5732 u32 reg;
5733 int i;
5734
5735 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
5736 return;
5737
5738 /* find what triggered the MDD event */
5739 reg = rd32(hw, I40E_GL_MDET_TX);
5740 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
5741 u8 func = (reg & I40E_GL_MDET_TX_FUNCTION_MASK)
5742 >> I40E_GL_MDET_TX_FUNCTION_SHIFT;
5743 u8 event = (reg & I40E_GL_MDET_TX_EVENT_SHIFT)
5744 >> I40E_GL_MDET_TX_EVENT_SHIFT;
5745 u8 queue = (reg & I40E_GL_MDET_TX_QUEUE_MASK)
5746 >> I40E_GL_MDET_TX_QUEUE_SHIFT;
5747 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00005748 "Malicious Driver Detection event 0x%02x on TX queue %d of function 0x%02x\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005749 event, queue, func);
5750 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
5751 mdd_detected = true;
5752 }
5753 reg = rd32(hw, I40E_GL_MDET_RX);
5754 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
5755 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK)
5756 >> I40E_GL_MDET_RX_FUNCTION_SHIFT;
5757 u8 event = (reg & I40E_GL_MDET_RX_EVENT_SHIFT)
5758 >> I40E_GL_MDET_RX_EVENT_SHIFT;
5759 u8 queue = (reg & I40E_GL_MDET_RX_QUEUE_MASK)
5760 >> I40E_GL_MDET_RX_QUEUE_SHIFT;
5761 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00005762 "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005763 event, queue, func);
5764 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
5765 mdd_detected = true;
5766 }
5767
5768 /* see if one of the VFs needs its hand slapped */
5769 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
5770 vf = &(pf->vf[i]);
5771 reg = rd32(hw, I40E_VP_MDET_TX(i));
5772 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
5773 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
5774 vf->num_mdd_events++;
5775 dev_info(&pf->pdev->dev, "MDD TX event on VF %d\n", i);
5776 }
5777
5778 reg = rd32(hw, I40E_VP_MDET_RX(i));
5779 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
5780 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
5781 vf->num_mdd_events++;
5782 dev_info(&pf->pdev->dev, "MDD RX event on VF %d\n", i);
5783 }
5784
5785 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
5786 dev_info(&pf->pdev->dev,
5787 "Too many MDD events on VF %d, disabled\n", i);
5788 dev_info(&pf->pdev->dev,
5789 "Use PF Control I/F to re-enable the VF\n");
5790 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
5791 }
5792 }
5793
5794 /* re-enable mdd interrupt cause */
5795 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
5796 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
5797 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
5798 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
5799 i40e_flush(hw);
5800}
5801
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005802#ifdef CONFIG_I40E_VXLAN
5803/**
5804 * i40e_sync_vxlan_filters_subtask - Sync the VSI filter list with HW
5805 * @pf: board private structure
5806 **/
5807static void i40e_sync_vxlan_filters_subtask(struct i40e_pf *pf)
5808{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005809 struct i40e_hw *hw = &pf->hw;
5810 i40e_status ret;
5811 u8 filter_index;
5812 __be16 port;
5813 int i;
5814
5815 if (!(pf->flags & I40E_FLAG_VXLAN_FILTER_SYNC))
5816 return;
5817
5818 pf->flags &= ~I40E_FLAG_VXLAN_FILTER_SYNC;
5819
5820 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
5821 if (pf->pending_vxlan_bitmap & (1 << i)) {
5822 pf->pending_vxlan_bitmap &= ~(1 << i);
5823 port = pf->vxlan_ports[i];
5824 ret = port ?
5825 i40e_aq_add_udp_tunnel(hw, ntohs(port),
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005826 I40E_AQC_TUNNEL_TYPE_VXLAN,
5827 &filter_index, NULL)
5828 : i40e_aq_del_udp_tunnel(hw, i, NULL);
5829
5830 if (ret) {
5831 dev_info(&pf->pdev->dev, "Failed to execute AQ command for %s port %d with index %d\n",
5832 port ? "adding" : "deleting",
5833 ntohs(port), port ? i : i);
5834
5835 pf->vxlan_ports[i] = 0;
5836 } else {
5837 dev_info(&pf->pdev->dev, "%s port %d with AQ command with index %d\n",
5838 port ? "Added" : "Deleted",
5839 ntohs(port), port ? i : filter_index);
5840 }
5841 }
5842 }
5843}
5844
5845#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005846/**
5847 * i40e_service_task - Run the driver's async subtasks
5848 * @work: pointer to work_struct containing our data
5849 **/
5850static void i40e_service_task(struct work_struct *work)
5851{
5852 struct i40e_pf *pf = container_of(work,
5853 struct i40e_pf,
5854 service_task);
5855 unsigned long start_time = jiffies;
5856
5857 i40e_reset_subtask(pf);
5858 i40e_handle_mdd_event(pf);
5859 i40e_vc_process_vflr_event(pf);
5860 i40e_watchdog_subtask(pf);
5861 i40e_fdir_reinit_subtask(pf);
5862 i40e_check_hang_subtask(pf);
5863 i40e_sync_filters_subtask(pf);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005864#ifdef CONFIG_I40E_VXLAN
5865 i40e_sync_vxlan_filters_subtask(pf);
5866#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005867 i40e_clean_adminq_subtask(pf);
5868
5869 i40e_service_event_complete(pf);
5870
5871 /* If the tasks have taken longer than one timer cycle or there
5872 * is more work to be done, reschedule the service task now
5873 * rather than wait for the timer to tick again.
5874 */
5875 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
5876 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
5877 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
5878 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
5879 i40e_service_event_schedule(pf);
5880}
5881
5882/**
5883 * i40e_service_timer - timer callback
5884 * @data: pointer to PF struct
5885 **/
5886static void i40e_service_timer(unsigned long data)
5887{
5888 struct i40e_pf *pf = (struct i40e_pf *)data;
5889
5890 mod_timer(&pf->service_timer,
5891 round_jiffies(jiffies + pf->service_timer_period));
5892 i40e_service_event_schedule(pf);
5893}
5894
5895/**
5896 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
5897 * @vsi: the VSI being configured
5898 **/
5899static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
5900{
5901 struct i40e_pf *pf = vsi->back;
5902
5903 switch (vsi->type) {
5904 case I40E_VSI_MAIN:
5905 vsi->alloc_queue_pairs = pf->num_lan_qps;
5906 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5907 I40E_REQ_DESCRIPTOR_MULTIPLE);
5908 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5909 vsi->num_q_vectors = pf->num_lan_msix;
5910 else
5911 vsi->num_q_vectors = 1;
5912
5913 break;
5914
5915 case I40E_VSI_FDIR:
5916 vsi->alloc_queue_pairs = 1;
5917 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
5918 I40E_REQ_DESCRIPTOR_MULTIPLE);
5919 vsi->num_q_vectors = 1;
5920 break;
5921
5922 case I40E_VSI_VMDQ2:
5923 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
5924 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5925 I40E_REQ_DESCRIPTOR_MULTIPLE);
5926 vsi->num_q_vectors = pf->num_vmdq_msix;
5927 break;
5928
5929 case I40E_VSI_SRIOV:
5930 vsi->alloc_queue_pairs = pf->num_vf_qps;
5931 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5932 I40E_REQ_DESCRIPTOR_MULTIPLE);
5933 break;
5934
5935 default:
5936 WARN_ON(1);
5937 return -ENODATA;
5938 }
5939
5940 return 0;
5941}
5942
5943/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005944 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
5945 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005946 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005947 *
5948 * On error: returns error code (negative)
5949 * On success: returns 0
5950 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005951static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005952{
5953 int size;
5954 int ret = 0;
5955
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00005956 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005957 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
5958 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
5959 if (!vsi->tx_rings)
5960 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005961 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
5962
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005963 if (alloc_qvectors) {
5964 /* allocate memory for q_vector pointers */
5965 size = sizeof(struct i40e_q_vectors *) * vsi->num_q_vectors;
5966 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
5967 if (!vsi->q_vectors) {
5968 ret = -ENOMEM;
5969 goto err_vectors;
5970 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005971 }
5972 return ret;
5973
5974err_vectors:
5975 kfree(vsi->tx_rings);
5976 return ret;
5977}
5978
5979/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005980 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
5981 * @pf: board private structure
5982 * @type: type of VSI
5983 *
5984 * On error: returns error code (negative)
5985 * On success: returns vsi index in PF (positive)
5986 **/
5987static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
5988{
5989 int ret = -ENODEV;
5990 struct i40e_vsi *vsi;
5991 int vsi_idx;
5992 int i;
5993
5994 /* Need to protect the allocation of the VSIs at the PF level */
5995 mutex_lock(&pf->switch_mutex);
5996
5997 /* VSI list may be fragmented if VSI creation/destruction has
5998 * been happening. We can afford to do a quick scan to look
5999 * for any free VSIs in the list.
6000 *
6001 * find next empty vsi slot, looping back around if necessary
6002 */
6003 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00006004 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006005 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00006006 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006007 i = 0;
6008 while (i < pf->next_vsi && pf->vsi[i])
6009 i++;
6010 }
6011
Mitch Williams505682c2014-05-20 08:01:37 +00006012 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006013 vsi_idx = i; /* Found one! */
6014 } else {
6015 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00006016 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006017 }
6018 pf->next_vsi = ++i;
6019
6020 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
6021 if (!vsi) {
6022 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00006023 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006024 }
6025 vsi->type = type;
6026 vsi->back = pf;
6027 set_bit(__I40E_DOWN, &vsi->state);
6028 vsi->flags = 0;
6029 vsi->idx = vsi_idx;
6030 vsi->rx_itr_setting = pf->rx_itr_default;
6031 vsi->tx_itr_setting = pf->tx_itr_default;
6032 vsi->netdev_registered = false;
6033 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
6034 INIT_LIST_HEAD(&vsi->mac_filter_list);
Shannon Nelson63741842014-04-23 04:50:16 +00006035 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006036
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006037 ret = i40e_set_num_rings_in_vsi(vsi);
6038 if (ret)
6039 goto err_rings;
6040
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006041 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006042 if (ret)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006043 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00006044
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006045 /* Setup default MSIX irq handler for VSI */
6046 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
6047
6048 pf->vsi[vsi_idx] = vsi;
6049 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00006050 goto unlock_pf;
6051
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006052err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00006053 pf->next_vsi = i - 1;
6054 kfree(vsi);
6055unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006056 mutex_unlock(&pf->switch_mutex);
6057 return ret;
6058}
6059
6060/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006061 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
6062 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006063 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006064 *
6065 * On error: returns error code (negative)
6066 * On success: returns 0
6067 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006068static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006069{
6070 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006071 if (free_qvectors) {
6072 kfree(vsi->q_vectors);
6073 vsi->q_vectors = NULL;
6074 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006075 kfree(vsi->tx_rings);
6076 vsi->tx_rings = NULL;
6077 vsi->rx_rings = NULL;
6078}
6079
6080/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006081 * i40e_vsi_clear - Deallocate the VSI provided
6082 * @vsi: the VSI being un-configured
6083 **/
6084static int i40e_vsi_clear(struct i40e_vsi *vsi)
6085{
6086 struct i40e_pf *pf;
6087
6088 if (!vsi)
6089 return 0;
6090
6091 if (!vsi->back)
6092 goto free_vsi;
6093 pf = vsi->back;
6094
6095 mutex_lock(&pf->switch_mutex);
6096 if (!pf->vsi[vsi->idx]) {
6097 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
6098 vsi->idx, vsi->idx, vsi, vsi->type);
6099 goto unlock_vsi;
6100 }
6101
6102 if (pf->vsi[vsi->idx] != vsi) {
6103 dev_err(&pf->pdev->dev,
6104 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
6105 pf->vsi[vsi->idx]->idx,
6106 pf->vsi[vsi->idx],
6107 pf->vsi[vsi->idx]->type,
6108 vsi->idx, vsi, vsi->type);
6109 goto unlock_vsi;
6110 }
6111
6112 /* updates the pf for this cleared vsi */
6113 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
6114 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
6115
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006116 i40e_vsi_free_arrays(vsi, true);
Alexander Duyck493fb302013-09-28 07:01:44 +00006117
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006118 pf->vsi[vsi->idx] = NULL;
6119 if (vsi->idx < pf->next_vsi)
6120 pf->next_vsi = vsi->idx;
6121
6122unlock_vsi:
6123 mutex_unlock(&pf->switch_mutex);
6124free_vsi:
6125 kfree(vsi);
6126
6127 return 0;
6128}
6129
6130/**
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006131 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
6132 * @vsi: the VSI being cleaned
6133 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00006134static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006135{
6136 int i;
6137
Greg Rose8e9dca52013-12-18 13:45:53 +00006138 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00006139 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00006140 kfree_rcu(vsi->tx_rings[i], rcu);
6141 vsi->tx_rings[i] = NULL;
6142 vsi->rx_rings[i] = NULL;
6143 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00006144 }
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006145}
6146
6147/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006148 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
6149 * @vsi: the VSI being configured
6150 **/
6151static int i40e_alloc_rings(struct i40e_vsi *vsi)
6152{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00006153 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006154 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006155 int i;
6156
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006157 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00006158 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00006159 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006160 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
6161 if (!tx_ring)
6162 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006163
6164 tx_ring->queue_index = i;
6165 tx_ring->reg_idx = vsi->base_queue + i;
6166 tx_ring->ring_active = false;
6167 tx_ring->vsi = vsi;
6168 tx_ring->netdev = vsi->netdev;
6169 tx_ring->dev = &pf->pdev->dev;
6170 tx_ring->count = vsi->num_desc;
6171 tx_ring->size = 0;
6172 tx_ring->dcb_tc = 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006173 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006174
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006175 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006176 rx_ring->queue_index = i;
6177 rx_ring->reg_idx = vsi->base_queue + i;
6178 rx_ring->ring_active = false;
6179 rx_ring->vsi = vsi;
6180 rx_ring->netdev = vsi->netdev;
6181 rx_ring->dev = &pf->pdev->dev;
6182 rx_ring->count = vsi->num_desc;
6183 rx_ring->size = 0;
6184 rx_ring->dcb_tc = 0;
6185 if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
6186 set_ring_16byte_desc_enabled(rx_ring);
6187 else
6188 clear_ring_16byte_desc_enabled(rx_ring);
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006189 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006190 }
6191
6192 return 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006193
6194err_out:
6195 i40e_vsi_clear_rings(vsi);
6196 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006197}
6198
6199/**
6200 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
6201 * @pf: board private structure
6202 * @vectors: the number of MSI-X vectors to request
6203 *
6204 * Returns the number of vectors reserved, or error
6205 **/
6206static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
6207{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01006208 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
6209 I40E_MIN_MSIX, vectors);
6210 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006211 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01006212 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006213 vectors = 0;
6214 }
6215
6216 return vectors;
6217}
6218
6219/**
6220 * i40e_init_msix - Setup the MSIX capability
6221 * @pf: board private structure
6222 *
6223 * Work with the OS to set up the MSIX vectors needed.
6224 *
6225 * Returns 0 on success, negative on failure
6226 **/
6227static int i40e_init_msix(struct i40e_pf *pf)
6228{
6229 i40e_status err = 0;
6230 struct i40e_hw *hw = &pf->hw;
6231 int v_budget, i;
6232 int vec;
6233
6234 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
6235 return -ENODEV;
6236
6237 /* The number of vectors we'll request will be comprised of:
6238 * - Add 1 for "other" cause for Admin Queue events, etc.
6239 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006240 * - Queues being used for RSS.
6241 * We don't need as many as max_rss_size vectors.
6242 * use rss_size instead in the calculation since that
6243 * is governed by number of cpus in the system.
6244 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006245 * - The number of VMDq pairs
6246 * Once we count this up, try the request.
6247 *
6248 * If we can't get what we want, we'll simplify to nearly nothing
6249 * and try again. If that still fails, we punt.
6250 */
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006251 pf->num_lan_msix = pf->num_lan_qps - (pf->rss_size_max - pf->rss_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006252 pf->num_vmdq_msix = pf->num_vmdq_qps;
6253 v_budget = 1 + pf->num_lan_msix;
6254 v_budget += (pf->num_vmdq_vsis * pf->num_vmdq_msix);
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006255 if (pf->flags & I40E_FLAG_FD_SB_ENABLED)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006256 v_budget++;
6257
6258 /* Scale down if necessary, and the rings will share vectors */
6259 v_budget = min_t(int, v_budget, hw->func_caps.num_msix_vectors);
6260
6261 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
6262 GFP_KERNEL);
6263 if (!pf->msix_entries)
6264 return -ENOMEM;
6265
6266 for (i = 0; i < v_budget; i++)
6267 pf->msix_entries[i].entry = i;
6268 vec = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00006269
6270 if (vec != v_budget) {
6271 /* If we have limited resources, we will start with no vectors
6272 * for the special features and then allocate vectors to some
6273 * of these features based on the policy and at the end disable
6274 * the features that did not get any vectors.
6275 */
6276 pf->num_vmdq_msix = 0;
6277 }
6278
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006279 if (vec < I40E_MIN_MSIX) {
6280 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
6281 kfree(pf->msix_entries);
6282 pf->msix_entries = NULL;
6283 return -ENODEV;
6284
6285 } else if (vec == I40E_MIN_MSIX) {
6286 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006287 pf->num_vmdq_vsis = 0;
6288 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006289 pf->num_lan_qps = 1;
6290 pf->num_lan_msix = 1;
6291
6292 } else if (vec != v_budget) {
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00006293 /* reserve the misc vector */
6294 vec--;
6295
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006296 /* Scale vector usage down */
6297 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00006298 pf->num_vmdq_vsis = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006299
6300 /* partition out the remaining vectors */
6301 switch (vec) {
6302 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006303 pf->num_lan_msix = 1;
6304 break;
6305 case 3:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006306 pf->num_lan_msix = 2;
6307 break;
6308 default:
6309 pf->num_lan_msix = min_t(int, (vec / 2),
6310 pf->num_lan_qps);
6311 pf->num_vmdq_vsis = min_t(int, (vec - pf->num_lan_msix),
6312 I40E_DEFAULT_NUM_VMDQ_VSI);
6313 break;
6314 }
6315 }
6316
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00006317 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
6318 (pf->num_vmdq_msix == 0)) {
6319 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
6320 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
6321 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006322 return err;
6323}
6324
6325/**
Greg Rose90e04072014-03-06 08:59:57 +00006326 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00006327 * @vsi: the VSI being configured
6328 * @v_idx: index of the vector in the vsi struct
6329 *
6330 * We allocate one q_vector. If allocation fails we return -ENOMEM.
6331 **/
Greg Rose90e04072014-03-06 08:59:57 +00006332static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
Alexander Duyck493fb302013-09-28 07:01:44 +00006333{
6334 struct i40e_q_vector *q_vector;
6335
6336 /* allocate q_vector */
6337 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
6338 if (!q_vector)
6339 return -ENOMEM;
6340
6341 q_vector->vsi = vsi;
6342 q_vector->v_idx = v_idx;
6343 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
6344 if (vsi->netdev)
6345 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00006346 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00006347
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00006348 q_vector->rx.latency_range = I40E_LOW_LATENCY;
6349 q_vector->tx.latency_range = I40E_LOW_LATENCY;
6350
Alexander Duyck493fb302013-09-28 07:01:44 +00006351 /* tie q_vector and vsi together */
6352 vsi->q_vectors[v_idx] = q_vector;
6353
6354 return 0;
6355}
6356
6357/**
Greg Rose90e04072014-03-06 08:59:57 +00006358 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006359 * @vsi: the VSI being configured
6360 *
6361 * We allocate one q_vector per queue interrupt. If allocation fails we
6362 * return -ENOMEM.
6363 **/
Greg Rose90e04072014-03-06 08:59:57 +00006364static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006365{
6366 struct i40e_pf *pf = vsi->back;
6367 int v_idx, num_q_vectors;
Alexander Duyck493fb302013-09-28 07:01:44 +00006368 int err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006369
6370 /* if not MSIX, give the one vector only to the LAN VSI */
6371 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6372 num_q_vectors = vsi->num_q_vectors;
6373 else if (vsi == pf->vsi[pf->lan_vsi])
6374 num_q_vectors = 1;
6375 else
6376 return -EINVAL;
6377
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006378 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Greg Rose90e04072014-03-06 08:59:57 +00006379 err = i40e_vsi_alloc_q_vector(vsi, v_idx);
Alexander Duyck493fb302013-09-28 07:01:44 +00006380 if (err)
6381 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006382 }
6383
6384 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00006385
6386err_out:
6387 while (v_idx--)
6388 i40e_free_q_vector(vsi, v_idx);
6389
6390 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006391}
6392
6393/**
6394 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
6395 * @pf: board private structure to initialize
6396 **/
6397static void i40e_init_interrupt_scheme(struct i40e_pf *pf)
6398{
6399 int err = 0;
6400
6401 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
6402 err = i40e_init_msix(pf);
6403 if (err) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006404 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
6405 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006406 I40E_FLAG_DCB_CAPABLE |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006407 I40E_FLAG_SRIOV_ENABLED |
6408 I40E_FLAG_FD_SB_ENABLED |
6409 I40E_FLAG_FD_ATR_ENABLED |
6410 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006411
6412 /* rework the queue expectations without MSIX */
6413 i40e_determine_queue_usage(pf);
6414 }
6415 }
6416
6417 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
6418 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006419 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006420 err = pci_enable_msi(pf->pdev);
6421 if (err) {
Shannon Nelson958a3e32013-09-28 07:13:28 +00006422 dev_info(&pf->pdev->dev, "MSI init failed - %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006423 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
6424 }
6425 }
6426
Shannon Nelson958a3e32013-09-28 07:13:28 +00006427 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006428 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00006429
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006430 /* track first vector for misc interrupts */
6431 err = i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT-1);
6432}
6433
6434/**
6435 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
6436 * @pf: board private structure
6437 *
6438 * This sets up the handler for MSIX 0, which is used to manage the
6439 * non-queue interrupts, e.g. AdminQ and errors. This is not used
6440 * when in MSI or Legacy interrupt mode.
6441 **/
6442static int i40e_setup_misc_vector(struct i40e_pf *pf)
6443{
6444 struct i40e_hw *hw = &pf->hw;
6445 int err = 0;
6446
6447 /* Only request the irq if this is the first time through, and
6448 * not when we're rebuilding after a Reset
6449 */
6450 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
6451 err = request_irq(pf->msix_entries[0].vector,
6452 i40e_intr, 0, pf->misc_int_name, pf);
6453 if (err) {
6454 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006455 "request_irq for %s failed: %d\n",
6456 pf->misc_int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006457 return -EFAULT;
6458 }
6459 }
6460
6461 i40e_enable_misc_int_causes(hw);
6462
6463 /* associate no queues to the misc vector */
6464 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
6465 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
6466
6467 i40e_flush(hw);
6468
6469 i40e_irq_dynamic_enable_icr0(pf);
6470
6471 return err;
6472}
6473
6474/**
6475 * i40e_config_rss - Prepare for RSS if used
6476 * @pf: board private structure
6477 **/
6478static int i40e_config_rss(struct i40e_pf *pf)
6479{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006480 /* Set of random keys generated using kernel random number generator */
6481 static const u32 seed[I40E_PFQF_HKEY_MAX_INDEX + 1] = {0x41b01687,
6482 0x183cfd8c, 0xce880440, 0x580cbc3c, 0x35897377,
6483 0x328b25e1, 0x4fa98922, 0xb7d90c14, 0xd5bad70d,
6484 0xcd15a2c1, 0xe8580225, 0x4a1e9d11, 0xfe5731be};
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00006485 struct i40e_hw *hw = &pf->hw;
6486 u32 lut = 0;
6487 int i, j;
6488 u64 hena;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006489
6490 /* Fill out hash function seed */
6491 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
6492 wr32(hw, I40E_PFQF_HKEY(i), seed[i]);
6493
6494 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
6495 hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
6496 ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
Mitch Williams12dc4fe2013-11-28 06:39:32 +00006497 hena |= I40E_DEFAULT_RSS_HENA;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006498 wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
6499 wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
6500
6501 /* Populate the LUT with max no. of queues in round robin fashion */
6502 for (i = 0, j = 0; i < pf->hw.func_caps.rss_table_size; i++, j++) {
6503
6504 /* The assumption is that lan qp count will be the highest
6505 * qp count for any PF VSI that needs RSS.
6506 * If multiple VSIs need RSS support, all the qp counts
6507 * for those VSIs should be a power of 2 for RSS to work.
6508 * If LAN VSI is the only consumer for RSS then this requirement
6509 * is not necessary.
6510 */
6511 if (j == pf->rss_size)
6512 j = 0;
6513 /* lut = 4-byte sliding window of 4 lut entries */
6514 lut = (lut << 8) | (j &
6515 ((0x1 << pf->hw.func_caps.rss_table_entry_width) - 1));
6516 /* On i = 3, we have 4 entries in lut; write to the register */
6517 if ((i & 3) == 3)
6518 wr32(hw, I40E_PFQF_HLUT(i >> 2), lut);
6519 }
6520 i40e_flush(hw);
6521
6522 return 0;
6523}
6524
6525/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006526 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
6527 * @pf: board private structure
6528 * @queue_count: the requested queue count for rss.
6529 *
6530 * returns 0 if rss is not enabled, if enabled returns the final rss queue
6531 * count which may be different from the requested queue count.
6532 **/
6533int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
6534{
6535 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
6536 return 0;
6537
6538 queue_count = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006539
6540 if (queue_count != pf->rss_size) {
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006541 i40e_prep_for_reset(pf);
6542
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006543 pf->rss_size = queue_count;
6544
6545 i40e_reset_and_rebuild(pf, true);
6546 i40e_config_rss(pf);
6547 }
6548 dev_info(&pf->pdev->dev, "RSS count: %d\n", pf->rss_size);
6549 return pf->rss_size;
6550}
6551
6552/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006553 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
6554 * @pf: board private structure to initialize
6555 *
6556 * i40e_sw_init initializes the Adapter private data structure.
6557 * Fields are initialized based on PCI device information and
6558 * OS network device settings (MTU size).
6559 **/
6560static int i40e_sw_init(struct i40e_pf *pf)
6561{
6562 int err = 0;
6563 int size;
6564
6565 pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
6566 (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
Shannon Nelson27599972013-11-16 10:00:43 +00006567 pf->hw.debug_mask = pf->msg_enable | I40E_DEBUG_DIAG;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006568 if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
6569 if (I40E_DEBUG_USER & debug)
6570 pf->hw.debug_mask = debug;
6571 pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
6572 I40E_DEFAULT_MSG_ENABLE);
6573 }
6574
6575 /* Set default capability flags */
6576 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
6577 I40E_FLAG_MSI_ENABLED |
6578 I40E_FLAG_MSIX_ENABLED |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006579 I40E_FLAG_RX_1BUF_ENABLED;
6580
Mitch Williamsca99eb92014-04-04 04:43:07 +00006581 /* Set default ITR */
6582 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
6583 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
6584
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006585 /* Depending on PF configurations, it is possible that the RSS
6586 * maximum might end up larger than the available queues
6587 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006588 pf->rss_size_max = 0x1 << pf->hw.func_caps.rss_table_entry_width;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006589 pf->rss_size_max = min_t(int, pf->rss_size_max,
6590 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006591 if (pf->hw.func_caps.rss) {
6592 pf->flags |= I40E_FLAG_RSS_ENABLED;
Jesse Brandeburgbf051a32013-11-26 10:49:17 +00006593 pf->rss_size = min_t(int, pf->rss_size_max, num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006594 } else {
6595 pf->rss_size = 1;
6596 }
6597
Catherine Sullivan2050bc62013-12-18 13:46:03 +00006598 /* MFP mode enabled */
6599 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.mfp_mode_1) {
6600 pf->flags |= I40E_FLAG_MFP_ENABLED;
6601 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
6602 }
6603
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006604 /* FW/NVM is not yet fixed in this regard */
6605 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
6606 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
6607 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
6608 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006609 if (!(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006610 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006611 } else {
6612 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00006613 "Flow Director Sideband mode Disabled in MFP mode\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006614 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006615 pf->fdir_pf_filter_count =
6616 pf->hw.func_caps.fd_filters_guaranteed;
6617 pf->hw.fdir_shared_filter_count =
6618 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006619 }
6620
6621 if (pf->hw.func_caps.vmdq) {
6622 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
6623 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
6624 pf->num_vmdq_qps = I40E_DEFAULT_QUEUES_PER_VMDQ;
6625 }
6626
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006627#ifdef CONFIG_PCI_IOV
6628 if (pf->hw.func_caps.num_vfs) {
6629 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
6630 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
6631 pf->num_req_vfs = min_t(int,
6632 pf->hw.func_caps.num_vfs,
6633 I40E_MAX_VF_COUNT);
6634 }
6635#endif /* CONFIG_PCI_IOV */
6636 pf->eeprom_version = 0xDEAD;
6637 pf->lan_veb = I40E_NO_VEB;
6638 pf->lan_vsi = I40E_NO_VSI;
6639
6640 /* set up queue assignment tracking */
6641 size = sizeof(struct i40e_lump_tracking)
6642 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
6643 pf->qp_pile = kzalloc(size, GFP_KERNEL);
6644 if (!pf->qp_pile) {
6645 err = -ENOMEM;
6646 goto sw_init_done;
6647 }
6648 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
6649 pf->qp_pile->search_hint = 0;
6650
6651 /* set up vector assignment tracking */
6652 size = sizeof(struct i40e_lump_tracking)
6653 + (sizeof(u16) * pf->hw.func_caps.num_msix_vectors);
6654 pf->irq_pile = kzalloc(size, GFP_KERNEL);
6655 if (!pf->irq_pile) {
6656 kfree(pf->qp_pile);
6657 err = -ENOMEM;
6658 goto sw_init_done;
6659 }
6660 pf->irq_pile->num_entries = pf->hw.func_caps.num_msix_vectors;
6661 pf->irq_pile->search_hint = 0;
6662
6663 mutex_init(&pf->switch_mutex);
6664
6665sw_init_done:
6666 return err;
6667}
6668
6669/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00006670 * i40e_set_ntuple - set the ntuple feature flag and take action
6671 * @pf: board private structure to initialize
6672 * @features: the feature set that the stack is suggesting
6673 *
6674 * returns a bool to indicate if reset needs to happen
6675 **/
6676bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
6677{
6678 bool need_reset = false;
6679
6680 /* Check if Flow Director n-tuple support was enabled or disabled. If
6681 * the state changed, we need to reset.
6682 */
6683 if (features & NETIF_F_NTUPLE) {
6684 /* Enable filters and mark for reset */
6685 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
6686 need_reset = true;
6687 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
6688 } else {
6689 /* turn off filters, mark for reset and clear SW filter list */
6690 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
6691 need_reset = true;
6692 i40e_fdir_filter_exit(pf);
6693 }
6694 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6695 /* if ATR was disabled it can be re-enabled. */
6696 if (!(pf->flags & I40E_FLAG_FD_ATR_ENABLED))
6697 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
6698 }
6699 return need_reset;
6700}
6701
6702/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006703 * i40e_set_features - set the netdev feature flags
6704 * @netdev: ptr to the netdev being adjusted
6705 * @features: the feature set that the stack is suggesting
6706 **/
6707static int i40e_set_features(struct net_device *netdev,
6708 netdev_features_t features)
6709{
6710 struct i40e_netdev_priv *np = netdev_priv(netdev);
6711 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00006712 struct i40e_pf *pf = vsi->back;
6713 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006714
6715 if (features & NETIF_F_HW_VLAN_CTAG_RX)
6716 i40e_vlan_stripping_enable(vsi);
6717 else
6718 i40e_vlan_stripping_disable(vsi);
6719
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00006720 need_reset = i40e_set_ntuple(pf, features);
6721
6722 if (need_reset)
6723 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
6724
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006725 return 0;
6726}
6727
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006728#ifdef CONFIG_I40E_VXLAN
6729/**
6730 * i40e_get_vxlan_port_idx - Lookup a possibly offloaded for Rx UDP port
6731 * @pf: board private structure
6732 * @port: The UDP port to look up
6733 *
6734 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
6735 **/
6736static u8 i40e_get_vxlan_port_idx(struct i40e_pf *pf, __be16 port)
6737{
6738 u8 i;
6739
6740 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
6741 if (pf->vxlan_ports[i] == port)
6742 return i;
6743 }
6744
6745 return i;
6746}
6747
6748/**
6749 * i40e_add_vxlan_port - Get notifications about VXLAN ports that come up
6750 * @netdev: This physical port's netdev
6751 * @sa_family: Socket Family that VXLAN is notifying us about
6752 * @port: New UDP port number that VXLAN started listening to
6753 **/
6754static void i40e_add_vxlan_port(struct net_device *netdev,
6755 sa_family_t sa_family, __be16 port)
6756{
6757 struct i40e_netdev_priv *np = netdev_priv(netdev);
6758 struct i40e_vsi *vsi = np->vsi;
6759 struct i40e_pf *pf = vsi->back;
6760 u8 next_idx;
6761 u8 idx;
6762
6763 if (sa_family == AF_INET6)
6764 return;
6765
6766 idx = i40e_get_vxlan_port_idx(pf, port);
6767
6768 /* Check if port already exists */
6769 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6770 netdev_info(netdev, "Port %d already offloaded\n", ntohs(port));
6771 return;
6772 }
6773
6774 /* Now check if there is space to add the new port */
6775 next_idx = i40e_get_vxlan_port_idx(pf, 0);
6776
6777 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6778 netdev_info(netdev, "Maximum number of UDP ports reached, not adding port %d\n",
6779 ntohs(port));
6780 return;
6781 }
6782
6783 /* New port: add it and mark its index in the bitmap */
6784 pf->vxlan_ports[next_idx] = port;
6785 pf->pending_vxlan_bitmap |= (1 << next_idx);
6786
6787 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
6788}
6789
6790/**
6791 * i40e_del_vxlan_port - Get notifications about VXLAN ports that go away
6792 * @netdev: This physical port's netdev
6793 * @sa_family: Socket Family that VXLAN is notifying us about
6794 * @port: UDP port number that VXLAN stopped listening to
6795 **/
6796static void i40e_del_vxlan_port(struct net_device *netdev,
6797 sa_family_t sa_family, __be16 port)
6798{
6799 struct i40e_netdev_priv *np = netdev_priv(netdev);
6800 struct i40e_vsi *vsi = np->vsi;
6801 struct i40e_pf *pf = vsi->back;
6802 u8 idx;
6803
6804 if (sa_family == AF_INET6)
6805 return;
6806
6807 idx = i40e_get_vxlan_port_idx(pf, port);
6808
6809 /* Check if port already exists */
6810 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6811 /* if port exists, set it to 0 (mark for deletion)
6812 * and make it pending
6813 */
6814 pf->vxlan_ports[idx] = 0;
6815
6816 pf->pending_vxlan_bitmap |= (1 << idx);
6817
6818 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
6819 } else {
6820 netdev_warn(netdev, "Port %d was not found, not deleting\n",
6821 ntohs(port));
6822 }
6823}
6824
6825#endif
Greg Rose4ba0dea2014-03-06 08:59:55 +00006826#ifdef HAVE_FDB_OPS
6827#ifdef USE_CONST_DEV_UC_CHAR
6828static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
6829 struct net_device *dev,
6830 const unsigned char *addr,
6831 u16 flags)
6832#else
6833static int i40e_ndo_fdb_add(struct ndmsg *ndm,
6834 struct net_device *dev,
6835 unsigned char *addr,
6836 u16 flags)
6837#endif
6838{
6839 struct i40e_netdev_priv *np = netdev_priv(dev);
6840 struct i40e_pf *pf = np->vsi->back;
6841 int err = 0;
6842
6843 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
6844 return -EOPNOTSUPP;
6845
6846 /* Hardware does not support aging addresses so if a
6847 * ndm_state is given only allow permanent addresses
6848 */
6849 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
6850 netdev_info(dev, "FDB only supports static addresses\n");
6851 return -EINVAL;
6852 }
6853
6854 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
6855 err = dev_uc_add_excl(dev, addr);
6856 else if (is_multicast_ether_addr(addr))
6857 err = dev_mc_add_excl(dev, addr);
6858 else
6859 err = -EINVAL;
6860
6861 /* Only return duplicate errors if NLM_F_EXCL is set */
6862 if (err == -EEXIST && !(flags & NLM_F_EXCL))
6863 err = 0;
6864
6865 return err;
6866}
6867
6868#ifndef USE_DEFAULT_FDB_DEL_DUMP
6869#ifdef USE_CONST_DEV_UC_CHAR
6870static int i40e_ndo_fdb_del(struct ndmsg *ndm,
6871 struct net_device *dev,
6872 const unsigned char *addr)
6873#else
6874static int i40e_ndo_fdb_del(struct ndmsg *ndm,
6875 struct net_device *dev,
6876 unsigned char *addr)
6877#endif
6878{
6879 struct i40e_netdev_priv *np = netdev_priv(dev);
6880 struct i40e_pf *pf = np->vsi->back;
6881 int err = -EOPNOTSUPP;
6882
6883 if (ndm->ndm_state & NUD_PERMANENT) {
6884 netdev_info(dev, "FDB only supports static addresses\n");
6885 return -EINVAL;
6886 }
6887
6888 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
6889 if (is_unicast_ether_addr(addr))
6890 err = dev_uc_del(dev, addr);
6891 else if (is_multicast_ether_addr(addr))
6892 err = dev_mc_del(dev, addr);
6893 else
6894 err = -EINVAL;
6895 }
6896
6897 return err;
6898}
6899
6900static int i40e_ndo_fdb_dump(struct sk_buff *skb,
6901 struct netlink_callback *cb,
6902 struct net_device *dev,
6903 int idx)
6904{
6905 struct i40e_netdev_priv *np = netdev_priv(dev);
6906 struct i40e_pf *pf = np->vsi->back;
6907
6908 if (pf->flags & I40E_FLAG_SRIOV_ENABLED)
6909 idx = ndo_dflt_fdb_dump(skb, cb, dev, idx);
6910
6911 return idx;
6912}
6913
6914#endif /* USE_DEFAULT_FDB_DEL_DUMP */
6915#endif /* HAVE_FDB_OPS */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006916static const struct net_device_ops i40e_netdev_ops = {
6917 .ndo_open = i40e_open,
6918 .ndo_stop = i40e_close,
6919 .ndo_start_xmit = i40e_lan_xmit_frame,
6920 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
6921 .ndo_set_rx_mode = i40e_set_rx_mode,
6922 .ndo_validate_addr = eth_validate_addr,
6923 .ndo_set_mac_address = i40e_set_mac,
6924 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006925 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006926 .ndo_tx_timeout = i40e_tx_timeout,
6927 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
6928 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
6929#ifdef CONFIG_NET_POLL_CONTROLLER
6930 .ndo_poll_controller = i40e_netpoll,
6931#endif
6932 .ndo_setup_tc = i40e_setup_tc,
6933 .ndo_set_features = i40e_set_features,
6934 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
6935 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04006936 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006937 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00006938 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Mitch Williamsc674d122014-05-20 08:01:40 +00006939 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofck,
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006940#ifdef CONFIG_I40E_VXLAN
6941 .ndo_add_vxlan_port = i40e_add_vxlan_port,
6942 .ndo_del_vxlan_port = i40e_del_vxlan_port,
6943#endif
Greg Rose4ba0dea2014-03-06 08:59:55 +00006944#ifdef HAVE_FDB_OPS
6945 .ndo_fdb_add = i40e_ndo_fdb_add,
6946#ifndef USE_DEFAULT_FDB_DEL_DUMP
6947 .ndo_fdb_del = i40e_ndo_fdb_del,
6948 .ndo_fdb_dump = i40e_ndo_fdb_dump,
6949#endif
6950#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006951};
6952
6953/**
6954 * i40e_config_netdev - Setup the netdev flags
6955 * @vsi: the VSI being configured
6956 *
6957 * Returns 0 on success, negative value on failure
6958 **/
6959static int i40e_config_netdev(struct i40e_vsi *vsi)
6960{
Greg Rose1a103702013-11-28 06:42:39 +00006961 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006962 struct i40e_pf *pf = vsi->back;
6963 struct i40e_hw *hw = &pf->hw;
6964 struct i40e_netdev_priv *np;
6965 struct net_device *netdev;
6966 u8 mac_addr[ETH_ALEN];
6967 int etherdev_size;
6968
6969 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006970 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006971 if (!netdev)
6972 return -ENOMEM;
6973
6974 vsi->netdev = netdev;
6975 np = netdev_priv(netdev);
6976 np->vsi = vsi;
6977
Or Gerlitzd70e9412014-03-18 10:36:45 +02006978 netdev->hw_enc_features |= NETIF_F_IP_CSUM |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006979 NETIF_F_GSO_UDP_TUNNEL |
Or Gerlitzd70e9412014-03-18 10:36:45 +02006980 NETIF_F_TSO;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006981
6982 netdev->features = NETIF_F_SG |
6983 NETIF_F_IP_CSUM |
6984 NETIF_F_SCTP_CSUM |
6985 NETIF_F_HIGHDMA |
6986 NETIF_F_GSO_UDP_TUNNEL |
6987 NETIF_F_HW_VLAN_CTAG_TX |
6988 NETIF_F_HW_VLAN_CTAG_RX |
6989 NETIF_F_HW_VLAN_CTAG_FILTER |
6990 NETIF_F_IPV6_CSUM |
6991 NETIF_F_TSO |
Jesse Brandeburg059dab62014-04-01 09:07:20 +00006992 NETIF_F_TSO_ECN |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006993 NETIF_F_TSO6 |
6994 NETIF_F_RXCSUM |
6995 NETIF_F_RXHASH |
6996 0;
6997
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00006998 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
6999 netdev->features |= NETIF_F_NTUPLE;
7000
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007001 /* copy netdev features into list of user selectable features */
7002 netdev->hw_features |= netdev->features;
7003
7004 if (vsi->type == I40E_VSI_MAIN) {
7005 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
7006 memcpy(mac_addr, hw->mac.perm_addr, ETH_ALEN);
Greg Rose8c27d422014-05-22 06:31:56 +00007007 /* The following two steps are necessary to prevent reception
7008 * of tagged packets - by default the NVM loads a MAC-VLAN
7009 * filter that will accept any tagged packet. This is to
7010 * prevent that during normal operations until a specific
7011 * VLAN tag filter has been set.
7012 */
7013 i40e_rm_default_mac_filter(vsi, mac_addr);
7014 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007015 } else {
7016 /* relate the VSI_VMDQ name to the VSI_MAIN name */
7017 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
7018 pf->vsi[pf->lan_vsi]->netdev->name);
7019 random_ether_addr(mac_addr);
7020 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
7021 }
Greg Rose1a103702013-11-28 06:42:39 +00007022 i40e_add_filter(vsi, brdcast, I40E_VLAN_ANY, false, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007023
7024 memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
7025 memcpy(netdev->perm_addr, mac_addr, ETH_ALEN);
7026 /* vlan gets same features (except vlan offload)
7027 * after any tweaks for specific VSI types
7028 */
7029 netdev->vlan_features = netdev->features & ~(NETIF_F_HW_VLAN_CTAG_TX |
7030 NETIF_F_HW_VLAN_CTAG_RX |
7031 NETIF_F_HW_VLAN_CTAG_FILTER);
7032 netdev->priv_flags |= IFF_UNICAST_FLT;
7033 netdev->priv_flags |= IFF_SUPP_NOFCS;
7034 /* Setup netdev TC information */
7035 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
7036
7037 netdev->netdev_ops = &i40e_netdev_ops;
7038 netdev->watchdog_timeo = 5 * HZ;
7039 i40e_set_ethtool_ops(netdev);
7040
7041 return 0;
7042}
7043
7044/**
7045 * i40e_vsi_delete - Delete a VSI from the switch
7046 * @vsi: the VSI being removed
7047 *
7048 * Returns 0 on success, negative value on failure
7049 **/
7050static void i40e_vsi_delete(struct i40e_vsi *vsi)
7051{
7052 /* remove default VSI is not allowed */
7053 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
7054 return;
7055
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007056 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007057}
7058
7059/**
7060 * i40e_add_vsi - Add a VSI to the switch
7061 * @vsi: the VSI being configured
7062 *
7063 * This initializes a VSI context depending on the VSI type to be added and
7064 * passes it down to the add_vsi aq command.
7065 **/
7066static int i40e_add_vsi(struct i40e_vsi *vsi)
7067{
7068 int ret = -ENODEV;
7069 struct i40e_mac_filter *f, *ftmp;
7070 struct i40e_pf *pf = vsi->back;
7071 struct i40e_hw *hw = &pf->hw;
7072 struct i40e_vsi_context ctxt;
7073 u8 enabled_tc = 0x1; /* TC0 enabled */
7074 int f_count = 0;
7075
7076 memset(&ctxt, 0, sizeof(ctxt));
7077 switch (vsi->type) {
7078 case I40E_VSI_MAIN:
7079 /* The PF's main VSI is already setup as part of the
7080 * device initialization, so we'll not bother with
7081 * the add_vsi call, but we will retrieve the current
7082 * VSI context.
7083 */
7084 ctxt.seid = pf->main_vsi_seid;
7085 ctxt.pf_num = pf->hw.pf_id;
7086 ctxt.vf_num = 0;
7087 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
7088 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
7089 if (ret) {
7090 dev_info(&pf->pdev->dev,
7091 "couldn't get pf vsi config, err %d, aq_err %d\n",
7092 ret, pf->hw.aq.asq_last_status);
7093 return -ENOENT;
7094 }
7095 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
7096 vsi->info.valid_sections = 0;
7097
7098 vsi->seid = ctxt.seid;
7099 vsi->id = ctxt.vsi_number;
7100
7101 enabled_tc = i40e_pf_get_tc_map(pf);
7102
7103 /* MFP mode setup queue map and update VSI */
7104 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
7105 memset(&ctxt, 0, sizeof(ctxt));
7106 ctxt.seid = pf->main_vsi_seid;
7107 ctxt.pf_num = pf->hw.pf_id;
7108 ctxt.vf_num = 0;
7109 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
7110 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7111 if (ret) {
7112 dev_info(&pf->pdev->dev,
7113 "update vsi failed, aq_err=%d\n",
7114 pf->hw.aq.asq_last_status);
7115 ret = -ENOENT;
7116 goto err;
7117 }
7118 /* update the local VSI info queue map */
7119 i40e_vsi_update_queue_map(vsi, &ctxt);
7120 vsi->info.valid_sections = 0;
7121 } else {
7122 /* Default/Main VSI is only enabled for TC0
7123 * reconfigure it to enable all TCs that are
7124 * available on the port in SFP mode.
7125 */
7126 ret = i40e_vsi_config_tc(vsi, enabled_tc);
7127 if (ret) {
7128 dev_info(&pf->pdev->dev,
7129 "failed to configure TCs for main VSI tc_map 0x%08x, err %d, aq_err %d\n",
7130 enabled_tc, ret,
7131 pf->hw.aq.asq_last_status);
7132 ret = -ENOENT;
7133 }
7134 }
7135 break;
7136
7137 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007138 ctxt.pf_num = hw->pf_id;
7139 ctxt.vf_num = 0;
7140 ctxt.uplink_seid = vsi->uplink_seid;
7141 ctxt.connection_type = 0x1; /* regular data port */
7142 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007143 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007144 break;
7145
7146 case I40E_VSI_VMDQ2:
7147 ctxt.pf_num = hw->pf_id;
7148 ctxt.vf_num = 0;
7149 ctxt.uplink_seid = vsi->uplink_seid;
7150 ctxt.connection_type = 0x1; /* regular data port */
7151 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
7152
7153 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
7154
7155 /* This VSI is connected to VEB so the switch_id
7156 * should be set to zero by default.
7157 */
7158 ctxt.info.switch_id = 0;
7159 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
7160 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
7161
7162 /* Setup the VSI tx/rx queue map for TC0 only for now */
7163 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
7164 break;
7165
7166 case I40E_VSI_SRIOV:
7167 ctxt.pf_num = hw->pf_id;
7168 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
7169 ctxt.uplink_seid = vsi->uplink_seid;
7170 ctxt.connection_type = 0x1; /* regular data port */
7171 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
7172
7173 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
7174
7175 /* This VSI is connected to VEB so the switch_id
7176 * should be set to zero by default.
7177 */
7178 ctxt.info.switch_id = cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
7179
7180 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
7181 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00007182 if (pf->vf[vsi->vf_id].spoofchk) {
7183 ctxt.info.valid_sections |=
7184 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
7185 ctxt.info.sec_flags |=
7186 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
7187 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
7188 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007189 /* Setup the VSI tx/rx queue map for TC0 only for now */
7190 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
7191 break;
7192
7193 default:
7194 return -ENODEV;
7195 }
7196
7197 if (vsi->type != I40E_VSI_MAIN) {
7198 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
7199 if (ret) {
7200 dev_info(&vsi->back->pdev->dev,
7201 "add vsi failed, aq_err=%d\n",
7202 vsi->back->hw.aq.asq_last_status);
7203 ret = -ENOENT;
7204 goto err;
7205 }
7206 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
7207 vsi->info.valid_sections = 0;
7208 vsi->seid = ctxt.seid;
7209 vsi->id = ctxt.vsi_number;
7210 }
7211
7212 /* If macvlan filters already exist, force them to get loaded */
7213 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
7214 f->changed = true;
7215 f_count++;
7216 }
7217 if (f_count) {
7218 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
7219 pf->flags |= I40E_FLAG_FILTER_SYNC;
7220 }
7221
7222 /* Update VSI BW information */
7223 ret = i40e_vsi_get_bw_info(vsi);
7224 if (ret) {
7225 dev_info(&pf->pdev->dev,
7226 "couldn't get vsi bw info, err %d, aq_err %d\n",
7227 ret, pf->hw.aq.asq_last_status);
7228 /* VSI is already added so not tearing that up */
7229 ret = 0;
7230 }
7231
7232err:
7233 return ret;
7234}
7235
7236/**
7237 * i40e_vsi_release - Delete a VSI and free its resources
7238 * @vsi: the VSI being removed
7239 *
7240 * Returns 0 on success or < 0 on error
7241 **/
7242int i40e_vsi_release(struct i40e_vsi *vsi)
7243{
7244 struct i40e_mac_filter *f, *ftmp;
7245 struct i40e_veb *veb = NULL;
7246 struct i40e_pf *pf;
7247 u16 uplink_seid;
7248 int i, n;
7249
7250 pf = vsi->back;
7251
7252 /* release of a VEB-owner or last VSI is not allowed */
7253 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
7254 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
7255 vsi->seid, vsi->uplink_seid);
7256 return -ENODEV;
7257 }
7258 if (vsi == pf->vsi[pf->lan_vsi] &&
7259 !test_bit(__I40E_DOWN, &pf->state)) {
7260 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
7261 return -ENODEV;
7262 }
7263
7264 uplink_seid = vsi->uplink_seid;
7265 if (vsi->type != I40E_VSI_SRIOV) {
7266 if (vsi->netdev_registered) {
7267 vsi->netdev_registered = false;
7268 if (vsi->netdev) {
7269 /* results in a call to i40e_close() */
7270 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007271 }
7272 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00007273 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007274 }
7275 i40e_vsi_disable_irq(vsi);
7276 }
7277
7278 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
7279 i40e_del_filter(vsi, f->macaddr, f->vlan,
7280 f->is_vf, f->is_netdev);
7281 i40e_sync_vsi_filters(vsi);
7282
7283 i40e_vsi_delete(vsi);
7284 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00007285 if (vsi->netdev) {
7286 free_netdev(vsi->netdev);
7287 vsi->netdev = NULL;
7288 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007289 i40e_vsi_clear_rings(vsi);
7290 i40e_vsi_clear(vsi);
7291
7292 /* If this was the last thing on the VEB, except for the
7293 * controlling VSI, remove the VEB, which puts the controlling
7294 * VSI onto the next level down in the switch.
7295 *
7296 * Well, okay, there's one more exception here: don't remove
7297 * the orphan VEBs yet. We'll wait for an explicit remove request
7298 * from up the network stack.
7299 */
Mitch Williams505682c2014-05-20 08:01:37 +00007300 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007301 if (pf->vsi[i] &&
7302 pf->vsi[i]->uplink_seid == uplink_seid &&
7303 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
7304 n++; /* count the VSIs */
7305 }
7306 }
7307 for (i = 0; i < I40E_MAX_VEB; i++) {
7308 if (!pf->veb[i])
7309 continue;
7310 if (pf->veb[i]->uplink_seid == uplink_seid)
7311 n++; /* count the VEBs */
7312 if (pf->veb[i]->seid == uplink_seid)
7313 veb = pf->veb[i];
7314 }
7315 if (n == 0 && veb && veb->uplink_seid != 0)
7316 i40e_veb_release(veb);
7317
7318 return 0;
7319}
7320
7321/**
7322 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
7323 * @vsi: ptr to the VSI
7324 *
7325 * This should only be called after i40e_vsi_mem_alloc() which allocates the
7326 * corresponding SW VSI structure and initializes num_queue_pairs for the
7327 * newly allocated VSI.
7328 *
7329 * Returns 0 on success or negative on failure
7330 **/
7331static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
7332{
7333 int ret = -ENOENT;
7334 struct i40e_pf *pf = vsi->back;
7335
Alexander Duyck493fb302013-09-28 07:01:44 +00007336 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007337 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
7338 vsi->seid);
7339 return -EEXIST;
7340 }
7341
7342 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00007343 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007344 vsi->seid, vsi->base_vector);
7345 return -EEXIST;
7346 }
7347
Greg Rose90e04072014-03-06 08:59:57 +00007348 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007349 if (ret) {
7350 dev_info(&pf->pdev->dev,
7351 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
7352 vsi->num_q_vectors, vsi->seid, ret);
7353 vsi->num_q_vectors = 0;
7354 goto vector_setup_out;
7355 }
7356
Shannon Nelson958a3e32013-09-28 07:13:28 +00007357 if (vsi->num_q_vectors)
7358 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
7359 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007360 if (vsi->base_vector < 0) {
7361 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00007362 "failed to get queue tracking for VSI %d, err=%d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007363 vsi->seid, vsi->base_vector);
7364 i40e_vsi_free_q_vectors(vsi);
7365 ret = -ENOENT;
7366 goto vector_setup_out;
7367 }
7368
7369vector_setup_out:
7370 return ret;
7371}
7372
7373/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007374 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
7375 * @vsi: pointer to the vsi.
7376 *
7377 * This re-allocates a vsi's queue resources.
7378 *
7379 * Returns pointer to the successfully allocated and configured VSI sw struct
7380 * on success, otherwise returns NULL on failure.
7381 **/
7382static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
7383{
7384 struct i40e_pf *pf = vsi->back;
7385 u8 enabled_tc;
7386 int ret;
7387
7388 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7389 i40e_vsi_clear_rings(vsi);
7390
7391 i40e_vsi_free_arrays(vsi, false);
7392 i40e_set_num_rings_in_vsi(vsi);
7393 ret = i40e_vsi_alloc_arrays(vsi, false);
7394 if (ret)
7395 goto err_vsi;
7396
7397 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
7398 if (ret < 0) {
7399 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
7400 vsi->seid, ret);
7401 goto err_vsi;
7402 }
7403 vsi->base_queue = ret;
7404
7405 /* Update the FW view of the VSI. Force a reset of TC and queue
7406 * layout configurations.
7407 */
7408 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
7409 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
7410 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
7411 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
7412
7413 /* assign it some queues */
7414 ret = i40e_alloc_rings(vsi);
7415 if (ret)
7416 goto err_rings;
7417
7418 /* map all of the rings to the q_vectors */
7419 i40e_vsi_map_rings_to_vectors(vsi);
7420 return vsi;
7421
7422err_rings:
7423 i40e_vsi_free_q_vectors(vsi);
7424 if (vsi->netdev_registered) {
7425 vsi->netdev_registered = false;
7426 unregister_netdev(vsi->netdev);
7427 free_netdev(vsi->netdev);
7428 vsi->netdev = NULL;
7429 }
7430 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
7431err_vsi:
7432 i40e_vsi_clear(vsi);
7433 return NULL;
7434}
7435
7436/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007437 * i40e_vsi_setup - Set up a VSI by a given type
7438 * @pf: board private structure
7439 * @type: VSI type
7440 * @uplink_seid: the switch element to link to
7441 * @param1: usage depends upon VSI type. For VF types, indicates VF id
7442 *
7443 * This allocates the sw VSI structure and its queue resources, then add a VSI
7444 * to the identified VEB.
7445 *
7446 * Returns pointer to the successfully allocated and configure VSI sw struct on
7447 * success, otherwise returns NULL on failure.
7448 **/
7449struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
7450 u16 uplink_seid, u32 param1)
7451{
7452 struct i40e_vsi *vsi = NULL;
7453 struct i40e_veb *veb = NULL;
7454 int ret, i;
7455 int v_idx;
7456
7457 /* The requested uplink_seid must be either
7458 * - the PF's port seid
7459 * no VEB is needed because this is the PF
7460 * or this is a Flow Director special case VSI
7461 * - seid of an existing VEB
7462 * - seid of a VSI that owns an existing VEB
7463 * - seid of a VSI that doesn't own a VEB
7464 * a new VEB is created and the VSI becomes the owner
7465 * - seid of the PF VSI, which is what creates the first VEB
7466 * this is a special case of the previous
7467 *
7468 * Find which uplink_seid we were given and create a new VEB if needed
7469 */
7470 for (i = 0; i < I40E_MAX_VEB; i++) {
7471 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
7472 veb = pf->veb[i];
7473 break;
7474 }
7475 }
7476
7477 if (!veb && uplink_seid != pf->mac_seid) {
7478
Mitch Williams505682c2014-05-20 08:01:37 +00007479 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007480 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
7481 vsi = pf->vsi[i];
7482 break;
7483 }
7484 }
7485 if (!vsi) {
7486 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
7487 uplink_seid);
7488 return NULL;
7489 }
7490
7491 if (vsi->uplink_seid == pf->mac_seid)
7492 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
7493 vsi->tc_config.enabled_tc);
7494 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
7495 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
7496 vsi->tc_config.enabled_tc);
7497
7498 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
7499 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
7500 veb = pf->veb[i];
7501 }
7502 if (!veb) {
7503 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
7504 return NULL;
7505 }
7506
7507 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
7508 uplink_seid = veb->seid;
7509 }
7510
7511 /* get vsi sw struct */
7512 v_idx = i40e_vsi_mem_alloc(pf, type);
7513 if (v_idx < 0)
7514 goto err_alloc;
7515 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007516 if (!vsi)
7517 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007518 vsi->type = type;
7519 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
7520
7521 if (type == I40E_VSI_MAIN)
7522 pf->lan_vsi = v_idx;
7523 else if (type == I40E_VSI_SRIOV)
7524 vsi->vf_id = param1;
7525 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007526 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
7527 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007528 if (ret < 0) {
7529 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
7530 vsi->seid, ret);
7531 goto err_vsi;
7532 }
7533 vsi->base_queue = ret;
7534
7535 /* get a VSI from the hardware */
7536 vsi->uplink_seid = uplink_seid;
7537 ret = i40e_add_vsi(vsi);
7538 if (ret)
7539 goto err_vsi;
7540
7541 switch (vsi->type) {
7542 /* setup the netdev if needed */
7543 case I40E_VSI_MAIN:
7544 case I40E_VSI_VMDQ2:
7545 ret = i40e_config_netdev(vsi);
7546 if (ret)
7547 goto err_netdev;
7548 ret = register_netdev(vsi->netdev);
7549 if (ret)
7550 goto err_netdev;
7551 vsi->netdev_registered = true;
7552 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007553#ifdef CONFIG_I40E_DCB
7554 /* Setup DCB netlink interface */
7555 i40e_dcbnl_setup(vsi);
7556#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007557 /* fall through */
7558
7559 case I40E_VSI_FDIR:
7560 /* set up vectors and rings if needed */
7561 ret = i40e_vsi_setup_vectors(vsi);
7562 if (ret)
7563 goto err_msix;
7564
7565 ret = i40e_alloc_rings(vsi);
7566 if (ret)
7567 goto err_rings;
7568
7569 /* map all of the rings to the q_vectors */
7570 i40e_vsi_map_rings_to_vectors(vsi);
7571
7572 i40e_vsi_reset_stats(vsi);
7573 break;
7574
7575 default:
7576 /* no netdev or rings for the other VSI types */
7577 break;
7578 }
7579
7580 return vsi;
7581
7582err_rings:
7583 i40e_vsi_free_q_vectors(vsi);
7584err_msix:
7585 if (vsi->netdev_registered) {
7586 vsi->netdev_registered = false;
7587 unregister_netdev(vsi->netdev);
7588 free_netdev(vsi->netdev);
7589 vsi->netdev = NULL;
7590 }
7591err_netdev:
7592 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
7593err_vsi:
7594 i40e_vsi_clear(vsi);
7595err_alloc:
7596 return NULL;
7597}
7598
7599/**
7600 * i40e_veb_get_bw_info - Query VEB BW information
7601 * @veb: the veb to query
7602 *
7603 * Query the Tx scheduler BW configuration data for given VEB
7604 **/
7605static int i40e_veb_get_bw_info(struct i40e_veb *veb)
7606{
7607 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
7608 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
7609 struct i40e_pf *pf = veb->pf;
7610 struct i40e_hw *hw = &pf->hw;
7611 u32 tc_bw_max;
7612 int ret = 0;
7613 int i;
7614
7615 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
7616 &bw_data, NULL);
7617 if (ret) {
7618 dev_info(&pf->pdev->dev,
7619 "query veb bw config failed, aq_err=%d\n",
7620 hw->aq.asq_last_status);
7621 goto out;
7622 }
7623
7624 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
7625 &ets_data, NULL);
7626 if (ret) {
7627 dev_info(&pf->pdev->dev,
7628 "query veb bw ets config failed, aq_err=%d\n",
7629 hw->aq.asq_last_status);
7630 goto out;
7631 }
7632
7633 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
7634 veb->bw_max_quanta = ets_data.tc_bw_max;
7635 veb->is_abs_credits = bw_data.absolute_credits_enable;
7636 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
7637 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
7638 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7639 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
7640 veb->bw_tc_limit_credits[i] =
7641 le16_to_cpu(bw_data.tc_bw_limits[i]);
7642 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
7643 }
7644
7645out:
7646 return ret;
7647}
7648
7649/**
7650 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
7651 * @pf: board private structure
7652 *
7653 * On error: returns error code (negative)
7654 * On success: returns vsi index in PF (positive)
7655 **/
7656static int i40e_veb_mem_alloc(struct i40e_pf *pf)
7657{
7658 int ret = -ENOENT;
7659 struct i40e_veb *veb;
7660 int i;
7661
7662 /* Need to protect the allocation of switch elements at the PF level */
7663 mutex_lock(&pf->switch_mutex);
7664
7665 /* VEB list may be fragmented if VEB creation/destruction has
7666 * been happening. We can afford to do a quick scan to look
7667 * for any free slots in the list.
7668 *
7669 * find next empty veb slot, looping back around if necessary
7670 */
7671 i = 0;
7672 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
7673 i++;
7674 if (i >= I40E_MAX_VEB) {
7675 ret = -ENOMEM;
7676 goto err_alloc_veb; /* out of VEB slots! */
7677 }
7678
7679 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
7680 if (!veb) {
7681 ret = -ENOMEM;
7682 goto err_alloc_veb;
7683 }
7684 veb->pf = pf;
7685 veb->idx = i;
7686 veb->enabled_tc = 1;
7687
7688 pf->veb[i] = veb;
7689 ret = i;
7690err_alloc_veb:
7691 mutex_unlock(&pf->switch_mutex);
7692 return ret;
7693}
7694
7695/**
7696 * i40e_switch_branch_release - Delete a branch of the switch tree
7697 * @branch: where to start deleting
7698 *
7699 * This uses recursion to find the tips of the branch to be
7700 * removed, deleting until we get back to and can delete this VEB.
7701 **/
7702static void i40e_switch_branch_release(struct i40e_veb *branch)
7703{
7704 struct i40e_pf *pf = branch->pf;
7705 u16 branch_seid = branch->seid;
7706 u16 veb_idx = branch->idx;
7707 int i;
7708
7709 /* release any VEBs on this VEB - RECURSION */
7710 for (i = 0; i < I40E_MAX_VEB; i++) {
7711 if (!pf->veb[i])
7712 continue;
7713 if (pf->veb[i]->uplink_seid == branch->seid)
7714 i40e_switch_branch_release(pf->veb[i]);
7715 }
7716
7717 /* Release the VSIs on this VEB, but not the owner VSI.
7718 *
7719 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
7720 * the VEB itself, so don't use (*branch) after this loop.
7721 */
Mitch Williams505682c2014-05-20 08:01:37 +00007722 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007723 if (!pf->vsi[i])
7724 continue;
7725 if (pf->vsi[i]->uplink_seid == branch_seid &&
7726 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
7727 i40e_vsi_release(pf->vsi[i]);
7728 }
7729 }
7730
7731 /* There's one corner case where the VEB might not have been
7732 * removed, so double check it here and remove it if needed.
7733 * This case happens if the veb was created from the debugfs
7734 * commands and no VSIs were added to it.
7735 */
7736 if (pf->veb[veb_idx])
7737 i40e_veb_release(pf->veb[veb_idx]);
7738}
7739
7740/**
7741 * i40e_veb_clear - remove veb struct
7742 * @veb: the veb to remove
7743 **/
7744static void i40e_veb_clear(struct i40e_veb *veb)
7745{
7746 if (!veb)
7747 return;
7748
7749 if (veb->pf) {
7750 struct i40e_pf *pf = veb->pf;
7751
7752 mutex_lock(&pf->switch_mutex);
7753 if (pf->veb[veb->idx] == veb)
7754 pf->veb[veb->idx] = NULL;
7755 mutex_unlock(&pf->switch_mutex);
7756 }
7757
7758 kfree(veb);
7759}
7760
7761/**
7762 * i40e_veb_release - Delete a VEB and free its resources
7763 * @veb: the VEB being removed
7764 **/
7765void i40e_veb_release(struct i40e_veb *veb)
7766{
7767 struct i40e_vsi *vsi = NULL;
7768 struct i40e_pf *pf;
7769 int i, n = 0;
7770
7771 pf = veb->pf;
7772
7773 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +00007774 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007775 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
7776 n++;
7777 vsi = pf->vsi[i];
7778 }
7779 }
7780 if (n != 1) {
7781 dev_info(&pf->pdev->dev,
7782 "can't remove VEB %d with %d VSIs left\n",
7783 veb->seid, n);
7784 return;
7785 }
7786
7787 /* move the remaining VSI to uplink veb */
7788 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
7789 if (veb->uplink_seid) {
7790 vsi->uplink_seid = veb->uplink_seid;
7791 if (veb->uplink_seid == pf->mac_seid)
7792 vsi->veb_idx = I40E_NO_VEB;
7793 else
7794 vsi->veb_idx = veb->veb_idx;
7795 } else {
7796 /* floating VEB */
7797 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
7798 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
7799 }
7800
7801 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
7802 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007803}
7804
7805/**
7806 * i40e_add_veb - create the VEB in the switch
7807 * @veb: the VEB to be instantiated
7808 * @vsi: the controlling VSI
7809 **/
7810static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
7811{
Greg Rose56747262013-11-28 06:39:37 +00007812 bool is_default = false;
Kevin Scotte1c51b952013-11-20 10:02:51 +00007813 bool is_cloud = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007814 int ret;
7815
7816 /* get a VEB from the hardware */
7817 ret = i40e_aq_add_veb(&veb->pf->hw, veb->uplink_seid, vsi->seid,
Kevin Scotte1c51b952013-11-20 10:02:51 +00007818 veb->enabled_tc, is_default,
7819 is_cloud, &veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007820 if (ret) {
7821 dev_info(&veb->pf->pdev->dev,
7822 "couldn't add VEB, err %d, aq_err %d\n",
7823 ret, veb->pf->hw.aq.asq_last_status);
7824 return -EPERM;
7825 }
7826
7827 /* get statistics counter */
7828 ret = i40e_aq_get_veb_parameters(&veb->pf->hw, veb->seid, NULL, NULL,
7829 &veb->stats_idx, NULL, NULL, NULL);
7830 if (ret) {
7831 dev_info(&veb->pf->pdev->dev,
7832 "couldn't get VEB statistics idx, err %d, aq_err %d\n",
7833 ret, veb->pf->hw.aq.asq_last_status);
7834 return -EPERM;
7835 }
7836 ret = i40e_veb_get_bw_info(veb);
7837 if (ret) {
7838 dev_info(&veb->pf->pdev->dev,
7839 "couldn't get VEB bw info, err %d, aq_err %d\n",
7840 ret, veb->pf->hw.aq.asq_last_status);
7841 i40e_aq_delete_element(&veb->pf->hw, veb->seid, NULL);
7842 return -ENOENT;
7843 }
7844
7845 vsi->uplink_seid = veb->seid;
7846 vsi->veb_idx = veb->idx;
7847 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
7848
7849 return 0;
7850}
7851
7852/**
7853 * i40e_veb_setup - Set up a VEB
7854 * @pf: board private structure
7855 * @flags: VEB setup flags
7856 * @uplink_seid: the switch element to link to
7857 * @vsi_seid: the initial VSI seid
7858 * @enabled_tc: Enabled TC bit-map
7859 *
7860 * This allocates the sw VEB structure and links it into the switch
7861 * It is possible and legal for this to be a duplicate of an already
7862 * existing VEB. It is also possible for both uplink and vsi seids
7863 * to be zero, in order to create a floating VEB.
7864 *
7865 * Returns pointer to the successfully allocated VEB sw struct on
7866 * success, otherwise returns NULL on failure.
7867 **/
7868struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
7869 u16 uplink_seid, u16 vsi_seid,
7870 u8 enabled_tc)
7871{
7872 struct i40e_veb *veb, *uplink_veb = NULL;
7873 int vsi_idx, veb_idx;
7874 int ret;
7875
7876 /* if one seid is 0, the other must be 0 to create a floating relay */
7877 if ((uplink_seid == 0 || vsi_seid == 0) &&
7878 (uplink_seid + vsi_seid != 0)) {
7879 dev_info(&pf->pdev->dev,
7880 "one, not both seid's are 0: uplink=%d vsi=%d\n",
7881 uplink_seid, vsi_seid);
7882 return NULL;
7883 }
7884
7885 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +00007886 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007887 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
7888 break;
Mitch Williams505682c2014-05-20 08:01:37 +00007889 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007890 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
7891 vsi_seid);
7892 return NULL;
7893 }
7894
7895 if (uplink_seid && uplink_seid != pf->mac_seid) {
7896 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
7897 if (pf->veb[veb_idx] &&
7898 pf->veb[veb_idx]->seid == uplink_seid) {
7899 uplink_veb = pf->veb[veb_idx];
7900 break;
7901 }
7902 }
7903 if (!uplink_veb) {
7904 dev_info(&pf->pdev->dev,
7905 "uplink seid %d not found\n", uplink_seid);
7906 return NULL;
7907 }
7908 }
7909
7910 /* get veb sw struct */
7911 veb_idx = i40e_veb_mem_alloc(pf);
7912 if (veb_idx < 0)
7913 goto err_alloc;
7914 veb = pf->veb[veb_idx];
7915 veb->flags = flags;
7916 veb->uplink_seid = uplink_seid;
7917 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
7918 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
7919
7920 /* create the VEB in the switch */
7921 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
7922 if (ret)
7923 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +00007924 if (vsi_idx == pf->lan_vsi)
7925 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007926
7927 return veb;
7928
7929err_veb:
7930 i40e_veb_clear(veb);
7931err_alloc:
7932 return NULL;
7933}
7934
7935/**
7936 * i40e_setup_pf_switch_element - set pf vars based on switch type
7937 * @pf: board private structure
7938 * @ele: element we are building info from
7939 * @num_reported: total number of elements
7940 * @printconfig: should we print the contents
7941 *
7942 * helper function to assist in extracting a few useful SEID values.
7943 **/
7944static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
7945 struct i40e_aqc_switch_config_element_resp *ele,
7946 u16 num_reported, bool printconfig)
7947{
7948 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
7949 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
7950 u8 element_type = ele->element_type;
7951 u16 seid = le16_to_cpu(ele->seid);
7952
7953 if (printconfig)
7954 dev_info(&pf->pdev->dev,
7955 "type=%d seid=%d uplink=%d downlink=%d\n",
7956 element_type, seid, uplink_seid, downlink_seid);
7957
7958 switch (element_type) {
7959 case I40E_SWITCH_ELEMENT_TYPE_MAC:
7960 pf->mac_seid = seid;
7961 break;
7962 case I40E_SWITCH_ELEMENT_TYPE_VEB:
7963 /* Main VEB? */
7964 if (uplink_seid != pf->mac_seid)
7965 break;
7966 if (pf->lan_veb == I40E_NO_VEB) {
7967 int v;
7968
7969 /* find existing or else empty VEB */
7970 for (v = 0; v < I40E_MAX_VEB; v++) {
7971 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
7972 pf->lan_veb = v;
7973 break;
7974 }
7975 }
7976 if (pf->lan_veb == I40E_NO_VEB) {
7977 v = i40e_veb_mem_alloc(pf);
7978 if (v < 0)
7979 break;
7980 pf->lan_veb = v;
7981 }
7982 }
7983
7984 pf->veb[pf->lan_veb]->seid = seid;
7985 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
7986 pf->veb[pf->lan_veb]->pf = pf;
7987 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
7988 break;
7989 case I40E_SWITCH_ELEMENT_TYPE_VSI:
7990 if (num_reported != 1)
7991 break;
7992 /* This is immediately after a reset so we can assume this is
7993 * the PF's VSI
7994 */
7995 pf->mac_seid = uplink_seid;
7996 pf->pf_seid = downlink_seid;
7997 pf->main_vsi_seid = seid;
7998 if (printconfig)
7999 dev_info(&pf->pdev->dev,
8000 "pf_seid=%d main_vsi_seid=%d\n",
8001 pf->pf_seid, pf->main_vsi_seid);
8002 break;
8003 case I40E_SWITCH_ELEMENT_TYPE_PF:
8004 case I40E_SWITCH_ELEMENT_TYPE_VF:
8005 case I40E_SWITCH_ELEMENT_TYPE_EMP:
8006 case I40E_SWITCH_ELEMENT_TYPE_BMC:
8007 case I40E_SWITCH_ELEMENT_TYPE_PE:
8008 case I40E_SWITCH_ELEMENT_TYPE_PA:
8009 /* ignore these for now */
8010 break;
8011 default:
8012 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
8013 element_type, seid);
8014 break;
8015 }
8016}
8017
8018/**
8019 * i40e_fetch_switch_configuration - Get switch config from firmware
8020 * @pf: board private structure
8021 * @printconfig: should we print the contents
8022 *
8023 * Get the current switch configuration from the device and
8024 * extract a few useful SEID values.
8025 **/
8026int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
8027{
8028 struct i40e_aqc_get_switch_config_resp *sw_config;
8029 u16 next_seid = 0;
8030 int ret = 0;
8031 u8 *aq_buf;
8032 int i;
8033
8034 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
8035 if (!aq_buf)
8036 return -ENOMEM;
8037
8038 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
8039 do {
8040 u16 num_reported, num_total;
8041
8042 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
8043 I40E_AQ_LARGE_BUF,
8044 &next_seid, NULL);
8045 if (ret) {
8046 dev_info(&pf->pdev->dev,
8047 "get switch config failed %d aq_err=%x\n",
8048 ret, pf->hw.aq.asq_last_status);
8049 kfree(aq_buf);
8050 return -ENOENT;
8051 }
8052
8053 num_reported = le16_to_cpu(sw_config->header.num_reported);
8054 num_total = le16_to_cpu(sw_config->header.num_total);
8055
8056 if (printconfig)
8057 dev_info(&pf->pdev->dev,
8058 "header: %d reported %d total\n",
8059 num_reported, num_total);
8060
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008061 for (i = 0; i < num_reported; i++) {
8062 struct i40e_aqc_switch_config_element_resp *ele =
8063 &sw_config->element[i];
8064
8065 i40e_setup_pf_switch_element(pf, ele, num_reported,
8066 printconfig);
8067 }
8068 } while (next_seid != 0);
8069
8070 kfree(aq_buf);
8071 return ret;
8072}
8073
8074/**
8075 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
8076 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008077 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008078 *
8079 * Returns 0 on success, negative value on failure
8080 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008081static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008082{
Jesse Brandeburg895106a2013-11-26 10:49:16 +00008083 u32 rxfc = 0, txfc = 0, rxfc_reg;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008084 int ret;
8085
8086 /* find out what's out there already */
8087 ret = i40e_fetch_switch_configuration(pf, false);
8088 if (ret) {
8089 dev_info(&pf->pdev->dev,
8090 "couldn't fetch switch config, err %d, aq_err %d\n",
8091 ret, pf->hw.aq.asq_last_status);
8092 return ret;
8093 }
8094 i40e_pf_reset_stats(pf);
8095
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008096 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008097 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008098 struct i40e_vsi *vsi = NULL;
8099 u16 uplink_seid;
8100
8101 /* Set up the PF VSI associated with the PF's main VSI
8102 * that is already in the HW switch
8103 */
8104 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
8105 uplink_seid = pf->veb[pf->lan_veb]->seid;
8106 else
8107 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008108 if (pf->lan_vsi == I40E_NO_VSI)
8109 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
8110 else if (reinit)
8111 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008112 if (!vsi) {
8113 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
8114 i40e_fdir_teardown(pf);
8115 return -EAGAIN;
8116 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008117 } else {
8118 /* force a reset of TC and queue layout configurations */
8119 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
8120 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
8121 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
8122 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
8123 }
8124 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
8125
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008126 i40e_fdir_sb_setup(pf);
8127
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008128 /* Setup static PF queue filter control settings */
8129 ret = i40e_setup_pf_filter_control(pf);
8130 if (ret) {
8131 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
8132 ret);
8133 /* Failure here should not stop continuing other steps */
8134 }
8135
8136 /* enable RSS in the HW, even for only one queue, as the stack can use
8137 * the hash
8138 */
8139 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
8140 i40e_config_rss(pf);
8141
8142 /* fill in link information and enable LSE reporting */
8143 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
8144 i40e_link_event(pf);
8145
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008146 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008147 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
8148 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008149 /* requested_mode is set in probe or by ethtool */
8150 if (!pf->fc_autoneg_status)
8151 goto no_autoneg;
8152
8153 if ((pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX) &&
8154 (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008155 pf->hw.fc.current_mode = I40E_FC_FULL;
8156 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX)
8157 pf->hw.fc.current_mode = I40E_FC_TX_PAUSE;
8158 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX)
8159 pf->hw.fc.current_mode = I40E_FC_RX_PAUSE;
8160 else
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008161 pf->hw.fc.current_mode = I40E_FC_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008162
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008163 /* sync the flow control settings with the auto-neg values */
8164 switch (pf->hw.fc.current_mode) {
8165 case I40E_FC_FULL:
8166 txfc = 1;
8167 rxfc = 1;
8168 break;
8169 case I40E_FC_TX_PAUSE:
8170 txfc = 1;
8171 rxfc = 0;
8172 break;
8173 case I40E_FC_RX_PAUSE:
8174 txfc = 0;
8175 rxfc = 1;
8176 break;
8177 case I40E_FC_NONE:
8178 case I40E_FC_DEFAULT:
8179 txfc = 0;
8180 rxfc = 0;
8181 break;
8182 case I40E_FC_PFC:
8183 /* TBD */
8184 break;
8185 /* no default case, we have to handle all possibilities here */
8186 }
8187
8188 wr32(&pf->hw, I40E_PRTDCB_FCCFG, txfc << I40E_PRTDCB_FCCFG_TFCE_SHIFT);
8189
8190 rxfc_reg = rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
8191 ~I40E_PRTDCB_MFLCN_RFCE_MASK;
8192 rxfc_reg |= (rxfc << I40E_PRTDCB_MFLCN_RFCE_SHIFT);
8193
8194 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rxfc_reg);
8195
8196 goto fc_complete;
8197
8198no_autoneg:
8199 /* disable L2 flow control, user can turn it on if they wish */
8200 wr32(&pf->hw, I40E_PRTDCB_FCCFG, 0);
8201 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
8202 ~I40E_PRTDCB_MFLCN_RFCE_MASK);
8203
8204fc_complete:
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008205 i40e_ptp_init(pf);
8206
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008207 return ret;
8208}
8209
8210/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008211 * i40e_determine_queue_usage - Work out queue distribution
8212 * @pf: board private structure
8213 **/
8214static void i40e_determine_queue_usage(struct i40e_pf *pf)
8215{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008216 int queues_left;
8217
8218 pf->num_lan_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008219
8220 /* Find the max queues to be put into basic use. We'll always be
8221 * using TC0, whether or not DCB is running, and TC0 will get the
8222 * big RSS set.
8223 */
8224 queues_left = pf->hw.func_caps.num_tx_qp;
8225
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008226 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +00008227 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008228 /* one qp for PF, no queues for anything else */
8229 queues_left = 0;
8230 pf->rss_size = pf->num_lan_qps = 1;
8231
8232 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008233 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
8234 I40E_FLAG_FD_SB_ENABLED |
8235 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008236 I40E_FLAG_DCB_CAPABLE |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008237 I40E_FLAG_SRIOV_ENABLED |
8238 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +00008239 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
8240 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +00008241 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008242 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +00008243 /* one qp for PF */
8244 pf->rss_size = pf->num_lan_qps = 1;
8245 queues_left -= pf->num_lan_qps;
8246
8247 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
8248 I40E_FLAG_FD_SB_ENABLED |
8249 I40E_FLAG_FD_ATR_ENABLED |
8250 I40E_FLAG_DCB_ENABLED |
8251 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008252 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008253 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008254 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008255 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008256 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008257 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
8258 }
8259 pf->num_lan_qps = pf->rss_size_max;
8260 queues_left -= pf->num_lan_qps;
8261 }
8262
8263 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8264 if (queues_left > 1) {
8265 queues_left -= 1; /* save 1 queue for FD */
8266 } else {
8267 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8268 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
8269 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008270 }
8271
8272 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
8273 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008274 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
8275 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008276 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
8277 }
8278
8279 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
8280 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
8281 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
8282 (queues_left / pf->num_vmdq_qps));
8283 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
8284 }
8285
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008286 pf->queues_left = queues_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008287}
8288
8289/**
8290 * i40e_setup_pf_filter_control - Setup PF static filter control
8291 * @pf: PF to be setup
8292 *
8293 * i40e_setup_pf_filter_control sets up a pf's initial filter control
8294 * settings. If PE/FCoE are enabled then it will also set the per PF
8295 * based filter sizes required for them. It also enables Flow director,
8296 * ethertype and macvlan type filter settings for the pf.
8297 *
8298 * Returns 0 on success, negative on failure
8299 **/
8300static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
8301{
8302 struct i40e_filter_control_settings *settings = &pf->filter_settings;
8303
8304 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
8305
8306 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008307 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008308 settings->enable_fdir = true;
8309
8310 /* Ethtype and MACVLAN filters enabled for PF */
8311 settings->enable_ethtype = true;
8312 settings->enable_macvlan = true;
8313
8314 if (i40e_set_filter_control(&pf->hw, settings))
8315 return -ENOENT;
8316
8317 return 0;
8318}
8319
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008320#define INFO_STRING_LEN 255
8321static void i40e_print_features(struct i40e_pf *pf)
8322{
8323 struct i40e_hw *hw = &pf->hw;
8324 char *buf, *string;
8325
8326 string = kzalloc(INFO_STRING_LEN, GFP_KERNEL);
8327 if (!string) {
8328 dev_err(&pf->pdev->dev, "Features string allocation failed\n");
8329 return;
8330 }
8331
8332 buf = string;
8333
8334 buf += sprintf(string, "Features: PF-id[%d] ", hw->pf_id);
8335#ifdef CONFIG_PCI_IOV
8336 buf += sprintf(buf, "VFs: %d ", pf->num_req_vfs);
8337#endif
8338 buf += sprintf(buf, "VSIs: %d QP: %d ", pf->hw.func_caps.num_vsis,
8339 pf->vsi[pf->lan_vsi]->num_queue_pairs);
8340
8341 if (pf->flags & I40E_FLAG_RSS_ENABLED)
8342 buf += sprintf(buf, "RSS ");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008343 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +00008344 buf += sprintf(buf, "FD_ATR ");
8345 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8346 buf += sprintf(buf, "FD_SB ");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008347 buf += sprintf(buf, "NTUPLE ");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +00008348 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008349 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008350 buf += sprintf(buf, "DCB ");
8351 if (pf->flags & I40E_FLAG_PTP)
8352 buf += sprintf(buf, "PTP ");
8353
8354 BUG_ON(buf > (string + INFO_STRING_LEN));
8355 dev_info(&pf->pdev->dev, "%s\n", string);
8356 kfree(string);
8357}
8358
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008359/**
8360 * i40e_probe - Device initialization routine
8361 * @pdev: PCI device information struct
8362 * @ent: entry in i40e_pci_tbl
8363 *
8364 * i40e_probe initializes a pf identified by a pci_dev structure.
8365 * The OS initialization, configuring of the pf private structure,
8366 * and a hardware reset occur.
8367 *
8368 * Returns 0 on success, negative on failure
8369 **/
8370static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
8371{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008372 struct i40e_pf *pf;
8373 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008374 static u16 pfs_found;
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008375 u16 link_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008376 int err = 0;
8377 u32 len;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008378 u32 i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008379
8380 err = pci_enable_device_mem(pdev);
8381 if (err)
8382 return err;
8383
8384 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +00008385 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00008386 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00008387 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
8388 if (err) {
8389 dev_err(&pdev->dev,
8390 "DMA configuration failed: 0x%x\n", err);
8391 goto err_dma;
8392 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008393 }
8394
8395 /* set up pci connections */
8396 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
8397 IORESOURCE_MEM), i40e_driver_name);
8398 if (err) {
8399 dev_info(&pdev->dev,
8400 "pci_request_selected_regions failed %d\n", err);
8401 goto err_pci_reg;
8402 }
8403
8404 pci_enable_pcie_error_reporting(pdev);
8405 pci_set_master(pdev);
8406
8407 /* Now that we have a PCI connection, we need to do the
8408 * low level device setup. This is primarily setting up
8409 * the Admin Queue structures and then querying for the
8410 * device's current profile information.
8411 */
8412 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
8413 if (!pf) {
8414 err = -ENOMEM;
8415 goto err_pf_alloc;
8416 }
8417 pf->next_vsi = 0;
8418 pf->pdev = pdev;
8419 set_bit(__I40E_DOWN, &pf->state);
8420
8421 hw = &pf->hw;
8422 hw->back = pf;
8423 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
8424 pci_resource_len(pdev, 0));
8425 if (!hw->hw_addr) {
8426 err = -EIO;
8427 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
8428 (unsigned int)pci_resource_start(pdev, 0),
8429 (unsigned int)pci_resource_len(pdev, 0), err);
8430 goto err_ioremap;
8431 }
8432 hw->vendor_id = pdev->vendor;
8433 hw->device_id = pdev->device;
8434 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
8435 hw->subsystem_vendor_id = pdev->subsystem_vendor;
8436 hw->subsystem_device_id = pdev->subsystem_device;
8437 hw->bus.device = PCI_SLOT(pdev->devfn);
8438 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008439 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008440
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008441 /* do a special CORER for clearing PXE mode once at init */
8442 if (hw->revision_id == 0 &&
8443 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
8444 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
8445 i40e_flush(hw);
8446 msleep(200);
8447 pf->corer_count++;
8448
8449 i40e_clear_pxe_mode(hw);
8450 }
8451
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008452 /* Reset here to make sure all is clean and to define PF 'n' */
8453 err = i40e_pf_reset(hw);
8454 if (err) {
8455 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
8456 goto err_pf_reset;
8457 }
8458 pf->pfr_count++;
8459
8460 hw->aq.num_arq_entries = I40E_AQ_LEN;
8461 hw->aq.num_asq_entries = I40E_AQ_LEN;
8462 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
8463 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
8464 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
8465 snprintf(pf->misc_int_name, sizeof(pf->misc_int_name) - 1,
8466 "%s-pf%d:misc",
8467 dev_driver_string(&pf->pdev->dev), pf->hw.pf_id);
8468
8469 err = i40e_init_shared_code(hw);
8470 if (err) {
8471 dev_info(&pdev->dev, "init_shared_code failed: %d\n", err);
8472 goto err_pf_reset;
8473 }
8474
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008475 /* set up a default setting for link flow control */
8476 pf->hw.fc.requested_mode = I40E_FC_NONE;
8477
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008478 err = i40e_init_adminq(hw);
8479 dev_info(&pdev->dev, "%s\n", i40e_fw_version_str(hw));
8480 if (err) {
8481 dev_info(&pdev->dev,
8482 "init_adminq failed: %d expecting API %02x.%02x\n",
8483 err,
8484 I40E_FW_API_VERSION_MAJOR, I40E_FW_API_VERSION_MINOR);
8485 goto err_pf_reset;
8486 }
8487
Shannon Nelson4eb3f762014-03-06 08:59:58 +00008488 i40e_verify_eeprom(pf);
8489
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +00008490 /* Rev 0 hardware was never productized */
8491 if (hw->revision_id < 1)
8492 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");
8493
Shannon Nelson6ff4ef82013-12-21 05:44:49 +00008494 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008495 err = i40e_get_capabilities(pf);
8496 if (err)
8497 goto err_adminq_setup;
8498
8499 err = i40e_sw_init(pf);
8500 if (err) {
8501 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
8502 goto err_sw_init;
8503 }
8504
8505 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
8506 hw->func_caps.num_rx_qp,
8507 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
8508 if (err) {
8509 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
8510 goto err_init_lan_hmc;
8511 }
8512
8513 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
8514 if (err) {
8515 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
8516 err = -ENOENT;
8517 goto err_configure_lan_hmc;
8518 }
8519
8520 i40e_get_mac_addr(hw, hw->mac.addr);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +00008521 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008522 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
8523 err = -EIO;
8524 goto err_mac_addr;
8525 }
8526 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
8527 memcpy(hw->mac.perm_addr, hw->mac.addr, ETH_ALEN);
8528
8529 pci_set_drvdata(pdev, pf);
8530 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008531#ifdef CONFIG_I40E_DCB
8532 err = i40e_init_pf_dcb(pf);
8533 if (err) {
8534 dev_info(&pdev->dev, "init_pf_dcb failed: %d\n", err);
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008535 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
Neerav Parikh014269f2014-04-01 07:11:48 +00008536 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008537 }
8538#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008539
8540 /* set up periodic task facility */
8541 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
8542 pf->service_timer_period = HZ;
8543
8544 INIT_WORK(&pf->service_task, i40e_service_task);
8545 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
8546 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
8547 pf->link_check_timeout = jiffies;
8548
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008549 /* WoL defaults to disabled */
8550 pf->wol_en = false;
8551 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
8552
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008553 /* set up the main switch operations */
8554 i40e_determine_queue_usage(pf);
8555 i40e_init_interrupt_scheme(pf);
8556
Mitch Williams505682c2014-05-20 08:01:37 +00008557 /* The number of VSIs reported by the FW is the minimum guaranteed
8558 * to us; HW supports far more and we share the remaining pool with
8559 * the other PFs. We allocate space for more than the guarantee with
8560 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008561 */
Mitch Williams505682c2014-05-20 08:01:37 +00008562 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
8563 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
8564 else
8565 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
8566
8567 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
8568 len = sizeof(struct i40e_vsi *) * pf->num_alloc_vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008569 pf->vsi = kzalloc(len, GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +00008570 if (!pf->vsi) {
8571 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008572 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +00008573 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008574
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008575 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008576 if (err) {
8577 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
8578 goto err_vsis;
8579 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008580 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +00008581 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008582 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
8583 i40e_vsi_open(pf->vsi[i]);
8584 break;
8585 }
8586 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008587
8588 /* The main driver is (mostly) up and happy. We need to set this state
8589 * before setting up the misc vector or we get a race and the vector
8590 * ends up disabled forever.
8591 */
8592 clear_bit(__I40E_DOWN, &pf->state);
8593
8594 /* In case of MSIX we are going to setup the misc vector right here
8595 * to handle admin queue events etc. In case of legacy and MSI
8596 * the misc functionality and queue processing is combined in
8597 * the same vector and that gets setup at open.
8598 */
8599 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
8600 err = i40e_setup_misc_vector(pf);
8601 if (err) {
8602 dev_info(&pdev->dev,
8603 "setup of misc vector failed: %d\n", err);
8604 goto err_vsis;
8605 }
8606 }
8607
Greg Rosedf805f62014-04-04 04:43:16 +00008608#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008609 /* prep for VF support */
8610 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +00008611 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
8612 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008613 u32 val;
8614
8615 /* disable link interrupts for VFs */
8616 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
8617 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
8618 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
8619 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -08008620
8621 if (pci_num_vf(pdev)) {
8622 dev_info(&pdev->dev,
8623 "Active VFs found, allocating resources.\n");
8624 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
8625 if (err)
8626 dev_info(&pdev->dev,
8627 "Error %d allocating resources for existing VFs\n",
8628 err);
8629 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008630 }
Greg Rosedf805f62014-04-04 04:43:16 +00008631#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008632
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008633 pfs_found++;
8634
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008635 i40e_dbg_pf_init(pf);
8636
8637 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00008638 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008639
8640 /* since everything's happy, start the service_task timer */
8641 mod_timer(&pf->service_timer,
8642 round_jiffies(jiffies + pf->service_timer_period));
8643
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008644 /* Get the negotiated link width and speed from PCI config space */
8645 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA, &link_status);
8646
8647 i40e_set_pci_config_data(hw, link_status);
8648
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00008649 dev_info(&pdev->dev, "PCI-Express: %s %s\n",
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008650 (hw->bus.speed == i40e_bus_speed_8000 ? "Speed 8.0GT/s" :
8651 hw->bus.speed == i40e_bus_speed_5000 ? "Speed 5.0GT/s" :
8652 hw->bus.speed == i40e_bus_speed_2500 ? "Speed 2.5GT/s" :
8653 "Unknown"),
8654 (hw->bus.width == i40e_bus_width_pcie_x8 ? "Width x8" :
8655 hw->bus.width == i40e_bus_width_pcie_x4 ? "Width x4" :
8656 hw->bus.width == i40e_bus_width_pcie_x2 ? "Width x2" :
8657 hw->bus.width == i40e_bus_width_pcie_x1 ? "Width x1" :
8658 "Unknown"));
8659
8660 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
8661 hw->bus.speed < i40e_bus_speed_8000) {
8662 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
8663 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
8664 }
8665
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008666 /* print a string summarizing features */
8667 i40e_print_features(pf);
8668
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008669 return 0;
8670
8671 /* Unwind what we've done if something failed in the setup */
8672err_vsis:
8673 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008674 i40e_clear_interrupt_scheme(pf);
8675 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +00008676err_switch_setup:
8677 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008678 del_timer_sync(&pf->service_timer);
8679err_mac_addr:
8680err_configure_lan_hmc:
8681 (void)i40e_shutdown_lan_hmc(hw);
8682err_init_lan_hmc:
8683 kfree(pf->qp_pile);
8684 kfree(pf->irq_pile);
8685err_sw_init:
8686err_adminq_setup:
8687 (void)i40e_shutdown_adminq(hw);
8688err_pf_reset:
8689 iounmap(hw->hw_addr);
8690err_ioremap:
8691 kfree(pf);
8692err_pf_alloc:
8693 pci_disable_pcie_error_reporting(pdev);
8694 pci_release_selected_regions(pdev,
8695 pci_select_bars(pdev, IORESOURCE_MEM));
8696err_pci_reg:
8697err_dma:
8698 pci_disable_device(pdev);
8699 return err;
8700}
8701
8702/**
8703 * i40e_remove - Device removal routine
8704 * @pdev: PCI device information struct
8705 *
8706 * i40e_remove is called by the PCI subsystem to alert the driver
8707 * that is should release a PCI device. This could be caused by a
8708 * Hot-Plug event, or because the driver is going to be removed from
8709 * memory.
8710 **/
8711static void i40e_remove(struct pci_dev *pdev)
8712{
8713 struct i40e_pf *pf = pci_get_drvdata(pdev);
8714 i40e_status ret_code;
8715 u32 reg;
8716 int i;
8717
8718 i40e_dbg_pf_exit(pf);
8719
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008720 i40e_ptp_stop(pf);
8721
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008722 /* no more scheduling of any task */
8723 set_bit(__I40E_DOWN, &pf->state);
8724 del_timer_sync(&pf->service_timer);
8725 cancel_work_sync(&pf->service_task);
8726
Mitch Williamseb2d80b2014-02-13 03:48:48 -08008727 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
8728 i40e_free_vfs(pf);
8729 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
8730 }
8731
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008732 i40e_fdir_teardown(pf);
8733
8734 /* If there is a switch structure or any orphans, remove them.
8735 * This will leave only the PF's VSI remaining.
8736 */
8737 for (i = 0; i < I40E_MAX_VEB; i++) {
8738 if (!pf->veb[i])
8739 continue;
8740
8741 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
8742 pf->veb[i]->uplink_seid == 0)
8743 i40e_switch_branch_release(pf->veb[i]);
8744 }
8745
8746 /* Now we can shutdown the PF's VSI, just before we kill
8747 * adminq and hmc.
8748 */
8749 if (pf->vsi[pf->lan_vsi])
8750 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
8751
8752 i40e_stop_misc_vector(pf);
8753 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
8754 synchronize_irq(pf->msix_entries[0].vector);
8755 free_irq(pf->msix_entries[0].vector, pf);
8756 }
8757
8758 /* shutdown and destroy the HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00008759 if (pf->hw.hmc.hmc_obj) {
8760 ret_code = i40e_shutdown_lan_hmc(&pf->hw);
8761 if (ret_code)
8762 dev_warn(&pdev->dev,
8763 "Failed to destroy the HMC resources: %d\n",
8764 ret_code);
8765 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008766
8767 /* shutdown the adminq */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008768 ret_code = i40e_shutdown_adminq(&pf->hw);
8769 if (ret_code)
8770 dev_warn(&pdev->dev,
8771 "Failed to destroy the Admin Queue resources: %d\n",
8772 ret_code);
8773
8774 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
8775 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +00008776 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008777 if (pf->vsi[i]) {
8778 i40e_vsi_clear_rings(pf->vsi[i]);
8779 i40e_vsi_clear(pf->vsi[i]);
8780 pf->vsi[i] = NULL;
8781 }
8782 }
8783
8784 for (i = 0; i < I40E_MAX_VEB; i++) {
8785 kfree(pf->veb[i]);
8786 pf->veb[i] = NULL;
8787 }
8788
8789 kfree(pf->qp_pile);
8790 kfree(pf->irq_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008791 kfree(pf->vsi);
8792
8793 /* force a PF reset to clean anything leftover */
8794 reg = rd32(&pf->hw, I40E_PFGEN_CTRL);
8795 wr32(&pf->hw, I40E_PFGEN_CTRL, (reg | I40E_PFGEN_CTRL_PFSWR_MASK));
8796 i40e_flush(&pf->hw);
8797
8798 iounmap(pf->hw.hw_addr);
8799 kfree(pf);
8800 pci_release_selected_regions(pdev,
8801 pci_select_bars(pdev, IORESOURCE_MEM));
8802
8803 pci_disable_pcie_error_reporting(pdev);
8804 pci_disable_device(pdev);
8805}
8806
8807/**
8808 * i40e_pci_error_detected - warning that something funky happened in PCI land
8809 * @pdev: PCI device information struct
8810 *
8811 * Called to warn that something happened and the error handling steps
8812 * are in progress. Allows the driver to quiesce things, be ready for
8813 * remediation.
8814 **/
8815static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
8816 enum pci_channel_state error)
8817{
8818 struct i40e_pf *pf = pci_get_drvdata(pdev);
8819
8820 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
8821
8822 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008823 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
8824 rtnl_lock();
8825 i40e_prep_for_reset(pf);
8826 rtnl_unlock();
8827 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008828
8829 /* Request a slot reset */
8830 return PCI_ERS_RESULT_NEED_RESET;
8831}
8832
8833/**
8834 * i40e_pci_error_slot_reset - a PCI slot reset just happened
8835 * @pdev: PCI device information struct
8836 *
8837 * Called to find if the driver can work with the device now that
8838 * the pci slot has been reset. If a basic connection seems good
8839 * (registers are readable and have sane content) then return a
8840 * happy little PCI_ERS_RESULT_xxx.
8841 **/
8842static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
8843{
8844 struct i40e_pf *pf = pci_get_drvdata(pdev);
8845 pci_ers_result_t result;
8846 int err;
8847 u32 reg;
8848
8849 dev_info(&pdev->dev, "%s\n", __func__);
8850 if (pci_enable_device_mem(pdev)) {
8851 dev_info(&pdev->dev,
8852 "Cannot re-enable PCI device after reset.\n");
8853 result = PCI_ERS_RESULT_DISCONNECT;
8854 } else {
8855 pci_set_master(pdev);
8856 pci_restore_state(pdev);
8857 pci_save_state(pdev);
8858 pci_wake_from_d3(pdev, false);
8859
8860 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8861 if (reg == 0)
8862 result = PCI_ERS_RESULT_RECOVERED;
8863 else
8864 result = PCI_ERS_RESULT_DISCONNECT;
8865 }
8866
8867 err = pci_cleanup_aer_uncorrect_error_status(pdev);
8868 if (err) {
8869 dev_info(&pdev->dev,
8870 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
8871 err);
8872 /* non-fatal, continue */
8873 }
8874
8875 return result;
8876}
8877
8878/**
8879 * i40e_pci_error_resume - restart operations after PCI error recovery
8880 * @pdev: PCI device information struct
8881 *
8882 * Called to allow the driver to bring things back up after PCI error
8883 * and/or reset recovery has finished.
8884 **/
8885static void i40e_pci_error_resume(struct pci_dev *pdev)
8886{
8887 struct i40e_pf *pf = pci_get_drvdata(pdev);
8888
8889 dev_info(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008890 if (test_bit(__I40E_SUSPENDED, &pf->state))
8891 return;
8892
8893 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008894 i40e_handle_reset_warning(pf);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008895 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008896}
8897
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008898/**
8899 * i40e_shutdown - PCI callback for shutting down
8900 * @pdev: PCI device information struct
8901 **/
8902static void i40e_shutdown(struct pci_dev *pdev)
8903{
8904 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008905 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008906
8907 set_bit(__I40E_SUSPENDED, &pf->state);
8908 set_bit(__I40E_DOWN, &pf->state);
8909 rtnl_lock();
8910 i40e_prep_for_reset(pf);
8911 rtnl_unlock();
8912
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008913 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
8914 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
8915
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008916 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008917 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008918 pci_set_power_state(pdev, PCI_D3hot);
8919 }
8920}
8921
8922#ifdef CONFIG_PM
8923/**
8924 * i40e_suspend - PCI callback for moving to D3
8925 * @pdev: PCI device information struct
8926 **/
8927static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
8928{
8929 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008930 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008931
8932 set_bit(__I40E_SUSPENDED, &pf->state);
8933 set_bit(__I40E_DOWN, &pf->state);
8934 rtnl_lock();
8935 i40e_prep_for_reset(pf);
8936 rtnl_unlock();
8937
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008938 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
8939 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
8940
8941 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008942 pci_set_power_state(pdev, PCI_D3hot);
8943
8944 return 0;
8945}
8946
8947/**
8948 * i40e_resume - PCI callback for waking up from D3
8949 * @pdev: PCI device information struct
8950 **/
8951static int i40e_resume(struct pci_dev *pdev)
8952{
8953 struct i40e_pf *pf = pci_get_drvdata(pdev);
8954 u32 err;
8955
8956 pci_set_power_state(pdev, PCI_D0);
8957 pci_restore_state(pdev);
8958 /* pci_restore_state() clears dev->state_saves, so
8959 * call pci_save_state() again to restore it.
8960 */
8961 pci_save_state(pdev);
8962
8963 err = pci_enable_device_mem(pdev);
8964 if (err) {
8965 dev_err(&pdev->dev,
8966 "%s: Cannot enable PCI device from suspend\n",
8967 __func__);
8968 return err;
8969 }
8970 pci_set_master(pdev);
8971
8972 /* no wakeup events while running */
8973 pci_wake_from_d3(pdev, false);
8974
8975 /* handling the reset will rebuild the device state */
8976 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
8977 clear_bit(__I40E_DOWN, &pf->state);
8978 rtnl_lock();
8979 i40e_reset_and_rebuild(pf, false);
8980 rtnl_unlock();
8981 }
8982
8983 return 0;
8984}
8985
8986#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008987static const struct pci_error_handlers i40e_err_handler = {
8988 .error_detected = i40e_pci_error_detected,
8989 .slot_reset = i40e_pci_error_slot_reset,
8990 .resume = i40e_pci_error_resume,
8991};
8992
8993static struct pci_driver i40e_driver = {
8994 .name = i40e_driver_name,
8995 .id_table = i40e_pci_tbl,
8996 .probe = i40e_probe,
8997 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008998#ifdef CONFIG_PM
8999 .suspend = i40e_suspend,
9000 .resume = i40e_resume,
9001#endif
9002 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009003 .err_handler = &i40e_err_handler,
9004 .sriov_configure = i40e_pci_sriov_configure,
9005};
9006
9007/**
9008 * i40e_init_module - Driver registration routine
9009 *
9010 * i40e_init_module is the first routine called when the driver is
9011 * loaded. All it does is register with the PCI subsystem.
9012 **/
9013static int __init i40e_init_module(void)
9014{
9015 pr_info("%s: %s - version %s\n", i40e_driver_name,
9016 i40e_driver_string, i40e_driver_version_str);
9017 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
9018 i40e_dbg_init();
9019 return pci_register_driver(&i40e_driver);
9020}
9021module_init(i40e_init_module);
9022
9023/**
9024 * i40e_exit_module - Driver exit cleanup routine
9025 *
9026 * i40e_exit_module is called just before the driver is removed
9027 * from memory.
9028 **/
9029static void __exit i40e_exit_module(void)
9030{
9031 pci_unregister_driver(&i40e_driver);
9032 i40e_dbg_exit();
9033}
9034module_exit(i40e_exit_module);