blob: e3916d00d403a9e102186b016f27ca717504564e [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
Greg Rose9a173902014-05-22 06:32:02 +00001205 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001206 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
Greg Rose9a173902014-05-22 06:32:02 +00001329 ether_addr_copy(vsi->back->hw.mac.addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001330 }
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
Greg Rose9a173902014-05-22 06:32:02 +00001343 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001344
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 */
Greg Rose9a173902014-05-22 06:32:02 +00001600 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001601 del_list[num_del].vlan_tag =
1602 cpu_to_le16((u16)(f->vlan ==
1603 I40E_VLAN_ANY ? 0 : f->vlan));
1604
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001605 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1606 del_list[num_del].flags = cmd_flags;
1607 num_del++;
1608
1609 /* unlink from filter list */
1610 list_del(&f->list);
1611 kfree(f);
1612
1613 /* flush a full buffer */
1614 if (num_del == filter_list_len) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001615 aq_ret = i40e_aq_remove_macvlan(&pf->hw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001616 vsi->seid, del_list, num_del,
1617 NULL);
1618 num_del = 0;
1619 memset(del_list, 0, sizeof(*del_list));
1620
Shannon Nelsonfdfe9cb2014-05-20 08:01:39 +00001621 if (aq_ret &&
1622 pf->hw.aq.asq_last_status !=
1623 I40E_AQ_RC_ENOENT)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001624 dev_info(&pf->pdev->dev,
1625 "ignoring delete macvlan error, err %d, aq_err %d while flushing a full buffer\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001626 aq_ret,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001627 pf->hw.aq.asq_last_status);
1628 }
1629 }
1630 if (num_del) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001631 aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001632 del_list, num_del, NULL);
1633 num_del = 0;
1634
Shannon Nelsonfdfe9cb2014-05-20 08:01:39 +00001635 if (aq_ret &&
1636 pf->hw.aq.asq_last_status != I40E_AQ_RC_ENOENT)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001637 dev_info(&pf->pdev->dev,
1638 "ignoring delete macvlan error, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001639 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001640 }
1641
1642 kfree(del_list);
1643 del_list = NULL;
1644
1645 /* do all the adds now */
1646 filter_list_len = pf->hw.aq.asq_buf_size /
1647 sizeof(struct i40e_aqc_add_macvlan_element_data),
1648 add_list = kcalloc(filter_list_len,
1649 sizeof(struct i40e_aqc_add_macvlan_element_data),
1650 GFP_KERNEL);
1651 if (!add_list)
1652 return -ENOMEM;
1653
1654 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1655 if (!f->changed)
1656 continue;
1657
1658 if (f->counter == 0)
1659 continue;
1660 f->changed = false;
1661 add_happened = true;
1662 cmd_flags = 0;
1663
1664 /* add to add array */
Greg Rose9a173902014-05-22 06:32:02 +00001665 ether_addr_copy(add_list[num_add].mac_addr, f->macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001666 add_list[num_add].vlan_tag =
1667 cpu_to_le16(
1668 (u16)(f->vlan == I40E_VLAN_ANY ? 0 : f->vlan));
1669 add_list[num_add].queue_number = 0;
1670
1671 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001672 add_list[num_add].flags = cpu_to_le16(cmd_flags);
1673 num_add++;
1674
1675 /* flush a full buffer */
1676 if (num_add == filter_list_len) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001677 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1678 add_list, num_add,
1679 NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001680 num_add = 0;
1681
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001682 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001683 break;
1684 memset(add_list, 0, sizeof(*add_list));
1685 }
1686 }
1687 if (num_add) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001688 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1689 add_list, num_add, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001690 num_add = 0;
1691 }
1692 kfree(add_list);
1693 add_list = NULL;
1694
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001695 if (add_happened && (!aq_ret)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001696 /* do nothing */;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001697 } else if (add_happened && (aq_ret)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001698 dev_info(&pf->pdev->dev,
1699 "add filter failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001700 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001701 if ((pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOSPC) &&
1702 !test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1703 &vsi->state)) {
1704 promisc_forced_on = true;
1705 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1706 &vsi->state);
1707 dev_info(&pf->pdev->dev, "promiscuous mode forced on\n");
1708 }
1709 }
1710 }
1711
1712 /* check for changes in promiscuous modes */
1713 if (changed_flags & IFF_ALLMULTI) {
1714 bool cur_multipromisc;
1715 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001716 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
1717 vsi->seid,
1718 cur_multipromisc,
1719 NULL);
1720 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001721 dev_info(&pf->pdev->dev,
1722 "set multi promisc failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001723 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001724 }
1725 if ((changed_flags & IFF_PROMISC) || promisc_forced_on) {
1726 bool cur_promisc;
1727 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
1728 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1729 &vsi->state));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001730 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(&vsi->back->hw,
1731 vsi->seid,
1732 cur_promisc, NULL);
1733 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001734 dev_info(&pf->pdev->dev,
1735 "set uni promisc failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001736 aq_ret, pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00001737 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
1738 vsi->seid,
1739 cur_promisc, NULL);
1740 if (aq_ret)
1741 dev_info(&pf->pdev->dev,
1742 "set brdcast promisc failed, err %d, aq_err %d\n",
1743 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001744 }
1745
1746 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
1747 return 0;
1748}
1749
1750/**
1751 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
1752 * @pf: board private structure
1753 **/
1754static void i40e_sync_filters_subtask(struct i40e_pf *pf)
1755{
1756 int v;
1757
1758 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
1759 return;
1760 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
1761
Mitch Williams505682c2014-05-20 08:01:37 +00001762 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001763 if (pf->vsi[v] &&
1764 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED))
1765 i40e_sync_vsi_filters(pf->vsi[v]);
1766 }
1767}
1768
1769/**
1770 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
1771 * @netdev: network interface device structure
1772 * @new_mtu: new value for maximum frame size
1773 *
1774 * Returns 0 on success, negative on failure
1775 **/
1776static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
1777{
1778 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg61a46a42014-04-23 04:50:05 +00001779 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001780 struct i40e_vsi *vsi = np->vsi;
1781
1782 /* MTU < 68 is an error and causes problems on some kernels */
1783 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
1784 return -EINVAL;
1785
1786 netdev_info(netdev, "changing MTU from %d to %d\n",
1787 netdev->mtu, new_mtu);
1788 netdev->mtu = new_mtu;
1789 if (netif_running(netdev))
1790 i40e_vsi_reinit_locked(vsi);
1791
1792 return 0;
1793}
1794
1795/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00001796 * i40e_ioctl - Access the hwtstamp interface
1797 * @netdev: network interface device structure
1798 * @ifr: interface request data
1799 * @cmd: ioctl command
1800 **/
1801int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1802{
1803 struct i40e_netdev_priv *np = netdev_priv(netdev);
1804 struct i40e_pf *pf = np->vsi->back;
1805
1806 switch (cmd) {
1807 case SIOCGHWTSTAMP:
1808 return i40e_ptp_get_ts_config(pf, ifr);
1809 case SIOCSHWTSTAMP:
1810 return i40e_ptp_set_ts_config(pf, ifr);
1811 default:
1812 return -EOPNOTSUPP;
1813 }
1814}
1815
1816/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001817 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
1818 * @vsi: the vsi being adjusted
1819 **/
1820void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
1821{
1822 struct i40e_vsi_context ctxt;
1823 i40e_status ret;
1824
1825 if ((vsi->info.valid_sections &
1826 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1827 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
1828 return; /* already enabled */
1829
1830 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1831 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1832 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
1833
1834 ctxt.seid = vsi->seid;
1835 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1836 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1837 if (ret) {
1838 dev_info(&vsi->back->pdev->dev,
1839 "%s: update vsi failed, aq_err=%d\n",
1840 __func__, vsi->back->hw.aq.asq_last_status);
1841 }
1842}
1843
1844/**
1845 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
1846 * @vsi: the vsi being adjusted
1847 **/
1848void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
1849{
1850 struct i40e_vsi_context ctxt;
1851 i40e_status ret;
1852
1853 if ((vsi->info.valid_sections &
1854 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1855 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
1856 I40E_AQ_VSI_PVLAN_EMOD_MASK))
1857 return; /* already disabled */
1858
1859 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1860 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1861 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
1862
1863 ctxt.seid = vsi->seid;
1864 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1865 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1866 if (ret) {
1867 dev_info(&vsi->back->pdev->dev,
1868 "%s: update vsi failed, aq_err=%d\n",
1869 __func__, vsi->back->hw.aq.asq_last_status);
1870 }
1871}
1872
1873/**
1874 * i40e_vlan_rx_register - Setup or shutdown vlan offload
1875 * @netdev: network interface to be adjusted
1876 * @features: netdev features to test if VLAN offload is enabled or not
1877 **/
1878static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
1879{
1880 struct i40e_netdev_priv *np = netdev_priv(netdev);
1881 struct i40e_vsi *vsi = np->vsi;
1882
1883 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1884 i40e_vlan_stripping_enable(vsi);
1885 else
1886 i40e_vlan_stripping_disable(vsi);
1887}
1888
1889/**
1890 * i40e_vsi_add_vlan - Add vsi membership for given vlan
1891 * @vsi: the vsi being configured
1892 * @vid: vlan id to be added (0 = untagged only , -1 = any)
1893 **/
1894int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
1895{
1896 struct i40e_mac_filter *f, *add_f;
1897 bool is_netdev, is_vf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001898
1899 is_vf = (vsi->type == I40E_VSI_SRIOV);
1900 is_netdev = !!(vsi->netdev);
1901
1902 if (is_netdev) {
1903 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid,
1904 is_vf, is_netdev);
1905 if (!add_f) {
1906 dev_info(&vsi->back->pdev->dev,
1907 "Could not add vlan filter %d for %pM\n",
1908 vid, vsi->netdev->dev_addr);
1909 return -ENOMEM;
1910 }
1911 }
1912
1913 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1914 add_f = i40e_add_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
1915 if (!add_f) {
1916 dev_info(&vsi->back->pdev->dev,
1917 "Could not add vlan filter %d for %pM\n",
1918 vid, f->macaddr);
1919 return -ENOMEM;
1920 }
1921 }
1922
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001923 /* Now if we add a vlan tag, make sure to check if it is the first
1924 * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
1925 * with 0, so we now accept untagged and specified tagged traffic
1926 * (and not any taged and untagged)
1927 */
1928 if (vid > 0) {
1929 if (is_netdev && i40e_find_filter(vsi, vsi->netdev->dev_addr,
1930 I40E_VLAN_ANY,
1931 is_vf, is_netdev)) {
1932 i40e_del_filter(vsi, vsi->netdev->dev_addr,
1933 I40E_VLAN_ANY, is_vf, is_netdev);
1934 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0,
1935 is_vf, is_netdev);
1936 if (!add_f) {
1937 dev_info(&vsi->back->pdev->dev,
1938 "Could not add filter 0 for %pM\n",
1939 vsi->netdev->dev_addr);
1940 return -ENOMEM;
1941 }
1942 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08001943 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001944
Greg Rose8d82a7c2014-01-13 16:13:04 -08001945 /* Do not assume that I40E_VLAN_ANY should be reset to VLAN 0 */
1946 if (vid > 0 && !vsi->info.pvid) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001947 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1948 if (i40e_find_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1949 is_vf, is_netdev)) {
1950 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1951 is_vf, is_netdev);
1952 add_f = i40e_add_filter(vsi, f->macaddr,
1953 0, is_vf, is_netdev);
1954 if (!add_f) {
1955 dev_info(&vsi->back->pdev->dev,
1956 "Could not add filter 0 for %pM\n",
1957 f->macaddr);
1958 return -ENOMEM;
1959 }
1960 }
1961 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001962 }
1963
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001964 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1965 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1966 return 0;
1967
1968 return i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001969}
1970
1971/**
1972 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
1973 * @vsi: the vsi being configured
1974 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001975 *
1976 * Return: 0 on success or negative otherwise
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001977 **/
1978int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
1979{
1980 struct net_device *netdev = vsi->netdev;
1981 struct i40e_mac_filter *f, *add_f;
1982 bool is_vf, is_netdev;
1983 int filter_count = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001984
1985 is_vf = (vsi->type == I40E_VSI_SRIOV);
1986 is_netdev = !!(netdev);
1987
1988 if (is_netdev)
1989 i40e_del_filter(vsi, netdev->dev_addr, vid, is_vf, is_netdev);
1990
1991 list_for_each_entry(f, &vsi->mac_filter_list, list)
1992 i40e_del_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
1993
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001994 /* go through all the filters for this VSI and if there is only
1995 * vid == 0 it means there are no other filters, so vid 0 must
1996 * be replaced with -1. This signifies that we should from now
1997 * on accept any traffic (with any tag present, or untagged)
1998 */
1999 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2000 if (is_netdev) {
2001 if (f->vlan &&
2002 ether_addr_equal(netdev->dev_addr, f->macaddr))
2003 filter_count++;
2004 }
2005
2006 if (f->vlan)
2007 filter_count++;
2008 }
2009
2010 if (!filter_count && is_netdev) {
2011 i40e_del_filter(vsi, netdev->dev_addr, 0, is_vf, is_netdev);
2012 f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
2013 is_vf, is_netdev);
2014 if (!f) {
2015 dev_info(&vsi->back->pdev->dev,
2016 "Could not add filter %d for %pM\n",
2017 I40E_VLAN_ANY, netdev->dev_addr);
2018 return -ENOMEM;
2019 }
2020 }
2021
2022 if (!filter_count) {
2023 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2024 i40e_del_filter(vsi, f->macaddr, 0, is_vf, is_netdev);
2025 add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2026 is_vf, is_netdev);
2027 if (!add_f) {
2028 dev_info(&vsi->back->pdev->dev,
2029 "Could not add filter %d for %pM\n",
2030 I40E_VLAN_ANY, f->macaddr);
2031 return -ENOMEM;
2032 }
2033 }
2034 }
2035
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00002036 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
2037 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
2038 return 0;
2039
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002040 return i40e_sync_vsi_filters(vsi);
2041}
2042
2043/**
2044 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2045 * @netdev: network interface to be adjusted
2046 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002047 *
2048 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002049 **/
2050static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2051 __always_unused __be16 proto, u16 vid)
2052{
2053 struct i40e_netdev_priv *np = netdev_priv(netdev);
2054 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002055 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002056
2057 if (vid > 4095)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002058 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002059
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002060 netdev_info(netdev, "adding %pM vid=%d\n", netdev->dev_addr, vid);
2061
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002062 /* If the network stack called us with vid = 0 then
2063 * it is asking to receive priority tagged packets with
2064 * vlan id 0. Our HW receives them by default when configured
2065 * to receive untagged packets so there is no need to add an
2066 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002067 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002068 if (vid)
2069 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002070
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002071 if (!ret && (vid < VLAN_N_VID))
2072 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002073
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002074 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002075}
2076
2077/**
2078 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2079 * @netdev: network interface to be adjusted
2080 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002081 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002082 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002083 **/
2084static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2085 __always_unused __be16 proto, u16 vid)
2086{
2087 struct i40e_netdev_priv *np = netdev_priv(netdev);
2088 struct i40e_vsi *vsi = np->vsi;
2089
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002090 netdev_info(netdev, "removing %pM vid=%d\n", netdev->dev_addr, vid);
2091
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002092 /* return code is ignored as there is nothing a user
2093 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002094 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002095 */
2096 i40e_vsi_kill_vlan(vsi, vid);
2097
2098 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002099
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002100 return 0;
2101}
2102
2103/**
2104 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2105 * @vsi: the vsi being brought back up
2106 **/
2107static void i40e_restore_vlan(struct i40e_vsi *vsi)
2108{
2109 u16 vid;
2110
2111 if (!vsi->netdev)
2112 return;
2113
2114 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2115
2116 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2117 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2118 vid);
2119}
2120
2121/**
2122 * i40e_vsi_add_pvid - Add pvid for the VSI
2123 * @vsi: the vsi being adjusted
2124 * @vid: the vlan id to set as a PVID
2125 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002126int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002127{
2128 struct i40e_vsi_context ctxt;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002129 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002130
2131 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2132 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002133 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2134 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002135 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002136
2137 ctxt.seid = vsi->seid;
2138 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002139 aq_ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2140 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002141 dev_info(&vsi->back->pdev->dev,
2142 "%s: update vsi failed, aq_err=%d\n",
2143 __func__, vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002144 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002145 }
2146
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002147 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002148}
2149
2150/**
2151 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2152 * @vsi: the vsi being adjusted
2153 *
2154 * Just use the vlan_rx_register() service to put it back to normal
2155 **/
2156void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2157{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002158 i40e_vlan_stripping_disable(vsi);
2159
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002160 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002161}
2162
2163/**
2164 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2165 * @vsi: ptr to the VSI
2166 *
2167 * If this function returns with an error, then it's possible one or
2168 * more of the rings is populated (while the rest are not). It is the
2169 * callers duty to clean those orphaned rings.
2170 *
2171 * Return 0 on success, negative on failure
2172 **/
2173static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2174{
2175 int i, err = 0;
2176
2177 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002178 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002179
2180 return err;
2181}
2182
2183/**
2184 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2185 * @vsi: ptr to the VSI
2186 *
2187 * Free VSI's transmit software resources
2188 **/
2189static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2190{
2191 int i;
2192
Greg Rose8e9dca52013-12-18 13:45:53 +00002193 if (!vsi->tx_rings)
2194 return;
2195
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002196 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002197 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002198 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002199}
2200
2201/**
2202 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2203 * @vsi: ptr to the VSI
2204 *
2205 * If this function returns with an error, then it's possible one or
2206 * more of the rings is populated (while the rest are not). It is the
2207 * callers duty to clean those orphaned rings.
2208 *
2209 * Return 0 on success, negative on failure
2210 **/
2211static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2212{
2213 int i, err = 0;
2214
2215 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002216 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002217 return err;
2218}
2219
2220/**
2221 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2222 * @vsi: ptr to the VSI
2223 *
2224 * Free all receive software resources
2225 **/
2226static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2227{
2228 int i;
2229
Greg Rose8e9dca52013-12-18 13:45:53 +00002230 if (!vsi->rx_rings)
2231 return;
2232
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002233 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002234 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002235 i40e_free_rx_resources(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002236}
2237
2238/**
2239 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2240 * @ring: The Tx ring to configure
2241 *
2242 * Configure the Tx descriptor ring in the HMC context.
2243 **/
2244static int i40e_configure_tx_ring(struct i40e_ring *ring)
2245{
2246 struct i40e_vsi *vsi = ring->vsi;
2247 u16 pf_q = vsi->base_queue + ring->queue_index;
2248 struct i40e_hw *hw = &vsi->back->hw;
2249 struct i40e_hmc_obj_txq tx_ctx;
2250 i40e_status err = 0;
2251 u32 qtx_ctl = 0;
2252
2253 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002254 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002255 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2256 ring->atr_count = 0;
2257 } else {
2258 ring->atr_sample_rate = 0;
2259 }
2260
2261 /* initialize XPS */
2262 if (ring->q_vector && ring->netdev &&
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08002263 vsi->tc_config.numtc <= 1 &&
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002264 !test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
2265 netif_set_xps_queue(ring->netdev,
2266 &ring->q_vector->affinity_mask,
2267 ring->queue_index);
2268
2269 /* clear the context structure first */
2270 memset(&tx_ctx, 0, sizeof(tx_ctx));
2271
2272 tx_ctx.new_context = 1;
2273 tx_ctx.base = (ring->dma / 128);
2274 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002275 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2276 I40E_FLAG_FD_ATR_ENABLED));
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002277 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002278 /* FDIR VSI tx ring can still use RS bit and writebacks */
2279 if (vsi->type != I40E_VSI_FDIR)
2280 tx_ctx.head_wb_ena = 1;
2281 tx_ctx.head_wb_addr = ring->dma +
2282 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002283
2284 /* As part of VSI creation/update, FW allocates certain
2285 * Tx arbitration queue sets for each TC enabled for
2286 * the VSI. The FW returns the handles to these queue
2287 * sets as part of the response buffer to Add VSI,
2288 * Update VSI, etc. AQ commands. It is expected that
2289 * these queue set handles be associated with the Tx
2290 * queues by the driver as part of the TX queue context
2291 * initialization. This has to be done regardless of
2292 * DCB as by default everything is mapped to TC0.
2293 */
2294 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2295 tx_ctx.rdylist_act = 0;
2296
2297 /* clear the context in the HMC */
2298 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2299 if (err) {
2300 dev_info(&vsi->back->pdev->dev,
2301 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2302 ring->queue_index, pf_q, err);
2303 return -ENOMEM;
2304 }
2305
2306 /* set the context in the HMC */
2307 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2308 if (err) {
2309 dev_info(&vsi->back->pdev->dev,
2310 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2311 ring->queue_index, pf_q, err);
2312 return -ENOMEM;
2313 }
2314
2315 /* Now associate this queue with this PCI function */
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002316 if (vsi->type == I40E_VSI_VMDQ2)
2317 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
2318 else
2319 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Shannon Nelson13fd9772013-09-28 07:14:19 +00002320 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2321 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002322 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2323 i40e_flush(hw);
2324
2325 clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
2326
2327 /* cache tail off for easier writes later */
2328 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2329
2330 return 0;
2331}
2332
2333/**
2334 * i40e_configure_rx_ring - Configure a receive ring context
2335 * @ring: The Rx ring to configure
2336 *
2337 * Configure the Rx descriptor ring in the HMC context.
2338 **/
2339static int i40e_configure_rx_ring(struct i40e_ring *ring)
2340{
2341 struct i40e_vsi *vsi = ring->vsi;
2342 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2343 u16 pf_q = vsi->base_queue + ring->queue_index;
2344 struct i40e_hw *hw = &vsi->back->hw;
2345 struct i40e_hmc_obj_rxq rx_ctx;
2346 i40e_status err = 0;
2347
2348 ring->state = 0;
2349
2350 /* clear the context structure first */
2351 memset(&rx_ctx, 0, sizeof(rx_ctx));
2352
2353 ring->rx_buf_len = vsi->rx_buf_len;
2354 ring->rx_hdr_len = vsi->rx_hdr_len;
2355
2356 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
2357 rx_ctx.hbuff = ring->rx_hdr_len >> I40E_RXQ_CTX_HBUFF_SHIFT;
2358
2359 rx_ctx.base = (ring->dma / 128);
2360 rx_ctx.qlen = ring->count;
2361
2362 if (vsi->back->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED) {
2363 set_ring_16byte_desc_enabled(ring);
2364 rx_ctx.dsize = 0;
2365 } else {
2366 rx_ctx.dsize = 1;
2367 }
2368
2369 rx_ctx.dtype = vsi->dtype;
2370 if (vsi->dtype) {
2371 set_ring_ps_enabled(ring);
2372 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
2373 I40E_RX_SPLIT_IP |
2374 I40E_RX_SPLIT_TCP_UDP |
2375 I40E_RX_SPLIT_SCTP;
2376 } else {
2377 rx_ctx.hsplit_0 = 0;
2378 }
2379
2380 rx_ctx.rxmax = min_t(u16, vsi->max_frame,
2381 (chain_len * ring->rx_buf_len));
2382 rx_ctx.tphrdesc_ena = 1;
2383 rx_ctx.tphwdesc_ena = 1;
2384 rx_ctx.tphdata_ena = 1;
2385 rx_ctx.tphhead_ena = 1;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00002386 if (hw->revision_id == 0)
2387 rx_ctx.lrxqthresh = 0;
2388 else
2389 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002390 rx_ctx.crcstrip = 1;
2391 rx_ctx.l2tsel = 1;
2392 rx_ctx.showiv = 1;
Catherine Sullivanacb36762014-03-06 09:02:30 +00002393 /* set the prefena field to 1 because the manual says to */
2394 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002395
2396 /* clear the context in the HMC */
2397 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2398 if (err) {
2399 dev_info(&vsi->back->pdev->dev,
2400 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2401 ring->queue_index, pf_q, err);
2402 return -ENOMEM;
2403 }
2404
2405 /* set the context in the HMC */
2406 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2407 if (err) {
2408 dev_info(&vsi->back->pdev->dev,
2409 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2410 ring->queue_index, pf_q, err);
2411 return -ENOMEM;
2412 }
2413
2414 /* cache tail for quicker writes, and clear the reg before use */
2415 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2416 writel(0, ring->tail);
2417
2418 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
2419
2420 return 0;
2421}
2422
2423/**
2424 * i40e_vsi_configure_tx - Configure the VSI for Tx
2425 * @vsi: VSI structure describing this set of rings and resources
2426 *
2427 * Configure the Tx VSI for operation.
2428 **/
2429static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2430{
2431 int err = 0;
2432 u16 i;
2433
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002434 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2435 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002436
2437 return err;
2438}
2439
2440/**
2441 * i40e_vsi_configure_rx - Configure the VSI for Rx
2442 * @vsi: the VSI being configured
2443 *
2444 * Configure the Rx VSI for operation.
2445 **/
2446static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
2447{
2448 int err = 0;
2449 u16 i;
2450
2451 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
2452 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
2453 + ETH_FCS_LEN + VLAN_HLEN;
2454 else
2455 vsi->max_frame = I40E_RXBUFFER_2048;
2456
2457 /* figure out correct receive buffer length */
2458 switch (vsi->back->flags & (I40E_FLAG_RX_1BUF_ENABLED |
2459 I40E_FLAG_RX_PS_ENABLED)) {
2460 case I40E_FLAG_RX_1BUF_ENABLED:
2461 vsi->rx_hdr_len = 0;
2462 vsi->rx_buf_len = vsi->max_frame;
2463 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2464 break;
2465 case I40E_FLAG_RX_PS_ENABLED:
2466 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2467 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2468 vsi->dtype = I40E_RX_DTYPE_HEADER_SPLIT;
2469 break;
2470 default:
2471 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2472 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2473 vsi->dtype = I40E_RX_DTYPE_SPLIT_ALWAYS;
2474 break;
2475 }
2476
2477 /* round up for the chip's needs */
2478 vsi->rx_hdr_len = ALIGN(vsi->rx_hdr_len,
2479 (1 << I40E_RXQ_CTX_HBUFF_SHIFT));
2480 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
2481 (1 << I40E_RXQ_CTX_DBUFF_SHIFT));
2482
2483 /* set up individual rings */
2484 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002485 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002486
2487 return err;
2488}
2489
2490/**
2491 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
2492 * @vsi: ptr to the VSI
2493 **/
2494static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
2495{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00002496 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002497 u16 qoffset, qcount;
2498 int i, n;
2499
2500 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED))
2501 return;
2502
2503 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
2504 if (!(vsi->tc_config.enabled_tc & (1 << n)))
2505 continue;
2506
2507 qoffset = vsi->tc_config.tc_info[n].qoffset;
2508 qcount = vsi->tc_config.tc_info[n].qcount;
2509 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00002510 rx_ring = vsi->rx_rings[i];
2511 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002512 rx_ring->dcb_tc = n;
2513 tx_ring->dcb_tc = n;
2514 }
2515 }
2516}
2517
2518/**
2519 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
2520 * @vsi: ptr to the VSI
2521 **/
2522static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
2523{
2524 if (vsi->netdev)
2525 i40e_set_rx_mode(vsi->netdev);
2526}
2527
2528/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00002529 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
2530 * @vsi: Pointer to the targeted VSI
2531 *
2532 * This function replays the hlist on the hw where all the SB Flow Director
2533 * filters were saved.
2534 **/
2535static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
2536{
2537 struct i40e_fdir_filter *filter;
2538 struct i40e_pf *pf = vsi->back;
2539 struct hlist_node *node;
2540
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00002541 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
2542 return;
2543
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00002544 hlist_for_each_entry_safe(filter, node,
2545 &pf->fdir_filter_list, fdir_node) {
2546 i40e_add_del_fdir(vsi, filter, true);
2547 }
2548}
2549
2550/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002551 * i40e_vsi_configure - Set up the VSI for action
2552 * @vsi: the VSI being configured
2553 **/
2554static int i40e_vsi_configure(struct i40e_vsi *vsi)
2555{
2556 int err;
2557
2558 i40e_set_vsi_rx_mode(vsi);
2559 i40e_restore_vlan(vsi);
2560 i40e_vsi_config_dcb_rings(vsi);
2561 err = i40e_vsi_configure_tx(vsi);
2562 if (!err)
2563 err = i40e_vsi_configure_rx(vsi);
2564
2565 return err;
2566}
2567
2568/**
2569 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
2570 * @vsi: the VSI being configured
2571 **/
2572static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
2573{
2574 struct i40e_pf *pf = vsi->back;
2575 struct i40e_q_vector *q_vector;
2576 struct i40e_hw *hw = &pf->hw;
2577 u16 vector;
2578 int i, q;
2579 u32 val;
2580 u32 qp;
2581
2582 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
2583 * and PFINT_LNKLSTn registers, e.g.:
2584 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
2585 */
2586 qp = vsi->base_queue;
2587 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00002588 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
2589 q_vector = vsi->q_vectors[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002590 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2591 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2592 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
2593 q_vector->rx.itr);
2594 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2595 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2596 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
2597 q_vector->tx.itr);
2598
2599 /* Linked list for the queuepairs assigned to this vector */
2600 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
2601 for (q = 0; q < q_vector->num_ringpairs; q++) {
2602 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2603 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2604 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
2605 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
2606 (I40E_QUEUE_TYPE_TX
2607 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
2608
2609 wr32(hw, I40E_QINT_RQCTL(qp), val);
2610
2611 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2612 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2613 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
2614 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
2615 (I40E_QUEUE_TYPE_RX
2616 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2617
2618 /* Terminate the linked list */
2619 if (q == (q_vector->num_ringpairs - 1))
2620 val |= (I40E_QUEUE_END_OF_LIST
2621 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2622
2623 wr32(hw, I40E_QINT_TQCTL(qp), val);
2624 qp++;
2625 }
2626 }
2627
2628 i40e_flush(hw);
2629}
2630
2631/**
2632 * i40e_enable_misc_int_causes - enable the non-queue interrupts
2633 * @hw: ptr to the hardware info
2634 **/
2635static void i40e_enable_misc_int_causes(struct i40e_hw *hw)
2636{
2637 u32 val;
2638
2639 /* clear things first */
2640 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
2641 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
2642
2643 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
2644 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
2645 I40E_PFINT_ICR0_ENA_GRST_MASK |
2646 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
2647 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002648 I40E_PFINT_ICR0_ENA_TIMESYNC_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002649 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
2650 I40E_PFINT_ICR0_ENA_VFLR_MASK |
2651 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2652
2653 wr32(hw, I40E_PFINT_ICR0_ENA, val);
2654
2655 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00002656 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
2657 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002658
2659 /* OTHER_ITR_IDX = 0 */
2660 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
2661}
2662
2663/**
2664 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
2665 * @vsi: the VSI being configured
2666 **/
2667static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
2668{
Alexander Duyck493fb302013-09-28 07:01:44 +00002669 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002670 struct i40e_pf *pf = vsi->back;
2671 struct i40e_hw *hw = &pf->hw;
2672 u32 val;
2673
2674 /* set the ITR configuration */
2675 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2676 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2677 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
2678 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2679 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2680 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
2681
2682 i40e_enable_misc_int_causes(hw);
2683
2684 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
2685 wr32(hw, I40E_PFINT_LNKLST0, 0);
2686
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00002687 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002688 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2689 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2690 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2691
2692 wr32(hw, I40E_QINT_RQCTL(0), val);
2693
2694 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2695 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2696 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2697
2698 wr32(hw, I40E_QINT_TQCTL(0), val);
2699 i40e_flush(hw);
2700}
2701
2702/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00002703 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
2704 * @pf: board private structure
2705 **/
2706void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
2707{
2708 struct i40e_hw *hw = &pf->hw;
2709
2710 wr32(hw, I40E_PFINT_DYN_CTL0,
2711 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2712 i40e_flush(hw);
2713}
2714
2715/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002716 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
2717 * @pf: board private structure
2718 **/
Shannon Nelson116a57d2013-09-28 07:13:59 +00002719void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002720{
2721 struct i40e_hw *hw = &pf->hw;
2722 u32 val;
2723
2724 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
2725 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
2726 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
2727
2728 wr32(hw, I40E_PFINT_DYN_CTL0, val);
2729 i40e_flush(hw);
2730}
2731
2732/**
2733 * i40e_irq_dynamic_enable - Enable default interrupt generation settings
2734 * @vsi: pointer to a vsi
2735 * @vector: enable a particular Hw Interrupt vector
2736 **/
2737void i40e_irq_dynamic_enable(struct i40e_vsi *vsi, int vector)
2738{
2739 struct i40e_pf *pf = vsi->back;
2740 struct i40e_hw *hw = &pf->hw;
2741 u32 val;
2742
2743 val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
2744 I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
2745 (I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2746 wr32(hw, I40E_PFINT_DYN_CTLN(vector - 1), val);
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00002747 /* skip the flush */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002748}
2749
2750/**
2751 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
2752 * @irq: interrupt number
2753 * @data: pointer to a q_vector
2754 **/
2755static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
2756{
2757 struct i40e_q_vector *q_vector = data;
2758
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002759 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002760 return IRQ_HANDLED;
2761
2762 napi_schedule(&q_vector->napi);
2763
2764 return IRQ_HANDLED;
2765}
2766
2767/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002768 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
2769 * @vsi: the VSI being configured
2770 * @basename: name for the vector
2771 *
2772 * Allocates MSI-X vectors and requests interrupts from the kernel.
2773 **/
2774static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
2775{
2776 int q_vectors = vsi->num_q_vectors;
2777 struct i40e_pf *pf = vsi->back;
2778 int base = vsi->base_vector;
2779 int rx_int_idx = 0;
2780 int tx_int_idx = 0;
2781 int vector, err;
2782
2783 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00002784 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002785
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002786 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002787 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2788 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
2789 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002790 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002791 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2792 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002793 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002794 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2795 "%s-%s-%d", basename, "tx", tx_int_idx++);
2796 } else {
2797 /* skip this unused q_vector */
2798 continue;
2799 }
2800 err = request_irq(pf->msix_entries[base + vector].vector,
2801 vsi->irq_handler,
2802 0,
2803 q_vector->name,
2804 q_vector);
2805 if (err) {
2806 dev_info(&pf->pdev->dev,
2807 "%s: request_irq failed, error: %d\n",
2808 __func__, err);
2809 goto free_queue_irqs;
2810 }
2811 /* assign the mask for this irq */
2812 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2813 &q_vector->affinity_mask);
2814 }
2815
Shannon Nelson63741842014-04-23 04:50:16 +00002816 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002817 return 0;
2818
2819free_queue_irqs:
2820 while (vector) {
2821 vector--;
2822 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2823 NULL);
2824 free_irq(pf->msix_entries[base + vector].vector,
2825 &(vsi->q_vectors[vector]));
2826 }
2827 return err;
2828}
2829
2830/**
2831 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
2832 * @vsi: the VSI being un-configured
2833 **/
2834static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
2835{
2836 struct i40e_pf *pf = vsi->back;
2837 struct i40e_hw *hw = &pf->hw;
2838 int base = vsi->base_vector;
2839 int i;
2840
2841 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002842 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
2843 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002844 }
2845
2846 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2847 for (i = vsi->base_vector;
2848 i < (vsi->num_q_vectors + vsi->base_vector); i++)
2849 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
2850
2851 i40e_flush(hw);
2852 for (i = 0; i < vsi->num_q_vectors; i++)
2853 synchronize_irq(pf->msix_entries[i + base].vector);
2854 } else {
2855 /* Legacy and MSI mode - this stops all interrupt handling */
2856 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
2857 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
2858 i40e_flush(hw);
2859 synchronize_irq(pf->pdev->irq);
2860 }
2861}
2862
2863/**
2864 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
2865 * @vsi: the VSI being configured
2866 **/
2867static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
2868{
2869 struct i40e_pf *pf = vsi->back;
2870 int i;
2871
2872 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2873 for (i = vsi->base_vector;
2874 i < (vsi->num_q_vectors + vsi->base_vector); i++)
2875 i40e_irq_dynamic_enable(vsi, i);
2876 } else {
2877 i40e_irq_dynamic_enable_icr0(pf);
2878 }
2879
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00002880 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002881 return 0;
2882}
2883
2884/**
2885 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
2886 * @pf: board private structure
2887 **/
2888static void i40e_stop_misc_vector(struct i40e_pf *pf)
2889{
2890 /* Disable ICR 0 */
2891 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
2892 i40e_flush(&pf->hw);
2893}
2894
2895/**
2896 * i40e_intr - MSI/Legacy and non-queue interrupt handler
2897 * @irq: interrupt number
2898 * @data: pointer to a q_vector
2899 *
2900 * This is the handler used for all MSI/Legacy interrupts, and deals
2901 * with both queue and non-queue interrupts. This is also used in
2902 * MSIX mode to handle the non-queue interrupts.
2903 **/
2904static irqreturn_t i40e_intr(int irq, void *data)
2905{
2906 struct i40e_pf *pf = (struct i40e_pf *)data;
2907 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002908 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002909 u32 icr0, icr0_remaining;
2910 u32 val, ena_mask;
2911
2912 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002913 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002914
Shannon Nelson116a57d2013-09-28 07:13:59 +00002915 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
2916 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002917 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002918
Shannon Nelsoncd92e722013-11-16 10:00:44 +00002919 /* if interrupt but no bits showing, must be SWINT */
2920 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
2921 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
2922 pf->sw_int_count++;
2923
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002924 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
2925 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
2926
2927 /* temporarily disable queue cause for NAPI processing */
2928 u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
2929 qval &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
2930 wr32(hw, I40E_QINT_RQCTL(0), qval);
2931
2932 qval = rd32(hw, I40E_QINT_TQCTL(0));
2933 qval &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
2934 wr32(hw, I40E_QINT_TQCTL(0), qval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002935
2936 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck493fb302013-09-28 07:01:44 +00002937 napi_schedule(&pf->vsi[pf->lan_vsi]->q_vectors[0]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002938 }
2939
2940 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
2941 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2942 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
2943 }
2944
2945 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
2946 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
2947 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
2948 }
2949
2950 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
2951 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
2952 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
2953 }
2954
2955 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
2956 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
2957 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
2958 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
2959 val = rd32(hw, I40E_GLGEN_RSTAT);
2960 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
2961 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002962 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002963 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002964 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002965 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002966 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002967 pf->empr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00002968 set_bit(__I40E_EMP_RESET_REQUESTED, &pf->state);
2969 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002970 }
2971
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002972 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
2973 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
2974 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
2975 }
2976
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002977 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
2978 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
2979
2980 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07002981 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002982 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002983 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002984 }
2985
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002986 /* If a critical error is pending we have no choice but to reset the
2987 * device.
2988 * Report and mask out any remaining unexpected interrupts.
2989 */
2990 icr0_remaining = icr0 & ena_mask;
2991 if (icr0_remaining) {
2992 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
2993 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002994 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002995 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00002996 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002997 dev_info(&pf->pdev->dev, "device will be reset\n");
2998 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
2999 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003000 }
3001 ena_mask &= ~icr0_remaining;
3002 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003003 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003004
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003005enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003006 /* re-enable interrupt causes */
3007 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003008 if (!test_bit(__I40E_DOWN, &pf->state)) {
3009 i40e_service_event_schedule(pf);
3010 i40e_irq_dynamic_enable_icr0(pf);
3011 }
3012
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003013 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003014}
3015
3016/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003017 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3018 * @tx_ring: tx ring to clean
3019 * @budget: how many cleans we're allowed
3020 *
3021 * Returns true if there's any budget left (e.g. the clean is finished)
3022 **/
3023static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3024{
3025 struct i40e_vsi *vsi = tx_ring->vsi;
3026 u16 i = tx_ring->next_to_clean;
3027 struct i40e_tx_buffer *tx_buf;
3028 struct i40e_tx_desc *tx_desc;
3029
3030 tx_buf = &tx_ring->tx_bi[i];
3031 tx_desc = I40E_TX_DESC(tx_ring, i);
3032 i -= tx_ring->count;
3033
3034 do {
3035 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3036
3037 /* if next_to_watch is not set then there is no work pending */
3038 if (!eop_desc)
3039 break;
3040
3041 /* prevent any other reads prior to eop_desc */
3042 read_barrier_depends();
3043
3044 /* if the descriptor isn't done, no work yet to do */
3045 if (!(eop_desc->cmd_type_offset_bsz &
3046 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3047 break;
3048
3049 /* clear next_to_watch to prevent false hangs */
3050 tx_buf->next_to_watch = NULL;
3051
3052 /* unmap skb header data */
3053 dma_unmap_single(tx_ring->dev,
3054 dma_unmap_addr(tx_buf, dma),
3055 dma_unmap_len(tx_buf, len),
3056 DMA_TO_DEVICE);
3057
3058 dma_unmap_len_set(tx_buf, len, 0);
3059
3060
3061 /* move to the next desc and buffer to clean */
3062 tx_buf++;
3063 tx_desc++;
3064 i++;
3065 if (unlikely(!i)) {
3066 i -= tx_ring->count;
3067 tx_buf = tx_ring->tx_bi;
3068 tx_desc = I40E_TX_DESC(tx_ring, 0);
3069 }
3070
3071 /* update budget accounting */
3072 budget--;
3073 } while (likely(budget));
3074
3075 i += tx_ring->count;
3076 tx_ring->next_to_clean = i;
3077
3078 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
3079 i40e_irq_dynamic_enable(vsi,
3080 tx_ring->q_vector->v_idx + vsi->base_vector);
3081 }
3082 return budget > 0;
3083}
3084
3085/**
3086 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3087 * @irq: interrupt number
3088 * @data: pointer to a q_vector
3089 **/
3090static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3091{
3092 struct i40e_q_vector *q_vector = data;
3093 struct i40e_vsi *vsi;
3094
3095 if (!q_vector->tx.ring)
3096 return IRQ_HANDLED;
3097
3098 vsi = q_vector->tx.ring->vsi;
3099 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3100
3101 return IRQ_HANDLED;
3102}
3103
3104/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003105 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003106 * @vsi: the VSI being configured
3107 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003108 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003109 **/
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003110static void map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003111{
Alexander Duyck493fb302013-09-28 07:01:44 +00003112 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00003113 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3114 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003115
3116 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003117 tx_ring->next = q_vector->tx.ring;
3118 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003119 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003120
3121 rx_ring->q_vector = q_vector;
3122 rx_ring->next = q_vector->rx.ring;
3123 q_vector->rx.ring = rx_ring;
3124 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003125}
3126
3127/**
3128 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3129 * @vsi: the VSI being configured
3130 *
3131 * This function maps descriptor rings to the queue-specific vectors
3132 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3133 * one vector per queue pair, but on a constrained vector budget, we
3134 * group the queue pairs as "efficiently" as possible.
3135 **/
3136static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3137{
3138 int qp_remaining = vsi->num_queue_pairs;
3139 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003140 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003141 int v_start = 0;
3142 int qp_idx = 0;
3143
3144 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3145 * group them so there are multiple queues per vector.
3146 */
3147 for (; v_start < q_vectors && qp_remaining; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003148 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3149
3150 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3151
3152 q_vector->num_ringpairs = num_ringpairs;
3153
3154 q_vector->rx.count = 0;
3155 q_vector->tx.count = 0;
3156 q_vector->rx.ring = NULL;
3157 q_vector->tx.ring = NULL;
3158
3159 while (num_ringpairs--) {
3160 map_vector_to_qp(vsi, v_start, qp_idx);
3161 qp_idx++;
3162 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003163 }
3164 }
3165}
3166
3167/**
3168 * i40e_vsi_request_irq - Request IRQ from the OS
3169 * @vsi: the VSI being configured
3170 * @basename: name for the vector
3171 **/
3172static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3173{
3174 struct i40e_pf *pf = vsi->back;
3175 int err;
3176
3177 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3178 err = i40e_vsi_request_irq_msix(vsi, basename);
3179 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3180 err = request_irq(pf->pdev->irq, i40e_intr, 0,
3181 pf->misc_int_name, pf);
3182 else
3183 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
3184 pf->misc_int_name, pf);
3185
3186 if (err)
3187 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3188
3189 return err;
3190}
3191
3192#ifdef CONFIG_NET_POLL_CONTROLLER
3193/**
3194 * i40e_netpoll - A Polling 'interrupt'handler
3195 * @netdev: network interface device structure
3196 *
3197 * This is used by netconsole to send skbs without having to re-enable
3198 * interrupts. It's not called while the normal interrupt routine is executing.
3199 **/
3200static void i40e_netpoll(struct net_device *netdev)
3201{
3202 struct i40e_netdev_priv *np = netdev_priv(netdev);
3203 struct i40e_vsi *vsi = np->vsi;
3204 struct i40e_pf *pf = vsi->back;
3205 int i;
3206
3207 /* if interface is down do nothing */
3208 if (test_bit(__I40E_DOWN, &vsi->state))
3209 return;
3210
3211 pf->flags |= I40E_FLAG_IN_NETPOLL;
3212 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3213 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003214 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003215 } else {
3216 i40e_intr(pf->pdev->irq, netdev);
3217 }
3218 pf->flags &= ~I40E_FLAG_IN_NETPOLL;
3219}
3220#endif
3221
3222/**
3223 * i40e_vsi_control_tx - Start or stop a VSI's rings
3224 * @vsi: the VSI being configured
3225 * @enable: start or stop the rings
3226 **/
3227static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3228{
3229 struct i40e_pf *pf = vsi->back;
3230 struct i40e_hw *hw = &pf->hw;
3231 int i, j, pf_q;
3232 u32 tx_reg;
3233
3234 pf_q = vsi->base_queue;
3235 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Matt Jared351499a2014-04-23 04:50:03 +00003236
3237 /* warn the TX unit of coming changes */
3238 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
3239 if (!enable)
3240 udelay(10);
3241
Mitch Williams6c5ef622014-02-20 19:29:16 -08003242 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003243 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003244 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3245 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3246 break;
3247 usleep_range(1000, 2000);
3248 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003249 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c122002014-03-14 07:32:29 +00003250 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00003251 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003252
3253 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003254 if (enable) {
3255 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003256 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003257 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003258 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003259 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003260
3261 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
3262
3263 /* wait for the change to finish */
3264 for (j = 0; j < 10; j++) {
3265 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Catherine Sullivan7c122002014-03-14 07:32:29 +00003266 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3267 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003268
3269 udelay(10);
3270 }
3271 if (j >= 10) {
3272 dev_info(&pf->pdev->dev, "Tx ring %d %sable timeout\n",
3273 pf_q, (enable ? "en" : "dis"));
3274 return -ETIMEDOUT;
3275 }
3276 }
3277
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003278 if (hw->revision_id == 0)
3279 mdelay(50);
3280
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003281 return 0;
3282}
3283
3284/**
3285 * i40e_vsi_control_rx - Start or stop a VSI's rings
3286 * @vsi: the VSI being configured
3287 * @enable: start or stop the rings
3288 **/
3289static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3290{
3291 struct i40e_pf *pf = vsi->back;
3292 struct i40e_hw *hw = &pf->hw;
3293 int i, j, pf_q;
3294 u32 rx_reg;
3295
3296 pf_q = vsi->base_queue;
3297 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003298 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003299 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003300 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
3301 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
3302 break;
3303 usleep_range(1000, 2000);
3304 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003305
Catherine Sullivan7c122002014-03-14 07:32:29 +00003306 /* Skip if the queue is already in the requested state */
3307 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3308 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003309
3310 /* turn on/off the queue */
3311 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08003312 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003313 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08003314 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003315 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
3316
3317 /* wait for the change to finish */
3318 for (j = 0; j < 10; j++) {
3319 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
3320
Catherine Sullivan7c122002014-03-14 07:32:29 +00003321 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3322 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003323
3324 udelay(10);
3325 }
3326 if (j >= 10) {
3327 dev_info(&pf->pdev->dev, "Rx ring %d %sable timeout\n",
3328 pf_q, (enable ? "en" : "dis"));
3329 return -ETIMEDOUT;
3330 }
3331 }
3332
3333 return 0;
3334}
3335
3336/**
3337 * i40e_vsi_control_rings - Start or stop a VSI's rings
3338 * @vsi: the VSI being configured
3339 * @enable: start or stop the rings
3340 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00003341int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003342{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003343 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003344
3345 /* do rx first for enable and last for disable */
3346 if (request) {
3347 ret = i40e_vsi_control_rx(vsi, request);
3348 if (ret)
3349 return ret;
3350 ret = i40e_vsi_control_tx(vsi, request);
3351 } else {
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003352 /* Ignore return value, we need to shutdown whatever we can */
3353 i40e_vsi_control_tx(vsi, request);
3354 i40e_vsi_control_rx(vsi, request);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003355 }
3356
3357 return ret;
3358}
3359
3360/**
3361 * i40e_vsi_free_irq - Free the irq association with the OS
3362 * @vsi: the VSI being configured
3363 **/
3364static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
3365{
3366 struct i40e_pf *pf = vsi->back;
3367 struct i40e_hw *hw = &pf->hw;
3368 int base = vsi->base_vector;
3369 u32 val, qp;
3370 int i;
3371
3372 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3373 if (!vsi->q_vectors)
3374 return;
3375
Shannon Nelson63741842014-04-23 04:50:16 +00003376 if (!vsi->irqs_ready)
3377 return;
3378
3379 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003380 for (i = 0; i < vsi->num_q_vectors; i++) {
3381 u16 vector = i + base;
3382
3383 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00003384 if (!vsi->q_vectors[i] ||
3385 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003386 continue;
3387
3388 /* clear the affinity_mask in the IRQ descriptor */
3389 irq_set_affinity_hint(pf->msix_entries[vector].vector,
3390 NULL);
3391 free_irq(pf->msix_entries[vector].vector,
Alexander Duyck493fb302013-09-28 07:01:44 +00003392 vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003393
3394 /* Tear down the interrupt queue link list
3395 *
3396 * We know that they come in pairs and always
3397 * the Rx first, then the Tx. To clear the
3398 * link list, stick the EOL value into the
3399 * next_q field of the registers.
3400 */
3401 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
3402 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3403 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3404 val |= I40E_QUEUE_END_OF_LIST
3405 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3406 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
3407
3408 while (qp != I40E_QUEUE_END_OF_LIST) {
3409 u32 next;
3410
3411 val = rd32(hw, I40E_QINT_RQCTL(qp));
3412
3413 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3414 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3415 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3416 I40E_QINT_RQCTL_INTEVENT_MASK);
3417
3418 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3419 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3420
3421 wr32(hw, I40E_QINT_RQCTL(qp), val);
3422
3423 val = rd32(hw, I40E_QINT_TQCTL(qp));
3424
3425 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
3426 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
3427
3428 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3429 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3430 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3431 I40E_QINT_TQCTL_INTEVENT_MASK);
3432
3433 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3434 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3435
3436 wr32(hw, I40E_QINT_TQCTL(qp), val);
3437 qp = next;
3438 }
3439 }
3440 } else {
3441 free_irq(pf->pdev->irq, pf);
3442
3443 val = rd32(hw, I40E_PFINT_LNKLST0);
3444 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3445 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3446 val |= I40E_QUEUE_END_OF_LIST
3447 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
3448 wr32(hw, I40E_PFINT_LNKLST0, val);
3449
3450 val = rd32(hw, I40E_QINT_RQCTL(qp));
3451 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3452 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3453 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3454 I40E_QINT_RQCTL_INTEVENT_MASK);
3455
3456 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3457 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3458
3459 wr32(hw, I40E_QINT_RQCTL(qp), val);
3460
3461 val = rd32(hw, I40E_QINT_TQCTL(qp));
3462
3463 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3464 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3465 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3466 I40E_QINT_TQCTL_INTEVENT_MASK);
3467
3468 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3469 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3470
3471 wr32(hw, I40E_QINT_TQCTL(qp), val);
3472 }
3473}
3474
3475/**
Alexander Duyck493fb302013-09-28 07:01:44 +00003476 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
3477 * @vsi: the VSI being configured
3478 * @v_idx: Index of vector to be freed
3479 *
3480 * This function frees the memory allocated to the q_vector. In addition if
3481 * NAPI is enabled it will delete any references to the NAPI struct prior
3482 * to freeing the q_vector.
3483 **/
3484static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
3485{
3486 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003487 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00003488
3489 if (!q_vector)
3490 return;
3491
3492 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003493 i40e_for_each_ring(ring, q_vector->tx)
3494 ring->q_vector = NULL;
3495
3496 i40e_for_each_ring(ring, q_vector->rx)
3497 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00003498
3499 /* only VSI w/ an associated netdev is set up w/ NAPI */
3500 if (vsi->netdev)
3501 netif_napi_del(&q_vector->napi);
3502
3503 vsi->q_vectors[v_idx] = NULL;
3504
3505 kfree_rcu(q_vector, rcu);
3506}
3507
3508/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003509 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
3510 * @vsi: the VSI being un-configured
3511 *
3512 * This frees the memory allocated to the q_vectors and
3513 * deletes references to the NAPI struct.
3514 **/
3515static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
3516{
3517 int v_idx;
3518
Alexander Duyck493fb302013-09-28 07:01:44 +00003519 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
3520 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003521}
3522
3523/**
3524 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
3525 * @pf: board private structure
3526 **/
3527static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
3528{
3529 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
3530 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3531 pci_disable_msix(pf->pdev);
3532 kfree(pf->msix_entries);
3533 pf->msix_entries = NULL;
3534 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
3535 pci_disable_msi(pf->pdev);
3536 }
3537 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
3538}
3539
3540/**
3541 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
3542 * @pf: board private structure
3543 *
3544 * We go through and clear interrupt specific resources and reset the structure
3545 * to pre-load conditions
3546 **/
3547static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
3548{
3549 int i;
3550
3551 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00003552 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003553 if (pf->vsi[i])
3554 i40e_vsi_free_q_vectors(pf->vsi[i]);
3555 i40e_reset_interrupt_capability(pf);
3556}
3557
3558/**
3559 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
3560 * @vsi: the VSI being configured
3561 **/
3562static void i40e_napi_enable_all(struct i40e_vsi *vsi)
3563{
3564 int q_idx;
3565
3566 if (!vsi->netdev)
3567 return;
3568
3569 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003570 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003571}
3572
3573/**
3574 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
3575 * @vsi: the VSI being configured
3576 **/
3577static void i40e_napi_disable_all(struct i40e_vsi *vsi)
3578{
3579 int q_idx;
3580
3581 if (!vsi->netdev)
3582 return;
3583
3584 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003585 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003586}
3587
3588/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00003589 * i40e_vsi_close - Shut down a VSI
3590 * @vsi: the vsi to be quelled
3591 **/
3592static void i40e_vsi_close(struct i40e_vsi *vsi)
3593{
3594 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
3595 i40e_down(vsi);
3596 i40e_vsi_free_irq(vsi);
3597 i40e_vsi_free_tx_resources(vsi);
3598 i40e_vsi_free_rx_resources(vsi);
3599}
3600
3601/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003602 * i40e_quiesce_vsi - Pause a given VSI
3603 * @vsi: the VSI being paused
3604 **/
3605static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
3606{
3607 if (test_bit(__I40E_DOWN, &vsi->state))
3608 return;
3609
3610 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
3611 if (vsi->netdev && netif_running(vsi->netdev)) {
3612 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
3613 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00003614 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003615 }
3616}
3617
3618/**
3619 * i40e_unquiesce_vsi - Resume a given VSI
3620 * @vsi: the VSI being resumed
3621 **/
3622static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
3623{
3624 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
3625 return;
3626
3627 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
3628 if (vsi->netdev && netif_running(vsi->netdev))
3629 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
3630 else
Shannon Nelson8276f752014-03-14 07:32:27 +00003631 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003632}
3633
3634/**
3635 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
3636 * @pf: the PF
3637 **/
3638static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
3639{
3640 int v;
3641
Mitch Williams505682c2014-05-20 08:01:37 +00003642 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003643 if (pf->vsi[v])
3644 i40e_quiesce_vsi(pf->vsi[v]);
3645 }
3646}
3647
3648/**
3649 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
3650 * @pf: the PF
3651 **/
3652static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
3653{
3654 int v;
3655
Mitch Williams505682c2014-05-20 08:01:37 +00003656 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003657 if (pf->vsi[v])
3658 i40e_unquiesce_vsi(pf->vsi[v]);
3659 }
3660}
3661
3662/**
3663 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
3664 * @dcbcfg: the corresponding DCBx configuration structure
3665 *
3666 * Return the number of TCs from given DCBx configuration
3667 **/
3668static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
3669{
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003670 u8 num_tc = 0;
3671 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003672
3673 /* Scan the ETS Config Priority Table to find
3674 * traffic class enabled for a given priority
3675 * and use the traffic class index to get the
3676 * number of traffic classes enabled
3677 */
3678 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3679 if (dcbcfg->etscfg.prioritytable[i] > num_tc)
3680 num_tc = dcbcfg->etscfg.prioritytable[i];
3681 }
3682
3683 /* Traffic class index starts from zero so
3684 * increment to return the actual count
3685 */
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003686 return num_tc + 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003687}
3688
3689/**
3690 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
3691 * @dcbcfg: the corresponding DCBx configuration structure
3692 *
3693 * Query the current DCB configuration and return the number of
3694 * traffic classes enabled from the given DCBX config
3695 **/
3696static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
3697{
3698 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
3699 u8 enabled_tc = 1;
3700 u8 i;
3701
3702 for (i = 0; i < num_tc; i++)
3703 enabled_tc |= 1 << i;
3704
3705 return enabled_tc;
3706}
3707
3708/**
3709 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
3710 * @pf: PF being queried
3711 *
3712 * Return number of traffic classes enabled for the given PF
3713 **/
3714static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
3715{
3716 struct i40e_hw *hw = &pf->hw;
3717 u8 i, enabled_tc;
3718 u8 num_tc = 0;
3719 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3720
3721 /* If DCB is not enabled then always in single TC */
3722 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3723 return 1;
3724
3725 /* MFP mode return count of enabled TCs for this PF */
3726 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
3727 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3728 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3729 if (enabled_tc & (1 << i))
3730 num_tc++;
3731 }
3732 return num_tc;
3733 }
3734
3735 /* SFP mode will be enabled for all TCs on port */
3736 return i40e_dcb_get_num_tc(dcbcfg);
3737}
3738
3739/**
3740 * i40e_pf_get_default_tc - Get bitmap for first enabled TC
3741 * @pf: PF being queried
3742 *
3743 * Return a bitmap for first enabled traffic class for this PF.
3744 **/
3745static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
3746{
3747 u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3748 u8 i = 0;
3749
3750 if (!enabled_tc)
3751 return 0x1; /* TC0 */
3752
3753 /* Find the first enabled TC */
3754 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3755 if (enabled_tc & (1 << i))
3756 break;
3757 }
3758
3759 return 1 << i;
3760}
3761
3762/**
3763 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
3764 * @pf: PF being queried
3765 *
3766 * Return a bitmap for enabled traffic classes for this PF.
3767 **/
3768static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
3769{
3770 /* If DCB is not enabled for this PF then just return default TC */
3771 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3772 return i40e_pf_get_default_tc(pf);
3773
3774 /* MFP mode will have enabled TCs set by FW */
3775 if (pf->flags & I40E_FLAG_MFP_ENABLED)
3776 return pf->hw.func_caps.enabled_tcmap;
3777
3778 /* SFP mode we want PF to be enabled for all TCs */
3779 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
3780}
3781
3782/**
3783 * i40e_vsi_get_bw_info - Query VSI BW Information
3784 * @vsi: the VSI being queried
3785 *
3786 * Returns 0 on success, negative value on failure
3787 **/
3788static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
3789{
3790 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
3791 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
3792 struct i40e_pf *pf = vsi->back;
3793 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003794 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003795 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003796 int i;
3797
3798 /* Get the VSI level BW configuration */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003799 aq_ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
3800 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003801 dev_info(&pf->pdev->dev,
3802 "couldn't get pf vsi bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003803 aq_ret, pf->hw.aq.asq_last_status);
3804 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003805 }
3806
3807 /* Get the VSI level BW configuration per TC */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003808 aq_ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
Jesse Brandeburg6838b532014-01-14 00:49:52 -08003809 NULL);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003810 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003811 dev_info(&pf->pdev->dev,
3812 "couldn't get pf vsi ets bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003813 aq_ret, pf->hw.aq.asq_last_status);
3814 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003815 }
3816
3817 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
3818 dev_info(&pf->pdev->dev,
3819 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
3820 bw_config.tc_valid_bits,
3821 bw_ets_config.tc_valid_bits);
3822 /* Still continuing */
3823 }
3824
3825 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
3826 vsi->bw_max_quanta = bw_config.max_bw;
3827 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
3828 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
3829 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3830 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
3831 vsi->bw_ets_limit_credits[i] =
3832 le16_to_cpu(bw_ets_config.credits[i]);
3833 /* 3 bits out of 4 for each TC */
3834 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
3835 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003836
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003837 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003838}
3839
3840/**
3841 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
3842 * @vsi: the VSI being configured
3843 * @enabled_tc: TC bitmap
3844 * @bw_credits: BW shared credits per TC
3845 *
3846 * Returns 0 on success, negative value on failure
3847 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003848static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003849 u8 *bw_share)
3850{
3851 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003852 i40e_status aq_ret;
3853 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003854
3855 bw_data.tc_valid_bits = enabled_tc;
3856 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3857 bw_data.tc_bw_credits[i] = bw_share[i];
3858
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003859 aq_ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
3860 NULL);
3861 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003862 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00003863 "AQ command Config VSI BW allocation per TC failed = %d\n",
3864 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003865 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003866 }
3867
3868 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3869 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
3870
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003871 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003872}
3873
3874/**
3875 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
3876 * @vsi: the VSI being configured
3877 * @enabled_tc: TC map to be enabled
3878 *
3879 **/
3880static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3881{
3882 struct net_device *netdev = vsi->netdev;
3883 struct i40e_pf *pf = vsi->back;
3884 struct i40e_hw *hw = &pf->hw;
3885 u8 netdev_tc = 0;
3886 int i;
3887 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3888
3889 if (!netdev)
3890 return;
3891
3892 if (!enabled_tc) {
3893 netdev_reset_tc(netdev);
3894 return;
3895 }
3896
3897 /* Set up actual enabled TCs on the VSI */
3898 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
3899 return;
3900
3901 /* set per TC queues for the VSI */
3902 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3903 /* Only set TC queues for enabled tcs
3904 *
3905 * e.g. For a VSI that has TC0 and TC3 enabled the
3906 * enabled_tc bitmap would be 0x00001001; the driver
3907 * will set the numtc for netdev as 2 that will be
3908 * referenced by the netdev layer as TC 0 and 1.
3909 */
3910 if (vsi->tc_config.enabled_tc & (1 << i))
3911 netdev_set_tc_queue(netdev,
3912 vsi->tc_config.tc_info[i].netdev_tc,
3913 vsi->tc_config.tc_info[i].qcount,
3914 vsi->tc_config.tc_info[i].qoffset);
3915 }
3916
3917 /* Assign UP2TC map for the VSI */
3918 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3919 /* Get the actual TC# for the UP */
3920 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
3921 /* Get the mapped netdev TC# for the UP */
3922 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
3923 netdev_set_prio_tc_map(netdev, i, netdev_tc);
3924 }
3925}
3926
3927/**
3928 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
3929 * @vsi: the VSI being configured
3930 * @ctxt: the ctxt buffer returned from AQ VSI update param command
3931 **/
3932static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
3933 struct i40e_vsi_context *ctxt)
3934{
3935 /* copy just the sections touched not the entire info
3936 * since not all sections are valid as returned by
3937 * update vsi params
3938 */
3939 vsi->info.mapping_flags = ctxt->info.mapping_flags;
3940 memcpy(&vsi->info.queue_mapping,
3941 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
3942 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
3943 sizeof(vsi->info.tc_mapping));
3944}
3945
3946/**
3947 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
3948 * @vsi: VSI to be configured
3949 * @enabled_tc: TC bitmap
3950 *
3951 * This configures a particular VSI for TCs that are mapped to the
3952 * given TC bitmap. It uses default bandwidth share for TCs across
3953 * VSIs to configure TC for a particular VSI.
3954 *
3955 * NOTE:
3956 * It is expected that the VSI queues have been quisced before calling
3957 * this function.
3958 **/
3959static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3960{
3961 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
3962 struct i40e_vsi_context ctxt;
3963 int ret = 0;
3964 int i;
3965
3966 /* Check if enabled_tc is same as existing or new TCs */
3967 if (vsi->tc_config.enabled_tc == enabled_tc)
3968 return ret;
3969
3970 /* Enable ETS TCs with equal BW Share for now across all VSIs */
3971 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3972 if (enabled_tc & (1 << i))
3973 bw_share[i] = 1;
3974 }
3975
3976 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
3977 if (ret) {
3978 dev_info(&vsi->back->pdev->dev,
3979 "Failed configuring TC map %d for VSI %d\n",
3980 enabled_tc, vsi->seid);
3981 goto out;
3982 }
3983
3984 /* Update Queue Pairs Mapping for currently enabled UPs */
3985 ctxt.seid = vsi->seid;
3986 ctxt.pf_num = vsi->back->hw.pf_id;
3987 ctxt.vf_num = 0;
3988 ctxt.uplink_seid = vsi->uplink_seid;
3989 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
3990 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
3991
3992 /* Update the VSI after updating the VSI queue-mapping information */
3993 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3994 if (ret) {
3995 dev_info(&vsi->back->pdev->dev,
3996 "update vsi failed, aq_err=%d\n",
3997 vsi->back->hw.aq.asq_last_status);
3998 goto out;
3999 }
4000 /* update the local VSI info with updated queue map */
4001 i40e_vsi_update_queue_map(vsi, &ctxt);
4002 vsi->info.valid_sections = 0;
4003
4004 /* Update current VSI BW information */
4005 ret = i40e_vsi_get_bw_info(vsi);
4006 if (ret) {
4007 dev_info(&vsi->back->pdev->dev,
4008 "Failed updating vsi bw info, aq_err=%d\n",
4009 vsi->back->hw.aq.asq_last_status);
4010 goto out;
4011 }
4012
4013 /* Update the netdev TC setup */
4014 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
4015out:
4016 return ret;
4017}
4018
4019/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004020 * i40e_veb_config_tc - Configure TCs for given VEB
4021 * @veb: given VEB
4022 * @enabled_tc: TC bitmap
4023 *
4024 * Configures given TC bitmap for VEB (switching) element
4025 **/
4026int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
4027{
4028 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
4029 struct i40e_pf *pf = veb->pf;
4030 int ret = 0;
4031 int i;
4032
4033 /* No TCs or already enabled TCs just return */
4034 if (!enabled_tc || veb->enabled_tc == enabled_tc)
4035 return ret;
4036
4037 bw_data.tc_valid_bits = enabled_tc;
4038 /* bw_data.absolute_credits is not set (relative) */
4039
4040 /* Enable ETS TCs with equal BW Share for now */
4041 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4042 if (enabled_tc & (1 << i))
4043 bw_data.tc_bw_share_credits[i] = 1;
4044 }
4045
4046 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
4047 &bw_data, NULL);
4048 if (ret) {
4049 dev_info(&pf->pdev->dev,
4050 "veb bw config failed, aq_err=%d\n",
4051 pf->hw.aq.asq_last_status);
4052 goto out;
4053 }
4054
4055 /* Update the BW information */
4056 ret = i40e_veb_get_bw_info(veb);
4057 if (ret) {
4058 dev_info(&pf->pdev->dev,
4059 "Failed getting veb bw config, aq_err=%d\n",
4060 pf->hw.aq.asq_last_status);
4061 }
4062
4063out:
4064 return ret;
4065}
4066
4067#ifdef CONFIG_I40E_DCB
4068/**
4069 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
4070 * @pf: PF struct
4071 *
4072 * Reconfigure VEB/VSIs on a given PF; it is assumed that
4073 * the caller would've quiesce all the VSIs before calling
4074 * this function
4075 **/
4076static void i40e_dcb_reconfigure(struct i40e_pf *pf)
4077{
4078 u8 tc_map = 0;
4079 int ret;
4080 u8 v;
4081
4082 /* Enable the TCs available on PF to all VEBs */
4083 tc_map = i40e_pf_get_tc_map(pf);
4084 for (v = 0; v < I40E_MAX_VEB; v++) {
4085 if (!pf->veb[v])
4086 continue;
4087 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
4088 if (ret) {
4089 dev_info(&pf->pdev->dev,
4090 "Failed configuring TC for VEB seid=%d\n",
4091 pf->veb[v]->seid);
4092 /* Will try to configure as many components */
4093 }
4094 }
4095
4096 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00004097 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004098 if (!pf->vsi[v])
4099 continue;
4100
4101 /* - Enable all TCs for the LAN VSI
4102 * - For all others keep them at TC0 for now
4103 */
4104 if (v == pf->lan_vsi)
4105 tc_map = i40e_pf_get_tc_map(pf);
4106 else
4107 tc_map = i40e_pf_get_default_tc(pf);
4108
4109 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
4110 if (ret) {
4111 dev_info(&pf->pdev->dev,
4112 "Failed configuring TC for VSI seid=%d\n",
4113 pf->vsi[v]->seid);
4114 /* Will try to configure as many components */
4115 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00004116 /* Re-configure VSI vectors based on updated TC map */
4117 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004118 if (pf->vsi[v]->netdev)
4119 i40e_dcbnl_set_all(pf->vsi[v]);
4120 }
4121 }
4122}
4123
4124/**
4125 * i40e_init_pf_dcb - Initialize DCB configuration
4126 * @pf: PF being configured
4127 *
4128 * Query the current DCB configuration and cache it
4129 * in the hardware structure
4130 **/
4131static int i40e_init_pf_dcb(struct i40e_pf *pf)
4132{
4133 struct i40e_hw *hw = &pf->hw;
4134 int err = 0;
4135
4136 if (pf->hw.func_caps.npar_enable)
4137 goto out;
4138
4139 /* Get the initial DCB configuration */
4140 err = i40e_init_dcb(hw);
4141 if (!err) {
4142 /* Device/Function is not DCBX capable */
4143 if ((!hw->func_caps.dcb) ||
4144 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
4145 dev_info(&pf->pdev->dev,
4146 "DCBX offload is not supported or is disabled for this PF.\n");
4147
4148 if (pf->flags & I40E_FLAG_MFP_ENABLED)
4149 goto out;
4150
4151 } else {
4152 /* When status is not DISABLED then DCBX in FW */
4153 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
4154 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00004155
4156 pf->flags |= I40E_FLAG_DCB_CAPABLE;
4157 /* Enable DCB tagging only when more than one TC */
4158 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
4159 pf->flags |= I40E_FLAG_DCB_ENABLED;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004160 }
Neerav Parikh014269f2014-04-01 07:11:48 +00004161 } else {
4162 dev_info(&pf->pdev->dev, "AQ Querying DCB configuration failed: %d\n",
4163 pf->hw.aq.asq_last_status);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004164 }
4165
4166out:
4167 return err;
4168}
4169#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004170#define SPEED_SIZE 14
4171#define FC_SIZE 8
4172/**
4173 * i40e_print_link_message - print link up or down
4174 * @vsi: the VSI for which link needs a message
4175 */
4176static void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
4177{
4178 char speed[SPEED_SIZE] = "Unknown";
4179 char fc[FC_SIZE] = "RX/TX";
4180
4181 if (!isup) {
4182 netdev_info(vsi->netdev, "NIC Link is Down\n");
4183 return;
4184 }
4185
4186 switch (vsi->back->hw.phy.link_info.link_speed) {
4187 case I40E_LINK_SPEED_40GB:
4188 strncpy(speed, "40 Gbps", SPEED_SIZE);
4189 break;
4190 case I40E_LINK_SPEED_10GB:
4191 strncpy(speed, "10 Gbps", SPEED_SIZE);
4192 break;
4193 case I40E_LINK_SPEED_1GB:
4194 strncpy(speed, "1000 Mbps", SPEED_SIZE);
4195 break;
4196 default:
4197 break;
4198 }
4199
4200 switch (vsi->back->hw.fc.current_mode) {
4201 case I40E_FC_FULL:
4202 strncpy(fc, "RX/TX", FC_SIZE);
4203 break;
4204 case I40E_FC_TX_PAUSE:
4205 strncpy(fc, "TX", FC_SIZE);
4206 break;
4207 case I40E_FC_RX_PAUSE:
4208 strncpy(fc, "RX", FC_SIZE);
4209 break;
4210 default:
4211 strncpy(fc, "None", FC_SIZE);
4212 break;
4213 }
4214
4215 netdev_info(vsi->netdev, "NIC Link is Up %s Full Duplex, Flow Control: %s\n",
4216 speed, fc);
4217}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004218
4219/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004220 * i40e_up_complete - Finish the last steps of bringing up a connection
4221 * @vsi: the VSI being configured
4222 **/
4223static int i40e_up_complete(struct i40e_vsi *vsi)
4224{
4225 struct i40e_pf *pf = vsi->back;
4226 int err;
4227
4228 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4229 i40e_vsi_configure_msix(vsi);
4230 else
4231 i40e_configure_msi_and_legacy(vsi);
4232
4233 /* start rings */
4234 err = i40e_vsi_control_rings(vsi, true);
4235 if (err)
4236 return err;
4237
4238 clear_bit(__I40E_DOWN, &vsi->state);
4239 i40e_napi_enable_all(vsi);
4240 i40e_vsi_enable_irq(vsi);
4241
4242 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
4243 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004244 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004245 netif_tx_start_all_queues(vsi->netdev);
4246 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00004247 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004248 i40e_print_link_message(vsi, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004249 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00004250
4251 /* replay FDIR SB filters */
4252 if (vsi->type == I40E_VSI_FDIR)
4253 i40e_fdir_filter_restore(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004254 i40e_service_event_schedule(pf);
4255
4256 return 0;
4257}
4258
4259/**
4260 * i40e_vsi_reinit_locked - Reset the VSI
4261 * @vsi: the VSI being configured
4262 *
4263 * Rebuild the ring structs after some configuration
4264 * has changed, e.g. MTU size.
4265 **/
4266static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
4267{
4268 struct i40e_pf *pf = vsi->back;
4269
4270 WARN_ON(in_interrupt());
4271 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
4272 usleep_range(1000, 2000);
4273 i40e_down(vsi);
4274
4275 /* Give a VF some time to respond to the reset. The
4276 * two second wait is based upon the watchdog cycle in
4277 * the VF driver.
4278 */
4279 if (vsi->type == I40E_VSI_SRIOV)
4280 msleep(2000);
4281 i40e_up(vsi);
4282 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
4283}
4284
4285/**
4286 * i40e_up - Bring the connection back up after being down
4287 * @vsi: the VSI being configured
4288 **/
4289int i40e_up(struct i40e_vsi *vsi)
4290{
4291 int err;
4292
4293 err = i40e_vsi_configure(vsi);
4294 if (!err)
4295 err = i40e_up_complete(vsi);
4296
4297 return err;
4298}
4299
4300/**
4301 * i40e_down - Shutdown the connection processing
4302 * @vsi: the VSI being stopped
4303 **/
4304void i40e_down(struct i40e_vsi *vsi)
4305{
4306 int i;
4307
4308 /* It is assumed that the caller of this function
4309 * sets the vsi->state __I40E_DOWN bit.
4310 */
4311 if (vsi->netdev) {
4312 netif_carrier_off(vsi->netdev);
4313 netif_tx_disable(vsi->netdev);
4314 }
4315 i40e_vsi_disable_irq(vsi);
4316 i40e_vsi_control_rings(vsi, false);
4317 i40e_napi_disable_all(vsi);
4318
4319 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004320 i40e_clean_tx_ring(vsi->tx_rings[i]);
4321 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004322 }
4323}
4324
4325/**
4326 * i40e_setup_tc - configure multiple traffic classes
4327 * @netdev: net device to configure
4328 * @tc: number of traffic classes to enable
4329 **/
4330static int i40e_setup_tc(struct net_device *netdev, u8 tc)
4331{
4332 struct i40e_netdev_priv *np = netdev_priv(netdev);
4333 struct i40e_vsi *vsi = np->vsi;
4334 struct i40e_pf *pf = vsi->back;
4335 u8 enabled_tc = 0;
4336 int ret = -EINVAL;
4337 int i;
4338
4339 /* Check if DCB enabled to continue */
4340 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
4341 netdev_info(netdev, "DCB is not enabled for adapter\n");
4342 goto exit;
4343 }
4344
4345 /* Check if MFP enabled */
4346 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
4347 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
4348 goto exit;
4349 }
4350
4351 /* Check whether tc count is within enabled limit */
4352 if (tc > i40e_pf_get_num_tc(pf)) {
4353 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
4354 goto exit;
4355 }
4356
4357 /* Generate TC map for number of tc requested */
4358 for (i = 0; i < tc; i++)
4359 enabled_tc |= (1 << i);
4360
4361 /* Requesting same TC configuration as already enabled */
4362 if (enabled_tc == vsi->tc_config.enabled_tc)
4363 return 0;
4364
4365 /* Quiesce VSI queues */
4366 i40e_quiesce_vsi(vsi);
4367
4368 /* Configure VSI for enabled TCs */
4369 ret = i40e_vsi_config_tc(vsi, enabled_tc);
4370 if (ret) {
4371 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
4372 vsi->seid);
4373 goto exit;
4374 }
4375
4376 /* Unquiesce VSI */
4377 i40e_unquiesce_vsi(vsi);
4378
4379exit:
4380 return ret;
4381}
4382
4383/**
4384 * i40e_open - Called when a network interface is made active
4385 * @netdev: network interface device structure
4386 *
4387 * The open entry point is called when a network interface is made
4388 * active by the system (IFF_UP). At this point all resources needed
4389 * for transmit and receive operations are allocated, the interrupt
4390 * handler is registered with the OS, the netdev watchdog subtask is
4391 * enabled, and the stack is notified that the interface is ready.
4392 *
4393 * Returns 0 on success, negative value on failure
4394 **/
4395static int i40e_open(struct net_device *netdev)
4396{
4397 struct i40e_netdev_priv *np = netdev_priv(netdev);
4398 struct i40e_vsi *vsi = np->vsi;
4399 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004400 int err;
4401
Shannon Nelson4eb3f762014-03-06 08:59:58 +00004402 /* disallow open during test or if eeprom is broken */
4403 if (test_bit(__I40E_TESTING, &pf->state) ||
4404 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004405 return -EBUSY;
4406
4407 netif_carrier_off(netdev);
4408
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004409 err = i40e_vsi_open(vsi);
4410 if (err)
4411 return err;
4412
Jesse Brandeburg059dab62014-04-01 09:07:20 +00004413 /* configure global TSO hardware offload settings */
4414 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
4415 TCP_FLAG_FIN) >> 16);
4416 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
4417 TCP_FLAG_FIN |
4418 TCP_FLAG_CWR) >> 16);
4419 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
4420
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004421#ifdef CONFIG_I40E_VXLAN
4422 vxlan_get_rx_port(netdev);
4423#endif
4424
4425 return 0;
4426}
4427
4428/**
4429 * i40e_vsi_open -
4430 * @vsi: the VSI to open
4431 *
4432 * Finish initialization of the VSI.
4433 *
4434 * Returns 0 on success, negative value on failure
4435 **/
4436int i40e_vsi_open(struct i40e_vsi *vsi)
4437{
4438 struct i40e_pf *pf = vsi->back;
4439 char int_name[IFNAMSIZ];
4440 int err;
4441
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004442 /* allocate descriptors */
4443 err = i40e_vsi_setup_tx_resources(vsi);
4444 if (err)
4445 goto err_setup_tx;
4446 err = i40e_vsi_setup_rx_resources(vsi);
4447 if (err)
4448 goto err_setup_rx;
4449
4450 err = i40e_vsi_configure(vsi);
4451 if (err)
4452 goto err_setup_rx;
4453
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00004454 if (vsi->netdev) {
4455 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
4456 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
4457 err = i40e_vsi_request_irq(vsi, int_name);
4458 if (err)
4459 goto err_setup_rx;
4460
4461 /* Notify the stack of the actual queue counts. */
4462 err = netif_set_real_num_tx_queues(vsi->netdev,
4463 vsi->num_queue_pairs);
4464 if (err)
4465 goto err_set_queues;
4466
4467 err = netif_set_real_num_rx_queues(vsi->netdev,
4468 vsi->num_queue_pairs);
4469 if (err)
4470 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00004471
4472 } else if (vsi->type == I40E_VSI_FDIR) {
4473 snprintf(int_name, sizeof(int_name) - 1, "%s-fdir",
4474 dev_driver_string(&pf->pdev->dev));
4475 err = i40e_vsi_request_irq(vsi, int_name);
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00004476 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00004477 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00004478 goto err_setup_rx;
4479 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004480
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004481 err = i40e_up_complete(vsi);
4482 if (err)
4483 goto err_up_complete;
4484
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004485 return 0;
4486
4487err_up_complete:
4488 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004489err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004490 i40e_vsi_free_irq(vsi);
4491err_setup_rx:
4492 i40e_vsi_free_rx_resources(vsi);
4493err_setup_tx:
4494 i40e_vsi_free_tx_resources(vsi);
4495 if (vsi == pf->vsi[pf->lan_vsi])
4496 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
4497
4498 return err;
4499}
4500
4501/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00004502 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
4503 * @pf: Pointer to pf
4504 *
4505 * This function destroys the hlist where all the Flow Director
4506 * filters were saved.
4507 **/
4508static void i40e_fdir_filter_exit(struct i40e_pf *pf)
4509{
4510 struct i40e_fdir_filter *filter;
4511 struct hlist_node *node2;
4512
4513 hlist_for_each_entry_safe(filter, node2,
4514 &pf->fdir_filter_list, fdir_node) {
4515 hlist_del(&filter->fdir_node);
4516 kfree(filter);
4517 }
4518 pf->fdir_pf_active_filters = 0;
4519}
4520
4521/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004522 * i40e_close - Disables a network interface
4523 * @netdev: network interface device structure
4524 *
4525 * The close entry point is called when an interface is de-activated
4526 * by the OS. The hardware is still under the driver's control, but
4527 * this netdev interface is disabled.
4528 *
4529 * Returns 0, this is not allowed to fail
4530 **/
4531static int i40e_close(struct net_device *netdev)
4532{
4533 struct i40e_netdev_priv *np = netdev_priv(netdev);
4534 struct i40e_vsi *vsi = np->vsi;
4535
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004536 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004537
4538 return 0;
4539}
4540
4541/**
4542 * i40e_do_reset - Start a PF or Core Reset sequence
4543 * @pf: board private structure
4544 * @reset_flags: which reset is requested
4545 *
4546 * The essential difference in resets is that the PF Reset
4547 * doesn't clear the packet buffers, doesn't reset the PE
4548 * firmware, and doesn't bother the other PFs on the chip.
4549 **/
4550void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
4551{
4552 u32 val;
4553
4554 WARN_ON(in_interrupt());
4555
Mitch Williams263fc482014-04-23 04:50:11 +00004556 if (i40e_check_asq_alive(&pf->hw))
4557 i40e_vc_notify_reset(pf);
4558
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004559 /* do the biggest reset indicated */
4560 if (reset_flags & (1 << __I40E_GLOBAL_RESET_REQUESTED)) {
4561
4562 /* Request a Global Reset
4563 *
4564 * This will start the chip's countdown to the actual full
4565 * chip reset event, and a warning interrupt to be sent
4566 * to all PFs, including the requestor. Our handler
4567 * for the warning interrupt will deal with the shutdown
4568 * and recovery of the switch setup.
4569 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004570 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004571 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4572 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
4573 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4574
4575 } else if (reset_flags & (1 << __I40E_CORE_RESET_REQUESTED)) {
4576
4577 /* Request a Core Reset
4578 *
4579 * Same as Global Reset, except does *not* include the MAC/PHY
4580 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004581 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004582 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4583 val |= I40E_GLGEN_RTRIG_CORER_MASK;
4584 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4585 i40e_flush(&pf->hw);
4586
Shannon Nelson7823fe32013-11-16 10:00:45 +00004587 } else if (reset_flags & (1 << __I40E_EMP_RESET_REQUESTED)) {
4588
4589 /* Request a Firmware Reset
4590 *
4591 * Same as Global reset, plus restarting the
4592 * embedded firmware engine.
4593 */
4594 /* enable EMP Reset */
4595 val = rd32(&pf->hw, I40E_GLGEN_RSTENA_EMP);
4596 val |= I40E_GLGEN_RSTENA_EMP_EMP_RST_ENA_MASK;
4597 wr32(&pf->hw, I40E_GLGEN_RSTENA_EMP, val);
4598
4599 /* force the reset */
4600 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4601 val |= I40E_GLGEN_RTRIG_EMPFWR_MASK;
4602 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4603 i40e_flush(&pf->hw);
4604
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004605 } else if (reset_flags & (1 << __I40E_PF_RESET_REQUESTED)) {
4606
4607 /* Request a PF Reset
4608 *
4609 * Resets only the PF-specific registers
4610 *
4611 * This goes directly to the tear-down and rebuild of
4612 * the switch, since we need to do all the recovery as
4613 * for the Core Reset.
4614 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004615 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004616 i40e_handle_reset_warning(pf);
4617
4618 } else if (reset_flags & (1 << __I40E_REINIT_REQUESTED)) {
4619 int v;
4620
4621 /* Find the VSI(s) that requested a re-init */
4622 dev_info(&pf->pdev->dev,
4623 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00004624 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004625 struct i40e_vsi *vsi = pf->vsi[v];
4626 if (vsi != NULL &&
4627 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
4628 i40e_vsi_reinit_locked(pf->vsi[v]);
4629 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
4630 }
4631 }
4632
4633 /* no further action needed, so return now */
4634 return;
4635 } else {
4636 dev_info(&pf->pdev->dev,
4637 "bad reset request 0x%08x\n", reset_flags);
4638 return;
4639 }
4640}
4641
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004642#ifdef CONFIG_I40E_DCB
4643/**
4644 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
4645 * @pf: board private structure
4646 * @old_cfg: current DCB config
4647 * @new_cfg: new DCB config
4648 **/
4649bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
4650 struct i40e_dcbx_config *old_cfg,
4651 struct i40e_dcbx_config *new_cfg)
4652{
4653 bool need_reconfig = false;
4654
4655 /* Check if ETS configuration has changed */
4656 if (memcmp(&new_cfg->etscfg,
4657 &old_cfg->etscfg,
4658 sizeof(new_cfg->etscfg))) {
4659 /* If Priority Table has changed reconfig is needed */
4660 if (memcmp(&new_cfg->etscfg.prioritytable,
4661 &old_cfg->etscfg.prioritytable,
4662 sizeof(new_cfg->etscfg.prioritytable))) {
4663 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004664 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004665 }
4666
4667 if (memcmp(&new_cfg->etscfg.tcbwtable,
4668 &old_cfg->etscfg.tcbwtable,
4669 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004670 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004671
4672 if (memcmp(&new_cfg->etscfg.tsatable,
4673 &old_cfg->etscfg.tsatable,
4674 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004675 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004676 }
4677
4678 /* Check if PFC configuration has changed */
4679 if (memcmp(&new_cfg->pfc,
4680 &old_cfg->pfc,
4681 sizeof(new_cfg->pfc))) {
4682 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004683 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004684 }
4685
4686 /* Check if APP Table has changed */
4687 if (memcmp(&new_cfg->app,
4688 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05004689 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004690 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004691 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05004692 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004693
4694 return need_reconfig;
4695}
4696
4697/**
4698 * i40e_handle_lldp_event - Handle LLDP Change MIB event
4699 * @pf: board private structure
4700 * @e: event info posted on ARQ
4701 **/
4702static int i40e_handle_lldp_event(struct i40e_pf *pf,
4703 struct i40e_arq_event_info *e)
4704{
4705 struct i40e_aqc_lldp_get_mib *mib =
4706 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
4707 struct i40e_hw *hw = &pf->hw;
4708 struct i40e_dcbx_config *dcbx_cfg = &hw->local_dcbx_config;
4709 struct i40e_dcbx_config tmp_dcbx_cfg;
4710 bool need_reconfig = false;
4711 int ret = 0;
4712 u8 type;
4713
Neerav Parikh4d9b6042014-05-22 06:31:51 +00004714 /* Not DCB capable or capability disabled */
4715 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
4716 return ret;
4717
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004718 /* Ignore if event is not for Nearest Bridge */
4719 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
4720 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
4721 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
4722 return ret;
4723
4724 /* Check MIB Type and return if event for Remote MIB update */
4725 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
4726 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
4727 /* Update the remote cached instance and return */
4728 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
4729 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
4730 &hw->remote_dcbx_config);
4731 goto exit;
4732 }
4733
4734 /* Convert/store the DCBX data from LLDPDU temporarily */
4735 memset(&tmp_dcbx_cfg, 0, sizeof(tmp_dcbx_cfg));
4736 ret = i40e_lldp_to_dcb_config(e->msg_buf, &tmp_dcbx_cfg);
4737 if (ret) {
4738 /* Error in LLDPDU parsing return */
4739 dev_info(&pf->pdev->dev, "Failed parsing LLDPDU from event buffer\n");
4740 goto exit;
4741 }
4742
4743 /* No change detected in DCBX configs */
4744 if (!memcmp(&tmp_dcbx_cfg, dcbx_cfg, sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004745 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004746 goto exit;
4747 }
4748
4749 need_reconfig = i40e_dcb_need_reconfig(pf, dcbx_cfg, &tmp_dcbx_cfg);
4750
4751 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg);
4752
4753 /* Overwrite the new configuration */
4754 *dcbx_cfg = tmp_dcbx_cfg;
4755
4756 if (!need_reconfig)
4757 goto exit;
4758
Neerav Parikh4d9b6042014-05-22 06:31:51 +00004759 /* Enable DCB tagging only when more than one TC */
4760 if (i40e_dcb_get_num_tc(dcbx_cfg) > 1)
4761 pf->flags |= I40E_FLAG_DCB_ENABLED;
4762 else
4763 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
4764
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004765 /* Reconfiguration needed quiesce all VSIs */
4766 i40e_pf_quiesce_all_vsi(pf);
4767
4768 /* Changes in configuration update VEB/VSI */
4769 i40e_dcb_reconfigure(pf);
4770
4771 i40e_pf_unquiesce_all_vsi(pf);
4772exit:
4773 return ret;
4774}
4775#endif /* CONFIG_I40E_DCB */
4776
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004777/**
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004778 * i40e_do_reset_safe - Protected reset path for userland calls.
4779 * @pf: board private structure
4780 * @reset_flags: which reset is requested
4781 *
4782 **/
4783void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
4784{
4785 rtnl_lock();
4786 i40e_do_reset(pf, reset_flags);
4787 rtnl_unlock();
4788}
4789
4790/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004791 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
4792 * @pf: board private structure
4793 * @e: event info posted on ARQ
4794 *
4795 * Handler for LAN Queue Overflow Event generated by the firmware for PF
4796 * and VF queues
4797 **/
4798static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
4799 struct i40e_arq_event_info *e)
4800{
4801 struct i40e_aqc_lan_overflow *data =
4802 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
4803 u32 queue = le32_to_cpu(data->prtdcb_rupto);
4804 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
4805 struct i40e_hw *hw = &pf->hw;
4806 struct i40e_vf *vf;
4807 u16 vf_id;
4808
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004809 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
4810 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004811
4812 /* Queue belongs to VF, find the VF and issue VF reset */
4813 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
4814 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
4815 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
4816 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
4817 vf_id -= hw->func_caps.vf_base_id;
4818 vf = &pf->vf[vf_id];
4819 i40e_vc_notify_vf_reset(vf);
4820 /* Allow VF to process pending reset notification */
4821 msleep(20);
4822 i40e_reset_vf(vf, false);
4823 }
4824}
4825
4826/**
4827 * i40e_service_event_complete - Finish up the service event
4828 * @pf: board private structure
4829 **/
4830static void i40e_service_event_complete(struct i40e_pf *pf)
4831{
4832 BUG_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
4833
4834 /* flush memory to make sure state is correct before next watchog */
4835 smp_mb__before_clear_bit();
4836 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
4837}
4838
4839/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004840 * i40e_get_current_fd_count - Get the count of FD filters programmed in the HW
4841 * @pf: board private structure
4842 **/
4843int i40e_get_current_fd_count(struct i40e_pf *pf)
4844{
4845 int val, fcnt_prog;
4846 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
4847 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
4848 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
4849 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
4850 return fcnt_prog;
4851}
4852
4853/**
4854 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
4855 * @pf: board private structure
4856 **/
4857void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
4858{
4859 u32 fcnt_prog, fcnt_avail;
4860
4861 /* Check if, FD SB or ATR was auto disabled and if there is enough room
4862 * to re-enable
4863 */
4864 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4865 (pf->flags & I40E_FLAG_FD_SB_ENABLED))
4866 return;
4867 fcnt_prog = i40e_get_current_fd_count(pf);
Anjali Singhai Jain89132782014-04-09 05:59:01 +00004868 fcnt_avail = i40e_get_fd_cnt_all(pf);
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004869 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) {
4870 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
4871 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
4872 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
4873 dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
4874 }
4875 }
4876 /* Wait for some more space to be available to turn on ATR */
4877 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
4878 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4879 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
4880 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
4881 dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table now\n");
4882 }
4883 }
4884}
4885
4886/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004887 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
4888 * @pf: board private structure
4889 **/
4890static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
4891{
4892 if (!(pf->flags & I40E_FLAG_FDIR_REQUIRES_REINIT))
4893 return;
4894
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004895 /* if interface is down do nothing */
4896 if (test_bit(__I40E_DOWN, &pf->state))
4897 return;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004898 i40e_fdir_check_and_reenable(pf);
4899
4900 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4901 (pf->flags & I40E_FLAG_FD_SB_ENABLED))
4902 pf->flags &= ~I40E_FLAG_FDIR_REQUIRES_REINIT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004903}
4904
4905/**
4906 * i40e_vsi_link_event - notify VSI of a link event
4907 * @vsi: vsi to be notified
4908 * @link_up: link up or down
4909 **/
4910static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
4911{
4912 if (!vsi)
4913 return;
4914
4915 switch (vsi->type) {
4916 case I40E_VSI_MAIN:
4917 if (!vsi->netdev || !vsi->netdev_registered)
4918 break;
4919
4920 if (link_up) {
4921 netif_carrier_on(vsi->netdev);
4922 netif_tx_wake_all_queues(vsi->netdev);
4923 } else {
4924 netif_carrier_off(vsi->netdev);
4925 netif_tx_stop_all_queues(vsi->netdev);
4926 }
4927 break;
4928
4929 case I40E_VSI_SRIOV:
4930 break;
4931
4932 case I40E_VSI_VMDQ2:
4933 case I40E_VSI_CTRL:
4934 case I40E_VSI_MIRROR:
4935 default:
4936 /* there is no notification for other VSIs */
4937 break;
4938 }
4939}
4940
4941/**
4942 * i40e_veb_link_event - notify elements on the veb of a link event
4943 * @veb: veb to be notified
4944 * @link_up: link up or down
4945 **/
4946static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
4947{
4948 struct i40e_pf *pf;
4949 int i;
4950
4951 if (!veb || !veb->pf)
4952 return;
4953 pf = veb->pf;
4954
4955 /* depth first... */
4956 for (i = 0; i < I40E_MAX_VEB; i++)
4957 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
4958 i40e_veb_link_event(pf->veb[i], link_up);
4959
4960 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00004961 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004962 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
4963 i40e_vsi_link_event(pf->vsi[i], link_up);
4964}
4965
4966/**
4967 * i40e_link_event - Update netif_carrier status
4968 * @pf: board private structure
4969 **/
4970static void i40e_link_event(struct i40e_pf *pf)
4971{
4972 bool new_link, old_link;
4973
4974 new_link = (pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP);
4975 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
4976
4977 if (new_link == old_link)
4978 return;
Anjali Singhai6d779b42013-09-28 06:00:02 +00004979 if (!test_bit(__I40E_DOWN, &pf->vsi[pf->lan_vsi]->state))
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00004980 i40e_print_link_message(pf->vsi[pf->lan_vsi], new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004981
4982 /* Notify the base of the switch tree connected to
4983 * the link. Floating VEBs are not notified.
4984 */
4985 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
4986 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
4987 else
4988 i40e_vsi_link_event(pf->vsi[pf->lan_vsi], new_link);
4989
4990 if (pf->vf)
4991 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00004992
4993 if (pf->flags & I40E_FLAG_PTP)
4994 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004995}
4996
4997/**
4998 * i40e_check_hang_subtask - Check for hung queues and dropped interrupts
4999 * @pf: board private structure
5000 *
5001 * Set the per-queue flags to request a check for stuck queues in the irq
5002 * clean functions, then force interrupts to be sure the irq clean is called.
5003 **/
5004static void i40e_check_hang_subtask(struct i40e_pf *pf)
5005{
5006 int i, v;
5007
5008 /* If we're down or resetting, just bail */
5009 if (test_bit(__I40E_CONFIG_BUSY, &pf->state))
5010 return;
5011
5012 /* for each VSI/netdev
5013 * for each Tx queue
5014 * set the check flag
5015 * for each q_vector
5016 * force an interrupt
5017 */
Mitch Williams505682c2014-05-20 08:01:37 +00005018 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005019 struct i40e_vsi *vsi = pf->vsi[v];
5020 int armed = 0;
5021
5022 if (!pf->vsi[v] ||
5023 test_bit(__I40E_DOWN, &vsi->state) ||
5024 (vsi->netdev && !netif_carrier_ok(vsi->netdev)))
5025 continue;
5026
5027 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005028 set_check_for_tx_hang(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005029 if (test_bit(__I40E_HANG_CHECK_ARMED,
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005030 &vsi->tx_rings[i]->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005031 armed++;
5032 }
5033
5034 if (armed) {
5035 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
5036 wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0,
5037 (I40E_PFINT_DYN_CTL0_INTENA_MASK |
5038 I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK));
5039 } else {
5040 u16 vec = vsi->base_vector - 1;
5041 u32 val = (I40E_PFINT_DYN_CTLN_INTENA_MASK |
5042 I40E_PFINT_DYN_CTLN_SWINT_TRIG_MASK);
5043 for (i = 0; i < vsi->num_q_vectors; i++, vec++)
5044 wr32(&vsi->back->hw,
5045 I40E_PFINT_DYN_CTLN(vec), val);
5046 }
5047 i40e_flush(&vsi->back->hw);
5048 }
5049 }
5050}
5051
5052/**
5053 * i40e_watchdog_subtask - Check and bring link up
5054 * @pf: board private structure
5055 **/
5056static void i40e_watchdog_subtask(struct i40e_pf *pf)
5057{
5058 int i;
5059
5060 /* if interface is down do nothing */
5061 if (test_bit(__I40E_DOWN, &pf->state) ||
5062 test_bit(__I40E_CONFIG_BUSY, &pf->state))
5063 return;
5064
5065 /* Update the stats for active netdevs so the network stack
5066 * can look at updated numbers whenever it cares to
5067 */
Mitch Williams505682c2014-05-20 08:01:37 +00005068 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005069 if (pf->vsi[i] && pf->vsi[i]->netdev)
5070 i40e_update_stats(pf->vsi[i]);
5071
5072 /* Update the stats for the active switching components */
5073 for (i = 0; i < I40E_MAX_VEB; i++)
5074 if (pf->veb[i])
5075 i40e_update_veb_stats(pf->veb[i]);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00005076
5077 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005078}
5079
5080/**
5081 * i40e_reset_subtask - Set up for resetting the device and driver
5082 * @pf: board private structure
5083 **/
5084static void i40e_reset_subtask(struct i40e_pf *pf)
5085{
5086 u32 reset_flags = 0;
5087
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005088 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005089 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
5090 reset_flags |= (1 << __I40E_REINIT_REQUESTED);
5091 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
5092 }
5093 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
5094 reset_flags |= (1 << __I40E_PF_RESET_REQUESTED);
5095 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5096 }
5097 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
5098 reset_flags |= (1 << __I40E_CORE_RESET_REQUESTED);
5099 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
5100 }
5101 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
5102 reset_flags |= (1 << __I40E_GLOBAL_RESET_REQUESTED);
5103 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
5104 }
5105
5106 /* If there's a recovery already waiting, it takes
5107 * precedence before starting a new reset sequence.
5108 */
5109 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
5110 i40e_handle_reset_warning(pf);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005111 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005112 }
5113
5114 /* If we're already down or resetting, just bail */
5115 if (reset_flags &&
5116 !test_bit(__I40E_DOWN, &pf->state) &&
5117 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
5118 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005119
5120unlock:
5121 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005122}
5123
5124/**
5125 * i40e_handle_link_event - Handle link event
5126 * @pf: board private structure
5127 * @e: event info posted on ARQ
5128 **/
5129static void i40e_handle_link_event(struct i40e_pf *pf,
5130 struct i40e_arq_event_info *e)
5131{
5132 struct i40e_hw *hw = &pf->hw;
5133 struct i40e_aqc_get_link_status *status =
5134 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
5135 struct i40e_link_status *hw_link_info = &hw->phy.link_info;
5136
5137 /* save off old link status information */
5138 memcpy(&pf->hw.phy.link_info_old, hw_link_info,
5139 sizeof(pf->hw.phy.link_info_old));
5140
5141 /* update link status */
5142 hw_link_info->phy_type = (enum i40e_aq_phy_type)status->phy_type;
5143 hw_link_info->link_speed = (enum i40e_aq_link_speed)status->link_speed;
5144 hw_link_info->link_info = status->link_info;
5145 hw_link_info->an_info = status->an_info;
5146 hw_link_info->ext_info = status->ext_info;
5147 hw_link_info->lse_enable =
5148 le16_to_cpu(status->command_flags) &
5149 I40E_AQ_LSE_ENABLE;
5150
5151 /* process the event */
5152 i40e_link_event(pf);
5153
5154 /* Do a new status request to re-enable LSE reporting
5155 * and load new status information into the hw struct,
5156 * then see if the status changed while processing the
5157 * initial event.
5158 */
5159 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
5160 i40e_link_event(pf);
5161}
5162
5163/**
5164 * i40e_clean_adminq_subtask - Clean the AdminQ rings
5165 * @pf: board private structure
5166 **/
5167static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
5168{
5169 struct i40e_arq_event_info event;
5170 struct i40e_hw *hw = &pf->hw;
5171 u16 pending, i = 0;
5172 i40e_status ret;
5173 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00005174 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005175 u32 val;
5176
5177 if (!test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state))
5178 return;
5179
Shannon Nelson86df2422014-05-20 08:01:35 +00005180 /* check for error indications */
5181 val = rd32(&pf->hw, pf->hw.aq.arq.len);
5182 oldval = val;
5183 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
5184 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
5185 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
5186 }
5187 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
5188 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
5189 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
5190 }
5191 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
5192 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
5193 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
5194 }
5195 if (oldval != val)
5196 wr32(&pf->hw, pf->hw.aq.arq.len, val);
5197
5198 val = rd32(&pf->hw, pf->hw.aq.asq.len);
5199 oldval = val;
5200 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
5201 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
5202 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
5203 }
5204 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
5205 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
5206 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
5207 }
5208 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
5209 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
5210 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
5211 }
5212 if (oldval != val)
5213 wr32(&pf->hw, pf->hw.aq.asq.len, val);
5214
Mitch Williams3197ce22013-11-28 06:39:39 +00005215 event.msg_size = I40E_MAX_AQ_BUF_SIZE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005216 event.msg_buf = kzalloc(event.msg_size, GFP_KERNEL);
5217 if (!event.msg_buf)
5218 return;
5219
5220 do {
Mitch Williams2f019122013-11-28 06:39:33 +00005221 event.msg_size = I40E_MAX_AQ_BUF_SIZE; /* reinit each time */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005222 ret = i40e_clean_arq_element(hw, &event, &pending);
5223 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
5224 dev_info(&pf->pdev->dev, "No ARQ event found\n");
5225 break;
5226 } else if (ret) {
5227 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
5228 break;
5229 }
5230
5231 opcode = le16_to_cpu(event.desc.opcode);
5232 switch (opcode) {
5233
5234 case i40e_aqc_opc_get_link_status:
5235 i40e_handle_link_event(pf, &event);
5236 break;
5237 case i40e_aqc_opc_send_msg_to_pf:
5238 ret = i40e_vc_process_vf_msg(pf,
5239 le16_to_cpu(event.desc.retval),
5240 le32_to_cpu(event.desc.cookie_high),
5241 le32_to_cpu(event.desc.cookie_low),
5242 event.msg_buf,
5243 event.msg_size);
5244 break;
5245 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005246 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005247#ifdef CONFIG_I40E_DCB
5248 rtnl_lock();
5249 ret = i40e_handle_lldp_event(pf, &event);
5250 rtnl_unlock();
5251#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005252 break;
5253 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005254 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005255 i40e_handle_lan_overflow_event(pf, &event);
5256 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00005257 case i40e_aqc_opc_send_msg_to_peer:
5258 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
5259 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005260 default:
5261 dev_info(&pf->pdev->dev,
Shannon Nelson0467bc92013-12-18 13:45:58 +00005262 "ARQ Error: Unknown event 0x%04x received\n",
5263 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005264 break;
5265 }
5266 } while (pending && (i++ < pf->adminq_work_limit));
5267
5268 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
5269 /* re-enable Admin queue interrupt cause */
5270 val = rd32(hw, I40E_PFINT_ICR0_ENA);
5271 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
5272 wr32(hw, I40E_PFINT_ICR0_ENA, val);
5273 i40e_flush(hw);
5274
5275 kfree(event.msg_buf);
5276}
5277
5278/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005279 * i40e_verify_eeprom - make sure eeprom is good to use
5280 * @pf: board private structure
5281 **/
5282static void i40e_verify_eeprom(struct i40e_pf *pf)
5283{
5284 int err;
5285
5286 err = i40e_diag_eeprom_test(&pf->hw);
5287 if (err) {
5288 /* retry in case of garbage read */
5289 err = i40e_diag_eeprom_test(&pf->hw);
5290 if (err) {
5291 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
5292 err);
5293 set_bit(__I40E_BAD_EEPROM, &pf->state);
5294 }
5295 }
5296
5297 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
5298 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
5299 clear_bit(__I40E_BAD_EEPROM, &pf->state);
5300 }
5301}
5302
5303/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005304 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
5305 * @veb: pointer to the VEB instance
5306 *
5307 * This is a recursive function that first builds the attached VSIs then
5308 * recurses in to build the next layer of VEB. We track the connections
5309 * through our own index numbers because the seid's from the HW could
5310 * change across the reset.
5311 **/
5312static int i40e_reconstitute_veb(struct i40e_veb *veb)
5313{
5314 struct i40e_vsi *ctl_vsi = NULL;
5315 struct i40e_pf *pf = veb->pf;
5316 int v, veb_idx;
5317 int ret;
5318
5319 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00005320 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005321 if (pf->vsi[v] &&
5322 pf->vsi[v]->veb_idx == veb->idx &&
5323 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
5324 ctl_vsi = pf->vsi[v];
5325 break;
5326 }
5327 }
5328 if (!ctl_vsi) {
5329 dev_info(&pf->pdev->dev,
5330 "missing owner VSI for veb_idx %d\n", veb->idx);
5331 ret = -ENOENT;
5332 goto end_reconstitute;
5333 }
5334 if (ctl_vsi != pf->vsi[pf->lan_vsi])
5335 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
5336 ret = i40e_add_vsi(ctl_vsi);
5337 if (ret) {
5338 dev_info(&pf->pdev->dev,
5339 "rebuild of owner VSI failed: %d\n", ret);
5340 goto end_reconstitute;
5341 }
5342 i40e_vsi_reset_stats(ctl_vsi);
5343
5344 /* create the VEB in the switch and move the VSI onto the VEB */
5345 ret = i40e_add_veb(veb, ctl_vsi);
5346 if (ret)
5347 goto end_reconstitute;
5348
5349 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00005350 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005351 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
5352 continue;
5353
5354 if (pf->vsi[v]->veb_idx == veb->idx) {
5355 struct i40e_vsi *vsi = pf->vsi[v];
5356 vsi->uplink_seid = veb->seid;
5357 ret = i40e_add_vsi(vsi);
5358 if (ret) {
5359 dev_info(&pf->pdev->dev,
5360 "rebuild of vsi_idx %d failed: %d\n",
5361 v, ret);
5362 goto end_reconstitute;
5363 }
5364 i40e_vsi_reset_stats(vsi);
5365 }
5366 }
5367
5368 /* create any VEBs attached to this VEB - RECURSION */
5369 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
5370 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
5371 pf->veb[veb_idx]->uplink_seid = veb->seid;
5372 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
5373 if (ret)
5374 break;
5375 }
5376 }
5377
5378end_reconstitute:
5379 return ret;
5380}
5381
5382/**
5383 * i40e_get_capabilities - get info about the HW
5384 * @pf: the PF struct
5385 **/
5386static int i40e_get_capabilities(struct i40e_pf *pf)
5387{
5388 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
5389 u16 data_size;
5390 int buf_len;
5391 int err;
5392
5393 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
5394 do {
5395 cap_buf = kzalloc(buf_len, GFP_KERNEL);
5396 if (!cap_buf)
5397 return -ENOMEM;
5398
5399 /* this loads the data into the hw struct for us */
5400 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
5401 &data_size,
5402 i40e_aqc_opc_list_func_capabilities,
5403 NULL);
5404 /* data loaded, buffer no longer needed */
5405 kfree(cap_buf);
5406
5407 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
5408 /* retry with a larger buffer */
5409 buf_len = data_size;
5410 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
5411 dev_info(&pf->pdev->dev,
5412 "capability discovery failed: aq=%d\n",
5413 pf->hw.aq.asq_last_status);
5414 return -ENODEV;
5415 }
5416 } while (err);
5417
Anjali Singhai Jainac71b7b2014-02-06 05:51:08 +00005418 if (((pf->hw.aq.fw_maj_ver == 2) && (pf->hw.aq.fw_min_ver < 22)) ||
5419 (pf->hw.aq.fw_maj_ver < 2)) {
5420 pf->hw.func_caps.num_msix_vectors++;
5421 pf->hw.func_caps.num_msix_vectors_vf++;
5422 }
5423
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005424 if (pf->hw.debug_mask & I40E_DEBUG_USER)
5425 dev_info(&pf->pdev->dev,
5426 "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",
5427 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
5428 pf->hw.func_caps.num_msix_vectors,
5429 pf->hw.func_caps.num_msix_vectors_vf,
5430 pf->hw.func_caps.fd_filters_guaranteed,
5431 pf->hw.func_caps.fd_filters_best_effort,
5432 pf->hw.func_caps.num_tx_qp,
5433 pf->hw.func_caps.num_vsis);
5434
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00005435#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
5436 + pf->hw.func_caps.num_vfs)
5437 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
5438 dev_info(&pf->pdev->dev,
5439 "got num_vsis %d, setting num_vsis to %d\n",
5440 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
5441 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
5442 }
5443
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005444 return 0;
5445}
5446
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005447static int i40e_vsi_clear(struct i40e_vsi *vsi);
5448
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005449/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005450 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005451 * @pf: board private structure
5452 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005453static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005454{
5455 struct i40e_vsi *vsi;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005456 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005457
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005458 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005459 return;
5460
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005461 /* find existing VSI and see if it needs configuring */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005462 vsi = NULL;
Mitch Williams505682c2014-05-20 08:01:37 +00005463 for (i = 0; i < pf->num_alloc_vsi; i++) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005464 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005465 vsi = pf->vsi[i];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005466 break;
5467 }
5468 }
5469
5470 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005471 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005472 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
5473 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005474 if (!vsi) {
5475 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005476 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
5477 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005478 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005479 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005480
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005481 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005482}
5483
5484/**
5485 * i40e_fdir_teardown - release the Flow Director resources
5486 * @pf: board private structure
5487 **/
5488static void i40e_fdir_teardown(struct i40e_pf *pf)
5489{
5490 int i;
5491
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005492 i40e_fdir_filter_exit(pf);
Mitch Williams505682c2014-05-20 08:01:37 +00005493 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005494 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
5495 i40e_vsi_release(pf->vsi[i]);
5496 break;
5497 }
5498 }
5499}
5500
5501/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005502 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005503 * @pf: board private structure
5504 *
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005505 * Close up the VFs and other things in prep for pf Reset.
5506 **/
5507static int i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005508{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005509 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00005510 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005511 u32 v;
5512
5513 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
5514 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005515 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005516
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005517 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005518
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005519 /* quiesce the VSIs and their queues that are not already DOWN */
5520 i40e_pf_quiesce_all_vsi(pf);
5521
Mitch Williams505682c2014-05-20 08:01:37 +00005522 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005523 if (pf->vsi[v])
5524 pf->vsi[v]->seid = 0;
5525 }
5526
5527 i40e_shutdown_adminq(&pf->hw);
5528
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005529 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00005530 if (hw->hmc.hmc_obj) {
5531 ret = i40e_shutdown_lan_hmc(hw);
5532 if (ret) {
5533 dev_warn(&pf->pdev->dev,
5534 "shutdown_lan_hmc failed: %d\n", ret);
5535 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
5536 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005537 }
5538 return ret;
5539}
5540
5541/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00005542 * i40e_send_version - update firmware with driver version
5543 * @pf: PF struct
5544 */
5545static void i40e_send_version(struct i40e_pf *pf)
5546{
5547 struct i40e_driver_version dv;
5548
5549 dv.major_version = DRV_VERSION_MAJOR;
5550 dv.minor_version = DRV_VERSION_MINOR;
5551 dv.build_version = DRV_VERSION_BUILD;
5552 dv.subbuild_version = 0;
5553 strncpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
5554 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
5555}
5556
5557/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00005558 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005559 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005560 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005561 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005562static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005563{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005564 struct i40e_hw *hw = &pf->hw;
5565 i40e_status ret;
5566 u32 v;
5567
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005568 /* Now we wait for GRST to settle out.
5569 * We don't have to delete the VEBs or VSIs from the hw switch
5570 * because the reset will make them disappear.
5571 */
5572 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00005573 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005574 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00005575 goto end_core_reset;
5576 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005577 pf->pfr_count++;
5578
5579 if (test_bit(__I40E_DOWN, &pf->state))
5580 goto end_core_reset;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005581 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005582
5583 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
5584 ret = i40e_init_adminq(&pf->hw);
5585 if (ret) {
5586 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, %d\n", ret);
5587 goto end_core_reset;
5588 }
5589
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005590 /* re-verify the eeprom if we just had an EMP reset */
5591 if (test_bit(__I40E_EMP_RESET_REQUESTED, &pf->state)) {
5592 clear_bit(__I40E_EMP_RESET_REQUESTED, &pf->state);
5593 i40e_verify_eeprom(pf);
5594 }
5595
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00005596 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005597 ret = i40e_get_capabilities(pf);
5598 if (ret) {
5599 dev_info(&pf->pdev->dev, "i40e_get_capabilities failed, %d\n",
5600 ret);
5601 goto end_core_reset;
5602 }
5603
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005604 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
5605 hw->func_caps.num_rx_qp,
5606 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
5607 if (ret) {
5608 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
5609 goto end_core_reset;
5610 }
5611 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
5612 if (ret) {
5613 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
5614 goto end_core_reset;
5615 }
5616
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005617#ifdef CONFIG_I40E_DCB
5618 ret = i40e_init_pf_dcb(pf);
5619 if (ret) {
5620 dev_info(&pf->pdev->dev, "init_pf_dcb failed: %d\n", ret);
5621 goto end_core_reset;
5622 }
5623#endif /* CONFIG_I40E_DCB */
5624
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005625 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005626 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005627 if (ret)
5628 goto end_core_reset;
5629
5630 /* Rebuild the VSIs and VEBs that existed before reset.
5631 * They are still in our local switch element arrays, so only
5632 * need to rebuild the switch model in the HW.
5633 *
5634 * If there were VEBs but the reconstitution failed, we'll try
5635 * try to recover minimal use by getting the basic PF VSI working.
5636 */
5637 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005638 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005639 /* find the one VEB connected to the MAC, and find orphans */
5640 for (v = 0; v < I40E_MAX_VEB; v++) {
5641 if (!pf->veb[v])
5642 continue;
5643
5644 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
5645 pf->veb[v]->uplink_seid == 0) {
5646 ret = i40e_reconstitute_veb(pf->veb[v]);
5647
5648 if (!ret)
5649 continue;
5650
5651 /* If Main VEB failed, we're in deep doodoo,
5652 * so give up rebuilding the switch and set up
5653 * for minimal rebuild of PF VSI.
5654 * If orphan failed, we'll report the error
5655 * but try to keep going.
5656 */
5657 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
5658 dev_info(&pf->pdev->dev,
5659 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
5660 ret);
5661 pf->vsi[pf->lan_vsi]->uplink_seid
5662 = pf->mac_seid;
5663 break;
5664 } else if (pf->veb[v]->uplink_seid == 0) {
5665 dev_info(&pf->pdev->dev,
5666 "rebuild of orphan VEB failed: %d\n",
5667 ret);
5668 }
5669 }
5670 }
5671 }
5672
5673 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
5674 dev_info(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
5675 /* no VEB, so rebuild only the Main VSI */
5676 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
5677 if (ret) {
5678 dev_info(&pf->pdev->dev,
5679 "rebuild of Main VSI failed: %d\n", ret);
5680 goto end_core_reset;
5681 }
5682 }
5683
5684 /* reinit the misc interrupt */
5685 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5686 ret = i40e_setup_misc_vector(pf);
5687
5688 /* restart the VSIs that were rebuilt and running before the reset */
5689 i40e_pf_unquiesce_all_vsi(pf);
5690
Mitch Williams69f64b22014-02-13 03:48:46 -08005691 if (pf->num_alloc_vfs) {
5692 for (v = 0; v < pf->num_alloc_vfs; v++)
5693 i40e_reset_vf(&pf->vf[v], true);
5694 }
5695
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005696 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00005697 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005698
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005699end_core_reset:
5700 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
5701}
5702
5703/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005704 * i40e_handle_reset_warning - prep for the pf to reset, reset and rebuild
5705 * @pf: board private structure
5706 *
5707 * Close up the VFs and other things in prep for a Core Reset,
5708 * then get ready to rebuild the world.
5709 **/
5710static void i40e_handle_reset_warning(struct i40e_pf *pf)
5711{
5712 i40e_status ret;
5713
5714 ret = i40e_prep_for_reset(pf);
5715 if (!ret)
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005716 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005717}
5718
5719/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005720 * i40e_handle_mdd_event
5721 * @pf: pointer to the pf structure
5722 *
5723 * Called from the MDD irq handler to identify possibly malicious vfs
5724 **/
5725static void i40e_handle_mdd_event(struct i40e_pf *pf)
5726{
5727 struct i40e_hw *hw = &pf->hw;
5728 bool mdd_detected = false;
5729 struct i40e_vf *vf;
5730 u32 reg;
5731 int i;
5732
5733 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
5734 return;
5735
5736 /* find what triggered the MDD event */
5737 reg = rd32(hw, I40E_GL_MDET_TX);
5738 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
5739 u8 func = (reg & I40E_GL_MDET_TX_FUNCTION_MASK)
5740 >> I40E_GL_MDET_TX_FUNCTION_SHIFT;
5741 u8 event = (reg & I40E_GL_MDET_TX_EVENT_SHIFT)
5742 >> I40E_GL_MDET_TX_EVENT_SHIFT;
5743 u8 queue = (reg & I40E_GL_MDET_TX_QUEUE_MASK)
5744 >> I40E_GL_MDET_TX_QUEUE_SHIFT;
5745 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00005746 "Malicious Driver Detection event 0x%02x on TX queue %d of function 0x%02x\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005747 event, queue, func);
5748 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
5749 mdd_detected = true;
5750 }
5751 reg = rd32(hw, I40E_GL_MDET_RX);
5752 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
5753 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK)
5754 >> I40E_GL_MDET_RX_FUNCTION_SHIFT;
5755 u8 event = (reg & I40E_GL_MDET_RX_EVENT_SHIFT)
5756 >> I40E_GL_MDET_RX_EVENT_SHIFT;
5757 u8 queue = (reg & I40E_GL_MDET_RX_QUEUE_MASK)
5758 >> I40E_GL_MDET_RX_QUEUE_SHIFT;
5759 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00005760 "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005761 event, queue, func);
5762 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
5763 mdd_detected = true;
5764 }
5765
5766 /* see if one of the VFs needs its hand slapped */
5767 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
5768 vf = &(pf->vf[i]);
5769 reg = rd32(hw, I40E_VP_MDET_TX(i));
5770 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
5771 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
5772 vf->num_mdd_events++;
5773 dev_info(&pf->pdev->dev, "MDD TX event on VF %d\n", i);
5774 }
5775
5776 reg = rd32(hw, I40E_VP_MDET_RX(i));
5777 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
5778 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
5779 vf->num_mdd_events++;
5780 dev_info(&pf->pdev->dev, "MDD RX event on VF %d\n", i);
5781 }
5782
5783 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
5784 dev_info(&pf->pdev->dev,
5785 "Too many MDD events on VF %d, disabled\n", i);
5786 dev_info(&pf->pdev->dev,
5787 "Use PF Control I/F to re-enable the VF\n");
5788 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
5789 }
5790 }
5791
5792 /* re-enable mdd interrupt cause */
5793 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
5794 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
5795 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
5796 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
5797 i40e_flush(hw);
5798}
5799
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005800#ifdef CONFIG_I40E_VXLAN
5801/**
5802 * i40e_sync_vxlan_filters_subtask - Sync the VSI filter list with HW
5803 * @pf: board private structure
5804 **/
5805static void i40e_sync_vxlan_filters_subtask(struct i40e_pf *pf)
5806{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005807 struct i40e_hw *hw = &pf->hw;
5808 i40e_status ret;
5809 u8 filter_index;
5810 __be16 port;
5811 int i;
5812
5813 if (!(pf->flags & I40E_FLAG_VXLAN_FILTER_SYNC))
5814 return;
5815
5816 pf->flags &= ~I40E_FLAG_VXLAN_FILTER_SYNC;
5817
5818 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
5819 if (pf->pending_vxlan_bitmap & (1 << i)) {
5820 pf->pending_vxlan_bitmap &= ~(1 << i);
5821 port = pf->vxlan_ports[i];
5822 ret = port ?
5823 i40e_aq_add_udp_tunnel(hw, ntohs(port),
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005824 I40E_AQC_TUNNEL_TYPE_VXLAN,
5825 &filter_index, NULL)
5826 : i40e_aq_del_udp_tunnel(hw, i, NULL);
5827
5828 if (ret) {
5829 dev_info(&pf->pdev->dev, "Failed to execute AQ command for %s port %d with index %d\n",
5830 port ? "adding" : "deleting",
5831 ntohs(port), port ? i : i);
5832
5833 pf->vxlan_ports[i] = 0;
5834 } else {
5835 dev_info(&pf->pdev->dev, "%s port %d with AQ command with index %d\n",
5836 port ? "Added" : "Deleted",
5837 ntohs(port), port ? i : filter_index);
5838 }
5839 }
5840 }
5841}
5842
5843#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005844/**
5845 * i40e_service_task - Run the driver's async subtasks
5846 * @work: pointer to work_struct containing our data
5847 **/
5848static void i40e_service_task(struct work_struct *work)
5849{
5850 struct i40e_pf *pf = container_of(work,
5851 struct i40e_pf,
5852 service_task);
5853 unsigned long start_time = jiffies;
5854
5855 i40e_reset_subtask(pf);
5856 i40e_handle_mdd_event(pf);
5857 i40e_vc_process_vflr_event(pf);
5858 i40e_watchdog_subtask(pf);
5859 i40e_fdir_reinit_subtask(pf);
5860 i40e_check_hang_subtask(pf);
5861 i40e_sync_filters_subtask(pf);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005862#ifdef CONFIG_I40E_VXLAN
5863 i40e_sync_vxlan_filters_subtask(pf);
5864#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005865 i40e_clean_adminq_subtask(pf);
5866
5867 i40e_service_event_complete(pf);
5868
5869 /* If the tasks have taken longer than one timer cycle or there
5870 * is more work to be done, reschedule the service task now
5871 * rather than wait for the timer to tick again.
5872 */
5873 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
5874 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
5875 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
5876 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
5877 i40e_service_event_schedule(pf);
5878}
5879
5880/**
5881 * i40e_service_timer - timer callback
5882 * @data: pointer to PF struct
5883 **/
5884static void i40e_service_timer(unsigned long data)
5885{
5886 struct i40e_pf *pf = (struct i40e_pf *)data;
5887
5888 mod_timer(&pf->service_timer,
5889 round_jiffies(jiffies + pf->service_timer_period));
5890 i40e_service_event_schedule(pf);
5891}
5892
5893/**
5894 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
5895 * @vsi: the VSI being configured
5896 **/
5897static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
5898{
5899 struct i40e_pf *pf = vsi->back;
5900
5901 switch (vsi->type) {
5902 case I40E_VSI_MAIN:
5903 vsi->alloc_queue_pairs = pf->num_lan_qps;
5904 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5905 I40E_REQ_DESCRIPTOR_MULTIPLE);
5906 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5907 vsi->num_q_vectors = pf->num_lan_msix;
5908 else
5909 vsi->num_q_vectors = 1;
5910
5911 break;
5912
5913 case I40E_VSI_FDIR:
5914 vsi->alloc_queue_pairs = 1;
5915 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
5916 I40E_REQ_DESCRIPTOR_MULTIPLE);
5917 vsi->num_q_vectors = 1;
5918 break;
5919
5920 case I40E_VSI_VMDQ2:
5921 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
5922 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5923 I40E_REQ_DESCRIPTOR_MULTIPLE);
5924 vsi->num_q_vectors = pf->num_vmdq_msix;
5925 break;
5926
5927 case I40E_VSI_SRIOV:
5928 vsi->alloc_queue_pairs = pf->num_vf_qps;
5929 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5930 I40E_REQ_DESCRIPTOR_MULTIPLE);
5931 break;
5932
5933 default:
5934 WARN_ON(1);
5935 return -ENODATA;
5936 }
5937
5938 return 0;
5939}
5940
5941/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005942 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
5943 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005944 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005945 *
5946 * On error: returns error code (negative)
5947 * On success: returns 0
5948 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005949static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005950{
5951 int size;
5952 int ret = 0;
5953
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00005954 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005955 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
5956 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
5957 if (!vsi->tx_rings)
5958 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005959 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
5960
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005961 if (alloc_qvectors) {
5962 /* allocate memory for q_vector pointers */
5963 size = sizeof(struct i40e_q_vectors *) * vsi->num_q_vectors;
5964 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
5965 if (!vsi->q_vectors) {
5966 ret = -ENOMEM;
5967 goto err_vectors;
5968 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005969 }
5970 return ret;
5971
5972err_vectors:
5973 kfree(vsi->tx_rings);
5974 return ret;
5975}
5976
5977/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005978 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
5979 * @pf: board private structure
5980 * @type: type of VSI
5981 *
5982 * On error: returns error code (negative)
5983 * On success: returns vsi index in PF (positive)
5984 **/
5985static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
5986{
5987 int ret = -ENODEV;
5988 struct i40e_vsi *vsi;
5989 int vsi_idx;
5990 int i;
5991
5992 /* Need to protect the allocation of the VSIs at the PF level */
5993 mutex_lock(&pf->switch_mutex);
5994
5995 /* VSI list may be fragmented if VSI creation/destruction has
5996 * been happening. We can afford to do a quick scan to look
5997 * for any free VSIs in the list.
5998 *
5999 * find next empty vsi slot, looping back around if necessary
6000 */
6001 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00006002 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006003 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00006004 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006005 i = 0;
6006 while (i < pf->next_vsi && pf->vsi[i])
6007 i++;
6008 }
6009
Mitch Williams505682c2014-05-20 08:01:37 +00006010 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006011 vsi_idx = i; /* Found one! */
6012 } else {
6013 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00006014 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006015 }
6016 pf->next_vsi = ++i;
6017
6018 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
6019 if (!vsi) {
6020 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00006021 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006022 }
6023 vsi->type = type;
6024 vsi->back = pf;
6025 set_bit(__I40E_DOWN, &vsi->state);
6026 vsi->flags = 0;
6027 vsi->idx = vsi_idx;
6028 vsi->rx_itr_setting = pf->rx_itr_default;
6029 vsi->tx_itr_setting = pf->tx_itr_default;
6030 vsi->netdev_registered = false;
6031 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
6032 INIT_LIST_HEAD(&vsi->mac_filter_list);
Shannon Nelson63741842014-04-23 04:50:16 +00006033 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006034
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006035 ret = i40e_set_num_rings_in_vsi(vsi);
6036 if (ret)
6037 goto err_rings;
6038
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006039 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006040 if (ret)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006041 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00006042
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006043 /* Setup default MSIX irq handler for VSI */
6044 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
6045
6046 pf->vsi[vsi_idx] = vsi;
6047 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00006048 goto unlock_pf;
6049
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006050err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00006051 pf->next_vsi = i - 1;
6052 kfree(vsi);
6053unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006054 mutex_unlock(&pf->switch_mutex);
6055 return ret;
6056}
6057
6058/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006059 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
6060 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006061 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006062 *
6063 * On error: returns error code (negative)
6064 * On success: returns 0
6065 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006066static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006067{
6068 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006069 if (free_qvectors) {
6070 kfree(vsi->q_vectors);
6071 vsi->q_vectors = NULL;
6072 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006073 kfree(vsi->tx_rings);
6074 vsi->tx_rings = NULL;
6075 vsi->rx_rings = NULL;
6076}
6077
6078/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006079 * i40e_vsi_clear - Deallocate the VSI provided
6080 * @vsi: the VSI being un-configured
6081 **/
6082static int i40e_vsi_clear(struct i40e_vsi *vsi)
6083{
6084 struct i40e_pf *pf;
6085
6086 if (!vsi)
6087 return 0;
6088
6089 if (!vsi->back)
6090 goto free_vsi;
6091 pf = vsi->back;
6092
6093 mutex_lock(&pf->switch_mutex);
6094 if (!pf->vsi[vsi->idx]) {
6095 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
6096 vsi->idx, vsi->idx, vsi, vsi->type);
6097 goto unlock_vsi;
6098 }
6099
6100 if (pf->vsi[vsi->idx] != vsi) {
6101 dev_err(&pf->pdev->dev,
6102 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
6103 pf->vsi[vsi->idx]->idx,
6104 pf->vsi[vsi->idx],
6105 pf->vsi[vsi->idx]->type,
6106 vsi->idx, vsi, vsi->type);
6107 goto unlock_vsi;
6108 }
6109
6110 /* updates the pf for this cleared vsi */
6111 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
6112 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
6113
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006114 i40e_vsi_free_arrays(vsi, true);
Alexander Duyck493fb302013-09-28 07:01:44 +00006115
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006116 pf->vsi[vsi->idx] = NULL;
6117 if (vsi->idx < pf->next_vsi)
6118 pf->next_vsi = vsi->idx;
6119
6120unlock_vsi:
6121 mutex_unlock(&pf->switch_mutex);
6122free_vsi:
6123 kfree(vsi);
6124
6125 return 0;
6126}
6127
6128/**
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006129 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
6130 * @vsi: the VSI being cleaned
6131 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00006132static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006133{
6134 int i;
6135
Greg Rose8e9dca52013-12-18 13:45:53 +00006136 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00006137 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00006138 kfree_rcu(vsi->tx_rings[i], rcu);
6139 vsi->tx_rings[i] = NULL;
6140 vsi->rx_rings[i] = NULL;
6141 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00006142 }
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006143}
6144
6145/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006146 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
6147 * @vsi: the VSI being configured
6148 **/
6149static int i40e_alloc_rings(struct i40e_vsi *vsi)
6150{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00006151 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006152 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006153 int i;
6154
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006155 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00006156 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00006157 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006158 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
6159 if (!tx_ring)
6160 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006161
6162 tx_ring->queue_index = i;
6163 tx_ring->reg_idx = vsi->base_queue + i;
6164 tx_ring->ring_active = false;
6165 tx_ring->vsi = vsi;
6166 tx_ring->netdev = vsi->netdev;
6167 tx_ring->dev = &pf->pdev->dev;
6168 tx_ring->count = vsi->num_desc;
6169 tx_ring->size = 0;
6170 tx_ring->dcb_tc = 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006171 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006172
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006173 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006174 rx_ring->queue_index = i;
6175 rx_ring->reg_idx = vsi->base_queue + i;
6176 rx_ring->ring_active = false;
6177 rx_ring->vsi = vsi;
6178 rx_ring->netdev = vsi->netdev;
6179 rx_ring->dev = &pf->pdev->dev;
6180 rx_ring->count = vsi->num_desc;
6181 rx_ring->size = 0;
6182 rx_ring->dcb_tc = 0;
6183 if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
6184 set_ring_16byte_desc_enabled(rx_ring);
6185 else
6186 clear_ring_16byte_desc_enabled(rx_ring);
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006187 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006188 }
6189
6190 return 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00006191
6192err_out:
6193 i40e_vsi_clear_rings(vsi);
6194 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006195}
6196
6197/**
6198 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
6199 * @pf: board private structure
6200 * @vectors: the number of MSI-X vectors to request
6201 *
6202 * Returns the number of vectors reserved, or error
6203 **/
6204static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
6205{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01006206 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
6207 I40E_MIN_MSIX, vectors);
6208 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006209 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01006210 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006211 vectors = 0;
6212 }
6213
6214 return vectors;
6215}
6216
6217/**
6218 * i40e_init_msix - Setup the MSIX capability
6219 * @pf: board private structure
6220 *
6221 * Work with the OS to set up the MSIX vectors needed.
6222 *
6223 * Returns 0 on success, negative on failure
6224 **/
6225static int i40e_init_msix(struct i40e_pf *pf)
6226{
6227 i40e_status err = 0;
6228 struct i40e_hw *hw = &pf->hw;
6229 int v_budget, i;
6230 int vec;
6231
6232 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
6233 return -ENODEV;
6234
6235 /* The number of vectors we'll request will be comprised of:
6236 * - Add 1 for "other" cause for Admin Queue events, etc.
6237 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006238 * - Queues being used for RSS.
6239 * We don't need as many as max_rss_size vectors.
6240 * use rss_size instead in the calculation since that
6241 * is governed by number of cpus in the system.
6242 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006243 * - The number of VMDq pairs
6244 * Once we count this up, try the request.
6245 *
6246 * If we can't get what we want, we'll simplify to nearly nothing
6247 * and try again. If that still fails, we punt.
6248 */
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006249 pf->num_lan_msix = pf->num_lan_qps - (pf->rss_size_max - pf->rss_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006250 pf->num_vmdq_msix = pf->num_vmdq_qps;
6251 v_budget = 1 + pf->num_lan_msix;
6252 v_budget += (pf->num_vmdq_vsis * pf->num_vmdq_msix);
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006253 if (pf->flags & I40E_FLAG_FD_SB_ENABLED)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006254 v_budget++;
6255
6256 /* Scale down if necessary, and the rings will share vectors */
6257 v_budget = min_t(int, v_budget, hw->func_caps.num_msix_vectors);
6258
6259 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
6260 GFP_KERNEL);
6261 if (!pf->msix_entries)
6262 return -ENOMEM;
6263
6264 for (i = 0; i < v_budget; i++)
6265 pf->msix_entries[i].entry = i;
6266 vec = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00006267
6268 if (vec != v_budget) {
6269 /* If we have limited resources, we will start with no vectors
6270 * for the special features and then allocate vectors to some
6271 * of these features based on the policy and at the end disable
6272 * the features that did not get any vectors.
6273 */
6274 pf->num_vmdq_msix = 0;
6275 }
6276
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006277 if (vec < I40E_MIN_MSIX) {
6278 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
6279 kfree(pf->msix_entries);
6280 pf->msix_entries = NULL;
6281 return -ENODEV;
6282
6283 } else if (vec == I40E_MIN_MSIX) {
6284 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006285 pf->num_vmdq_vsis = 0;
6286 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006287 pf->num_lan_qps = 1;
6288 pf->num_lan_msix = 1;
6289
6290 } else if (vec != v_budget) {
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00006291 /* reserve the misc vector */
6292 vec--;
6293
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006294 /* Scale vector usage down */
6295 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00006296 pf->num_vmdq_vsis = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006297
6298 /* partition out the remaining vectors */
6299 switch (vec) {
6300 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006301 pf->num_lan_msix = 1;
6302 break;
6303 case 3:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006304 pf->num_lan_msix = 2;
6305 break;
6306 default:
6307 pf->num_lan_msix = min_t(int, (vec / 2),
6308 pf->num_lan_qps);
6309 pf->num_vmdq_vsis = min_t(int, (vec - pf->num_lan_msix),
6310 I40E_DEFAULT_NUM_VMDQ_VSI);
6311 break;
6312 }
6313 }
6314
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00006315 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
6316 (pf->num_vmdq_msix == 0)) {
6317 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
6318 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
6319 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006320 return err;
6321}
6322
6323/**
Greg Rose90e04072014-03-06 08:59:57 +00006324 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00006325 * @vsi: the VSI being configured
6326 * @v_idx: index of the vector in the vsi struct
6327 *
6328 * We allocate one q_vector. If allocation fails we return -ENOMEM.
6329 **/
Greg Rose90e04072014-03-06 08:59:57 +00006330static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
Alexander Duyck493fb302013-09-28 07:01:44 +00006331{
6332 struct i40e_q_vector *q_vector;
6333
6334 /* allocate q_vector */
6335 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
6336 if (!q_vector)
6337 return -ENOMEM;
6338
6339 q_vector->vsi = vsi;
6340 q_vector->v_idx = v_idx;
6341 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
6342 if (vsi->netdev)
6343 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00006344 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00006345
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00006346 q_vector->rx.latency_range = I40E_LOW_LATENCY;
6347 q_vector->tx.latency_range = I40E_LOW_LATENCY;
6348
Alexander Duyck493fb302013-09-28 07:01:44 +00006349 /* tie q_vector and vsi together */
6350 vsi->q_vectors[v_idx] = q_vector;
6351
6352 return 0;
6353}
6354
6355/**
Greg Rose90e04072014-03-06 08:59:57 +00006356 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006357 * @vsi: the VSI being configured
6358 *
6359 * We allocate one q_vector per queue interrupt. If allocation fails we
6360 * return -ENOMEM.
6361 **/
Greg Rose90e04072014-03-06 08:59:57 +00006362static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006363{
6364 struct i40e_pf *pf = vsi->back;
6365 int v_idx, num_q_vectors;
Alexander Duyck493fb302013-09-28 07:01:44 +00006366 int err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006367
6368 /* if not MSIX, give the one vector only to the LAN VSI */
6369 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6370 num_q_vectors = vsi->num_q_vectors;
6371 else if (vsi == pf->vsi[pf->lan_vsi])
6372 num_q_vectors = 1;
6373 else
6374 return -EINVAL;
6375
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006376 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Greg Rose90e04072014-03-06 08:59:57 +00006377 err = i40e_vsi_alloc_q_vector(vsi, v_idx);
Alexander Duyck493fb302013-09-28 07:01:44 +00006378 if (err)
6379 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006380 }
6381
6382 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00006383
6384err_out:
6385 while (v_idx--)
6386 i40e_free_q_vector(vsi, v_idx);
6387
6388 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006389}
6390
6391/**
6392 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
6393 * @pf: board private structure to initialize
6394 **/
6395static void i40e_init_interrupt_scheme(struct i40e_pf *pf)
6396{
6397 int err = 0;
6398
6399 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
6400 err = i40e_init_msix(pf);
6401 if (err) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006402 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
6403 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006404 I40E_FLAG_DCB_CAPABLE |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006405 I40E_FLAG_SRIOV_ENABLED |
6406 I40E_FLAG_FD_SB_ENABLED |
6407 I40E_FLAG_FD_ATR_ENABLED |
6408 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006409
6410 /* rework the queue expectations without MSIX */
6411 i40e_determine_queue_usage(pf);
6412 }
6413 }
6414
6415 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
6416 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006417 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006418 err = pci_enable_msi(pf->pdev);
6419 if (err) {
Shannon Nelson958a3e32013-09-28 07:13:28 +00006420 dev_info(&pf->pdev->dev, "MSI init failed - %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006421 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
6422 }
6423 }
6424
Shannon Nelson958a3e32013-09-28 07:13:28 +00006425 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006426 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00006427
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006428 /* track first vector for misc interrupts */
6429 err = i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT-1);
6430}
6431
6432/**
6433 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
6434 * @pf: board private structure
6435 *
6436 * This sets up the handler for MSIX 0, which is used to manage the
6437 * non-queue interrupts, e.g. AdminQ and errors. This is not used
6438 * when in MSI or Legacy interrupt mode.
6439 **/
6440static int i40e_setup_misc_vector(struct i40e_pf *pf)
6441{
6442 struct i40e_hw *hw = &pf->hw;
6443 int err = 0;
6444
6445 /* Only request the irq if this is the first time through, and
6446 * not when we're rebuilding after a Reset
6447 */
6448 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
6449 err = request_irq(pf->msix_entries[0].vector,
6450 i40e_intr, 0, pf->misc_int_name, pf);
6451 if (err) {
6452 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006453 "request_irq for %s failed: %d\n",
6454 pf->misc_int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006455 return -EFAULT;
6456 }
6457 }
6458
6459 i40e_enable_misc_int_causes(hw);
6460
6461 /* associate no queues to the misc vector */
6462 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
6463 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
6464
6465 i40e_flush(hw);
6466
6467 i40e_irq_dynamic_enable_icr0(pf);
6468
6469 return err;
6470}
6471
6472/**
6473 * i40e_config_rss - Prepare for RSS if used
6474 * @pf: board private structure
6475 **/
6476static int i40e_config_rss(struct i40e_pf *pf)
6477{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006478 /* Set of random keys generated using kernel random number generator */
6479 static const u32 seed[I40E_PFQF_HKEY_MAX_INDEX + 1] = {0x41b01687,
6480 0x183cfd8c, 0xce880440, 0x580cbc3c, 0x35897377,
6481 0x328b25e1, 0x4fa98922, 0xb7d90c14, 0xd5bad70d,
6482 0xcd15a2c1, 0xe8580225, 0x4a1e9d11, 0xfe5731be};
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00006483 struct i40e_hw *hw = &pf->hw;
6484 u32 lut = 0;
6485 int i, j;
6486 u64 hena;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006487
6488 /* Fill out hash function seed */
6489 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
6490 wr32(hw, I40E_PFQF_HKEY(i), seed[i]);
6491
6492 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
6493 hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
6494 ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
Mitch Williams12dc4fe2013-11-28 06:39:32 +00006495 hena |= I40E_DEFAULT_RSS_HENA;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006496 wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
6497 wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
6498
6499 /* Populate the LUT with max no. of queues in round robin fashion */
6500 for (i = 0, j = 0; i < pf->hw.func_caps.rss_table_size; i++, j++) {
6501
6502 /* The assumption is that lan qp count will be the highest
6503 * qp count for any PF VSI that needs RSS.
6504 * If multiple VSIs need RSS support, all the qp counts
6505 * for those VSIs should be a power of 2 for RSS to work.
6506 * If LAN VSI is the only consumer for RSS then this requirement
6507 * is not necessary.
6508 */
6509 if (j == pf->rss_size)
6510 j = 0;
6511 /* lut = 4-byte sliding window of 4 lut entries */
6512 lut = (lut << 8) | (j &
6513 ((0x1 << pf->hw.func_caps.rss_table_entry_width) - 1));
6514 /* On i = 3, we have 4 entries in lut; write to the register */
6515 if ((i & 3) == 3)
6516 wr32(hw, I40E_PFQF_HLUT(i >> 2), lut);
6517 }
6518 i40e_flush(hw);
6519
6520 return 0;
6521}
6522
6523/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006524 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
6525 * @pf: board private structure
6526 * @queue_count: the requested queue count for rss.
6527 *
6528 * returns 0 if rss is not enabled, if enabled returns the final rss queue
6529 * count which may be different from the requested queue count.
6530 **/
6531int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
6532{
6533 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
6534 return 0;
6535
6536 queue_count = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006537
6538 if (queue_count != pf->rss_size) {
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006539 i40e_prep_for_reset(pf);
6540
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006541 pf->rss_size = queue_count;
6542
6543 i40e_reset_and_rebuild(pf, true);
6544 i40e_config_rss(pf);
6545 }
6546 dev_info(&pf->pdev->dev, "RSS count: %d\n", pf->rss_size);
6547 return pf->rss_size;
6548}
6549
6550/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006551 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
6552 * @pf: board private structure to initialize
6553 *
6554 * i40e_sw_init initializes the Adapter private data structure.
6555 * Fields are initialized based on PCI device information and
6556 * OS network device settings (MTU size).
6557 **/
6558static int i40e_sw_init(struct i40e_pf *pf)
6559{
6560 int err = 0;
6561 int size;
6562
6563 pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
6564 (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
Shannon Nelson27599972013-11-16 10:00:43 +00006565 pf->hw.debug_mask = pf->msg_enable | I40E_DEBUG_DIAG;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006566 if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
6567 if (I40E_DEBUG_USER & debug)
6568 pf->hw.debug_mask = debug;
6569 pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
6570 I40E_DEFAULT_MSG_ENABLE);
6571 }
6572
6573 /* Set default capability flags */
6574 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
6575 I40E_FLAG_MSI_ENABLED |
6576 I40E_FLAG_MSIX_ENABLED |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006577 I40E_FLAG_RX_1BUF_ENABLED;
6578
Mitch Williamsca99eb92014-04-04 04:43:07 +00006579 /* Set default ITR */
6580 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
6581 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
6582
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006583 /* Depending on PF configurations, it is possible that the RSS
6584 * maximum might end up larger than the available queues
6585 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006586 pf->rss_size_max = 0x1 << pf->hw.func_caps.rss_table_entry_width;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006587 pf->rss_size_max = min_t(int, pf->rss_size_max,
6588 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006589 if (pf->hw.func_caps.rss) {
6590 pf->flags |= I40E_FLAG_RSS_ENABLED;
Jesse Brandeburgbf051a32013-11-26 10:49:17 +00006591 pf->rss_size = min_t(int, pf->rss_size_max, num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006592 } else {
6593 pf->rss_size = 1;
6594 }
6595
Catherine Sullivan2050bc62013-12-18 13:46:03 +00006596 /* MFP mode enabled */
6597 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.mfp_mode_1) {
6598 pf->flags |= I40E_FLAG_MFP_ENABLED;
6599 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
6600 }
6601
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006602 /* FW/NVM is not yet fixed in this regard */
6603 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
6604 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
6605 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
6606 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006607 if (!(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006608 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006609 } else {
6610 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00006611 "Flow Director Sideband mode Disabled in MFP mode\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006612 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006613 pf->fdir_pf_filter_count =
6614 pf->hw.func_caps.fd_filters_guaranteed;
6615 pf->hw.fdir_shared_filter_count =
6616 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006617 }
6618
6619 if (pf->hw.func_caps.vmdq) {
6620 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
6621 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
6622 pf->num_vmdq_qps = I40E_DEFAULT_QUEUES_PER_VMDQ;
6623 }
6624
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006625#ifdef CONFIG_PCI_IOV
6626 if (pf->hw.func_caps.num_vfs) {
6627 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
6628 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
6629 pf->num_req_vfs = min_t(int,
6630 pf->hw.func_caps.num_vfs,
6631 I40E_MAX_VF_COUNT);
6632 }
6633#endif /* CONFIG_PCI_IOV */
6634 pf->eeprom_version = 0xDEAD;
6635 pf->lan_veb = I40E_NO_VEB;
6636 pf->lan_vsi = I40E_NO_VSI;
6637
6638 /* set up queue assignment tracking */
6639 size = sizeof(struct i40e_lump_tracking)
6640 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
6641 pf->qp_pile = kzalloc(size, GFP_KERNEL);
6642 if (!pf->qp_pile) {
6643 err = -ENOMEM;
6644 goto sw_init_done;
6645 }
6646 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
6647 pf->qp_pile->search_hint = 0;
6648
6649 /* set up vector assignment tracking */
6650 size = sizeof(struct i40e_lump_tracking)
6651 + (sizeof(u16) * pf->hw.func_caps.num_msix_vectors);
6652 pf->irq_pile = kzalloc(size, GFP_KERNEL);
6653 if (!pf->irq_pile) {
6654 kfree(pf->qp_pile);
6655 err = -ENOMEM;
6656 goto sw_init_done;
6657 }
6658 pf->irq_pile->num_entries = pf->hw.func_caps.num_msix_vectors;
6659 pf->irq_pile->search_hint = 0;
6660
6661 mutex_init(&pf->switch_mutex);
6662
6663sw_init_done:
6664 return err;
6665}
6666
6667/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00006668 * i40e_set_ntuple - set the ntuple feature flag and take action
6669 * @pf: board private structure to initialize
6670 * @features: the feature set that the stack is suggesting
6671 *
6672 * returns a bool to indicate if reset needs to happen
6673 **/
6674bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
6675{
6676 bool need_reset = false;
6677
6678 /* Check if Flow Director n-tuple support was enabled or disabled. If
6679 * the state changed, we need to reset.
6680 */
6681 if (features & NETIF_F_NTUPLE) {
6682 /* Enable filters and mark for reset */
6683 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
6684 need_reset = true;
6685 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
6686 } else {
6687 /* turn off filters, mark for reset and clear SW filter list */
6688 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
6689 need_reset = true;
6690 i40e_fdir_filter_exit(pf);
6691 }
6692 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6693 /* if ATR was disabled it can be re-enabled. */
6694 if (!(pf->flags & I40E_FLAG_FD_ATR_ENABLED))
6695 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
6696 }
6697 return need_reset;
6698}
6699
6700/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006701 * i40e_set_features - set the netdev feature flags
6702 * @netdev: ptr to the netdev being adjusted
6703 * @features: the feature set that the stack is suggesting
6704 **/
6705static int i40e_set_features(struct net_device *netdev,
6706 netdev_features_t features)
6707{
6708 struct i40e_netdev_priv *np = netdev_priv(netdev);
6709 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00006710 struct i40e_pf *pf = vsi->back;
6711 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006712
6713 if (features & NETIF_F_HW_VLAN_CTAG_RX)
6714 i40e_vlan_stripping_enable(vsi);
6715 else
6716 i40e_vlan_stripping_disable(vsi);
6717
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00006718 need_reset = i40e_set_ntuple(pf, features);
6719
6720 if (need_reset)
6721 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
6722
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006723 return 0;
6724}
6725
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006726#ifdef CONFIG_I40E_VXLAN
6727/**
6728 * i40e_get_vxlan_port_idx - Lookup a possibly offloaded for Rx UDP port
6729 * @pf: board private structure
6730 * @port: The UDP port to look up
6731 *
6732 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
6733 **/
6734static u8 i40e_get_vxlan_port_idx(struct i40e_pf *pf, __be16 port)
6735{
6736 u8 i;
6737
6738 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
6739 if (pf->vxlan_ports[i] == port)
6740 return i;
6741 }
6742
6743 return i;
6744}
6745
6746/**
6747 * i40e_add_vxlan_port - Get notifications about VXLAN ports that come up
6748 * @netdev: This physical port's netdev
6749 * @sa_family: Socket Family that VXLAN is notifying us about
6750 * @port: New UDP port number that VXLAN started listening to
6751 **/
6752static void i40e_add_vxlan_port(struct net_device *netdev,
6753 sa_family_t sa_family, __be16 port)
6754{
6755 struct i40e_netdev_priv *np = netdev_priv(netdev);
6756 struct i40e_vsi *vsi = np->vsi;
6757 struct i40e_pf *pf = vsi->back;
6758 u8 next_idx;
6759 u8 idx;
6760
6761 if (sa_family == AF_INET6)
6762 return;
6763
6764 idx = i40e_get_vxlan_port_idx(pf, port);
6765
6766 /* Check if port already exists */
6767 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6768 netdev_info(netdev, "Port %d already offloaded\n", ntohs(port));
6769 return;
6770 }
6771
6772 /* Now check if there is space to add the new port */
6773 next_idx = i40e_get_vxlan_port_idx(pf, 0);
6774
6775 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6776 netdev_info(netdev, "Maximum number of UDP ports reached, not adding port %d\n",
6777 ntohs(port));
6778 return;
6779 }
6780
6781 /* New port: add it and mark its index in the bitmap */
6782 pf->vxlan_ports[next_idx] = port;
6783 pf->pending_vxlan_bitmap |= (1 << next_idx);
6784
6785 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
6786}
6787
6788/**
6789 * i40e_del_vxlan_port - Get notifications about VXLAN ports that go away
6790 * @netdev: This physical port's netdev
6791 * @sa_family: Socket Family that VXLAN is notifying us about
6792 * @port: UDP port number that VXLAN stopped listening to
6793 **/
6794static void i40e_del_vxlan_port(struct net_device *netdev,
6795 sa_family_t sa_family, __be16 port)
6796{
6797 struct i40e_netdev_priv *np = netdev_priv(netdev);
6798 struct i40e_vsi *vsi = np->vsi;
6799 struct i40e_pf *pf = vsi->back;
6800 u8 idx;
6801
6802 if (sa_family == AF_INET6)
6803 return;
6804
6805 idx = i40e_get_vxlan_port_idx(pf, port);
6806
6807 /* Check if port already exists */
6808 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6809 /* if port exists, set it to 0 (mark for deletion)
6810 * and make it pending
6811 */
6812 pf->vxlan_ports[idx] = 0;
6813
6814 pf->pending_vxlan_bitmap |= (1 << idx);
6815
6816 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
6817 } else {
6818 netdev_warn(netdev, "Port %d was not found, not deleting\n",
6819 ntohs(port));
6820 }
6821}
6822
6823#endif
Greg Rose4ba0dea2014-03-06 08:59:55 +00006824#ifdef HAVE_FDB_OPS
6825#ifdef USE_CONST_DEV_UC_CHAR
6826static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
6827 struct net_device *dev,
6828 const unsigned char *addr,
6829 u16 flags)
6830#else
6831static int i40e_ndo_fdb_add(struct ndmsg *ndm,
6832 struct net_device *dev,
6833 unsigned char *addr,
6834 u16 flags)
6835#endif
6836{
6837 struct i40e_netdev_priv *np = netdev_priv(dev);
6838 struct i40e_pf *pf = np->vsi->back;
6839 int err = 0;
6840
6841 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
6842 return -EOPNOTSUPP;
6843
6844 /* Hardware does not support aging addresses so if a
6845 * ndm_state is given only allow permanent addresses
6846 */
6847 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
6848 netdev_info(dev, "FDB only supports static addresses\n");
6849 return -EINVAL;
6850 }
6851
6852 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
6853 err = dev_uc_add_excl(dev, addr);
6854 else if (is_multicast_ether_addr(addr))
6855 err = dev_mc_add_excl(dev, addr);
6856 else
6857 err = -EINVAL;
6858
6859 /* Only return duplicate errors if NLM_F_EXCL is set */
6860 if (err == -EEXIST && !(flags & NLM_F_EXCL))
6861 err = 0;
6862
6863 return err;
6864}
6865
6866#ifndef USE_DEFAULT_FDB_DEL_DUMP
6867#ifdef USE_CONST_DEV_UC_CHAR
6868static int i40e_ndo_fdb_del(struct ndmsg *ndm,
6869 struct net_device *dev,
6870 const unsigned char *addr)
6871#else
6872static int i40e_ndo_fdb_del(struct ndmsg *ndm,
6873 struct net_device *dev,
6874 unsigned char *addr)
6875#endif
6876{
6877 struct i40e_netdev_priv *np = netdev_priv(dev);
6878 struct i40e_pf *pf = np->vsi->back;
6879 int err = -EOPNOTSUPP;
6880
6881 if (ndm->ndm_state & NUD_PERMANENT) {
6882 netdev_info(dev, "FDB only supports static addresses\n");
6883 return -EINVAL;
6884 }
6885
6886 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
6887 if (is_unicast_ether_addr(addr))
6888 err = dev_uc_del(dev, addr);
6889 else if (is_multicast_ether_addr(addr))
6890 err = dev_mc_del(dev, addr);
6891 else
6892 err = -EINVAL;
6893 }
6894
6895 return err;
6896}
6897
6898static int i40e_ndo_fdb_dump(struct sk_buff *skb,
6899 struct netlink_callback *cb,
6900 struct net_device *dev,
6901 int idx)
6902{
6903 struct i40e_netdev_priv *np = netdev_priv(dev);
6904 struct i40e_pf *pf = np->vsi->back;
6905
6906 if (pf->flags & I40E_FLAG_SRIOV_ENABLED)
6907 idx = ndo_dflt_fdb_dump(skb, cb, dev, idx);
6908
6909 return idx;
6910}
6911
6912#endif /* USE_DEFAULT_FDB_DEL_DUMP */
6913#endif /* HAVE_FDB_OPS */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006914static const struct net_device_ops i40e_netdev_ops = {
6915 .ndo_open = i40e_open,
6916 .ndo_stop = i40e_close,
6917 .ndo_start_xmit = i40e_lan_xmit_frame,
6918 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
6919 .ndo_set_rx_mode = i40e_set_rx_mode,
6920 .ndo_validate_addr = eth_validate_addr,
6921 .ndo_set_mac_address = i40e_set_mac,
6922 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006923 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006924 .ndo_tx_timeout = i40e_tx_timeout,
6925 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
6926 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
6927#ifdef CONFIG_NET_POLL_CONTROLLER
6928 .ndo_poll_controller = i40e_netpoll,
6929#endif
6930 .ndo_setup_tc = i40e_setup_tc,
6931 .ndo_set_features = i40e_set_features,
6932 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
6933 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04006934 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006935 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00006936 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Mitch Williamsc674d122014-05-20 08:01:40 +00006937 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofck,
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006938#ifdef CONFIG_I40E_VXLAN
6939 .ndo_add_vxlan_port = i40e_add_vxlan_port,
6940 .ndo_del_vxlan_port = i40e_del_vxlan_port,
6941#endif
Greg Rose4ba0dea2014-03-06 08:59:55 +00006942#ifdef HAVE_FDB_OPS
6943 .ndo_fdb_add = i40e_ndo_fdb_add,
6944#ifndef USE_DEFAULT_FDB_DEL_DUMP
6945 .ndo_fdb_del = i40e_ndo_fdb_del,
6946 .ndo_fdb_dump = i40e_ndo_fdb_dump,
6947#endif
6948#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006949};
6950
6951/**
6952 * i40e_config_netdev - Setup the netdev flags
6953 * @vsi: the VSI being configured
6954 *
6955 * Returns 0 on success, negative value on failure
6956 **/
6957static int i40e_config_netdev(struct i40e_vsi *vsi)
6958{
Greg Rose1a103702013-11-28 06:42:39 +00006959 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006960 struct i40e_pf *pf = vsi->back;
6961 struct i40e_hw *hw = &pf->hw;
6962 struct i40e_netdev_priv *np;
6963 struct net_device *netdev;
6964 u8 mac_addr[ETH_ALEN];
6965 int etherdev_size;
6966
6967 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006968 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006969 if (!netdev)
6970 return -ENOMEM;
6971
6972 vsi->netdev = netdev;
6973 np = netdev_priv(netdev);
6974 np->vsi = vsi;
6975
Or Gerlitzd70e9412014-03-18 10:36:45 +02006976 netdev->hw_enc_features |= NETIF_F_IP_CSUM |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006977 NETIF_F_GSO_UDP_TUNNEL |
Or Gerlitzd70e9412014-03-18 10:36:45 +02006978 NETIF_F_TSO;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006979
6980 netdev->features = NETIF_F_SG |
6981 NETIF_F_IP_CSUM |
6982 NETIF_F_SCTP_CSUM |
6983 NETIF_F_HIGHDMA |
6984 NETIF_F_GSO_UDP_TUNNEL |
6985 NETIF_F_HW_VLAN_CTAG_TX |
6986 NETIF_F_HW_VLAN_CTAG_RX |
6987 NETIF_F_HW_VLAN_CTAG_FILTER |
6988 NETIF_F_IPV6_CSUM |
6989 NETIF_F_TSO |
Jesse Brandeburg059dab62014-04-01 09:07:20 +00006990 NETIF_F_TSO_ECN |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006991 NETIF_F_TSO6 |
6992 NETIF_F_RXCSUM |
6993 NETIF_F_RXHASH |
6994 0;
6995
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00006996 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
6997 netdev->features |= NETIF_F_NTUPLE;
6998
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006999 /* copy netdev features into list of user selectable features */
7000 netdev->hw_features |= netdev->features;
7001
7002 if (vsi->type == I40E_VSI_MAIN) {
7003 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00007004 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Greg Rose8c27d422014-05-22 06:31:56 +00007005 /* The following two steps are necessary to prevent reception
7006 * of tagged packets - by default the NVM loads a MAC-VLAN
7007 * filter that will accept any tagged packet. This is to
7008 * prevent that during normal operations until a specific
7009 * VLAN tag filter has been set.
7010 */
7011 i40e_rm_default_mac_filter(vsi, mac_addr);
7012 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007013 } else {
7014 /* relate the VSI_VMDQ name to the VSI_MAIN name */
7015 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
7016 pf->vsi[pf->lan_vsi]->netdev->name);
7017 random_ether_addr(mac_addr);
7018 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
7019 }
Greg Rose1a103702013-11-28 06:42:39 +00007020 i40e_add_filter(vsi, brdcast, I40E_VLAN_ANY, false, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007021
Greg Rose9a173902014-05-22 06:32:02 +00007022 ether_addr_copy(netdev->dev_addr, mac_addr);
7023 ether_addr_copy(netdev->perm_addr, mac_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007024 /* vlan gets same features (except vlan offload)
7025 * after any tweaks for specific VSI types
7026 */
7027 netdev->vlan_features = netdev->features & ~(NETIF_F_HW_VLAN_CTAG_TX |
7028 NETIF_F_HW_VLAN_CTAG_RX |
7029 NETIF_F_HW_VLAN_CTAG_FILTER);
7030 netdev->priv_flags |= IFF_UNICAST_FLT;
7031 netdev->priv_flags |= IFF_SUPP_NOFCS;
7032 /* Setup netdev TC information */
7033 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
7034
7035 netdev->netdev_ops = &i40e_netdev_ops;
7036 netdev->watchdog_timeo = 5 * HZ;
7037 i40e_set_ethtool_ops(netdev);
7038
7039 return 0;
7040}
7041
7042/**
7043 * i40e_vsi_delete - Delete a VSI from the switch
7044 * @vsi: the VSI being removed
7045 *
7046 * Returns 0 on success, negative value on failure
7047 **/
7048static void i40e_vsi_delete(struct i40e_vsi *vsi)
7049{
7050 /* remove default VSI is not allowed */
7051 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
7052 return;
7053
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007054 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007055}
7056
7057/**
7058 * i40e_add_vsi - Add a VSI to the switch
7059 * @vsi: the VSI being configured
7060 *
7061 * This initializes a VSI context depending on the VSI type to be added and
7062 * passes it down to the add_vsi aq command.
7063 **/
7064static int i40e_add_vsi(struct i40e_vsi *vsi)
7065{
7066 int ret = -ENODEV;
7067 struct i40e_mac_filter *f, *ftmp;
7068 struct i40e_pf *pf = vsi->back;
7069 struct i40e_hw *hw = &pf->hw;
7070 struct i40e_vsi_context ctxt;
7071 u8 enabled_tc = 0x1; /* TC0 enabled */
7072 int f_count = 0;
7073
7074 memset(&ctxt, 0, sizeof(ctxt));
7075 switch (vsi->type) {
7076 case I40E_VSI_MAIN:
7077 /* The PF's main VSI is already setup as part of the
7078 * device initialization, so we'll not bother with
7079 * the add_vsi call, but we will retrieve the current
7080 * VSI context.
7081 */
7082 ctxt.seid = pf->main_vsi_seid;
7083 ctxt.pf_num = pf->hw.pf_id;
7084 ctxt.vf_num = 0;
7085 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
7086 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
7087 if (ret) {
7088 dev_info(&pf->pdev->dev,
7089 "couldn't get pf vsi config, err %d, aq_err %d\n",
7090 ret, pf->hw.aq.asq_last_status);
7091 return -ENOENT;
7092 }
7093 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
7094 vsi->info.valid_sections = 0;
7095
7096 vsi->seid = ctxt.seid;
7097 vsi->id = ctxt.vsi_number;
7098
7099 enabled_tc = i40e_pf_get_tc_map(pf);
7100
7101 /* MFP mode setup queue map and update VSI */
7102 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
7103 memset(&ctxt, 0, sizeof(ctxt));
7104 ctxt.seid = pf->main_vsi_seid;
7105 ctxt.pf_num = pf->hw.pf_id;
7106 ctxt.vf_num = 0;
7107 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
7108 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7109 if (ret) {
7110 dev_info(&pf->pdev->dev,
7111 "update vsi failed, aq_err=%d\n",
7112 pf->hw.aq.asq_last_status);
7113 ret = -ENOENT;
7114 goto err;
7115 }
7116 /* update the local VSI info queue map */
7117 i40e_vsi_update_queue_map(vsi, &ctxt);
7118 vsi->info.valid_sections = 0;
7119 } else {
7120 /* Default/Main VSI is only enabled for TC0
7121 * reconfigure it to enable all TCs that are
7122 * available on the port in SFP mode.
7123 */
7124 ret = i40e_vsi_config_tc(vsi, enabled_tc);
7125 if (ret) {
7126 dev_info(&pf->pdev->dev,
7127 "failed to configure TCs for main VSI tc_map 0x%08x, err %d, aq_err %d\n",
7128 enabled_tc, ret,
7129 pf->hw.aq.asq_last_status);
7130 ret = -ENOENT;
7131 }
7132 }
7133 break;
7134
7135 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007136 ctxt.pf_num = hw->pf_id;
7137 ctxt.vf_num = 0;
7138 ctxt.uplink_seid = vsi->uplink_seid;
7139 ctxt.connection_type = 0x1; /* regular data port */
7140 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007141 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007142 break;
7143
7144 case I40E_VSI_VMDQ2:
7145 ctxt.pf_num = hw->pf_id;
7146 ctxt.vf_num = 0;
7147 ctxt.uplink_seid = vsi->uplink_seid;
7148 ctxt.connection_type = 0x1; /* regular data port */
7149 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
7150
7151 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
7152
7153 /* This VSI is connected to VEB so the switch_id
7154 * should be set to zero by default.
7155 */
7156 ctxt.info.switch_id = 0;
7157 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
7158 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
7159
7160 /* Setup the VSI tx/rx queue map for TC0 only for now */
7161 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
7162 break;
7163
7164 case I40E_VSI_SRIOV:
7165 ctxt.pf_num = hw->pf_id;
7166 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
7167 ctxt.uplink_seid = vsi->uplink_seid;
7168 ctxt.connection_type = 0x1; /* regular data port */
7169 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
7170
7171 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
7172
7173 /* This VSI is connected to VEB so the switch_id
7174 * should be set to zero by default.
7175 */
7176 ctxt.info.switch_id = cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
7177
7178 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
7179 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00007180 if (pf->vf[vsi->vf_id].spoofchk) {
7181 ctxt.info.valid_sections |=
7182 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
7183 ctxt.info.sec_flags |=
7184 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
7185 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
7186 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007187 /* Setup the VSI tx/rx queue map for TC0 only for now */
7188 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
7189 break;
7190
7191 default:
7192 return -ENODEV;
7193 }
7194
7195 if (vsi->type != I40E_VSI_MAIN) {
7196 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
7197 if (ret) {
7198 dev_info(&vsi->back->pdev->dev,
7199 "add vsi failed, aq_err=%d\n",
7200 vsi->back->hw.aq.asq_last_status);
7201 ret = -ENOENT;
7202 goto err;
7203 }
7204 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
7205 vsi->info.valid_sections = 0;
7206 vsi->seid = ctxt.seid;
7207 vsi->id = ctxt.vsi_number;
7208 }
7209
7210 /* If macvlan filters already exist, force them to get loaded */
7211 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
7212 f->changed = true;
7213 f_count++;
7214 }
7215 if (f_count) {
7216 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
7217 pf->flags |= I40E_FLAG_FILTER_SYNC;
7218 }
7219
7220 /* Update VSI BW information */
7221 ret = i40e_vsi_get_bw_info(vsi);
7222 if (ret) {
7223 dev_info(&pf->pdev->dev,
7224 "couldn't get vsi bw info, err %d, aq_err %d\n",
7225 ret, pf->hw.aq.asq_last_status);
7226 /* VSI is already added so not tearing that up */
7227 ret = 0;
7228 }
7229
7230err:
7231 return ret;
7232}
7233
7234/**
7235 * i40e_vsi_release - Delete a VSI and free its resources
7236 * @vsi: the VSI being removed
7237 *
7238 * Returns 0 on success or < 0 on error
7239 **/
7240int i40e_vsi_release(struct i40e_vsi *vsi)
7241{
7242 struct i40e_mac_filter *f, *ftmp;
7243 struct i40e_veb *veb = NULL;
7244 struct i40e_pf *pf;
7245 u16 uplink_seid;
7246 int i, n;
7247
7248 pf = vsi->back;
7249
7250 /* release of a VEB-owner or last VSI is not allowed */
7251 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
7252 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
7253 vsi->seid, vsi->uplink_seid);
7254 return -ENODEV;
7255 }
7256 if (vsi == pf->vsi[pf->lan_vsi] &&
7257 !test_bit(__I40E_DOWN, &pf->state)) {
7258 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
7259 return -ENODEV;
7260 }
7261
7262 uplink_seid = vsi->uplink_seid;
7263 if (vsi->type != I40E_VSI_SRIOV) {
7264 if (vsi->netdev_registered) {
7265 vsi->netdev_registered = false;
7266 if (vsi->netdev) {
7267 /* results in a call to i40e_close() */
7268 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007269 }
7270 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00007271 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007272 }
7273 i40e_vsi_disable_irq(vsi);
7274 }
7275
7276 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
7277 i40e_del_filter(vsi, f->macaddr, f->vlan,
7278 f->is_vf, f->is_netdev);
7279 i40e_sync_vsi_filters(vsi);
7280
7281 i40e_vsi_delete(vsi);
7282 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00007283 if (vsi->netdev) {
7284 free_netdev(vsi->netdev);
7285 vsi->netdev = NULL;
7286 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007287 i40e_vsi_clear_rings(vsi);
7288 i40e_vsi_clear(vsi);
7289
7290 /* If this was the last thing on the VEB, except for the
7291 * controlling VSI, remove the VEB, which puts the controlling
7292 * VSI onto the next level down in the switch.
7293 *
7294 * Well, okay, there's one more exception here: don't remove
7295 * the orphan VEBs yet. We'll wait for an explicit remove request
7296 * from up the network stack.
7297 */
Mitch Williams505682c2014-05-20 08:01:37 +00007298 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007299 if (pf->vsi[i] &&
7300 pf->vsi[i]->uplink_seid == uplink_seid &&
7301 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
7302 n++; /* count the VSIs */
7303 }
7304 }
7305 for (i = 0; i < I40E_MAX_VEB; i++) {
7306 if (!pf->veb[i])
7307 continue;
7308 if (pf->veb[i]->uplink_seid == uplink_seid)
7309 n++; /* count the VEBs */
7310 if (pf->veb[i]->seid == uplink_seid)
7311 veb = pf->veb[i];
7312 }
7313 if (n == 0 && veb && veb->uplink_seid != 0)
7314 i40e_veb_release(veb);
7315
7316 return 0;
7317}
7318
7319/**
7320 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
7321 * @vsi: ptr to the VSI
7322 *
7323 * This should only be called after i40e_vsi_mem_alloc() which allocates the
7324 * corresponding SW VSI structure and initializes num_queue_pairs for the
7325 * newly allocated VSI.
7326 *
7327 * Returns 0 on success or negative on failure
7328 **/
7329static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
7330{
7331 int ret = -ENOENT;
7332 struct i40e_pf *pf = vsi->back;
7333
Alexander Duyck493fb302013-09-28 07:01:44 +00007334 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007335 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
7336 vsi->seid);
7337 return -EEXIST;
7338 }
7339
7340 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00007341 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007342 vsi->seid, vsi->base_vector);
7343 return -EEXIST;
7344 }
7345
Greg Rose90e04072014-03-06 08:59:57 +00007346 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007347 if (ret) {
7348 dev_info(&pf->pdev->dev,
7349 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
7350 vsi->num_q_vectors, vsi->seid, ret);
7351 vsi->num_q_vectors = 0;
7352 goto vector_setup_out;
7353 }
7354
Shannon Nelson958a3e32013-09-28 07:13:28 +00007355 if (vsi->num_q_vectors)
7356 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
7357 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007358 if (vsi->base_vector < 0) {
7359 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00007360 "failed to get queue tracking for VSI %d, err=%d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007361 vsi->seid, vsi->base_vector);
7362 i40e_vsi_free_q_vectors(vsi);
7363 ret = -ENOENT;
7364 goto vector_setup_out;
7365 }
7366
7367vector_setup_out:
7368 return ret;
7369}
7370
7371/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007372 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
7373 * @vsi: pointer to the vsi.
7374 *
7375 * This re-allocates a vsi's queue resources.
7376 *
7377 * Returns pointer to the successfully allocated and configured VSI sw struct
7378 * on success, otherwise returns NULL on failure.
7379 **/
7380static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
7381{
7382 struct i40e_pf *pf = vsi->back;
7383 u8 enabled_tc;
7384 int ret;
7385
7386 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7387 i40e_vsi_clear_rings(vsi);
7388
7389 i40e_vsi_free_arrays(vsi, false);
7390 i40e_set_num_rings_in_vsi(vsi);
7391 ret = i40e_vsi_alloc_arrays(vsi, false);
7392 if (ret)
7393 goto err_vsi;
7394
7395 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
7396 if (ret < 0) {
7397 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
7398 vsi->seid, ret);
7399 goto err_vsi;
7400 }
7401 vsi->base_queue = ret;
7402
7403 /* Update the FW view of the VSI. Force a reset of TC and queue
7404 * layout configurations.
7405 */
7406 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
7407 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
7408 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
7409 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
7410
7411 /* assign it some queues */
7412 ret = i40e_alloc_rings(vsi);
7413 if (ret)
7414 goto err_rings;
7415
7416 /* map all of the rings to the q_vectors */
7417 i40e_vsi_map_rings_to_vectors(vsi);
7418 return vsi;
7419
7420err_rings:
7421 i40e_vsi_free_q_vectors(vsi);
7422 if (vsi->netdev_registered) {
7423 vsi->netdev_registered = false;
7424 unregister_netdev(vsi->netdev);
7425 free_netdev(vsi->netdev);
7426 vsi->netdev = NULL;
7427 }
7428 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
7429err_vsi:
7430 i40e_vsi_clear(vsi);
7431 return NULL;
7432}
7433
7434/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007435 * i40e_vsi_setup - Set up a VSI by a given type
7436 * @pf: board private structure
7437 * @type: VSI type
7438 * @uplink_seid: the switch element to link to
7439 * @param1: usage depends upon VSI type. For VF types, indicates VF id
7440 *
7441 * This allocates the sw VSI structure and its queue resources, then add a VSI
7442 * to the identified VEB.
7443 *
7444 * Returns pointer to the successfully allocated and configure VSI sw struct on
7445 * success, otherwise returns NULL on failure.
7446 **/
7447struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
7448 u16 uplink_seid, u32 param1)
7449{
7450 struct i40e_vsi *vsi = NULL;
7451 struct i40e_veb *veb = NULL;
7452 int ret, i;
7453 int v_idx;
7454
7455 /* The requested uplink_seid must be either
7456 * - the PF's port seid
7457 * no VEB is needed because this is the PF
7458 * or this is a Flow Director special case VSI
7459 * - seid of an existing VEB
7460 * - seid of a VSI that owns an existing VEB
7461 * - seid of a VSI that doesn't own a VEB
7462 * a new VEB is created and the VSI becomes the owner
7463 * - seid of the PF VSI, which is what creates the first VEB
7464 * this is a special case of the previous
7465 *
7466 * Find which uplink_seid we were given and create a new VEB if needed
7467 */
7468 for (i = 0; i < I40E_MAX_VEB; i++) {
7469 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
7470 veb = pf->veb[i];
7471 break;
7472 }
7473 }
7474
7475 if (!veb && uplink_seid != pf->mac_seid) {
7476
Mitch Williams505682c2014-05-20 08:01:37 +00007477 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007478 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
7479 vsi = pf->vsi[i];
7480 break;
7481 }
7482 }
7483 if (!vsi) {
7484 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
7485 uplink_seid);
7486 return NULL;
7487 }
7488
7489 if (vsi->uplink_seid == pf->mac_seid)
7490 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
7491 vsi->tc_config.enabled_tc);
7492 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
7493 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
7494 vsi->tc_config.enabled_tc);
7495
7496 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
7497 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
7498 veb = pf->veb[i];
7499 }
7500 if (!veb) {
7501 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
7502 return NULL;
7503 }
7504
7505 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
7506 uplink_seid = veb->seid;
7507 }
7508
7509 /* get vsi sw struct */
7510 v_idx = i40e_vsi_mem_alloc(pf, type);
7511 if (v_idx < 0)
7512 goto err_alloc;
7513 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007514 if (!vsi)
7515 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007516 vsi->type = type;
7517 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
7518
7519 if (type == I40E_VSI_MAIN)
7520 pf->lan_vsi = v_idx;
7521 else if (type == I40E_VSI_SRIOV)
7522 vsi->vf_id = param1;
7523 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007524 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
7525 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007526 if (ret < 0) {
7527 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
7528 vsi->seid, ret);
7529 goto err_vsi;
7530 }
7531 vsi->base_queue = ret;
7532
7533 /* get a VSI from the hardware */
7534 vsi->uplink_seid = uplink_seid;
7535 ret = i40e_add_vsi(vsi);
7536 if (ret)
7537 goto err_vsi;
7538
7539 switch (vsi->type) {
7540 /* setup the netdev if needed */
7541 case I40E_VSI_MAIN:
7542 case I40E_VSI_VMDQ2:
7543 ret = i40e_config_netdev(vsi);
7544 if (ret)
7545 goto err_netdev;
7546 ret = register_netdev(vsi->netdev);
7547 if (ret)
7548 goto err_netdev;
7549 vsi->netdev_registered = true;
7550 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007551#ifdef CONFIG_I40E_DCB
7552 /* Setup DCB netlink interface */
7553 i40e_dcbnl_setup(vsi);
7554#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007555 /* fall through */
7556
7557 case I40E_VSI_FDIR:
7558 /* set up vectors and rings if needed */
7559 ret = i40e_vsi_setup_vectors(vsi);
7560 if (ret)
7561 goto err_msix;
7562
7563 ret = i40e_alloc_rings(vsi);
7564 if (ret)
7565 goto err_rings;
7566
7567 /* map all of the rings to the q_vectors */
7568 i40e_vsi_map_rings_to_vectors(vsi);
7569
7570 i40e_vsi_reset_stats(vsi);
7571 break;
7572
7573 default:
7574 /* no netdev or rings for the other VSI types */
7575 break;
7576 }
7577
7578 return vsi;
7579
7580err_rings:
7581 i40e_vsi_free_q_vectors(vsi);
7582err_msix:
7583 if (vsi->netdev_registered) {
7584 vsi->netdev_registered = false;
7585 unregister_netdev(vsi->netdev);
7586 free_netdev(vsi->netdev);
7587 vsi->netdev = NULL;
7588 }
7589err_netdev:
7590 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
7591err_vsi:
7592 i40e_vsi_clear(vsi);
7593err_alloc:
7594 return NULL;
7595}
7596
7597/**
7598 * i40e_veb_get_bw_info - Query VEB BW information
7599 * @veb: the veb to query
7600 *
7601 * Query the Tx scheduler BW configuration data for given VEB
7602 **/
7603static int i40e_veb_get_bw_info(struct i40e_veb *veb)
7604{
7605 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
7606 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
7607 struct i40e_pf *pf = veb->pf;
7608 struct i40e_hw *hw = &pf->hw;
7609 u32 tc_bw_max;
7610 int ret = 0;
7611 int i;
7612
7613 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
7614 &bw_data, NULL);
7615 if (ret) {
7616 dev_info(&pf->pdev->dev,
7617 "query veb bw config failed, aq_err=%d\n",
7618 hw->aq.asq_last_status);
7619 goto out;
7620 }
7621
7622 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
7623 &ets_data, NULL);
7624 if (ret) {
7625 dev_info(&pf->pdev->dev,
7626 "query veb bw ets config failed, aq_err=%d\n",
7627 hw->aq.asq_last_status);
7628 goto out;
7629 }
7630
7631 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
7632 veb->bw_max_quanta = ets_data.tc_bw_max;
7633 veb->is_abs_credits = bw_data.absolute_credits_enable;
7634 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
7635 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
7636 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7637 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
7638 veb->bw_tc_limit_credits[i] =
7639 le16_to_cpu(bw_data.tc_bw_limits[i]);
7640 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
7641 }
7642
7643out:
7644 return ret;
7645}
7646
7647/**
7648 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
7649 * @pf: board private structure
7650 *
7651 * On error: returns error code (negative)
7652 * On success: returns vsi index in PF (positive)
7653 **/
7654static int i40e_veb_mem_alloc(struct i40e_pf *pf)
7655{
7656 int ret = -ENOENT;
7657 struct i40e_veb *veb;
7658 int i;
7659
7660 /* Need to protect the allocation of switch elements at the PF level */
7661 mutex_lock(&pf->switch_mutex);
7662
7663 /* VEB list may be fragmented if VEB creation/destruction has
7664 * been happening. We can afford to do a quick scan to look
7665 * for any free slots in the list.
7666 *
7667 * find next empty veb slot, looping back around if necessary
7668 */
7669 i = 0;
7670 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
7671 i++;
7672 if (i >= I40E_MAX_VEB) {
7673 ret = -ENOMEM;
7674 goto err_alloc_veb; /* out of VEB slots! */
7675 }
7676
7677 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
7678 if (!veb) {
7679 ret = -ENOMEM;
7680 goto err_alloc_veb;
7681 }
7682 veb->pf = pf;
7683 veb->idx = i;
7684 veb->enabled_tc = 1;
7685
7686 pf->veb[i] = veb;
7687 ret = i;
7688err_alloc_veb:
7689 mutex_unlock(&pf->switch_mutex);
7690 return ret;
7691}
7692
7693/**
7694 * i40e_switch_branch_release - Delete a branch of the switch tree
7695 * @branch: where to start deleting
7696 *
7697 * This uses recursion to find the tips of the branch to be
7698 * removed, deleting until we get back to and can delete this VEB.
7699 **/
7700static void i40e_switch_branch_release(struct i40e_veb *branch)
7701{
7702 struct i40e_pf *pf = branch->pf;
7703 u16 branch_seid = branch->seid;
7704 u16 veb_idx = branch->idx;
7705 int i;
7706
7707 /* release any VEBs on this VEB - RECURSION */
7708 for (i = 0; i < I40E_MAX_VEB; i++) {
7709 if (!pf->veb[i])
7710 continue;
7711 if (pf->veb[i]->uplink_seid == branch->seid)
7712 i40e_switch_branch_release(pf->veb[i]);
7713 }
7714
7715 /* Release the VSIs on this VEB, but not the owner VSI.
7716 *
7717 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
7718 * the VEB itself, so don't use (*branch) after this loop.
7719 */
Mitch Williams505682c2014-05-20 08:01:37 +00007720 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007721 if (!pf->vsi[i])
7722 continue;
7723 if (pf->vsi[i]->uplink_seid == branch_seid &&
7724 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
7725 i40e_vsi_release(pf->vsi[i]);
7726 }
7727 }
7728
7729 /* There's one corner case where the VEB might not have been
7730 * removed, so double check it here and remove it if needed.
7731 * This case happens if the veb was created from the debugfs
7732 * commands and no VSIs were added to it.
7733 */
7734 if (pf->veb[veb_idx])
7735 i40e_veb_release(pf->veb[veb_idx]);
7736}
7737
7738/**
7739 * i40e_veb_clear - remove veb struct
7740 * @veb: the veb to remove
7741 **/
7742static void i40e_veb_clear(struct i40e_veb *veb)
7743{
7744 if (!veb)
7745 return;
7746
7747 if (veb->pf) {
7748 struct i40e_pf *pf = veb->pf;
7749
7750 mutex_lock(&pf->switch_mutex);
7751 if (pf->veb[veb->idx] == veb)
7752 pf->veb[veb->idx] = NULL;
7753 mutex_unlock(&pf->switch_mutex);
7754 }
7755
7756 kfree(veb);
7757}
7758
7759/**
7760 * i40e_veb_release - Delete a VEB and free its resources
7761 * @veb: the VEB being removed
7762 **/
7763void i40e_veb_release(struct i40e_veb *veb)
7764{
7765 struct i40e_vsi *vsi = NULL;
7766 struct i40e_pf *pf;
7767 int i, n = 0;
7768
7769 pf = veb->pf;
7770
7771 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +00007772 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007773 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
7774 n++;
7775 vsi = pf->vsi[i];
7776 }
7777 }
7778 if (n != 1) {
7779 dev_info(&pf->pdev->dev,
7780 "can't remove VEB %d with %d VSIs left\n",
7781 veb->seid, n);
7782 return;
7783 }
7784
7785 /* move the remaining VSI to uplink veb */
7786 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
7787 if (veb->uplink_seid) {
7788 vsi->uplink_seid = veb->uplink_seid;
7789 if (veb->uplink_seid == pf->mac_seid)
7790 vsi->veb_idx = I40E_NO_VEB;
7791 else
7792 vsi->veb_idx = veb->veb_idx;
7793 } else {
7794 /* floating VEB */
7795 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
7796 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
7797 }
7798
7799 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
7800 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007801}
7802
7803/**
7804 * i40e_add_veb - create the VEB in the switch
7805 * @veb: the VEB to be instantiated
7806 * @vsi: the controlling VSI
7807 **/
7808static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
7809{
Greg Rose56747262013-11-28 06:39:37 +00007810 bool is_default = false;
Kevin Scotte1c51b952013-11-20 10:02:51 +00007811 bool is_cloud = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007812 int ret;
7813
7814 /* get a VEB from the hardware */
7815 ret = i40e_aq_add_veb(&veb->pf->hw, veb->uplink_seid, vsi->seid,
Kevin Scotte1c51b952013-11-20 10:02:51 +00007816 veb->enabled_tc, is_default,
7817 is_cloud, &veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007818 if (ret) {
7819 dev_info(&veb->pf->pdev->dev,
7820 "couldn't add VEB, err %d, aq_err %d\n",
7821 ret, veb->pf->hw.aq.asq_last_status);
7822 return -EPERM;
7823 }
7824
7825 /* get statistics counter */
7826 ret = i40e_aq_get_veb_parameters(&veb->pf->hw, veb->seid, NULL, NULL,
7827 &veb->stats_idx, NULL, NULL, NULL);
7828 if (ret) {
7829 dev_info(&veb->pf->pdev->dev,
7830 "couldn't get VEB statistics idx, err %d, aq_err %d\n",
7831 ret, veb->pf->hw.aq.asq_last_status);
7832 return -EPERM;
7833 }
7834 ret = i40e_veb_get_bw_info(veb);
7835 if (ret) {
7836 dev_info(&veb->pf->pdev->dev,
7837 "couldn't get VEB bw info, err %d, aq_err %d\n",
7838 ret, veb->pf->hw.aq.asq_last_status);
7839 i40e_aq_delete_element(&veb->pf->hw, veb->seid, NULL);
7840 return -ENOENT;
7841 }
7842
7843 vsi->uplink_seid = veb->seid;
7844 vsi->veb_idx = veb->idx;
7845 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
7846
7847 return 0;
7848}
7849
7850/**
7851 * i40e_veb_setup - Set up a VEB
7852 * @pf: board private structure
7853 * @flags: VEB setup flags
7854 * @uplink_seid: the switch element to link to
7855 * @vsi_seid: the initial VSI seid
7856 * @enabled_tc: Enabled TC bit-map
7857 *
7858 * This allocates the sw VEB structure and links it into the switch
7859 * It is possible and legal for this to be a duplicate of an already
7860 * existing VEB. It is also possible for both uplink and vsi seids
7861 * to be zero, in order to create a floating VEB.
7862 *
7863 * Returns pointer to the successfully allocated VEB sw struct on
7864 * success, otherwise returns NULL on failure.
7865 **/
7866struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
7867 u16 uplink_seid, u16 vsi_seid,
7868 u8 enabled_tc)
7869{
7870 struct i40e_veb *veb, *uplink_veb = NULL;
7871 int vsi_idx, veb_idx;
7872 int ret;
7873
7874 /* if one seid is 0, the other must be 0 to create a floating relay */
7875 if ((uplink_seid == 0 || vsi_seid == 0) &&
7876 (uplink_seid + vsi_seid != 0)) {
7877 dev_info(&pf->pdev->dev,
7878 "one, not both seid's are 0: uplink=%d vsi=%d\n",
7879 uplink_seid, vsi_seid);
7880 return NULL;
7881 }
7882
7883 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +00007884 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007885 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
7886 break;
Mitch Williams505682c2014-05-20 08:01:37 +00007887 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007888 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
7889 vsi_seid);
7890 return NULL;
7891 }
7892
7893 if (uplink_seid && uplink_seid != pf->mac_seid) {
7894 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
7895 if (pf->veb[veb_idx] &&
7896 pf->veb[veb_idx]->seid == uplink_seid) {
7897 uplink_veb = pf->veb[veb_idx];
7898 break;
7899 }
7900 }
7901 if (!uplink_veb) {
7902 dev_info(&pf->pdev->dev,
7903 "uplink seid %d not found\n", uplink_seid);
7904 return NULL;
7905 }
7906 }
7907
7908 /* get veb sw struct */
7909 veb_idx = i40e_veb_mem_alloc(pf);
7910 if (veb_idx < 0)
7911 goto err_alloc;
7912 veb = pf->veb[veb_idx];
7913 veb->flags = flags;
7914 veb->uplink_seid = uplink_seid;
7915 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
7916 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
7917
7918 /* create the VEB in the switch */
7919 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
7920 if (ret)
7921 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +00007922 if (vsi_idx == pf->lan_vsi)
7923 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007924
7925 return veb;
7926
7927err_veb:
7928 i40e_veb_clear(veb);
7929err_alloc:
7930 return NULL;
7931}
7932
7933/**
7934 * i40e_setup_pf_switch_element - set pf vars based on switch type
7935 * @pf: board private structure
7936 * @ele: element we are building info from
7937 * @num_reported: total number of elements
7938 * @printconfig: should we print the contents
7939 *
7940 * helper function to assist in extracting a few useful SEID values.
7941 **/
7942static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
7943 struct i40e_aqc_switch_config_element_resp *ele,
7944 u16 num_reported, bool printconfig)
7945{
7946 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
7947 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
7948 u8 element_type = ele->element_type;
7949 u16 seid = le16_to_cpu(ele->seid);
7950
7951 if (printconfig)
7952 dev_info(&pf->pdev->dev,
7953 "type=%d seid=%d uplink=%d downlink=%d\n",
7954 element_type, seid, uplink_seid, downlink_seid);
7955
7956 switch (element_type) {
7957 case I40E_SWITCH_ELEMENT_TYPE_MAC:
7958 pf->mac_seid = seid;
7959 break;
7960 case I40E_SWITCH_ELEMENT_TYPE_VEB:
7961 /* Main VEB? */
7962 if (uplink_seid != pf->mac_seid)
7963 break;
7964 if (pf->lan_veb == I40E_NO_VEB) {
7965 int v;
7966
7967 /* find existing or else empty VEB */
7968 for (v = 0; v < I40E_MAX_VEB; v++) {
7969 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
7970 pf->lan_veb = v;
7971 break;
7972 }
7973 }
7974 if (pf->lan_veb == I40E_NO_VEB) {
7975 v = i40e_veb_mem_alloc(pf);
7976 if (v < 0)
7977 break;
7978 pf->lan_veb = v;
7979 }
7980 }
7981
7982 pf->veb[pf->lan_veb]->seid = seid;
7983 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
7984 pf->veb[pf->lan_veb]->pf = pf;
7985 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
7986 break;
7987 case I40E_SWITCH_ELEMENT_TYPE_VSI:
7988 if (num_reported != 1)
7989 break;
7990 /* This is immediately after a reset so we can assume this is
7991 * the PF's VSI
7992 */
7993 pf->mac_seid = uplink_seid;
7994 pf->pf_seid = downlink_seid;
7995 pf->main_vsi_seid = seid;
7996 if (printconfig)
7997 dev_info(&pf->pdev->dev,
7998 "pf_seid=%d main_vsi_seid=%d\n",
7999 pf->pf_seid, pf->main_vsi_seid);
8000 break;
8001 case I40E_SWITCH_ELEMENT_TYPE_PF:
8002 case I40E_SWITCH_ELEMENT_TYPE_VF:
8003 case I40E_SWITCH_ELEMENT_TYPE_EMP:
8004 case I40E_SWITCH_ELEMENT_TYPE_BMC:
8005 case I40E_SWITCH_ELEMENT_TYPE_PE:
8006 case I40E_SWITCH_ELEMENT_TYPE_PA:
8007 /* ignore these for now */
8008 break;
8009 default:
8010 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
8011 element_type, seid);
8012 break;
8013 }
8014}
8015
8016/**
8017 * i40e_fetch_switch_configuration - Get switch config from firmware
8018 * @pf: board private structure
8019 * @printconfig: should we print the contents
8020 *
8021 * Get the current switch configuration from the device and
8022 * extract a few useful SEID values.
8023 **/
8024int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
8025{
8026 struct i40e_aqc_get_switch_config_resp *sw_config;
8027 u16 next_seid = 0;
8028 int ret = 0;
8029 u8 *aq_buf;
8030 int i;
8031
8032 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
8033 if (!aq_buf)
8034 return -ENOMEM;
8035
8036 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
8037 do {
8038 u16 num_reported, num_total;
8039
8040 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
8041 I40E_AQ_LARGE_BUF,
8042 &next_seid, NULL);
8043 if (ret) {
8044 dev_info(&pf->pdev->dev,
8045 "get switch config failed %d aq_err=%x\n",
8046 ret, pf->hw.aq.asq_last_status);
8047 kfree(aq_buf);
8048 return -ENOENT;
8049 }
8050
8051 num_reported = le16_to_cpu(sw_config->header.num_reported);
8052 num_total = le16_to_cpu(sw_config->header.num_total);
8053
8054 if (printconfig)
8055 dev_info(&pf->pdev->dev,
8056 "header: %d reported %d total\n",
8057 num_reported, num_total);
8058
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008059 for (i = 0; i < num_reported; i++) {
8060 struct i40e_aqc_switch_config_element_resp *ele =
8061 &sw_config->element[i];
8062
8063 i40e_setup_pf_switch_element(pf, ele, num_reported,
8064 printconfig);
8065 }
8066 } while (next_seid != 0);
8067
8068 kfree(aq_buf);
8069 return ret;
8070}
8071
8072/**
8073 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
8074 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008075 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008076 *
8077 * Returns 0 on success, negative value on failure
8078 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008079static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008080{
Jesse Brandeburg895106a2013-11-26 10:49:16 +00008081 u32 rxfc = 0, txfc = 0, rxfc_reg;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008082 int ret;
8083
8084 /* find out what's out there already */
8085 ret = i40e_fetch_switch_configuration(pf, false);
8086 if (ret) {
8087 dev_info(&pf->pdev->dev,
8088 "couldn't fetch switch config, err %d, aq_err %d\n",
8089 ret, pf->hw.aq.asq_last_status);
8090 return ret;
8091 }
8092 i40e_pf_reset_stats(pf);
8093
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008094 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008095 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008096 struct i40e_vsi *vsi = NULL;
8097 u16 uplink_seid;
8098
8099 /* Set up the PF VSI associated with the PF's main VSI
8100 * that is already in the HW switch
8101 */
8102 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
8103 uplink_seid = pf->veb[pf->lan_veb]->seid;
8104 else
8105 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008106 if (pf->lan_vsi == I40E_NO_VSI)
8107 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
8108 else if (reinit)
8109 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008110 if (!vsi) {
8111 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
8112 i40e_fdir_teardown(pf);
8113 return -EAGAIN;
8114 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008115 } else {
8116 /* force a reset of TC and queue layout configurations */
8117 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
8118 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
8119 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
8120 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
8121 }
8122 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
8123
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008124 i40e_fdir_sb_setup(pf);
8125
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008126 /* Setup static PF queue filter control settings */
8127 ret = i40e_setup_pf_filter_control(pf);
8128 if (ret) {
8129 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
8130 ret);
8131 /* Failure here should not stop continuing other steps */
8132 }
8133
8134 /* enable RSS in the HW, even for only one queue, as the stack can use
8135 * the hash
8136 */
8137 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
8138 i40e_config_rss(pf);
8139
8140 /* fill in link information and enable LSE reporting */
8141 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
8142 i40e_link_event(pf);
8143
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008144 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008145 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
8146 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008147 /* requested_mode is set in probe or by ethtool */
8148 if (!pf->fc_autoneg_status)
8149 goto no_autoneg;
8150
8151 if ((pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX) &&
8152 (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008153 pf->hw.fc.current_mode = I40E_FC_FULL;
8154 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX)
8155 pf->hw.fc.current_mode = I40E_FC_TX_PAUSE;
8156 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX)
8157 pf->hw.fc.current_mode = I40E_FC_RX_PAUSE;
8158 else
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008159 pf->hw.fc.current_mode = I40E_FC_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008160
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008161 /* sync the flow control settings with the auto-neg values */
8162 switch (pf->hw.fc.current_mode) {
8163 case I40E_FC_FULL:
8164 txfc = 1;
8165 rxfc = 1;
8166 break;
8167 case I40E_FC_TX_PAUSE:
8168 txfc = 1;
8169 rxfc = 0;
8170 break;
8171 case I40E_FC_RX_PAUSE:
8172 txfc = 0;
8173 rxfc = 1;
8174 break;
8175 case I40E_FC_NONE:
8176 case I40E_FC_DEFAULT:
8177 txfc = 0;
8178 rxfc = 0;
8179 break;
8180 case I40E_FC_PFC:
8181 /* TBD */
8182 break;
8183 /* no default case, we have to handle all possibilities here */
8184 }
8185
8186 wr32(&pf->hw, I40E_PRTDCB_FCCFG, txfc << I40E_PRTDCB_FCCFG_TFCE_SHIFT);
8187
8188 rxfc_reg = rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
8189 ~I40E_PRTDCB_MFLCN_RFCE_MASK;
8190 rxfc_reg |= (rxfc << I40E_PRTDCB_MFLCN_RFCE_SHIFT);
8191
8192 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rxfc_reg);
8193
8194 goto fc_complete;
8195
8196no_autoneg:
8197 /* disable L2 flow control, user can turn it on if they wish */
8198 wr32(&pf->hw, I40E_PRTDCB_FCCFG, 0);
8199 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
8200 ~I40E_PRTDCB_MFLCN_RFCE_MASK);
8201
8202fc_complete:
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008203 i40e_ptp_init(pf);
8204
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008205 return ret;
8206}
8207
8208/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008209 * i40e_determine_queue_usage - Work out queue distribution
8210 * @pf: board private structure
8211 **/
8212static void i40e_determine_queue_usage(struct i40e_pf *pf)
8213{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008214 int queues_left;
8215
8216 pf->num_lan_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008217
8218 /* Find the max queues to be put into basic use. We'll always be
8219 * using TC0, whether or not DCB is running, and TC0 will get the
8220 * big RSS set.
8221 */
8222 queues_left = pf->hw.func_caps.num_tx_qp;
8223
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008224 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +00008225 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008226 /* one qp for PF, no queues for anything else */
8227 queues_left = 0;
8228 pf->rss_size = pf->num_lan_qps = 1;
8229
8230 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008231 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
8232 I40E_FLAG_FD_SB_ENABLED |
8233 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008234 I40E_FLAG_DCB_CAPABLE |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008235 I40E_FLAG_SRIOV_ENABLED |
8236 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +00008237 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
8238 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +00008239 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008240 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +00008241 /* one qp for PF */
8242 pf->rss_size = pf->num_lan_qps = 1;
8243 queues_left -= pf->num_lan_qps;
8244
8245 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
8246 I40E_FLAG_FD_SB_ENABLED |
8247 I40E_FLAG_FD_ATR_ENABLED |
8248 I40E_FLAG_DCB_ENABLED |
8249 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008250 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008251 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008252 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008253 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008254 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008255 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
8256 }
8257 pf->num_lan_qps = pf->rss_size_max;
8258 queues_left -= pf->num_lan_qps;
8259 }
8260
8261 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8262 if (queues_left > 1) {
8263 queues_left -= 1; /* save 1 queue for FD */
8264 } else {
8265 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8266 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
8267 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008268 }
8269
8270 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
8271 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008272 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
8273 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008274 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
8275 }
8276
8277 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
8278 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
8279 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
8280 (queues_left / pf->num_vmdq_qps));
8281 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
8282 }
8283
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008284 pf->queues_left = queues_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008285}
8286
8287/**
8288 * i40e_setup_pf_filter_control - Setup PF static filter control
8289 * @pf: PF to be setup
8290 *
8291 * i40e_setup_pf_filter_control sets up a pf's initial filter control
8292 * settings. If PE/FCoE are enabled then it will also set the per PF
8293 * based filter sizes required for them. It also enables Flow director,
8294 * ethertype and macvlan type filter settings for the pf.
8295 *
8296 * Returns 0 on success, negative on failure
8297 **/
8298static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
8299{
8300 struct i40e_filter_control_settings *settings = &pf->filter_settings;
8301
8302 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
8303
8304 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008305 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008306 settings->enable_fdir = true;
8307
8308 /* Ethtype and MACVLAN filters enabled for PF */
8309 settings->enable_ethtype = true;
8310 settings->enable_macvlan = true;
8311
8312 if (i40e_set_filter_control(&pf->hw, settings))
8313 return -ENOENT;
8314
8315 return 0;
8316}
8317
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008318#define INFO_STRING_LEN 255
8319static void i40e_print_features(struct i40e_pf *pf)
8320{
8321 struct i40e_hw *hw = &pf->hw;
8322 char *buf, *string;
8323
8324 string = kzalloc(INFO_STRING_LEN, GFP_KERNEL);
8325 if (!string) {
8326 dev_err(&pf->pdev->dev, "Features string allocation failed\n");
8327 return;
8328 }
8329
8330 buf = string;
8331
8332 buf += sprintf(string, "Features: PF-id[%d] ", hw->pf_id);
8333#ifdef CONFIG_PCI_IOV
8334 buf += sprintf(buf, "VFs: %d ", pf->num_req_vfs);
8335#endif
8336 buf += sprintf(buf, "VSIs: %d QP: %d ", pf->hw.func_caps.num_vsis,
8337 pf->vsi[pf->lan_vsi]->num_queue_pairs);
8338
8339 if (pf->flags & I40E_FLAG_RSS_ENABLED)
8340 buf += sprintf(buf, "RSS ");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008341 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +00008342 buf += sprintf(buf, "FD_ATR ");
8343 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8344 buf += sprintf(buf, "FD_SB ");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008345 buf += sprintf(buf, "NTUPLE ");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +00008346 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008347 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008348 buf += sprintf(buf, "DCB ");
8349 if (pf->flags & I40E_FLAG_PTP)
8350 buf += sprintf(buf, "PTP ");
8351
8352 BUG_ON(buf > (string + INFO_STRING_LEN));
8353 dev_info(&pf->pdev->dev, "%s\n", string);
8354 kfree(string);
8355}
8356
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008357/**
8358 * i40e_probe - Device initialization routine
8359 * @pdev: PCI device information struct
8360 * @ent: entry in i40e_pci_tbl
8361 *
8362 * i40e_probe initializes a pf identified by a pci_dev structure.
8363 * The OS initialization, configuring of the pf private structure,
8364 * and a hardware reset occur.
8365 *
8366 * Returns 0 on success, negative on failure
8367 **/
8368static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
8369{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008370 struct i40e_pf *pf;
8371 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008372 static u16 pfs_found;
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008373 u16 link_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008374 int err = 0;
8375 u32 len;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008376 u32 i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008377
8378 err = pci_enable_device_mem(pdev);
8379 if (err)
8380 return err;
8381
8382 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +00008383 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +00008384 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +00008385 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
8386 if (err) {
8387 dev_err(&pdev->dev,
8388 "DMA configuration failed: 0x%x\n", err);
8389 goto err_dma;
8390 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008391 }
8392
8393 /* set up pci connections */
8394 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
8395 IORESOURCE_MEM), i40e_driver_name);
8396 if (err) {
8397 dev_info(&pdev->dev,
8398 "pci_request_selected_regions failed %d\n", err);
8399 goto err_pci_reg;
8400 }
8401
8402 pci_enable_pcie_error_reporting(pdev);
8403 pci_set_master(pdev);
8404
8405 /* Now that we have a PCI connection, we need to do the
8406 * low level device setup. This is primarily setting up
8407 * the Admin Queue structures and then querying for the
8408 * device's current profile information.
8409 */
8410 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
8411 if (!pf) {
8412 err = -ENOMEM;
8413 goto err_pf_alloc;
8414 }
8415 pf->next_vsi = 0;
8416 pf->pdev = pdev;
8417 set_bit(__I40E_DOWN, &pf->state);
8418
8419 hw = &pf->hw;
8420 hw->back = pf;
8421 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
8422 pci_resource_len(pdev, 0));
8423 if (!hw->hw_addr) {
8424 err = -EIO;
8425 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
8426 (unsigned int)pci_resource_start(pdev, 0),
8427 (unsigned int)pci_resource_len(pdev, 0), err);
8428 goto err_ioremap;
8429 }
8430 hw->vendor_id = pdev->vendor;
8431 hw->device_id = pdev->device;
8432 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
8433 hw->subsystem_vendor_id = pdev->subsystem_vendor;
8434 hw->subsystem_device_id = pdev->subsystem_device;
8435 hw->bus.device = PCI_SLOT(pdev->devfn);
8436 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008437 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008438
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008439 /* do a special CORER for clearing PXE mode once at init */
8440 if (hw->revision_id == 0 &&
8441 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
8442 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
8443 i40e_flush(hw);
8444 msleep(200);
8445 pf->corer_count++;
8446
8447 i40e_clear_pxe_mode(hw);
8448 }
8449
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008450 /* Reset here to make sure all is clean and to define PF 'n' */
8451 err = i40e_pf_reset(hw);
8452 if (err) {
8453 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
8454 goto err_pf_reset;
8455 }
8456 pf->pfr_count++;
8457
8458 hw->aq.num_arq_entries = I40E_AQ_LEN;
8459 hw->aq.num_asq_entries = I40E_AQ_LEN;
8460 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
8461 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
8462 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
8463 snprintf(pf->misc_int_name, sizeof(pf->misc_int_name) - 1,
8464 "%s-pf%d:misc",
8465 dev_driver_string(&pf->pdev->dev), pf->hw.pf_id);
8466
8467 err = i40e_init_shared_code(hw);
8468 if (err) {
8469 dev_info(&pdev->dev, "init_shared_code failed: %d\n", err);
8470 goto err_pf_reset;
8471 }
8472
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008473 /* set up a default setting for link flow control */
8474 pf->hw.fc.requested_mode = I40E_FC_NONE;
8475
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008476 err = i40e_init_adminq(hw);
8477 dev_info(&pdev->dev, "%s\n", i40e_fw_version_str(hw));
8478 if (err) {
8479 dev_info(&pdev->dev,
8480 "init_adminq failed: %d expecting API %02x.%02x\n",
8481 err,
8482 I40E_FW_API_VERSION_MAJOR, I40E_FW_API_VERSION_MINOR);
8483 goto err_pf_reset;
8484 }
8485
Shannon Nelson4eb3f762014-03-06 08:59:58 +00008486 i40e_verify_eeprom(pf);
8487
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +00008488 /* Rev 0 hardware was never productized */
8489 if (hw->revision_id < 1)
8490 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");
8491
Shannon Nelson6ff4ef82013-12-21 05:44:49 +00008492 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008493 err = i40e_get_capabilities(pf);
8494 if (err)
8495 goto err_adminq_setup;
8496
8497 err = i40e_sw_init(pf);
8498 if (err) {
8499 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
8500 goto err_sw_init;
8501 }
8502
8503 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
8504 hw->func_caps.num_rx_qp,
8505 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
8506 if (err) {
8507 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
8508 goto err_init_lan_hmc;
8509 }
8510
8511 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
8512 if (err) {
8513 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
8514 err = -ENOENT;
8515 goto err_configure_lan_hmc;
8516 }
8517
8518 i40e_get_mac_addr(hw, hw->mac.addr);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +00008519 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008520 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
8521 err = -EIO;
8522 goto err_mac_addr;
8523 }
8524 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +00008525 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008526
8527 pci_set_drvdata(pdev, pf);
8528 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008529#ifdef CONFIG_I40E_DCB
8530 err = i40e_init_pf_dcb(pf);
8531 if (err) {
8532 dev_info(&pdev->dev, "init_pf_dcb failed: %d\n", err);
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008533 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
Neerav Parikh014269f2014-04-01 07:11:48 +00008534 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008535 }
8536#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008537
8538 /* set up periodic task facility */
8539 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
8540 pf->service_timer_period = HZ;
8541
8542 INIT_WORK(&pf->service_task, i40e_service_task);
8543 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
8544 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
8545 pf->link_check_timeout = jiffies;
8546
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008547 /* WoL defaults to disabled */
8548 pf->wol_en = false;
8549 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
8550
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008551 /* set up the main switch operations */
8552 i40e_determine_queue_usage(pf);
8553 i40e_init_interrupt_scheme(pf);
8554
Mitch Williams505682c2014-05-20 08:01:37 +00008555 /* The number of VSIs reported by the FW is the minimum guaranteed
8556 * to us; HW supports far more and we share the remaining pool with
8557 * the other PFs. We allocate space for more than the guarantee with
8558 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008559 */
Mitch Williams505682c2014-05-20 08:01:37 +00008560 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
8561 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
8562 else
8563 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
8564
8565 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
8566 len = sizeof(struct i40e_vsi *) * pf->num_alloc_vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008567 pf->vsi = kzalloc(len, GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +00008568 if (!pf->vsi) {
8569 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008570 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +00008571 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008572
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008573 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008574 if (err) {
8575 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
8576 goto err_vsis;
8577 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008578 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +00008579 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008580 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
8581 i40e_vsi_open(pf->vsi[i]);
8582 break;
8583 }
8584 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008585
8586 /* The main driver is (mostly) up and happy. We need to set this state
8587 * before setting up the misc vector or we get a race and the vector
8588 * ends up disabled forever.
8589 */
8590 clear_bit(__I40E_DOWN, &pf->state);
8591
8592 /* In case of MSIX we are going to setup the misc vector right here
8593 * to handle admin queue events etc. In case of legacy and MSI
8594 * the misc functionality and queue processing is combined in
8595 * the same vector and that gets setup at open.
8596 */
8597 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
8598 err = i40e_setup_misc_vector(pf);
8599 if (err) {
8600 dev_info(&pdev->dev,
8601 "setup of misc vector failed: %d\n", err);
8602 goto err_vsis;
8603 }
8604 }
8605
Greg Rosedf805f62014-04-04 04:43:16 +00008606#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008607 /* prep for VF support */
8608 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +00008609 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
8610 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008611 u32 val;
8612
8613 /* disable link interrupts for VFs */
8614 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
8615 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
8616 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
8617 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -08008618
8619 if (pci_num_vf(pdev)) {
8620 dev_info(&pdev->dev,
8621 "Active VFs found, allocating resources.\n");
8622 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
8623 if (err)
8624 dev_info(&pdev->dev,
8625 "Error %d allocating resources for existing VFs\n",
8626 err);
8627 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008628 }
Greg Rosedf805f62014-04-04 04:43:16 +00008629#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008630
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008631 pfs_found++;
8632
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008633 i40e_dbg_pf_init(pf);
8634
8635 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00008636 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008637
8638 /* since everything's happy, start the service_task timer */
8639 mod_timer(&pf->service_timer,
8640 round_jiffies(jiffies + pf->service_timer_period));
8641
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008642 /* Get the negotiated link width and speed from PCI config space */
8643 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA, &link_status);
8644
8645 i40e_set_pci_config_data(hw, link_status);
8646
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00008647 dev_info(&pdev->dev, "PCI-Express: %s %s\n",
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008648 (hw->bus.speed == i40e_bus_speed_8000 ? "Speed 8.0GT/s" :
8649 hw->bus.speed == i40e_bus_speed_5000 ? "Speed 5.0GT/s" :
8650 hw->bus.speed == i40e_bus_speed_2500 ? "Speed 2.5GT/s" :
8651 "Unknown"),
8652 (hw->bus.width == i40e_bus_width_pcie_x8 ? "Width x8" :
8653 hw->bus.width == i40e_bus_width_pcie_x4 ? "Width x4" :
8654 hw->bus.width == i40e_bus_width_pcie_x2 ? "Width x2" :
8655 hw->bus.width == i40e_bus_width_pcie_x1 ? "Width x1" :
8656 "Unknown"));
8657
8658 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
8659 hw->bus.speed < i40e_bus_speed_8000) {
8660 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
8661 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
8662 }
8663
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008664 /* print a string summarizing features */
8665 i40e_print_features(pf);
8666
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008667 return 0;
8668
8669 /* Unwind what we've done if something failed in the setup */
8670err_vsis:
8671 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008672 i40e_clear_interrupt_scheme(pf);
8673 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +00008674err_switch_setup:
8675 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008676 del_timer_sync(&pf->service_timer);
8677err_mac_addr:
8678err_configure_lan_hmc:
8679 (void)i40e_shutdown_lan_hmc(hw);
8680err_init_lan_hmc:
8681 kfree(pf->qp_pile);
8682 kfree(pf->irq_pile);
8683err_sw_init:
8684err_adminq_setup:
8685 (void)i40e_shutdown_adminq(hw);
8686err_pf_reset:
8687 iounmap(hw->hw_addr);
8688err_ioremap:
8689 kfree(pf);
8690err_pf_alloc:
8691 pci_disable_pcie_error_reporting(pdev);
8692 pci_release_selected_regions(pdev,
8693 pci_select_bars(pdev, IORESOURCE_MEM));
8694err_pci_reg:
8695err_dma:
8696 pci_disable_device(pdev);
8697 return err;
8698}
8699
8700/**
8701 * i40e_remove - Device removal routine
8702 * @pdev: PCI device information struct
8703 *
8704 * i40e_remove is called by the PCI subsystem to alert the driver
8705 * that is should release a PCI device. This could be caused by a
8706 * Hot-Plug event, or because the driver is going to be removed from
8707 * memory.
8708 **/
8709static void i40e_remove(struct pci_dev *pdev)
8710{
8711 struct i40e_pf *pf = pci_get_drvdata(pdev);
8712 i40e_status ret_code;
8713 u32 reg;
8714 int i;
8715
8716 i40e_dbg_pf_exit(pf);
8717
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008718 i40e_ptp_stop(pf);
8719
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008720 /* no more scheduling of any task */
8721 set_bit(__I40E_DOWN, &pf->state);
8722 del_timer_sync(&pf->service_timer);
8723 cancel_work_sync(&pf->service_task);
8724
Mitch Williamseb2d80b2014-02-13 03:48:48 -08008725 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
8726 i40e_free_vfs(pf);
8727 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
8728 }
8729
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008730 i40e_fdir_teardown(pf);
8731
8732 /* If there is a switch structure or any orphans, remove them.
8733 * This will leave only the PF's VSI remaining.
8734 */
8735 for (i = 0; i < I40E_MAX_VEB; i++) {
8736 if (!pf->veb[i])
8737 continue;
8738
8739 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
8740 pf->veb[i]->uplink_seid == 0)
8741 i40e_switch_branch_release(pf->veb[i]);
8742 }
8743
8744 /* Now we can shutdown the PF's VSI, just before we kill
8745 * adminq and hmc.
8746 */
8747 if (pf->vsi[pf->lan_vsi])
8748 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
8749
8750 i40e_stop_misc_vector(pf);
8751 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
8752 synchronize_irq(pf->msix_entries[0].vector);
8753 free_irq(pf->msix_entries[0].vector, pf);
8754 }
8755
8756 /* shutdown and destroy the HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00008757 if (pf->hw.hmc.hmc_obj) {
8758 ret_code = i40e_shutdown_lan_hmc(&pf->hw);
8759 if (ret_code)
8760 dev_warn(&pdev->dev,
8761 "Failed to destroy the HMC resources: %d\n",
8762 ret_code);
8763 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008764
8765 /* shutdown the adminq */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008766 ret_code = i40e_shutdown_adminq(&pf->hw);
8767 if (ret_code)
8768 dev_warn(&pdev->dev,
8769 "Failed to destroy the Admin Queue resources: %d\n",
8770 ret_code);
8771
8772 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
8773 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +00008774 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008775 if (pf->vsi[i]) {
8776 i40e_vsi_clear_rings(pf->vsi[i]);
8777 i40e_vsi_clear(pf->vsi[i]);
8778 pf->vsi[i] = NULL;
8779 }
8780 }
8781
8782 for (i = 0; i < I40E_MAX_VEB; i++) {
8783 kfree(pf->veb[i]);
8784 pf->veb[i] = NULL;
8785 }
8786
8787 kfree(pf->qp_pile);
8788 kfree(pf->irq_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008789 kfree(pf->vsi);
8790
8791 /* force a PF reset to clean anything leftover */
8792 reg = rd32(&pf->hw, I40E_PFGEN_CTRL);
8793 wr32(&pf->hw, I40E_PFGEN_CTRL, (reg | I40E_PFGEN_CTRL_PFSWR_MASK));
8794 i40e_flush(&pf->hw);
8795
8796 iounmap(pf->hw.hw_addr);
8797 kfree(pf);
8798 pci_release_selected_regions(pdev,
8799 pci_select_bars(pdev, IORESOURCE_MEM));
8800
8801 pci_disable_pcie_error_reporting(pdev);
8802 pci_disable_device(pdev);
8803}
8804
8805/**
8806 * i40e_pci_error_detected - warning that something funky happened in PCI land
8807 * @pdev: PCI device information struct
8808 *
8809 * Called to warn that something happened and the error handling steps
8810 * are in progress. Allows the driver to quiesce things, be ready for
8811 * remediation.
8812 **/
8813static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
8814 enum pci_channel_state error)
8815{
8816 struct i40e_pf *pf = pci_get_drvdata(pdev);
8817
8818 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
8819
8820 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008821 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
8822 rtnl_lock();
8823 i40e_prep_for_reset(pf);
8824 rtnl_unlock();
8825 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008826
8827 /* Request a slot reset */
8828 return PCI_ERS_RESULT_NEED_RESET;
8829}
8830
8831/**
8832 * i40e_pci_error_slot_reset - a PCI slot reset just happened
8833 * @pdev: PCI device information struct
8834 *
8835 * Called to find if the driver can work with the device now that
8836 * the pci slot has been reset. If a basic connection seems good
8837 * (registers are readable and have sane content) then return a
8838 * happy little PCI_ERS_RESULT_xxx.
8839 **/
8840static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
8841{
8842 struct i40e_pf *pf = pci_get_drvdata(pdev);
8843 pci_ers_result_t result;
8844 int err;
8845 u32 reg;
8846
8847 dev_info(&pdev->dev, "%s\n", __func__);
8848 if (pci_enable_device_mem(pdev)) {
8849 dev_info(&pdev->dev,
8850 "Cannot re-enable PCI device after reset.\n");
8851 result = PCI_ERS_RESULT_DISCONNECT;
8852 } else {
8853 pci_set_master(pdev);
8854 pci_restore_state(pdev);
8855 pci_save_state(pdev);
8856 pci_wake_from_d3(pdev, false);
8857
8858 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8859 if (reg == 0)
8860 result = PCI_ERS_RESULT_RECOVERED;
8861 else
8862 result = PCI_ERS_RESULT_DISCONNECT;
8863 }
8864
8865 err = pci_cleanup_aer_uncorrect_error_status(pdev);
8866 if (err) {
8867 dev_info(&pdev->dev,
8868 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
8869 err);
8870 /* non-fatal, continue */
8871 }
8872
8873 return result;
8874}
8875
8876/**
8877 * i40e_pci_error_resume - restart operations after PCI error recovery
8878 * @pdev: PCI device information struct
8879 *
8880 * Called to allow the driver to bring things back up after PCI error
8881 * and/or reset recovery has finished.
8882 **/
8883static void i40e_pci_error_resume(struct pci_dev *pdev)
8884{
8885 struct i40e_pf *pf = pci_get_drvdata(pdev);
8886
8887 dev_info(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008888 if (test_bit(__I40E_SUSPENDED, &pf->state))
8889 return;
8890
8891 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008892 i40e_handle_reset_warning(pf);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008893 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008894}
8895
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008896/**
8897 * i40e_shutdown - PCI callback for shutting down
8898 * @pdev: PCI device information struct
8899 **/
8900static void i40e_shutdown(struct pci_dev *pdev)
8901{
8902 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008903 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008904
8905 set_bit(__I40E_SUSPENDED, &pf->state);
8906 set_bit(__I40E_DOWN, &pf->state);
8907 rtnl_lock();
8908 i40e_prep_for_reset(pf);
8909 rtnl_unlock();
8910
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008911 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
8912 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
8913
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008914 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008915 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008916 pci_set_power_state(pdev, PCI_D3hot);
8917 }
8918}
8919
8920#ifdef CONFIG_PM
8921/**
8922 * i40e_suspend - PCI callback for moving to D3
8923 * @pdev: PCI device information struct
8924 **/
8925static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
8926{
8927 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008928 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008929
8930 set_bit(__I40E_SUSPENDED, &pf->state);
8931 set_bit(__I40E_DOWN, &pf->state);
8932 rtnl_lock();
8933 i40e_prep_for_reset(pf);
8934 rtnl_unlock();
8935
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008936 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
8937 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
8938
8939 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008940 pci_set_power_state(pdev, PCI_D3hot);
8941
8942 return 0;
8943}
8944
8945/**
8946 * i40e_resume - PCI callback for waking up from D3
8947 * @pdev: PCI device information struct
8948 **/
8949static int i40e_resume(struct pci_dev *pdev)
8950{
8951 struct i40e_pf *pf = pci_get_drvdata(pdev);
8952 u32 err;
8953
8954 pci_set_power_state(pdev, PCI_D0);
8955 pci_restore_state(pdev);
8956 /* pci_restore_state() clears dev->state_saves, so
8957 * call pci_save_state() again to restore it.
8958 */
8959 pci_save_state(pdev);
8960
8961 err = pci_enable_device_mem(pdev);
8962 if (err) {
8963 dev_err(&pdev->dev,
8964 "%s: Cannot enable PCI device from suspend\n",
8965 __func__);
8966 return err;
8967 }
8968 pci_set_master(pdev);
8969
8970 /* no wakeup events while running */
8971 pci_wake_from_d3(pdev, false);
8972
8973 /* handling the reset will rebuild the device state */
8974 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
8975 clear_bit(__I40E_DOWN, &pf->state);
8976 rtnl_lock();
8977 i40e_reset_and_rebuild(pf, false);
8978 rtnl_unlock();
8979 }
8980
8981 return 0;
8982}
8983
8984#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008985static const struct pci_error_handlers i40e_err_handler = {
8986 .error_detected = i40e_pci_error_detected,
8987 .slot_reset = i40e_pci_error_slot_reset,
8988 .resume = i40e_pci_error_resume,
8989};
8990
8991static struct pci_driver i40e_driver = {
8992 .name = i40e_driver_name,
8993 .id_table = i40e_pci_tbl,
8994 .probe = i40e_probe,
8995 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008996#ifdef CONFIG_PM
8997 .suspend = i40e_suspend,
8998 .resume = i40e_resume,
8999#endif
9000 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009001 .err_handler = &i40e_err_handler,
9002 .sriov_configure = i40e_pci_sriov_configure,
9003};
9004
9005/**
9006 * i40e_init_module - Driver registration routine
9007 *
9008 * i40e_init_module is the first routine called when the driver is
9009 * loaded. All it does is register with the PCI subsystem.
9010 **/
9011static int __init i40e_init_module(void)
9012{
9013 pr_info("%s: %s - version %s\n", i40e_driver_name,
9014 i40e_driver_string, i40e_driver_version_str);
9015 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
9016 i40e_dbg_init();
9017 return pci_register_driver(&i40e_driver);
9018}
9019module_init(i40e_init_module);
9020
9021/**
9022 * i40e_exit_module - Driver exit cleanup routine
9023 *
9024 * i40e_exit_module is called just before the driver is removed
9025 * from memory.
9026 **/
9027static void __exit i40e_exit_module(void)
9028{
9029 pci_unregister_driver(&i40e_driver);
9030 i40e_dbg_exit();
9031}
9032module_exit(i40e_exit_module);