blob: 0dd578f5d8034dc4ead75fa1c88bd4bacfae36cf [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"
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000029#ifdef CONFIG_I40E_VXLAN
30#include <net/vxlan.h>
31#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000032
33const char i40e_driver_name[] = "i40e";
34static const char i40e_driver_string[] =
35 "Intel(R) Ethernet Connection XL710 Network Driver";
36
37#define DRV_KERN "-k"
38
39#define DRV_VERSION_MAJOR 0
40#define DRV_VERSION_MINOR 3
Catherine Sullivan962dd662014-01-14 00:49:54 -080041#define DRV_VERSION_BUILD 30
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000042#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
43 __stringify(DRV_VERSION_MINOR) "." \
44 __stringify(DRV_VERSION_BUILD) DRV_KERN
45const char i40e_driver_version_str[] = DRV_VERSION;
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -080046static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000047
48/* a bit of forward declarations */
49static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
50static void i40e_handle_reset_warning(struct i40e_pf *pf);
51static int i40e_add_vsi(struct i40e_vsi *vsi);
52static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000053static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000054static int i40e_setup_misc_vector(struct i40e_pf *pf);
55static void i40e_determine_queue_usage(struct i40e_pf *pf);
56static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080057static void i40e_fdir_sb_setup(struct i40e_pf *pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000058
59/* i40e_pci_tbl - PCI Device ID Table
60 *
61 * Last entry must be all 0s
62 *
63 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
64 * Class, Class Mask, private data (not used) }
65 */
66static DEFINE_PCI_DEVICE_TABLE(i40e_pci_tbl) = {
67 {PCI_VDEVICE(INTEL, I40E_SFP_XL710_DEVICE_ID), 0},
68 {PCI_VDEVICE(INTEL, I40E_SFP_X710_DEVICE_ID), 0},
69 {PCI_VDEVICE(INTEL, I40E_QEMU_DEVICE_ID), 0},
70 {PCI_VDEVICE(INTEL, I40E_KX_A_DEVICE_ID), 0},
71 {PCI_VDEVICE(INTEL, I40E_KX_B_DEVICE_ID), 0},
72 {PCI_VDEVICE(INTEL, I40E_KX_C_DEVICE_ID), 0},
73 {PCI_VDEVICE(INTEL, I40E_KX_D_DEVICE_ID), 0},
74 {PCI_VDEVICE(INTEL, I40E_QSFP_A_DEVICE_ID), 0},
75 {PCI_VDEVICE(INTEL, I40E_QSFP_B_DEVICE_ID), 0},
76 {PCI_VDEVICE(INTEL, I40E_QSFP_C_DEVICE_ID), 0},
77 /* 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");
307 i40e_down(vsi);
308 break;
309 }
310 i40e_service_event_schedule(pf);
311 pf->tx_timeout_recovery_level++;
312}
313
314/**
315 * i40e_release_rx_desc - Store the new tail and head values
316 * @rx_ring: ring to bump
317 * @val: new head index
318 **/
319static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
320{
321 rx_ring->next_to_use = val;
322
323 /* Force memory writes to complete before letting h/w
324 * know there are new descriptors to fetch. (Only
325 * applicable for weak-ordered memory model archs,
326 * such as IA-64).
327 */
328 wmb();
329 writel(val, rx_ring->tail);
330}
331
332/**
333 * i40e_get_vsi_stats_struct - Get System Network Statistics
334 * @vsi: the VSI we care about
335 *
336 * Returns the address of the device statistics structure.
337 * The statistics are actually updated from the service task.
338 **/
339struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
340{
341 return &vsi->net_stats;
342}
343
344/**
345 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
346 * @netdev: network interface device structure
347 *
348 * Returns the address of the device statistics structure.
349 * The statistics are actually updated from the service task.
350 **/
351static struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
352 struct net_device *netdev,
Alexander Duyck980e9b12013-09-28 06:01:03 +0000353 struct rtnl_link_stats64 *stats)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000354{
355 struct i40e_netdev_priv *np = netdev_priv(netdev);
356 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000357 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
358 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000359
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000360 if (test_bit(__I40E_DOWN, &vsi->state))
361 return stats;
362
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800363 if (!vsi->tx_rings)
364 return stats;
365
Alexander Duyck980e9b12013-09-28 06:01:03 +0000366 rcu_read_lock();
367 for (i = 0; i < vsi->num_queue_pairs; i++) {
368 struct i40e_ring *tx_ring, *rx_ring;
369 u64 bytes, packets;
370 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000371
Alexander Duyck980e9b12013-09-28 06:01:03 +0000372 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
373 if (!tx_ring)
374 continue;
375
376 do {
377 start = u64_stats_fetch_begin_bh(&tx_ring->syncp);
378 packets = tx_ring->stats.packets;
379 bytes = tx_ring->stats.bytes;
380 } while (u64_stats_fetch_retry_bh(&tx_ring->syncp, start));
381
382 stats->tx_packets += packets;
383 stats->tx_bytes += bytes;
384 rx_ring = &tx_ring[1];
385
386 do {
387 start = u64_stats_fetch_begin_bh(&rx_ring->syncp);
388 packets = rx_ring->stats.packets;
389 bytes = rx_ring->stats.bytes;
390 } while (u64_stats_fetch_retry_bh(&rx_ring->syncp, start));
391
392 stats->rx_packets += packets;
393 stats->rx_bytes += bytes;
394 }
395 rcu_read_unlock();
396
397 /* following stats updated by ixgbe_watchdog_task() */
398 stats->multicast = vsi_stats->multicast;
399 stats->tx_errors = vsi_stats->tx_errors;
400 stats->tx_dropped = vsi_stats->tx_dropped;
401 stats->rx_errors = vsi_stats->rx_errors;
402 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
403 stats->rx_length_errors = vsi_stats->rx_length_errors;
404
405 return stats;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000406}
407
408/**
409 * i40e_vsi_reset_stats - Resets all stats of the given vsi
410 * @vsi: the VSI to have its stats reset
411 **/
412void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
413{
414 struct rtnl_link_stats64 *ns;
415 int i;
416
417 if (!vsi)
418 return;
419
420 ns = i40e_get_vsi_stats_struct(vsi);
421 memset(ns, 0, sizeof(*ns));
422 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
423 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
424 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
Greg Rose8e9dca52013-12-18 13:45:53 +0000425 if (vsi->rx_rings && vsi->rx_rings[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000426 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000427 memset(&vsi->rx_rings[i]->stats, 0 ,
428 sizeof(vsi->rx_rings[i]->stats));
429 memset(&vsi->rx_rings[i]->rx_stats, 0 ,
430 sizeof(vsi->rx_rings[i]->rx_stats));
431 memset(&vsi->tx_rings[i]->stats, 0 ,
432 sizeof(vsi->tx_rings[i]->stats));
433 memset(&vsi->tx_rings[i]->tx_stats, 0,
434 sizeof(vsi->tx_rings[i]->tx_stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000435 }
Greg Rose8e9dca52013-12-18 13:45:53 +0000436 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000437 vsi->stat_offsets_loaded = false;
438}
439
440/**
441 * i40e_pf_reset_stats - Reset all of the stats for the given pf
442 * @pf: the PF to be reset
443 **/
444void i40e_pf_reset_stats(struct i40e_pf *pf)
445{
446 memset(&pf->stats, 0, sizeof(pf->stats));
447 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
448 pf->stat_offsets_loaded = false;
449}
450
451/**
452 * i40e_stat_update48 - read and update a 48 bit stat from the chip
453 * @hw: ptr to the hardware info
454 * @hireg: the high 32 bit reg to read
455 * @loreg: the low 32 bit reg to read
456 * @offset_loaded: has the initial offset been loaded yet
457 * @offset: ptr to current offset value
458 * @stat: ptr to the stat
459 *
460 * Since the device stats are not reset at PFReset, they likely will not
461 * be zeroed when the driver starts. We'll save the first values read
462 * and use them as offsets to be subtracted from the raw values in order
463 * to report stats that count from zero. In the process, we also manage
464 * the potential roll-over.
465 **/
466static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
467 bool offset_loaded, u64 *offset, u64 *stat)
468{
469 u64 new_data;
470
471 if (hw->device_id == I40E_QEMU_DEVICE_ID) {
472 new_data = rd32(hw, loreg);
473 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
474 } else {
475 new_data = rd64(hw, loreg);
476 }
477 if (!offset_loaded)
478 *offset = new_data;
479 if (likely(new_data >= *offset))
480 *stat = new_data - *offset;
481 else
482 *stat = (new_data + ((u64)1 << 48)) - *offset;
483 *stat &= 0xFFFFFFFFFFFFULL;
484}
485
486/**
487 * i40e_stat_update32 - read and update a 32 bit stat from the chip
488 * @hw: ptr to the hardware info
489 * @reg: the hw reg to read
490 * @offset_loaded: has the initial offset been loaded yet
491 * @offset: ptr to current offset value
492 * @stat: ptr to the stat
493 **/
494static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
495 bool offset_loaded, u64 *offset, u64 *stat)
496{
497 u32 new_data;
498
499 new_data = rd32(hw, reg);
500 if (!offset_loaded)
501 *offset = new_data;
502 if (likely(new_data >= *offset))
503 *stat = (u32)(new_data - *offset);
504 else
505 *stat = (u32)((new_data + ((u64)1 << 32)) - *offset);
506}
507
508/**
509 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
510 * @vsi: the VSI to be updated
511 **/
512void i40e_update_eth_stats(struct i40e_vsi *vsi)
513{
514 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
515 struct i40e_pf *pf = vsi->back;
516 struct i40e_hw *hw = &pf->hw;
517 struct i40e_eth_stats *oes;
518 struct i40e_eth_stats *es; /* device's eth stats */
519
520 es = &vsi->eth_stats;
521 oes = &vsi->eth_stats_offsets;
522
523 /* Gather up the stats that the hw collects */
524 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
525 vsi->stat_offsets_loaded,
526 &oes->tx_errors, &es->tx_errors);
527 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
528 vsi->stat_offsets_loaded,
529 &oes->rx_discards, &es->rx_discards);
530
531 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
532 I40E_GLV_GORCL(stat_idx),
533 vsi->stat_offsets_loaded,
534 &oes->rx_bytes, &es->rx_bytes);
535 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
536 I40E_GLV_UPRCL(stat_idx),
537 vsi->stat_offsets_loaded,
538 &oes->rx_unicast, &es->rx_unicast);
539 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
540 I40E_GLV_MPRCL(stat_idx),
541 vsi->stat_offsets_loaded,
542 &oes->rx_multicast, &es->rx_multicast);
543 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
544 I40E_GLV_BPRCL(stat_idx),
545 vsi->stat_offsets_loaded,
546 &oes->rx_broadcast, &es->rx_broadcast);
547
548 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
549 I40E_GLV_GOTCL(stat_idx),
550 vsi->stat_offsets_loaded,
551 &oes->tx_bytes, &es->tx_bytes);
552 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
553 I40E_GLV_UPTCL(stat_idx),
554 vsi->stat_offsets_loaded,
555 &oes->tx_unicast, &es->tx_unicast);
556 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
557 I40E_GLV_MPTCL(stat_idx),
558 vsi->stat_offsets_loaded,
559 &oes->tx_multicast, &es->tx_multicast);
560 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
561 I40E_GLV_BPTCL(stat_idx),
562 vsi->stat_offsets_loaded,
563 &oes->tx_broadcast, &es->tx_broadcast);
564 vsi->stat_offsets_loaded = true;
565}
566
567/**
568 * i40e_update_veb_stats - Update Switch component statistics
569 * @veb: the VEB being updated
570 **/
571static void i40e_update_veb_stats(struct i40e_veb *veb)
572{
573 struct i40e_pf *pf = veb->pf;
574 struct i40e_hw *hw = &pf->hw;
575 struct i40e_eth_stats *oes;
576 struct i40e_eth_stats *es; /* device's eth stats */
577 int idx = 0;
578
579 idx = veb->stats_idx;
580 es = &veb->stats;
581 oes = &veb->stats_offsets;
582
583 /* Gather up the stats that the hw collects */
584 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
585 veb->stat_offsets_loaded,
586 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000587 if (hw->revision_id > 0)
588 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
589 veb->stat_offsets_loaded,
590 &oes->rx_unknown_protocol,
591 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000592 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
593 veb->stat_offsets_loaded,
594 &oes->rx_bytes, &es->rx_bytes);
595 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
596 veb->stat_offsets_loaded,
597 &oes->rx_unicast, &es->rx_unicast);
598 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
599 veb->stat_offsets_loaded,
600 &oes->rx_multicast, &es->rx_multicast);
601 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
602 veb->stat_offsets_loaded,
603 &oes->rx_broadcast, &es->rx_broadcast);
604
605 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
606 veb->stat_offsets_loaded,
607 &oes->tx_bytes, &es->tx_bytes);
608 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
609 veb->stat_offsets_loaded,
610 &oes->tx_unicast, &es->tx_unicast);
611 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
612 veb->stat_offsets_loaded,
613 &oes->tx_multicast, &es->tx_multicast);
614 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
615 veb->stat_offsets_loaded,
616 &oes->tx_broadcast, &es->tx_broadcast);
617 veb->stat_offsets_loaded = true;
618}
619
620/**
621 * i40e_update_link_xoff_rx - Update XOFF received in link flow control mode
622 * @pf: the corresponding PF
623 *
624 * Update the Rx XOFF counter (PAUSE frames) in link flow control mode
625 **/
626static void i40e_update_link_xoff_rx(struct i40e_pf *pf)
627{
628 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
629 struct i40e_hw_port_stats *nsd = &pf->stats;
630 struct i40e_hw *hw = &pf->hw;
631 u64 xoff = 0;
632 u16 i, v;
633
634 if ((hw->fc.current_mode != I40E_FC_FULL) &&
635 (hw->fc.current_mode != I40E_FC_RX_PAUSE))
636 return;
637
638 xoff = nsd->link_xoff_rx;
639 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
640 pf->stat_offsets_loaded,
641 &osd->link_xoff_rx, &nsd->link_xoff_rx);
642
643 /* No new LFC xoff rx */
644 if (!(nsd->link_xoff_rx - xoff))
645 return;
646
647 /* Clear the __I40E_HANG_CHECK_ARMED bit for all Tx rings */
648 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
649 struct i40e_vsi *vsi = pf->vsi[v];
650
651 if (!vsi)
652 continue;
653
654 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000655 struct i40e_ring *ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000656 clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
657 }
658 }
659}
660
661/**
662 * i40e_update_prio_xoff_rx - Update XOFF received in PFC mode
663 * @pf: the corresponding PF
664 *
665 * Update the Rx XOFF counter (PAUSE frames) in PFC mode
666 **/
667static void i40e_update_prio_xoff_rx(struct i40e_pf *pf)
668{
669 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
670 struct i40e_hw_port_stats *nsd = &pf->stats;
671 bool xoff[I40E_MAX_TRAFFIC_CLASS] = {false};
672 struct i40e_dcbx_config *dcb_cfg;
673 struct i40e_hw *hw = &pf->hw;
674 u16 i, v;
675 u8 tc;
676
677 dcb_cfg = &hw->local_dcbx_config;
678
679 /* See if DCB enabled with PFC TC */
680 if (!(pf->flags & I40E_FLAG_DCB_ENABLED) ||
681 !(dcb_cfg->pfc.pfcenable)) {
682 i40e_update_link_xoff_rx(pf);
683 return;
684 }
685
686 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
687 u64 prio_xoff = nsd->priority_xoff_rx[i];
688 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
689 pf->stat_offsets_loaded,
690 &osd->priority_xoff_rx[i],
691 &nsd->priority_xoff_rx[i]);
692
693 /* No new PFC xoff rx */
694 if (!(nsd->priority_xoff_rx[i] - prio_xoff))
695 continue;
696 /* Get the TC for given priority */
697 tc = dcb_cfg->etscfg.prioritytable[i];
698 xoff[tc] = true;
699 }
700
701 /* Clear the __I40E_HANG_CHECK_ARMED bit for Tx rings */
702 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
703 struct i40e_vsi *vsi = pf->vsi[v];
704
705 if (!vsi)
706 continue;
707
708 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000709 struct i40e_ring *ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000710
711 tc = ring->dcb_tc;
712 if (xoff[tc])
713 clear_bit(__I40E_HANG_CHECK_ARMED,
714 &ring->state);
715 }
716 }
717}
718
719/**
720 * i40e_update_stats - Update the board statistics counters.
721 * @vsi: the VSI to be updated
722 *
723 * There are a few instances where we store the same stat in a
724 * couple of different structs. This is partly because we have
725 * the netdev stats that need to be filled out, which is slightly
726 * different from the "eth_stats" defined by the chip and used in
727 * VF communications. We sort it all out here in a central place.
728 **/
729void i40e_update_stats(struct i40e_vsi *vsi)
730{
731 struct i40e_pf *pf = vsi->back;
732 struct i40e_hw *hw = &pf->hw;
733 struct rtnl_link_stats64 *ons;
734 struct rtnl_link_stats64 *ns; /* netdev stats */
735 struct i40e_eth_stats *oes;
736 struct i40e_eth_stats *es; /* device's eth stats */
737 u32 tx_restart, tx_busy;
738 u32 rx_page, rx_buf;
739 u64 rx_p, rx_b;
740 u64 tx_p, tx_b;
741 int i;
742 u16 q;
743
744 if (test_bit(__I40E_DOWN, &vsi->state) ||
745 test_bit(__I40E_CONFIG_BUSY, &pf->state))
746 return;
747
748 ns = i40e_get_vsi_stats_struct(vsi);
749 ons = &vsi->net_stats_offsets;
750 es = &vsi->eth_stats;
751 oes = &vsi->eth_stats_offsets;
752
753 /* Gather up the netdev and vsi stats that the driver collects
754 * on the fly during packet processing
755 */
756 rx_b = rx_p = 0;
757 tx_b = tx_p = 0;
758 tx_restart = tx_busy = 0;
759 rx_page = 0;
760 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000761 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000762 for (q = 0; q < vsi->num_queue_pairs; q++) {
763 struct i40e_ring *p;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000764 u64 bytes, packets;
765 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000766
Alexander Duyck980e9b12013-09-28 06:01:03 +0000767 /* locate Tx ring */
768 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000769
Alexander Duyck980e9b12013-09-28 06:01:03 +0000770 do {
771 start = u64_stats_fetch_begin_bh(&p->syncp);
772 packets = p->stats.packets;
773 bytes = p->stats.bytes;
774 } while (u64_stats_fetch_retry_bh(&p->syncp, start));
775 tx_b += bytes;
776 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000777 tx_restart += p->tx_stats.restart_queue;
778 tx_busy += p->tx_stats.tx_busy;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000779
780 /* Rx queue is part of the same block as Tx queue */
781 p = &p[1];
782 do {
783 start = u64_stats_fetch_begin_bh(&p->syncp);
784 packets = p->stats.packets;
785 bytes = p->stats.bytes;
786 } while (u64_stats_fetch_retry_bh(&p->syncp, start));
787 rx_b += bytes;
788 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000789 rx_buf += p->rx_stats.alloc_buff_failed;
790 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000791 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000792 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000793 vsi->tx_restart = tx_restart;
794 vsi->tx_busy = tx_busy;
795 vsi->rx_page_failed = rx_page;
796 vsi->rx_buf_failed = rx_buf;
797
798 ns->rx_packets = rx_p;
799 ns->rx_bytes = rx_b;
800 ns->tx_packets = tx_p;
801 ns->tx_bytes = tx_b;
802
803 i40e_update_eth_stats(vsi);
804 /* update netdev stats from eth stats */
805 ons->rx_errors = oes->rx_errors;
806 ns->rx_errors = es->rx_errors;
807 ons->tx_errors = oes->tx_errors;
808 ns->tx_errors = es->tx_errors;
809 ons->multicast = oes->rx_multicast;
810 ns->multicast = es->rx_multicast;
811 ons->tx_dropped = oes->tx_discards;
812 ns->tx_dropped = es->tx_discards;
813
814 /* Get the port data only if this is the main PF VSI */
815 if (vsi == pf->vsi[pf->lan_vsi]) {
816 struct i40e_hw_port_stats *nsd = &pf->stats;
817 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
818
819 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
820 I40E_GLPRT_GORCL(hw->port),
821 pf->stat_offsets_loaded,
822 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
823 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
824 I40E_GLPRT_GOTCL(hw->port),
825 pf->stat_offsets_loaded,
826 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
827 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
828 pf->stat_offsets_loaded,
829 &osd->eth.rx_discards,
830 &nsd->eth.rx_discards);
831 i40e_stat_update32(hw, I40E_GLPRT_TDPC(hw->port),
832 pf->stat_offsets_loaded,
833 &osd->eth.tx_discards,
834 &nsd->eth.tx_discards);
835 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
836 I40E_GLPRT_MPRCL(hw->port),
837 pf->stat_offsets_loaded,
838 &osd->eth.rx_multicast,
839 &nsd->eth.rx_multicast);
840
841 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
842 pf->stat_offsets_loaded,
843 &osd->tx_dropped_link_down,
844 &nsd->tx_dropped_link_down);
845
846 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
847 pf->stat_offsets_loaded,
848 &osd->crc_errors, &nsd->crc_errors);
849 ns->rx_crc_errors = nsd->crc_errors;
850
851 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
852 pf->stat_offsets_loaded,
853 &osd->illegal_bytes, &nsd->illegal_bytes);
854 ns->rx_errors = nsd->crc_errors
855 + nsd->illegal_bytes;
856
857 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
858 pf->stat_offsets_loaded,
859 &osd->mac_local_faults,
860 &nsd->mac_local_faults);
861 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
862 pf->stat_offsets_loaded,
863 &osd->mac_remote_faults,
864 &nsd->mac_remote_faults);
865
866 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
867 pf->stat_offsets_loaded,
868 &osd->rx_length_errors,
869 &nsd->rx_length_errors);
870 ns->rx_length_errors = nsd->rx_length_errors;
871
872 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
873 pf->stat_offsets_loaded,
874 &osd->link_xon_rx, &nsd->link_xon_rx);
875 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
876 pf->stat_offsets_loaded,
877 &osd->link_xon_tx, &nsd->link_xon_tx);
878 i40e_update_prio_xoff_rx(pf); /* handles I40E_GLPRT_LXOFFRXC */
879 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
880 pf->stat_offsets_loaded,
881 &osd->link_xoff_tx, &nsd->link_xoff_tx);
882
883 for (i = 0; i < 8; i++) {
884 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
885 pf->stat_offsets_loaded,
886 &osd->priority_xon_rx[i],
887 &nsd->priority_xon_rx[i]);
888 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
889 pf->stat_offsets_loaded,
890 &osd->priority_xon_tx[i],
891 &nsd->priority_xon_tx[i]);
892 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
893 pf->stat_offsets_loaded,
894 &osd->priority_xoff_tx[i],
895 &nsd->priority_xoff_tx[i]);
896 i40e_stat_update32(hw,
897 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
898 pf->stat_offsets_loaded,
899 &osd->priority_xon_2_xoff[i],
900 &nsd->priority_xon_2_xoff[i]);
901 }
902
903 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
904 I40E_GLPRT_PRC64L(hw->port),
905 pf->stat_offsets_loaded,
906 &osd->rx_size_64, &nsd->rx_size_64);
907 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
908 I40E_GLPRT_PRC127L(hw->port),
909 pf->stat_offsets_loaded,
910 &osd->rx_size_127, &nsd->rx_size_127);
911 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
912 I40E_GLPRT_PRC255L(hw->port),
913 pf->stat_offsets_loaded,
914 &osd->rx_size_255, &nsd->rx_size_255);
915 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
916 I40E_GLPRT_PRC511L(hw->port),
917 pf->stat_offsets_loaded,
918 &osd->rx_size_511, &nsd->rx_size_511);
919 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
920 I40E_GLPRT_PRC1023L(hw->port),
921 pf->stat_offsets_loaded,
922 &osd->rx_size_1023, &nsd->rx_size_1023);
923 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
924 I40E_GLPRT_PRC1522L(hw->port),
925 pf->stat_offsets_loaded,
926 &osd->rx_size_1522, &nsd->rx_size_1522);
927 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
928 I40E_GLPRT_PRC9522L(hw->port),
929 pf->stat_offsets_loaded,
930 &osd->rx_size_big, &nsd->rx_size_big);
931
932 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
933 I40E_GLPRT_PTC64L(hw->port),
934 pf->stat_offsets_loaded,
935 &osd->tx_size_64, &nsd->tx_size_64);
936 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
937 I40E_GLPRT_PTC127L(hw->port),
938 pf->stat_offsets_loaded,
939 &osd->tx_size_127, &nsd->tx_size_127);
940 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
941 I40E_GLPRT_PTC255L(hw->port),
942 pf->stat_offsets_loaded,
943 &osd->tx_size_255, &nsd->tx_size_255);
944 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
945 I40E_GLPRT_PTC511L(hw->port),
946 pf->stat_offsets_loaded,
947 &osd->tx_size_511, &nsd->tx_size_511);
948 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
949 I40E_GLPRT_PTC1023L(hw->port),
950 pf->stat_offsets_loaded,
951 &osd->tx_size_1023, &nsd->tx_size_1023);
952 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
953 I40E_GLPRT_PTC1522L(hw->port),
954 pf->stat_offsets_loaded,
955 &osd->tx_size_1522, &nsd->tx_size_1522);
956 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
957 I40E_GLPRT_PTC9522L(hw->port),
958 pf->stat_offsets_loaded,
959 &osd->tx_size_big, &nsd->tx_size_big);
960
961 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
962 pf->stat_offsets_loaded,
963 &osd->rx_undersize, &nsd->rx_undersize);
964 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
965 pf->stat_offsets_loaded,
966 &osd->rx_fragments, &nsd->rx_fragments);
967 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
968 pf->stat_offsets_loaded,
969 &osd->rx_oversize, &nsd->rx_oversize);
970 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
971 pf->stat_offsets_loaded,
972 &osd->rx_jabber, &nsd->rx_jabber);
973 }
974
975 pf->stat_offsets_loaded = true;
976}
977
978/**
979 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
980 * @vsi: the VSI to be searched
981 * @macaddr: the MAC address
982 * @vlan: the vlan
983 * @is_vf: make sure its a vf filter, else doesn't matter
984 * @is_netdev: make sure its a netdev filter, else doesn't matter
985 *
986 * Returns ptr to the filter object or NULL
987 **/
988static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
989 u8 *macaddr, s16 vlan,
990 bool is_vf, bool is_netdev)
991{
992 struct i40e_mac_filter *f;
993
994 if (!vsi || !macaddr)
995 return NULL;
996
997 list_for_each_entry(f, &vsi->mac_filter_list, list) {
998 if ((ether_addr_equal(macaddr, f->macaddr)) &&
999 (vlan == f->vlan) &&
1000 (!is_vf || f->is_vf) &&
1001 (!is_netdev || f->is_netdev))
1002 return f;
1003 }
1004 return NULL;
1005}
1006
1007/**
1008 * i40e_find_mac - Find a mac addr in the macvlan filters list
1009 * @vsi: the VSI to be searched
1010 * @macaddr: the MAC address we are searching for
1011 * @is_vf: make sure its a vf filter, else doesn't matter
1012 * @is_netdev: make sure its a netdev filter, else doesn't matter
1013 *
1014 * Returns the first filter with the provided MAC address or NULL if
1015 * MAC address was not found
1016 **/
1017struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, u8 *macaddr,
1018 bool is_vf, bool is_netdev)
1019{
1020 struct i40e_mac_filter *f;
1021
1022 if (!vsi || !macaddr)
1023 return NULL;
1024
1025 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1026 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1027 (!is_vf || f->is_vf) &&
1028 (!is_netdev || f->is_netdev))
1029 return f;
1030 }
1031 return NULL;
1032}
1033
1034/**
1035 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1036 * @vsi: the VSI to be searched
1037 *
1038 * Returns true if VSI is in vlan mode or false otherwise
1039 **/
1040bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1041{
1042 struct i40e_mac_filter *f;
1043
1044 /* Only -1 for all the filters denotes not in vlan mode
1045 * so we have to go through all the list in order to make sure
1046 */
1047 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1048 if (f->vlan >= 0)
1049 return true;
1050 }
1051
1052 return false;
1053}
1054
1055/**
1056 * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
1057 * @vsi: the VSI to be searched
1058 * @macaddr: the mac address to be filtered
1059 * @is_vf: true if it is a vf
1060 * @is_netdev: true if it is a netdev
1061 *
1062 * Goes through all the macvlan filters and adds a
1063 * macvlan filter for each unique vlan that already exists
1064 *
1065 * Returns first filter found on success, else NULL
1066 **/
1067struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi, u8 *macaddr,
1068 bool is_vf, bool is_netdev)
1069{
1070 struct i40e_mac_filter *f;
1071
1072 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1073 if (!i40e_find_filter(vsi, macaddr, f->vlan,
1074 is_vf, is_netdev)) {
1075 if (!i40e_add_filter(vsi, macaddr, f->vlan,
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -08001076 is_vf, is_netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001077 return NULL;
1078 }
1079 }
1080
1081 return list_first_entry_or_null(&vsi->mac_filter_list,
1082 struct i40e_mac_filter, list);
1083}
1084
1085/**
1086 * i40e_add_filter - Add a mac/vlan filter to the VSI
1087 * @vsi: the VSI to be searched
1088 * @macaddr: the MAC address
1089 * @vlan: the vlan
1090 * @is_vf: make sure its a vf filter, else doesn't matter
1091 * @is_netdev: make sure its a netdev filter, else doesn't matter
1092 *
1093 * Returns ptr to the filter object or NULL when no memory available.
1094 **/
1095struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1096 u8 *macaddr, s16 vlan,
1097 bool is_vf, bool is_netdev)
1098{
1099 struct i40e_mac_filter *f;
1100
1101 if (!vsi || !macaddr)
1102 return NULL;
1103
1104 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1105 if (!f) {
1106 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1107 if (!f)
1108 goto add_filter_out;
1109
1110 memcpy(f->macaddr, macaddr, ETH_ALEN);
1111 f->vlan = vlan;
1112 f->changed = true;
1113
1114 INIT_LIST_HEAD(&f->list);
1115 list_add(&f->list, &vsi->mac_filter_list);
1116 }
1117
1118 /* increment counter and add a new flag if needed */
1119 if (is_vf) {
1120 if (!f->is_vf) {
1121 f->is_vf = true;
1122 f->counter++;
1123 }
1124 } else if (is_netdev) {
1125 if (!f->is_netdev) {
1126 f->is_netdev = true;
1127 f->counter++;
1128 }
1129 } else {
1130 f->counter++;
1131 }
1132
1133 /* changed tells sync_filters_subtask to
1134 * push the filter down to the firmware
1135 */
1136 if (f->changed) {
1137 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1138 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1139 }
1140
1141add_filter_out:
1142 return f;
1143}
1144
1145/**
1146 * i40e_del_filter - Remove a mac/vlan filter from the VSI
1147 * @vsi: the VSI to be searched
1148 * @macaddr: the MAC address
1149 * @vlan: the vlan
1150 * @is_vf: make sure it's a vf filter, else doesn't matter
1151 * @is_netdev: make sure it's a netdev filter, else doesn't matter
1152 **/
1153void i40e_del_filter(struct i40e_vsi *vsi,
1154 u8 *macaddr, s16 vlan,
1155 bool is_vf, bool is_netdev)
1156{
1157 struct i40e_mac_filter *f;
1158
1159 if (!vsi || !macaddr)
1160 return;
1161
1162 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1163 if (!f || f->counter == 0)
1164 return;
1165
1166 if (is_vf) {
1167 if (f->is_vf) {
1168 f->is_vf = false;
1169 f->counter--;
1170 }
1171 } else if (is_netdev) {
1172 if (f->is_netdev) {
1173 f->is_netdev = false;
1174 f->counter--;
1175 }
1176 } else {
1177 /* make sure we don't remove a filter in use by vf or netdev */
1178 int min_f = 0;
1179 min_f += (f->is_vf ? 1 : 0);
1180 min_f += (f->is_netdev ? 1 : 0);
1181
1182 if (f->counter > min_f)
1183 f->counter--;
1184 }
1185
1186 /* counter == 0 tells sync_filters_subtask to
1187 * remove the filter from the firmware's list
1188 */
1189 if (f->counter == 0) {
1190 f->changed = true;
1191 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1192 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1193 }
1194}
1195
1196/**
1197 * i40e_set_mac - NDO callback to set mac address
1198 * @netdev: network interface device structure
1199 * @p: pointer to an address structure
1200 *
1201 * Returns 0 on success, negative on failure
1202 **/
1203static int i40e_set_mac(struct net_device *netdev, void *p)
1204{
1205 struct i40e_netdev_priv *np = netdev_priv(netdev);
1206 struct i40e_vsi *vsi = np->vsi;
1207 struct sockaddr *addr = p;
1208 struct i40e_mac_filter *f;
1209
1210 if (!is_valid_ether_addr(addr->sa_data))
1211 return -EADDRNOTAVAIL;
1212
1213 netdev_info(netdev, "set mac address=%pM\n", addr->sa_data);
1214
1215 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
1216 return 0;
1217
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001218 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1219 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1220 return -EADDRNOTAVAIL;
1221
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001222 if (vsi->type == I40E_VSI_MAIN) {
1223 i40e_status ret;
1224 ret = i40e_aq_mac_address_write(&vsi->back->hw,
1225 I40E_AQC_WRITE_TYPE_LAA_ONLY,
1226 addr->sa_data, NULL);
1227 if (ret) {
1228 netdev_info(netdev,
1229 "Addr change for Main VSI failed: %d\n",
1230 ret);
1231 return -EADDRNOTAVAIL;
1232 }
1233
1234 memcpy(vsi->back->hw.mac.addr, addr->sa_data, netdev->addr_len);
1235 }
1236
1237 /* In order to be sure to not drop any packets, add the new address
1238 * then delete the old one.
1239 */
1240 f = i40e_add_filter(vsi, addr->sa_data, I40E_VLAN_ANY, false, false);
1241 if (!f)
1242 return -ENOMEM;
1243
1244 i40e_sync_vsi_filters(vsi);
1245 i40e_del_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY, false, false);
1246 i40e_sync_vsi_filters(vsi);
1247
1248 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1249
1250 return 0;
1251}
1252
1253/**
1254 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1255 * @vsi: the VSI being setup
1256 * @ctxt: VSI context structure
1257 * @enabled_tc: Enabled TCs bitmap
1258 * @is_add: True if called before Add VSI
1259 *
1260 * Setup VSI queue mapping for enabled traffic classes.
1261 **/
1262static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1263 struct i40e_vsi_context *ctxt,
1264 u8 enabled_tc,
1265 bool is_add)
1266{
1267 struct i40e_pf *pf = vsi->back;
1268 u16 sections = 0;
1269 u8 netdev_tc = 0;
1270 u16 numtc = 0;
1271 u16 qcount;
1272 u8 offset;
1273 u16 qmap;
1274 int i;
1275
1276 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1277 offset = 0;
1278
1279 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1280 /* Find numtc from enabled TC bitmap */
1281 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1282 if (enabled_tc & (1 << i)) /* TC is enabled */
1283 numtc++;
1284 }
1285 if (!numtc) {
1286 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1287 numtc = 1;
1288 }
1289 } else {
1290 /* At least TC0 is enabled in case of non-DCB case */
1291 numtc = 1;
1292 }
1293
1294 vsi->tc_config.numtc = numtc;
1295 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1296
1297 /* Setup queue offset/count for all TCs for given VSI */
1298 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1299 /* See if the given TC is enabled for the given VSI */
1300 if (vsi->tc_config.enabled_tc & (1 << i)) { /* TC is enabled */
1301 int pow, num_qps;
1302
1303 vsi->tc_config.tc_info[i].qoffset = offset;
1304 switch (vsi->type) {
1305 case I40E_VSI_MAIN:
1306 if (i == 0)
1307 qcount = pf->rss_size;
1308 else
1309 qcount = pf->num_tc_qps;
1310 vsi->tc_config.tc_info[i].qcount = qcount;
1311 break;
1312 case I40E_VSI_FDIR:
1313 case I40E_VSI_SRIOV:
1314 case I40E_VSI_VMDQ2:
1315 default:
1316 qcount = vsi->alloc_queue_pairs;
1317 vsi->tc_config.tc_info[i].qcount = qcount;
1318 WARN_ON(i != 0);
1319 break;
1320 }
1321
1322 /* find the power-of-2 of the number of queue pairs */
1323 num_qps = vsi->tc_config.tc_info[i].qcount;
1324 pow = 0;
1325 while (num_qps &&
1326 ((1 << pow) < vsi->tc_config.tc_info[i].qcount)) {
1327 pow++;
1328 num_qps >>= 1;
1329 }
1330
1331 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1332 qmap =
1333 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1334 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1335
1336 offset += vsi->tc_config.tc_info[i].qcount;
1337 } else {
1338 /* TC is not enabled so set the offset to
1339 * default queue and allocate one queue
1340 * for the given TC.
1341 */
1342 vsi->tc_config.tc_info[i].qoffset = 0;
1343 vsi->tc_config.tc_info[i].qcount = 1;
1344 vsi->tc_config.tc_info[i].netdev_tc = 0;
1345
1346 qmap = 0;
1347 }
1348 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1349 }
1350
1351 /* Set actual Tx/Rx queue pairs */
1352 vsi->num_queue_pairs = offset;
1353
1354 /* Scheduler section valid can only be set for ADD VSI */
1355 if (is_add) {
1356 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1357
1358 ctxt->info.up_enable_bits = enabled_tc;
1359 }
1360 if (vsi->type == I40E_VSI_SRIOV) {
1361 ctxt->info.mapping_flags |=
1362 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1363 for (i = 0; i < vsi->num_queue_pairs; i++)
1364 ctxt->info.queue_mapping[i] =
1365 cpu_to_le16(vsi->base_queue + i);
1366 } else {
1367 ctxt->info.mapping_flags |=
1368 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1369 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1370 }
1371 ctxt->info.valid_sections |= cpu_to_le16(sections);
1372}
1373
1374/**
1375 * i40e_set_rx_mode - NDO callback to set the netdev filters
1376 * @netdev: network interface device structure
1377 **/
1378static void i40e_set_rx_mode(struct net_device *netdev)
1379{
1380 struct i40e_netdev_priv *np = netdev_priv(netdev);
1381 struct i40e_mac_filter *f, *ftmp;
1382 struct i40e_vsi *vsi = np->vsi;
1383 struct netdev_hw_addr *uca;
1384 struct netdev_hw_addr *mca;
1385 struct netdev_hw_addr *ha;
1386
1387 /* add addr if not already in the filter list */
1388 netdev_for_each_uc_addr(uca, netdev) {
1389 if (!i40e_find_mac(vsi, uca->addr, false, true)) {
1390 if (i40e_is_vsi_in_vlan(vsi))
1391 i40e_put_mac_in_vlan(vsi, uca->addr,
1392 false, true);
1393 else
1394 i40e_add_filter(vsi, uca->addr, I40E_VLAN_ANY,
1395 false, true);
1396 }
1397 }
1398
1399 netdev_for_each_mc_addr(mca, netdev) {
1400 if (!i40e_find_mac(vsi, mca->addr, false, true)) {
1401 if (i40e_is_vsi_in_vlan(vsi))
1402 i40e_put_mac_in_vlan(vsi, mca->addr,
1403 false, true);
1404 else
1405 i40e_add_filter(vsi, mca->addr, I40E_VLAN_ANY,
1406 false, true);
1407 }
1408 }
1409
1410 /* remove filter if not in netdev list */
1411 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1412 bool found = false;
1413
1414 if (!f->is_netdev)
1415 continue;
1416
1417 if (is_multicast_ether_addr(f->macaddr)) {
1418 netdev_for_each_mc_addr(mca, netdev) {
1419 if (ether_addr_equal(mca->addr, f->macaddr)) {
1420 found = true;
1421 break;
1422 }
1423 }
1424 } else {
1425 netdev_for_each_uc_addr(uca, netdev) {
1426 if (ether_addr_equal(uca->addr, f->macaddr)) {
1427 found = true;
1428 break;
1429 }
1430 }
1431
1432 for_each_dev_addr(netdev, ha) {
1433 if (ether_addr_equal(ha->addr, f->macaddr)) {
1434 found = true;
1435 break;
1436 }
1437 }
1438 }
1439 if (!found)
1440 i40e_del_filter(
1441 vsi, f->macaddr, I40E_VLAN_ANY, false, true);
1442 }
1443
1444 /* check for other flag changes */
1445 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1446 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1447 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1448 }
1449}
1450
1451/**
1452 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1453 * @vsi: ptr to the VSI
1454 *
1455 * Push any outstanding VSI filter changes through the AdminQ.
1456 *
1457 * Returns 0 or error value
1458 **/
1459int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
1460{
1461 struct i40e_mac_filter *f, *ftmp;
1462 bool promisc_forced_on = false;
1463 bool add_happened = false;
1464 int filter_list_len = 0;
1465 u32 changed_flags = 0;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001466 i40e_status aq_ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001467 struct i40e_pf *pf;
1468 int num_add = 0;
1469 int num_del = 0;
1470 u16 cmd_flags;
1471
1472 /* empty array typed pointers, kcalloc later */
1473 struct i40e_aqc_add_macvlan_element_data *add_list;
1474 struct i40e_aqc_remove_macvlan_element_data *del_list;
1475
1476 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1477 usleep_range(1000, 2000);
1478 pf = vsi->back;
1479
1480 if (vsi->netdev) {
1481 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1482 vsi->current_netdev_flags = vsi->netdev->flags;
1483 }
1484
1485 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1486 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1487
1488 filter_list_len = pf->hw.aq.asq_buf_size /
1489 sizeof(struct i40e_aqc_remove_macvlan_element_data);
1490 del_list = kcalloc(filter_list_len,
1491 sizeof(struct i40e_aqc_remove_macvlan_element_data),
1492 GFP_KERNEL);
1493 if (!del_list)
1494 return -ENOMEM;
1495
1496 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1497 if (!f->changed)
1498 continue;
1499
1500 if (f->counter != 0)
1501 continue;
1502 f->changed = false;
1503 cmd_flags = 0;
1504
1505 /* add to delete list */
1506 memcpy(del_list[num_del].mac_addr,
1507 f->macaddr, ETH_ALEN);
1508 del_list[num_del].vlan_tag =
1509 cpu_to_le16((u16)(f->vlan ==
1510 I40E_VLAN_ANY ? 0 : f->vlan));
1511
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001512 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1513 del_list[num_del].flags = cmd_flags;
1514 num_del++;
1515
1516 /* unlink from filter list */
1517 list_del(&f->list);
1518 kfree(f);
1519
1520 /* flush a full buffer */
1521 if (num_del == filter_list_len) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001522 aq_ret = i40e_aq_remove_macvlan(&pf->hw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001523 vsi->seid, del_list, num_del,
1524 NULL);
1525 num_del = 0;
1526 memset(del_list, 0, sizeof(*del_list));
1527
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001528 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001529 dev_info(&pf->pdev->dev,
1530 "ignoring delete macvlan error, err %d, aq_err %d while flushing a full buffer\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001531 aq_ret,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001532 pf->hw.aq.asq_last_status);
1533 }
1534 }
1535 if (num_del) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001536 aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001537 del_list, num_del, NULL);
1538 num_del = 0;
1539
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001540 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001541 dev_info(&pf->pdev->dev,
1542 "ignoring delete macvlan error, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001543 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001544 }
1545
1546 kfree(del_list);
1547 del_list = NULL;
1548
1549 /* do all the adds now */
1550 filter_list_len = pf->hw.aq.asq_buf_size /
1551 sizeof(struct i40e_aqc_add_macvlan_element_data),
1552 add_list = kcalloc(filter_list_len,
1553 sizeof(struct i40e_aqc_add_macvlan_element_data),
1554 GFP_KERNEL);
1555 if (!add_list)
1556 return -ENOMEM;
1557
1558 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1559 if (!f->changed)
1560 continue;
1561
1562 if (f->counter == 0)
1563 continue;
1564 f->changed = false;
1565 add_happened = true;
1566 cmd_flags = 0;
1567
1568 /* add to add array */
1569 memcpy(add_list[num_add].mac_addr,
1570 f->macaddr, ETH_ALEN);
1571 add_list[num_add].vlan_tag =
1572 cpu_to_le16(
1573 (u16)(f->vlan == I40E_VLAN_ANY ? 0 : f->vlan));
1574 add_list[num_add].queue_number = 0;
1575
1576 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001577 add_list[num_add].flags = cpu_to_le16(cmd_flags);
1578 num_add++;
1579
1580 /* flush a full buffer */
1581 if (num_add == filter_list_len) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001582 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1583 add_list, num_add,
1584 NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001585 num_add = 0;
1586
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001587 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001588 break;
1589 memset(add_list, 0, sizeof(*add_list));
1590 }
1591 }
1592 if (num_add) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001593 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1594 add_list, num_add, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001595 num_add = 0;
1596 }
1597 kfree(add_list);
1598 add_list = NULL;
1599
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001600 if (add_happened && (!aq_ret)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001601 /* do nothing */;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001602 } else if (add_happened && (aq_ret)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001603 dev_info(&pf->pdev->dev,
1604 "add filter failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001605 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001606 if ((pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOSPC) &&
1607 !test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1608 &vsi->state)) {
1609 promisc_forced_on = true;
1610 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1611 &vsi->state);
1612 dev_info(&pf->pdev->dev, "promiscuous mode forced on\n");
1613 }
1614 }
1615 }
1616
1617 /* check for changes in promiscuous modes */
1618 if (changed_flags & IFF_ALLMULTI) {
1619 bool cur_multipromisc;
1620 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001621 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
1622 vsi->seid,
1623 cur_multipromisc,
1624 NULL);
1625 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001626 dev_info(&pf->pdev->dev,
1627 "set multi promisc failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001628 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001629 }
1630 if ((changed_flags & IFF_PROMISC) || promisc_forced_on) {
1631 bool cur_promisc;
1632 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
1633 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1634 &vsi->state));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001635 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(&vsi->back->hw,
1636 vsi->seid,
1637 cur_promisc, NULL);
1638 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001639 dev_info(&pf->pdev->dev,
1640 "set uni promisc failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001641 aq_ret, pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00001642 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
1643 vsi->seid,
1644 cur_promisc, NULL);
1645 if (aq_ret)
1646 dev_info(&pf->pdev->dev,
1647 "set brdcast promisc failed, err %d, aq_err %d\n",
1648 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001649 }
1650
1651 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
1652 return 0;
1653}
1654
1655/**
1656 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
1657 * @pf: board private structure
1658 **/
1659static void i40e_sync_filters_subtask(struct i40e_pf *pf)
1660{
1661 int v;
1662
1663 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
1664 return;
1665 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
1666
1667 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
1668 if (pf->vsi[v] &&
1669 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED))
1670 i40e_sync_vsi_filters(pf->vsi[v]);
1671 }
1672}
1673
1674/**
1675 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
1676 * @netdev: network interface device structure
1677 * @new_mtu: new value for maximum frame size
1678 *
1679 * Returns 0 on success, negative on failure
1680 **/
1681static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
1682{
1683 struct i40e_netdev_priv *np = netdev_priv(netdev);
1684 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
1685 struct i40e_vsi *vsi = np->vsi;
1686
1687 /* MTU < 68 is an error and causes problems on some kernels */
1688 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
1689 return -EINVAL;
1690
1691 netdev_info(netdev, "changing MTU from %d to %d\n",
1692 netdev->mtu, new_mtu);
1693 netdev->mtu = new_mtu;
1694 if (netif_running(netdev))
1695 i40e_vsi_reinit_locked(vsi);
1696
1697 return 0;
1698}
1699
1700/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00001701 * i40e_ioctl - Access the hwtstamp interface
1702 * @netdev: network interface device structure
1703 * @ifr: interface request data
1704 * @cmd: ioctl command
1705 **/
1706int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1707{
1708 struct i40e_netdev_priv *np = netdev_priv(netdev);
1709 struct i40e_pf *pf = np->vsi->back;
1710
1711 switch (cmd) {
1712 case SIOCGHWTSTAMP:
1713 return i40e_ptp_get_ts_config(pf, ifr);
1714 case SIOCSHWTSTAMP:
1715 return i40e_ptp_set_ts_config(pf, ifr);
1716 default:
1717 return -EOPNOTSUPP;
1718 }
1719}
1720
1721/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001722 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
1723 * @vsi: the vsi being adjusted
1724 **/
1725void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
1726{
1727 struct i40e_vsi_context ctxt;
1728 i40e_status ret;
1729
1730 if ((vsi->info.valid_sections &
1731 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1732 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
1733 return; /* already enabled */
1734
1735 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1736 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1737 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
1738
1739 ctxt.seid = vsi->seid;
1740 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1741 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1742 if (ret) {
1743 dev_info(&vsi->back->pdev->dev,
1744 "%s: update vsi failed, aq_err=%d\n",
1745 __func__, vsi->back->hw.aq.asq_last_status);
1746 }
1747}
1748
1749/**
1750 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
1751 * @vsi: the vsi being adjusted
1752 **/
1753void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
1754{
1755 struct i40e_vsi_context ctxt;
1756 i40e_status ret;
1757
1758 if ((vsi->info.valid_sections &
1759 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1760 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
1761 I40E_AQ_VSI_PVLAN_EMOD_MASK))
1762 return; /* already disabled */
1763
1764 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1765 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1766 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
1767
1768 ctxt.seid = vsi->seid;
1769 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1770 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1771 if (ret) {
1772 dev_info(&vsi->back->pdev->dev,
1773 "%s: update vsi failed, aq_err=%d\n",
1774 __func__, vsi->back->hw.aq.asq_last_status);
1775 }
1776}
1777
1778/**
1779 * i40e_vlan_rx_register - Setup or shutdown vlan offload
1780 * @netdev: network interface to be adjusted
1781 * @features: netdev features to test if VLAN offload is enabled or not
1782 **/
1783static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
1784{
1785 struct i40e_netdev_priv *np = netdev_priv(netdev);
1786 struct i40e_vsi *vsi = np->vsi;
1787
1788 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1789 i40e_vlan_stripping_enable(vsi);
1790 else
1791 i40e_vlan_stripping_disable(vsi);
1792}
1793
1794/**
1795 * i40e_vsi_add_vlan - Add vsi membership for given vlan
1796 * @vsi: the vsi being configured
1797 * @vid: vlan id to be added (0 = untagged only , -1 = any)
1798 **/
1799int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
1800{
1801 struct i40e_mac_filter *f, *add_f;
1802 bool is_netdev, is_vf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001803
1804 is_vf = (vsi->type == I40E_VSI_SRIOV);
1805 is_netdev = !!(vsi->netdev);
1806
1807 if (is_netdev) {
1808 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid,
1809 is_vf, is_netdev);
1810 if (!add_f) {
1811 dev_info(&vsi->back->pdev->dev,
1812 "Could not add vlan filter %d for %pM\n",
1813 vid, vsi->netdev->dev_addr);
1814 return -ENOMEM;
1815 }
1816 }
1817
1818 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1819 add_f = i40e_add_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
1820 if (!add_f) {
1821 dev_info(&vsi->back->pdev->dev,
1822 "Could not add vlan filter %d for %pM\n",
1823 vid, f->macaddr);
1824 return -ENOMEM;
1825 }
1826 }
1827
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001828 /* Now if we add a vlan tag, make sure to check if it is the first
1829 * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
1830 * with 0, so we now accept untagged and specified tagged traffic
1831 * (and not any taged and untagged)
1832 */
1833 if (vid > 0) {
1834 if (is_netdev && i40e_find_filter(vsi, vsi->netdev->dev_addr,
1835 I40E_VLAN_ANY,
1836 is_vf, is_netdev)) {
1837 i40e_del_filter(vsi, vsi->netdev->dev_addr,
1838 I40E_VLAN_ANY, is_vf, is_netdev);
1839 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0,
1840 is_vf, is_netdev);
1841 if (!add_f) {
1842 dev_info(&vsi->back->pdev->dev,
1843 "Could not add filter 0 for %pM\n",
1844 vsi->netdev->dev_addr);
1845 return -ENOMEM;
1846 }
1847 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08001848 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001849
Greg Rose8d82a7c2014-01-13 16:13:04 -08001850 /* Do not assume that I40E_VLAN_ANY should be reset to VLAN 0 */
1851 if (vid > 0 && !vsi->info.pvid) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001852 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1853 if (i40e_find_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1854 is_vf, is_netdev)) {
1855 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1856 is_vf, is_netdev);
1857 add_f = i40e_add_filter(vsi, f->macaddr,
1858 0, is_vf, is_netdev);
1859 if (!add_f) {
1860 dev_info(&vsi->back->pdev->dev,
1861 "Could not add filter 0 for %pM\n",
1862 f->macaddr);
1863 return -ENOMEM;
1864 }
1865 }
1866 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001867 }
1868
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001869 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1870 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1871 return 0;
1872
1873 return i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001874}
1875
1876/**
1877 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
1878 * @vsi: the vsi being configured
1879 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001880 *
1881 * Return: 0 on success or negative otherwise
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001882 **/
1883int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
1884{
1885 struct net_device *netdev = vsi->netdev;
1886 struct i40e_mac_filter *f, *add_f;
1887 bool is_vf, is_netdev;
1888 int filter_count = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001889
1890 is_vf = (vsi->type == I40E_VSI_SRIOV);
1891 is_netdev = !!(netdev);
1892
1893 if (is_netdev)
1894 i40e_del_filter(vsi, netdev->dev_addr, vid, is_vf, is_netdev);
1895
1896 list_for_each_entry(f, &vsi->mac_filter_list, list)
1897 i40e_del_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
1898
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001899 /* go through all the filters for this VSI and if there is only
1900 * vid == 0 it means there are no other filters, so vid 0 must
1901 * be replaced with -1. This signifies that we should from now
1902 * on accept any traffic (with any tag present, or untagged)
1903 */
1904 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1905 if (is_netdev) {
1906 if (f->vlan &&
1907 ether_addr_equal(netdev->dev_addr, f->macaddr))
1908 filter_count++;
1909 }
1910
1911 if (f->vlan)
1912 filter_count++;
1913 }
1914
1915 if (!filter_count && is_netdev) {
1916 i40e_del_filter(vsi, netdev->dev_addr, 0, is_vf, is_netdev);
1917 f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
1918 is_vf, is_netdev);
1919 if (!f) {
1920 dev_info(&vsi->back->pdev->dev,
1921 "Could not add filter %d for %pM\n",
1922 I40E_VLAN_ANY, netdev->dev_addr);
1923 return -ENOMEM;
1924 }
1925 }
1926
1927 if (!filter_count) {
1928 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1929 i40e_del_filter(vsi, f->macaddr, 0, is_vf, is_netdev);
1930 add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1931 is_vf, is_netdev);
1932 if (!add_f) {
1933 dev_info(&vsi->back->pdev->dev,
1934 "Could not add filter %d for %pM\n",
1935 I40E_VLAN_ANY, f->macaddr);
1936 return -ENOMEM;
1937 }
1938 }
1939 }
1940
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001941 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1942 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1943 return 0;
1944
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001945 return i40e_sync_vsi_filters(vsi);
1946}
1947
1948/**
1949 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
1950 * @netdev: network interface to be adjusted
1951 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001952 *
1953 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001954 **/
1955static int i40e_vlan_rx_add_vid(struct net_device *netdev,
1956 __always_unused __be16 proto, u16 vid)
1957{
1958 struct i40e_netdev_priv *np = netdev_priv(netdev);
1959 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001960 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001961
1962 if (vid > 4095)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001963 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001964
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001965 netdev_info(netdev, "adding %pM vid=%d\n", netdev->dev_addr, vid);
1966
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001967 /* If the network stack called us with vid = 0, we should
1968 * indicate to i40e_vsi_add_vlan() that we want to receive
1969 * any traffic (i.e. with any vlan tag, or untagged)
1970 */
1971 ret = i40e_vsi_add_vlan(vsi, vid ? vid : I40E_VLAN_ANY);
1972
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001973 if (!ret && (vid < VLAN_N_VID))
1974 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001975
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001976 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001977}
1978
1979/**
1980 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
1981 * @netdev: network interface to be adjusted
1982 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001983 *
1984 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001985 **/
1986static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
1987 __always_unused __be16 proto, u16 vid)
1988{
1989 struct i40e_netdev_priv *np = netdev_priv(netdev);
1990 struct i40e_vsi *vsi = np->vsi;
1991
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001992 netdev_info(netdev, "removing %pM vid=%d\n", netdev->dev_addr, vid);
1993
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001994 /* return code is ignored as there is nothing a user
1995 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001996 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001997 */
1998 i40e_vsi_kill_vlan(vsi, vid);
1999
2000 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002001
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002002 return 0;
2003}
2004
2005/**
2006 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2007 * @vsi: the vsi being brought back up
2008 **/
2009static void i40e_restore_vlan(struct i40e_vsi *vsi)
2010{
2011 u16 vid;
2012
2013 if (!vsi->netdev)
2014 return;
2015
2016 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2017
2018 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2019 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2020 vid);
2021}
2022
2023/**
2024 * i40e_vsi_add_pvid - Add pvid for the VSI
2025 * @vsi: the vsi being adjusted
2026 * @vid: the vlan id to set as a PVID
2027 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002028int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002029{
2030 struct i40e_vsi_context ctxt;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002031 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002032
2033 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2034 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002035 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2036 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002037 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002038
2039 ctxt.seid = vsi->seid;
2040 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002041 aq_ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2042 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002043 dev_info(&vsi->back->pdev->dev,
2044 "%s: update vsi failed, aq_err=%d\n",
2045 __func__, vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002046 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002047 }
2048
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002049 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002050}
2051
2052/**
2053 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2054 * @vsi: the vsi being adjusted
2055 *
2056 * Just use the vlan_rx_register() service to put it back to normal
2057 **/
2058void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2059{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002060 i40e_vlan_stripping_disable(vsi);
2061
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002062 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002063}
2064
2065/**
2066 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2067 * @vsi: ptr to the VSI
2068 *
2069 * If this function returns with an error, then it's possible one or
2070 * more of the rings is populated (while the rest are not). It is the
2071 * callers duty to clean those orphaned rings.
2072 *
2073 * Return 0 on success, negative on failure
2074 **/
2075static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2076{
2077 int i, err = 0;
2078
2079 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002080 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002081
2082 return err;
2083}
2084
2085/**
2086 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2087 * @vsi: ptr to the VSI
2088 *
2089 * Free VSI's transmit software resources
2090 **/
2091static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2092{
2093 int i;
2094
Greg Rose8e9dca52013-12-18 13:45:53 +00002095 if (!vsi->tx_rings)
2096 return;
2097
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002098 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002099 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002100 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002101}
2102
2103/**
2104 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2105 * @vsi: ptr to the VSI
2106 *
2107 * If this function returns with an error, then it's possible one or
2108 * more of the rings is populated (while the rest are not). It is the
2109 * callers duty to clean those orphaned rings.
2110 *
2111 * Return 0 on success, negative on failure
2112 **/
2113static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2114{
2115 int i, err = 0;
2116
2117 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002118 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002119 return err;
2120}
2121
2122/**
2123 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2124 * @vsi: ptr to the VSI
2125 *
2126 * Free all receive software resources
2127 **/
2128static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2129{
2130 int i;
2131
Greg Rose8e9dca52013-12-18 13:45:53 +00002132 if (!vsi->rx_rings)
2133 return;
2134
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002135 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002136 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002137 i40e_free_rx_resources(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002138}
2139
2140/**
2141 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2142 * @ring: The Tx ring to configure
2143 *
2144 * Configure the Tx descriptor ring in the HMC context.
2145 **/
2146static int i40e_configure_tx_ring(struct i40e_ring *ring)
2147{
2148 struct i40e_vsi *vsi = ring->vsi;
2149 u16 pf_q = vsi->base_queue + ring->queue_index;
2150 struct i40e_hw *hw = &vsi->back->hw;
2151 struct i40e_hmc_obj_txq tx_ctx;
2152 i40e_status err = 0;
2153 u32 qtx_ctl = 0;
2154
2155 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002156 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002157 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2158 ring->atr_count = 0;
2159 } else {
2160 ring->atr_sample_rate = 0;
2161 }
2162
2163 /* initialize XPS */
2164 if (ring->q_vector && ring->netdev &&
2165 !test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
2166 netif_set_xps_queue(ring->netdev,
2167 &ring->q_vector->affinity_mask,
2168 ring->queue_index);
2169
2170 /* clear the context structure first */
2171 memset(&tx_ctx, 0, sizeof(tx_ctx));
2172
2173 tx_ctx.new_context = 1;
2174 tx_ctx.base = (ring->dma / 128);
2175 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002176 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2177 I40E_FLAG_FD_ATR_ENABLED));
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002178 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002179
2180 /* As part of VSI creation/update, FW allocates certain
2181 * Tx arbitration queue sets for each TC enabled for
2182 * the VSI. The FW returns the handles to these queue
2183 * sets as part of the response buffer to Add VSI,
2184 * Update VSI, etc. AQ commands. It is expected that
2185 * these queue set handles be associated with the Tx
2186 * queues by the driver as part of the TX queue context
2187 * initialization. This has to be done regardless of
2188 * DCB as by default everything is mapped to TC0.
2189 */
2190 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2191 tx_ctx.rdylist_act = 0;
2192
2193 /* clear the context in the HMC */
2194 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2195 if (err) {
2196 dev_info(&vsi->back->pdev->dev,
2197 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2198 ring->queue_index, pf_q, err);
2199 return -ENOMEM;
2200 }
2201
2202 /* set the context in the HMC */
2203 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2204 if (err) {
2205 dev_info(&vsi->back->pdev->dev,
2206 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2207 ring->queue_index, pf_q, err);
2208 return -ENOMEM;
2209 }
2210
2211 /* Now associate this queue with this PCI function */
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002212 if (vsi->type == I40E_VSI_VMDQ2)
2213 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
2214 else
2215 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Shannon Nelson13fd9772013-09-28 07:14:19 +00002216 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2217 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002218 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2219 i40e_flush(hw);
2220
2221 clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
2222
2223 /* cache tail off for easier writes later */
2224 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2225
2226 return 0;
2227}
2228
2229/**
2230 * i40e_configure_rx_ring - Configure a receive ring context
2231 * @ring: The Rx ring to configure
2232 *
2233 * Configure the Rx descriptor ring in the HMC context.
2234 **/
2235static int i40e_configure_rx_ring(struct i40e_ring *ring)
2236{
2237 struct i40e_vsi *vsi = ring->vsi;
2238 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2239 u16 pf_q = vsi->base_queue + ring->queue_index;
2240 struct i40e_hw *hw = &vsi->back->hw;
2241 struct i40e_hmc_obj_rxq rx_ctx;
2242 i40e_status err = 0;
2243
2244 ring->state = 0;
2245
2246 /* clear the context structure first */
2247 memset(&rx_ctx, 0, sizeof(rx_ctx));
2248
2249 ring->rx_buf_len = vsi->rx_buf_len;
2250 ring->rx_hdr_len = vsi->rx_hdr_len;
2251
2252 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
2253 rx_ctx.hbuff = ring->rx_hdr_len >> I40E_RXQ_CTX_HBUFF_SHIFT;
2254
2255 rx_ctx.base = (ring->dma / 128);
2256 rx_ctx.qlen = ring->count;
2257
2258 if (vsi->back->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED) {
2259 set_ring_16byte_desc_enabled(ring);
2260 rx_ctx.dsize = 0;
2261 } else {
2262 rx_ctx.dsize = 1;
2263 }
2264
2265 rx_ctx.dtype = vsi->dtype;
2266 if (vsi->dtype) {
2267 set_ring_ps_enabled(ring);
2268 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
2269 I40E_RX_SPLIT_IP |
2270 I40E_RX_SPLIT_TCP_UDP |
2271 I40E_RX_SPLIT_SCTP;
2272 } else {
2273 rx_ctx.hsplit_0 = 0;
2274 }
2275
2276 rx_ctx.rxmax = min_t(u16, vsi->max_frame,
2277 (chain_len * ring->rx_buf_len));
2278 rx_ctx.tphrdesc_ena = 1;
2279 rx_ctx.tphwdesc_ena = 1;
2280 rx_ctx.tphdata_ena = 1;
2281 rx_ctx.tphhead_ena = 1;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00002282 if (hw->revision_id == 0)
2283 rx_ctx.lrxqthresh = 0;
2284 else
2285 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002286 rx_ctx.crcstrip = 1;
2287 rx_ctx.l2tsel = 1;
2288 rx_ctx.showiv = 1;
2289
2290 /* clear the context in the HMC */
2291 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2292 if (err) {
2293 dev_info(&vsi->back->pdev->dev,
2294 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2295 ring->queue_index, pf_q, err);
2296 return -ENOMEM;
2297 }
2298
2299 /* set the context in the HMC */
2300 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2301 if (err) {
2302 dev_info(&vsi->back->pdev->dev,
2303 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2304 ring->queue_index, pf_q, err);
2305 return -ENOMEM;
2306 }
2307
2308 /* cache tail for quicker writes, and clear the reg before use */
2309 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2310 writel(0, ring->tail);
2311
2312 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
2313
2314 return 0;
2315}
2316
2317/**
2318 * i40e_vsi_configure_tx - Configure the VSI for Tx
2319 * @vsi: VSI structure describing this set of rings and resources
2320 *
2321 * Configure the Tx VSI for operation.
2322 **/
2323static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2324{
2325 int err = 0;
2326 u16 i;
2327
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002328 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2329 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002330
2331 return err;
2332}
2333
2334/**
2335 * i40e_vsi_configure_rx - Configure the VSI for Rx
2336 * @vsi: the VSI being configured
2337 *
2338 * Configure the Rx VSI for operation.
2339 **/
2340static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
2341{
2342 int err = 0;
2343 u16 i;
2344
2345 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
2346 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
2347 + ETH_FCS_LEN + VLAN_HLEN;
2348 else
2349 vsi->max_frame = I40E_RXBUFFER_2048;
2350
2351 /* figure out correct receive buffer length */
2352 switch (vsi->back->flags & (I40E_FLAG_RX_1BUF_ENABLED |
2353 I40E_FLAG_RX_PS_ENABLED)) {
2354 case I40E_FLAG_RX_1BUF_ENABLED:
2355 vsi->rx_hdr_len = 0;
2356 vsi->rx_buf_len = vsi->max_frame;
2357 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2358 break;
2359 case I40E_FLAG_RX_PS_ENABLED:
2360 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2361 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2362 vsi->dtype = I40E_RX_DTYPE_HEADER_SPLIT;
2363 break;
2364 default:
2365 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2366 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2367 vsi->dtype = I40E_RX_DTYPE_SPLIT_ALWAYS;
2368 break;
2369 }
2370
2371 /* round up for the chip's needs */
2372 vsi->rx_hdr_len = ALIGN(vsi->rx_hdr_len,
2373 (1 << I40E_RXQ_CTX_HBUFF_SHIFT));
2374 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
2375 (1 << I40E_RXQ_CTX_DBUFF_SHIFT));
2376
2377 /* set up individual rings */
2378 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002379 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002380
2381 return err;
2382}
2383
2384/**
2385 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
2386 * @vsi: ptr to the VSI
2387 **/
2388static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
2389{
2390 u16 qoffset, qcount;
2391 int i, n;
2392
2393 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED))
2394 return;
2395
2396 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
2397 if (!(vsi->tc_config.enabled_tc & (1 << n)))
2398 continue;
2399
2400 qoffset = vsi->tc_config.tc_info[n].qoffset;
2401 qcount = vsi->tc_config.tc_info[n].qcount;
2402 for (i = qoffset; i < (qoffset + qcount); i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002403 struct i40e_ring *rx_ring = vsi->rx_rings[i];
2404 struct i40e_ring *tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002405 rx_ring->dcb_tc = n;
2406 tx_ring->dcb_tc = n;
2407 }
2408 }
2409}
2410
2411/**
2412 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
2413 * @vsi: ptr to the VSI
2414 **/
2415static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
2416{
2417 if (vsi->netdev)
2418 i40e_set_rx_mode(vsi->netdev);
2419}
2420
2421/**
2422 * i40e_vsi_configure - Set up the VSI for action
2423 * @vsi: the VSI being configured
2424 **/
2425static int i40e_vsi_configure(struct i40e_vsi *vsi)
2426{
2427 int err;
2428
2429 i40e_set_vsi_rx_mode(vsi);
2430 i40e_restore_vlan(vsi);
2431 i40e_vsi_config_dcb_rings(vsi);
2432 err = i40e_vsi_configure_tx(vsi);
2433 if (!err)
2434 err = i40e_vsi_configure_rx(vsi);
2435
2436 return err;
2437}
2438
2439/**
2440 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
2441 * @vsi: the VSI being configured
2442 **/
2443static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
2444{
2445 struct i40e_pf *pf = vsi->back;
2446 struct i40e_q_vector *q_vector;
2447 struct i40e_hw *hw = &pf->hw;
2448 u16 vector;
2449 int i, q;
2450 u32 val;
2451 u32 qp;
2452
2453 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
2454 * and PFINT_LNKLSTn registers, e.g.:
2455 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
2456 */
2457 qp = vsi->base_queue;
2458 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00002459 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
2460 q_vector = vsi->q_vectors[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002461 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2462 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2463 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
2464 q_vector->rx.itr);
2465 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2466 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2467 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
2468 q_vector->tx.itr);
2469
2470 /* Linked list for the queuepairs assigned to this vector */
2471 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
2472 for (q = 0; q < q_vector->num_ringpairs; q++) {
2473 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2474 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2475 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
2476 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
2477 (I40E_QUEUE_TYPE_TX
2478 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
2479
2480 wr32(hw, I40E_QINT_RQCTL(qp), val);
2481
2482 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2483 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2484 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
2485 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
2486 (I40E_QUEUE_TYPE_RX
2487 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2488
2489 /* Terminate the linked list */
2490 if (q == (q_vector->num_ringpairs - 1))
2491 val |= (I40E_QUEUE_END_OF_LIST
2492 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2493
2494 wr32(hw, I40E_QINT_TQCTL(qp), val);
2495 qp++;
2496 }
2497 }
2498
2499 i40e_flush(hw);
2500}
2501
2502/**
2503 * i40e_enable_misc_int_causes - enable the non-queue interrupts
2504 * @hw: ptr to the hardware info
2505 **/
2506static void i40e_enable_misc_int_causes(struct i40e_hw *hw)
2507{
2508 u32 val;
2509
2510 /* clear things first */
2511 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
2512 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
2513
2514 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
2515 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
2516 I40E_PFINT_ICR0_ENA_GRST_MASK |
2517 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
2518 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002519 I40E_PFINT_ICR0_ENA_TIMESYNC_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002520 I40E_PFINT_ICR0_ENA_STORM_DETECT_MASK |
2521 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
2522 I40E_PFINT_ICR0_ENA_VFLR_MASK |
2523 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2524
2525 wr32(hw, I40E_PFINT_ICR0_ENA, val);
2526
2527 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00002528 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
2529 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002530
2531 /* OTHER_ITR_IDX = 0 */
2532 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
2533}
2534
2535/**
2536 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
2537 * @vsi: the VSI being configured
2538 **/
2539static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
2540{
Alexander Duyck493fb302013-09-28 07:01:44 +00002541 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002542 struct i40e_pf *pf = vsi->back;
2543 struct i40e_hw *hw = &pf->hw;
2544 u32 val;
2545
2546 /* set the ITR configuration */
2547 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2548 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2549 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
2550 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2551 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2552 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
2553
2554 i40e_enable_misc_int_causes(hw);
2555
2556 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
2557 wr32(hw, I40E_PFINT_LNKLST0, 0);
2558
2559 /* Associate the queue pair to the vector and enable the q int */
2560 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2561 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2562 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2563
2564 wr32(hw, I40E_QINT_RQCTL(0), val);
2565
2566 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2567 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2568 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2569
2570 wr32(hw, I40E_QINT_TQCTL(0), val);
2571 i40e_flush(hw);
2572}
2573
2574/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00002575 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
2576 * @pf: board private structure
2577 **/
2578void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
2579{
2580 struct i40e_hw *hw = &pf->hw;
2581
2582 wr32(hw, I40E_PFINT_DYN_CTL0,
2583 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2584 i40e_flush(hw);
2585}
2586
2587/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002588 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
2589 * @pf: board private structure
2590 **/
Shannon Nelson116a57d2013-09-28 07:13:59 +00002591void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002592{
2593 struct i40e_hw *hw = &pf->hw;
2594 u32 val;
2595
2596 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
2597 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
2598 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
2599
2600 wr32(hw, I40E_PFINT_DYN_CTL0, val);
2601 i40e_flush(hw);
2602}
2603
2604/**
2605 * i40e_irq_dynamic_enable - Enable default interrupt generation settings
2606 * @vsi: pointer to a vsi
2607 * @vector: enable a particular Hw Interrupt vector
2608 **/
2609void i40e_irq_dynamic_enable(struct i40e_vsi *vsi, int vector)
2610{
2611 struct i40e_pf *pf = vsi->back;
2612 struct i40e_hw *hw = &pf->hw;
2613 u32 val;
2614
2615 val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
2616 I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
2617 (I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2618 wr32(hw, I40E_PFINT_DYN_CTLN(vector - 1), val);
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00002619 /* skip the flush */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002620}
2621
2622/**
2623 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
2624 * @irq: interrupt number
2625 * @data: pointer to a q_vector
2626 **/
2627static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
2628{
2629 struct i40e_q_vector *q_vector = data;
2630
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002631 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002632 return IRQ_HANDLED;
2633
2634 napi_schedule(&q_vector->napi);
2635
2636 return IRQ_HANDLED;
2637}
2638
2639/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002640 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
2641 * @vsi: the VSI being configured
2642 * @basename: name for the vector
2643 *
2644 * Allocates MSI-X vectors and requests interrupts from the kernel.
2645 **/
2646static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
2647{
2648 int q_vectors = vsi->num_q_vectors;
2649 struct i40e_pf *pf = vsi->back;
2650 int base = vsi->base_vector;
2651 int rx_int_idx = 0;
2652 int tx_int_idx = 0;
2653 int vector, err;
2654
2655 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00002656 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002657
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002658 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002659 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2660 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
2661 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002662 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002663 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2664 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002665 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002666 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2667 "%s-%s-%d", basename, "tx", tx_int_idx++);
2668 } else {
2669 /* skip this unused q_vector */
2670 continue;
2671 }
2672 err = request_irq(pf->msix_entries[base + vector].vector,
2673 vsi->irq_handler,
2674 0,
2675 q_vector->name,
2676 q_vector);
2677 if (err) {
2678 dev_info(&pf->pdev->dev,
2679 "%s: request_irq failed, error: %d\n",
2680 __func__, err);
2681 goto free_queue_irqs;
2682 }
2683 /* assign the mask for this irq */
2684 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2685 &q_vector->affinity_mask);
2686 }
2687
2688 return 0;
2689
2690free_queue_irqs:
2691 while (vector) {
2692 vector--;
2693 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2694 NULL);
2695 free_irq(pf->msix_entries[base + vector].vector,
2696 &(vsi->q_vectors[vector]));
2697 }
2698 return err;
2699}
2700
2701/**
2702 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
2703 * @vsi: the VSI being un-configured
2704 **/
2705static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
2706{
2707 struct i40e_pf *pf = vsi->back;
2708 struct i40e_hw *hw = &pf->hw;
2709 int base = vsi->base_vector;
2710 int i;
2711
2712 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002713 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
2714 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002715 }
2716
2717 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2718 for (i = vsi->base_vector;
2719 i < (vsi->num_q_vectors + vsi->base_vector); i++)
2720 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
2721
2722 i40e_flush(hw);
2723 for (i = 0; i < vsi->num_q_vectors; i++)
2724 synchronize_irq(pf->msix_entries[i + base].vector);
2725 } else {
2726 /* Legacy and MSI mode - this stops all interrupt handling */
2727 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
2728 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
2729 i40e_flush(hw);
2730 synchronize_irq(pf->pdev->irq);
2731 }
2732}
2733
2734/**
2735 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
2736 * @vsi: the VSI being configured
2737 **/
2738static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
2739{
2740 struct i40e_pf *pf = vsi->back;
2741 int i;
2742
2743 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2744 for (i = vsi->base_vector;
2745 i < (vsi->num_q_vectors + vsi->base_vector); i++)
2746 i40e_irq_dynamic_enable(vsi, i);
2747 } else {
2748 i40e_irq_dynamic_enable_icr0(pf);
2749 }
2750
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00002751 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002752 return 0;
2753}
2754
2755/**
2756 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
2757 * @pf: board private structure
2758 **/
2759static void i40e_stop_misc_vector(struct i40e_pf *pf)
2760{
2761 /* Disable ICR 0 */
2762 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
2763 i40e_flush(&pf->hw);
2764}
2765
2766/**
2767 * i40e_intr - MSI/Legacy and non-queue interrupt handler
2768 * @irq: interrupt number
2769 * @data: pointer to a q_vector
2770 *
2771 * This is the handler used for all MSI/Legacy interrupts, and deals
2772 * with both queue and non-queue interrupts. This is also used in
2773 * MSIX mode to handle the non-queue interrupts.
2774 **/
2775static irqreturn_t i40e_intr(int irq, void *data)
2776{
2777 struct i40e_pf *pf = (struct i40e_pf *)data;
2778 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002779 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002780 u32 icr0, icr0_remaining;
2781 u32 val, ena_mask;
2782
2783 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002784 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002785
Shannon Nelson116a57d2013-09-28 07:13:59 +00002786 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
2787 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002788 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002789
Shannon Nelsoncd92e722013-11-16 10:00:44 +00002790 /* if interrupt but no bits showing, must be SWINT */
2791 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
2792 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
2793 pf->sw_int_count++;
2794
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002795 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
2796 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
2797
2798 /* temporarily disable queue cause for NAPI processing */
2799 u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
2800 qval &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
2801 wr32(hw, I40E_QINT_RQCTL(0), qval);
2802
2803 qval = rd32(hw, I40E_QINT_TQCTL(0));
2804 qval &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
2805 wr32(hw, I40E_QINT_TQCTL(0), qval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002806
2807 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck493fb302013-09-28 07:01:44 +00002808 napi_schedule(&pf->vsi[pf->lan_vsi]->q_vectors[0]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002809 }
2810
2811 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
2812 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2813 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
2814 }
2815
2816 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
2817 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
2818 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
2819 }
2820
2821 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
2822 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
2823 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
2824 }
2825
2826 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
2827 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
2828 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
2829 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
2830 val = rd32(hw, I40E_GLGEN_RSTAT);
2831 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
2832 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelsond52cf0a2013-11-16 10:00:39 +00002833 if (val == I40E_RESET_CORER)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002834 pf->corer_count++;
Shannon Nelsond52cf0a2013-11-16 10:00:39 +00002835 else if (val == I40E_RESET_GLOBR)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002836 pf->globr_count++;
Shannon Nelsond52cf0a2013-11-16 10:00:39 +00002837 else if (val == I40E_RESET_EMPR)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002838 pf->empr_count++;
2839 }
2840
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002841 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
2842 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
2843 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
2844 }
2845
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002846 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
2847 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
2848
2849 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
2850 ena_mask &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
2851 i40e_ptp_tx_hwtstamp(pf);
2852 prttsyn_stat &= ~I40E_PRTTSYN_STAT_0_TXTIME_MASK;
2853 }
2854
2855 wr32(hw, I40E_PRTTSYN_STAT_0, prttsyn_stat);
2856 }
2857
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002858 /* If a critical error is pending we have no choice but to reset the
2859 * device.
2860 * Report and mask out any remaining unexpected interrupts.
2861 */
2862 icr0_remaining = icr0 & ena_mask;
2863 if (icr0_remaining) {
2864 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
2865 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002866 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002867 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
2868 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK) ||
2869 (icr0_remaining & I40E_PFINT_ICR0_MAL_DETECT_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002870 dev_info(&pf->pdev->dev, "device will be reset\n");
2871 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
2872 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002873 }
2874 ena_mask &= ~icr0_remaining;
2875 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002876 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002877
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002878enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002879 /* re-enable interrupt causes */
2880 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002881 if (!test_bit(__I40E_DOWN, &pf->state)) {
2882 i40e_service_event_schedule(pf);
2883 i40e_irq_dynamic_enable_icr0(pf);
2884 }
2885
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002886 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002887}
2888
2889/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08002890 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
2891 * @tx_ring: tx ring to clean
2892 * @budget: how many cleans we're allowed
2893 *
2894 * Returns true if there's any budget left (e.g. the clean is finished)
2895 **/
2896static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
2897{
2898 struct i40e_vsi *vsi = tx_ring->vsi;
2899 u16 i = tx_ring->next_to_clean;
2900 struct i40e_tx_buffer *tx_buf;
2901 struct i40e_tx_desc *tx_desc;
2902
2903 tx_buf = &tx_ring->tx_bi[i];
2904 tx_desc = I40E_TX_DESC(tx_ring, i);
2905 i -= tx_ring->count;
2906
2907 do {
2908 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
2909
2910 /* if next_to_watch is not set then there is no work pending */
2911 if (!eop_desc)
2912 break;
2913
2914 /* prevent any other reads prior to eop_desc */
2915 read_barrier_depends();
2916
2917 /* if the descriptor isn't done, no work yet to do */
2918 if (!(eop_desc->cmd_type_offset_bsz &
2919 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
2920 break;
2921
2922 /* clear next_to_watch to prevent false hangs */
2923 tx_buf->next_to_watch = NULL;
2924
2925 /* unmap skb header data */
2926 dma_unmap_single(tx_ring->dev,
2927 dma_unmap_addr(tx_buf, dma),
2928 dma_unmap_len(tx_buf, len),
2929 DMA_TO_DEVICE);
2930
2931 dma_unmap_len_set(tx_buf, len, 0);
2932
2933
2934 /* move to the next desc and buffer to clean */
2935 tx_buf++;
2936 tx_desc++;
2937 i++;
2938 if (unlikely(!i)) {
2939 i -= tx_ring->count;
2940 tx_buf = tx_ring->tx_bi;
2941 tx_desc = I40E_TX_DESC(tx_ring, 0);
2942 }
2943
2944 /* update budget accounting */
2945 budget--;
2946 } while (likely(budget));
2947
2948 i += tx_ring->count;
2949 tx_ring->next_to_clean = i;
2950
2951 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
2952 i40e_irq_dynamic_enable(vsi,
2953 tx_ring->q_vector->v_idx + vsi->base_vector);
2954 }
2955 return budget > 0;
2956}
2957
2958/**
2959 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
2960 * @irq: interrupt number
2961 * @data: pointer to a q_vector
2962 **/
2963static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
2964{
2965 struct i40e_q_vector *q_vector = data;
2966 struct i40e_vsi *vsi;
2967
2968 if (!q_vector->tx.ring)
2969 return IRQ_HANDLED;
2970
2971 vsi = q_vector->tx.ring->vsi;
2972 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
2973
2974 return IRQ_HANDLED;
2975}
2976
2977/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002978 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002979 * @vsi: the VSI being configured
2980 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002981 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002982 **/
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002983static void map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002984{
Alexander Duyck493fb302013-09-28 07:01:44 +00002985 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002986 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
2987 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002988
2989 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002990 tx_ring->next = q_vector->tx.ring;
2991 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002992 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002993
2994 rx_ring->q_vector = q_vector;
2995 rx_ring->next = q_vector->rx.ring;
2996 q_vector->rx.ring = rx_ring;
2997 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002998}
2999
3000/**
3001 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3002 * @vsi: the VSI being configured
3003 *
3004 * This function maps descriptor rings to the queue-specific vectors
3005 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3006 * one vector per queue pair, but on a constrained vector budget, we
3007 * group the queue pairs as "efficiently" as possible.
3008 **/
3009static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3010{
3011 int qp_remaining = vsi->num_queue_pairs;
3012 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003013 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003014 int v_start = 0;
3015 int qp_idx = 0;
3016
3017 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3018 * group them so there are multiple queues per vector.
3019 */
3020 for (; v_start < q_vectors && qp_remaining; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003021 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3022
3023 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3024
3025 q_vector->num_ringpairs = num_ringpairs;
3026
3027 q_vector->rx.count = 0;
3028 q_vector->tx.count = 0;
3029 q_vector->rx.ring = NULL;
3030 q_vector->tx.ring = NULL;
3031
3032 while (num_ringpairs--) {
3033 map_vector_to_qp(vsi, v_start, qp_idx);
3034 qp_idx++;
3035 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003036 }
3037 }
3038}
3039
3040/**
3041 * i40e_vsi_request_irq - Request IRQ from the OS
3042 * @vsi: the VSI being configured
3043 * @basename: name for the vector
3044 **/
3045static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3046{
3047 struct i40e_pf *pf = vsi->back;
3048 int err;
3049
3050 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3051 err = i40e_vsi_request_irq_msix(vsi, basename);
3052 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3053 err = request_irq(pf->pdev->irq, i40e_intr, 0,
3054 pf->misc_int_name, pf);
3055 else
3056 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
3057 pf->misc_int_name, pf);
3058
3059 if (err)
3060 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3061
3062 return err;
3063}
3064
3065#ifdef CONFIG_NET_POLL_CONTROLLER
3066/**
3067 * i40e_netpoll - A Polling 'interrupt'handler
3068 * @netdev: network interface device structure
3069 *
3070 * This is used by netconsole to send skbs without having to re-enable
3071 * interrupts. It's not called while the normal interrupt routine is executing.
3072 **/
3073static void i40e_netpoll(struct net_device *netdev)
3074{
3075 struct i40e_netdev_priv *np = netdev_priv(netdev);
3076 struct i40e_vsi *vsi = np->vsi;
3077 struct i40e_pf *pf = vsi->back;
3078 int i;
3079
3080 /* if interface is down do nothing */
3081 if (test_bit(__I40E_DOWN, &vsi->state))
3082 return;
3083
3084 pf->flags |= I40E_FLAG_IN_NETPOLL;
3085 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3086 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003087 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003088 } else {
3089 i40e_intr(pf->pdev->irq, netdev);
3090 }
3091 pf->flags &= ~I40E_FLAG_IN_NETPOLL;
3092}
3093#endif
3094
3095/**
3096 * i40e_vsi_control_tx - Start or stop a VSI's rings
3097 * @vsi: the VSI being configured
3098 * @enable: start or stop the rings
3099 **/
3100static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3101{
3102 struct i40e_pf *pf = vsi->back;
3103 struct i40e_hw *hw = &pf->hw;
3104 int i, j, pf_q;
3105 u32 tx_reg;
3106
3107 pf_q = vsi->base_queue;
3108 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
3109 j = 1000;
3110 do {
3111 usleep_range(1000, 2000);
3112 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
3113 } while (j-- && ((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT)
3114 ^ (tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT)) & 1);
3115
Mitch Williamsfda972f2013-11-28 06:39:29 +00003116 /* Skip if the queue is already in the requested state */
3117 if (enable && (tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3118 continue;
3119 if (!enable && !(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3120 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003121
3122 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003123 if (enable) {
3124 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003125 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK |
3126 I40E_QTX_ENA_QENA_STAT_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003127 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003128 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003129 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003130
3131 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
3132
3133 /* wait for the change to finish */
3134 for (j = 0; j < 10; j++) {
3135 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
3136 if (enable) {
3137 if ((tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3138 break;
3139 } else {
3140 if (!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3141 break;
3142 }
3143
3144 udelay(10);
3145 }
3146 if (j >= 10) {
3147 dev_info(&pf->pdev->dev, "Tx ring %d %sable timeout\n",
3148 pf_q, (enable ? "en" : "dis"));
3149 return -ETIMEDOUT;
3150 }
3151 }
3152
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003153 if (hw->revision_id == 0)
3154 mdelay(50);
3155
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003156 return 0;
3157}
3158
3159/**
3160 * i40e_vsi_control_rx - Start or stop a VSI's rings
3161 * @vsi: the VSI being configured
3162 * @enable: start or stop the rings
3163 **/
3164static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3165{
3166 struct i40e_pf *pf = vsi->back;
3167 struct i40e_hw *hw = &pf->hw;
3168 int i, j, pf_q;
3169 u32 rx_reg;
3170
3171 pf_q = vsi->base_queue;
3172 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
3173 j = 1000;
3174 do {
3175 usleep_range(1000, 2000);
3176 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
3177 } while (j-- && ((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT)
3178 ^ (rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT)) & 1);
3179
3180 if (enable) {
3181 /* is STAT set ? */
3182 if ((rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3183 continue;
3184 } else {
3185 /* is !STAT set ? */
3186 if (!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3187 continue;
3188 }
3189
3190 /* turn on/off the queue */
3191 if (enable)
3192 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK |
3193 I40E_QRX_ENA_QENA_STAT_MASK;
3194 else
3195 rx_reg &= ~(I40E_QRX_ENA_QENA_REQ_MASK |
3196 I40E_QRX_ENA_QENA_STAT_MASK);
3197 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
3198
3199 /* wait for the change to finish */
3200 for (j = 0; j < 10; j++) {
3201 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
3202
3203 if (enable) {
3204 if ((rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3205 break;
3206 } else {
3207 if (!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3208 break;
3209 }
3210
3211 udelay(10);
3212 }
3213 if (j >= 10) {
3214 dev_info(&pf->pdev->dev, "Rx ring %d %sable timeout\n",
3215 pf_q, (enable ? "en" : "dis"));
3216 return -ETIMEDOUT;
3217 }
3218 }
3219
3220 return 0;
3221}
3222
3223/**
3224 * i40e_vsi_control_rings - Start or stop a VSI's rings
3225 * @vsi: the VSI being configured
3226 * @enable: start or stop the rings
3227 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00003228int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003229{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003230 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003231
3232 /* do rx first for enable and last for disable */
3233 if (request) {
3234 ret = i40e_vsi_control_rx(vsi, request);
3235 if (ret)
3236 return ret;
3237 ret = i40e_vsi_control_tx(vsi, request);
3238 } else {
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003239 /* Ignore return value, we need to shutdown whatever we can */
3240 i40e_vsi_control_tx(vsi, request);
3241 i40e_vsi_control_rx(vsi, request);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003242 }
3243
3244 return ret;
3245}
3246
3247/**
3248 * i40e_vsi_free_irq - Free the irq association with the OS
3249 * @vsi: the VSI being configured
3250 **/
3251static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
3252{
3253 struct i40e_pf *pf = vsi->back;
3254 struct i40e_hw *hw = &pf->hw;
3255 int base = vsi->base_vector;
3256 u32 val, qp;
3257 int i;
3258
3259 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3260 if (!vsi->q_vectors)
3261 return;
3262
3263 for (i = 0; i < vsi->num_q_vectors; i++) {
3264 u16 vector = i + base;
3265
3266 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00003267 if (!vsi->q_vectors[i] ||
3268 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003269 continue;
3270
3271 /* clear the affinity_mask in the IRQ descriptor */
3272 irq_set_affinity_hint(pf->msix_entries[vector].vector,
3273 NULL);
3274 free_irq(pf->msix_entries[vector].vector,
Alexander Duyck493fb302013-09-28 07:01:44 +00003275 vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003276
3277 /* Tear down the interrupt queue link list
3278 *
3279 * We know that they come in pairs and always
3280 * the Rx first, then the Tx. To clear the
3281 * link list, stick the EOL value into the
3282 * next_q field of the registers.
3283 */
3284 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
3285 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3286 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3287 val |= I40E_QUEUE_END_OF_LIST
3288 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3289 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
3290
3291 while (qp != I40E_QUEUE_END_OF_LIST) {
3292 u32 next;
3293
3294 val = rd32(hw, I40E_QINT_RQCTL(qp));
3295
3296 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3297 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3298 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3299 I40E_QINT_RQCTL_INTEVENT_MASK);
3300
3301 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3302 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3303
3304 wr32(hw, I40E_QINT_RQCTL(qp), val);
3305
3306 val = rd32(hw, I40E_QINT_TQCTL(qp));
3307
3308 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
3309 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
3310
3311 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3312 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3313 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3314 I40E_QINT_TQCTL_INTEVENT_MASK);
3315
3316 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3317 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3318
3319 wr32(hw, I40E_QINT_TQCTL(qp), val);
3320 qp = next;
3321 }
3322 }
3323 } else {
3324 free_irq(pf->pdev->irq, pf);
3325
3326 val = rd32(hw, I40E_PFINT_LNKLST0);
3327 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3328 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3329 val |= I40E_QUEUE_END_OF_LIST
3330 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
3331 wr32(hw, I40E_PFINT_LNKLST0, val);
3332
3333 val = rd32(hw, I40E_QINT_RQCTL(qp));
3334 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3335 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3336 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3337 I40E_QINT_RQCTL_INTEVENT_MASK);
3338
3339 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3340 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3341
3342 wr32(hw, I40E_QINT_RQCTL(qp), val);
3343
3344 val = rd32(hw, I40E_QINT_TQCTL(qp));
3345
3346 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3347 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3348 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3349 I40E_QINT_TQCTL_INTEVENT_MASK);
3350
3351 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3352 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3353
3354 wr32(hw, I40E_QINT_TQCTL(qp), val);
3355 }
3356}
3357
3358/**
Alexander Duyck493fb302013-09-28 07:01:44 +00003359 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
3360 * @vsi: the VSI being configured
3361 * @v_idx: Index of vector to be freed
3362 *
3363 * This function frees the memory allocated to the q_vector. In addition if
3364 * NAPI is enabled it will delete any references to the NAPI struct prior
3365 * to freeing the q_vector.
3366 **/
3367static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
3368{
3369 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003370 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00003371
3372 if (!q_vector)
3373 return;
3374
3375 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003376 i40e_for_each_ring(ring, q_vector->tx)
3377 ring->q_vector = NULL;
3378
3379 i40e_for_each_ring(ring, q_vector->rx)
3380 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00003381
3382 /* only VSI w/ an associated netdev is set up w/ NAPI */
3383 if (vsi->netdev)
3384 netif_napi_del(&q_vector->napi);
3385
3386 vsi->q_vectors[v_idx] = NULL;
3387
3388 kfree_rcu(q_vector, rcu);
3389}
3390
3391/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003392 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
3393 * @vsi: the VSI being un-configured
3394 *
3395 * This frees the memory allocated to the q_vectors and
3396 * deletes references to the NAPI struct.
3397 **/
3398static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
3399{
3400 int v_idx;
3401
Alexander Duyck493fb302013-09-28 07:01:44 +00003402 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
3403 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003404}
3405
3406/**
3407 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
3408 * @pf: board private structure
3409 **/
3410static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
3411{
3412 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
3413 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3414 pci_disable_msix(pf->pdev);
3415 kfree(pf->msix_entries);
3416 pf->msix_entries = NULL;
3417 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
3418 pci_disable_msi(pf->pdev);
3419 }
3420 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
3421}
3422
3423/**
3424 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
3425 * @pf: board private structure
3426 *
3427 * We go through and clear interrupt specific resources and reset the structure
3428 * to pre-load conditions
3429 **/
3430static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
3431{
3432 int i;
3433
3434 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
3435 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
3436 if (pf->vsi[i])
3437 i40e_vsi_free_q_vectors(pf->vsi[i]);
3438 i40e_reset_interrupt_capability(pf);
3439}
3440
3441/**
3442 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
3443 * @vsi: the VSI being configured
3444 **/
3445static void i40e_napi_enable_all(struct i40e_vsi *vsi)
3446{
3447 int q_idx;
3448
3449 if (!vsi->netdev)
3450 return;
3451
3452 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003453 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003454}
3455
3456/**
3457 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
3458 * @vsi: the VSI being configured
3459 **/
3460static void i40e_napi_disable_all(struct i40e_vsi *vsi)
3461{
3462 int q_idx;
3463
3464 if (!vsi->netdev)
3465 return;
3466
3467 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003468 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003469}
3470
3471/**
3472 * i40e_quiesce_vsi - Pause a given VSI
3473 * @vsi: the VSI being paused
3474 **/
3475static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
3476{
3477 if (test_bit(__I40E_DOWN, &vsi->state))
3478 return;
3479
3480 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
3481 if (vsi->netdev && netif_running(vsi->netdev)) {
3482 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
3483 } else {
3484 set_bit(__I40E_DOWN, &vsi->state);
3485 i40e_down(vsi);
3486 }
3487}
3488
3489/**
3490 * i40e_unquiesce_vsi - Resume a given VSI
3491 * @vsi: the VSI being resumed
3492 **/
3493static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
3494{
3495 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
3496 return;
3497
3498 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
3499 if (vsi->netdev && netif_running(vsi->netdev))
3500 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
3501 else
3502 i40e_up(vsi); /* this clears the DOWN bit */
3503}
3504
3505/**
3506 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
3507 * @pf: the PF
3508 **/
3509static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
3510{
3511 int v;
3512
3513 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
3514 if (pf->vsi[v])
3515 i40e_quiesce_vsi(pf->vsi[v]);
3516 }
3517}
3518
3519/**
3520 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
3521 * @pf: the PF
3522 **/
3523static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
3524{
3525 int v;
3526
3527 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
3528 if (pf->vsi[v])
3529 i40e_unquiesce_vsi(pf->vsi[v]);
3530 }
3531}
3532
3533/**
3534 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
3535 * @dcbcfg: the corresponding DCBx configuration structure
3536 *
3537 * Return the number of TCs from given DCBx configuration
3538 **/
3539static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
3540{
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003541 u8 num_tc = 0;
3542 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003543
3544 /* Scan the ETS Config Priority Table to find
3545 * traffic class enabled for a given priority
3546 * and use the traffic class index to get the
3547 * number of traffic classes enabled
3548 */
3549 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3550 if (dcbcfg->etscfg.prioritytable[i] > num_tc)
3551 num_tc = dcbcfg->etscfg.prioritytable[i];
3552 }
3553
3554 /* Traffic class index starts from zero so
3555 * increment to return the actual count
3556 */
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003557 return num_tc + 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003558}
3559
3560/**
3561 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
3562 * @dcbcfg: the corresponding DCBx configuration structure
3563 *
3564 * Query the current DCB configuration and return the number of
3565 * traffic classes enabled from the given DCBX config
3566 **/
3567static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
3568{
3569 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
3570 u8 enabled_tc = 1;
3571 u8 i;
3572
3573 for (i = 0; i < num_tc; i++)
3574 enabled_tc |= 1 << i;
3575
3576 return enabled_tc;
3577}
3578
3579/**
3580 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
3581 * @pf: PF being queried
3582 *
3583 * Return number of traffic classes enabled for the given PF
3584 **/
3585static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
3586{
3587 struct i40e_hw *hw = &pf->hw;
3588 u8 i, enabled_tc;
3589 u8 num_tc = 0;
3590 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3591
3592 /* If DCB is not enabled then always in single TC */
3593 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3594 return 1;
3595
3596 /* MFP mode return count of enabled TCs for this PF */
3597 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
3598 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3599 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3600 if (enabled_tc & (1 << i))
3601 num_tc++;
3602 }
3603 return num_tc;
3604 }
3605
3606 /* SFP mode will be enabled for all TCs on port */
3607 return i40e_dcb_get_num_tc(dcbcfg);
3608}
3609
3610/**
3611 * i40e_pf_get_default_tc - Get bitmap for first enabled TC
3612 * @pf: PF being queried
3613 *
3614 * Return a bitmap for first enabled traffic class for this PF.
3615 **/
3616static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
3617{
3618 u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3619 u8 i = 0;
3620
3621 if (!enabled_tc)
3622 return 0x1; /* TC0 */
3623
3624 /* Find the first enabled TC */
3625 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3626 if (enabled_tc & (1 << i))
3627 break;
3628 }
3629
3630 return 1 << i;
3631}
3632
3633/**
3634 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
3635 * @pf: PF being queried
3636 *
3637 * Return a bitmap for enabled traffic classes for this PF.
3638 **/
3639static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
3640{
3641 /* If DCB is not enabled for this PF then just return default TC */
3642 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3643 return i40e_pf_get_default_tc(pf);
3644
3645 /* MFP mode will have enabled TCs set by FW */
3646 if (pf->flags & I40E_FLAG_MFP_ENABLED)
3647 return pf->hw.func_caps.enabled_tcmap;
3648
3649 /* SFP mode we want PF to be enabled for all TCs */
3650 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
3651}
3652
3653/**
3654 * i40e_vsi_get_bw_info - Query VSI BW Information
3655 * @vsi: the VSI being queried
3656 *
3657 * Returns 0 on success, negative value on failure
3658 **/
3659static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
3660{
3661 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
3662 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
3663 struct i40e_pf *pf = vsi->back;
3664 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003665 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003666 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003667 int i;
3668
3669 /* Get the VSI level BW configuration */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003670 aq_ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
3671 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003672 dev_info(&pf->pdev->dev,
3673 "couldn't get pf vsi bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003674 aq_ret, pf->hw.aq.asq_last_status);
3675 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003676 }
3677
3678 /* Get the VSI level BW configuration per TC */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003679 aq_ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
Jesse Brandeburg6838b532014-01-14 00:49:52 -08003680 NULL);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003681 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003682 dev_info(&pf->pdev->dev,
3683 "couldn't get pf vsi ets bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003684 aq_ret, pf->hw.aq.asq_last_status);
3685 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003686 }
3687
3688 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
3689 dev_info(&pf->pdev->dev,
3690 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
3691 bw_config.tc_valid_bits,
3692 bw_ets_config.tc_valid_bits);
3693 /* Still continuing */
3694 }
3695
3696 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
3697 vsi->bw_max_quanta = bw_config.max_bw;
3698 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
3699 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
3700 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3701 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
3702 vsi->bw_ets_limit_credits[i] =
3703 le16_to_cpu(bw_ets_config.credits[i]);
3704 /* 3 bits out of 4 for each TC */
3705 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
3706 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003707
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003708 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003709}
3710
3711/**
3712 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
3713 * @vsi: the VSI being configured
3714 * @enabled_tc: TC bitmap
3715 * @bw_credits: BW shared credits per TC
3716 *
3717 * Returns 0 on success, negative value on failure
3718 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003719static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003720 u8 *bw_share)
3721{
3722 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003723 i40e_status aq_ret;
3724 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003725
3726 bw_data.tc_valid_bits = enabled_tc;
3727 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3728 bw_data.tc_bw_credits[i] = bw_share[i];
3729
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003730 aq_ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
3731 NULL);
3732 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003733 dev_info(&vsi->back->pdev->dev,
3734 "%s: AQ command Config VSI BW allocation per TC failed = %d\n",
3735 __func__, vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003736 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003737 }
3738
3739 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3740 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
3741
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003742 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003743}
3744
3745/**
3746 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
3747 * @vsi: the VSI being configured
3748 * @enabled_tc: TC map to be enabled
3749 *
3750 **/
3751static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3752{
3753 struct net_device *netdev = vsi->netdev;
3754 struct i40e_pf *pf = vsi->back;
3755 struct i40e_hw *hw = &pf->hw;
3756 u8 netdev_tc = 0;
3757 int i;
3758 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3759
3760 if (!netdev)
3761 return;
3762
3763 if (!enabled_tc) {
3764 netdev_reset_tc(netdev);
3765 return;
3766 }
3767
3768 /* Set up actual enabled TCs on the VSI */
3769 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
3770 return;
3771
3772 /* set per TC queues for the VSI */
3773 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3774 /* Only set TC queues for enabled tcs
3775 *
3776 * e.g. For a VSI that has TC0 and TC3 enabled the
3777 * enabled_tc bitmap would be 0x00001001; the driver
3778 * will set the numtc for netdev as 2 that will be
3779 * referenced by the netdev layer as TC 0 and 1.
3780 */
3781 if (vsi->tc_config.enabled_tc & (1 << i))
3782 netdev_set_tc_queue(netdev,
3783 vsi->tc_config.tc_info[i].netdev_tc,
3784 vsi->tc_config.tc_info[i].qcount,
3785 vsi->tc_config.tc_info[i].qoffset);
3786 }
3787
3788 /* Assign UP2TC map for the VSI */
3789 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3790 /* Get the actual TC# for the UP */
3791 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
3792 /* Get the mapped netdev TC# for the UP */
3793 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
3794 netdev_set_prio_tc_map(netdev, i, netdev_tc);
3795 }
3796}
3797
3798/**
3799 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
3800 * @vsi: the VSI being configured
3801 * @ctxt: the ctxt buffer returned from AQ VSI update param command
3802 **/
3803static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
3804 struct i40e_vsi_context *ctxt)
3805{
3806 /* copy just the sections touched not the entire info
3807 * since not all sections are valid as returned by
3808 * update vsi params
3809 */
3810 vsi->info.mapping_flags = ctxt->info.mapping_flags;
3811 memcpy(&vsi->info.queue_mapping,
3812 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
3813 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
3814 sizeof(vsi->info.tc_mapping));
3815}
3816
3817/**
3818 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
3819 * @vsi: VSI to be configured
3820 * @enabled_tc: TC bitmap
3821 *
3822 * This configures a particular VSI for TCs that are mapped to the
3823 * given TC bitmap. It uses default bandwidth share for TCs across
3824 * VSIs to configure TC for a particular VSI.
3825 *
3826 * NOTE:
3827 * It is expected that the VSI queues have been quisced before calling
3828 * this function.
3829 **/
3830static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3831{
3832 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
3833 struct i40e_vsi_context ctxt;
3834 int ret = 0;
3835 int i;
3836
3837 /* Check if enabled_tc is same as existing or new TCs */
3838 if (vsi->tc_config.enabled_tc == enabled_tc)
3839 return ret;
3840
3841 /* Enable ETS TCs with equal BW Share for now across all VSIs */
3842 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3843 if (enabled_tc & (1 << i))
3844 bw_share[i] = 1;
3845 }
3846
3847 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
3848 if (ret) {
3849 dev_info(&vsi->back->pdev->dev,
3850 "Failed configuring TC map %d for VSI %d\n",
3851 enabled_tc, vsi->seid);
3852 goto out;
3853 }
3854
3855 /* Update Queue Pairs Mapping for currently enabled UPs */
3856 ctxt.seid = vsi->seid;
3857 ctxt.pf_num = vsi->back->hw.pf_id;
3858 ctxt.vf_num = 0;
3859 ctxt.uplink_seid = vsi->uplink_seid;
3860 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
3861 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
3862
3863 /* Update the VSI after updating the VSI queue-mapping information */
3864 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3865 if (ret) {
3866 dev_info(&vsi->back->pdev->dev,
3867 "update vsi failed, aq_err=%d\n",
3868 vsi->back->hw.aq.asq_last_status);
3869 goto out;
3870 }
3871 /* update the local VSI info with updated queue map */
3872 i40e_vsi_update_queue_map(vsi, &ctxt);
3873 vsi->info.valid_sections = 0;
3874
3875 /* Update current VSI BW information */
3876 ret = i40e_vsi_get_bw_info(vsi);
3877 if (ret) {
3878 dev_info(&vsi->back->pdev->dev,
3879 "Failed updating vsi bw info, aq_err=%d\n",
3880 vsi->back->hw.aq.asq_last_status);
3881 goto out;
3882 }
3883
3884 /* Update the netdev TC setup */
3885 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
3886out:
3887 return ret;
3888}
3889
3890/**
3891 * i40e_up_complete - Finish the last steps of bringing up a connection
3892 * @vsi: the VSI being configured
3893 **/
3894static int i40e_up_complete(struct i40e_vsi *vsi)
3895{
3896 struct i40e_pf *pf = vsi->back;
3897 int err;
3898
3899 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3900 i40e_vsi_configure_msix(vsi);
3901 else
3902 i40e_configure_msi_and_legacy(vsi);
3903
3904 /* start rings */
3905 err = i40e_vsi_control_rings(vsi, true);
3906 if (err)
3907 return err;
3908
3909 clear_bit(__I40E_DOWN, &vsi->state);
3910 i40e_napi_enable_all(vsi);
3911 i40e_vsi_enable_irq(vsi);
3912
3913 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
3914 (vsi->netdev)) {
Anjali Singhai6d779b42013-09-28 06:00:02 +00003915 netdev_info(vsi->netdev, "NIC Link is Up\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003916 netif_tx_start_all_queues(vsi->netdev);
3917 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00003918 } else if (vsi->netdev) {
3919 netdev_info(vsi->netdev, "NIC Link is Down\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003920 }
3921 i40e_service_event_schedule(pf);
3922
3923 return 0;
3924}
3925
3926/**
3927 * i40e_vsi_reinit_locked - Reset the VSI
3928 * @vsi: the VSI being configured
3929 *
3930 * Rebuild the ring structs after some configuration
3931 * has changed, e.g. MTU size.
3932 **/
3933static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
3934{
3935 struct i40e_pf *pf = vsi->back;
3936
3937 WARN_ON(in_interrupt());
3938 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
3939 usleep_range(1000, 2000);
3940 i40e_down(vsi);
3941
3942 /* Give a VF some time to respond to the reset. The
3943 * two second wait is based upon the watchdog cycle in
3944 * the VF driver.
3945 */
3946 if (vsi->type == I40E_VSI_SRIOV)
3947 msleep(2000);
3948 i40e_up(vsi);
3949 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
3950}
3951
3952/**
3953 * i40e_up - Bring the connection back up after being down
3954 * @vsi: the VSI being configured
3955 **/
3956int i40e_up(struct i40e_vsi *vsi)
3957{
3958 int err;
3959
3960 err = i40e_vsi_configure(vsi);
3961 if (!err)
3962 err = i40e_up_complete(vsi);
3963
3964 return err;
3965}
3966
3967/**
3968 * i40e_down - Shutdown the connection processing
3969 * @vsi: the VSI being stopped
3970 **/
3971void i40e_down(struct i40e_vsi *vsi)
3972{
3973 int i;
3974
3975 /* It is assumed that the caller of this function
3976 * sets the vsi->state __I40E_DOWN bit.
3977 */
3978 if (vsi->netdev) {
3979 netif_carrier_off(vsi->netdev);
3980 netif_tx_disable(vsi->netdev);
3981 }
3982 i40e_vsi_disable_irq(vsi);
3983 i40e_vsi_control_rings(vsi, false);
3984 i40e_napi_disable_all(vsi);
3985
3986 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00003987 i40e_clean_tx_ring(vsi->tx_rings[i]);
3988 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003989 }
3990}
3991
3992/**
3993 * i40e_setup_tc - configure multiple traffic classes
3994 * @netdev: net device to configure
3995 * @tc: number of traffic classes to enable
3996 **/
3997static int i40e_setup_tc(struct net_device *netdev, u8 tc)
3998{
3999 struct i40e_netdev_priv *np = netdev_priv(netdev);
4000 struct i40e_vsi *vsi = np->vsi;
4001 struct i40e_pf *pf = vsi->back;
4002 u8 enabled_tc = 0;
4003 int ret = -EINVAL;
4004 int i;
4005
4006 /* Check if DCB enabled to continue */
4007 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
4008 netdev_info(netdev, "DCB is not enabled for adapter\n");
4009 goto exit;
4010 }
4011
4012 /* Check if MFP enabled */
4013 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
4014 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
4015 goto exit;
4016 }
4017
4018 /* Check whether tc count is within enabled limit */
4019 if (tc > i40e_pf_get_num_tc(pf)) {
4020 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
4021 goto exit;
4022 }
4023
4024 /* Generate TC map for number of tc requested */
4025 for (i = 0; i < tc; i++)
4026 enabled_tc |= (1 << i);
4027
4028 /* Requesting same TC configuration as already enabled */
4029 if (enabled_tc == vsi->tc_config.enabled_tc)
4030 return 0;
4031
4032 /* Quiesce VSI queues */
4033 i40e_quiesce_vsi(vsi);
4034
4035 /* Configure VSI for enabled TCs */
4036 ret = i40e_vsi_config_tc(vsi, enabled_tc);
4037 if (ret) {
4038 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
4039 vsi->seid);
4040 goto exit;
4041 }
4042
4043 /* Unquiesce VSI */
4044 i40e_unquiesce_vsi(vsi);
4045
4046exit:
4047 return ret;
4048}
4049
4050/**
4051 * i40e_open - Called when a network interface is made active
4052 * @netdev: network interface device structure
4053 *
4054 * The open entry point is called when a network interface is made
4055 * active by the system (IFF_UP). At this point all resources needed
4056 * for transmit and receive operations are allocated, the interrupt
4057 * handler is registered with the OS, the netdev watchdog subtask is
4058 * enabled, and the stack is notified that the interface is ready.
4059 *
4060 * Returns 0 on success, negative value on failure
4061 **/
4062static int i40e_open(struct net_device *netdev)
4063{
4064 struct i40e_netdev_priv *np = netdev_priv(netdev);
4065 struct i40e_vsi *vsi = np->vsi;
4066 struct i40e_pf *pf = vsi->back;
4067 char int_name[IFNAMSIZ];
4068 int err;
4069
4070 /* disallow open during test */
4071 if (test_bit(__I40E_TESTING, &pf->state))
4072 return -EBUSY;
4073
4074 netif_carrier_off(netdev);
4075
4076 /* allocate descriptors */
4077 err = i40e_vsi_setup_tx_resources(vsi);
4078 if (err)
4079 goto err_setup_tx;
4080 err = i40e_vsi_setup_rx_resources(vsi);
4081 if (err)
4082 goto err_setup_rx;
4083
4084 err = i40e_vsi_configure(vsi);
4085 if (err)
4086 goto err_setup_rx;
4087
4088 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
4089 dev_driver_string(&pf->pdev->dev), netdev->name);
4090 err = i40e_vsi_request_irq(vsi, int_name);
4091 if (err)
4092 goto err_setup_rx;
4093
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004094 /* Notify the stack of the actual queue counts. */
Neerav Parikhd7397642013-11-28 06:39:37 +00004095 err = netif_set_real_num_tx_queues(netdev, vsi->num_queue_pairs);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004096 if (err)
4097 goto err_set_queues;
4098
Neerav Parikhd7397642013-11-28 06:39:37 +00004099 err = netif_set_real_num_rx_queues(netdev, vsi->num_queue_pairs);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004100 if (err)
4101 goto err_set_queues;
4102
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004103 err = i40e_up_complete(vsi);
4104 if (err)
4105 goto err_up_complete;
4106
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00004107#ifdef CONFIG_I40E_VXLAN
4108 vxlan_get_rx_port(netdev);
4109#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004110
4111 return 0;
4112
4113err_up_complete:
4114 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004115err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004116 i40e_vsi_free_irq(vsi);
4117err_setup_rx:
4118 i40e_vsi_free_rx_resources(vsi);
4119err_setup_tx:
4120 i40e_vsi_free_tx_resources(vsi);
4121 if (vsi == pf->vsi[pf->lan_vsi])
4122 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
4123
4124 return err;
4125}
4126
4127/**
4128 * i40e_close - Disables a network interface
4129 * @netdev: network interface device structure
4130 *
4131 * The close entry point is called when an interface is de-activated
4132 * by the OS. The hardware is still under the driver's control, but
4133 * this netdev interface is disabled.
4134 *
4135 * Returns 0, this is not allowed to fail
4136 **/
4137static int i40e_close(struct net_device *netdev)
4138{
4139 struct i40e_netdev_priv *np = netdev_priv(netdev);
4140 struct i40e_vsi *vsi = np->vsi;
4141
4142 if (test_and_set_bit(__I40E_DOWN, &vsi->state))
4143 return 0;
4144
4145 i40e_down(vsi);
4146 i40e_vsi_free_irq(vsi);
4147
4148 i40e_vsi_free_tx_resources(vsi);
4149 i40e_vsi_free_rx_resources(vsi);
4150
4151 return 0;
4152}
4153
4154/**
4155 * i40e_do_reset - Start a PF or Core Reset sequence
4156 * @pf: board private structure
4157 * @reset_flags: which reset is requested
4158 *
4159 * The essential difference in resets is that the PF Reset
4160 * doesn't clear the packet buffers, doesn't reset the PE
4161 * firmware, and doesn't bother the other PFs on the chip.
4162 **/
4163void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
4164{
4165 u32 val;
4166
4167 WARN_ON(in_interrupt());
4168
4169 /* do the biggest reset indicated */
4170 if (reset_flags & (1 << __I40E_GLOBAL_RESET_REQUESTED)) {
4171
4172 /* Request a Global Reset
4173 *
4174 * This will start the chip's countdown to the actual full
4175 * chip reset event, and a warning interrupt to be sent
4176 * to all PFs, including the requestor. Our handler
4177 * for the warning interrupt will deal with the shutdown
4178 * and recovery of the switch setup.
4179 */
4180 dev_info(&pf->pdev->dev, "GlobalR requested\n");
4181 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4182 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
4183 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4184
4185 } else if (reset_flags & (1 << __I40E_CORE_RESET_REQUESTED)) {
4186
4187 /* Request a Core Reset
4188 *
4189 * Same as Global Reset, except does *not* include the MAC/PHY
4190 */
4191 dev_info(&pf->pdev->dev, "CoreR requested\n");
4192 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4193 val |= I40E_GLGEN_RTRIG_CORER_MASK;
4194 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4195 i40e_flush(&pf->hw);
4196
Shannon Nelson7823fe32013-11-16 10:00:45 +00004197 } else if (reset_flags & (1 << __I40E_EMP_RESET_REQUESTED)) {
4198
4199 /* Request a Firmware Reset
4200 *
4201 * Same as Global reset, plus restarting the
4202 * embedded firmware engine.
4203 */
4204 /* enable EMP Reset */
4205 val = rd32(&pf->hw, I40E_GLGEN_RSTENA_EMP);
4206 val |= I40E_GLGEN_RSTENA_EMP_EMP_RST_ENA_MASK;
4207 wr32(&pf->hw, I40E_GLGEN_RSTENA_EMP, val);
4208
4209 /* force the reset */
4210 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4211 val |= I40E_GLGEN_RTRIG_EMPFWR_MASK;
4212 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4213 i40e_flush(&pf->hw);
4214
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004215 } else if (reset_flags & (1 << __I40E_PF_RESET_REQUESTED)) {
4216
4217 /* Request a PF Reset
4218 *
4219 * Resets only the PF-specific registers
4220 *
4221 * This goes directly to the tear-down and rebuild of
4222 * the switch, since we need to do all the recovery as
4223 * for the Core Reset.
4224 */
4225 dev_info(&pf->pdev->dev, "PFR requested\n");
4226 i40e_handle_reset_warning(pf);
4227
4228 } else if (reset_flags & (1 << __I40E_REINIT_REQUESTED)) {
4229 int v;
4230
4231 /* Find the VSI(s) that requested a re-init */
4232 dev_info(&pf->pdev->dev,
4233 "VSI reinit requested\n");
4234 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4235 struct i40e_vsi *vsi = pf->vsi[v];
4236 if (vsi != NULL &&
4237 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
4238 i40e_vsi_reinit_locked(pf->vsi[v]);
4239 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
4240 }
4241 }
4242
4243 /* no further action needed, so return now */
4244 return;
4245 } else {
4246 dev_info(&pf->pdev->dev,
4247 "bad reset request 0x%08x\n", reset_flags);
4248 return;
4249 }
4250}
4251
4252/**
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004253 * i40e_do_reset_safe - Protected reset path for userland calls.
4254 * @pf: board private structure
4255 * @reset_flags: which reset is requested
4256 *
4257 **/
4258void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
4259{
4260 rtnl_lock();
4261 i40e_do_reset(pf, reset_flags);
4262 rtnl_unlock();
4263}
4264
4265/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004266 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
4267 * @pf: board private structure
4268 * @e: event info posted on ARQ
4269 *
4270 * Handler for LAN Queue Overflow Event generated by the firmware for PF
4271 * and VF queues
4272 **/
4273static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
4274 struct i40e_arq_event_info *e)
4275{
4276 struct i40e_aqc_lan_overflow *data =
4277 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
4278 u32 queue = le32_to_cpu(data->prtdcb_rupto);
4279 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
4280 struct i40e_hw *hw = &pf->hw;
4281 struct i40e_vf *vf;
4282 u16 vf_id;
4283
4284 dev_info(&pf->pdev->dev, "%s: Rx Queue Number = %d QTX_CTL=0x%08x\n",
4285 __func__, queue, qtx_ctl);
4286
4287 /* Queue belongs to VF, find the VF and issue VF reset */
4288 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
4289 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
4290 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
4291 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
4292 vf_id -= hw->func_caps.vf_base_id;
4293 vf = &pf->vf[vf_id];
4294 i40e_vc_notify_vf_reset(vf);
4295 /* Allow VF to process pending reset notification */
4296 msleep(20);
4297 i40e_reset_vf(vf, false);
4298 }
4299}
4300
4301/**
4302 * i40e_service_event_complete - Finish up the service event
4303 * @pf: board private structure
4304 **/
4305static void i40e_service_event_complete(struct i40e_pf *pf)
4306{
4307 BUG_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
4308
4309 /* flush memory to make sure state is correct before next watchog */
4310 smp_mb__before_clear_bit();
4311 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
4312}
4313
4314/**
4315 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
4316 * @pf: board private structure
4317 **/
4318static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
4319{
4320 if (!(pf->flags & I40E_FLAG_FDIR_REQUIRES_REINIT))
4321 return;
4322
4323 pf->flags &= ~I40E_FLAG_FDIR_REQUIRES_REINIT;
4324
4325 /* if interface is down do nothing */
4326 if (test_bit(__I40E_DOWN, &pf->state))
4327 return;
4328}
4329
4330/**
4331 * i40e_vsi_link_event - notify VSI of a link event
4332 * @vsi: vsi to be notified
4333 * @link_up: link up or down
4334 **/
4335static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
4336{
4337 if (!vsi)
4338 return;
4339
4340 switch (vsi->type) {
4341 case I40E_VSI_MAIN:
4342 if (!vsi->netdev || !vsi->netdev_registered)
4343 break;
4344
4345 if (link_up) {
4346 netif_carrier_on(vsi->netdev);
4347 netif_tx_wake_all_queues(vsi->netdev);
4348 } else {
4349 netif_carrier_off(vsi->netdev);
4350 netif_tx_stop_all_queues(vsi->netdev);
4351 }
4352 break;
4353
4354 case I40E_VSI_SRIOV:
4355 break;
4356
4357 case I40E_VSI_VMDQ2:
4358 case I40E_VSI_CTRL:
4359 case I40E_VSI_MIRROR:
4360 default:
4361 /* there is no notification for other VSIs */
4362 break;
4363 }
4364}
4365
4366/**
4367 * i40e_veb_link_event - notify elements on the veb of a link event
4368 * @veb: veb to be notified
4369 * @link_up: link up or down
4370 **/
4371static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
4372{
4373 struct i40e_pf *pf;
4374 int i;
4375
4376 if (!veb || !veb->pf)
4377 return;
4378 pf = veb->pf;
4379
4380 /* depth first... */
4381 for (i = 0; i < I40E_MAX_VEB; i++)
4382 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
4383 i40e_veb_link_event(pf->veb[i], link_up);
4384
4385 /* ... now the local VSIs */
4386 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
4387 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
4388 i40e_vsi_link_event(pf->vsi[i], link_up);
4389}
4390
4391/**
4392 * i40e_link_event - Update netif_carrier status
4393 * @pf: board private structure
4394 **/
4395static void i40e_link_event(struct i40e_pf *pf)
4396{
4397 bool new_link, old_link;
4398
4399 new_link = (pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP);
4400 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
4401
4402 if (new_link == old_link)
4403 return;
4404
Anjali Singhai6d779b42013-09-28 06:00:02 +00004405 if (!test_bit(__I40E_DOWN, &pf->vsi[pf->lan_vsi]->state))
4406 netdev_info(pf->vsi[pf->lan_vsi]->netdev,
4407 "NIC Link is %s\n", (new_link ? "Up" : "Down"));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004408
4409 /* Notify the base of the switch tree connected to
4410 * the link. Floating VEBs are not notified.
4411 */
4412 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
4413 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
4414 else
4415 i40e_vsi_link_event(pf->vsi[pf->lan_vsi], new_link);
4416
4417 if (pf->vf)
4418 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00004419
4420 if (pf->flags & I40E_FLAG_PTP)
4421 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004422}
4423
4424/**
4425 * i40e_check_hang_subtask - Check for hung queues and dropped interrupts
4426 * @pf: board private structure
4427 *
4428 * Set the per-queue flags to request a check for stuck queues in the irq
4429 * clean functions, then force interrupts to be sure the irq clean is called.
4430 **/
4431static void i40e_check_hang_subtask(struct i40e_pf *pf)
4432{
4433 int i, v;
4434
4435 /* If we're down or resetting, just bail */
4436 if (test_bit(__I40E_CONFIG_BUSY, &pf->state))
4437 return;
4438
4439 /* for each VSI/netdev
4440 * for each Tx queue
4441 * set the check flag
4442 * for each q_vector
4443 * force an interrupt
4444 */
4445 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4446 struct i40e_vsi *vsi = pf->vsi[v];
4447 int armed = 0;
4448
4449 if (!pf->vsi[v] ||
4450 test_bit(__I40E_DOWN, &vsi->state) ||
4451 (vsi->netdev && !netif_carrier_ok(vsi->netdev)))
4452 continue;
4453
4454 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004455 set_check_for_tx_hang(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004456 if (test_bit(__I40E_HANG_CHECK_ARMED,
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004457 &vsi->tx_rings[i]->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004458 armed++;
4459 }
4460
4461 if (armed) {
4462 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
4463 wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0,
4464 (I40E_PFINT_DYN_CTL0_INTENA_MASK |
4465 I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK));
4466 } else {
4467 u16 vec = vsi->base_vector - 1;
4468 u32 val = (I40E_PFINT_DYN_CTLN_INTENA_MASK |
4469 I40E_PFINT_DYN_CTLN_SWINT_TRIG_MASK);
4470 for (i = 0; i < vsi->num_q_vectors; i++, vec++)
4471 wr32(&vsi->back->hw,
4472 I40E_PFINT_DYN_CTLN(vec), val);
4473 }
4474 i40e_flush(&vsi->back->hw);
4475 }
4476 }
4477}
4478
4479/**
4480 * i40e_watchdog_subtask - Check and bring link up
4481 * @pf: board private structure
4482 **/
4483static void i40e_watchdog_subtask(struct i40e_pf *pf)
4484{
4485 int i;
4486
4487 /* if interface is down do nothing */
4488 if (test_bit(__I40E_DOWN, &pf->state) ||
4489 test_bit(__I40E_CONFIG_BUSY, &pf->state))
4490 return;
4491
4492 /* Update the stats for active netdevs so the network stack
4493 * can look at updated numbers whenever it cares to
4494 */
4495 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
4496 if (pf->vsi[i] && pf->vsi[i]->netdev)
4497 i40e_update_stats(pf->vsi[i]);
4498
4499 /* Update the stats for the active switching components */
4500 for (i = 0; i < I40E_MAX_VEB; i++)
4501 if (pf->veb[i])
4502 i40e_update_veb_stats(pf->veb[i]);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00004503
4504 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004505}
4506
4507/**
4508 * i40e_reset_subtask - Set up for resetting the device and driver
4509 * @pf: board private structure
4510 **/
4511static void i40e_reset_subtask(struct i40e_pf *pf)
4512{
4513 u32 reset_flags = 0;
4514
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004515 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004516 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
4517 reset_flags |= (1 << __I40E_REINIT_REQUESTED);
4518 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
4519 }
4520 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
4521 reset_flags |= (1 << __I40E_PF_RESET_REQUESTED);
4522 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
4523 }
4524 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
4525 reset_flags |= (1 << __I40E_CORE_RESET_REQUESTED);
4526 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
4527 }
4528 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
4529 reset_flags |= (1 << __I40E_GLOBAL_RESET_REQUESTED);
4530 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
4531 }
4532
4533 /* If there's a recovery already waiting, it takes
4534 * precedence before starting a new reset sequence.
4535 */
4536 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
4537 i40e_handle_reset_warning(pf);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004538 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004539 }
4540
4541 /* If we're already down or resetting, just bail */
4542 if (reset_flags &&
4543 !test_bit(__I40E_DOWN, &pf->state) &&
4544 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
4545 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004546
4547unlock:
4548 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004549}
4550
4551/**
4552 * i40e_handle_link_event - Handle link event
4553 * @pf: board private structure
4554 * @e: event info posted on ARQ
4555 **/
4556static void i40e_handle_link_event(struct i40e_pf *pf,
4557 struct i40e_arq_event_info *e)
4558{
4559 struct i40e_hw *hw = &pf->hw;
4560 struct i40e_aqc_get_link_status *status =
4561 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
4562 struct i40e_link_status *hw_link_info = &hw->phy.link_info;
4563
4564 /* save off old link status information */
4565 memcpy(&pf->hw.phy.link_info_old, hw_link_info,
4566 sizeof(pf->hw.phy.link_info_old));
4567
4568 /* update link status */
4569 hw_link_info->phy_type = (enum i40e_aq_phy_type)status->phy_type;
4570 hw_link_info->link_speed = (enum i40e_aq_link_speed)status->link_speed;
4571 hw_link_info->link_info = status->link_info;
4572 hw_link_info->an_info = status->an_info;
4573 hw_link_info->ext_info = status->ext_info;
4574 hw_link_info->lse_enable =
4575 le16_to_cpu(status->command_flags) &
4576 I40E_AQ_LSE_ENABLE;
4577
4578 /* process the event */
4579 i40e_link_event(pf);
4580
4581 /* Do a new status request to re-enable LSE reporting
4582 * and load new status information into the hw struct,
4583 * then see if the status changed while processing the
4584 * initial event.
4585 */
4586 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
4587 i40e_link_event(pf);
4588}
4589
4590/**
4591 * i40e_clean_adminq_subtask - Clean the AdminQ rings
4592 * @pf: board private structure
4593 **/
4594static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
4595{
4596 struct i40e_arq_event_info event;
4597 struct i40e_hw *hw = &pf->hw;
4598 u16 pending, i = 0;
4599 i40e_status ret;
4600 u16 opcode;
4601 u32 val;
4602
4603 if (!test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state))
4604 return;
4605
Mitch Williams3197ce22013-11-28 06:39:39 +00004606 event.msg_size = I40E_MAX_AQ_BUF_SIZE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004607 event.msg_buf = kzalloc(event.msg_size, GFP_KERNEL);
4608 if (!event.msg_buf)
4609 return;
4610
4611 do {
Mitch Williams2f019122013-11-28 06:39:33 +00004612 event.msg_size = I40E_MAX_AQ_BUF_SIZE; /* reinit each time */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004613 ret = i40e_clean_arq_element(hw, &event, &pending);
4614 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
4615 dev_info(&pf->pdev->dev, "No ARQ event found\n");
4616 break;
4617 } else if (ret) {
4618 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
4619 break;
4620 }
4621
4622 opcode = le16_to_cpu(event.desc.opcode);
4623 switch (opcode) {
4624
4625 case i40e_aqc_opc_get_link_status:
4626 i40e_handle_link_event(pf, &event);
4627 break;
4628 case i40e_aqc_opc_send_msg_to_pf:
4629 ret = i40e_vc_process_vf_msg(pf,
4630 le16_to_cpu(event.desc.retval),
4631 le32_to_cpu(event.desc.cookie_high),
4632 le32_to_cpu(event.desc.cookie_low),
4633 event.msg_buf,
4634 event.msg_size);
4635 break;
4636 case i40e_aqc_opc_lldp_update_mib:
4637 dev_info(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
4638 break;
4639 case i40e_aqc_opc_event_lan_overflow:
4640 dev_info(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
4641 i40e_handle_lan_overflow_event(pf, &event);
4642 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00004643 case i40e_aqc_opc_send_msg_to_peer:
4644 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
4645 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004646 default:
4647 dev_info(&pf->pdev->dev,
Shannon Nelson0467bc92013-12-18 13:45:58 +00004648 "ARQ Error: Unknown event 0x%04x received\n",
4649 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004650 break;
4651 }
4652 } while (pending && (i++ < pf->adminq_work_limit));
4653
4654 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
4655 /* re-enable Admin queue interrupt cause */
4656 val = rd32(hw, I40E_PFINT_ICR0_ENA);
4657 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4658 wr32(hw, I40E_PFINT_ICR0_ENA, val);
4659 i40e_flush(hw);
4660
4661 kfree(event.msg_buf);
4662}
4663
4664/**
4665 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
4666 * @veb: pointer to the VEB instance
4667 *
4668 * This is a recursive function that first builds the attached VSIs then
4669 * recurses in to build the next layer of VEB. We track the connections
4670 * through our own index numbers because the seid's from the HW could
4671 * change across the reset.
4672 **/
4673static int i40e_reconstitute_veb(struct i40e_veb *veb)
4674{
4675 struct i40e_vsi *ctl_vsi = NULL;
4676 struct i40e_pf *pf = veb->pf;
4677 int v, veb_idx;
4678 int ret;
4679
4680 /* build VSI that owns this VEB, temporarily attached to base VEB */
4681 for (v = 0; v < pf->hw.func_caps.num_vsis && !ctl_vsi; v++) {
4682 if (pf->vsi[v] &&
4683 pf->vsi[v]->veb_idx == veb->idx &&
4684 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
4685 ctl_vsi = pf->vsi[v];
4686 break;
4687 }
4688 }
4689 if (!ctl_vsi) {
4690 dev_info(&pf->pdev->dev,
4691 "missing owner VSI for veb_idx %d\n", veb->idx);
4692 ret = -ENOENT;
4693 goto end_reconstitute;
4694 }
4695 if (ctl_vsi != pf->vsi[pf->lan_vsi])
4696 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
4697 ret = i40e_add_vsi(ctl_vsi);
4698 if (ret) {
4699 dev_info(&pf->pdev->dev,
4700 "rebuild of owner VSI failed: %d\n", ret);
4701 goto end_reconstitute;
4702 }
4703 i40e_vsi_reset_stats(ctl_vsi);
4704
4705 /* create the VEB in the switch and move the VSI onto the VEB */
4706 ret = i40e_add_veb(veb, ctl_vsi);
4707 if (ret)
4708 goto end_reconstitute;
4709
4710 /* create the remaining VSIs attached to this VEB */
4711 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4712 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
4713 continue;
4714
4715 if (pf->vsi[v]->veb_idx == veb->idx) {
4716 struct i40e_vsi *vsi = pf->vsi[v];
4717 vsi->uplink_seid = veb->seid;
4718 ret = i40e_add_vsi(vsi);
4719 if (ret) {
4720 dev_info(&pf->pdev->dev,
4721 "rebuild of vsi_idx %d failed: %d\n",
4722 v, ret);
4723 goto end_reconstitute;
4724 }
4725 i40e_vsi_reset_stats(vsi);
4726 }
4727 }
4728
4729 /* create any VEBs attached to this VEB - RECURSION */
4730 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
4731 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
4732 pf->veb[veb_idx]->uplink_seid = veb->seid;
4733 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
4734 if (ret)
4735 break;
4736 }
4737 }
4738
4739end_reconstitute:
4740 return ret;
4741}
4742
4743/**
4744 * i40e_get_capabilities - get info about the HW
4745 * @pf: the PF struct
4746 **/
4747static int i40e_get_capabilities(struct i40e_pf *pf)
4748{
4749 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
4750 u16 data_size;
4751 int buf_len;
4752 int err;
4753
4754 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
4755 do {
4756 cap_buf = kzalloc(buf_len, GFP_KERNEL);
4757 if (!cap_buf)
4758 return -ENOMEM;
4759
4760 /* this loads the data into the hw struct for us */
4761 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
4762 &data_size,
4763 i40e_aqc_opc_list_func_capabilities,
4764 NULL);
4765 /* data loaded, buffer no longer needed */
4766 kfree(cap_buf);
4767
4768 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
4769 /* retry with a larger buffer */
4770 buf_len = data_size;
4771 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
4772 dev_info(&pf->pdev->dev,
4773 "capability discovery failed: aq=%d\n",
4774 pf->hw.aq.asq_last_status);
4775 return -ENODEV;
4776 }
4777 } while (err);
4778
Jesse Brandeburgd0b10242014-01-17 15:36:32 -08004779 /* increment MSI-X count because current FW skips one */
4780 pf->hw.func_caps.num_msix_vectors++;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004781
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004782 if (pf->hw.debug_mask & I40E_DEBUG_USER)
4783 dev_info(&pf->pdev->dev,
4784 "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",
4785 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
4786 pf->hw.func_caps.num_msix_vectors,
4787 pf->hw.func_caps.num_msix_vectors_vf,
4788 pf->hw.func_caps.fd_filters_guaranteed,
4789 pf->hw.func_caps.fd_filters_best_effort,
4790 pf->hw.func_caps.num_tx_qp,
4791 pf->hw.func_caps.num_vsis);
4792
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004793#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
4794 + pf->hw.func_caps.num_vfs)
4795 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
4796 dev_info(&pf->pdev->dev,
4797 "got num_vsis %d, setting num_vsis to %d\n",
4798 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
4799 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
4800 }
4801
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004802 return 0;
4803}
4804
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08004805static int i40e_vsi_clear(struct i40e_vsi *vsi);
4806
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004807/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08004808 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004809 * @pf: board private structure
4810 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08004811static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004812{
4813 struct i40e_vsi *vsi;
4814 bool new_vsi = false;
4815 int err, i;
4816
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08004817 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004818 return;
4819
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08004820 /* find existing VSI and see if it needs configuring */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004821 vsi = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08004822 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
4823 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004824 vsi = pf->vsi[i];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08004825 break;
4826 }
4827 }
4828
4829 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004830 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08004831 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
4832 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004833 if (!vsi) {
4834 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08004835 goto err_vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004836 }
4837 new_vsi = true;
4838 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08004839 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004840
4841 err = i40e_vsi_setup_tx_resources(vsi);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08004842 if (err)
4843 goto err_setup_tx;
4844 err = i40e_vsi_setup_rx_resources(vsi);
4845 if (err)
4846 goto err_setup_rx;
4847
4848 if (new_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004849 char int_name[IFNAMSIZ + 9];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08004850 err = i40e_vsi_configure(vsi);
4851 if (err)
4852 goto err_setup_rx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004853 snprintf(int_name, sizeof(int_name) - 1, "%s-fdir",
4854 dev_driver_string(&pf->pdev->dev));
4855 err = i40e_vsi_request_irq(vsi, int_name);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08004856 if (err)
4857 goto err_setup_rx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004858 err = i40e_up_complete(vsi);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08004859 if (err)
4860 goto err_up_complete;
4861 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004862
4863 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08004864 return;
4865
4866err_up_complete:
4867 i40e_down(vsi);
4868 i40e_vsi_free_irq(vsi);
4869err_setup_rx:
4870 i40e_vsi_free_rx_resources(vsi);
4871err_setup_tx:
4872 i40e_vsi_free_tx_resources(vsi);
4873err_vsi:
4874 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
4875 i40e_vsi_clear(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004876}
4877
4878/**
4879 * i40e_fdir_teardown - release the Flow Director resources
4880 * @pf: board private structure
4881 **/
4882static void i40e_fdir_teardown(struct i40e_pf *pf)
4883{
4884 int i;
4885
4886 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
4887 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
4888 i40e_vsi_release(pf->vsi[i]);
4889 break;
4890 }
4891 }
4892}
4893
4894/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00004895 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004896 * @pf: board private structure
4897 *
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00004898 * Close up the VFs and other things in prep for pf Reset.
4899 **/
4900static int i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004901{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004902 struct i40e_hw *hw = &pf->hw;
4903 i40e_status ret;
4904 u32 v;
4905
4906 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
4907 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00004908 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004909
4910 dev_info(&pf->pdev->dev, "Tearing down internal switch for reset\n");
4911
Anjali Singhai Jain37f0be62013-11-28 06:39:46 +00004912 if (i40e_check_asq_alive(hw))
4913 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004914
4915 /* quiesce the VSIs and their queues that are not already DOWN */
4916 i40e_pf_quiesce_all_vsi(pf);
4917
4918 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4919 if (pf->vsi[v])
4920 pf->vsi[v]->seid = 0;
4921 }
4922
4923 i40e_shutdown_adminq(&pf->hw);
4924
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00004925 /* call shutdown HMC */
4926 ret = i40e_shutdown_lan_hmc(hw);
4927 if (ret) {
4928 dev_info(&pf->pdev->dev, "shutdown_lan_hmc failed: %d\n", ret);
4929 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
4930 }
4931 return ret;
4932}
4933
4934/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00004935 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00004936 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00004937 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00004938 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00004939static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00004940{
4941 struct i40e_driver_version dv;
4942 struct i40e_hw *hw = &pf->hw;
4943 i40e_status ret;
4944 u32 v;
4945
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004946 /* Now we wait for GRST to settle out.
4947 * We don't have to delete the VEBs or VSIs from the hw switch
4948 * because the reset will make them disappear.
4949 */
4950 ret = i40e_pf_reset(hw);
4951 if (ret)
4952 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
4953 pf->pfr_count++;
4954
4955 if (test_bit(__I40E_DOWN, &pf->state))
4956 goto end_core_reset;
4957 dev_info(&pf->pdev->dev, "Rebuilding internal switch\n");
4958
4959 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
4960 ret = i40e_init_adminq(&pf->hw);
4961 if (ret) {
4962 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, %d\n", ret);
4963 goto end_core_reset;
4964 }
4965
4966 ret = i40e_get_capabilities(pf);
4967 if (ret) {
4968 dev_info(&pf->pdev->dev, "i40e_get_capabilities failed, %d\n",
4969 ret);
4970 goto end_core_reset;
4971 }
4972
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004973 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
4974 hw->func_caps.num_rx_qp,
4975 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
4976 if (ret) {
4977 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
4978 goto end_core_reset;
4979 }
4980 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
4981 if (ret) {
4982 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
4983 goto end_core_reset;
4984 }
4985
4986 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00004987 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004988 if (ret)
4989 goto end_core_reset;
4990
4991 /* Rebuild the VSIs and VEBs that existed before reset.
4992 * They are still in our local switch element arrays, so only
4993 * need to rebuild the switch model in the HW.
4994 *
4995 * If there were VEBs but the reconstitution failed, we'll try
4996 * try to recover minimal use by getting the basic PF VSI working.
4997 */
4998 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
4999 dev_info(&pf->pdev->dev, "attempting to rebuild switch\n");
5000 /* find the one VEB connected to the MAC, and find orphans */
5001 for (v = 0; v < I40E_MAX_VEB; v++) {
5002 if (!pf->veb[v])
5003 continue;
5004
5005 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
5006 pf->veb[v]->uplink_seid == 0) {
5007 ret = i40e_reconstitute_veb(pf->veb[v]);
5008
5009 if (!ret)
5010 continue;
5011
5012 /* If Main VEB failed, we're in deep doodoo,
5013 * so give up rebuilding the switch and set up
5014 * for minimal rebuild of PF VSI.
5015 * If orphan failed, we'll report the error
5016 * but try to keep going.
5017 */
5018 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
5019 dev_info(&pf->pdev->dev,
5020 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
5021 ret);
5022 pf->vsi[pf->lan_vsi]->uplink_seid
5023 = pf->mac_seid;
5024 break;
5025 } else if (pf->veb[v]->uplink_seid == 0) {
5026 dev_info(&pf->pdev->dev,
5027 "rebuild of orphan VEB failed: %d\n",
5028 ret);
5029 }
5030 }
5031 }
5032 }
5033
5034 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
5035 dev_info(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
5036 /* no VEB, so rebuild only the Main VSI */
5037 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
5038 if (ret) {
5039 dev_info(&pf->pdev->dev,
5040 "rebuild of Main VSI failed: %d\n", ret);
5041 goto end_core_reset;
5042 }
5043 }
5044
5045 /* reinit the misc interrupt */
5046 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5047 ret = i40e_setup_misc_vector(pf);
5048
5049 /* restart the VSIs that were rebuilt and running before the reset */
5050 i40e_pf_unquiesce_all_vsi(pf);
5051
5052 /* tell the firmware that we're starting */
5053 dv.major_version = DRV_VERSION_MAJOR;
5054 dv.minor_version = DRV_VERSION_MINOR;
5055 dv.build_version = DRV_VERSION_BUILD;
5056 dv.subbuild_version = 0;
5057 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
5058
5059 dev_info(&pf->pdev->dev, "PF reset done\n");
5060
5061end_core_reset:
5062 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
5063}
5064
5065/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005066 * i40e_handle_reset_warning - prep for the pf to reset, reset and rebuild
5067 * @pf: board private structure
5068 *
5069 * Close up the VFs and other things in prep for a Core Reset,
5070 * then get ready to rebuild the world.
5071 **/
5072static void i40e_handle_reset_warning(struct i40e_pf *pf)
5073{
5074 i40e_status ret;
5075
5076 ret = i40e_prep_for_reset(pf);
5077 if (!ret)
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005078 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005079}
5080
5081/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005082 * i40e_handle_mdd_event
5083 * @pf: pointer to the pf structure
5084 *
5085 * Called from the MDD irq handler to identify possibly malicious vfs
5086 **/
5087static void i40e_handle_mdd_event(struct i40e_pf *pf)
5088{
5089 struct i40e_hw *hw = &pf->hw;
5090 bool mdd_detected = false;
5091 struct i40e_vf *vf;
5092 u32 reg;
5093 int i;
5094
5095 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
5096 return;
5097
5098 /* find what triggered the MDD event */
5099 reg = rd32(hw, I40E_GL_MDET_TX);
5100 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
5101 u8 func = (reg & I40E_GL_MDET_TX_FUNCTION_MASK)
5102 >> I40E_GL_MDET_TX_FUNCTION_SHIFT;
5103 u8 event = (reg & I40E_GL_MDET_TX_EVENT_SHIFT)
5104 >> I40E_GL_MDET_TX_EVENT_SHIFT;
5105 u8 queue = (reg & I40E_GL_MDET_TX_QUEUE_MASK)
5106 >> I40E_GL_MDET_TX_QUEUE_SHIFT;
5107 dev_info(&pf->pdev->dev,
5108 "Malicious Driver Detection TX event 0x%02x on q %d of function 0x%02x\n",
5109 event, queue, func);
5110 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
5111 mdd_detected = true;
5112 }
5113 reg = rd32(hw, I40E_GL_MDET_RX);
5114 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
5115 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK)
5116 >> I40E_GL_MDET_RX_FUNCTION_SHIFT;
5117 u8 event = (reg & I40E_GL_MDET_RX_EVENT_SHIFT)
5118 >> I40E_GL_MDET_RX_EVENT_SHIFT;
5119 u8 queue = (reg & I40E_GL_MDET_RX_QUEUE_MASK)
5120 >> I40E_GL_MDET_RX_QUEUE_SHIFT;
5121 dev_info(&pf->pdev->dev,
5122 "Malicious Driver Detection RX event 0x%02x on q %d of function 0x%02x\n",
5123 event, queue, func);
5124 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
5125 mdd_detected = true;
5126 }
5127
5128 /* see if one of the VFs needs its hand slapped */
5129 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
5130 vf = &(pf->vf[i]);
5131 reg = rd32(hw, I40E_VP_MDET_TX(i));
5132 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
5133 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
5134 vf->num_mdd_events++;
5135 dev_info(&pf->pdev->dev, "MDD TX event on VF %d\n", i);
5136 }
5137
5138 reg = rd32(hw, I40E_VP_MDET_RX(i));
5139 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
5140 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
5141 vf->num_mdd_events++;
5142 dev_info(&pf->pdev->dev, "MDD RX event on VF %d\n", i);
5143 }
5144
5145 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
5146 dev_info(&pf->pdev->dev,
5147 "Too many MDD events on VF %d, disabled\n", i);
5148 dev_info(&pf->pdev->dev,
5149 "Use PF Control I/F to re-enable the VF\n");
5150 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
5151 }
5152 }
5153
5154 /* re-enable mdd interrupt cause */
5155 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
5156 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
5157 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
5158 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
5159 i40e_flush(hw);
5160}
5161
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005162#ifdef CONFIG_I40E_VXLAN
5163/**
5164 * i40e_sync_vxlan_filters_subtask - Sync the VSI filter list with HW
5165 * @pf: board private structure
5166 **/
5167static void i40e_sync_vxlan_filters_subtask(struct i40e_pf *pf)
5168{
5169 const int vxlan_hdr_qwords = 4;
5170 struct i40e_hw *hw = &pf->hw;
5171 i40e_status ret;
5172 u8 filter_index;
5173 __be16 port;
5174 int i;
5175
5176 if (!(pf->flags & I40E_FLAG_VXLAN_FILTER_SYNC))
5177 return;
5178
5179 pf->flags &= ~I40E_FLAG_VXLAN_FILTER_SYNC;
5180
5181 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
5182 if (pf->pending_vxlan_bitmap & (1 << i)) {
5183 pf->pending_vxlan_bitmap &= ~(1 << i);
5184 port = pf->vxlan_ports[i];
5185 ret = port ?
5186 i40e_aq_add_udp_tunnel(hw, ntohs(port),
5187 vxlan_hdr_qwords,
5188 I40E_AQC_TUNNEL_TYPE_VXLAN,
5189 &filter_index, NULL)
5190 : i40e_aq_del_udp_tunnel(hw, i, NULL);
5191
5192 if (ret) {
5193 dev_info(&pf->pdev->dev, "Failed to execute AQ command for %s port %d with index %d\n",
5194 port ? "adding" : "deleting",
5195 ntohs(port), port ? i : i);
5196
5197 pf->vxlan_ports[i] = 0;
5198 } else {
5199 dev_info(&pf->pdev->dev, "%s port %d with AQ command with index %d\n",
5200 port ? "Added" : "Deleted",
5201 ntohs(port), port ? i : filter_index);
5202 }
5203 }
5204 }
5205}
5206
5207#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005208/**
5209 * i40e_service_task - Run the driver's async subtasks
5210 * @work: pointer to work_struct containing our data
5211 **/
5212static void i40e_service_task(struct work_struct *work)
5213{
5214 struct i40e_pf *pf = container_of(work,
5215 struct i40e_pf,
5216 service_task);
5217 unsigned long start_time = jiffies;
5218
5219 i40e_reset_subtask(pf);
5220 i40e_handle_mdd_event(pf);
5221 i40e_vc_process_vflr_event(pf);
5222 i40e_watchdog_subtask(pf);
5223 i40e_fdir_reinit_subtask(pf);
5224 i40e_check_hang_subtask(pf);
5225 i40e_sync_filters_subtask(pf);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005226#ifdef CONFIG_I40E_VXLAN
5227 i40e_sync_vxlan_filters_subtask(pf);
5228#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005229 i40e_clean_adminq_subtask(pf);
5230
5231 i40e_service_event_complete(pf);
5232
5233 /* If the tasks have taken longer than one timer cycle or there
5234 * is more work to be done, reschedule the service task now
5235 * rather than wait for the timer to tick again.
5236 */
5237 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
5238 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
5239 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
5240 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
5241 i40e_service_event_schedule(pf);
5242}
5243
5244/**
5245 * i40e_service_timer - timer callback
5246 * @data: pointer to PF struct
5247 **/
5248static void i40e_service_timer(unsigned long data)
5249{
5250 struct i40e_pf *pf = (struct i40e_pf *)data;
5251
5252 mod_timer(&pf->service_timer,
5253 round_jiffies(jiffies + pf->service_timer_period));
5254 i40e_service_event_schedule(pf);
5255}
5256
5257/**
5258 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
5259 * @vsi: the VSI being configured
5260 **/
5261static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
5262{
5263 struct i40e_pf *pf = vsi->back;
5264
5265 switch (vsi->type) {
5266 case I40E_VSI_MAIN:
5267 vsi->alloc_queue_pairs = pf->num_lan_qps;
5268 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5269 I40E_REQ_DESCRIPTOR_MULTIPLE);
5270 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5271 vsi->num_q_vectors = pf->num_lan_msix;
5272 else
5273 vsi->num_q_vectors = 1;
5274
5275 break;
5276
5277 case I40E_VSI_FDIR:
5278 vsi->alloc_queue_pairs = 1;
5279 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
5280 I40E_REQ_DESCRIPTOR_MULTIPLE);
5281 vsi->num_q_vectors = 1;
5282 break;
5283
5284 case I40E_VSI_VMDQ2:
5285 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
5286 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5287 I40E_REQ_DESCRIPTOR_MULTIPLE);
5288 vsi->num_q_vectors = pf->num_vmdq_msix;
5289 break;
5290
5291 case I40E_VSI_SRIOV:
5292 vsi->alloc_queue_pairs = pf->num_vf_qps;
5293 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5294 I40E_REQ_DESCRIPTOR_MULTIPLE);
5295 break;
5296
5297 default:
5298 WARN_ON(1);
5299 return -ENODATA;
5300 }
5301
5302 return 0;
5303}
5304
5305/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005306 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
5307 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005308 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005309 *
5310 * On error: returns error code (negative)
5311 * On success: returns 0
5312 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005313static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005314{
5315 int size;
5316 int ret = 0;
5317
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00005318 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005319 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
5320 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
5321 if (!vsi->tx_rings)
5322 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005323 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
5324
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005325 if (alloc_qvectors) {
5326 /* allocate memory for q_vector pointers */
5327 size = sizeof(struct i40e_q_vectors *) * vsi->num_q_vectors;
5328 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
5329 if (!vsi->q_vectors) {
5330 ret = -ENOMEM;
5331 goto err_vectors;
5332 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005333 }
5334 return ret;
5335
5336err_vectors:
5337 kfree(vsi->tx_rings);
5338 return ret;
5339}
5340
5341/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005342 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
5343 * @pf: board private structure
5344 * @type: type of VSI
5345 *
5346 * On error: returns error code (negative)
5347 * On success: returns vsi index in PF (positive)
5348 **/
5349static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
5350{
5351 int ret = -ENODEV;
5352 struct i40e_vsi *vsi;
5353 int vsi_idx;
5354 int i;
5355
5356 /* Need to protect the allocation of the VSIs at the PF level */
5357 mutex_lock(&pf->switch_mutex);
5358
5359 /* VSI list may be fragmented if VSI creation/destruction has
5360 * been happening. We can afford to do a quick scan to look
5361 * for any free VSIs in the list.
5362 *
5363 * find next empty vsi slot, looping back around if necessary
5364 */
5365 i = pf->next_vsi;
5366 while (i < pf->hw.func_caps.num_vsis && pf->vsi[i])
5367 i++;
5368 if (i >= pf->hw.func_caps.num_vsis) {
5369 i = 0;
5370 while (i < pf->next_vsi && pf->vsi[i])
5371 i++;
5372 }
5373
5374 if (i < pf->hw.func_caps.num_vsis && !pf->vsi[i]) {
5375 vsi_idx = i; /* Found one! */
5376 } else {
5377 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00005378 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005379 }
5380 pf->next_vsi = ++i;
5381
5382 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
5383 if (!vsi) {
5384 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00005385 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005386 }
5387 vsi->type = type;
5388 vsi->back = pf;
5389 set_bit(__I40E_DOWN, &vsi->state);
5390 vsi->flags = 0;
5391 vsi->idx = vsi_idx;
5392 vsi->rx_itr_setting = pf->rx_itr_default;
5393 vsi->tx_itr_setting = pf->tx_itr_default;
5394 vsi->netdev_registered = false;
5395 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
5396 INIT_LIST_HEAD(&vsi->mac_filter_list);
5397
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005398 ret = i40e_set_num_rings_in_vsi(vsi);
5399 if (ret)
5400 goto err_rings;
5401
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005402 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005403 if (ret)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005404 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00005405
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005406 /* Setup default MSIX irq handler for VSI */
5407 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
5408
5409 pf->vsi[vsi_idx] = vsi;
5410 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00005411 goto unlock_pf;
5412
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005413err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00005414 pf->next_vsi = i - 1;
5415 kfree(vsi);
5416unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005417 mutex_unlock(&pf->switch_mutex);
5418 return ret;
5419}
5420
5421/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005422 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
5423 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005424 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005425 *
5426 * On error: returns error code (negative)
5427 * On success: returns 0
5428 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005429static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005430{
5431 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005432 if (free_qvectors) {
5433 kfree(vsi->q_vectors);
5434 vsi->q_vectors = NULL;
5435 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005436 kfree(vsi->tx_rings);
5437 vsi->tx_rings = NULL;
5438 vsi->rx_rings = NULL;
5439}
5440
5441/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005442 * i40e_vsi_clear - Deallocate the VSI provided
5443 * @vsi: the VSI being un-configured
5444 **/
5445static int i40e_vsi_clear(struct i40e_vsi *vsi)
5446{
5447 struct i40e_pf *pf;
5448
5449 if (!vsi)
5450 return 0;
5451
5452 if (!vsi->back)
5453 goto free_vsi;
5454 pf = vsi->back;
5455
5456 mutex_lock(&pf->switch_mutex);
5457 if (!pf->vsi[vsi->idx]) {
5458 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
5459 vsi->idx, vsi->idx, vsi, vsi->type);
5460 goto unlock_vsi;
5461 }
5462
5463 if (pf->vsi[vsi->idx] != vsi) {
5464 dev_err(&pf->pdev->dev,
5465 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
5466 pf->vsi[vsi->idx]->idx,
5467 pf->vsi[vsi->idx],
5468 pf->vsi[vsi->idx]->type,
5469 vsi->idx, vsi, vsi->type);
5470 goto unlock_vsi;
5471 }
5472
5473 /* updates the pf for this cleared vsi */
5474 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
5475 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
5476
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005477 i40e_vsi_free_arrays(vsi, true);
Alexander Duyck493fb302013-09-28 07:01:44 +00005478
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005479 pf->vsi[vsi->idx] = NULL;
5480 if (vsi->idx < pf->next_vsi)
5481 pf->next_vsi = vsi->idx;
5482
5483unlock_vsi:
5484 mutex_unlock(&pf->switch_mutex);
5485free_vsi:
5486 kfree(vsi);
5487
5488 return 0;
5489}
5490
5491/**
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005492 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
5493 * @vsi: the VSI being cleaned
5494 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00005495static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005496{
5497 int i;
5498
Greg Rose8e9dca52013-12-18 13:45:53 +00005499 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00005500 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00005501 kfree_rcu(vsi->tx_rings[i], rcu);
5502 vsi->tx_rings[i] = NULL;
5503 vsi->rx_rings[i] = NULL;
5504 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00005505 }
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005506}
5507
5508/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005509 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
5510 * @vsi: the VSI being configured
5511 **/
5512static int i40e_alloc_rings(struct i40e_vsi *vsi)
5513{
5514 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005515 int i;
5516
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005517 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00005518 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005519 struct i40e_ring *tx_ring;
5520 struct i40e_ring *rx_ring;
5521
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00005522 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005523 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
5524 if (!tx_ring)
5525 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005526
5527 tx_ring->queue_index = i;
5528 tx_ring->reg_idx = vsi->base_queue + i;
5529 tx_ring->ring_active = false;
5530 tx_ring->vsi = vsi;
5531 tx_ring->netdev = vsi->netdev;
5532 tx_ring->dev = &pf->pdev->dev;
5533 tx_ring->count = vsi->num_desc;
5534 tx_ring->size = 0;
5535 tx_ring->dcb_tc = 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005536 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005537
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005538 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005539 rx_ring->queue_index = i;
5540 rx_ring->reg_idx = vsi->base_queue + i;
5541 rx_ring->ring_active = false;
5542 rx_ring->vsi = vsi;
5543 rx_ring->netdev = vsi->netdev;
5544 rx_ring->dev = &pf->pdev->dev;
5545 rx_ring->count = vsi->num_desc;
5546 rx_ring->size = 0;
5547 rx_ring->dcb_tc = 0;
5548 if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
5549 set_ring_16byte_desc_enabled(rx_ring);
5550 else
5551 clear_ring_16byte_desc_enabled(rx_ring);
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005552 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005553 }
5554
5555 return 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005556
5557err_out:
5558 i40e_vsi_clear_rings(vsi);
5559 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005560}
5561
5562/**
5563 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
5564 * @pf: board private structure
5565 * @vectors: the number of MSI-X vectors to request
5566 *
5567 * Returns the number of vectors reserved, or error
5568 **/
5569static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
5570{
5571 int err = 0;
5572
5573 pf->num_msix_entries = 0;
5574 while (vectors >= I40E_MIN_MSIX) {
5575 err = pci_enable_msix(pf->pdev, pf->msix_entries, vectors);
5576 if (err == 0) {
5577 /* good to go */
5578 pf->num_msix_entries = vectors;
5579 break;
5580 } else if (err < 0) {
5581 /* total failure */
5582 dev_info(&pf->pdev->dev,
5583 "MSI-X vector reservation failed: %d\n", err);
5584 vectors = 0;
5585 break;
5586 } else {
5587 /* err > 0 is the hint for retry */
5588 dev_info(&pf->pdev->dev,
5589 "MSI-X vectors wanted %d, retrying with %d\n",
5590 vectors, err);
5591 vectors = err;
5592 }
5593 }
5594
5595 if (vectors > 0 && vectors < I40E_MIN_MSIX) {
5596 dev_info(&pf->pdev->dev,
5597 "Couldn't get enough vectors, only %d available\n",
5598 vectors);
5599 vectors = 0;
5600 }
5601
5602 return vectors;
5603}
5604
5605/**
5606 * i40e_init_msix - Setup the MSIX capability
5607 * @pf: board private structure
5608 *
5609 * Work with the OS to set up the MSIX vectors needed.
5610 *
5611 * Returns 0 on success, negative on failure
5612 **/
5613static int i40e_init_msix(struct i40e_pf *pf)
5614{
5615 i40e_status err = 0;
5616 struct i40e_hw *hw = &pf->hw;
5617 int v_budget, i;
5618 int vec;
5619
5620 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
5621 return -ENODEV;
5622
5623 /* The number of vectors we'll request will be comprised of:
5624 * - Add 1 for "other" cause for Admin Queue events, etc.
5625 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00005626 * - Queues being used for RSS.
5627 * We don't need as many as max_rss_size vectors.
5628 * use rss_size instead in the calculation since that
5629 * is governed by number of cpus in the system.
5630 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005631 * - The number of VMDq pairs
5632 * Once we count this up, try the request.
5633 *
5634 * If we can't get what we want, we'll simplify to nearly nothing
5635 * and try again. If that still fails, we punt.
5636 */
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00005637 pf->num_lan_msix = pf->num_lan_qps - (pf->rss_size_max - pf->rss_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005638 pf->num_vmdq_msix = pf->num_vmdq_qps;
5639 v_budget = 1 + pf->num_lan_msix;
5640 v_budget += (pf->num_vmdq_vsis * pf->num_vmdq_msix);
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08005641 if (pf->flags & I40E_FLAG_FD_SB_ENABLED)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005642 v_budget++;
5643
5644 /* Scale down if necessary, and the rings will share vectors */
5645 v_budget = min_t(int, v_budget, hw->func_caps.num_msix_vectors);
5646
5647 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
5648 GFP_KERNEL);
5649 if (!pf->msix_entries)
5650 return -ENOMEM;
5651
5652 for (i = 0; i < v_budget; i++)
5653 pf->msix_entries[i].entry = i;
5654 vec = i40e_reserve_msix_vectors(pf, v_budget);
5655 if (vec < I40E_MIN_MSIX) {
5656 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
5657 kfree(pf->msix_entries);
5658 pf->msix_entries = NULL;
5659 return -ENODEV;
5660
5661 } else if (vec == I40E_MIN_MSIX) {
5662 /* Adjust for minimal MSIX use */
5663 dev_info(&pf->pdev->dev, "Features disabled, not enough MSIX vectors\n");
5664 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
5665 pf->num_vmdq_vsis = 0;
5666 pf->num_vmdq_qps = 0;
5667 pf->num_vmdq_msix = 0;
5668 pf->num_lan_qps = 1;
5669 pf->num_lan_msix = 1;
5670
5671 } else if (vec != v_budget) {
5672 /* Scale vector usage down */
5673 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
5674 vec--; /* reserve the misc vector */
5675
5676 /* partition out the remaining vectors */
5677 switch (vec) {
5678 case 2:
5679 pf->num_vmdq_vsis = 1;
5680 pf->num_lan_msix = 1;
5681 break;
5682 case 3:
5683 pf->num_vmdq_vsis = 1;
5684 pf->num_lan_msix = 2;
5685 break;
5686 default:
5687 pf->num_lan_msix = min_t(int, (vec / 2),
5688 pf->num_lan_qps);
5689 pf->num_vmdq_vsis = min_t(int, (vec - pf->num_lan_msix),
5690 I40E_DEFAULT_NUM_VMDQ_VSI);
5691 break;
5692 }
5693 }
5694
5695 return err;
5696}
5697
5698/**
Alexander Duyck493fb302013-09-28 07:01:44 +00005699 * i40e_alloc_q_vector - Allocate memory for a single interrupt vector
5700 * @vsi: the VSI being configured
5701 * @v_idx: index of the vector in the vsi struct
5702 *
5703 * We allocate one q_vector. If allocation fails we return -ENOMEM.
5704 **/
5705static int i40e_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
5706{
5707 struct i40e_q_vector *q_vector;
5708
5709 /* allocate q_vector */
5710 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
5711 if (!q_vector)
5712 return -ENOMEM;
5713
5714 q_vector->vsi = vsi;
5715 q_vector->v_idx = v_idx;
5716 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
5717 if (vsi->netdev)
5718 netif_napi_add(vsi->netdev, &q_vector->napi,
5719 i40e_napi_poll, vsi->work_limit);
5720
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00005721 q_vector->rx.latency_range = I40E_LOW_LATENCY;
5722 q_vector->tx.latency_range = I40E_LOW_LATENCY;
5723
Alexander Duyck493fb302013-09-28 07:01:44 +00005724 /* tie q_vector and vsi together */
5725 vsi->q_vectors[v_idx] = q_vector;
5726
5727 return 0;
5728}
5729
5730/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005731 * i40e_alloc_q_vectors - Allocate memory for interrupt vectors
5732 * @vsi: the VSI being configured
5733 *
5734 * We allocate one q_vector per queue interrupt. If allocation fails we
5735 * return -ENOMEM.
5736 **/
5737static int i40e_alloc_q_vectors(struct i40e_vsi *vsi)
5738{
5739 struct i40e_pf *pf = vsi->back;
5740 int v_idx, num_q_vectors;
Alexander Duyck493fb302013-09-28 07:01:44 +00005741 int err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005742
5743 /* if not MSIX, give the one vector only to the LAN VSI */
5744 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5745 num_q_vectors = vsi->num_q_vectors;
5746 else if (vsi == pf->vsi[pf->lan_vsi])
5747 num_q_vectors = 1;
5748 else
5749 return -EINVAL;
5750
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005751 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00005752 err = i40e_alloc_q_vector(vsi, v_idx);
5753 if (err)
5754 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005755 }
5756
5757 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00005758
5759err_out:
5760 while (v_idx--)
5761 i40e_free_q_vector(vsi, v_idx);
5762
5763 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005764}
5765
5766/**
5767 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
5768 * @pf: board private structure to initialize
5769 **/
5770static void i40e_init_interrupt_scheme(struct i40e_pf *pf)
5771{
5772 int err = 0;
5773
5774 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
5775 err = i40e_init_msix(pf);
5776 if (err) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08005777 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
5778 I40E_FLAG_RSS_ENABLED |
5779 I40E_FLAG_DCB_ENABLED |
5780 I40E_FLAG_SRIOV_ENABLED |
5781 I40E_FLAG_FD_SB_ENABLED |
5782 I40E_FLAG_FD_ATR_ENABLED |
5783 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005784
5785 /* rework the queue expectations without MSIX */
5786 i40e_determine_queue_usage(pf);
5787 }
5788 }
5789
5790 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
5791 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Shannon Nelson958a3e32013-09-28 07:13:28 +00005792 dev_info(&pf->pdev->dev, "MSIX not available, trying MSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005793 err = pci_enable_msi(pf->pdev);
5794 if (err) {
Shannon Nelson958a3e32013-09-28 07:13:28 +00005795 dev_info(&pf->pdev->dev, "MSI init failed - %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005796 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
5797 }
5798 }
5799
Shannon Nelson958a3e32013-09-28 07:13:28 +00005800 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
5801 dev_info(&pf->pdev->dev, "MSIX and MSI not available, falling back to Legacy IRQ\n");
5802
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005803 /* track first vector for misc interrupts */
5804 err = i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT-1);
5805}
5806
5807/**
5808 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
5809 * @pf: board private structure
5810 *
5811 * This sets up the handler for MSIX 0, which is used to manage the
5812 * non-queue interrupts, e.g. AdminQ and errors. This is not used
5813 * when in MSI or Legacy interrupt mode.
5814 **/
5815static int i40e_setup_misc_vector(struct i40e_pf *pf)
5816{
5817 struct i40e_hw *hw = &pf->hw;
5818 int err = 0;
5819
5820 /* Only request the irq if this is the first time through, and
5821 * not when we're rebuilding after a Reset
5822 */
5823 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
5824 err = request_irq(pf->msix_entries[0].vector,
5825 i40e_intr, 0, pf->misc_int_name, pf);
5826 if (err) {
5827 dev_info(&pf->pdev->dev,
5828 "request_irq for msix_misc failed: %d\n", err);
5829 return -EFAULT;
5830 }
5831 }
5832
5833 i40e_enable_misc_int_causes(hw);
5834
5835 /* associate no queues to the misc vector */
5836 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
5837 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
5838
5839 i40e_flush(hw);
5840
5841 i40e_irq_dynamic_enable_icr0(pf);
5842
5843 return err;
5844}
5845
5846/**
5847 * i40e_config_rss - Prepare for RSS if used
5848 * @pf: board private structure
5849 **/
5850static int i40e_config_rss(struct i40e_pf *pf)
5851{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005852 /* Set of random keys generated using kernel random number generator */
5853 static const u32 seed[I40E_PFQF_HKEY_MAX_INDEX + 1] = {0x41b01687,
5854 0x183cfd8c, 0xce880440, 0x580cbc3c, 0x35897377,
5855 0x328b25e1, 0x4fa98922, 0xb7d90c14, 0xd5bad70d,
5856 0xcd15a2c1, 0xe8580225, 0x4a1e9d11, 0xfe5731be};
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00005857 struct i40e_hw *hw = &pf->hw;
5858 u32 lut = 0;
5859 int i, j;
5860 u64 hena;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005861
5862 /* Fill out hash function seed */
5863 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
5864 wr32(hw, I40E_PFQF_HKEY(i), seed[i]);
5865
5866 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
5867 hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
5868 ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
Mitch Williams12dc4fe2013-11-28 06:39:32 +00005869 hena |= I40E_DEFAULT_RSS_HENA;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005870 wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
5871 wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
5872
5873 /* Populate the LUT with max no. of queues in round robin fashion */
5874 for (i = 0, j = 0; i < pf->hw.func_caps.rss_table_size; i++, j++) {
5875
5876 /* The assumption is that lan qp count will be the highest
5877 * qp count for any PF VSI that needs RSS.
5878 * If multiple VSIs need RSS support, all the qp counts
5879 * for those VSIs should be a power of 2 for RSS to work.
5880 * If LAN VSI is the only consumer for RSS then this requirement
5881 * is not necessary.
5882 */
5883 if (j == pf->rss_size)
5884 j = 0;
5885 /* lut = 4-byte sliding window of 4 lut entries */
5886 lut = (lut << 8) | (j &
5887 ((0x1 << pf->hw.func_caps.rss_table_entry_width) - 1));
5888 /* On i = 3, we have 4 entries in lut; write to the register */
5889 if ((i & 3) == 3)
5890 wr32(hw, I40E_PFQF_HLUT(i >> 2), lut);
5891 }
5892 i40e_flush(hw);
5893
5894 return 0;
5895}
5896
5897/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00005898 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
5899 * @pf: board private structure
5900 * @queue_count: the requested queue count for rss.
5901 *
5902 * returns 0 if rss is not enabled, if enabled returns the final rss queue
5903 * count which may be different from the requested queue count.
5904 **/
5905int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
5906{
5907 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
5908 return 0;
5909
5910 queue_count = min_t(int, queue_count, pf->rss_size_max);
5911 queue_count = rounddown_pow_of_two(queue_count);
5912
5913 if (queue_count != pf->rss_size) {
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00005914 i40e_prep_for_reset(pf);
5915
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00005916 pf->rss_size = queue_count;
5917
5918 i40e_reset_and_rebuild(pf, true);
5919 i40e_config_rss(pf);
5920 }
5921 dev_info(&pf->pdev->dev, "RSS count: %d\n", pf->rss_size);
5922 return pf->rss_size;
5923}
5924
5925/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005926 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
5927 * @pf: board private structure to initialize
5928 *
5929 * i40e_sw_init initializes the Adapter private data structure.
5930 * Fields are initialized based on PCI device information and
5931 * OS network device settings (MTU size).
5932 **/
5933static int i40e_sw_init(struct i40e_pf *pf)
5934{
5935 int err = 0;
5936 int size;
5937
5938 pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
5939 (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
Shannon Nelson27599972013-11-16 10:00:43 +00005940 pf->hw.debug_mask = pf->msg_enable | I40E_DEBUG_DIAG;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005941 if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
5942 if (I40E_DEBUG_USER & debug)
5943 pf->hw.debug_mask = debug;
5944 pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
5945 I40E_DEFAULT_MSG_ENABLE);
5946 }
5947
5948 /* Set default capability flags */
5949 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
5950 I40E_FLAG_MSI_ENABLED |
5951 I40E_FLAG_MSIX_ENABLED |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005952 I40E_FLAG_RX_1BUF_ENABLED;
5953
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00005954 /* Depending on PF configurations, it is possible that the RSS
5955 * maximum might end up larger than the available queues
5956 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005957 pf->rss_size_max = 0x1 << pf->hw.func_caps.rss_table_entry_width;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00005958 pf->rss_size_max = min_t(int, pf->rss_size_max,
5959 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005960 if (pf->hw.func_caps.rss) {
5961 pf->flags |= I40E_FLAG_RSS_ENABLED;
Jesse Brandeburgbf051a32013-11-26 10:49:17 +00005962 pf->rss_size = min_t(int, pf->rss_size_max, num_online_cpus());
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005963 pf->rss_size = rounddown_pow_of_two(pf->rss_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005964 } else {
5965 pf->rss_size = 1;
5966 }
5967
Catherine Sullivan2050bc62013-12-18 13:46:03 +00005968 /* MFP mode enabled */
5969 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.mfp_mode_1) {
5970 pf->flags |= I40E_FLAG_MFP_ENABLED;
5971 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
5972 }
5973
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005974 if (pf->hw.func_caps.dcb)
5975 pf->num_tc_qps = I40E_DEFAULT_QUEUES_PER_TC;
5976 else
5977 pf->num_tc_qps = 0;
5978
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005979 /* FW/NVM is not yet fixed in this regard */
5980 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
5981 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
5982 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
5983 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
5984 dev_info(&pf->pdev->dev,
5985 "Flow Director ATR mode Enabled\n");
5986 if (!(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08005987 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005988 dev_info(&pf->pdev->dev,
5989 "Flow Director Side Band mode Enabled\n");
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005990 } else {
5991 dev_info(&pf->pdev->dev,
5992 "Flow Director Side Band mode Disabled in MFP mode\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005993 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005994 pf->fdir_pf_filter_count =
5995 pf->hw.func_caps.fd_filters_guaranteed;
5996 pf->hw.fdir_shared_filter_count =
5997 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005998 }
5999
6000 if (pf->hw.func_caps.vmdq) {
6001 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
6002 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
6003 pf->num_vmdq_qps = I40E_DEFAULT_QUEUES_PER_VMDQ;
6004 }
6005
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006006#ifdef CONFIG_PCI_IOV
6007 if (pf->hw.func_caps.num_vfs) {
6008 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
6009 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
6010 pf->num_req_vfs = min_t(int,
6011 pf->hw.func_caps.num_vfs,
6012 I40E_MAX_VF_COUNT);
Anjali Singhai Jain4a38d092013-11-20 10:03:00 +00006013 dev_info(&pf->pdev->dev,
6014 "Number of VFs being requested for PF[%d] = %d\n",
6015 pf->hw.pf_id, pf->num_req_vfs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006016 }
6017#endif /* CONFIG_PCI_IOV */
6018 pf->eeprom_version = 0xDEAD;
6019 pf->lan_veb = I40E_NO_VEB;
6020 pf->lan_vsi = I40E_NO_VSI;
6021
6022 /* set up queue assignment tracking */
6023 size = sizeof(struct i40e_lump_tracking)
6024 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
6025 pf->qp_pile = kzalloc(size, GFP_KERNEL);
6026 if (!pf->qp_pile) {
6027 err = -ENOMEM;
6028 goto sw_init_done;
6029 }
6030 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
6031 pf->qp_pile->search_hint = 0;
6032
6033 /* set up vector assignment tracking */
6034 size = sizeof(struct i40e_lump_tracking)
6035 + (sizeof(u16) * pf->hw.func_caps.num_msix_vectors);
6036 pf->irq_pile = kzalloc(size, GFP_KERNEL);
6037 if (!pf->irq_pile) {
6038 kfree(pf->qp_pile);
6039 err = -ENOMEM;
6040 goto sw_init_done;
6041 }
6042 pf->irq_pile->num_entries = pf->hw.func_caps.num_msix_vectors;
6043 pf->irq_pile->search_hint = 0;
6044
6045 mutex_init(&pf->switch_mutex);
6046
6047sw_init_done:
6048 return err;
6049}
6050
6051/**
6052 * i40e_set_features - set the netdev feature flags
6053 * @netdev: ptr to the netdev being adjusted
6054 * @features: the feature set that the stack is suggesting
6055 **/
6056static int i40e_set_features(struct net_device *netdev,
6057 netdev_features_t features)
6058{
6059 struct i40e_netdev_priv *np = netdev_priv(netdev);
6060 struct i40e_vsi *vsi = np->vsi;
6061
6062 if (features & NETIF_F_HW_VLAN_CTAG_RX)
6063 i40e_vlan_stripping_enable(vsi);
6064 else
6065 i40e_vlan_stripping_disable(vsi);
6066
6067 return 0;
6068}
6069
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006070#ifdef CONFIG_I40E_VXLAN
6071/**
6072 * i40e_get_vxlan_port_idx - Lookup a possibly offloaded for Rx UDP port
6073 * @pf: board private structure
6074 * @port: The UDP port to look up
6075 *
6076 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
6077 **/
6078static u8 i40e_get_vxlan_port_idx(struct i40e_pf *pf, __be16 port)
6079{
6080 u8 i;
6081
6082 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
6083 if (pf->vxlan_ports[i] == port)
6084 return i;
6085 }
6086
6087 return i;
6088}
6089
6090/**
6091 * i40e_add_vxlan_port - Get notifications about VXLAN ports that come up
6092 * @netdev: This physical port's netdev
6093 * @sa_family: Socket Family that VXLAN is notifying us about
6094 * @port: New UDP port number that VXLAN started listening to
6095 **/
6096static void i40e_add_vxlan_port(struct net_device *netdev,
6097 sa_family_t sa_family, __be16 port)
6098{
6099 struct i40e_netdev_priv *np = netdev_priv(netdev);
6100 struct i40e_vsi *vsi = np->vsi;
6101 struct i40e_pf *pf = vsi->back;
6102 u8 next_idx;
6103 u8 idx;
6104
6105 if (sa_family == AF_INET6)
6106 return;
6107
6108 idx = i40e_get_vxlan_port_idx(pf, port);
6109
6110 /* Check if port already exists */
6111 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6112 netdev_info(netdev, "Port %d already offloaded\n", ntohs(port));
6113 return;
6114 }
6115
6116 /* Now check if there is space to add the new port */
6117 next_idx = i40e_get_vxlan_port_idx(pf, 0);
6118
6119 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6120 netdev_info(netdev, "Maximum number of UDP ports reached, not adding port %d\n",
6121 ntohs(port));
6122 return;
6123 }
6124
6125 /* New port: add it and mark its index in the bitmap */
6126 pf->vxlan_ports[next_idx] = port;
6127 pf->pending_vxlan_bitmap |= (1 << next_idx);
6128
6129 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
6130}
6131
6132/**
6133 * i40e_del_vxlan_port - Get notifications about VXLAN ports that go away
6134 * @netdev: This physical port's netdev
6135 * @sa_family: Socket Family that VXLAN is notifying us about
6136 * @port: UDP port number that VXLAN stopped listening to
6137 **/
6138static void i40e_del_vxlan_port(struct net_device *netdev,
6139 sa_family_t sa_family, __be16 port)
6140{
6141 struct i40e_netdev_priv *np = netdev_priv(netdev);
6142 struct i40e_vsi *vsi = np->vsi;
6143 struct i40e_pf *pf = vsi->back;
6144 u8 idx;
6145
6146 if (sa_family == AF_INET6)
6147 return;
6148
6149 idx = i40e_get_vxlan_port_idx(pf, port);
6150
6151 /* Check if port already exists */
6152 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6153 /* if port exists, set it to 0 (mark for deletion)
6154 * and make it pending
6155 */
6156 pf->vxlan_ports[idx] = 0;
6157
6158 pf->pending_vxlan_bitmap |= (1 << idx);
6159
6160 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
6161 } else {
6162 netdev_warn(netdev, "Port %d was not found, not deleting\n",
6163 ntohs(port));
6164 }
6165}
6166
6167#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006168static const struct net_device_ops i40e_netdev_ops = {
6169 .ndo_open = i40e_open,
6170 .ndo_stop = i40e_close,
6171 .ndo_start_xmit = i40e_lan_xmit_frame,
6172 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
6173 .ndo_set_rx_mode = i40e_set_rx_mode,
6174 .ndo_validate_addr = eth_validate_addr,
6175 .ndo_set_mac_address = i40e_set_mac,
6176 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006177 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006178 .ndo_tx_timeout = i40e_tx_timeout,
6179 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
6180 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
6181#ifdef CONFIG_NET_POLL_CONTROLLER
6182 .ndo_poll_controller = i40e_netpoll,
6183#endif
6184 .ndo_setup_tc = i40e_setup_tc,
6185 .ndo_set_features = i40e_set_features,
6186 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
6187 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
6188 .ndo_set_vf_tx_rate = i40e_ndo_set_vf_bw,
6189 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006190#ifdef CONFIG_I40E_VXLAN
6191 .ndo_add_vxlan_port = i40e_add_vxlan_port,
6192 .ndo_del_vxlan_port = i40e_del_vxlan_port,
6193#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006194};
6195
6196/**
6197 * i40e_config_netdev - Setup the netdev flags
6198 * @vsi: the VSI being configured
6199 *
6200 * Returns 0 on success, negative value on failure
6201 **/
6202static int i40e_config_netdev(struct i40e_vsi *vsi)
6203{
Greg Rose1a103702013-11-28 06:42:39 +00006204 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006205 struct i40e_pf *pf = vsi->back;
6206 struct i40e_hw *hw = &pf->hw;
6207 struct i40e_netdev_priv *np;
6208 struct net_device *netdev;
6209 u8 mac_addr[ETH_ALEN];
6210 int etherdev_size;
6211
6212 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006213 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006214 if (!netdev)
6215 return -ENOMEM;
6216
6217 vsi->netdev = netdev;
6218 np = netdev_priv(netdev);
6219 np->vsi = vsi;
6220
6221 netdev->hw_enc_features = NETIF_F_IP_CSUM |
6222 NETIF_F_GSO_UDP_TUNNEL |
6223 NETIF_F_TSO |
6224 NETIF_F_SG;
6225
6226 netdev->features = NETIF_F_SG |
6227 NETIF_F_IP_CSUM |
6228 NETIF_F_SCTP_CSUM |
6229 NETIF_F_HIGHDMA |
6230 NETIF_F_GSO_UDP_TUNNEL |
6231 NETIF_F_HW_VLAN_CTAG_TX |
6232 NETIF_F_HW_VLAN_CTAG_RX |
6233 NETIF_F_HW_VLAN_CTAG_FILTER |
6234 NETIF_F_IPV6_CSUM |
6235 NETIF_F_TSO |
6236 NETIF_F_TSO6 |
6237 NETIF_F_RXCSUM |
6238 NETIF_F_RXHASH |
6239 0;
6240
6241 /* copy netdev features into list of user selectable features */
6242 netdev->hw_features |= netdev->features;
6243
6244 if (vsi->type == I40E_VSI_MAIN) {
6245 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
6246 memcpy(mac_addr, hw->mac.perm_addr, ETH_ALEN);
6247 } else {
6248 /* relate the VSI_VMDQ name to the VSI_MAIN name */
6249 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
6250 pf->vsi[pf->lan_vsi]->netdev->name);
6251 random_ether_addr(mac_addr);
6252 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
6253 }
Greg Rose1a103702013-11-28 06:42:39 +00006254 i40e_add_filter(vsi, brdcast, I40E_VLAN_ANY, false, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006255
6256 memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
6257 memcpy(netdev->perm_addr, mac_addr, ETH_ALEN);
6258 /* vlan gets same features (except vlan offload)
6259 * after any tweaks for specific VSI types
6260 */
6261 netdev->vlan_features = netdev->features & ~(NETIF_F_HW_VLAN_CTAG_TX |
6262 NETIF_F_HW_VLAN_CTAG_RX |
6263 NETIF_F_HW_VLAN_CTAG_FILTER);
6264 netdev->priv_flags |= IFF_UNICAST_FLT;
6265 netdev->priv_flags |= IFF_SUPP_NOFCS;
6266 /* Setup netdev TC information */
6267 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
6268
6269 netdev->netdev_ops = &i40e_netdev_ops;
6270 netdev->watchdog_timeo = 5 * HZ;
6271 i40e_set_ethtool_ops(netdev);
6272
6273 return 0;
6274}
6275
6276/**
6277 * i40e_vsi_delete - Delete a VSI from the switch
6278 * @vsi: the VSI being removed
6279 *
6280 * Returns 0 on success, negative value on failure
6281 **/
6282static void i40e_vsi_delete(struct i40e_vsi *vsi)
6283{
6284 /* remove default VSI is not allowed */
6285 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
6286 return;
6287
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006288 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
6289 return;
6290}
6291
6292/**
6293 * i40e_add_vsi - Add a VSI to the switch
6294 * @vsi: the VSI being configured
6295 *
6296 * This initializes a VSI context depending on the VSI type to be added and
6297 * passes it down to the add_vsi aq command.
6298 **/
6299static int i40e_add_vsi(struct i40e_vsi *vsi)
6300{
6301 int ret = -ENODEV;
6302 struct i40e_mac_filter *f, *ftmp;
6303 struct i40e_pf *pf = vsi->back;
6304 struct i40e_hw *hw = &pf->hw;
6305 struct i40e_vsi_context ctxt;
6306 u8 enabled_tc = 0x1; /* TC0 enabled */
6307 int f_count = 0;
6308
6309 memset(&ctxt, 0, sizeof(ctxt));
6310 switch (vsi->type) {
6311 case I40E_VSI_MAIN:
6312 /* The PF's main VSI is already setup as part of the
6313 * device initialization, so we'll not bother with
6314 * the add_vsi call, but we will retrieve the current
6315 * VSI context.
6316 */
6317 ctxt.seid = pf->main_vsi_seid;
6318 ctxt.pf_num = pf->hw.pf_id;
6319 ctxt.vf_num = 0;
6320 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6321 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6322 if (ret) {
6323 dev_info(&pf->pdev->dev,
6324 "couldn't get pf vsi config, err %d, aq_err %d\n",
6325 ret, pf->hw.aq.asq_last_status);
6326 return -ENOENT;
6327 }
6328 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
6329 vsi->info.valid_sections = 0;
6330
6331 vsi->seid = ctxt.seid;
6332 vsi->id = ctxt.vsi_number;
6333
6334 enabled_tc = i40e_pf_get_tc_map(pf);
6335
6336 /* MFP mode setup queue map and update VSI */
6337 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
6338 memset(&ctxt, 0, sizeof(ctxt));
6339 ctxt.seid = pf->main_vsi_seid;
6340 ctxt.pf_num = pf->hw.pf_id;
6341 ctxt.vf_num = 0;
6342 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
6343 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
6344 if (ret) {
6345 dev_info(&pf->pdev->dev,
6346 "update vsi failed, aq_err=%d\n",
6347 pf->hw.aq.asq_last_status);
6348 ret = -ENOENT;
6349 goto err;
6350 }
6351 /* update the local VSI info queue map */
6352 i40e_vsi_update_queue_map(vsi, &ctxt);
6353 vsi->info.valid_sections = 0;
6354 } else {
6355 /* Default/Main VSI is only enabled for TC0
6356 * reconfigure it to enable all TCs that are
6357 * available on the port in SFP mode.
6358 */
6359 ret = i40e_vsi_config_tc(vsi, enabled_tc);
6360 if (ret) {
6361 dev_info(&pf->pdev->dev,
6362 "failed to configure TCs for main VSI tc_map 0x%08x, err %d, aq_err %d\n",
6363 enabled_tc, ret,
6364 pf->hw.aq.asq_last_status);
6365 ret = -ENOENT;
6366 }
6367 }
6368 break;
6369
6370 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006371 ctxt.pf_num = hw->pf_id;
6372 ctxt.vf_num = 0;
6373 ctxt.uplink_seid = vsi->uplink_seid;
6374 ctxt.connection_type = 0x1; /* regular data port */
6375 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006376 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006377 break;
6378
6379 case I40E_VSI_VMDQ2:
6380 ctxt.pf_num = hw->pf_id;
6381 ctxt.vf_num = 0;
6382 ctxt.uplink_seid = vsi->uplink_seid;
6383 ctxt.connection_type = 0x1; /* regular data port */
6384 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6385
6386 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6387
6388 /* This VSI is connected to VEB so the switch_id
6389 * should be set to zero by default.
6390 */
6391 ctxt.info.switch_id = 0;
6392 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
6393 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6394
6395 /* Setup the VSI tx/rx queue map for TC0 only for now */
6396 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
6397 break;
6398
6399 case I40E_VSI_SRIOV:
6400 ctxt.pf_num = hw->pf_id;
6401 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
6402 ctxt.uplink_seid = vsi->uplink_seid;
6403 ctxt.connection_type = 0x1; /* regular data port */
6404 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
6405
6406 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6407
6408 /* This VSI is connected to VEB so the switch_id
6409 * should be set to zero by default.
6410 */
6411 ctxt.info.switch_id = cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6412
6413 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
6414 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
6415 /* Setup the VSI tx/rx queue map for TC0 only for now */
6416 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
6417 break;
6418
6419 default:
6420 return -ENODEV;
6421 }
6422
6423 if (vsi->type != I40E_VSI_MAIN) {
6424 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6425 if (ret) {
6426 dev_info(&vsi->back->pdev->dev,
6427 "add vsi failed, aq_err=%d\n",
6428 vsi->back->hw.aq.asq_last_status);
6429 ret = -ENOENT;
6430 goto err;
6431 }
6432 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
6433 vsi->info.valid_sections = 0;
6434 vsi->seid = ctxt.seid;
6435 vsi->id = ctxt.vsi_number;
6436 }
6437
6438 /* If macvlan filters already exist, force them to get loaded */
6439 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
6440 f->changed = true;
6441 f_count++;
6442 }
6443 if (f_count) {
6444 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
6445 pf->flags |= I40E_FLAG_FILTER_SYNC;
6446 }
6447
6448 /* Update VSI BW information */
6449 ret = i40e_vsi_get_bw_info(vsi);
6450 if (ret) {
6451 dev_info(&pf->pdev->dev,
6452 "couldn't get vsi bw info, err %d, aq_err %d\n",
6453 ret, pf->hw.aq.asq_last_status);
6454 /* VSI is already added so not tearing that up */
6455 ret = 0;
6456 }
6457
6458err:
6459 return ret;
6460}
6461
6462/**
6463 * i40e_vsi_release - Delete a VSI and free its resources
6464 * @vsi: the VSI being removed
6465 *
6466 * Returns 0 on success or < 0 on error
6467 **/
6468int i40e_vsi_release(struct i40e_vsi *vsi)
6469{
6470 struct i40e_mac_filter *f, *ftmp;
6471 struct i40e_veb *veb = NULL;
6472 struct i40e_pf *pf;
6473 u16 uplink_seid;
6474 int i, n;
6475
6476 pf = vsi->back;
6477
6478 /* release of a VEB-owner or last VSI is not allowed */
6479 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
6480 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
6481 vsi->seid, vsi->uplink_seid);
6482 return -ENODEV;
6483 }
6484 if (vsi == pf->vsi[pf->lan_vsi] &&
6485 !test_bit(__I40E_DOWN, &pf->state)) {
6486 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
6487 return -ENODEV;
6488 }
6489
6490 uplink_seid = vsi->uplink_seid;
6491 if (vsi->type != I40E_VSI_SRIOV) {
6492 if (vsi->netdev_registered) {
6493 vsi->netdev_registered = false;
6494 if (vsi->netdev) {
6495 /* results in a call to i40e_close() */
6496 unregister_netdev(vsi->netdev);
6497 free_netdev(vsi->netdev);
6498 vsi->netdev = NULL;
6499 }
6500 } else {
6501 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
6502 i40e_down(vsi);
6503 i40e_vsi_free_irq(vsi);
6504 i40e_vsi_free_tx_resources(vsi);
6505 i40e_vsi_free_rx_resources(vsi);
6506 }
6507 i40e_vsi_disable_irq(vsi);
6508 }
6509
6510 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
6511 i40e_del_filter(vsi, f->macaddr, f->vlan,
6512 f->is_vf, f->is_netdev);
6513 i40e_sync_vsi_filters(vsi);
6514
6515 i40e_vsi_delete(vsi);
6516 i40e_vsi_free_q_vectors(vsi);
6517 i40e_vsi_clear_rings(vsi);
6518 i40e_vsi_clear(vsi);
6519
6520 /* If this was the last thing on the VEB, except for the
6521 * controlling VSI, remove the VEB, which puts the controlling
6522 * VSI onto the next level down in the switch.
6523 *
6524 * Well, okay, there's one more exception here: don't remove
6525 * the orphan VEBs yet. We'll wait for an explicit remove request
6526 * from up the network stack.
6527 */
6528 for (n = 0, i = 0; i < pf->hw.func_caps.num_vsis; i++) {
6529 if (pf->vsi[i] &&
6530 pf->vsi[i]->uplink_seid == uplink_seid &&
6531 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
6532 n++; /* count the VSIs */
6533 }
6534 }
6535 for (i = 0; i < I40E_MAX_VEB; i++) {
6536 if (!pf->veb[i])
6537 continue;
6538 if (pf->veb[i]->uplink_seid == uplink_seid)
6539 n++; /* count the VEBs */
6540 if (pf->veb[i]->seid == uplink_seid)
6541 veb = pf->veb[i];
6542 }
6543 if (n == 0 && veb && veb->uplink_seid != 0)
6544 i40e_veb_release(veb);
6545
6546 return 0;
6547}
6548
6549/**
6550 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
6551 * @vsi: ptr to the VSI
6552 *
6553 * This should only be called after i40e_vsi_mem_alloc() which allocates the
6554 * corresponding SW VSI structure and initializes num_queue_pairs for the
6555 * newly allocated VSI.
6556 *
6557 * Returns 0 on success or negative on failure
6558 **/
6559static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
6560{
6561 int ret = -ENOENT;
6562 struct i40e_pf *pf = vsi->back;
6563
Alexander Duyck493fb302013-09-28 07:01:44 +00006564 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006565 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
6566 vsi->seid);
6567 return -EEXIST;
6568 }
6569
6570 if (vsi->base_vector) {
6571 dev_info(&pf->pdev->dev,
6572 "VSI %d has non-zero base vector %d\n",
6573 vsi->seid, vsi->base_vector);
6574 return -EEXIST;
6575 }
6576
6577 ret = i40e_alloc_q_vectors(vsi);
6578 if (ret) {
6579 dev_info(&pf->pdev->dev,
6580 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
6581 vsi->num_q_vectors, vsi->seid, ret);
6582 vsi->num_q_vectors = 0;
6583 goto vector_setup_out;
6584 }
6585
Shannon Nelson958a3e32013-09-28 07:13:28 +00006586 if (vsi->num_q_vectors)
6587 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
6588 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006589 if (vsi->base_vector < 0) {
6590 dev_info(&pf->pdev->dev,
6591 "failed to get q tracking for VSI %d, err=%d\n",
6592 vsi->seid, vsi->base_vector);
6593 i40e_vsi_free_q_vectors(vsi);
6594 ret = -ENOENT;
6595 goto vector_setup_out;
6596 }
6597
6598vector_setup_out:
6599 return ret;
6600}
6601
6602/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006603 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
6604 * @vsi: pointer to the vsi.
6605 *
6606 * This re-allocates a vsi's queue resources.
6607 *
6608 * Returns pointer to the successfully allocated and configured VSI sw struct
6609 * on success, otherwise returns NULL on failure.
6610 **/
6611static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
6612{
6613 struct i40e_pf *pf = vsi->back;
6614 u8 enabled_tc;
6615 int ret;
6616
6617 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
6618 i40e_vsi_clear_rings(vsi);
6619
6620 i40e_vsi_free_arrays(vsi, false);
6621 i40e_set_num_rings_in_vsi(vsi);
6622 ret = i40e_vsi_alloc_arrays(vsi, false);
6623 if (ret)
6624 goto err_vsi;
6625
6626 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
6627 if (ret < 0) {
6628 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
6629 vsi->seid, ret);
6630 goto err_vsi;
6631 }
6632 vsi->base_queue = ret;
6633
6634 /* Update the FW view of the VSI. Force a reset of TC and queue
6635 * layout configurations.
6636 */
6637 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
6638 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
6639 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
6640 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
6641
6642 /* assign it some queues */
6643 ret = i40e_alloc_rings(vsi);
6644 if (ret)
6645 goto err_rings;
6646
6647 /* map all of the rings to the q_vectors */
6648 i40e_vsi_map_rings_to_vectors(vsi);
6649 return vsi;
6650
6651err_rings:
6652 i40e_vsi_free_q_vectors(vsi);
6653 if (vsi->netdev_registered) {
6654 vsi->netdev_registered = false;
6655 unregister_netdev(vsi->netdev);
6656 free_netdev(vsi->netdev);
6657 vsi->netdev = NULL;
6658 }
6659 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
6660err_vsi:
6661 i40e_vsi_clear(vsi);
6662 return NULL;
6663}
6664
6665/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006666 * i40e_vsi_setup - Set up a VSI by a given type
6667 * @pf: board private structure
6668 * @type: VSI type
6669 * @uplink_seid: the switch element to link to
6670 * @param1: usage depends upon VSI type. For VF types, indicates VF id
6671 *
6672 * This allocates the sw VSI structure and its queue resources, then add a VSI
6673 * to the identified VEB.
6674 *
6675 * Returns pointer to the successfully allocated and configure VSI sw struct on
6676 * success, otherwise returns NULL on failure.
6677 **/
6678struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
6679 u16 uplink_seid, u32 param1)
6680{
6681 struct i40e_vsi *vsi = NULL;
6682 struct i40e_veb *veb = NULL;
6683 int ret, i;
6684 int v_idx;
6685
6686 /* The requested uplink_seid must be either
6687 * - the PF's port seid
6688 * no VEB is needed because this is the PF
6689 * or this is a Flow Director special case VSI
6690 * - seid of an existing VEB
6691 * - seid of a VSI that owns an existing VEB
6692 * - seid of a VSI that doesn't own a VEB
6693 * a new VEB is created and the VSI becomes the owner
6694 * - seid of the PF VSI, which is what creates the first VEB
6695 * this is a special case of the previous
6696 *
6697 * Find which uplink_seid we were given and create a new VEB if needed
6698 */
6699 for (i = 0; i < I40E_MAX_VEB; i++) {
6700 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
6701 veb = pf->veb[i];
6702 break;
6703 }
6704 }
6705
6706 if (!veb && uplink_seid != pf->mac_seid) {
6707
6708 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
6709 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
6710 vsi = pf->vsi[i];
6711 break;
6712 }
6713 }
6714 if (!vsi) {
6715 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
6716 uplink_seid);
6717 return NULL;
6718 }
6719
6720 if (vsi->uplink_seid == pf->mac_seid)
6721 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
6722 vsi->tc_config.enabled_tc);
6723 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
6724 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
6725 vsi->tc_config.enabled_tc);
6726
6727 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
6728 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
6729 veb = pf->veb[i];
6730 }
6731 if (!veb) {
6732 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
6733 return NULL;
6734 }
6735
6736 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
6737 uplink_seid = veb->seid;
6738 }
6739
6740 /* get vsi sw struct */
6741 v_idx = i40e_vsi_mem_alloc(pf, type);
6742 if (v_idx < 0)
6743 goto err_alloc;
6744 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006745 if (!vsi)
6746 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006747 vsi->type = type;
6748 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
6749
6750 if (type == I40E_VSI_MAIN)
6751 pf->lan_vsi = v_idx;
6752 else if (type == I40E_VSI_SRIOV)
6753 vsi->vf_id = param1;
6754 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006755 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
6756 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006757 if (ret < 0) {
6758 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
6759 vsi->seid, ret);
6760 goto err_vsi;
6761 }
6762 vsi->base_queue = ret;
6763
6764 /* get a VSI from the hardware */
6765 vsi->uplink_seid = uplink_seid;
6766 ret = i40e_add_vsi(vsi);
6767 if (ret)
6768 goto err_vsi;
6769
6770 switch (vsi->type) {
6771 /* setup the netdev if needed */
6772 case I40E_VSI_MAIN:
6773 case I40E_VSI_VMDQ2:
6774 ret = i40e_config_netdev(vsi);
6775 if (ret)
6776 goto err_netdev;
6777 ret = register_netdev(vsi->netdev);
6778 if (ret)
6779 goto err_netdev;
6780 vsi->netdev_registered = true;
6781 netif_carrier_off(vsi->netdev);
6782 /* fall through */
6783
6784 case I40E_VSI_FDIR:
6785 /* set up vectors and rings if needed */
6786 ret = i40e_vsi_setup_vectors(vsi);
6787 if (ret)
6788 goto err_msix;
6789
6790 ret = i40e_alloc_rings(vsi);
6791 if (ret)
6792 goto err_rings;
6793
6794 /* map all of the rings to the q_vectors */
6795 i40e_vsi_map_rings_to_vectors(vsi);
6796
6797 i40e_vsi_reset_stats(vsi);
6798 break;
6799
6800 default:
6801 /* no netdev or rings for the other VSI types */
6802 break;
6803 }
6804
6805 return vsi;
6806
6807err_rings:
6808 i40e_vsi_free_q_vectors(vsi);
6809err_msix:
6810 if (vsi->netdev_registered) {
6811 vsi->netdev_registered = false;
6812 unregister_netdev(vsi->netdev);
6813 free_netdev(vsi->netdev);
6814 vsi->netdev = NULL;
6815 }
6816err_netdev:
6817 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
6818err_vsi:
6819 i40e_vsi_clear(vsi);
6820err_alloc:
6821 return NULL;
6822}
6823
6824/**
6825 * i40e_veb_get_bw_info - Query VEB BW information
6826 * @veb: the veb to query
6827 *
6828 * Query the Tx scheduler BW configuration data for given VEB
6829 **/
6830static int i40e_veb_get_bw_info(struct i40e_veb *veb)
6831{
6832 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
6833 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
6834 struct i40e_pf *pf = veb->pf;
6835 struct i40e_hw *hw = &pf->hw;
6836 u32 tc_bw_max;
6837 int ret = 0;
6838 int i;
6839
6840 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
6841 &bw_data, NULL);
6842 if (ret) {
6843 dev_info(&pf->pdev->dev,
6844 "query veb bw config failed, aq_err=%d\n",
6845 hw->aq.asq_last_status);
6846 goto out;
6847 }
6848
6849 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
6850 &ets_data, NULL);
6851 if (ret) {
6852 dev_info(&pf->pdev->dev,
6853 "query veb bw ets config failed, aq_err=%d\n",
6854 hw->aq.asq_last_status);
6855 goto out;
6856 }
6857
6858 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
6859 veb->bw_max_quanta = ets_data.tc_bw_max;
6860 veb->is_abs_credits = bw_data.absolute_credits_enable;
6861 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
6862 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
6863 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6864 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
6865 veb->bw_tc_limit_credits[i] =
6866 le16_to_cpu(bw_data.tc_bw_limits[i]);
6867 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
6868 }
6869
6870out:
6871 return ret;
6872}
6873
6874/**
6875 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
6876 * @pf: board private structure
6877 *
6878 * On error: returns error code (negative)
6879 * On success: returns vsi index in PF (positive)
6880 **/
6881static int i40e_veb_mem_alloc(struct i40e_pf *pf)
6882{
6883 int ret = -ENOENT;
6884 struct i40e_veb *veb;
6885 int i;
6886
6887 /* Need to protect the allocation of switch elements at the PF level */
6888 mutex_lock(&pf->switch_mutex);
6889
6890 /* VEB list may be fragmented if VEB creation/destruction has
6891 * been happening. We can afford to do a quick scan to look
6892 * for any free slots in the list.
6893 *
6894 * find next empty veb slot, looping back around if necessary
6895 */
6896 i = 0;
6897 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
6898 i++;
6899 if (i >= I40E_MAX_VEB) {
6900 ret = -ENOMEM;
6901 goto err_alloc_veb; /* out of VEB slots! */
6902 }
6903
6904 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
6905 if (!veb) {
6906 ret = -ENOMEM;
6907 goto err_alloc_veb;
6908 }
6909 veb->pf = pf;
6910 veb->idx = i;
6911 veb->enabled_tc = 1;
6912
6913 pf->veb[i] = veb;
6914 ret = i;
6915err_alloc_veb:
6916 mutex_unlock(&pf->switch_mutex);
6917 return ret;
6918}
6919
6920/**
6921 * i40e_switch_branch_release - Delete a branch of the switch tree
6922 * @branch: where to start deleting
6923 *
6924 * This uses recursion to find the tips of the branch to be
6925 * removed, deleting until we get back to and can delete this VEB.
6926 **/
6927static void i40e_switch_branch_release(struct i40e_veb *branch)
6928{
6929 struct i40e_pf *pf = branch->pf;
6930 u16 branch_seid = branch->seid;
6931 u16 veb_idx = branch->idx;
6932 int i;
6933
6934 /* release any VEBs on this VEB - RECURSION */
6935 for (i = 0; i < I40E_MAX_VEB; i++) {
6936 if (!pf->veb[i])
6937 continue;
6938 if (pf->veb[i]->uplink_seid == branch->seid)
6939 i40e_switch_branch_release(pf->veb[i]);
6940 }
6941
6942 /* Release the VSIs on this VEB, but not the owner VSI.
6943 *
6944 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
6945 * the VEB itself, so don't use (*branch) after this loop.
6946 */
6947 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
6948 if (!pf->vsi[i])
6949 continue;
6950 if (pf->vsi[i]->uplink_seid == branch_seid &&
6951 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
6952 i40e_vsi_release(pf->vsi[i]);
6953 }
6954 }
6955
6956 /* There's one corner case where the VEB might not have been
6957 * removed, so double check it here and remove it if needed.
6958 * This case happens if the veb was created from the debugfs
6959 * commands and no VSIs were added to it.
6960 */
6961 if (pf->veb[veb_idx])
6962 i40e_veb_release(pf->veb[veb_idx]);
6963}
6964
6965/**
6966 * i40e_veb_clear - remove veb struct
6967 * @veb: the veb to remove
6968 **/
6969static void i40e_veb_clear(struct i40e_veb *veb)
6970{
6971 if (!veb)
6972 return;
6973
6974 if (veb->pf) {
6975 struct i40e_pf *pf = veb->pf;
6976
6977 mutex_lock(&pf->switch_mutex);
6978 if (pf->veb[veb->idx] == veb)
6979 pf->veb[veb->idx] = NULL;
6980 mutex_unlock(&pf->switch_mutex);
6981 }
6982
6983 kfree(veb);
6984}
6985
6986/**
6987 * i40e_veb_release - Delete a VEB and free its resources
6988 * @veb: the VEB being removed
6989 **/
6990void i40e_veb_release(struct i40e_veb *veb)
6991{
6992 struct i40e_vsi *vsi = NULL;
6993 struct i40e_pf *pf;
6994 int i, n = 0;
6995
6996 pf = veb->pf;
6997
6998 /* find the remaining VSI and check for extras */
6999 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
7000 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
7001 n++;
7002 vsi = pf->vsi[i];
7003 }
7004 }
7005 if (n != 1) {
7006 dev_info(&pf->pdev->dev,
7007 "can't remove VEB %d with %d VSIs left\n",
7008 veb->seid, n);
7009 return;
7010 }
7011
7012 /* move the remaining VSI to uplink veb */
7013 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
7014 if (veb->uplink_seid) {
7015 vsi->uplink_seid = veb->uplink_seid;
7016 if (veb->uplink_seid == pf->mac_seid)
7017 vsi->veb_idx = I40E_NO_VEB;
7018 else
7019 vsi->veb_idx = veb->veb_idx;
7020 } else {
7021 /* floating VEB */
7022 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
7023 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
7024 }
7025
7026 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
7027 i40e_veb_clear(veb);
7028
7029 return;
7030}
7031
7032/**
7033 * i40e_add_veb - create the VEB in the switch
7034 * @veb: the VEB to be instantiated
7035 * @vsi: the controlling VSI
7036 **/
7037static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
7038{
Greg Rose56747262013-11-28 06:39:37 +00007039 bool is_default = false;
Kevin Scotte1c51b952013-11-20 10:02:51 +00007040 bool is_cloud = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007041 int ret;
7042
7043 /* get a VEB from the hardware */
7044 ret = i40e_aq_add_veb(&veb->pf->hw, veb->uplink_seid, vsi->seid,
Kevin Scotte1c51b952013-11-20 10:02:51 +00007045 veb->enabled_tc, is_default,
7046 is_cloud, &veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007047 if (ret) {
7048 dev_info(&veb->pf->pdev->dev,
7049 "couldn't add VEB, err %d, aq_err %d\n",
7050 ret, veb->pf->hw.aq.asq_last_status);
7051 return -EPERM;
7052 }
7053
7054 /* get statistics counter */
7055 ret = i40e_aq_get_veb_parameters(&veb->pf->hw, veb->seid, NULL, NULL,
7056 &veb->stats_idx, NULL, NULL, NULL);
7057 if (ret) {
7058 dev_info(&veb->pf->pdev->dev,
7059 "couldn't get VEB statistics idx, err %d, aq_err %d\n",
7060 ret, veb->pf->hw.aq.asq_last_status);
7061 return -EPERM;
7062 }
7063 ret = i40e_veb_get_bw_info(veb);
7064 if (ret) {
7065 dev_info(&veb->pf->pdev->dev,
7066 "couldn't get VEB bw info, err %d, aq_err %d\n",
7067 ret, veb->pf->hw.aq.asq_last_status);
7068 i40e_aq_delete_element(&veb->pf->hw, veb->seid, NULL);
7069 return -ENOENT;
7070 }
7071
7072 vsi->uplink_seid = veb->seid;
7073 vsi->veb_idx = veb->idx;
7074 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
7075
7076 return 0;
7077}
7078
7079/**
7080 * i40e_veb_setup - Set up a VEB
7081 * @pf: board private structure
7082 * @flags: VEB setup flags
7083 * @uplink_seid: the switch element to link to
7084 * @vsi_seid: the initial VSI seid
7085 * @enabled_tc: Enabled TC bit-map
7086 *
7087 * This allocates the sw VEB structure and links it into the switch
7088 * It is possible and legal for this to be a duplicate of an already
7089 * existing VEB. It is also possible for both uplink and vsi seids
7090 * to be zero, in order to create a floating VEB.
7091 *
7092 * Returns pointer to the successfully allocated VEB sw struct on
7093 * success, otherwise returns NULL on failure.
7094 **/
7095struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
7096 u16 uplink_seid, u16 vsi_seid,
7097 u8 enabled_tc)
7098{
7099 struct i40e_veb *veb, *uplink_veb = NULL;
7100 int vsi_idx, veb_idx;
7101 int ret;
7102
7103 /* if one seid is 0, the other must be 0 to create a floating relay */
7104 if ((uplink_seid == 0 || vsi_seid == 0) &&
7105 (uplink_seid + vsi_seid != 0)) {
7106 dev_info(&pf->pdev->dev,
7107 "one, not both seid's are 0: uplink=%d vsi=%d\n",
7108 uplink_seid, vsi_seid);
7109 return NULL;
7110 }
7111
7112 /* make sure there is such a vsi and uplink */
7113 for (vsi_idx = 0; vsi_idx < pf->hw.func_caps.num_vsis; vsi_idx++)
7114 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
7115 break;
7116 if (vsi_idx >= pf->hw.func_caps.num_vsis && vsi_seid != 0) {
7117 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
7118 vsi_seid);
7119 return NULL;
7120 }
7121
7122 if (uplink_seid && uplink_seid != pf->mac_seid) {
7123 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
7124 if (pf->veb[veb_idx] &&
7125 pf->veb[veb_idx]->seid == uplink_seid) {
7126 uplink_veb = pf->veb[veb_idx];
7127 break;
7128 }
7129 }
7130 if (!uplink_veb) {
7131 dev_info(&pf->pdev->dev,
7132 "uplink seid %d not found\n", uplink_seid);
7133 return NULL;
7134 }
7135 }
7136
7137 /* get veb sw struct */
7138 veb_idx = i40e_veb_mem_alloc(pf);
7139 if (veb_idx < 0)
7140 goto err_alloc;
7141 veb = pf->veb[veb_idx];
7142 veb->flags = flags;
7143 veb->uplink_seid = uplink_seid;
7144 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
7145 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
7146
7147 /* create the VEB in the switch */
7148 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
7149 if (ret)
7150 goto err_veb;
7151
7152 return veb;
7153
7154err_veb:
7155 i40e_veb_clear(veb);
7156err_alloc:
7157 return NULL;
7158}
7159
7160/**
7161 * i40e_setup_pf_switch_element - set pf vars based on switch type
7162 * @pf: board private structure
7163 * @ele: element we are building info from
7164 * @num_reported: total number of elements
7165 * @printconfig: should we print the contents
7166 *
7167 * helper function to assist in extracting a few useful SEID values.
7168 **/
7169static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
7170 struct i40e_aqc_switch_config_element_resp *ele,
7171 u16 num_reported, bool printconfig)
7172{
7173 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
7174 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
7175 u8 element_type = ele->element_type;
7176 u16 seid = le16_to_cpu(ele->seid);
7177
7178 if (printconfig)
7179 dev_info(&pf->pdev->dev,
7180 "type=%d seid=%d uplink=%d downlink=%d\n",
7181 element_type, seid, uplink_seid, downlink_seid);
7182
7183 switch (element_type) {
7184 case I40E_SWITCH_ELEMENT_TYPE_MAC:
7185 pf->mac_seid = seid;
7186 break;
7187 case I40E_SWITCH_ELEMENT_TYPE_VEB:
7188 /* Main VEB? */
7189 if (uplink_seid != pf->mac_seid)
7190 break;
7191 if (pf->lan_veb == I40E_NO_VEB) {
7192 int v;
7193
7194 /* find existing or else empty VEB */
7195 for (v = 0; v < I40E_MAX_VEB; v++) {
7196 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
7197 pf->lan_veb = v;
7198 break;
7199 }
7200 }
7201 if (pf->lan_veb == I40E_NO_VEB) {
7202 v = i40e_veb_mem_alloc(pf);
7203 if (v < 0)
7204 break;
7205 pf->lan_veb = v;
7206 }
7207 }
7208
7209 pf->veb[pf->lan_veb]->seid = seid;
7210 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
7211 pf->veb[pf->lan_veb]->pf = pf;
7212 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
7213 break;
7214 case I40E_SWITCH_ELEMENT_TYPE_VSI:
7215 if (num_reported != 1)
7216 break;
7217 /* This is immediately after a reset so we can assume this is
7218 * the PF's VSI
7219 */
7220 pf->mac_seid = uplink_seid;
7221 pf->pf_seid = downlink_seid;
7222 pf->main_vsi_seid = seid;
7223 if (printconfig)
7224 dev_info(&pf->pdev->dev,
7225 "pf_seid=%d main_vsi_seid=%d\n",
7226 pf->pf_seid, pf->main_vsi_seid);
7227 break;
7228 case I40E_SWITCH_ELEMENT_TYPE_PF:
7229 case I40E_SWITCH_ELEMENT_TYPE_VF:
7230 case I40E_SWITCH_ELEMENT_TYPE_EMP:
7231 case I40E_SWITCH_ELEMENT_TYPE_BMC:
7232 case I40E_SWITCH_ELEMENT_TYPE_PE:
7233 case I40E_SWITCH_ELEMENT_TYPE_PA:
7234 /* ignore these for now */
7235 break;
7236 default:
7237 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
7238 element_type, seid);
7239 break;
7240 }
7241}
7242
7243/**
7244 * i40e_fetch_switch_configuration - Get switch config from firmware
7245 * @pf: board private structure
7246 * @printconfig: should we print the contents
7247 *
7248 * Get the current switch configuration from the device and
7249 * extract a few useful SEID values.
7250 **/
7251int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
7252{
7253 struct i40e_aqc_get_switch_config_resp *sw_config;
7254 u16 next_seid = 0;
7255 int ret = 0;
7256 u8 *aq_buf;
7257 int i;
7258
7259 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
7260 if (!aq_buf)
7261 return -ENOMEM;
7262
7263 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
7264 do {
7265 u16 num_reported, num_total;
7266
7267 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
7268 I40E_AQ_LARGE_BUF,
7269 &next_seid, NULL);
7270 if (ret) {
7271 dev_info(&pf->pdev->dev,
7272 "get switch config failed %d aq_err=%x\n",
7273 ret, pf->hw.aq.asq_last_status);
7274 kfree(aq_buf);
7275 return -ENOENT;
7276 }
7277
7278 num_reported = le16_to_cpu(sw_config->header.num_reported);
7279 num_total = le16_to_cpu(sw_config->header.num_total);
7280
7281 if (printconfig)
7282 dev_info(&pf->pdev->dev,
7283 "header: %d reported %d total\n",
7284 num_reported, num_total);
7285
7286 if (num_reported) {
7287 int sz = sizeof(*sw_config) * num_reported;
7288
7289 kfree(pf->sw_config);
7290 pf->sw_config = kzalloc(sz, GFP_KERNEL);
7291 if (pf->sw_config)
7292 memcpy(pf->sw_config, sw_config, sz);
7293 }
7294
7295 for (i = 0; i < num_reported; i++) {
7296 struct i40e_aqc_switch_config_element_resp *ele =
7297 &sw_config->element[i];
7298
7299 i40e_setup_pf_switch_element(pf, ele, num_reported,
7300 printconfig);
7301 }
7302 } while (next_seid != 0);
7303
7304 kfree(aq_buf);
7305 return ret;
7306}
7307
7308/**
7309 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
7310 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007311 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007312 *
7313 * Returns 0 on success, negative value on failure
7314 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007315static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007316{
Jesse Brandeburg895106a2013-11-26 10:49:16 +00007317 u32 rxfc = 0, txfc = 0, rxfc_reg;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007318 int ret;
7319
7320 /* find out what's out there already */
7321 ret = i40e_fetch_switch_configuration(pf, false);
7322 if (ret) {
7323 dev_info(&pf->pdev->dev,
7324 "couldn't fetch switch config, err %d, aq_err %d\n",
7325 ret, pf->hw.aq.asq_last_status);
7326 return ret;
7327 }
7328 i40e_pf_reset_stats(pf);
7329
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007330 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007331 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007332 struct i40e_vsi *vsi = NULL;
7333 u16 uplink_seid;
7334
7335 /* Set up the PF VSI associated with the PF's main VSI
7336 * that is already in the HW switch
7337 */
7338 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
7339 uplink_seid = pf->veb[pf->lan_veb]->seid;
7340 else
7341 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007342 if (pf->lan_vsi == I40E_NO_VSI)
7343 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
7344 else if (reinit)
7345 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007346 if (!vsi) {
7347 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
7348 i40e_fdir_teardown(pf);
7349 return -EAGAIN;
7350 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007351 } else {
7352 /* force a reset of TC and queue layout configurations */
7353 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
7354 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
7355 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
7356 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
7357 }
7358 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
7359
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007360 i40e_fdir_sb_setup(pf);
7361
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007362 /* Setup static PF queue filter control settings */
7363 ret = i40e_setup_pf_filter_control(pf);
7364 if (ret) {
7365 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
7366 ret);
7367 /* Failure here should not stop continuing other steps */
7368 }
7369
7370 /* enable RSS in the HW, even for only one queue, as the stack can use
7371 * the hash
7372 */
7373 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
7374 i40e_config_rss(pf);
7375
7376 /* fill in link information and enable LSE reporting */
7377 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
7378 i40e_link_event(pf);
7379
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007380 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007381 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
7382 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007383 /* requested_mode is set in probe or by ethtool */
7384 if (!pf->fc_autoneg_status)
7385 goto no_autoneg;
7386
7387 if ((pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX) &&
7388 (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007389 pf->hw.fc.current_mode = I40E_FC_FULL;
7390 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX)
7391 pf->hw.fc.current_mode = I40E_FC_TX_PAUSE;
7392 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX)
7393 pf->hw.fc.current_mode = I40E_FC_RX_PAUSE;
7394 else
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007395 pf->hw.fc.current_mode = I40E_FC_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007396
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007397 /* sync the flow control settings with the auto-neg values */
7398 switch (pf->hw.fc.current_mode) {
7399 case I40E_FC_FULL:
7400 txfc = 1;
7401 rxfc = 1;
7402 break;
7403 case I40E_FC_TX_PAUSE:
7404 txfc = 1;
7405 rxfc = 0;
7406 break;
7407 case I40E_FC_RX_PAUSE:
7408 txfc = 0;
7409 rxfc = 1;
7410 break;
7411 case I40E_FC_NONE:
7412 case I40E_FC_DEFAULT:
7413 txfc = 0;
7414 rxfc = 0;
7415 break;
7416 case I40E_FC_PFC:
7417 /* TBD */
7418 break;
7419 /* no default case, we have to handle all possibilities here */
7420 }
7421
7422 wr32(&pf->hw, I40E_PRTDCB_FCCFG, txfc << I40E_PRTDCB_FCCFG_TFCE_SHIFT);
7423
7424 rxfc_reg = rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
7425 ~I40E_PRTDCB_MFLCN_RFCE_MASK;
7426 rxfc_reg |= (rxfc << I40E_PRTDCB_MFLCN_RFCE_SHIFT);
7427
7428 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rxfc_reg);
7429
7430 goto fc_complete;
7431
7432no_autoneg:
7433 /* disable L2 flow control, user can turn it on if they wish */
7434 wr32(&pf->hw, I40E_PRTDCB_FCCFG, 0);
7435 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
7436 ~I40E_PRTDCB_MFLCN_RFCE_MASK);
7437
7438fc_complete:
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00007439 i40e_ptp_init(pf);
7440
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007441 return ret;
7442}
7443
7444/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007445 * i40e_determine_queue_usage - Work out queue distribution
7446 * @pf: board private structure
7447 **/
7448static void i40e_determine_queue_usage(struct i40e_pf *pf)
7449{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007450 int queues_left;
7451
7452 pf->num_lan_qps = 0;
7453 pf->num_tc_qps = rounddown_pow_of_two(pf->num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007454
7455 /* Find the max queues to be put into basic use. We'll always be
7456 * using TC0, whether or not DCB is running, and TC0 will get the
7457 * big RSS set.
7458 */
7459 queues_left = pf->hw.func_caps.num_tx_qp;
7460
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007461 if ((queues_left == 1) ||
7462 !(pf->flags & I40E_FLAG_MSIX_ENABLED) ||
7463 !(pf->flags & (I40E_FLAG_RSS_ENABLED | I40E_FLAG_FD_SB_ENABLED |
7464 I40E_FLAG_DCB_ENABLED))) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007465 /* one qp for PF, no queues for anything else */
7466 queues_left = 0;
7467 pf->rss_size = pf->num_lan_qps = 1;
7468
7469 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007470 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
7471 I40E_FLAG_FD_SB_ENABLED |
7472 I40E_FLAG_FD_ATR_ENABLED |
7473 I40E_FLAG_DCB_ENABLED |
7474 I40E_FLAG_SRIOV_ENABLED |
7475 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007476 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007477 /* Not enough queues for all TCs */
7478 if ((pf->flags & I40E_FLAG_DCB_ENABLED) &&
7479 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
7480 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7481 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
7482 }
7483 pf->num_lan_qps = pf->rss_size_max;
7484 queues_left -= pf->num_lan_qps;
7485 }
7486
7487 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7488 if (queues_left > 1) {
7489 queues_left -= 1; /* save 1 queue for FD */
7490 } else {
7491 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7492 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
7493 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007494 }
7495
7496 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
7497 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007498 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
7499 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007500 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
7501 }
7502
7503 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7504 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
7505 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
7506 (queues_left / pf->num_vmdq_qps));
7507 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
7508 }
7509
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007510 pf->queues_left = queues_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007511 return;
7512}
7513
7514/**
7515 * i40e_setup_pf_filter_control - Setup PF static filter control
7516 * @pf: PF to be setup
7517 *
7518 * i40e_setup_pf_filter_control sets up a pf's initial filter control
7519 * settings. If PE/FCoE are enabled then it will also set the per PF
7520 * based filter sizes required for them. It also enables Flow director,
7521 * ethertype and macvlan type filter settings for the pf.
7522 *
7523 * Returns 0 on success, negative on failure
7524 **/
7525static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
7526{
7527 struct i40e_filter_control_settings *settings = &pf->filter_settings;
7528
7529 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
7530
7531 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007532 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007533 settings->enable_fdir = true;
7534
7535 /* Ethtype and MACVLAN filters enabled for PF */
7536 settings->enable_ethtype = true;
7537 settings->enable_macvlan = true;
7538
7539 if (i40e_set_filter_control(&pf->hw, settings))
7540 return -ENOENT;
7541
7542 return 0;
7543}
7544
7545/**
7546 * i40e_probe - Device initialization routine
7547 * @pdev: PCI device information struct
7548 * @ent: entry in i40e_pci_tbl
7549 *
7550 * i40e_probe initializes a pf identified by a pci_dev structure.
7551 * The OS initialization, configuring of the pf private structure,
7552 * and a hardware reset occur.
7553 *
7554 * Returns 0 on success, negative on failure
7555 **/
7556static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
7557{
7558 struct i40e_driver_version dv;
7559 struct i40e_pf *pf;
7560 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00007561 static u16 pfs_found;
Catherine Sullivand4dfb812013-11-28 06:39:21 +00007562 u16 link_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007563 int err = 0;
7564 u32 len;
7565
7566 err = pci_enable_device_mem(pdev);
7567 if (err)
7568 return err;
7569
7570 /* set up for high or low dma */
7571 if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64))) {
7572 /* coherent mask for the same size will always succeed if
7573 * dma_set_mask does
7574 */
7575 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
7576 } else if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
7577 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
7578 } else {
7579 dev_err(&pdev->dev, "DMA configuration failed: %d\n", err);
7580 err = -EIO;
7581 goto err_dma;
7582 }
7583
7584 /* set up pci connections */
7585 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
7586 IORESOURCE_MEM), i40e_driver_name);
7587 if (err) {
7588 dev_info(&pdev->dev,
7589 "pci_request_selected_regions failed %d\n", err);
7590 goto err_pci_reg;
7591 }
7592
7593 pci_enable_pcie_error_reporting(pdev);
7594 pci_set_master(pdev);
7595
7596 /* Now that we have a PCI connection, we need to do the
7597 * low level device setup. This is primarily setting up
7598 * the Admin Queue structures and then querying for the
7599 * device's current profile information.
7600 */
7601 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
7602 if (!pf) {
7603 err = -ENOMEM;
7604 goto err_pf_alloc;
7605 }
7606 pf->next_vsi = 0;
7607 pf->pdev = pdev;
7608 set_bit(__I40E_DOWN, &pf->state);
7609
7610 hw = &pf->hw;
7611 hw->back = pf;
7612 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
7613 pci_resource_len(pdev, 0));
7614 if (!hw->hw_addr) {
7615 err = -EIO;
7616 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
7617 (unsigned int)pci_resource_start(pdev, 0),
7618 (unsigned int)pci_resource_len(pdev, 0), err);
7619 goto err_ioremap;
7620 }
7621 hw->vendor_id = pdev->vendor;
7622 hw->device_id = pdev->device;
7623 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
7624 hw->subsystem_vendor_id = pdev->subsystem_vendor;
7625 hw->subsystem_device_id = pdev->subsystem_device;
7626 hw->bus.device = PCI_SLOT(pdev->devfn);
7627 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00007628 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007629
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00007630 /* do a special CORER for clearing PXE mode once at init */
7631 if (hw->revision_id == 0 &&
7632 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
7633 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
7634 i40e_flush(hw);
7635 msleep(200);
7636 pf->corer_count++;
7637
7638 i40e_clear_pxe_mode(hw);
7639 }
7640
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007641 /* Reset here to make sure all is clean and to define PF 'n' */
7642 err = i40e_pf_reset(hw);
7643 if (err) {
7644 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
7645 goto err_pf_reset;
7646 }
7647 pf->pfr_count++;
7648
7649 hw->aq.num_arq_entries = I40E_AQ_LEN;
7650 hw->aq.num_asq_entries = I40E_AQ_LEN;
7651 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
7652 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
7653 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
7654 snprintf(pf->misc_int_name, sizeof(pf->misc_int_name) - 1,
7655 "%s-pf%d:misc",
7656 dev_driver_string(&pf->pdev->dev), pf->hw.pf_id);
7657
7658 err = i40e_init_shared_code(hw);
7659 if (err) {
7660 dev_info(&pdev->dev, "init_shared_code failed: %d\n", err);
7661 goto err_pf_reset;
7662 }
7663
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007664 /* set up a default setting for link flow control */
7665 pf->hw.fc.requested_mode = I40E_FC_NONE;
7666
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007667 err = i40e_init_adminq(hw);
7668 dev_info(&pdev->dev, "%s\n", i40e_fw_version_str(hw));
Anjali Singhai jainfe310702013-11-16 10:00:37 +00007669 if (((hw->nvm.version & I40E_NVM_VERSION_HI_MASK)
7670 >> I40E_NVM_VERSION_HI_SHIFT) != I40E_CURRENT_NVM_VERSION_HI) {
7671 dev_info(&pdev->dev,
7672 "warning: NVM version not supported, supported version: %02x.%02x\n",
7673 I40E_CURRENT_NVM_VERSION_HI,
7674 I40E_CURRENT_NVM_VERSION_LO);
7675 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007676 if (err) {
7677 dev_info(&pdev->dev,
7678 "init_adminq failed: %d expecting API %02x.%02x\n",
7679 err,
7680 I40E_FW_API_VERSION_MAJOR, I40E_FW_API_VERSION_MINOR);
7681 goto err_pf_reset;
7682 }
7683
Shannon Nelson6ff4ef82013-12-21 05:44:49 +00007684 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007685 err = i40e_get_capabilities(pf);
7686 if (err)
7687 goto err_adminq_setup;
7688
7689 err = i40e_sw_init(pf);
7690 if (err) {
7691 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
7692 goto err_sw_init;
7693 }
7694
7695 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
7696 hw->func_caps.num_rx_qp,
7697 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
7698 if (err) {
7699 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
7700 goto err_init_lan_hmc;
7701 }
7702
7703 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
7704 if (err) {
7705 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
7706 err = -ENOENT;
7707 goto err_configure_lan_hmc;
7708 }
7709
7710 i40e_get_mac_addr(hw, hw->mac.addr);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +00007711 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007712 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
7713 err = -EIO;
7714 goto err_mac_addr;
7715 }
7716 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
7717 memcpy(hw->mac.perm_addr, hw->mac.addr, ETH_ALEN);
7718
7719 pci_set_drvdata(pdev, pf);
7720 pci_save_state(pdev);
7721
7722 /* set up periodic task facility */
7723 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
7724 pf->service_timer_period = HZ;
7725
7726 INIT_WORK(&pf->service_task, i40e_service_task);
7727 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
7728 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
7729 pf->link_check_timeout = jiffies;
7730
Shannon Nelson8e2773a2013-11-28 06:39:22 +00007731 /* WoL defaults to disabled */
7732 pf->wol_en = false;
7733 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
7734
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007735 /* set up the main switch operations */
7736 i40e_determine_queue_usage(pf);
7737 i40e_init_interrupt_scheme(pf);
7738
7739 /* Set up the *vsi struct based on the number of VSIs in the HW,
7740 * and set up our local tracking of the MAIN PF vsi.
7741 */
7742 len = sizeof(struct i40e_vsi *) * pf->hw.func_caps.num_vsis;
7743 pf->vsi = kzalloc(len, GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +00007744 if (!pf->vsi) {
7745 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007746 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +00007747 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007748
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007749 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007750 if (err) {
7751 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
7752 goto err_vsis;
7753 }
7754
7755 /* The main driver is (mostly) up and happy. We need to set this state
7756 * before setting up the misc vector or we get a race and the vector
7757 * ends up disabled forever.
7758 */
7759 clear_bit(__I40E_DOWN, &pf->state);
7760
7761 /* In case of MSIX we are going to setup the misc vector right here
7762 * to handle admin queue events etc. In case of legacy and MSI
7763 * the misc functionality and queue processing is combined in
7764 * the same vector and that gets setup at open.
7765 */
7766 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
7767 err = i40e_setup_misc_vector(pf);
7768 if (err) {
7769 dev_info(&pdev->dev,
7770 "setup of misc vector failed: %d\n", err);
7771 goto err_vsis;
7772 }
7773 }
7774
7775 /* prep for VF support */
7776 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
7777 (pf->flags & I40E_FLAG_MSIX_ENABLED)) {
7778 u32 val;
7779
7780 /* disable link interrupts for VFs */
7781 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
7782 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
7783 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
7784 i40e_flush(hw);
7785 }
7786
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00007787 pfs_found++;
7788
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007789 i40e_dbg_pf_init(pf);
7790
7791 /* tell the firmware that we're starting */
7792 dv.major_version = DRV_VERSION_MAJOR;
7793 dv.minor_version = DRV_VERSION_MINOR;
7794 dv.build_version = DRV_VERSION_BUILD;
7795 dv.subbuild_version = 0;
7796 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
7797
7798 /* since everything's happy, start the service_task timer */
7799 mod_timer(&pf->service_timer,
7800 round_jiffies(jiffies + pf->service_timer_period));
7801
Catherine Sullivand4dfb812013-11-28 06:39:21 +00007802 /* Get the negotiated link width and speed from PCI config space */
7803 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA, &link_status);
7804
7805 i40e_set_pci_config_data(hw, link_status);
7806
7807 dev_info(&pdev->dev, "PCI Express: %s %s\n",
7808 (hw->bus.speed == i40e_bus_speed_8000 ? "Speed 8.0GT/s" :
7809 hw->bus.speed == i40e_bus_speed_5000 ? "Speed 5.0GT/s" :
7810 hw->bus.speed == i40e_bus_speed_2500 ? "Speed 2.5GT/s" :
7811 "Unknown"),
7812 (hw->bus.width == i40e_bus_width_pcie_x8 ? "Width x8" :
7813 hw->bus.width == i40e_bus_width_pcie_x4 ? "Width x4" :
7814 hw->bus.width == i40e_bus_width_pcie_x2 ? "Width x2" :
7815 hw->bus.width == i40e_bus_width_pcie_x1 ? "Width x1" :
7816 "Unknown"));
7817
7818 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
7819 hw->bus.speed < i40e_bus_speed_8000) {
7820 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
7821 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
7822 }
7823
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007824 return 0;
7825
7826 /* Unwind what we've done if something failed in the setup */
7827err_vsis:
7828 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007829 i40e_clear_interrupt_scheme(pf);
7830 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +00007831err_switch_setup:
7832 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007833 del_timer_sync(&pf->service_timer);
7834err_mac_addr:
7835err_configure_lan_hmc:
7836 (void)i40e_shutdown_lan_hmc(hw);
7837err_init_lan_hmc:
7838 kfree(pf->qp_pile);
7839 kfree(pf->irq_pile);
7840err_sw_init:
7841err_adminq_setup:
7842 (void)i40e_shutdown_adminq(hw);
7843err_pf_reset:
7844 iounmap(hw->hw_addr);
7845err_ioremap:
7846 kfree(pf);
7847err_pf_alloc:
7848 pci_disable_pcie_error_reporting(pdev);
7849 pci_release_selected_regions(pdev,
7850 pci_select_bars(pdev, IORESOURCE_MEM));
7851err_pci_reg:
7852err_dma:
7853 pci_disable_device(pdev);
7854 return err;
7855}
7856
7857/**
7858 * i40e_remove - Device removal routine
7859 * @pdev: PCI device information struct
7860 *
7861 * i40e_remove is called by the PCI subsystem to alert the driver
7862 * that is should release a PCI device. This could be caused by a
7863 * Hot-Plug event, or because the driver is going to be removed from
7864 * memory.
7865 **/
7866static void i40e_remove(struct pci_dev *pdev)
7867{
7868 struct i40e_pf *pf = pci_get_drvdata(pdev);
7869 i40e_status ret_code;
7870 u32 reg;
7871 int i;
7872
7873 i40e_dbg_pf_exit(pf);
7874
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00007875 i40e_ptp_stop(pf);
7876
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007877 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
7878 i40e_free_vfs(pf);
7879 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
7880 }
7881
7882 /* no more scheduling of any task */
7883 set_bit(__I40E_DOWN, &pf->state);
7884 del_timer_sync(&pf->service_timer);
7885 cancel_work_sync(&pf->service_task);
7886
7887 i40e_fdir_teardown(pf);
7888
7889 /* If there is a switch structure or any orphans, remove them.
7890 * This will leave only the PF's VSI remaining.
7891 */
7892 for (i = 0; i < I40E_MAX_VEB; i++) {
7893 if (!pf->veb[i])
7894 continue;
7895
7896 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
7897 pf->veb[i]->uplink_seid == 0)
7898 i40e_switch_branch_release(pf->veb[i]);
7899 }
7900
7901 /* Now we can shutdown the PF's VSI, just before we kill
7902 * adminq and hmc.
7903 */
7904 if (pf->vsi[pf->lan_vsi])
7905 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
7906
7907 i40e_stop_misc_vector(pf);
7908 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
7909 synchronize_irq(pf->msix_entries[0].vector);
7910 free_irq(pf->msix_entries[0].vector, pf);
7911 }
7912
7913 /* shutdown and destroy the HMC */
7914 ret_code = i40e_shutdown_lan_hmc(&pf->hw);
7915 if (ret_code)
7916 dev_warn(&pdev->dev,
7917 "Failed to destroy the HMC resources: %d\n", ret_code);
7918
7919 /* shutdown the adminq */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007920 ret_code = i40e_shutdown_adminq(&pf->hw);
7921 if (ret_code)
7922 dev_warn(&pdev->dev,
7923 "Failed to destroy the Admin Queue resources: %d\n",
7924 ret_code);
7925
7926 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
7927 i40e_clear_interrupt_scheme(pf);
7928 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
7929 if (pf->vsi[i]) {
7930 i40e_vsi_clear_rings(pf->vsi[i]);
7931 i40e_vsi_clear(pf->vsi[i]);
7932 pf->vsi[i] = NULL;
7933 }
7934 }
7935
7936 for (i = 0; i < I40E_MAX_VEB; i++) {
7937 kfree(pf->veb[i]);
7938 pf->veb[i] = NULL;
7939 }
7940
7941 kfree(pf->qp_pile);
7942 kfree(pf->irq_pile);
7943 kfree(pf->sw_config);
7944 kfree(pf->vsi);
7945
7946 /* force a PF reset to clean anything leftover */
7947 reg = rd32(&pf->hw, I40E_PFGEN_CTRL);
7948 wr32(&pf->hw, I40E_PFGEN_CTRL, (reg | I40E_PFGEN_CTRL_PFSWR_MASK));
7949 i40e_flush(&pf->hw);
7950
7951 iounmap(pf->hw.hw_addr);
7952 kfree(pf);
7953 pci_release_selected_regions(pdev,
7954 pci_select_bars(pdev, IORESOURCE_MEM));
7955
7956 pci_disable_pcie_error_reporting(pdev);
7957 pci_disable_device(pdev);
7958}
7959
7960/**
7961 * i40e_pci_error_detected - warning that something funky happened in PCI land
7962 * @pdev: PCI device information struct
7963 *
7964 * Called to warn that something happened and the error handling steps
7965 * are in progress. Allows the driver to quiesce things, be ready for
7966 * remediation.
7967 **/
7968static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
7969 enum pci_channel_state error)
7970{
7971 struct i40e_pf *pf = pci_get_drvdata(pdev);
7972
7973 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
7974
7975 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +00007976 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
7977 rtnl_lock();
7978 i40e_prep_for_reset(pf);
7979 rtnl_unlock();
7980 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007981
7982 /* Request a slot reset */
7983 return PCI_ERS_RESULT_NEED_RESET;
7984}
7985
7986/**
7987 * i40e_pci_error_slot_reset - a PCI slot reset just happened
7988 * @pdev: PCI device information struct
7989 *
7990 * Called to find if the driver can work with the device now that
7991 * the pci slot has been reset. If a basic connection seems good
7992 * (registers are readable and have sane content) then return a
7993 * happy little PCI_ERS_RESULT_xxx.
7994 **/
7995static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
7996{
7997 struct i40e_pf *pf = pci_get_drvdata(pdev);
7998 pci_ers_result_t result;
7999 int err;
8000 u32 reg;
8001
8002 dev_info(&pdev->dev, "%s\n", __func__);
8003 if (pci_enable_device_mem(pdev)) {
8004 dev_info(&pdev->dev,
8005 "Cannot re-enable PCI device after reset.\n");
8006 result = PCI_ERS_RESULT_DISCONNECT;
8007 } else {
8008 pci_set_master(pdev);
8009 pci_restore_state(pdev);
8010 pci_save_state(pdev);
8011 pci_wake_from_d3(pdev, false);
8012
8013 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8014 if (reg == 0)
8015 result = PCI_ERS_RESULT_RECOVERED;
8016 else
8017 result = PCI_ERS_RESULT_DISCONNECT;
8018 }
8019
8020 err = pci_cleanup_aer_uncorrect_error_status(pdev);
8021 if (err) {
8022 dev_info(&pdev->dev,
8023 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
8024 err);
8025 /* non-fatal, continue */
8026 }
8027
8028 return result;
8029}
8030
8031/**
8032 * i40e_pci_error_resume - restart operations after PCI error recovery
8033 * @pdev: PCI device information struct
8034 *
8035 * Called to allow the driver to bring things back up after PCI error
8036 * and/or reset recovery has finished.
8037 **/
8038static void i40e_pci_error_resume(struct pci_dev *pdev)
8039{
8040 struct i40e_pf *pf = pci_get_drvdata(pdev);
8041
8042 dev_info(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008043 if (test_bit(__I40E_SUSPENDED, &pf->state))
8044 return;
8045
8046 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008047 i40e_handle_reset_warning(pf);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008048 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008049}
8050
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008051/**
8052 * i40e_shutdown - PCI callback for shutting down
8053 * @pdev: PCI device information struct
8054 **/
8055static void i40e_shutdown(struct pci_dev *pdev)
8056{
8057 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008058 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008059
8060 set_bit(__I40E_SUSPENDED, &pf->state);
8061 set_bit(__I40E_DOWN, &pf->state);
8062 rtnl_lock();
8063 i40e_prep_for_reset(pf);
8064 rtnl_unlock();
8065
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008066 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
8067 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
8068
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008069 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008070 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008071 pci_set_power_state(pdev, PCI_D3hot);
8072 }
8073}
8074
8075#ifdef CONFIG_PM
8076/**
8077 * i40e_suspend - PCI callback for moving to D3
8078 * @pdev: PCI device information struct
8079 **/
8080static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
8081{
8082 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008083 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008084
8085 set_bit(__I40E_SUSPENDED, &pf->state);
8086 set_bit(__I40E_DOWN, &pf->state);
8087 rtnl_lock();
8088 i40e_prep_for_reset(pf);
8089 rtnl_unlock();
8090
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008091 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
8092 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
8093
8094 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008095 pci_set_power_state(pdev, PCI_D3hot);
8096
8097 return 0;
8098}
8099
8100/**
8101 * i40e_resume - PCI callback for waking up from D3
8102 * @pdev: PCI device information struct
8103 **/
8104static int i40e_resume(struct pci_dev *pdev)
8105{
8106 struct i40e_pf *pf = pci_get_drvdata(pdev);
8107 u32 err;
8108
8109 pci_set_power_state(pdev, PCI_D0);
8110 pci_restore_state(pdev);
8111 /* pci_restore_state() clears dev->state_saves, so
8112 * call pci_save_state() again to restore it.
8113 */
8114 pci_save_state(pdev);
8115
8116 err = pci_enable_device_mem(pdev);
8117 if (err) {
8118 dev_err(&pdev->dev,
8119 "%s: Cannot enable PCI device from suspend\n",
8120 __func__);
8121 return err;
8122 }
8123 pci_set_master(pdev);
8124
8125 /* no wakeup events while running */
8126 pci_wake_from_d3(pdev, false);
8127
8128 /* handling the reset will rebuild the device state */
8129 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
8130 clear_bit(__I40E_DOWN, &pf->state);
8131 rtnl_lock();
8132 i40e_reset_and_rebuild(pf, false);
8133 rtnl_unlock();
8134 }
8135
8136 return 0;
8137}
8138
8139#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008140static const struct pci_error_handlers i40e_err_handler = {
8141 .error_detected = i40e_pci_error_detected,
8142 .slot_reset = i40e_pci_error_slot_reset,
8143 .resume = i40e_pci_error_resume,
8144};
8145
8146static struct pci_driver i40e_driver = {
8147 .name = i40e_driver_name,
8148 .id_table = i40e_pci_tbl,
8149 .probe = i40e_probe,
8150 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008151#ifdef CONFIG_PM
8152 .suspend = i40e_suspend,
8153 .resume = i40e_resume,
8154#endif
8155 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008156 .err_handler = &i40e_err_handler,
8157 .sriov_configure = i40e_pci_sriov_configure,
8158};
8159
8160/**
8161 * i40e_init_module - Driver registration routine
8162 *
8163 * i40e_init_module is the first routine called when the driver is
8164 * loaded. All it does is register with the PCI subsystem.
8165 **/
8166static int __init i40e_init_module(void)
8167{
8168 pr_info("%s: %s - version %s\n", i40e_driver_name,
8169 i40e_driver_string, i40e_driver_version_str);
8170 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
8171 i40e_dbg_init();
8172 return pci_register_driver(&i40e_driver);
8173}
8174module_init(i40e_init_module);
8175
8176/**
8177 * i40e_exit_module - Driver exit cleanup routine
8178 *
8179 * i40e_exit_module is called just before the driver is removed
8180 * from memory.
8181 **/
8182static void __exit i40e_exit_module(void)
8183{
8184 pci_unregister_driver(&i40e_driver);
8185 i40e_dbg_exit();
8186}
8187module_exit(i40e_exit_module);