blob: 6185856689bc615896db6b9a6eb4f149e9a03ed9 [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 Sullivan20628622014-02-06 05:51:14 +000041#define DRV_VERSION_BUILD 34
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);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080058static int i40e_veb_get_bw_info(struct i40e_veb *veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000059
60/* i40e_pci_tbl - PCI Device ID Table
61 *
62 * Last entry must be all 0s
63 *
64 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
65 * Class, Class Mask, private data (not used) }
66 */
67static DEFINE_PCI_DEVICE_TABLE(i40e_pci_tbl) = {
Shannon Nelsonab600852014-01-17 15:36:39 -080068 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
69 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X710), 0},
70 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
71 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_A), 0},
72 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
73 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
74 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_D), 0},
75 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
76 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
77 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000078 /* required last entry */
79 {0, }
80};
81MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
82
83#define I40E_MAX_VF_COUNT 128
84static int debug = -1;
85module_param(debug, int, 0);
86MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
87
88MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
89MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
90MODULE_LICENSE("GPL");
91MODULE_VERSION(DRV_VERSION);
92
93/**
94 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
95 * @hw: pointer to the HW structure
96 * @mem: ptr to mem struct to fill out
97 * @size: size of memory requested
98 * @alignment: what to align the allocation to
99 **/
100int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
101 u64 size, u32 alignment)
102{
103 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
104
105 mem->size = ALIGN(size, alignment);
106 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
107 &mem->pa, GFP_KERNEL);
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000108 if (!mem->va)
109 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000110
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000111 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000112}
113
114/**
115 * i40e_free_dma_mem_d - OS specific memory free for shared code
116 * @hw: pointer to the HW structure
117 * @mem: ptr to mem struct to free
118 **/
119int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
120{
121 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
122
123 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
124 mem->va = NULL;
125 mem->pa = 0;
126 mem->size = 0;
127
128 return 0;
129}
130
131/**
132 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
133 * @hw: pointer to the HW structure
134 * @mem: ptr to mem struct to fill out
135 * @size: size of memory requested
136 **/
137int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
138 u32 size)
139{
140 mem->size = size;
141 mem->va = kzalloc(size, GFP_KERNEL);
142
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000143 if (!mem->va)
144 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000145
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000146 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000147}
148
149/**
150 * i40e_free_virt_mem_d - OS specific memory free for shared code
151 * @hw: pointer to the HW structure
152 * @mem: ptr to mem struct to free
153 **/
154int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
155{
156 /* it's ok to kfree a NULL pointer */
157 kfree(mem->va);
158 mem->va = NULL;
159 mem->size = 0;
160
161 return 0;
162}
163
164/**
165 * i40e_get_lump - find a lump of free generic resource
166 * @pf: board private structure
167 * @pile: the pile of resource to search
168 * @needed: the number of items needed
169 * @id: an owner id to stick on the items assigned
170 *
171 * Returns the base item index of the lump, or negative for error
172 *
173 * The search_hint trick and lack of advanced fit-finding only work
174 * because we're highly likely to have all the same size lump requests.
175 * Linear search time and any fragmentation should be minimal.
176 **/
177static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
178 u16 needed, u16 id)
179{
180 int ret = -ENOMEM;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000181 int i, j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000182
183 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
184 dev_info(&pf->pdev->dev,
185 "param err: pile=%p needed=%d id=0x%04x\n",
186 pile, needed, id);
187 return -EINVAL;
188 }
189
190 /* start the linear search with an imperfect hint */
191 i = pile->search_hint;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000192 while (i < pile->num_entries) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000193 /* skip already allocated entries */
194 if (pile->list[i] & I40E_PILE_VALID_BIT) {
195 i++;
196 continue;
197 }
198
199 /* do we have enough in this lump? */
200 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
201 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
202 break;
203 }
204
205 if (j == needed) {
206 /* there was enough, so assign it to the requestor */
207 for (j = 0; j < needed; j++)
208 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
209 ret = i;
210 pile->search_hint = i + j;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000211 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000212 } else {
213 /* not enough, so skip over it and continue looking */
214 i += j;
215 }
216 }
217
218 return ret;
219}
220
221/**
222 * i40e_put_lump - return a lump of generic resource
223 * @pile: the pile of resource to search
224 * @index: the base item index
225 * @id: the owner id of the items assigned
226 *
227 * Returns the count of items in the lump
228 **/
229static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
230{
231 int valid_id = (id | I40E_PILE_VALID_BIT);
232 int count = 0;
233 int i;
234
235 if (!pile || index >= pile->num_entries)
236 return -EINVAL;
237
238 for (i = index;
239 i < pile->num_entries && pile->list[i] == valid_id;
240 i++) {
241 pile->list[i] = 0;
242 count++;
243 }
244
245 if (count && index < pile->search_hint)
246 pile->search_hint = index;
247
248 return count;
249}
250
251/**
252 * i40e_service_event_schedule - Schedule the service task to wake up
253 * @pf: board private structure
254 *
255 * If not already scheduled, this puts the task into the work queue
256 **/
257static void i40e_service_event_schedule(struct i40e_pf *pf)
258{
259 if (!test_bit(__I40E_DOWN, &pf->state) &&
260 !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) &&
261 !test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
262 schedule_work(&pf->service_task);
263}
264
265/**
266 * i40e_tx_timeout - Respond to a Tx Hang
267 * @netdev: network interface device structure
268 *
269 * If any port has noticed a Tx timeout, it is likely that the whole
270 * device is munged, not just the one netdev port, so go for the full
271 * reset.
272 **/
273static void i40e_tx_timeout(struct net_device *netdev)
274{
275 struct i40e_netdev_priv *np = netdev_priv(netdev);
276 struct i40e_vsi *vsi = np->vsi;
277 struct i40e_pf *pf = vsi->back;
278
279 pf->tx_timeout_count++;
280
281 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
282 pf->tx_timeout_recovery_level = 0;
283 pf->tx_timeout_last_recovery = jiffies;
284 netdev_info(netdev, "tx_timeout recovery level %d\n",
285 pf->tx_timeout_recovery_level);
286
287 switch (pf->tx_timeout_recovery_level) {
288 case 0:
289 /* disable and re-enable queues for the VSI */
290 if (in_interrupt()) {
291 set_bit(__I40E_REINIT_REQUESTED, &pf->state);
292 set_bit(__I40E_REINIT_REQUESTED, &vsi->state);
293 } else {
294 i40e_vsi_reinit_locked(vsi);
295 }
296 break;
297 case 1:
298 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
299 break;
300 case 2:
301 set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
302 break;
303 case 3:
304 set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
305 break;
306 default:
307 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
Akeem G Abodunrine108b0e2014-02-13 03:48:43 -0800308 set_bit(__I40E_DOWN, &vsi->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000309 i40e_down(vsi);
310 break;
311 }
312 i40e_service_event_schedule(pf);
313 pf->tx_timeout_recovery_level++;
314}
315
316/**
317 * i40e_release_rx_desc - Store the new tail and head values
318 * @rx_ring: ring to bump
319 * @val: new head index
320 **/
321static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
322{
323 rx_ring->next_to_use = val;
324
325 /* Force memory writes to complete before letting h/w
326 * know there are new descriptors to fetch. (Only
327 * applicable for weak-ordered memory model archs,
328 * such as IA-64).
329 */
330 wmb();
331 writel(val, rx_ring->tail);
332}
333
334/**
335 * i40e_get_vsi_stats_struct - Get System Network Statistics
336 * @vsi: the VSI we care about
337 *
338 * Returns the address of the device statistics structure.
339 * The statistics are actually updated from the service task.
340 **/
341struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
342{
343 return &vsi->net_stats;
344}
345
346/**
347 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
348 * @netdev: network interface device structure
349 *
350 * Returns the address of the device statistics structure.
351 * The statistics are actually updated from the service task.
352 **/
353static struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
354 struct net_device *netdev,
Alexander Duyck980e9b12013-09-28 06:01:03 +0000355 struct rtnl_link_stats64 *stats)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000356{
357 struct i40e_netdev_priv *np = netdev_priv(netdev);
358 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000359 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
360 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000361
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000362 if (test_bit(__I40E_DOWN, &vsi->state))
363 return stats;
364
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800365 if (!vsi->tx_rings)
366 return stats;
367
Alexander Duyck980e9b12013-09-28 06:01:03 +0000368 rcu_read_lock();
369 for (i = 0; i < vsi->num_queue_pairs; i++) {
370 struct i40e_ring *tx_ring, *rx_ring;
371 u64 bytes, packets;
372 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000373
Alexander Duyck980e9b12013-09-28 06:01:03 +0000374 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
375 if (!tx_ring)
376 continue;
377
378 do {
379 start = u64_stats_fetch_begin_bh(&tx_ring->syncp);
380 packets = tx_ring->stats.packets;
381 bytes = tx_ring->stats.bytes;
382 } while (u64_stats_fetch_retry_bh(&tx_ring->syncp, start));
383
384 stats->tx_packets += packets;
385 stats->tx_bytes += bytes;
386 rx_ring = &tx_ring[1];
387
388 do {
389 start = u64_stats_fetch_begin_bh(&rx_ring->syncp);
390 packets = rx_ring->stats.packets;
391 bytes = rx_ring->stats.bytes;
392 } while (u64_stats_fetch_retry_bh(&rx_ring->syncp, start));
393
394 stats->rx_packets += packets;
395 stats->rx_bytes += bytes;
396 }
397 rcu_read_unlock();
398
399 /* following stats updated by ixgbe_watchdog_task() */
400 stats->multicast = vsi_stats->multicast;
401 stats->tx_errors = vsi_stats->tx_errors;
402 stats->tx_dropped = vsi_stats->tx_dropped;
403 stats->rx_errors = vsi_stats->rx_errors;
404 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
405 stats->rx_length_errors = vsi_stats->rx_length_errors;
406
407 return stats;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000408}
409
410/**
411 * i40e_vsi_reset_stats - Resets all stats of the given vsi
412 * @vsi: the VSI to have its stats reset
413 **/
414void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
415{
416 struct rtnl_link_stats64 *ns;
417 int i;
418
419 if (!vsi)
420 return;
421
422 ns = i40e_get_vsi_stats_struct(vsi);
423 memset(ns, 0, sizeof(*ns));
424 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
425 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
426 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
Greg Rose8e9dca52013-12-18 13:45:53 +0000427 if (vsi->rx_rings && vsi->rx_rings[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000428 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000429 memset(&vsi->rx_rings[i]->stats, 0 ,
430 sizeof(vsi->rx_rings[i]->stats));
431 memset(&vsi->rx_rings[i]->rx_stats, 0 ,
432 sizeof(vsi->rx_rings[i]->rx_stats));
433 memset(&vsi->tx_rings[i]->stats, 0 ,
434 sizeof(vsi->tx_rings[i]->stats));
435 memset(&vsi->tx_rings[i]->tx_stats, 0,
436 sizeof(vsi->tx_rings[i]->tx_stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000437 }
Greg Rose8e9dca52013-12-18 13:45:53 +0000438 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000439 vsi->stat_offsets_loaded = false;
440}
441
442/**
443 * i40e_pf_reset_stats - Reset all of the stats for the given pf
444 * @pf: the PF to be reset
445 **/
446void i40e_pf_reset_stats(struct i40e_pf *pf)
447{
448 memset(&pf->stats, 0, sizeof(pf->stats));
449 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
450 pf->stat_offsets_loaded = false;
451}
452
453/**
454 * i40e_stat_update48 - read and update a 48 bit stat from the chip
455 * @hw: ptr to the hardware info
456 * @hireg: the high 32 bit reg to read
457 * @loreg: the low 32 bit reg to read
458 * @offset_loaded: has the initial offset been loaded yet
459 * @offset: ptr to current offset value
460 * @stat: ptr to the stat
461 *
462 * Since the device stats are not reset at PFReset, they likely will not
463 * be zeroed when the driver starts. We'll save the first values read
464 * and use them as offsets to be subtracted from the raw values in order
465 * to report stats that count from zero. In the process, we also manage
466 * the potential roll-over.
467 **/
468static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
469 bool offset_loaded, u64 *offset, u64 *stat)
470{
471 u64 new_data;
472
Shannon Nelsonab600852014-01-17 15:36:39 -0800473 if (hw->device_id == I40E_DEV_ID_QEMU) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000474 new_data = rd32(hw, loreg);
475 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
476 } else {
477 new_data = rd64(hw, loreg);
478 }
479 if (!offset_loaded)
480 *offset = new_data;
481 if (likely(new_data >= *offset))
482 *stat = new_data - *offset;
483 else
484 *stat = (new_data + ((u64)1 << 48)) - *offset;
485 *stat &= 0xFFFFFFFFFFFFULL;
486}
487
488/**
489 * i40e_stat_update32 - read and update a 32 bit stat from the chip
490 * @hw: ptr to the hardware info
491 * @reg: the hw reg to read
492 * @offset_loaded: has the initial offset been loaded yet
493 * @offset: ptr to current offset value
494 * @stat: ptr to the stat
495 **/
496static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
497 bool offset_loaded, u64 *offset, u64 *stat)
498{
499 u32 new_data;
500
501 new_data = rd32(hw, reg);
502 if (!offset_loaded)
503 *offset = new_data;
504 if (likely(new_data >= *offset))
505 *stat = (u32)(new_data - *offset);
506 else
507 *stat = (u32)((new_data + ((u64)1 << 32)) - *offset);
508}
509
510/**
511 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
512 * @vsi: the VSI to be updated
513 **/
514void i40e_update_eth_stats(struct i40e_vsi *vsi)
515{
516 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
517 struct i40e_pf *pf = vsi->back;
518 struct i40e_hw *hw = &pf->hw;
519 struct i40e_eth_stats *oes;
520 struct i40e_eth_stats *es; /* device's eth stats */
521
522 es = &vsi->eth_stats;
523 oes = &vsi->eth_stats_offsets;
524
525 /* Gather up the stats that the hw collects */
526 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
527 vsi->stat_offsets_loaded,
528 &oes->tx_errors, &es->tx_errors);
529 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
530 vsi->stat_offsets_loaded,
531 &oes->rx_discards, &es->rx_discards);
532
533 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
534 I40E_GLV_GORCL(stat_idx),
535 vsi->stat_offsets_loaded,
536 &oes->rx_bytes, &es->rx_bytes);
537 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
538 I40E_GLV_UPRCL(stat_idx),
539 vsi->stat_offsets_loaded,
540 &oes->rx_unicast, &es->rx_unicast);
541 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
542 I40E_GLV_MPRCL(stat_idx),
543 vsi->stat_offsets_loaded,
544 &oes->rx_multicast, &es->rx_multicast);
545 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
546 I40E_GLV_BPRCL(stat_idx),
547 vsi->stat_offsets_loaded,
548 &oes->rx_broadcast, &es->rx_broadcast);
549
550 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
551 I40E_GLV_GOTCL(stat_idx),
552 vsi->stat_offsets_loaded,
553 &oes->tx_bytes, &es->tx_bytes);
554 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
555 I40E_GLV_UPTCL(stat_idx),
556 vsi->stat_offsets_loaded,
557 &oes->tx_unicast, &es->tx_unicast);
558 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
559 I40E_GLV_MPTCL(stat_idx),
560 vsi->stat_offsets_loaded,
561 &oes->tx_multicast, &es->tx_multicast);
562 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
563 I40E_GLV_BPTCL(stat_idx),
564 vsi->stat_offsets_loaded,
565 &oes->tx_broadcast, &es->tx_broadcast);
566 vsi->stat_offsets_loaded = true;
567}
568
569/**
570 * i40e_update_veb_stats - Update Switch component statistics
571 * @veb: the VEB being updated
572 **/
573static void i40e_update_veb_stats(struct i40e_veb *veb)
574{
575 struct i40e_pf *pf = veb->pf;
576 struct i40e_hw *hw = &pf->hw;
577 struct i40e_eth_stats *oes;
578 struct i40e_eth_stats *es; /* device's eth stats */
579 int idx = 0;
580
581 idx = veb->stats_idx;
582 es = &veb->stats;
583 oes = &veb->stats_offsets;
584
585 /* Gather up the stats that the hw collects */
586 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
587 veb->stat_offsets_loaded,
588 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000589 if (hw->revision_id > 0)
590 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
591 veb->stat_offsets_loaded,
592 &oes->rx_unknown_protocol,
593 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000594 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
595 veb->stat_offsets_loaded,
596 &oes->rx_bytes, &es->rx_bytes);
597 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
598 veb->stat_offsets_loaded,
599 &oes->rx_unicast, &es->rx_unicast);
600 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
601 veb->stat_offsets_loaded,
602 &oes->rx_multicast, &es->rx_multicast);
603 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
604 veb->stat_offsets_loaded,
605 &oes->rx_broadcast, &es->rx_broadcast);
606
607 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
608 veb->stat_offsets_loaded,
609 &oes->tx_bytes, &es->tx_bytes);
610 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
611 veb->stat_offsets_loaded,
612 &oes->tx_unicast, &es->tx_unicast);
613 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
614 veb->stat_offsets_loaded,
615 &oes->tx_multicast, &es->tx_multicast);
616 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
617 veb->stat_offsets_loaded,
618 &oes->tx_broadcast, &es->tx_broadcast);
619 veb->stat_offsets_loaded = true;
620}
621
622/**
623 * i40e_update_link_xoff_rx - Update XOFF received in link flow control mode
624 * @pf: the corresponding PF
625 *
626 * Update the Rx XOFF counter (PAUSE frames) in link flow control mode
627 **/
628static void i40e_update_link_xoff_rx(struct i40e_pf *pf)
629{
630 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
631 struct i40e_hw_port_stats *nsd = &pf->stats;
632 struct i40e_hw *hw = &pf->hw;
633 u64 xoff = 0;
634 u16 i, v;
635
636 if ((hw->fc.current_mode != I40E_FC_FULL) &&
637 (hw->fc.current_mode != I40E_FC_RX_PAUSE))
638 return;
639
640 xoff = nsd->link_xoff_rx;
641 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
642 pf->stat_offsets_loaded,
643 &osd->link_xoff_rx, &nsd->link_xoff_rx);
644
645 /* No new LFC xoff rx */
646 if (!(nsd->link_xoff_rx - xoff))
647 return;
648
649 /* Clear the __I40E_HANG_CHECK_ARMED bit for all Tx rings */
650 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
651 struct i40e_vsi *vsi = pf->vsi[v];
652
653 if (!vsi)
654 continue;
655
656 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000657 struct i40e_ring *ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000658 clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
659 }
660 }
661}
662
663/**
664 * i40e_update_prio_xoff_rx - Update XOFF received in PFC mode
665 * @pf: the corresponding PF
666 *
667 * Update the Rx XOFF counter (PAUSE frames) in PFC mode
668 **/
669static void i40e_update_prio_xoff_rx(struct i40e_pf *pf)
670{
671 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
672 struct i40e_hw_port_stats *nsd = &pf->stats;
673 bool xoff[I40E_MAX_TRAFFIC_CLASS] = {false};
674 struct i40e_dcbx_config *dcb_cfg;
675 struct i40e_hw *hw = &pf->hw;
676 u16 i, v;
677 u8 tc;
678
679 dcb_cfg = &hw->local_dcbx_config;
680
681 /* See if DCB enabled with PFC TC */
682 if (!(pf->flags & I40E_FLAG_DCB_ENABLED) ||
683 !(dcb_cfg->pfc.pfcenable)) {
684 i40e_update_link_xoff_rx(pf);
685 return;
686 }
687
688 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
689 u64 prio_xoff = nsd->priority_xoff_rx[i];
690 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
691 pf->stat_offsets_loaded,
692 &osd->priority_xoff_rx[i],
693 &nsd->priority_xoff_rx[i]);
694
695 /* No new PFC xoff rx */
696 if (!(nsd->priority_xoff_rx[i] - prio_xoff))
697 continue;
698 /* Get the TC for given priority */
699 tc = dcb_cfg->etscfg.prioritytable[i];
700 xoff[tc] = true;
701 }
702
703 /* Clear the __I40E_HANG_CHECK_ARMED bit for Tx rings */
704 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
705 struct i40e_vsi *vsi = pf->vsi[v];
706
707 if (!vsi)
708 continue;
709
710 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000711 struct i40e_ring *ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000712
713 tc = ring->dcb_tc;
714 if (xoff[tc])
715 clear_bit(__I40E_HANG_CHECK_ARMED,
716 &ring->state);
717 }
718 }
719}
720
721/**
722 * i40e_update_stats - Update the board statistics counters.
723 * @vsi: the VSI to be updated
724 *
725 * There are a few instances where we store the same stat in a
726 * couple of different structs. This is partly because we have
727 * the netdev stats that need to be filled out, which is slightly
728 * different from the "eth_stats" defined by the chip and used in
729 * VF communications. We sort it all out here in a central place.
730 **/
731void i40e_update_stats(struct i40e_vsi *vsi)
732{
733 struct i40e_pf *pf = vsi->back;
734 struct i40e_hw *hw = &pf->hw;
735 struct rtnl_link_stats64 *ons;
736 struct rtnl_link_stats64 *ns; /* netdev stats */
737 struct i40e_eth_stats *oes;
738 struct i40e_eth_stats *es; /* device's eth stats */
739 u32 tx_restart, tx_busy;
740 u32 rx_page, rx_buf;
741 u64 rx_p, rx_b;
742 u64 tx_p, tx_b;
743 int i;
744 u16 q;
745
746 if (test_bit(__I40E_DOWN, &vsi->state) ||
747 test_bit(__I40E_CONFIG_BUSY, &pf->state))
748 return;
749
750 ns = i40e_get_vsi_stats_struct(vsi);
751 ons = &vsi->net_stats_offsets;
752 es = &vsi->eth_stats;
753 oes = &vsi->eth_stats_offsets;
754
755 /* Gather up the netdev and vsi stats that the driver collects
756 * on the fly during packet processing
757 */
758 rx_b = rx_p = 0;
759 tx_b = tx_p = 0;
760 tx_restart = tx_busy = 0;
761 rx_page = 0;
762 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000763 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000764 for (q = 0; q < vsi->num_queue_pairs; q++) {
765 struct i40e_ring *p;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000766 u64 bytes, packets;
767 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000768
Alexander Duyck980e9b12013-09-28 06:01:03 +0000769 /* locate Tx ring */
770 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000771
Alexander Duyck980e9b12013-09-28 06:01:03 +0000772 do {
773 start = u64_stats_fetch_begin_bh(&p->syncp);
774 packets = p->stats.packets;
775 bytes = p->stats.bytes;
776 } while (u64_stats_fetch_retry_bh(&p->syncp, start));
777 tx_b += bytes;
778 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000779 tx_restart += p->tx_stats.restart_queue;
780 tx_busy += p->tx_stats.tx_busy;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000781
782 /* Rx queue is part of the same block as Tx queue */
783 p = &p[1];
784 do {
785 start = u64_stats_fetch_begin_bh(&p->syncp);
786 packets = p->stats.packets;
787 bytes = p->stats.bytes;
788 } while (u64_stats_fetch_retry_bh(&p->syncp, start));
789 rx_b += bytes;
790 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000791 rx_buf += p->rx_stats.alloc_buff_failed;
792 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000793 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000794 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000795 vsi->tx_restart = tx_restart;
796 vsi->tx_busy = tx_busy;
797 vsi->rx_page_failed = rx_page;
798 vsi->rx_buf_failed = rx_buf;
799
800 ns->rx_packets = rx_p;
801 ns->rx_bytes = rx_b;
802 ns->tx_packets = tx_p;
803 ns->tx_bytes = tx_b;
804
805 i40e_update_eth_stats(vsi);
806 /* update netdev stats from eth stats */
807 ons->rx_errors = oes->rx_errors;
808 ns->rx_errors = es->rx_errors;
809 ons->tx_errors = oes->tx_errors;
810 ns->tx_errors = es->tx_errors;
811 ons->multicast = oes->rx_multicast;
812 ns->multicast = es->rx_multicast;
813 ons->tx_dropped = oes->tx_discards;
814 ns->tx_dropped = es->tx_discards;
815
816 /* Get the port data only if this is the main PF VSI */
817 if (vsi == pf->vsi[pf->lan_vsi]) {
818 struct i40e_hw_port_stats *nsd = &pf->stats;
819 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
820
821 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
822 I40E_GLPRT_GORCL(hw->port),
823 pf->stat_offsets_loaded,
824 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
825 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
826 I40E_GLPRT_GOTCL(hw->port),
827 pf->stat_offsets_loaded,
828 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
829 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
830 pf->stat_offsets_loaded,
831 &osd->eth.rx_discards,
832 &nsd->eth.rx_discards);
833 i40e_stat_update32(hw, I40E_GLPRT_TDPC(hw->port),
834 pf->stat_offsets_loaded,
835 &osd->eth.tx_discards,
836 &nsd->eth.tx_discards);
837 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
838 I40E_GLPRT_MPRCL(hw->port),
839 pf->stat_offsets_loaded,
840 &osd->eth.rx_multicast,
841 &nsd->eth.rx_multicast);
842
843 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
844 pf->stat_offsets_loaded,
845 &osd->tx_dropped_link_down,
846 &nsd->tx_dropped_link_down);
847
848 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
849 pf->stat_offsets_loaded,
850 &osd->crc_errors, &nsd->crc_errors);
851 ns->rx_crc_errors = nsd->crc_errors;
852
853 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
854 pf->stat_offsets_loaded,
855 &osd->illegal_bytes, &nsd->illegal_bytes);
856 ns->rx_errors = nsd->crc_errors
857 + nsd->illegal_bytes;
858
859 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
860 pf->stat_offsets_loaded,
861 &osd->mac_local_faults,
862 &nsd->mac_local_faults);
863 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
864 pf->stat_offsets_loaded,
865 &osd->mac_remote_faults,
866 &nsd->mac_remote_faults);
867
868 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
869 pf->stat_offsets_loaded,
870 &osd->rx_length_errors,
871 &nsd->rx_length_errors);
872 ns->rx_length_errors = nsd->rx_length_errors;
873
874 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
875 pf->stat_offsets_loaded,
876 &osd->link_xon_rx, &nsd->link_xon_rx);
877 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
878 pf->stat_offsets_loaded,
879 &osd->link_xon_tx, &nsd->link_xon_tx);
880 i40e_update_prio_xoff_rx(pf); /* handles I40E_GLPRT_LXOFFRXC */
881 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
882 pf->stat_offsets_loaded,
883 &osd->link_xoff_tx, &nsd->link_xoff_tx);
884
885 for (i = 0; i < 8; i++) {
886 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
887 pf->stat_offsets_loaded,
888 &osd->priority_xon_rx[i],
889 &nsd->priority_xon_rx[i]);
890 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
891 pf->stat_offsets_loaded,
892 &osd->priority_xon_tx[i],
893 &nsd->priority_xon_tx[i]);
894 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
895 pf->stat_offsets_loaded,
896 &osd->priority_xoff_tx[i],
897 &nsd->priority_xoff_tx[i]);
898 i40e_stat_update32(hw,
899 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
900 pf->stat_offsets_loaded,
901 &osd->priority_xon_2_xoff[i],
902 &nsd->priority_xon_2_xoff[i]);
903 }
904
905 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
906 I40E_GLPRT_PRC64L(hw->port),
907 pf->stat_offsets_loaded,
908 &osd->rx_size_64, &nsd->rx_size_64);
909 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
910 I40E_GLPRT_PRC127L(hw->port),
911 pf->stat_offsets_loaded,
912 &osd->rx_size_127, &nsd->rx_size_127);
913 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
914 I40E_GLPRT_PRC255L(hw->port),
915 pf->stat_offsets_loaded,
916 &osd->rx_size_255, &nsd->rx_size_255);
917 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
918 I40E_GLPRT_PRC511L(hw->port),
919 pf->stat_offsets_loaded,
920 &osd->rx_size_511, &nsd->rx_size_511);
921 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
922 I40E_GLPRT_PRC1023L(hw->port),
923 pf->stat_offsets_loaded,
924 &osd->rx_size_1023, &nsd->rx_size_1023);
925 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
926 I40E_GLPRT_PRC1522L(hw->port),
927 pf->stat_offsets_loaded,
928 &osd->rx_size_1522, &nsd->rx_size_1522);
929 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
930 I40E_GLPRT_PRC9522L(hw->port),
931 pf->stat_offsets_loaded,
932 &osd->rx_size_big, &nsd->rx_size_big);
933
934 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
935 I40E_GLPRT_PTC64L(hw->port),
936 pf->stat_offsets_loaded,
937 &osd->tx_size_64, &nsd->tx_size_64);
938 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
939 I40E_GLPRT_PTC127L(hw->port),
940 pf->stat_offsets_loaded,
941 &osd->tx_size_127, &nsd->tx_size_127);
942 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
943 I40E_GLPRT_PTC255L(hw->port),
944 pf->stat_offsets_loaded,
945 &osd->tx_size_255, &nsd->tx_size_255);
946 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
947 I40E_GLPRT_PTC511L(hw->port),
948 pf->stat_offsets_loaded,
949 &osd->tx_size_511, &nsd->tx_size_511);
950 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
951 I40E_GLPRT_PTC1023L(hw->port),
952 pf->stat_offsets_loaded,
953 &osd->tx_size_1023, &nsd->tx_size_1023);
954 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
955 I40E_GLPRT_PTC1522L(hw->port),
956 pf->stat_offsets_loaded,
957 &osd->tx_size_1522, &nsd->tx_size_1522);
958 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
959 I40E_GLPRT_PTC9522L(hw->port),
960 pf->stat_offsets_loaded,
961 &osd->tx_size_big, &nsd->tx_size_big);
962
963 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
964 pf->stat_offsets_loaded,
965 &osd->rx_undersize, &nsd->rx_undersize);
966 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
967 pf->stat_offsets_loaded,
968 &osd->rx_fragments, &nsd->rx_fragments);
969 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
970 pf->stat_offsets_loaded,
971 &osd->rx_oversize, &nsd->rx_oversize);
972 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
973 pf->stat_offsets_loaded,
974 &osd->rx_jabber, &nsd->rx_jabber);
975 }
976
977 pf->stat_offsets_loaded = true;
978}
979
980/**
981 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
982 * @vsi: the VSI to be searched
983 * @macaddr: the MAC address
984 * @vlan: the vlan
985 * @is_vf: make sure its a vf filter, else doesn't matter
986 * @is_netdev: make sure its a netdev filter, else doesn't matter
987 *
988 * Returns ptr to the filter object or NULL
989 **/
990static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
991 u8 *macaddr, s16 vlan,
992 bool is_vf, bool is_netdev)
993{
994 struct i40e_mac_filter *f;
995
996 if (!vsi || !macaddr)
997 return NULL;
998
999 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1000 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1001 (vlan == f->vlan) &&
1002 (!is_vf || f->is_vf) &&
1003 (!is_netdev || f->is_netdev))
1004 return f;
1005 }
1006 return NULL;
1007}
1008
1009/**
1010 * i40e_find_mac - Find a mac addr in the macvlan filters list
1011 * @vsi: the VSI to be searched
1012 * @macaddr: the MAC address we are searching for
1013 * @is_vf: make sure its a vf filter, else doesn't matter
1014 * @is_netdev: make sure its a netdev filter, else doesn't matter
1015 *
1016 * Returns the first filter with the provided MAC address or NULL if
1017 * MAC address was not found
1018 **/
1019struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, u8 *macaddr,
1020 bool is_vf, bool is_netdev)
1021{
1022 struct i40e_mac_filter *f;
1023
1024 if (!vsi || !macaddr)
1025 return NULL;
1026
1027 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1028 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1029 (!is_vf || f->is_vf) &&
1030 (!is_netdev || f->is_netdev))
1031 return f;
1032 }
1033 return NULL;
1034}
1035
1036/**
1037 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1038 * @vsi: the VSI to be searched
1039 *
1040 * Returns true if VSI is in vlan mode or false otherwise
1041 **/
1042bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1043{
1044 struct i40e_mac_filter *f;
1045
1046 /* Only -1 for all the filters denotes not in vlan mode
1047 * so we have to go through all the list in order to make sure
1048 */
1049 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1050 if (f->vlan >= 0)
1051 return true;
1052 }
1053
1054 return false;
1055}
1056
1057/**
1058 * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
1059 * @vsi: the VSI to be searched
1060 * @macaddr: the mac address to be filtered
1061 * @is_vf: true if it is a vf
1062 * @is_netdev: true if it is a netdev
1063 *
1064 * Goes through all the macvlan filters and adds a
1065 * macvlan filter for each unique vlan that already exists
1066 *
1067 * Returns first filter found on success, else NULL
1068 **/
1069struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi, u8 *macaddr,
1070 bool is_vf, bool is_netdev)
1071{
1072 struct i40e_mac_filter *f;
1073
1074 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1075 if (!i40e_find_filter(vsi, macaddr, f->vlan,
1076 is_vf, is_netdev)) {
1077 if (!i40e_add_filter(vsi, macaddr, f->vlan,
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -08001078 is_vf, is_netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001079 return NULL;
1080 }
1081 }
1082
1083 return list_first_entry_or_null(&vsi->mac_filter_list,
1084 struct i40e_mac_filter, list);
1085}
1086
1087/**
1088 * i40e_add_filter - Add a mac/vlan filter to the VSI
1089 * @vsi: the VSI to be searched
1090 * @macaddr: the MAC address
1091 * @vlan: the vlan
1092 * @is_vf: make sure its a vf filter, else doesn't matter
1093 * @is_netdev: make sure its a netdev filter, else doesn't matter
1094 *
1095 * Returns ptr to the filter object or NULL when no memory available.
1096 **/
1097struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1098 u8 *macaddr, s16 vlan,
1099 bool is_vf, bool is_netdev)
1100{
1101 struct i40e_mac_filter *f;
1102
1103 if (!vsi || !macaddr)
1104 return NULL;
1105
1106 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1107 if (!f) {
1108 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1109 if (!f)
1110 goto add_filter_out;
1111
1112 memcpy(f->macaddr, macaddr, ETH_ALEN);
1113 f->vlan = vlan;
1114 f->changed = true;
1115
1116 INIT_LIST_HEAD(&f->list);
1117 list_add(&f->list, &vsi->mac_filter_list);
1118 }
1119
1120 /* increment counter and add a new flag if needed */
1121 if (is_vf) {
1122 if (!f->is_vf) {
1123 f->is_vf = true;
1124 f->counter++;
1125 }
1126 } else if (is_netdev) {
1127 if (!f->is_netdev) {
1128 f->is_netdev = true;
1129 f->counter++;
1130 }
1131 } else {
1132 f->counter++;
1133 }
1134
1135 /* changed tells sync_filters_subtask to
1136 * push the filter down to the firmware
1137 */
1138 if (f->changed) {
1139 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1140 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1141 }
1142
1143add_filter_out:
1144 return f;
1145}
1146
1147/**
1148 * i40e_del_filter - Remove a mac/vlan filter from the VSI
1149 * @vsi: the VSI to be searched
1150 * @macaddr: the MAC address
1151 * @vlan: the vlan
1152 * @is_vf: make sure it's a vf filter, else doesn't matter
1153 * @is_netdev: make sure it's a netdev filter, else doesn't matter
1154 **/
1155void i40e_del_filter(struct i40e_vsi *vsi,
1156 u8 *macaddr, s16 vlan,
1157 bool is_vf, bool is_netdev)
1158{
1159 struct i40e_mac_filter *f;
1160
1161 if (!vsi || !macaddr)
1162 return;
1163
1164 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1165 if (!f || f->counter == 0)
1166 return;
1167
1168 if (is_vf) {
1169 if (f->is_vf) {
1170 f->is_vf = false;
1171 f->counter--;
1172 }
1173 } else if (is_netdev) {
1174 if (f->is_netdev) {
1175 f->is_netdev = false;
1176 f->counter--;
1177 }
1178 } else {
1179 /* make sure we don't remove a filter in use by vf or netdev */
1180 int min_f = 0;
1181 min_f += (f->is_vf ? 1 : 0);
1182 min_f += (f->is_netdev ? 1 : 0);
1183
1184 if (f->counter > min_f)
1185 f->counter--;
1186 }
1187
1188 /* counter == 0 tells sync_filters_subtask to
1189 * remove the filter from the firmware's list
1190 */
1191 if (f->counter == 0) {
1192 f->changed = true;
1193 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1194 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1195 }
1196}
1197
1198/**
1199 * i40e_set_mac - NDO callback to set mac address
1200 * @netdev: network interface device structure
1201 * @p: pointer to an address structure
1202 *
1203 * Returns 0 on success, negative on failure
1204 **/
1205static int i40e_set_mac(struct net_device *netdev, void *p)
1206{
1207 struct i40e_netdev_priv *np = netdev_priv(netdev);
1208 struct i40e_vsi *vsi = np->vsi;
1209 struct sockaddr *addr = p;
1210 struct i40e_mac_filter *f;
1211
1212 if (!is_valid_ether_addr(addr->sa_data))
1213 return -EADDRNOTAVAIL;
1214
1215 netdev_info(netdev, "set mac address=%pM\n", addr->sa_data);
1216
1217 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
1218 return 0;
1219
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001220 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1221 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1222 return -EADDRNOTAVAIL;
1223
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001224 if (vsi->type == I40E_VSI_MAIN) {
1225 i40e_status ret;
1226 ret = i40e_aq_mac_address_write(&vsi->back->hw,
1227 I40E_AQC_WRITE_TYPE_LAA_ONLY,
1228 addr->sa_data, NULL);
1229 if (ret) {
1230 netdev_info(netdev,
1231 "Addr change for Main VSI failed: %d\n",
1232 ret);
1233 return -EADDRNOTAVAIL;
1234 }
1235
1236 memcpy(vsi->back->hw.mac.addr, addr->sa_data, netdev->addr_len);
1237 }
1238
1239 /* In order to be sure to not drop any packets, add the new address
1240 * then delete the old one.
1241 */
1242 f = i40e_add_filter(vsi, addr->sa_data, I40E_VLAN_ANY, false, false);
1243 if (!f)
1244 return -ENOMEM;
1245
1246 i40e_sync_vsi_filters(vsi);
1247 i40e_del_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY, false, false);
1248 i40e_sync_vsi_filters(vsi);
1249
1250 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1251
1252 return 0;
1253}
1254
1255/**
1256 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1257 * @vsi: the VSI being setup
1258 * @ctxt: VSI context structure
1259 * @enabled_tc: Enabled TCs bitmap
1260 * @is_add: True if called before Add VSI
1261 *
1262 * Setup VSI queue mapping for enabled traffic classes.
1263 **/
1264static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1265 struct i40e_vsi_context *ctxt,
1266 u8 enabled_tc,
1267 bool is_add)
1268{
1269 struct i40e_pf *pf = vsi->back;
1270 u16 sections = 0;
1271 u8 netdev_tc = 0;
1272 u16 numtc = 0;
1273 u16 qcount;
1274 u8 offset;
1275 u16 qmap;
1276 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001277 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001278
1279 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1280 offset = 0;
1281
1282 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1283 /* Find numtc from enabled TC bitmap */
1284 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1285 if (enabled_tc & (1 << i)) /* TC is enabled */
1286 numtc++;
1287 }
1288 if (!numtc) {
1289 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1290 numtc = 1;
1291 }
1292 } else {
1293 /* At least TC0 is enabled in case of non-DCB case */
1294 numtc = 1;
1295 }
1296
1297 vsi->tc_config.numtc = numtc;
1298 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001299 /* Number of queues per enabled TC */
1300 num_tc_qps = rounddown_pow_of_two(vsi->alloc_queue_pairs/numtc);
1301 num_tc_qps = min_t(int, num_tc_qps, I40E_MAX_QUEUES_PER_TC);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001302
1303 /* Setup queue offset/count for all TCs for given VSI */
1304 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1305 /* See if the given TC is enabled for the given VSI */
1306 if (vsi->tc_config.enabled_tc & (1 << i)) { /* TC is enabled */
1307 int pow, num_qps;
1308
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001309 switch (vsi->type) {
1310 case I40E_VSI_MAIN:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001311 qcount = min_t(int, pf->rss_size, num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001312 break;
1313 case I40E_VSI_FDIR:
1314 case I40E_VSI_SRIOV:
1315 case I40E_VSI_VMDQ2:
1316 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001317 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001318 WARN_ON(i != 0);
1319 break;
1320 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001321 vsi->tc_config.tc_info[i].qoffset = offset;
1322 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001323
1324 /* find the power-of-2 of the number of queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001325 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001326 pow = 0;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001327 while (num_qps && ((1 << pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001328 pow++;
1329 num_qps >>= 1;
1330 }
1331
1332 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1333 qmap =
1334 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1335 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1336
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001337 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001338 } else {
1339 /* TC is not enabled so set the offset to
1340 * default queue and allocate one queue
1341 * for the given TC.
1342 */
1343 vsi->tc_config.tc_info[i].qoffset = 0;
1344 vsi->tc_config.tc_info[i].qcount = 1;
1345 vsi->tc_config.tc_info[i].netdev_tc = 0;
1346
1347 qmap = 0;
1348 }
1349 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1350 }
1351
1352 /* Set actual Tx/Rx queue pairs */
1353 vsi->num_queue_pairs = offset;
1354
1355 /* Scheduler section valid can only be set for ADD VSI */
1356 if (is_add) {
1357 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1358
1359 ctxt->info.up_enable_bits = enabled_tc;
1360 }
1361 if (vsi->type == I40E_VSI_SRIOV) {
1362 ctxt->info.mapping_flags |=
1363 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1364 for (i = 0; i < vsi->num_queue_pairs; i++)
1365 ctxt->info.queue_mapping[i] =
1366 cpu_to_le16(vsi->base_queue + i);
1367 } else {
1368 ctxt->info.mapping_flags |=
1369 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1370 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1371 }
1372 ctxt->info.valid_sections |= cpu_to_le16(sections);
1373}
1374
1375/**
1376 * i40e_set_rx_mode - NDO callback to set the netdev filters
1377 * @netdev: network interface device structure
1378 **/
1379static void i40e_set_rx_mode(struct net_device *netdev)
1380{
1381 struct i40e_netdev_priv *np = netdev_priv(netdev);
1382 struct i40e_mac_filter *f, *ftmp;
1383 struct i40e_vsi *vsi = np->vsi;
1384 struct netdev_hw_addr *uca;
1385 struct netdev_hw_addr *mca;
1386 struct netdev_hw_addr *ha;
1387
1388 /* add addr if not already in the filter list */
1389 netdev_for_each_uc_addr(uca, netdev) {
1390 if (!i40e_find_mac(vsi, uca->addr, false, true)) {
1391 if (i40e_is_vsi_in_vlan(vsi))
1392 i40e_put_mac_in_vlan(vsi, uca->addr,
1393 false, true);
1394 else
1395 i40e_add_filter(vsi, uca->addr, I40E_VLAN_ANY,
1396 false, true);
1397 }
1398 }
1399
1400 netdev_for_each_mc_addr(mca, netdev) {
1401 if (!i40e_find_mac(vsi, mca->addr, false, true)) {
1402 if (i40e_is_vsi_in_vlan(vsi))
1403 i40e_put_mac_in_vlan(vsi, mca->addr,
1404 false, true);
1405 else
1406 i40e_add_filter(vsi, mca->addr, I40E_VLAN_ANY,
1407 false, true);
1408 }
1409 }
1410
1411 /* remove filter if not in netdev list */
1412 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1413 bool found = false;
1414
1415 if (!f->is_netdev)
1416 continue;
1417
1418 if (is_multicast_ether_addr(f->macaddr)) {
1419 netdev_for_each_mc_addr(mca, netdev) {
1420 if (ether_addr_equal(mca->addr, f->macaddr)) {
1421 found = true;
1422 break;
1423 }
1424 }
1425 } else {
1426 netdev_for_each_uc_addr(uca, netdev) {
1427 if (ether_addr_equal(uca->addr, f->macaddr)) {
1428 found = true;
1429 break;
1430 }
1431 }
1432
1433 for_each_dev_addr(netdev, ha) {
1434 if (ether_addr_equal(ha->addr, f->macaddr)) {
1435 found = true;
1436 break;
1437 }
1438 }
1439 }
1440 if (!found)
1441 i40e_del_filter(
1442 vsi, f->macaddr, I40E_VLAN_ANY, false, true);
1443 }
1444
1445 /* check for other flag changes */
1446 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1447 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1448 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1449 }
1450}
1451
1452/**
1453 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1454 * @vsi: ptr to the VSI
1455 *
1456 * Push any outstanding VSI filter changes through the AdminQ.
1457 *
1458 * Returns 0 or error value
1459 **/
1460int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
1461{
1462 struct i40e_mac_filter *f, *ftmp;
1463 bool promisc_forced_on = false;
1464 bool add_happened = false;
1465 int filter_list_len = 0;
1466 u32 changed_flags = 0;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001467 i40e_status aq_ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001468 struct i40e_pf *pf;
1469 int num_add = 0;
1470 int num_del = 0;
1471 u16 cmd_flags;
1472
1473 /* empty array typed pointers, kcalloc later */
1474 struct i40e_aqc_add_macvlan_element_data *add_list;
1475 struct i40e_aqc_remove_macvlan_element_data *del_list;
1476
1477 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1478 usleep_range(1000, 2000);
1479 pf = vsi->back;
1480
1481 if (vsi->netdev) {
1482 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1483 vsi->current_netdev_flags = vsi->netdev->flags;
1484 }
1485
1486 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1487 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1488
1489 filter_list_len = pf->hw.aq.asq_buf_size /
1490 sizeof(struct i40e_aqc_remove_macvlan_element_data);
1491 del_list = kcalloc(filter_list_len,
1492 sizeof(struct i40e_aqc_remove_macvlan_element_data),
1493 GFP_KERNEL);
1494 if (!del_list)
1495 return -ENOMEM;
1496
1497 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1498 if (!f->changed)
1499 continue;
1500
1501 if (f->counter != 0)
1502 continue;
1503 f->changed = false;
1504 cmd_flags = 0;
1505
1506 /* add to delete list */
1507 memcpy(del_list[num_del].mac_addr,
1508 f->macaddr, ETH_ALEN);
1509 del_list[num_del].vlan_tag =
1510 cpu_to_le16((u16)(f->vlan ==
1511 I40E_VLAN_ANY ? 0 : f->vlan));
1512
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001513 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1514 del_list[num_del].flags = cmd_flags;
1515 num_del++;
1516
1517 /* unlink from filter list */
1518 list_del(&f->list);
1519 kfree(f);
1520
1521 /* flush a full buffer */
1522 if (num_del == filter_list_len) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001523 aq_ret = i40e_aq_remove_macvlan(&pf->hw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001524 vsi->seid, del_list, num_del,
1525 NULL);
1526 num_del = 0;
1527 memset(del_list, 0, sizeof(*del_list));
1528
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001529 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001530 dev_info(&pf->pdev->dev,
1531 "ignoring delete macvlan error, err %d, aq_err %d while flushing a full buffer\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001532 aq_ret,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001533 pf->hw.aq.asq_last_status);
1534 }
1535 }
1536 if (num_del) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001537 aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001538 del_list, num_del, NULL);
1539 num_del = 0;
1540
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001541 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001542 dev_info(&pf->pdev->dev,
1543 "ignoring delete macvlan error, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001544 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001545 }
1546
1547 kfree(del_list);
1548 del_list = NULL;
1549
1550 /* do all the adds now */
1551 filter_list_len = pf->hw.aq.asq_buf_size /
1552 sizeof(struct i40e_aqc_add_macvlan_element_data),
1553 add_list = kcalloc(filter_list_len,
1554 sizeof(struct i40e_aqc_add_macvlan_element_data),
1555 GFP_KERNEL);
1556 if (!add_list)
1557 return -ENOMEM;
1558
1559 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1560 if (!f->changed)
1561 continue;
1562
1563 if (f->counter == 0)
1564 continue;
1565 f->changed = false;
1566 add_happened = true;
1567 cmd_flags = 0;
1568
1569 /* add to add array */
1570 memcpy(add_list[num_add].mac_addr,
1571 f->macaddr, ETH_ALEN);
1572 add_list[num_add].vlan_tag =
1573 cpu_to_le16(
1574 (u16)(f->vlan == I40E_VLAN_ANY ? 0 : f->vlan));
1575 add_list[num_add].queue_number = 0;
1576
1577 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001578 add_list[num_add].flags = cpu_to_le16(cmd_flags);
1579 num_add++;
1580
1581 /* flush a full buffer */
1582 if (num_add == filter_list_len) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001583 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1584 add_list, num_add,
1585 NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001586 num_add = 0;
1587
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001588 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001589 break;
1590 memset(add_list, 0, sizeof(*add_list));
1591 }
1592 }
1593 if (num_add) {
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001594 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1595 add_list, num_add, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001596 num_add = 0;
1597 }
1598 kfree(add_list);
1599 add_list = NULL;
1600
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001601 if (add_happened && (!aq_ret)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001602 /* do nothing */;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001603 } else if (add_happened && (aq_ret)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001604 dev_info(&pf->pdev->dev,
1605 "add filter failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001606 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001607 if ((pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOSPC) &&
1608 !test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1609 &vsi->state)) {
1610 promisc_forced_on = true;
1611 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1612 &vsi->state);
1613 dev_info(&pf->pdev->dev, "promiscuous mode forced on\n");
1614 }
1615 }
1616 }
1617
1618 /* check for changes in promiscuous modes */
1619 if (changed_flags & IFF_ALLMULTI) {
1620 bool cur_multipromisc;
1621 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001622 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
1623 vsi->seid,
1624 cur_multipromisc,
1625 NULL);
1626 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001627 dev_info(&pf->pdev->dev,
1628 "set multi promisc failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001629 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001630 }
1631 if ((changed_flags & IFF_PROMISC) || promisc_forced_on) {
1632 bool cur_promisc;
1633 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
1634 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1635 &vsi->state));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001636 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(&vsi->back->hw,
1637 vsi->seid,
1638 cur_promisc, NULL);
1639 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001640 dev_info(&pf->pdev->dev,
1641 "set uni promisc failed, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00001642 aq_ret, pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00001643 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
1644 vsi->seid,
1645 cur_promisc, NULL);
1646 if (aq_ret)
1647 dev_info(&pf->pdev->dev,
1648 "set brdcast promisc failed, err %d, aq_err %d\n",
1649 aq_ret, pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001650 }
1651
1652 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
1653 return 0;
1654}
1655
1656/**
1657 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
1658 * @pf: board private structure
1659 **/
1660static void i40e_sync_filters_subtask(struct i40e_pf *pf)
1661{
1662 int v;
1663
1664 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
1665 return;
1666 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
1667
1668 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
1669 if (pf->vsi[v] &&
1670 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED))
1671 i40e_sync_vsi_filters(pf->vsi[v]);
1672 }
1673}
1674
1675/**
1676 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
1677 * @netdev: network interface device structure
1678 * @new_mtu: new value for maximum frame size
1679 *
1680 * Returns 0 on success, negative on failure
1681 **/
1682static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
1683{
1684 struct i40e_netdev_priv *np = netdev_priv(netdev);
1685 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
1686 struct i40e_vsi *vsi = np->vsi;
1687
1688 /* MTU < 68 is an error and causes problems on some kernels */
1689 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
1690 return -EINVAL;
1691
1692 netdev_info(netdev, "changing MTU from %d to %d\n",
1693 netdev->mtu, new_mtu);
1694 netdev->mtu = new_mtu;
1695 if (netif_running(netdev))
1696 i40e_vsi_reinit_locked(vsi);
1697
1698 return 0;
1699}
1700
1701/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00001702 * i40e_ioctl - Access the hwtstamp interface
1703 * @netdev: network interface device structure
1704 * @ifr: interface request data
1705 * @cmd: ioctl command
1706 **/
1707int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1708{
1709 struct i40e_netdev_priv *np = netdev_priv(netdev);
1710 struct i40e_pf *pf = np->vsi->back;
1711
1712 switch (cmd) {
1713 case SIOCGHWTSTAMP:
1714 return i40e_ptp_get_ts_config(pf, ifr);
1715 case SIOCSHWTSTAMP:
1716 return i40e_ptp_set_ts_config(pf, ifr);
1717 default:
1718 return -EOPNOTSUPP;
1719 }
1720}
1721
1722/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001723 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
1724 * @vsi: the vsi being adjusted
1725 **/
1726void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
1727{
1728 struct i40e_vsi_context ctxt;
1729 i40e_status ret;
1730
1731 if ((vsi->info.valid_sections &
1732 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1733 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
1734 return; /* already enabled */
1735
1736 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1737 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1738 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
1739
1740 ctxt.seid = vsi->seid;
1741 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1742 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1743 if (ret) {
1744 dev_info(&vsi->back->pdev->dev,
1745 "%s: update vsi failed, aq_err=%d\n",
1746 __func__, vsi->back->hw.aq.asq_last_status);
1747 }
1748}
1749
1750/**
1751 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
1752 * @vsi: the vsi being adjusted
1753 **/
1754void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
1755{
1756 struct i40e_vsi_context ctxt;
1757 i40e_status ret;
1758
1759 if ((vsi->info.valid_sections &
1760 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1761 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
1762 I40E_AQ_VSI_PVLAN_EMOD_MASK))
1763 return; /* already disabled */
1764
1765 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1766 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1767 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
1768
1769 ctxt.seid = vsi->seid;
1770 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1771 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1772 if (ret) {
1773 dev_info(&vsi->back->pdev->dev,
1774 "%s: update vsi failed, aq_err=%d\n",
1775 __func__, vsi->back->hw.aq.asq_last_status);
1776 }
1777}
1778
1779/**
1780 * i40e_vlan_rx_register - Setup or shutdown vlan offload
1781 * @netdev: network interface to be adjusted
1782 * @features: netdev features to test if VLAN offload is enabled or not
1783 **/
1784static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
1785{
1786 struct i40e_netdev_priv *np = netdev_priv(netdev);
1787 struct i40e_vsi *vsi = np->vsi;
1788
1789 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1790 i40e_vlan_stripping_enable(vsi);
1791 else
1792 i40e_vlan_stripping_disable(vsi);
1793}
1794
1795/**
1796 * i40e_vsi_add_vlan - Add vsi membership for given vlan
1797 * @vsi: the vsi being configured
1798 * @vid: vlan id to be added (0 = untagged only , -1 = any)
1799 **/
1800int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
1801{
1802 struct i40e_mac_filter *f, *add_f;
1803 bool is_netdev, is_vf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001804
1805 is_vf = (vsi->type == I40E_VSI_SRIOV);
1806 is_netdev = !!(vsi->netdev);
1807
1808 if (is_netdev) {
1809 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid,
1810 is_vf, is_netdev);
1811 if (!add_f) {
1812 dev_info(&vsi->back->pdev->dev,
1813 "Could not add vlan filter %d for %pM\n",
1814 vid, vsi->netdev->dev_addr);
1815 return -ENOMEM;
1816 }
1817 }
1818
1819 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1820 add_f = i40e_add_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
1821 if (!add_f) {
1822 dev_info(&vsi->back->pdev->dev,
1823 "Could not add vlan filter %d for %pM\n",
1824 vid, f->macaddr);
1825 return -ENOMEM;
1826 }
1827 }
1828
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001829 /* Now if we add a vlan tag, make sure to check if it is the first
1830 * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
1831 * with 0, so we now accept untagged and specified tagged traffic
1832 * (and not any taged and untagged)
1833 */
1834 if (vid > 0) {
1835 if (is_netdev && i40e_find_filter(vsi, vsi->netdev->dev_addr,
1836 I40E_VLAN_ANY,
1837 is_vf, is_netdev)) {
1838 i40e_del_filter(vsi, vsi->netdev->dev_addr,
1839 I40E_VLAN_ANY, is_vf, is_netdev);
1840 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0,
1841 is_vf, is_netdev);
1842 if (!add_f) {
1843 dev_info(&vsi->back->pdev->dev,
1844 "Could not add filter 0 for %pM\n",
1845 vsi->netdev->dev_addr);
1846 return -ENOMEM;
1847 }
1848 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08001849 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001850
Greg Rose8d82a7c2014-01-13 16:13:04 -08001851 /* Do not assume that I40E_VLAN_ANY should be reset to VLAN 0 */
1852 if (vid > 0 && !vsi->info.pvid) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001853 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1854 if (i40e_find_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1855 is_vf, is_netdev)) {
1856 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1857 is_vf, is_netdev);
1858 add_f = i40e_add_filter(vsi, f->macaddr,
1859 0, is_vf, is_netdev);
1860 if (!add_f) {
1861 dev_info(&vsi->back->pdev->dev,
1862 "Could not add filter 0 for %pM\n",
1863 f->macaddr);
1864 return -ENOMEM;
1865 }
1866 }
1867 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001868 }
1869
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001870 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1871 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1872 return 0;
1873
1874 return i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001875}
1876
1877/**
1878 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
1879 * @vsi: the vsi being configured
1880 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001881 *
1882 * Return: 0 on success or negative otherwise
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001883 **/
1884int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
1885{
1886 struct net_device *netdev = vsi->netdev;
1887 struct i40e_mac_filter *f, *add_f;
1888 bool is_vf, is_netdev;
1889 int filter_count = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001890
1891 is_vf = (vsi->type == I40E_VSI_SRIOV);
1892 is_netdev = !!(netdev);
1893
1894 if (is_netdev)
1895 i40e_del_filter(vsi, netdev->dev_addr, vid, is_vf, is_netdev);
1896
1897 list_for_each_entry(f, &vsi->mac_filter_list, list)
1898 i40e_del_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
1899
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001900 /* go through all the filters for this VSI and if there is only
1901 * vid == 0 it means there are no other filters, so vid 0 must
1902 * be replaced with -1. This signifies that we should from now
1903 * on accept any traffic (with any tag present, or untagged)
1904 */
1905 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1906 if (is_netdev) {
1907 if (f->vlan &&
1908 ether_addr_equal(netdev->dev_addr, f->macaddr))
1909 filter_count++;
1910 }
1911
1912 if (f->vlan)
1913 filter_count++;
1914 }
1915
1916 if (!filter_count && is_netdev) {
1917 i40e_del_filter(vsi, netdev->dev_addr, 0, is_vf, is_netdev);
1918 f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
1919 is_vf, is_netdev);
1920 if (!f) {
1921 dev_info(&vsi->back->pdev->dev,
1922 "Could not add filter %d for %pM\n",
1923 I40E_VLAN_ANY, netdev->dev_addr);
1924 return -ENOMEM;
1925 }
1926 }
1927
1928 if (!filter_count) {
1929 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1930 i40e_del_filter(vsi, f->macaddr, 0, is_vf, is_netdev);
1931 add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1932 is_vf, is_netdev);
1933 if (!add_f) {
1934 dev_info(&vsi->back->pdev->dev,
1935 "Could not add filter %d for %pM\n",
1936 I40E_VLAN_ANY, f->macaddr);
1937 return -ENOMEM;
1938 }
1939 }
1940 }
1941
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001942 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1943 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1944 return 0;
1945
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001946 return i40e_sync_vsi_filters(vsi);
1947}
1948
1949/**
1950 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
1951 * @netdev: network interface to be adjusted
1952 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001953 *
1954 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001955 **/
1956static int i40e_vlan_rx_add_vid(struct net_device *netdev,
1957 __always_unused __be16 proto, u16 vid)
1958{
1959 struct i40e_netdev_priv *np = netdev_priv(netdev);
1960 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001961 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001962
1963 if (vid > 4095)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001964 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001965
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001966 netdev_info(netdev, "adding %pM vid=%d\n", netdev->dev_addr, vid);
1967
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00001968 /* If the network stack called us with vid = 0 then
1969 * it is asking to receive priority tagged packets with
1970 * vlan id 0. Our HW receives them by default when configured
1971 * to receive untagged packets so there is no need to add an
1972 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001973 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00001974 if (vid)
1975 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001976
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001977 if (!ret && (vid < VLAN_N_VID))
1978 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001979
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001980 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001981}
1982
1983/**
1984 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
1985 * @netdev: network interface to be adjusted
1986 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001987 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00001988 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001989 **/
1990static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
1991 __always_unused __be16 proto, u16 vid)
1992{
1993 struct i40e_netdev_priv *np = netdev_priv(netdev);
1994 struct i40e_vsi *vsi = np->vsi;
1995
Jesse Brandeburg078b5872013-09-25 23:41:14 +00001996 netdev_info(netdev, "removing %pM vid=%d\n", netdev->dev_addr, vid);
1997
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001998 /* return code is ignored as there is nothing a user
1999 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002000 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002001 */
2002 i40e_vsi_kill_vlan(vsi, vid);
2003
2004 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002005
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002006 return 0;
2007}
2008
2009/**
2010 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2011 * @vsi: the vsi being brought back up
2012 **/
2013static void i40e_restore_vlan(struct i40e_vsi *vsi)
2014{
2015 u16 vid;
2016
2017 if (!vsi->netdev)
2018 return;
2019
2020 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2021
2022 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2023 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2024 vid);
2025}
2026
2027/**
2028 * i40e_vsi_add_pvid - Add pvid for the VSI
2029 * @vsi: the vsi being adjusted
2030 * @vid: the vlan id to set as a PVID
2031 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002032int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002033{
2034 struct i40e_vsi_context ctxt;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002035 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002036
2037 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2038 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002039 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2040 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002041 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002042
2043 ctxt.seid = vsi->seid;
2044 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002045 aq_ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2046 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002047 dev_info(&vsi->back->pdev->dev,
2048 "%s: update vsi failed, aq_err=%d\n",
2049 __func__, vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002050 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002051 }
2052
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002053 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002054}
2055
2056/**
2057 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2058 * @vsi: the vsi being adjusted
2059 *
2060 * Just use the vlan_rx_register() service to put it back to normal
2061 **/
2062void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2063{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002064 i40e_vlan_stripping_disable(vsi);
2065
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002066 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002067}
2068
2069/**
2070 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2071 * @vsi: ptr to the VSI
2072 *
2073 * If this function returns with an error, then it's possible one or
2074 * more of the rings is populated (while the rest are not). It is the
2075 * callers duty to clean those orphaned rings.
2076 *
2077 * Return 0 on success, negative on failure
2078 **/
2079static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2080{
2081 int i, err = 0;
2082
2083 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002084 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002085
2086 return err;
2087}
2088
2089/**
2090 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2091 * @vsi: ptr to the VSI
2092 *
2093 * Free VSI's transmit software resources
2094 **/
2095static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2096{
2097 int i;
2098
Greg Rose8e9dca52013-12-18 13:45:53 +00002099 if (!vsi->tx_rings)
2100 return;
2101
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002102 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002103 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002104 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002105}
2106
2107/**
2108 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2109 * @vsi: ptr to the VSI
2110 *
2111 * If this function returns with an error, then it's possible one or
2112 * more of the rings is populated (while the rest are not). It is the
2113 * callers duty to clean those orphaned rings.
2114 *
2115 * Return 0 on success, negative on failure
2116 **/
2117static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2118{
2119 int i, err = 0;
2120
2121 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002122 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002123 return err;
2124}
2125
2126/**
2127 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2128 * @vsi: ptr to the VSI
2129 *
2130 * Free all receive software resources
2131 **/
2132static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2133{
2134 int i;
2135
Greg Rose8e9dca52013-12-18 13:45:53 +00002136 if (!vsi->rx_rings)
2137 return;
2138
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002139 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002140 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002141 i40e_free_rx_resources(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002142}
2143
2144/**
2145 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2146 * @ring: The Tx ring to configure
2147 *
2148 * Configure the Tx descriptor ring in the HMC context.
2149 **/
2150static int i40e_configure_tx_ring(struct i40e_ring *ring)
2151{
2152 struct i40e_vsi *vsi = ring->vsi;
2153 u16 pf_q = vsi->base_queue + ring->queue_index;
2154 struct i40e_hw *hw = &vsi->back->hw;
2155 struct i40e_hmc_obj_txq tx_ctx;
2156 i40e_status err = 0;
2157 u32 qtx_ctl = 0;
2158
2159 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002160 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002161 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2162 ring->atr_count = 0;
2163 } else {
2164 ring->atr_sample_rate = 0;
2165 }
2166
2167 /* initialize XPS */
2168 if (ring->q_vector && ring->netdev &&
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08002169 vsi->tc_config.numtc <= 1 &&
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002170 !test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
2171 netif_set_xps_queue(ring->netdev,
2172 &ring->q_vector->affinity_mask,
2173 ring->queue_index);
2174
2175 /* clear the context structure first */
2176 memset(&tx_ctx, 0, sizeof(tx_ctx));
2177
2178 tx_ctx.new_context = 1;
2179 tx_ctx.base = (ring->dma / 128);
2180 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002181 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2182 I40E_FLAG_FD_ATR_ENABLED));
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002183 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002184
2185 /* As part of VSI creation/update, FW allocates certain
2186 * Tx arbitration queue sets for each TC enabled for
2187 * the VSI. The FW returns the handles to these queue
2188 * sets as part of the response buffer to Add VSI,
2189 * Update VSI, etc. AQ commands. It is expected that
2190 * these queue set handles be associated with the Tx
2191 * queues by the driver as part of the TX queue context
2192 * initialization. This has to be done regardless of
2193 * DCB as by default everything is mapped to TC0.
2194 */
2195 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2196 tx_ctx.rdylist_act = 0;
2197
2198 /* clear the context in the HMC */
2199 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2200 if (err) {
2201 dev_info(&vsi->back->pdev->dev,
2202 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2203 ring->queue_index, pf_q, err);
2204 return -ENOMEM;
2205 }
2206
2207 /* set the context in the HMC */
2208 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2209 if (err) {
2210 dev_info(&vsi->back->pdev->dev,
2211 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2212 ring->queue_index, pf_q, err);
2213 return -ENOMEM;
2214 }
2215
2216 /* Now associate this queue with this PCI function */
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002217 if (vsi->type == I40E_VSI_VMDQ2)
2218 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
2219 else
2220 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Shannon Nelson13fd9772013-09-28 07:14:19 +00002221 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2222 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002223 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2224 i40e_flush(hw);
2225
2226 clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
2227
2228 /* cache tail off for easier writes later */
2229 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2230
2231 return 0;
2232}
2233
2234/**
2235 * i40e_configure_rx_ring - Configure a receive ring context
2236 * @ring: The Rx ring to configure
2237 *
2238 * Configure the Rx descriptor ring in the HMC context.
2239 **/
2240static int i40e_configure_rx_ring(struct i40e_ring *ring)
2241{
2242 struct i40e_vsi *vsi = ring->vsi;
2243 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2244 u16 pf_q = vsi->base_queue + ring->queue_index;
2245 struct i40e_hw *hw = &vsi->back->hw;
2246 struct i40e_hmc_obj_rxq rx_ctx;
2247 i40e_status err = 0;
2248
2249 ring->state = 0;
2250
2251 /* clear the context structure first */
2252 memset(&rx_ctx, 0, sizeof(rx_ctx));
2253
2254 ring->rx_buf_len = vsi->rx_buf_len;
2255 ring->rx_hdr_len = vsi->rx_hdr_len;
2256
2257 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
2258 rx_ctx.hbuff = ring->rx_hdr_len >> I40E_RXQ_CTX_HBUFF_SHIFT;
2259
2260 rx_ctx.base = (ring->dma / 128);
2261 rx_ctx.qlen = ring->count;
2262
2263 if (vsi->back->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED) {
2264 set_ring_16byte_desc_enabled(ring);
2265 rx_ctx.dsize = 0;
2266 } else {
2267 rx_ctx.dsize = 1;
2268 }
2269
2270 rx_ctx.dtype = vsi->dtype;
2271 if (vsi->dtype) {
2272 set_ring_ps_enabled(ring);
2273 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
2274 I40E_RX_SPLIT_IP |
2275 I40E_RX_SPLIT_TCP_UDP |
2276 I40E_RX_SPLIT_SCTP;
2277 } else {
2278 rx_ctx.hsplit_0 = 0;
2279 }
2280
2281 rx_ctx.rxmax = min_t(u16, vsi->max_frame,
2282 (chain_len * ring->rx_buf_len));
2283 rx_ctx.tphrdesc_ena = 1;
2284 rx_ctx.tphwdesc_ena = 1;
2285 rx_ctx.tphdata_ena = 1;
2286 rx_ctx.tphhead_ena = 1;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00002287 if (hw->revision_id == 0)
2288 rx_ctx.lrxqthresh = 0;
2289 else
2290 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002291 rx_ctx.crcstrip = 1;
2292 rx_ctx.l2tsel = 1;
2293 rx_ctx.showiv = 1;
2294
2295 /* clear the context in the HMC */
2296 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2297 if (err) {
2298 dev_info(&vsi->back->pdev->dev,
2299 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2300 ring->queue_index, pf_q, err);
2301 return -ENOMEM;
2302 }
2303
2304 /* set the context in the HMC */
2305 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2306 if (err) {
2307 dev_info(&vsi->back->pdev->dev,
2308 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2309 ring->queue_index, pf_q, err);
2310 return -ENOMEM;
2311 }
2312
2313 /* cache tail for quicker writes, and clear the reg before use */
2314 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2315 writel(0, ring->tail);
2316
2317 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
2318
2319 return 0;
2320}
2321
2322/**
2323 * i40e_vsi_configure_tx - Configure the VSI for Tx
2324 * @vsi: VSI structure describing this set of rings and resources
2325 *
2326 * Configure the Tx VSI for operation.
2327 **/
2328static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2329{
2330 int err = 0;
2331 u16 i;
2332
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002333 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2334 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002335
2336 return err;
2337}
2338
2339/**
2340 * i40e_vsi_configure_rx - Configure the VSI for Rx
2341 * @vsi: the VSI being configured
2342 *
2343 * Configure the Rx VSI for operation.
2344 **/
2345static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
2346{
2347 int err = 0;
2348 u16 i;
2349
2350 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
2351 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
2352 + ETH_FCS_LEN + VLAN_HLEN;
2353 else
2354 vsi->max_frame = I40E_RXBUFFER_2048;
2355
2356 /* figure out correct receive buffer length */
2357 switch (vsi->back->flags & (I40E_FLAG_RX_1BUF_ENABLED |
2358 I40E_FLAG_RX_PS_ENABLED)) {
2359 case I40E_FLAG_RX_1BUF_ENABLED:
2360 vsi->rx_hdr_len = 0;
2361 vsi->rx_buf_len = vsi->max_frame;
2362 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2363 break;
2364 case I40E_FLAG_RX_PS_ENABLED:
2365 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2366 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2367 vsi->dtype = I40E_RX_DTYPE_HEADER_SPLIT;
2368 break;
2369 default:
2370 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2371 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2372 vsi->dtype = I40E_RX_DTYPE_SPLIT_ALWAYS;
2373 break;
2374 }
2375
2376 /* round up for the chip's needs */
2377 vsi->rx_hdr_len = ALIGN(vsi->rx_hdr_len,
2378 (1 << I40E_RXQ_CTX_HBUFF_SHIFT));
2379 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
2380 (1 << I40E_RXQ_CTX_DBUFF_SHIFT));
2381
2382 /* set up individual rings */
2383 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002384 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002385
2386 return err;
2387}
2388
2389/**
2390 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
2391 * @vsi: ptr to the VSI
2392 **/
2393static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
2394{
2395 u16 qoffset, qcount;
2396 int i, n;
2397
2398 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED))
2399 return;
2400
2401 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
2402 if (!(vsi->tc_config.enabled_tc & (1 << n)))
2403 continue;
2404
2405 qoffset = vsi->tc_config.tc_info[n].qoffset;
2406 qcount = vsi->tc_config.tc_info[n].qcount;
2407 for (i = qoffset; i < (qoffset + qcount); i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002408 struct i40e_ring *rx_ring = vsi->rx_rings[i];
2409 struct i40e_ring *tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002410 rx_ring->dcb_tc = n;
2411 tx_ring->dcb_tc = n;
2412 }
2413 }
2414}
2415
2416/**
2417 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
2418 * @vsi: ptr to the VSI
2419 **/
2420static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
2421{
2422 if (vsi->netdev)
2423 i40e_set_rx_mode(vsi->netdev);
2424}
2425
2426/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00002427 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
2428 * @vsi: Pointer to the targeted VSI
2429 *
2430 * This function replays the hlist on the hw where all the SB Flow Director
2431 * filters were saved.
2432 **/
2433static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
2434{
2435 struct i40e_fdir_filter *filter;
2436 struct i40e_pf *pf = vsi->back;
2437 struct hlist_node *node;
2438
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00002439 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
2440 return;
2441
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00002442 hlist_for_each_entry_safe(filter, node,
2443 &pf->fdir_filter_list, fdir_node) {
2444 i40e_add_del_fdir(vsi, filter, true);
2445 }
2446}
2447
2448/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002449 * i40e_vsi_configure - Set up the VSI for action
2450 * @vsi: the VSI being configured
2451 **/
2452static int i40e_vsi_configure(struct i40e_vsi *vsi)
2453{
2454 int err;
2455
2456 i40e_set_vsi_rx_mode(vsi);
2457 i40e_restore_vlan(vsi);
2458 i40e_vsi_config_dcb_rings(vsi);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00002459 if (vsi->type == I40E_VSI_FDIR)
2460 i40e_fdir_filter_restore(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002461 err = i40e_vsi_configure_tx(vsi);
2462 if (!err)
2463 err = i40e_vsi_configure_rx(vsi);
2464
2465 return err;
2466}
2467
2468/**
2469 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
2470 * @vsi: the VSI being configured
2471 **/
2472static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
2473{
2474 struct i40e_pf *pf = vsi->back;
2475 struct i40e_q_vector *q_vector;
2476 struct i40e_hw *hw = &pf->hw;
2477 u16 vector;
2478 int i, q;
2479 u32 val;
2480 u32 qp;
2481
2482 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
2483 * and PFINT_LNKLSTn registers, e.g.:
2484 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
2485 */
2486 qp = vsi->base_queue;
2487 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00002488 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
2489 q_vector = vsi->q_vectors[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002490 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2491 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2492 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
2493 q_vector->rx.itr);
2494 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2495 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2496 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
2497 q_vector->tx.itr);
2498
2499 /* Linked list for the queuepairs assigned to this vector */
2500 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
2501 for (q = 0; q < q_vector->num_ringpairs; q++) {
2502 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2503 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2504 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
2505 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
2506 (I40E_QUEUE_TYPE_TX
2507 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
2508
2509 wr32(hw, I40E_QINT_RQCTL(qp), val);
2510
2511 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2512 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2513 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
2514 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
2515 (I40E_QUEUE_TYPE_RX
2516 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2517
2518 /* Terminate the linked list */
2519 if (q == (q_vector->num_ringpairs - 1))
2520 val |= (I40E_QUEUE_END_OF_LIST
2521 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2522
2523 wr32(hw, I40E_QINT_TQCTL(qp), val);
2524 qp++;
2525 }
2526 }
2527
2528 i40e_flush(hw);
2529}
2530
2531/**
2532 * i40e_enable_misc_int_causes - enable the non-queue interrupts
2533 * @hw: ptr to the hardware info
2534 **/
2535static void i40e_enable_misc_int_causes(struct i40e_hw *hw)
2536{
2537 u32 val;
2538
2539 /* clear things first */
2540 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
2541 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
2542
2543 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
2544 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
2545 I40E_PFINT_ICR0_ENA_GRST_MASK |
2546 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
2547 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002548 I40E_PFINT_ICR0_ENA_TIMESYNC_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002549 I40E_PFINT_ICR0_ENA_STORM_DETECT_MASK |
2550 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
2551 I40E_PFINT_ICR0_ENA_VFLR_MASK |
2552 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2553
2554 wr32(hw, I40E_PFINT_ICR0_ENA, val);
2555
2556 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00002557 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
2558 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002559
2560 /* OTHER_ITR_IDX = 0 */
2561 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
2562}
2563
2564/**
2565 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
2566 * @vsi: the VSI being configured
2567 **/
2568static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
2569{
Alexander Duyck493fb302013-09-28 07:01:44 +00002570 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002571 struct i40e_pf *pf = vsi->back;
2572 struct i40e_hw *hw = &pf->hw;
2573 u32 val;
2574
2575 /* set the ITR configuration */
2576 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2577 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2578 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
2579 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2580 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2581 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
2582
2583 i40e_enable_misc_int_causes(hw);
2584
2585 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
2586 wr32(hw, I40E_PFINT_LNKLST0, 0);
2587
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00002588 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002589 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2590 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2591 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2592
2593 wr32(hw, I40E_QINT_RQCTL(0), val);
2594
2595 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2596 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2597 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2598
2599 wr32(hw, I40E_QINT_TQCTL(0), val);
2600 i40e_flush(hw);
2601}
2602
2603/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00002604 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
2605 * @pf: board private structure
2606 **/
2607void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
2608{
2609 struct i40e_hw *hw = &pf->hw;
2610
2611 wr32(hw, I40E_PFINT_DYN_CTL0,
2612 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2613 i40e_flush(hw);
2614}
2615
2616/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002617 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
2618 * @pf: board private structure
2619 **/
Shannon Nelson116a57d2013-09-28 07:13:59 +00002620void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002621{
2622 struct i40e_hw *hw = &pf->hw;
2623 u32 val;
2624
2625 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
2626 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
2627 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
2628
2629 wr32(hw, I40E_PFINT_DYN_CTL0, val);
2630 i40e_flush(hw);
2631}
2632
2633/**
2634 * i40e_irq_dynamic_enable - Enable default interrupt generation settings
2635 * @vsi: pointer to a vsi
2636 * @vector: enable a particular Hw Interrupt vector
2637 **/
2638void i40e_irq_dynamic_enable(struct i40e_vsi *vsi, int vector)
2639{
2640 struct i40e_pf *pf = vsi->back;
2641 struct i40e_hw *hw = &pf->hw;
2642 u32 val;
2643
2644 val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
2645 I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
2646 (I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2647 wr32(hw, I40E_PFINT_DYN_CTLN(vector - 1), val);
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00002648 /* skip the flush */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002649}
2650
2651/**
2652 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
2653 * @irq: interrupt number
2654 * @data: pointer to a q_vector
2655 **/
2656static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
2657{
2658 struct i40e_q_vector *q_vector = data;
2659
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002660 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002661 return IRQ_HANDLED;
2662
2663 napi_schedule(&q_vector->napi);
2664
2665 return IRQ_HANDLED;
2666}
2667
2668/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002669 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
2670 * @vsi: the VSI being configured
2671 * @basename: name for the vector
2672 *
2673 * Allocates MSI-X vectors and requests interrupts from the kernel.
2674 **/
2675static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
2676{
2677 int q_vectors = vsi->num_q_vectors;
2678 struct i40e_pf *pf = vsi->back;
2679 int base = vsi->base_vector;
2680 int rx_int_idx = 0;
2681 int tx_int_idx = 0;
2682 int vector, err;
2683
2684 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00002685 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002686
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002687 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002688 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2689 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
2690 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002691 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002692 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2693 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002694 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002695 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2696 "%s-%s-%d", basename, "tx", tx_int_idx++);
2697 } else {
2698 /* skip this unused q_vector */
2699 continue;
2700 }
2701 err = request_irq(pf->msix_entries[base + vector].vector,
2702 vsi->irq_handler,
2703 0,
2704 q_vector->name,
2705 q_vector);
2706 if (err) {
2707 dev_info(&pf->pdev->dev,
2708 "%s: request_irq failed, error: %d\n",
2709 __func__, err);
2710 goto free_queue_irqs;
2711 }
2712 /* assign the mask for this irq */
2713 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2714 &q_vector->affinity_mask);
2715 }
2716
2717 return 0;
2718
2719free_queue_irqs:
2720 while (vector) {
2721 vector--;
2722 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2723 NULL);
2724 free_irq(pf->msix_entries[base + vector].vector,
2725 &(vsi->q_vectors[vector]));
2726 }
2727 return err;
2728}
2729
2730/**
2731 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
2732 * @vsi: the VSI being un-configured
2733 **/
2734static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
2735{
2736 struct i40e_pf *pf = vsi->back;
2737 struct i40e_hw *hw = &pf->hw;
2738 int base = vsi->base_vector;
2739 int i;
2740
2741 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002742 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
2743 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002744 }
2745
2746 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2747 for (i = vsi->base_vector;
2748 i < (vsi->num_q_vectors + vsi->base_vector); i++)
2749 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
2750
2751 i40e_flush(hw);
2752 for (i = 0; i < vsi->num_q_vectors; i++)
2753 synchronize_irq(pf->msix_entries[i + base].vector);
2754 } else {
2755 /* Legacy and MSI mode - this stops all interrupt handling */
2756 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
2757 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
2758 i40e_flush(hw);
2759 synchronize_irq(pf->pdev->irq);
2760 }
2761}
2762
2763/**
2764 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
2765 * @vsi: the VSI being configured
2766 **/
2767static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
2768{
2769 struct i40e_pf *pf = vsi->back;
2770 int i;
2771
2772 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2773 for (i = vsi->base_vector;
2774 i < (vsi->num_q_vectors + vsi->base_vector); i++)
2775 i40e_irq_dynamic_enable(vsi, i);
2776 } else {
2777 i40e_irq_dynamic_enable_icr0(pf);
2778 }
2779
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00002780 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002781 return 0;
2782}
2783
2784/**
2785 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
2786 * @pf: board private structure
2787 **/
2788static void i40e_stop_misc_vector(struct i40e_pf *pf)
2789{
2790 /* Disable ICR 0 */
2791 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
2792 i40e_flush(&pf->hw);
2793}
2794
2795/**
2796 * i40e_intr - MSI/Legacy and non-queue interrupt handler
2797 * @irq: interrupt number
2798 * @data: pointer to a q_vector
2799 *
2800 * This is the handler used for all MSI/Legacy interrupts, and deals
2801 * with both queue and non-queue interrupts. This is also used in
2802 * MSIX mode to handle the non-queue interrupts.
2803 **/
2804static irqreturn_t i40e_intr(int irq, void *data)
2805{
2806 struct i40e_pf *pf = (struct i40e_pf *)data;
2807 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002808 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002809 u32 icr0, icr0_remaining;
2810 u32 val, ena_mask;
2811
2812 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002813 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002814
Shannon Nelson116a57d2013-09-28 07:13:59 +00002815 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
2816 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002817 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002818
Shannon Nelsoncd92e722013-11-16 10:00:44 +00002819 /* if interrupt but no bits showing, must be SWINT */
2820 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
2821 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
2822 pf->sw_int_count++;
2823
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002824 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
2825 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
2826
2827 /* temporarily disable queue cause for NAPI processing */
2828 u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
2829 qval &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
2830 wr32(hw, I40E_QINT_RQCTL(0), qval);
2831
2832 qval = rd32(hw, I40E_QINT_TQCTL(0));
2833 qval &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
2834 wr32(hw, I40E_QINT_TQCTL(0), qval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002835
2836 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck493fb302013-09-28 07:01:44 +00002837 napi_schedule(&pf->vsi[pf->lan_vsi]->q_vectors[0]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002838 }
2839
2840 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
2841 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2842 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
2843 }
2844
2845 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
2846 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
2847 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
2848 }
2849
2850 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
2851 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
2852 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
2853 }
2854
2855 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
2856 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
2857 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
2858 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
2859 val = rd32(hw, I40E_GLGEN_RSTAT);
2860 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
2861 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelsond52cf0a2013-11-16 10:00:39 +00002862 if (val == I40E_RESET_CORER)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002863 pf->corer_count++;
Shannon Nelsond52cf0a2013-11-16 10:00:39 +00002864 else if (val == I40E_RESET_GLOBR)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002865 pf->globr_count++;
Shannon Nelsond52cf0a2013-11-16 10:00:39 +00002866 else if (val == I40E_RESET_EMPR)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002867 pf->empr_count++;
2868 }
2869
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002870 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
2871 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
2872 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
2873 }
2874
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002875 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
2876 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
2877
2878 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
2879 ena_mask &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
2880 i40e_ptp_tx_hwtstamp(pf);
2881 prttsyn_stat &= ~I40E_PRTTSYN_STAT_0_TXTIME_MASK;
2882 }
2883
2884 wr32(hw, I40E_PRTTSYN_STAT_0, prttsyn_stat);
2885 }
2886
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002887 /* If a critical error is pending we have no choice but to reset the
2888 * device.
2889 * Report and mask out any remaining unexpected interrupts.
2890 */
2891 icr0_remaining = icr0 & ena_mask;
2892 if (icr0_remaining) {
2893 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
2894 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002895 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002896 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
2897 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK) ||
2898 (icr0_remaining & I40E_PFINT_ICR0_MAL_DETECT_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002899 dev_info(&pf->pdev->dev, "device will be reset\n");
2900 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
2901 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002902 }
2903 ena_mask &= ~icr0_remaining;
2904 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002905 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002906
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002907enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002908 /* re-enable interrupt causes */
2909 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002910 if (!test_bit(__I40E_DOWN, &pf->state)) {
2911 i40e_service_event_schedule(pf);
2912 i40e_irq_dynamic_enable_icr0(pf);
2913 }
2914
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002915 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002916}
2917
2918/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08002919 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
2920 * @tx_ring: tx ring to clean
2921 * @budget: how many cleans we're allowed
2922 *
2923 * Returns true if there's any budget left (e.g. the clean is finished)
2924 **/
2925static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
2926{
2927 struct i40e_vsi *vsi = tx_ring->vsi;
2928 u16 i = tx_ring->next_to_clean;
2929 struct i40e_tx_buffer *tx_buf;
2930 struct i40e_tx_desc *tx_desc;
2931
2932 tx_buf = &tx_ring->tx_bi[i];
2933 tx_desc = I40E_TX_DESC(tx_ring, i);
2934 i -= tx_ring->count;
2935
2936 do {
2937 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
2938
2939 /* if next_to_watch is not set then there is no work pending */
2940 if (!eop_desc)
2941 break;
2942
2943 /* prevent any other reads prior to eop_desc */
2944 read_barrier_depends();
2945
2946 /* if the descriptor isn't done, no work yet to do */
2947 if (!(eop_desc->cmd_type_offset_bsz &
2948 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
2949 break;
2950
2951 /* clear next_to_watch to prevent false hangs */
2952 tx_buf->next_to_watch = NULL;
2953
2954 /* unmap skb header data */
2955 dma_unmap_single(tx_ring->dev,
2956 dma_unmap_addr(tx_buf, dma),
2957 dma_unmap_len(tx_buf, len),
2958 DMA_TO_DEVICE);
2959
2960 dma_unmap_len_set(tx_buf, len, 0);
2961
2962
2963 /* move to the next desc and buffer to clean */
2964 tx_buf++;
2965 tx_desc++;
2966 i++;
2967 if (unlikely(!i)) {
2968 i -= tx_ring->count;
2969 tx_buf = tx_ring->tx_bi;
2970 tx_desc = I40E_TX_DESC(tx_ring, 0);
2971 }
2972
2973 /* update budget accounting */
2974 budget--;
2975 } while (likely(budget));
2976
2977 i += tx_ring->count;
2978 tx_ring->next_to_clean = i;
2979
2980 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
2981 i40e_irq_dynamic_enable(vsi,
2982 tx_ring->q_vector->v_idx + vsi->base_vector);
2983 }
2984 return budget > 0;
2985}
2986
2987/**
2988 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
2989 * @irq: interrupt number
2990 * @data: pointer to a q_vector
2991 **/
2992static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
2993{
2994 struct i40e_q_vector *q_vector = data;
2995 struct i40e_vsi *vsi;
2996
2997 if (!q_vector->tx.ring)
2998 return IRQ_HANDLED;
2999
3000 vsi = q_vector->tx.ring->vsi;
3001 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3002
3003 return IRQ_HANDLED;
3004}
3005
3006/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003007 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003008 * @vsi: the VSI being configured
3009 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003010 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003011 **/
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003012static void map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003013{
Alexander Duyck493fb302013-09-28 07:01:44 +00003014 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00003015 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3016 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003017
3018 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003019 tx_ring->next = q_vector->tx.ring;
3020 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003021 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003022
3023 rx_ring->q_vector = q_vector;
3024 rx_ring->next = q_vector->rx.ring;
3025 q_vector->rx.ring = rx_ring;
3026 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003027}
3028
3029/**
3030 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3031 * @vsi: the VSI being configured
3032 *
3033 * This function maps descriptor rings to the queue-specific vectors
3034 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3035 * one vector per queue pair, but on a constrained vector budget, we
3036 * group the queue pairs as "efficiently" as possible.
3037 **/
3038static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3039{
3040 int qp_remaining = vsi->num_queue_pairs;
3041 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003042 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003043 int v_start = 0;
3044 int qp_idx = 0;
3045
3046 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3047 * group them so there are multiple queues per vector.
3048 */
3049 for (; v_start < q_vectors && qp_remaining; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003050 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3051
3052 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3053
3054 q_vector->num_ringpairs = num_ringpairs;
3055
3056 q_vector->rx.count = 0;
3057 q_vector->tx.count = 0;
3058 q_vector->rx.ring = NULL;
3059 q_vector->tx.ring = NULL;
3060
3061 while (num_ringpairs--) {
3062 map_vector_to_qp(vsi, v_start, qp_idx);
3063 qp_idx++;
3064 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003065 }
3066 }
3067}
3068
3069/**
3070 * i40e_vsi_request_irq - Request IRQ from the OS
3071 * @vsi: the VSI being configured
3072 * @basename: name for the vector
3073 **/
3074static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3075{
3076 struct i40e_pf *pf = vsi->back;
3077 int err;
3078
3079 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3080 err = i40e_vsi_request_irq_msix(vsi, basename);
3081 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3082 err = request_irq(pf->pdev->irq, i40e_intr, 0,
3083 pf->misc_int_name, pf);
3084 else
3085 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
3086 pf->misc_int_name, pf);
3087
3088 if (err)
3089 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3090
3091 return err;
3092}
3093
3094#ifdef CONFIG_NET_POLL_CONTROLLER
3095/**
3096 * i40e_netpoll - A Polling 'interrupt'handler
3097 * @netdev: network interface device structure
3098 *
3099 * This is used by netconsole to send skbs without having to re-enable
3100 * interrupts. It's not called while the normal interrupt routine is executing.
3101 **/
3102static void i40e_netpoll(struct net_device *netdev)
3103{
3104 struct i40e_netdev_priv *np = netdev_priv(netdev);
3105 struct i40e_vsi *vsi = np->vsi;
3106 struct i40e_pf *pf = vsi->back;
3107 int i;
3108
3109 /* if interface is down do nothing */
3110 if (test_bit(__I40E_DOWN, &vsi->state))
3111 return;
3112
3113 pf->flags |= I40E_FLAG_IN_NETPOLL;
3114 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3115 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003116 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003117 } else {
3118 i40e_intr(pf->pdev->irq, netdev);
3119 }
3120 pf->flags &= ~I40E_FLAG_IN_NETPOLL;
3121}
3122#endif
3123
3124/**
3125 * i40e_vsi_control_tx - Start or stop a VSI's rings
3126 * @vsi: the VSI being configured
3127 * @enable: start or stop the rings
3128 **/
3129static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3130{
3131 struct i40e_pf *pf = vsi->back;
3132 struct i40e_hw *hw = &pf->hw;
3133 int i, j, pf_q;
3134 u32 tx_reg;
3135
3136 pf_q = vsi->base_queue;
3137 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003138 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003139 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003140 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3141 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3142 break;
3143 usleep_range(1000, 2000);
3144 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003145 /* Skip if the queue is already in the requested state */
3146 if (enable && (tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3147 continue;
3148 if (!enable && !(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3149 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003150
3151 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003152 if (enable) {
3153 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003154 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003155 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003156 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003157 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003158
3159 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
3160
3161 /* wait for the change to finish */
3162 for (j = 0; j < 10; j++) {
3163 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
3164 if (enable) {
3165 if ((tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3166 break;
3167 } else {
3168 if (!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3169 break;
3170 }
3171
3172 udelay(10);
3173 }
3174 if (j >= 10) {
3175 dev_info(&pf->pdev->dev, "Tx ring %d %sable timeout\n",
3176 pf_q, (enable ? "en" : "dis"));
3177 return -ETIMEDOUT;
3178 }
3179 }
3180
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003181 if (hw->revision_id == 0)
3182 mdelay(50);
3183
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003184 return 0;
3185}
3186
3187/**
3188 * i40e_vsi_control_rx - Start or stop a VSI's rings
3189 * @vsi: the VSI being configured
3190 * @enable: start or stop the rings
3191 **/
3192static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3193{
3194 struct i40e_pf *pf = vsi->back;
3195 struct i40e_hw *hw = &pf->hw;
3196 int i, j, pf_q;
3197 u32 rx_reg;
3198
3199 pf_q = vsi->base_queue;
3200 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003201 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003202 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003203 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
3204 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
3205 break;
3206 usleep_range(1000, 2000);
3207 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003208
3209 if (enable) {
3210 /* is STAT set ? */
3211 if ((rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3212 continue;
3213 } else {
3214 /* is !STAT set ? */
3215 if (!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3216 continue;
3217 }
3218
3219 /* turn on/off the queue */
3220 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08003221 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003222 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08003223 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003224 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
3225
3226 /* wait for the change to finish */
3227 for (j = 0; j < 10; j++) {
3228 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
3229
3230 if (enable) {
3231 if ((rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3232 break;
3233 } else {
3234 if (!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3235 break;
3236 }
3237
3238 udelay(10);
3239 }
3240 if (j >= 10) {
3241 dev_info(&pf->pdev->dev, "Rx ring %d %sable timeout\n",
3242 pf_q, (enable ? "en" : "dis"));
3243 return -ETIMEDOUT;
3244 }
3245 }
3246
3247 return 0;
3248}
3249
3250/**
3251 * i40e_vsi_control_rings - Start or stop a VSI's rings
3252 * @vsi: the VSI being configured
3253 * @enable: start or stop the rings
3254 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00003255int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003256{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003257 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003258
3259 /* do rx first for enable and last for disable */
3260 if (request) {
3261 ret = i40e_vsi_control_rx(vsi, request);
3262 if (ret)
3263 return ret;
3264 ret = i40e_vsi_control_tx(vsi, request);
3265 } else {
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003266 /* Ignore return value, we need to shutdown whatever we can */
3267 i40e_vsi_control_tx(vsi, request);
3268 i40e_vsi_control_rx(vsi, request);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003269 }
3270
3271 return ret;
3272}
3273
3274/**
3275 * i40e_vsi_free_irq - Free the irq association with the OS
3276 * @vsi: the VSI being configured
3277 **/
3278static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
3279{
3280 struct i40e_pf *pf = vsi->back;
3281 struct i40e_hw *hw = &pf->hw;
3282 int base = vsi->base_vector;
3283 u32 val, qp;
3284 int i;
3285
3286 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3287 if (!vsi->q_vectors)
3288 return;
3289
3290 for (i = 0; i < vsi->num_q_vectors; i++) {
3291 u16 vector = i + base;
3292
3293 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00003294 if (!vsi->q_vectors[i] ||
3295 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003296 continue;
3297
3298 /* clear the affinity_mask in the IRQ descriptor */
3299 irq_set_affinity_hint(pf->msix_entries[vector].vector,
3300 NULL);
3301 free_irq(pf->msix_entries[vector].vector,
Alexander Duyck493fb302013-09-28 07:01:44 +00003302 vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003303
3304 /* Tear down the interrupt queue link list
3305 *
3306 * We know that they come in pairs and always
3307 * the Rx first, then the Tx. To clear the
3308 * link list, stick the EOL value into the
3309 * next_q field of the registers.
3310 */
3311 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
3312 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3313 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3314 val |= I40E_QUEUE_END_OF_LIST
3315 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3316 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
3317
3318 while (qp != I40E_QUEUE_END_OF_LIST) {
3319 u32 next;
3320
3321 val = rd32(hw, I40E_QINT_RQCTL(qp));
3322
3323 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3324 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3325 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3326 I40E_QINT_RQCTL_INTEVENT_MASK);
3327
3328 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3329 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3330
3331 wr32(hw, I40E_QINT_RQCTL(qp), val);
3332
3333 val = rd32(hw, I40E_QINT_TQCTL(qp));
3334
3335 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
3336 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
3337
3338 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3339 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3340 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3341 I40E_QINT_TQCTL_INTEVENT_MASK);
3342
3343 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3344 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3345
3346 wr32(hw, I40E_QINT_TQCTL(qp), val);
3347 qp = next;
3348 }
3349 }
3350 } else {
3351 free_irq(pf->pdev->irq, pf);
3352
3353 val = rd32(hw, I40E_PFINT_LNKLST0);
3354 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3355 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3356 val |= I40E_QUEUE_END_OF_LIST
3357 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
3358 wr32(hw, I40E_PFINT_LNKLST0, val);
3359
3360 val = rd32(hw, I40E_QINT_RQCTL(qp));
3361 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3362 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3363 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3364 I40E_QINT_RQCTL_INTEVENT_MASK);
3365
3366 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3367 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3368
3369 wr32(hw, I40E_QINT_RQCTL(qp), val);
3370
3371 val = rd32(hw, I40E_QINT_TQCTL(qp));
3372
3373 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3374 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3375 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3376 I40E_QINT_TQCTL_INTEVENT_MASK);
3377
3378 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3379 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3380
3381 wr32(hw, I40E_QINT_TQCTL(qp), val);
3382 }
3383}
3384
3385/**
Alexander Duyck493fb302013-09-28 07:01:44 +00003386 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
3387 * @vsi: the VSI being configured
3388 * @v_idx: Index of vector to be freed
3389 *
3390 * This function frees the memory allocated to the q_vector. In addition if
3391 * NAPI is enabled it will delete any references to the NAPI struct prior
3392 * to freeing the q_vector.
3393 **/
3394static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
3395{
3396 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003397 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00003398
3399 if (!q_vector)
3400 return;
3401
3402 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003403 i40e_for_each_ring(ring, q_vector->tx)
3404 ring->q_vector = NULL;
3405
3406 i40e_for_each_ring(ring, q_vector->rx)
3407 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00003408
3409 /* only VSI w/ an associated netdev is set up w/ NAPI */
3410 if (vsi->netdev)
3411 netif_napi_del(&q_vector->napi);
3412
3413 vsi->q_vectors[v_idx] = NULL;
3414
3415 kfree_rcu(q_vector, rcu);
3416}
3417
3418/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003419 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
3420 * @vsi: the VSI being un-configured
3421 *
3422 * This frees the memory allocated to the q_vectors and
3423 * deletes references to the NAPI struct.
3424 **/
3425static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
3426{
3427 int v_idx;
3428
Alexander Duyck493fb302013-09-28 07:01:44 +00003429 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
3430 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003431}
3432
3433/**
3434 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
3435 * @pf: board private structure
3436 **/
3437static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
3438{
3439 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
3440 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3441 pci_disable_msix(pf->pdev);
3442 kfree(pf->msix_entries);
3443 pf->msix_entries = NULL;
3444 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
3445 pci_disable_msi(pf->pdev);
3446 }
3447 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
3448}
3449
3450/**
3451 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
3452 * @pf: board private structure
3453 *
3454 * We go through and clear interrupt specific resources and reset the structure
3455 * to pre-load conditions
3456 **/
3457static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
3458{
3459 int i;
3460
3461 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
3462 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
3463 if (pf->vsi[i])
3464 i40e_vsi_free_q_vectors(pf->vsi[i]);
3465 i40e_reset_interrupt_capability(pf);
3466}
3467
3468/**
3469 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
3470 * @vsi: the VSI being configured
3471 **/
3472static void i40e_napi_enable_all(struct i40e_vsi *vsi)
3473{
3474 int q_idx;
3475
3476 if (!vsi->netdev)
3477 return;
3478
3479 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003480 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003481}
3482
3483/**
3484 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
3485 * @vsi: the VSI being configured
3486 **/
3487static void i40e_napi_disable_all(struct i40e_vsi *vsi)
3488{
3489 int q_idx;
3490
3491 if (!vsi->netdev)
3492 return;
3493
3494 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003495 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003496}
3497
3498/**
3499 * i40e_quiesce_vsi - Pause a given VSI
3500 * @vsi: the VSI being paused
3501 **/
3502static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
3503{
3504 if (test_bit(__I40E_DOWN, &vsi->state))
3505 return;
3506
3507 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
3508 if (vsi->netdev && netif_running(vsi->netdev)) {
3509 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
3510 } else {
3511 set_bit(__I40E_DOWN, &vsi->state);
3512 i40e_down(vsi);
3513 }
3514}
3515
3516/**
3517 * i40e_unquiesce_vsi - Resume a given VSI
3518 * @vsi: the VSI being resumed
3519 **/
3520static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
3521{
3522 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
3523 return;
3524
3525 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
3526 if (vsi->netdev && netif_running(vsi->netdev))
3527 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
3528 else
3529 i40e_up(vsi); /* this clears the DOWN bit */
3530}
3531
3532/**
3533 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
3534 * @pf: the PF
3535 **/
3536static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
3537{
3538 int v;
3539
3540 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
3541 if (pf->vsi[v])
3542 i40e_quiesce_vsi(pf->vsi[v]);
3543 }
3544}
3545
3546/**
3547 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
3548 * @pf: the PF
3549 **/
3550static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
3551{
3552 int v;
3553
3554 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
3555 if (pf->vsi[v])
3556 i40e_unquiesce_vsi(pf->vsi[v]);
3557 }
3558}
3559
3560/**
3561 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
3562 * @dcbcfg: the corresponding DCBx configuration structure
3563 *
3564 * Return the number of TCs from given DCBx configuration
3565 **/
3566static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
3567{
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003568 u8 num_tc = 0;
3569 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003570
3571 /* Scan the ETS Config Priority Table to find
3572 * traffic class enabled for a given priority
3573 * and use the traffic class index to get the
3574 * number of traffic classes enabled
3575 */
3576 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3577 if (dcbcfg->etscfg.prioritytable[i] > num_tc)
3578 num_tc = dcbcfg->etscfg.prioritytable[i];
3579 }
3580
3581 /* Traffic class index starts from zero so
3582 * increment to return the actual count
3583 */
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003584 return num_tc + 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003585}
3586
3587/**
3588 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
3589 * @dcbcfg: the corresponding DCBx configuration structure
3590 *
3591 * Query the current DCB configuration and return the number of
3592 * traffic classes enabled from the given DCBX config
3593 **/
3594static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
3595{
3596 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
3597 u8 enabled_tc = 1;
3598 u8 i;
3599
3600 for (i = 0; i < num_tc; i++)
3601 enabled_tc |= 1 << i;
3602
3603 return enabled_tc;
3604}
3605
3606/**
3607 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
3608 * @pf: PF being queried
3609 *
3610 * Return number of traffic classes enabled for the given PF
3611 **/
3612static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
3613{
3614 struct i40e_hw *hw = &pf->hw;
3615 u8 i, enabled_tc;
3616 u8 num_tc = 0;
3617 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3618
3619 /* If DCB is not enabled then always in single TC */
3620 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3621 return 1;
3622
3623 /* MFP mode return count of enabled TCs for this PF */
3624 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
3625 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3626 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3627 if (enabled_tc & (1 << i))
3628 num_tc++;
3629 }
3630 return num_tc;
3631 }
3632
3633 /* SFP mode will be enabled for all TCs on port */
3634 return i40e_dcb_get_num_tc(dcbcfg);
3635}
3636
3637/**
3638 * i40e_pf_get_default_tc - Get bitmap for first enabled TC
3639 * @pf: PF being queried
3640 *
3641 * Return a bitmap for first enabled traffic class for this PF.
3642 **/
3643static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
3644{
3645 u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3646 u8 i = 0;
3647
3648 if (!enabled_tc)
3649 return 0x1; /* TC0 */
3650
3651 /* Find the first enabled TC */
3652 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3653 if (enabled_tc & (1 << i))
3654 break;
3655 }
3656
3657 return 1 << i;
3658}
3659
3660/**
3661 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
3662 * @pf: PF being queried
3663 *
3664 * Return a bitmap for enabled traffic classes for this PF.
3665 **/
3666static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
3667{
3668 /* If DCB is not enabled for this PF then just return default TC */
3669 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3670 return i40e_pf_get_default_tc(pf);
3671
3672 /* MFP mode will have enabled TCs set by FW */
3673 if (pf->flags & I40E_FLAG_MFP_ENABLED)
3674 return pf->hw.func_caps.enabled_tcmap;
3675
3676 /* SFP mode we want PF to be enabled for all TCs */
3677 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
3678}
3679
3680/**
3681 * i40e_vsi_get_bw_info - Query VSI BW Information
3682 * @vsi: the VSI being queried
3683 *
3684 * Returns 0 on success, negative value on failure
3685 **/
3686static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
3687{
3688 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
3689 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
3690 struct i40e_pf *pf = vsi->back;
3691 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003692 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003693 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003694 int i;
3695
3696 /* Get the VSI level BW configuration */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003697 aq_ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
3698 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003699 dev_info(&pf->pdev->dev,
3700 "couldn't get pf vsi bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003701 aq_ret, pf->hw.aq.asq_last_status);
3702 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003703 }
3704
3705 /* Get the VSI level BW configuration per TC */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003706 aq_ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
Jesse Brandeburg6838b532014-01-14 00:49:52 -08003707 NULL);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003708 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003709 dev_info(&pf->pdev->dev,
3710 "couldn't get pf vsi ets bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003711 aq_ret, pf->hw.aq.asq_last_status);
3712 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003713 }
3714
3715 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
3716 dev_info(&pf->pdev->dev,
3717 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
3718 bw_config.tc_valid_bits,
3719 bw_ets_config.tc_valid_bits);
3720 /* Still continuing */
3721 }
3722
3723 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
3724 vsi->bw_max_quanta = bw_config.max_bw;
3725 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
3726 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
3727 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3728 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
3729 vsi->bw_ets_limit_credits[i] =
3730 le16_to_cpu(bw_ets_config.credits[i]);
3731 /* 3 bits out of 4 for each TC */
3732 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
3733 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003734
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003735 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003736}
3737
3738/**
3739 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
3740 * @vsi: the VSI being configured
3741 * @enabled_tc: TC bitmap
3742 * @bw_credits: BW shared credits per TC
3743 *
3744 * Returns 0 on success, negative value on failure
3745 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003746static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003747 u8 *bw_share)
3748{
3749 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003750 i40e_status aq_ret;
3751 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003752
3753 bw_data.tc_valid_bits = enabled_tc;
3754 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3755 bw_data.tc_bw_credits[i] = bw_share[i];
3756
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003757 aq_ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
3758 NULL);
3759 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003760 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00003761 "AQ command Config VSI BW allocation per TC failed = %d\n",
3762 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003763 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003764 }
3765
3766 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3767 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
3768
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003769 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003770}
3771
3772/**
3773 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
3774 * @vsi: the VSI being configured
3775 * @enabled_tc: TC map to be enabled
3776 *
3777 **/
3778static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3779{
3780 struct net_device *netdev = vsi->netdev;
3781 struct i40e_pf *pf = vsi->back;
3782 struct i40e_hw *hw = &pf->hw;
3783 u8 netdev_tc = 0;
3784 int i;
3785 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3786
3787 if (!netdev)
3788 return;
3789
3790 if (!enabled_tc) {
3791 netdev_reset_tc(netdev);
3792 return;
3793 }
3794
3795 /* Set up actual enabled TCs on the VSI */
3796 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
3797 return;
3798
3799 /* set per TC queues for the VSI */
3800 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3801 /* Only set TC queues for enabled tcs
3802 *
3803 * e.g. For a VSI that has TC0 and TC3 enabled the
3804 * enabled_tc bitmap would be 0x00001001; the driver
3805 * will set the numtc for netdev as 2 that will be
3806 * referenced by the netdev layer as TC 0 and 1.
3807 */
3808 if (vsi->tc_config.enabled_tc & (1 << i))
3809 netdev_set_tc_queue(netdev,
3810 vsi->tc_config.tc_info[i].netdev_tc,
3811 vsi->tc_config.tc_info[i].qcount,
3812 vsi->tc_config.tc_info[i].qoffset);
3813 }
3814
3815 /* Assign UP2TC map for the VSI */
3816 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3817 /* Get the actual TC# for the UP */
3818 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
3819 /* Get the mapped netdev TC# for the UP */
3820 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
3821 netdev_set_prio_tc_map(netdev, i, netdev_tc);
3822 }
3823}
3824
3825/**
3826 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
3827 * @vsi: the VSI being configured
3828 * @ctxt: the ctxt buffer returned from AQ VSI update param command
3829 **/
3830static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
3831 struct i40e_vsi_context *ctxt)
3832{
3833 /* copy just the sections touched not the entire info
3834 * since not all sections are valid as returned by
3835 * update vsi params
3836 */
3837 vsi->info.mapping_flags = ctxt->info.mapping_flags;
3838 memcpy(&vsi->info.queue_mapping,
3839 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
3840 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
3841 sizeof(vsi->info.tc_mapping));
3842}
3843
3844/**
3845 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
3846 * @vsi: VSI to be configured
3847 * @enabled_tc: TC bitmap
3848 *
3849 * This configures a particular VSI for TCs that are mapped to the
3850 * given TC bitmap. It uses default bandwidth share for TCs across
3851 * VSIs to configure TC for a particular VSI.
3852 *
3853 * NOTE:
3854 * It is expected that the VSI queues have been quisced before calling
3855 * this function.
3856 **/
3857static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3858{
3859 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
3860 struct i40e_vsi_context ctxt;
3861 int ret = 0;
3862 int i;
3863
3864 /* Check if enabled_tc is same as existing or new TCs */
3865 if (vsi->tc_config.enabled_tc == enabled_tc)
3866 return ret;
3867
3868 /* Enable ETS TCs with equal BW Share for now across all VSIs */
3869 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3870 if (enabled_tc & (1 << i))
3871 bw_share[i] = 1;
3872 }
3873
3874 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
3875 if (ret) {
3876 dev_info(&vsi->back->pdev->dev,
3877 "Failed configuring TC map %d for VSI %d\n",
3878 enabled_tc, vsi->seid);
3879 goto out;
3880 }
3881
3882 /* Update Queue Pairs Mapping for currently enabled UPs */
3883 ctxt.seid = vsi->seid;
3884 ctxt.pf_num = vsi->back->hw.pf_id;
3885 ctxt.vf_num = 0;
3886 ctxt.uplink_seid = vsi->uplink_seid;
3887 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
3888 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
3889
3890 /* Update the VSI after updating the VSI queue-mapping information */
3891 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3892 if (ret) {
3893 dev_info(&vsi->back->pdev->dev,
3894 "update vsi failed, aq_err=%d\n",
3895 vsi->back->hw.aq.asq_last_status);
3896 goto out;
3897 }
3898 /* update the local VSI info with updated queue map */
3899 i40e_vsi_update_queue_map(vsi, &ctxt);
3900 vsi->info.valid_sections = 0;
3901
3902 /* Update current VSI BW information */
3903 ret = i40e_vsi_get_bw_info(vsi);
3904 if (ret) {
3905 dev_info(&vsi->back->pdev->dev,
3906 "Failed updating vsi bw info, aq_err=%d\n",
3907 vsi->back->hw.aq.asq_last_status);
3908 goto out;
3909 }
3910
3911 /* Update the netdev TC setup */
3912 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
3913out:
3914 return ret;
3915}
3916
3917/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08003918 * i40e_veb_config_tc - Configure TCs for given VEB
3919 * @veb: given VEB
3920 * @enabled_tc: TC bitmap
3921 *
3922 * Configures given TC bitmap for VEB (switching) element
3923 **/
3924int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
3925{
3926 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
3927 struct i40e_pf *pf = veb->pf;
3928 int ret = 0;
3929 int i;
3930
3931 /* No TCs or already enabled TCs just return */
3932 if (!enabled_tc || veb->enabled_tc == enabled_tc)
3933 return ret;
3934
3935 bw_data.tc_valid_bits = enabled_tc;
3936 /* bw_data.absolute_credits is not set (relative) */
3937
3938 /* Enable ETS TCs with equal BW Share for now */
3939 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3940 if (enabled_tc & (1 << i))
3941 bw_data.tc_bw_share_credits[i] = 1;
3942 }
3943
3944 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
3945 &bw_data, NULL);
3946 if (ret) {
3947 dev_info(&pf->pdev->dev,
3948 "veb bw config failed, aq_err=%d\n",
3949 pf->hw.aq.asq_last_status);
3950 goto out;
3951 }
3952
3953 /* Update the BW information */
3954 ret = i40e_veb_get_bw_info(veb);
3955 if (ret) {
3956 dev_info(&pf->pdev->dev,
3957 "Failed getting veb bw config, aq_err=%d\n",
3958 pf->hw.aq.asq_last_status);
3959 }
3960
3961out:
3962 return ret;
3963}
3964
3965#ifdef CONFIG_I40E_DCB
3966/**
3967 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
3968 * @pf: PF struct
3969 *
3970 * Reconfigure VEB/VSIs on a given PF; it is assumed that
3971 * the caller would've quiesce all the VSIs before calling
3972 * this function
3973 **/
3974static void i40e_dcb_reconfigure(struct i40e_pf *pf)
3975{
3976 u8 tc_map = 0;
3977 int ret;
3978 u8 v;
3979
3980 /* Enable the TCs available on PF to all VEBs */
3981 tc_map = i40e_pf_get_tc_map(pf);
3982 for (v = 0; v < I40E_MAX_VEB; v++) {
3983 if (!pf->veb[v])
3984 continue;
3985 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
3986 if (ret) {
3987 dev_info(&pf->pdev->dev,
3988 "Failed configuring TC for VEB seid=%d\n",
3989 pf->veb[v]->seid);
3990 /* Will try to configure as many components */
3991 }
3992 }
3993
3994 /* Update each VSI */
3995 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
3996 if (!pf->vsi[v])
3997 continue;
3998
3999 /* - Enable all TCs for the LAN VSI
4000 * - For all others keep them at TC0 for now
4001 */
4002 if (v == pf->lan_vsi)
4003 tc_map = i40e_pf_get_tc_map(pf);
4004 else
4005 tc_map = i40e_pf_get_default_tc(pf);
4006
4007 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
4008 if (ret) {
4009 dev_info(&pf->pdev->dev,
4010 "Failed configuring TC for VSI seid=%d\n",
4011 pf->vsi[v]->seid);
4012 /* Will try to configure as many components */
4013 } else {
4014 if (pf->vsi[v]->netdev)
4015 i40e_dcbnl_set_all(pf->vsi[v]);
4016 }
4017 }
4018}
4019
4020/**
4021 * i40e_init_pf_dcb - Initialize DCB configuration
4022 * @pf: PF being configured
4023 *
4024 * Query the current DCB configuration and cache it
4025 * in the hardware structure
4026 **/
4027static int i40e_init_pf_dcb(struct i40e_pf *pf)
4028{
4029 struct i40e_hw *hw = &pf->hw;
4030 int err = 0;
4031
4032 if (pf->hw.func_caps.npar_enable)
4033 goto out;
4034
4035 /* Get the initial DCB configuration */
4036 err = i40e_init_dcb(hw);
4037 if (!err) {
4038 /* Device/Function is not DCBX capable */
4039 if ((!hw->func_caps.dcb) ||
4040 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
4041 dev_info(&pf->pdev->dev,
4042 "DCBX offload is not supported or is disabled for this PF.\n");
4043
4044 if (pf->flags & I40E_FLAG_MFP_ENABLED)
4045 goto out;
4046
4047 } else {
4048 /* When status is not DISABLED then DCBX in FW */
4049 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
4050 DCB_CAP_DCBX_VER_IEEE;
4051 pf->flags |= I40E_FLAG_DCB_ENABLED;
4052 }
4053 }
4054
4055out:
4056 return err;
4057}
4058#endif /* CONFIG_I40E_DCB */
4059
4060/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004061 * i40e_up_complete - Finish the last steps of bringing up a connection
4062 * @vsi: the VSI being configured
4063 **/
4064static int i40e_up_complete(struct i40e_vsi *vsi)
4065{
4066 struct i40e_pf *pf = vsi->back;
4067 int err;
4068
4069 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4070 i40e_vsi_configure_msix(vsi);
4071 else
4072 i40e_configure_msi_and_legacy(vsi);
4073
4074 /* start rings */
4075 err = i40e_vsi_control_rings(vsi, true);
4076 if (err)
4077 return err;
4078
4079 clear_bit(__I40E_DOWN, &vsi->state);
4080 i40e_napi_enable_all(vsi);
4081 i40e_vsi_enable_irq(vsi);
4082
4083 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
4084 (vsi->netdev)) {
Anjali Singhai6d779b42013-09-28 06:00:02 +00004085 netdev_info(vsi->netdev, "NIC Link is Up\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004086 netif_tx_start_all_queues(vsi->netdev);
4087 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00004088 } else if (vsi->netdev) {
4089 netdev_info(vsi->netdev, "NIC Link is Down\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004090 }
4091 i40e_service_event_schedule(pf);
4092
4093 return 0;
4094}
4095
4096/**
4097 * i40e_vsi_reinit_locked - Reset the VSI
4098 * @vsi: the VSI being configured
4099 *
4100 * Rebuild the ring structs after some configuration
4101 * has changed, e.g. MTU size.
4102 **/
4103static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
4104{
4105 struct i40e_pf *pf = vsi->back;
4106
4107 WARN_ON(in_interrupt());
4108 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
4109 usleep_range(1000, 2000);
4110 i40e_down(vsi);
4111
4112 /* Give a VF some time to respond to the reset. The
4113 * two second wait is based upon the watchdog cycle in
4114 * the VF driver.
4115 */
4116 if (vsi->type == I40E_VSI_SRIOV)
4117 msleep(2000);
4118 i40e_up(vsi);
4119 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
4120}
4121
4122/**
4123 * i40e_up - Bring the connection back up after being down
4124 * @vsi: the VSI being configured
4125 **/
4126int i40e_up(struct i40e_vsi *vsi)
4127{
4128 int err;
4129
4130 err = i40e_vsi_configure(vsi);
4131 if (!err)
4132 err = i40e_up_complete(vsi);
4133
4134 return err;
4135}
4136
4137/**
4138 * i40e_down - Shutdown the connection processing
4139 * @vsi: the VSI being stopped
4140 **/
4141void i40e_down(struct i40e_vsi *vsi)
4142{
4143 int i;
4144
4145 /* It is assumed that the caller of this function
4146 * sets the vsi->state __I40E_DOWN bit.
4147 */
4148 if (vsi->netdev) {
4149 netif_carrier_off(vsi->netdev);
4150 netif_tx_disable(vsi->netdev);
4151 }
4152 i40e_vsi_disable_irq(vsi);
4153 i40e_vsi_control_rings(vsi, false);
4154 i40e_napi_disable_all(vsi);
4155
4156 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004157 i40e_clean_tx_ring(vsi->tx_rings[i]);
4158 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004159 }
4160}
4161
4162/**
4163 * i40e_setup_tc - configure multiple traffic classes
4164 * @netdev: net device to configure
4165 * @tc: number of traffic classes to enable
4166 **/
4167static int i40e_setup_tc(struct net_device *netdev, u8 tc)
4168{
4169 struct i40e_netdev_priv *np = netdev_priv(netdev);
4170 struct i40e_vsi *vsi = np->vsi;
4171 struct i40e_pf *pf = vsi->back;
4172 u8 enabled_tc = 0;
4173 int ret = -EINVAL;
4174 int i;
4175
4176 /* Check if DCB enabled to continue */
4177 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
4178 netdev_info(netdev, "DCB is not enabled for adapter\n");
4179 goto exit;
4180 }
4181
4182 /* Check if MFP enabled */
4183 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
4184 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
4185 goto exit;
4186 }
4187
4188 /* Check whether tc count is within enabled limit */
4189 if (tc > i40e_pf_get_num_tc(pf)) {
4190 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
4191 goto exit;
4192 }
4193
4194 /* Generate TC map for number of tc requested */
4195 for (i = 0; i < tc; i++)
4196 enabled_tc |= (1 << i);
4197
4198 /* Requesting same TC configuration as already enabled */
4199 if (enabled_tc == vsi->tc_config.enabled_tc)
4200 return 0;
4201
4202 /* Quiesce VSI queues */
4203 i40e_quiesce_vsi(vsi);
4204
4205 /* Configure VSI for enabled TCs */
4206 ret = i40e_vsi_config_tc(vsi, enabled_tc);
4207 if (ret) {
4208 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
4209 vsi->seid);
4210 goto exit;
4211 }
4212
4213 /* Unquiesce VSI */
4214 i40e_unquiesce_vsi(vsi);
4215
4216exit:
4217 return ret;
4218}
4219
4220/**
4221 * i40e_open - Called when a network interface is made active
4222 * @netdev: network interface device structure
4223 *
4224 * The open entry point is called when a network interface is made
4225 * active by the system (IFF_UP). At this point all resources needed
4226 * for transmit and receive operations are allocated, the interrupt
4227 * handler is registered with the OS, the netdev watchdog subtask is
4228 * enabled, and the stack is notified that the interface is ready.
4229 *
4230 * Returns 0 on success, negative value on failure
4231 **/
4232static int i40e_open(struct net_device *netdev)
4233{
4234 struct i40e_netdev_priv *np = netdev_priv(netdev);
4235 struct i40e_vsi *vsi = np->vsi;
4236 struct i40e_pf *pf = vsi->back;
4237 char int_name[IFNAMSIZ];
4238 int err;
4239
4240 /* disallow open during test */
4241 if (test_bit(__I40E_TESTING, &pf->state))
4242 return -EBUSY;
4243
4244 netif_carrier_off(netdev);
4245
4246 /* allocate descriptors */
4247 err = i40e_vsi_setup_tx_resources(vsi);
4248 if (err)
4249 goto err_setup_tx;
4250 err = i40e_vsi_setup_rx_resources(vsi);
4251 if (err)
4252 goto err_setup_rx;
4253
4254 err = i40e_vsi_configure(vsi);
4255 if (err)
4256 goto err_setup_rx;
4257
4258 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
4259 dev_driver_string(&pf->pdev->dev), netdev->name);
4260 err = i40e_vsi_request_irq(vsi, int_name);
4261 if (err)
4262 goto err_setup_rx;
4263
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004264 /* Notify the stack of the actual queue counts. */
Neerav Parikhd7397642013-11-28 06:39:37 +00004265 err = netif_set_real_num_tx_queues(netdev, vsi->num_queue_pairs);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004266 if (err)
4267 goto err_set_queues;
4268
Neerav Parikhd7397642013-11-28 06:39:37 +00004269 err = netif_set_real_num_rx_queues(netdev, vsi->num_queue_pairs);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004270 if (err)
4271 goto err_set_queues;
4272
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004273 err = i40e_up_complete(vsi);
4274 if (err)
4275 goto err_up_complete;
4276
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00004277#ifdef CONFIG_I40E_VXLAN
4278 vxlan_get_rx_port(netdev);
4279#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004280
4281 return 0;
4282
4283err_up_complete:
4284 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004285err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004286 i40e_vsi_free_irq(vsi);
4287err_setup_rx:
4288 i40e_vsi_free_rx_resources(vsi);
4289err_setup_tx:
4290 i40e_vsi_free_tx_resources(vsi);
4291 if (vsi == pf->vsi[pf->lan_vsi])
4292 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
4293
4294 return err;
4295}
4296
4297/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00004298 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
4299 * @pf: Pointer to pf
4300 *
4301 * This function destroys the hlist where all the Flow Director
4302 * filters were saved.
4303 **/
4304static void i40e_fdir_filter_exit(struct i40e_pf *pf)
4305{
4306 struct i40e_fdir_filter *filter;
4307 struct hlist_node *node2;
4308
4309 hlist_for_each_entry_safe(filter, node2,
4310 &pf->fdir_filter_list, fdir_node) {
4311 hlist_del(&filter->fdir_node);
4312 kfree(filter);
4313 }
4314 pf->fdir_pf_active_filters = 0;
4315}
4316
4317/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004318 * i40e_close - Disables a network interface
4319 * @netdev: network interface device structure
4320 *
4321 * The close entry point is called when an interface is de-activated
4322 * by the OS. The hardware is still under the driver's control, but
4323 * this netdev interface is disabled.
4324 *
4325 * Returns 0, this is not allowed to fail
4326 **/
4327static int i40e_close(struct net_device *netdev)
4328{
4329 struct i40e_netdev_priv *np = netdev_priv(netdev);
4330 struct i40e_vsi *vsi = np->vsi;
4331
4332 if (test_and_set_bit(__I40E_DOWN, &vsi->state))
4333 return 0;
4334
4335 i40e_down(vsi);
4336 i40e_vsi_free_irq(vsi);
4337
4338 i40e_vsi_free_tx_resources(vsi);
4339 i40e_vsi_free_rx_resources(vsi);
4340
4341 return 0;
4342}
4343
4344/**
4345 * i40e_do_reset - Start a PF or Core Reset sequence
4346 * @pf: board private structure
4347 * @reset_flags: which reset is requested
4348 *
4349 * The essential difference in resets is that the PF Reset
4350 * doesn't clear the packet buffers, doesn't reset the PE
4351 * firmware, and doesn't bother the other PFs on the chip.
4352 **/
4353void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
4354{
4355 u32 val;
4356
4357 WARN_ON(in_interrupt());
4358
4359 /* do the biggest reset indicated */
4360 if (reset_flags & (1 << __I40E_GLOBAL_RESET_REQUESTED)) {
4361
4362 /* Request a Global Reset
4363 *
4364 * This will start the chip's countdown to the actual full
4365 * chip reset event, and a warning interrupt to be sent
4366 * to all PFs, including the requestor. Our handler
4367 * for the warning interrupt will deal with the shutdown
4368 * and recovery of the switch setup.
4369 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004370 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004371 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4372 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
4373 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4374
4375 } else if (reset_flags & (1 << __I40E_CORE_RESET_REQUESTED)) {
4376
4377 /* Request a Core Reset
4378 *
4379 * Same as Global Reset, except does *not* include the MAC/PHY
4380 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004381 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004382 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4383 val |= I40E_GLGEN_RTRIG_CORER_MASK;
4384 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4385 i40e_flush(&pf->hw);
4386
Shannon Nelson7823fe32013-11-16 10:00:45 +00004387 } else if (reset_flags & (1 << __I40E_EMP_RESET_REQUESTED)) {
4388
4389 /* Request a Firmware Reset
4390 *
4391 * Same as Global reset, plus restarting the
4392 * embedded firmware engine.
4393 */
4394 /* enable EMP Reset */
4395 val = rd32(&pf->hw, I40E_GLGEN_RSTENA_EMP);
4396 val |= I40E_GLGEN_RSTENA_EMP_EMP_RST_ENA_MASK;
4397 wr32(&pf->hw, I40E_GLGEN_RSTENA_EMP, val);
4398
4399 /* force the reset */
4400 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4401 val |= I40E_GLGEN_RTRIG_EMPFWR_MASK;
4402 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4403 i40e_flush(&pf->hw);
4404
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004405 } else if (reset_flags & (1 << __I40E_PF_RESET_REQUESTED)) {
4406
4407 /* Request a PF Reset
4408 *
4409 * Resets only the PF-specific registers
4410 *
4411 * This goes directly to the tear-down and rebuild of
4412 * the switch, since we need to do all the recovery as
4413 * for the Core Reset.
4414 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004415 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004416 i40e_handle_reset_warning(pf);
4417
4418 } else if (reset_flags & (1 << __I40E_REINIT_REQUESTED)) {
4419 int v;
4420
4421 /* Find the VSI(s) that requested a re-init */
4422 dev_info(&pf->pdev->dev,
4423 "VSI reinit requested\n");
4424 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4425 struct i40e_vsi *vsi = pf->vsi[v];
4426 if (vsi != NULL &&
4427 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
4428 i40e_vsi_reinit_locked(pf->vsi[v]);
4429 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
4430 }
4431 }
4432
4433 /* no further action needed, so return now */
4434 return;
4435 } else {
4436 dev_info(&pf->pdev->dev,
4437 "bad reset request 0x%08x\n", reset_flags);
4438 return;
4439 }
4440}
4441
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004442#ifdef CONFIG_I40E_DCB
4443/**
4444 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
4445 * @pf: board private structure
4446 * @old_cfg: current DCB config
4447 * @new_cfg: new DCB config
4448 **/
4449bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
4450 struct i40e_dcbx_config *old_cfg,
4451 struct i40e_dcbx_config *new_cfg)
4452{
4453 bool need_reconfig = false;
4454
4455 /* Check if ETS configuration has changed */
4456 if (memcmp(&new_cfg->etscfg,
4457 &old_cfg->etscfg,
4458 sizeof(new_cfg->etscfg))) {
4459 /* If Priority Table has changed reconfig is needed */
4460 if (memcmp(&new_cfg->etscfg.prioritytable,
4461 &old_cfg->etscfg.prioritytable,
4462 sizeof(new_cfg->etscfg.prioritytable))) {
4463 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004464 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004465 }
4466
4467 if (memcmp(&new_cfg->etscfg.tcbwtable,
4468 &old_cfg->etscfg.tcbwtable,
4469 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004470 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004471
4472 if (memcmp(&new_cfg->etscfg.tsatable,
4473 &old_cfg->etscfg.tsatable,
4474 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004475 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004476 }
4477
4478 /* Check if PFC configuration has changed */
4479 if (memcmp(&new_cfg->pfc,
4480 &old_cfg->pfc,
4481 sizeof(new_cfg->pfc))) {
4482 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004483 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004484 }
4485
4486 /* Check if APP Table has changed */
4487 if (memcmp(&new_cfg->app,
4488 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05004489 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004490 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004491 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05004492 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004493
4494 return need_reconfig;
4495}
4496
4497/**
4498 * i40e_handle_lldp_event - Handle LLDP Change MIB event
4499 * @pf: board private structure
4500 * @e: event info posted on ARQ
4501 **/
4502static int i40e_handle_lldp_event(struct i40e_pf *pf,
4503 struct i40e_arq_event_info *e)
4504{
4505 struct i40e_aqc_lldp_get_mib *mib =
4506 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
4507 struct i40e_hw *hw = &pf->hw;
4508 struct i40e_dcbx_config *dcbx_cfg = &hw->local_dcbx_config;
4509 struct i40e_dcbx_config tmp_dcbx_cfg;
4510 bool need_reconfig = false;
4511 int ret = 0;
4512 u8 type;
4513
4514 /* Ignore if event is not for Nearest Bridge */
4515 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
4516 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
4517 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
4518 return ret;
4519
4520 /* Check MIB Type and return if event for Remote MIB update */
4521 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
4522 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
4523 /* Update the remote cached instance and return */
4524 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
4525 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
4526 &hw->remote_dcbx_config);
4527 goto exit;
4528 }
4529
4530 /* Convert/store the DCBX data from LLDPDU temporarily */
4531 memset(&tmp_dcbx_cfg, 0, sizeof(tmp_dcbx_cfg));
4532 ret = i40e_lldp_to_dcb_config(e->msg_buf, &tmp_dcbx_cfg);
4533 if (ret) {
4534 /* Error in LLDPDU parsing return */
4535 dev_info(&pf->pdev->dev, "Failed parsing LLDPDU from event buffer\n");
4536 goto exit;
4537 }
4538
4539 /* No change detected in DCBX configs */
4540 if (!memcmp(&tmp_dcbx_cfg, dcbx_cfg, sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004541 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004542 goto exit;
4543 }
4544
4545 need_reconfig = i40e_dcb_need_reconfig(pf, dcbx_cfg, &tmp_dcbx_cfg);
4546
4547 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg);
4548
4549 /* Overwrite the new configuration */
4550 *dcbx_cfg = tmp_dcbx_cfg;
4551
4552 if (!need_reconfig)
4553 goto exit;
4554
4555 /* Reconfiguration needed quiesce all VSIs */
4556 i40e_pf_quiesce_all_vsi(pf);
4557
4558 /* Changes in configuration update VEB/VSI */
4559 i40e_dcb_reconfigure(pf);
4560
4561 i40e_pf_unquiesce_all_vsi(pf);
4562exit:
4563 return ret;
4564}
4565#endif /* CONFIG_I40E_DCB */
4566
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004567/**
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004568 * i40e_do_reset_safe - Protected reset path for userland calls.
4569 * @pf: board private structure
4570 * @reset_flags: which reset is requested
4571 *
4572 **/
4573void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
4574{
4575 rtnl_lock();
4576 i40e_do_reset(pf, reset_flags);
4577 rtnl_unlock();
4578}
4579
4580/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004581 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
4582 * @pf: board private structure
4583 * @e: event info posted on ARQ
4584 *
4585 * Handler for LAN Queue Overflow Event generated by the firmware for PF
4586 * and VF queues
4587 **/
4588static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
4589 struct i40e_arq_event_info *e)
4590{
4591 struct i40e_aqc_lan_overflow *data =
4592 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
4593 u32 queue = le32_to_cpu(data->prtdcb_rupto);
4594 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
4595 struct i40e_hw *hw = &pf->hw;
4596 struct i40e_vf *vf;
4597 u16 vf_id;
4598
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004599 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
4600 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004601
4602 /* Queue belongs to VF, find the VF and issue VF reset */
4603 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
4604 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
4605 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
4606 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
4607 vf_id -= hw->func_caps.vf_base_id;
4608 vf = &pf->vf[vf_id];
4609 i40e_vc_notify_vf_reset(vf);
4610 /* Allow VF to process pending reset notification */
4611 msleep(20);
4612 i40e_reset_vf(vf, false);
4613 }
4614}
4615
4616/**
4617 * i40e_service_event_complete - Finish up the service event
4618 * @pf: board private structure
4619 **/
4620static void i40e_service_event_complete(struct i40e_pf *pf)
4621{
4622 BUG_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
4623
4624 /* flush memory to make sure state is correct before next watchog */
4625 smp_mb__before_clear_bit();
4626 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
4627}
4628
4629/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004630 * i40e_get_current_fd_count - Get the count of FD filters programmed in the HW
4631 * @pf: board private structure
4632 **/
4633int i40e_get_current_fd_count(struct i40e_pf *pf)
4634{
4635 int val, fcnt_prog;
4636 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
4637 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
4638 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
4639 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
4640 return fcnt_prog;
4641}
4642
4643/**
4644 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
4645 * @pf: board private structure
4646 **/
4647void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
4648{
4649 u32 fcnt_prog, fcnt_avail;
4650
4651 /* Check if, FD SB or ATR was auto disabled and if there is enough room
4652 * to re-enable
4653 */
4654 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4655 (pf->flags & I40E_FLAG_FD_SB_ENABLED))
4656 return;
4657 fcnt_prog = i40e_get_current_fd_count(pf);
4658 fcnt_avail = pf->hw.fdir_shared_filter_count +
4659 pf->fdir_pf_filter_count;
4660 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) {
4661 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
4662 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
4663 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
4664 dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
4665 }
4666 }
4667 /* Wait for some more space to be available to turn on ATR */
4668 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
4669 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4670 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
4671 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
4672 dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table now\n");
4673 }
4674 }
4675}
4676
4677/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004678 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
4679 * @pf: board private structure
4680 **/
4681static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
4682{
4683 if (!(pf->flags & I40E_FLAG_FDIR_REQUIRES_REINIT))
4684 return;
4685
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004686 /* if interface is down do nothing */
4687 if (test_bit(__I40E_DOWN, &pf->state))
4688 return;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004689 i40e_fdir_check_and_reenable(pf);
4690
4691 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4692 (pf->flags & I40E_FLAG_FD_SB_ENABLED))
4693 pf->flags &= ~I40E_FLAG_FDIR_REQUIRES_REINIT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004694}
4695
4696/**
4697 * i40e_vsi_link_event - notify VSI of a link event
4698 * @vsi: vsi to be notified
4699 * @link_up: link up or down
4700 **/
4701static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
4702{
4703 if (!vsi)
4704 return;
4705
4706 switch (vsi->type) {
4707 case I40E_VSI_MAIN:
4708 if (!vsi->netdev || !vsi->netdev_registered)
4709 break;
4710
4711 if (link_up) {
4712 netif_carrier_on(vsi->netdev);
4713 netif_tx_wake_all_queues(vsi->netdev);
4714 } else {
4715 netif_carrier_off(vsi->netdev);
4716 netif_tx_stop_all_queues(vsi->netdev);
4717 }
4718 break;
4719
4720 case I40E_VSI_SRIOV:
4721 break;
4722
4723 case I40E_VSI_VMDQ2:
4724 case I40E_VSI_CTRL:
4725 case I40E_VSI_MIRROR:
4726 default:
4727 /* there is no notification for other VSIs */
4728 break;
4729 }
4730}
4731
4732/**
4733 * i40e_veb_link_event - notify elements on the veb of a link event
4734 * @veb: veb to be notified
4735 * @link_up: link up or down
4736 **/
4737static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
4738{
4739 struct i40e_pf *pf;
4740 int i;
4741
4742 if (!veb || !veb->pf)
4743 return;
4744 pf = veb->pf;
4745
4746 /* depth first... */
4747 for (i = 0; i < I40E_MAX_VEB; i++)
4748 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
4749 i40e_veb_link_event(pf->veb[i], link_up);
4750
4751 /* ... now the local VSIs */
4752 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
4753 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
4754 i40e_vsi_link_event(pf->vsi[i], link_up);
4755}
4756
4757/**
4758 * i40e_link_event - Update netif_carrier status
4759 * @pf: board private structure
4760 **/
4761static void i40e_link_event(struct i40e_pf *pf)
4762{
4763 bool new_link, old_link;
4764
4765 new_link = (pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP);
4766 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
4767
4768 if (new_link == old_link)
4769 return;
4770
Anjali Singhai6d779b42013-09-28 06:00:02 +00004771 if (!test_bit(__I40E_DOWN, &pf->vsi[pf->lan_vsi]->state))
4772 netdev_info(pf->vsi[pf->lan_vsi]->netdev,
4773 "NIC Link is %s\n", (new_link ? "Up" : "Down"));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004774
4775 /* Notify the base of the switch tree connected to
4776 * the link. Floating VEBs are not notified.
4777 */
4778 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
4779 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
4780 else
4781 i40e_vsi_link_event(pf->vsi[pf->lan_vsi], new_link);
4782
4783 if (pf->vf)
4784 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00004785
4786 if (pf->flags & I40E_FLAG_PTP)
4787 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004788}
4789
4790/**
4791 * i40e_check_hang_subtask - Check for hung queues and dropped interrupts
4792 * @pf: board private structure
4793 *
4794 * Set the per-queue flags to request a check for stuck queues in the irq
4795 * clean functions, then force interrupts to be sure the irq clean is called.
4796 **/
4797static void i40e_check_hang_subtask(struct i40e_pf *pf)
4798{
4799 int i, v;
4800
4801 /* If we're down or resetting, just bail */
4802 if (test_bit(__I40E_CONFIG_BUSY, &pf->state))
4803 return;
4804
4805 /* for each VSI/netdev
4806 * for each Tx queue
4807 * set the check flag
4808 * for each q_vector
4809 * force an interrupt
4810 */
4811 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4812 struct i40e_vsi *vsi = pf->vsi[v];
4813 int armed = 0;
4814
4815 if (!pf->vsi[v] ||
4816 test_bit(__I40E_DOWN, &vsi->state) ||
4817 (vsi->netdev && !netif_carrier_ok(vsi->netdev)))
4818 continue;
4819
4820 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004821 set_check_for_tx_hang(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004822 if (test_bit(__I40E_HANG_CHECK_ARMED,
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004823 &vsi->tx_rings[i]->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004824 armed++;
4825 }
4826
4827 if (armed) {
4828 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
4829 wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0,
4830 (I40E_PFINT_DYN_CTL0_INTENA_MASK |
4831 I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK));
4832 } else {
4833 u16 vec = vsi->base_vector - 1;
4834 u32 val = (I40E_PFINT_DYN_CTLN_INTENA_MASK |
4835 I40E_PFINT_DYN_CTLN_SWINT_TRIG_MASK);
4836 for (i = 0; i < vsi->num_q_vectors; i++, vec++)
4837 wr32(&vsi->back->hw,
4838 I40E_PFINT_DYN_CTLN(vec), val);
4839 }
4840 i40e_flush(&vsi->back->hw);
4841 }
4842 }
4843}
4844
4845/**
4846 * i40e_watchdog_subtask - Check and bring link up
4847 * @pf: board private structure
4848 **/
4849static void i40e_watchdog_subtask(struct i40e_pf *pf)
4850{
4851 int i;
4852
4853 /* if interface is down do nothing */
4854 if (test_bit(__I40E_DOWN, &pf->state) ||
4855 test_bit(__I40E_CONFIG_BUSY, &pf->state))
4856 return;
4857
4858 /* Update the stats for active netdevs so the network stack
4859 * can look at updated numbers whenever it cares to
4860 */
4861 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
4862 if (pf->vsi[i] && pf->vsi[i]->netdev)
4863 i40e_update_stats(pf->vsi[i]);
4864
4865 /* Update the stats for the active switching components */
4866 for (i = 0; i < I40E_MAX_VEB; i++)
4867 if (pf->veb[i])
4868 i40e_update_veb_stats(pf->veb[i]);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00004869
4870 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004871}
4872
4873/**
4874 * i40e_reset_subtask - Set up for resetting the device and driver
4875 * @pf: board private structure
4876 **/
4877static void i40e_reset_subtask(struct i40e_pf *pf)
4878{
4879 u32 reset_flags = 0;
4880
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004881 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004882 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
4883 reset_flags |= (1 << __I40E_REINIT_REQUESTED);
4884 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
4885 }
4886 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
4887 reset_flags |= (1 << __I40E_PF_RESET_REQUESTED);
4888 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
4889 }
4890 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
4891 reset_flags |= (1 << __I40E_CORE_RESET_REQUESTED);
4892 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
4893 }
4894 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
4895 reset_flags |= (1 << __I40E_GLOBAL_RESET_REQUESTED);
4896 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
4897 }
4898
4899 /* If there's a recovery already waiting, it takes
4900 * precedence before starting a new reset sequence.
4901 */
4902 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
4903 i40e_handle_reset_warning(pf);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004904 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004905 }
4906
4907 /* If we're already down or resetting, just bail */
4908 if (reset_flags &&
4909 !test_bit(__I40E_DOWN, &pf->state) &&
4910 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
4911 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004912
4913unlock:
4914 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004915}
4916
4917/**
4918 * i40e_handle_link_event - Handle link event
4919 * @pf: board private structure
4920 * @e: event info posted on ARQ
4921 **/
4922static void i40e_handle_link_event(struct i40e_pf *pf,
4923 struct i40e_arq_event_info *e)
4924{
4925 struct i40e_hw *hw = &pf->hw;
4926 struct i40e_aqc_get_link_status *status =
4927 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
4928 struct i40e_link_status *hw_link_info = &hw->phy.link_info;
4929
4930 /* save off old link status information */
4931 memcpy(&pf->hw.phy.link_info_old, hw_link_info,
4932 sizeof(pf->hw.phy.link_info_old));
4933
4934 /* update link status */
4935 hw_link_info->phy_type = (enum i40e_aq_phy_type)status->phy_type;
4936 hw_link_info->link_speed = (enum i40e_aq_link_speed)status->link_speed;
4937 hw_link_info->link_info = status->link_info;
4938 hw_link_info->an_info = status->an_info;
4939 hw_link_info->ext_info = status->ext_info;
4940 hw_link_info->lse_enable =
4941 le16_to_cpu(status->command_flags) &
4942 I40E_AQ_LSE_ENABLE;
4943
4944 /* process the event */
4945 i40e_link_event(pf);
4946
4947 /* Do a new status request to re-enable LSE reporting
4948 * and load new status information into the hw struct,
4949 * then see if the status changed while processing the
4950 * initial event.
4951 */
4952 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
4953 i40e_link_event(pf);
4954}
4955
4956/**
4957 * i40e_clean_adminq_subtask - Clean the AdminQ rings
4958 * @pf: board private structure
4959 **/
4960static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
4961{
4962 struct i40e_arq_event_info event;
4963 struct i40e_hw *hw = &pf->hw;
4964 u16 pending, i = 0;
4965 i40e_status ret;
4966 u16 opcode;
4967 u32 val;
4968
4969 if (!test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state))
4970 return;
4971
Mitch Williams3197ce22013-11-28 06:39:39 +00004972 event.msg_size = I40E_MAX_AQ_BUF_SIZE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004973 event.msg_buf = kzalloc(event.msg_size, GFP_KERNEL);
4974 if (!event.msg_buf)
4975 return;
4976
4977 do {
Mitch Williams2f019122013-11-28 06:39:33 +00004978 event.msg_size = I40E_MAX_AQ_BUF_SIZE; /* reinit each time */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004979 ret = i40e_clean_arq_element(hw, &event, &pending);
4980 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
4981 dev_info(&pf->pdev->dev, "No ARQ event found\n");
4982 break;
4983 } else if (ret) {
4984 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
4985 break;
4986 }
4987
4988 opcode = le16_to_cpu(event.desc.opcode);
4989 switch (opcode) {
4990
4991 case i40e_aqc_opc_get_link_status:
4992 i40e_handle_link_event(pf, &event);
4993 break;
4994 case i40e_aqc_opc_send_msg_to_pf:
4995 ret = i40e_vc_process_vf_msg(pf,
4996 le16_to_cpu(event.desc.retval),
4997 le32_to_cpu(event.desc.cookie_high),
4998 le32_to_cpu(event.desc.cookie_low),
4999 event.msg_buf,
5000 event.msg_size);
5001 break;
5002 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005003 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005004#ifdef CONFIG_I40E_DCB
5005 rtnl_lock();
5006 ret = i40e_handle_lldp_event(pf, &event);
5007 rtnl_unlock();
5008#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005009 break;
5010 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005011 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005012 i40e_handle_lan_overflow_event(pf, &event);
5013 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00005014 case i40e_aqc_opc_send_msg_to_peer:
5015 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
5016 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005017 default:
5018 dev_info(&pf->pdev->dev,
Shannon Nelson0467bc92013-12-18 13:45:58 +00005019 "ARQ Error: Unknown event 0x%04x received\n",
5020 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005021 break;
5022 }
5023 } while (pending && (i++ < pf->adminq_work_limit));
5024
5025 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
5026 /* re-enable Admin queue interrupt cause */
5027 val = rd32(hw, I40E_PFINT_ICR0_ENA);
5028 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
5029 wr32(hw, I40E_PFINT_ICR0_ENA, val);
5030 i40e_flush(hw);
5031
5032 kfree(event.msg_buf);
5033}
5034
5035/**
5036 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
5037 * @veb: pointer to the VEB instance
5038 *
5039 * This is a recursive function that first builds the attached VSIs then
5040 * recurses in to build the next layer of VEB. We track the connections
5041 * through our own index numbers because the seid's from the HW could
5042 * change across the reset.
5043 **/
5044static int i40e_reconstitute_veb(struct i40e_veb *veb)
5045{
5046 struct i40e_vsi *ctl_vsi = NULL;
5047 struct i40e_pf *pf = veb->pf;
5048 int v, veb_idx;
5049 int ret;
5050
5051 /* build VSI that owns this VEB, temporarily attached to base VEB */
5052 for (v = 0; v < pf->hw.func_caps.num_vsis && !ctl_vsi; v++) {
5053 if (pf->vsi[v] &&
5054 pf->vsi[v]->veb_idx == veb->idx &&
5055 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
5056 ctl_vsi = pf->vsi[v];
5057 break;
5058 }
5059 }
5060 if (!ctl_vsi) {
5061 dev_info(&pf->pdev->dev,
5062 "missing owner VSI for veb_idx %d\n", veb->idx);
5063 ret = -ENOENT;
5064 goto end_reconstitute;
5065 }
5066 if (ctl_vsi != pf->vsi[pf->lan_vsi])
5067 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
5068 ret = i40e_add_vsi(ctl_vsi);
5069 if (ret) {
5070 dev_info(&pf->pdev->dev,
5071 "rebuild of owner VSI failed: %d\n", ret);
5072 goto end_reconstitute;
5073 }
5074 i40e_vsi_reset_stats(ctl_vsi);
5075
5076 /* create the VEB in the switch and move the VSI onto the VEB */
5077 ret = i40e_add_veb(veb, ctl_vsi);
5078 if (ret)
5079 goto end_reconstitute;
5080
5081 /* create the remaining VSIs attached to this VEB */
5082 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5083 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
5084 continue;
5085
5086 if (pf->vsi[v]->veb_idx == veb->idx) {
5087 struct i40e_vsi *vsi = pf->vsi[v];
5088 vsi->uplink_seid = veb->seid;
5089 ret = i40e_add_vsi(vsi);
5090 if (ret) {
5091 dev_info(&pf->pdev->dev,
5092 "rebuild of vsi_idx %d failed: %d\n",
5093 v, ret);
5094 goto end_reconstitute;
5095 }
5096 i40e_vsi_reset_stats(vsi);
5097 }
5098 }
5099
5100 /* create any VEBs attached to this VEB - RECURSION */
5101 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
5102 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
5103 pf->veb[veb_idx]->uplink_seid = veb->seid;
5104 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
5105 if (ret)
5106 break;
5107 }
5108 }
5109
5110end_reconstitute:
5111 return ret;
5112}
5113
5114/**
5115 * i40e_get_capabilities - get info about the HW
5116 * @pf: the PF struct
5117 **/
5118static int i40e_get_capabilities(struct i40e_pf *pf)
5119{
5120 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
5121 u16 data_size;
5122 int buf_len;
5123 int err;
5124
5125 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
5126 do {
5127 cap_buf = kzalloc(buf_len, GFP_KERNEL);
5128 if (!cap_buf)
5129 return -ENOMEM;
5130
5131 /* this loads the data into the hw struct for us */
5132 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
5133 &data_size,
5134 i40e_aqc_opc_list_func_capabilities,
5135 NULL);
5136 /* data loaded, buffer no longer needed */
5137 kfree(cap_buf);
5138
5139 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
5140 /* retry with a larger buffer */
5141 buf_len = data_size;
5142 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
5143 dev_info(&pf->pdev->dev,
5144 "capability discovery failed: aq=%d\n",
5145 pf->hw.aq.asq_last_status);
5146 return -ENODEV;
5147 }
5148 } while (err);
5149
Jesse Brandeburgd0b10242014-01-17 15:36:32 -08005150 /* increment MSI-X count because current FW skips one */
5151 pf->hw.func_caps.num_msix_vectors++;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00005152
Anjali Singhai Jainac71b7b2014-02-06 05:51:08 +00005153 if (((pf->hw.aq.fw_maj_ver == 2) && (pf->hw.aq.fw_min_ver < 22)) ||
5154 (pf->hw.aq.fw_maj_ver < 2)) {
5155 pf->hw.func_caps.num_msix_vectors++;
5156 pf->hw.func_caps.num_msix_vectors_vf++;
5157 }
5158
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005159 if (pf->hw.debug_mask & I40E_DEBUG_USER)
5160 dev_info(&pf->pdev->dev,
5161 "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",
5162 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
5163 pf->hw.func_caps.num_msix_vectors,
5164 pf->hw.func_caps.num_msix_vectors_vf,
5165 pf->hw.func_caps.fd_filters_guaranteed,
5166 pf->hw.func_caps.fd_filters_best_effort,
5167 pf->hw.func_caps.num_tx_qp,
5168 pf->hw.func_caps.num_vsis);
5169
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00005170#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
5171 + pf->hw.func_caps.num_vfs)
5172 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
5173 dev_info(&pf->pdev->dev,
5174 "got num_vsis %d, setting num_vsis to %d\n",
5175 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
5176 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
5177 }
5178
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005179 return 0;
5180}
5181
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005182static int i40e_vsi_clear(struct i40e_vsi *vsi);
5183
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005184/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005185 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005186 * @pf: board private structure
5187 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005188static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005189{
5190 struct i40e_vsi *vsi;
5191 bool new_vsi = false;
5192 int err, i;
5193
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005194 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005195 return;
5196
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005197 /* find existing VSI and see if it needs configuring */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005198 vsi = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005199 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
5200 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005201 vsi = pf->vsi[i];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005202 break;
5203 }
5204 }
5205
5206 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005207 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005208 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
5209 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005210 if (!vsi) {
5211 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005212 goto err_vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005213 }
5214 new_vsi = true;
5215 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005216 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005217
5218 err = i40e_vsi_setup_tx_resources(vsi);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005219 if (err)
5220 goto err_setup_tx;
5221 err = i40e_vsi_setup_rx_resources(vsi);
5222 if (err)
5223 goto err_setup_rx;
5224
5225 if (new_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005226 char int_name[IFNAMSIZ + 9];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005227 err = i40e_vsi_configure(vsi);
5228 if (err)
5229 goto err_setup_rx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005230 snprintf(int_name, sizeof(int_name) - 1, "%s-fdir",
5231 dev_driver_string(&pf->pdev->dev));
5232 err = i40e_vsi_request_irq(vsi, int_name);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005233 if (err)
5234 goto err_setup_rx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005235 err = i40e_up_complete(vsi);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005236 if (err)
5237 goto err_up_complete;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005238 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005239 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005240
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005241 return;
5242
5243err_up_complete:
5244 i40e_down(vsi);
5245 i40e_vsi_free_irq(vsi);
5246err_setup_rx:
5247 i40e_vsi_free_rx_resources(vsi);
5248err_setup_tx:
5249 i40e_vsi_free_tx_resources(vsi);
5250err_vsi:
5251 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
5252 i40e_vsi_clear(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005253}
5254
5255/**
5256 * i40e_fdir_teardown - release the Flow Director resources
5257 * @pf: board private structure
5258 **/
5259static void i40e_fdir_teardown(struct i40e_pf *pf)
5260{
5261 int i;
5262
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005263 i40e_fdir_filter_exit(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005264 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
5265 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
5266 i40e_vsi_release(pf->vsi[i]);
5267 break;
5268 }
5269 }
5270}
5271
5272/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005273 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005274 * @pf: board private structure
5275 *
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005276 * Close up the VFs and other things in prep for pf Reset.
5277 **/
5278static int i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005279{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005280 struct i40e_hw *hw = &pf->hw;
5281 i40e_status ret;
5282 u32 v;
5283
5284 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
5285 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005286 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005287
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005288 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005289
Anjali Singhai Jain37f0be62013-11-28 06:39:46 +00005290 if (i40e_check_asq_alive(hw))
5291 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005292
5293 /* quiesce the VSIs and their queues that are not already DOWN */
5294 i40e_pf_quiesce_all_vsi(pf);
5295
5296 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5297 if (pf->vsi[v])
5298 pf->vsi[v]->seid = 0;
5299 }
5300
5301 i40e_shutdown_adminq(&pf->hw);
5302
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005303 /* call shutdown HMC */
5304 ret = i40e_shutdown_lan_hmc(hw);
5305 if (ret) {
5306 dev_info(&pf->pdev->dev, "shutdown_lan_hmc failed: %d\n", ret);
5307 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
5308 }
5309 return ret;
5310}
5311
5312/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00005313 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005314 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005315 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005316 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005317static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005318{
5319 struct i40e_driver_version dv;
5320 struct i40e_hw *hw = &pf->hw;
5321 i40e_status ret;
5322 u32 v;
5323
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005324 /* Now we wait for GRST to settle out.
5325 * We don't have to delete the VEBs or VSIs from the hw switch
5326 * because the reset will make them disappear.
5327 */
5328 ret = i40e_pf_reset(hw);
5329 if (ret)
5330 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
5331 pf->pfr_count++;
5332
5333 if (test_bit(__I40E_DOWN, &pf->state))
5334 goto end_core_reset;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005335 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005336
5337 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
5338 ret = i40e_init_adminq(&pf->hw);
5339 if (ret) {
5340 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, %d\n", ret);
5341 goto end_core_reset;
5342 }
5343
5344 ret = i40e_get_capabilities(pf);
5345 if (ret) {
5346 dev_info(&pf->pdev->dev, "i40e_get_capabilities failed, %d\n",
5347 ret);
5348 goto end_core_reset;
5349 }
5350
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005351 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
5352 hw->func_caps.num_rx_qp,
5353 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
5354 if (ret) {
5355 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
5356 goto end_core_reset;
5357 }
5358 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
5359 if (ret) {
5360 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
5361 goto end_core_reset;
5362 }
5363
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005364#ifdef CONFIG_I40E_DCB
5365 ret = i40e_init_pf_dcb(pf);
5366 if (ret) {
5367 dev_info(&pf->pdev->dev, "init_pf_dcb failed: %d\n", ret);
5368 goto end_core_reset;
5369 }
5370#endif /* CONFIG_I40E_DCB */
5371
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005372 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005373 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005374 if (ret)
5375 goto end_core_reset;
5376
5377 /* Rebuild the VSIs and VEBs that existed before reset.
5378 * They are still in our local switch element arrays, so only
5379 * need to rebuild the switch model in the HW.
5380 *
5381 * If there were VEBs but the reconstitution failed, we'll try
5382 * try to recover minimal use by getting the basic PF VSI working.
5383 */
5384 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005385 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005386 /* find the one VEB connected to the MAC, and find orphans */
5387 for (v = 0; v < I40E_MAX_VEB; v++) {
5388 if (!pf->veb[v])
5389 continue;
5390
5391 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
5392 pf->veb[v]->uplink_seid == 0) {
5393 ret = i40e_reconstitute_veb(pf->veb[v]);
5394
5395 if (!ret)
5396 continue;
5397
5398 /* If Main VEB failed, we're in deep doodoo,
5399 * so give up rebuilding the switch and set up
5400 * for minimal rebuild of PF VSI.
5401 * If orphan failed, we'll report the error
5402 * but try to keep going.
5403 */
5404 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
5405 dev_info(&pf->pdev->dev,
5406 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
5407 ret);
5408 pf->vsi[pf->lan_vsi]->uplink_seid
5409 = pf->mac_seid;
5410 break;
5411 } else if (pf->veb[v]->uplink_seid == 0) {
5412 dev_info(&pf->pdev->dev,
5413 "rebuild of orphan VEB failed: %d\n",
5414 ret);
5415 }
5416 }
5417 }
5418 }
5419
5420 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
5421 dev_info(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
5422 /* no VEB, so rebuild only the Main VSI */
5423 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
5424 if (ret) {
5425 dev_info(&pf->pdev->dev,
5426 "rebuild of Main VSI failed: %d\n", ret);
5427 goto end_core_reset;
5428 }
5429 }
5430
5431 /* reinit the misc interrupt */
5432 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5433 ret = i40e_setup_misc_vector(pf);
5434
5435 /* restart the VSIs that were rebuilt and running before the reset */
5436 i40e_pf_unquiesce_all_vsi(pf);
5437
Mitch Williams69f64b22014-02-13 03:48:46 -08005438 if (pf->num_alloc_vfs) {
5439 for (v = 0; v < pf->num_alloc_vfs; v++)
5440 i40e_reset_vf(&pf->vf[v], true);
5441 }
5442
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005443 /* tell the firmware that we're starting */
5444 dv.major_version = DRV_VERSION_MAJOR;
5445 dv.minor_version = DRV_VERSION_MINOR;
5446 dv.build_version = DRV_VERSION_BUILD;
5447 dv.subbuild_version = 0;
5448 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
5449
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005450 dev_info(&pf->pdev->dev, "reset complete\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005451
5452end_core_reset:
5453 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
5454}
5455
5456/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005457 * i40e_handle_reset_warning - prep for the pf to reset, reset and rebuild
5458 * @pf: board private structure
5459 *
5460 * Close up the VFs and other things in prep for a Core Reset,
5461 * then get ready to rebuild the world.
5462 **/
5463static void i40e_handle_reset_warning(struct i40e_pf *pf)
5464{
5465 i40e_status ret;
5466
5467 ret = i40e_prep_for_reset(pf);
5468 if (!ret)
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005469 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005470}
5471
5472/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005473 * i40e_handle_mdd_event
5474 * @pf: pointer to the pf structure
5475 *
5476 * Called from the MDD irq handler to identify possibly malicious vfs
5477 **/
5478static void i40e_handle_mdd_event(struct i40e_pf *pf)
5479{
5480 struct i40e_hw *hw = &pf->hw;
5481 bool mdd_detected = false;
5482 struct i40e_vf *vf;
5483 u32 reg;
5484 int i;
5485
5486 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
5487 return;
5488
5489 /* find what triggered the MDD event */
5490 reg = rd32(hw, I40E_GL_MDET_TX);
5491 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
5492 u8 func = (reg & I40E_GL_MDET_TX_FUNCTION_MASK)
5493 >> I40E_GL_MDET_TX_FUNCTION_SHIFT;
5494 u8 event = (reg & I40E_GL_MDET_TX_EVENT_SHIFT)
5495 >> I40E_GL_MDET_TX_EVENT_SHIFT;
5496 u8 queue = (reg & I40E_GL_MDET_TX_QUEUE_MASK)
5497 >> I40E_GL_MDET_TX_QUEUE_SHIFT;
5498 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00005499 "Malicious Driver Detection event 0x%02x on TX queue %d of function 0x%02x\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005500 event, queue, func);
5501 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
5502 mdd_detected = true;
5503 }
5504 reg = rd32(hw, I40E_GL_MDET_RX);
5505 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
5506 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK)
5507 >> I40E_GL_MDET_RX_FUNCTION_SHIFT;
5508 u8 event = (reg & I40E_GL_MDET_RX_EVENT_SHIFT)
5509 >> I40E_GL_MDET_RX_EVENT_SHIFT;
5510 u8 queue = (reg & I40E_GL_MDET_RX_QUEUE_MASK)
5511 >> I40E_GL_MDET_RX_QUEUE_SHIFT;
5512 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00005513 "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005514 event, queue, func);
5515 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
5516 mdd_detected = true;
5517 }
5518
5519 /* see if one of the VFs needs its hand slapped */
5520 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
5521 vf = &(pf->vf[i]);
5522 reg = rd32(hw, I40E_VP_MDET_TX(i));
5523 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
5524 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
5525 vf->num_mdd_events++;
5526 dev_info(&pf->pdev->dev, "MDD TX event on VF %d\n", i);
5527 }
5528
5529 reg = rd32(hw, I40E_VP_MDET_RX(i));
5530 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
5531 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
5532 vf->num_mdd_events++;
5533 dev_info(&pf->pdev->dev, "MDD RX event on VF %d\n", i);
5534 }
5535
5536 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
5537 dev_info(&pf->pdev->dev,
5538 "Too many MDD events on VF %d, disabled\n", i);
5539 dev_info(&pf->pdev->dev,
5540 "Use PF Control I/F to re-enable the VF\n");
5541 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
5542 }
5543 }
5544
5545 /* re-enable mdd interrupt cause */
5546 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
5547 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
5548 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
5549 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
5550 i40e_flush(hw);
5551}
5552
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005553#ifdef CONFIG_I40E_VXLAN
5554/**
5555 * i40e_sync_vxlan_filters_subtask - Sync the VSI filter list with HW
5556 * @pf: board private structure
5557 **/
5558static void i40e_sync_vxlan_filters_subtask(struct i40e_pf *pf)
5559{
5560 const int vxlan_hdr_qwords = 4;
5561 struct i40e_hw *hw = &pf->hw;
5562 i40e_status ret;
5563 u8 filter_index;
5564 __be16 port;
5565 int i;
5566
5567 if (!(pf->flags & I40E_FLAG_VXLAN_FILTER_SYNC))
5568 return;
5569
5570 pf->flags &= ~I40E_FLAG_VXLAN_FILTER_SYNC;
5571
5572 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
5573 if (pf->pending_vxlan_bitmap & (1 << i)) {
5574 pf->pending_vxlan_bitmap &= ~(1 << i);
5575 port = pf->vxlan_ports[i];
5576 ret = port ?
5577 i40e_aq_add_udp_tunnel(hw, ntohs(port),
5578 vxlan_hdr_qwords,
5579 I40E_AQC_TUNNEL_TYPE_VXLAN,
5580 &filter_index, NULL)
5581 : i40e_aq_del_udp_tunnel(hw, i, NULL);
5582
5583 if (ret) {
5584 dev_info(&pf->pdev->dev, "Failed to execute AQ command for %s port %d with index %d\n",
5585 port ? "adding" : "deleting",
5586 ntohs(port), port ? i : i);
5587
5588 pf->vxlan_ports[i] = 0;
5589 } else {
5590 dev_info(&pf->pdev->dev, "%s port %d with AQ command with index %d\n",
5591 port ? "Added" : "Deleted",
5592 ntohs(port), port ? i : filter_index);
5593 }
5594 }
5595 }
5596}
5597
5598#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005599/**
5600 * i40e_service_task - Run the driver's async subtasks
5601 * @work: pointer to work_struct containing our data
5602 **/
5603static void i40e_service_task(struct work_struct *work)
5604{
5605 struct i40e_pf *pf = container_of(work,
5606 struct i40e_pf,
5607 service_task);
5608 unsigned long start_time = jiffies;
5609
5610 i40e_reset_subtask(pf);
5611 i40e_handle_mdd_event(pf);
5612 i40e_vc_process_vflr_event(pf);
5613 i40e_watchdog_subtask(pf);
5614 i40e_fdir_reinit_subtask(pf);
5615 i40e_check_hang_subtask(pf);
5616 i40e_sync_filters_subtask(pf);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005617#ifdef CONFIG_I40E_VXLAN
5618 i40e_sync_vxlan_filters_subtask(pf);
5619#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005620 i40e_clean_adminq_subtask(pf);
5621
5622 i40e_service_event_complete(pf);
5623
5624 /* If the tasks have taken longer than one timer cycle or there
5625 * is more work to be done, reschedule the service task now
5626 * rather than wait for the timer to tick again.
5627 */
5628 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
5629 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
5630 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
5631 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
5632 i40e_service_event_schedule(pf);
5633}
5634
5635/**
5636 * i40e_service_timer - timer callback
5637 * @data: pointer to PF struct
5638 **/
5639static void i40e_service_timer(unsigned long data)
5640{
5641 struct i40e_pf *pf = (struct i40e_pf *)data;
5642
5643 mod_timer(&pf->service_timer,
5644 round_jiffies(jiffies + pf->service_timer_period));
5645 i40e_service_event_schedule(pf);
5646}
5647
5648/**
5649 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
5650 * @vsi: the VSI being configured
5651 **/
5652static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
5653{
5654 struct i40e_pf *pf = vsi->back;
5655
5656 switch (vsi->type) {
5657 case I40E_VSI_MAIN:
5658 vsi->alloc_queue_pairs = pf->num_lan_qps;
5659 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5660 I40E_REQ_DESCRIPTOR_MULTIPLE);
5661 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5662 vsi->num_q_vectors = pf->num_lan_msix;
5663 else
5664 vsi->num_q_vectors = 1;
5665
5666 break;
5667
5668 case I40E_VSI_FDIR:
5669 vsi->alloc_queue_pairs = 1;
5670 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
5671 I40E_REQ_DESCRIPTOR_MULTIPLE);
5672 vsi->num_q_vectors = 1;
5673 break;
5674
5675 case I40E_VSI_VMDQ2:
5676 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
5677 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5678 I40E_REQ_DESCRIPTOR_MULTIPLE);
5679 vsi->num_q_vectors = pf->num_vmdq_msix;
5680 break;
5681
5682 case I40E_VSI_SRIOV:
5683 vsi->alloc_queue_pairs = pf->num_vf_qps;
5684 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5685 I40E_REQ_DESCRIPTOR_MULTIPLE);
5686 break;
5687
5688 default:
5689 WARN_ON(1);
5690 return -ENODATA;
5691 }
5692
5693 return 0;
5694}
5695
5696/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005697 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
5698 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005699 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005700 *
5701 * On error: returns error code (negative)
5702 * On success: returns 0
5703 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005704static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005705{
5706 int size;
5707 int ret = 0;
5708
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00005709 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005710 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
5711 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
5712 if (!vsi->tx_rings)
5713 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005714 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
5715
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005716 if (alloc_qvectors) {
5717 /* allocate memory for q_vector pointers */
5718 size = sizeof(struct i40e_q_vectors *) * vsi->num_q_vectors;
5719 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
5720 if (!vsi->q_vectors) {
5721 ret = -ENOMEM;
5722 goto err_vectors;
5723 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005724 }
5725 return ret;
5726
5727err_vectors:
5728 kfree(vsi->tx_rings);
5729 return ret;
5730}
5731
5732/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005733 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
5734 * @pf: board private structure
5735 * @type: type of VSI
5736 *
5737 * On error: returns error code (negative)
5738 * On success: returns vsi index in PF (positive)
5739 **/
5740static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
5741{
5742 int ret = -ENODEV;
5743 struct i40e_vsi *vsi;
5744 int vsi_idx;
5745 int i;
5746
5747 /* Need to protect the allocation of the VSIs at the PF level */
5748 mutex_lock(&pf->switch_mutex);
5749
5750 /* VSI list may be fragmented if VSI creation/destruction has
5751 * been happening. We can afford to do a quick scan to look
5752 * for any free VSIs in the list.
5753 *
5754 * find next empty vsi slot, looping back around if necessary
5755 */
5756 i = pf->next_vsi;
5757 while (i < pf->hw.func_caps.num_vsis && pf->vsi[i])
5758 i++;
5759 if (i >= pf->hw.func_caps.num_vsis) {
5760 i = 0;
5761 while (i < pf->next_vsi && pf->vsi[i])
5762 i++;
5763 }
5764
5765 if (i < pf->hw.func_caps.num_vsis && !pf->vsi[i]) {
5766 vsi_idx = i; /* Found one! */
5767 } else {
5768 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00005769 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005770 }
5771 pf->next_vsi = ++i;
5772
5773 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
5774 if (!vsi) {
5775 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00005776 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005777 }
5778 vsi->type = type;
5779 vsi->back = pf;
5780 set_bit(__I40E_DOWN, &vsi->state);
5781 vsi->flags = 0;
5782 vsi->idx = vsi_idx;
5783 vsi->rx_itr_setting = pf->rx_itr_default;
5784 vsi->tx_itr_setting = pf->tx_itr_default;
5785 vsi->netdev_registered = false;
5786 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
5787 INIT_LIST_HEAD(&vsi->mac_filter_list);
5788
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005789 ret = i40e_set_num_rings_in_vsi(vsi);
5790 if (ret)
5791 goto err_rings;
5792
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005793 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005794 if (ret)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005795 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00005796
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005797 /* Setup default MSIX irq handler for VSI */
5798 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
5799
5800 pf->vsi[vsi_idx] = vsi;
5801 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00005802 goto unlock_pf;
5803
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005804err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00005805 pf->next_vsi = i - 1;
5806 kfree(vsi);
5807unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005808 mutex_unlock(&pf->switch_mutex);
5809 return ret;
5810}
5811
5812/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005813 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
5814 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005815 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005816 *
5817 * On error: returns error code (negative)
5818 * On success: returns 0
5819 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005820static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005821{
5822 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005823 if (free_qvectors) {
5824 kfree(vsi->q_vectors);
5825 vsi->q_vectors = NULL;
5826 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005827 kfree(vsi->tx_rings);
5828 vsi->tx_rings = NULL;
5829 vsi->rx_rings = NULL;
5830}
5831
5832/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005833 * i40e_vsi_clear - Deallocate the VSI provided
5834 * @vsi: the VSI being un-configured
5835 **/
5836static int i40e_vsi_clear(struct i40e_vsi *vsi)
5837{
5838 struct i40e_pf *pf;
5839
5840 if (!vsi)
5841 return 0;
5842
5843 if (!vsi->back)
5844 goto free_vsi;
5845 pf = vsi->back;
5846
5847 mutex_lock(&pf->switch_mutex);
5848 if (!pf->vsi[vsi->idx]) {
5849 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
5850 vsi->idx, vsi->idx, vsi, vsi->type);
5851 goto unlock_vsi;
5852 }
5853
5854 if (pf->vsi[vsi->idx] != vsi) {
5855 dev_err(&pf->pdev->dev,
5856 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
5857 pf->vsi[vsi->idx]->idx,
5858 pf->vsi[vsi->idx],
5859 pf->vsi[vsi->idx]->type,
5860 vsi->idx, vsi, vsi->type);
5861 goto unlock_vsi;
5862 }
5863
5864 /* updates the pf for this cleared vsi */
5865 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
5866 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
5867
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005868 i40e_vsi_free_arrays(vsi, true);
Alexander Duyck493fb302013-09-28 07:01:44 +00005869
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005870 pf->vsi[vsi->idx] = NULL;
5871 if (vsi->idx < pf->next_vsi)
5872 pf->next_vsi = vsi->idx;
5873
5874unlock_vsi:
5875 mutex_unlock(&pf->switch_mutex);
5876free_vsi:
5877 kfree(vsi);
5878
5879 return 0;
5880}
5881
5882/**
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005883 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
5884 * @vsi: the VSI being cleaned
5885 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00005886static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005887{
5888 int i;
5889
Greg Rose8e9dca52013-12-18 13:45:53 +00005890 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00005891 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00005892 kfree_rcu(vsi->tx_rings[i], rcu);
5893 vsi->tx_rings[i] = NULL;
5894 vsi->rx_rings[i] = NULL;
5895 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00005896 }
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005897}
5898
5899/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005900 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
5901 * @vsi: the VSI being configured
5902 **/
5903static int i40e_alloc_rings(struct i40e_vsi *vsi)
5904{
5905 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005906 int i;
5907
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005908 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00005909 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005910 struct i40e_ring *tx_ring;
5911 struct i40e_ring *rx_ring;
5912
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00005913 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005914 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
5915 if (!tx_ring)
5916 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005917
5918 tx_ring->queue_index = i;
5919 tx_ring->reg_idx = vsi->base_queue + i;
5920 tx_ring->ring_active = false;
5921 tx_ring->vsi = vsi;
5922 tx_ring->netdev = vsi->netdev;
5923 tx_ring->dev = &pf->pdev->dev;
5924 tx_ring->count = vsi->num_desc;
5925 tx_ring->size = 0;
5926 tx_ring->dcb_tc = 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005927 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005928
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005929 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005930 rx_ring->queue_index = i;
5931 rx_ring->reg_idx = vsi->base_queue + i;
5932 rx_ring->ring_active = false;
5933 rx_ring->vsi = vsi;
5934 rx_ring->netdev = vsi->netdev;
5935 rx_ring->dev = &pf->pdev->dev;
5936 rx_ring->count = vsi->num_desc;
5937 rx_ring->size = 0;
5938 rx_ring->dcb_tc = 0;
5939 if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
5940 set_ring_16byte_desc_enabled(rx_ring);
5941 else
5942 clear_ring_16byte_desc_enabled(rx_ring);
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005943 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005944 }
5945
5946 return 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005947
5948err_out:
5949 i40e_vsi_clear_rings(vsi);
5950 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005951}
5952
5953/**
5954 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
5955 * @pf: board private structure
5956 * @vectors: the number of MSI-X vectors to request
5957 *
5958 * Returns the number of vectors reserved, or error
5959 **/
5960static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
5961{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01005962 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
5963 I40E_MIN_MSIX, vectors);
5964 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005965 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01005966 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005967 vectors = 0;
5968 }
5969
Alexander Gordeev7b37f372014-02-18 11:11:42 +01005970 pf->num_msix_entries = vectors;
5971
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005972 return vectors;
5973}
5974
5975/**
5976 * i40e_init_msix - Setup the MSIX capability
5977 * @pf: board private structure
5978 *
5979 * Work with the OS to set up the MSIX vectors needed.
5980 *
5981 * Returns 0 on success, negative on failure
5982 **/
5983static int i40e_init_msix(struct i40e_pf *pf)
5984{
5985 i40e_status err = 0;
5986 struct i40e_hw *hw = &pf->hw;
5987 int v_budget, i;
5988 int vec;
5989
5990 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
5991 return -ENODEV;
5992
5993 /* The number of vectors we'll request will be comprised of:
5994 * - Add 1 for "other" cause for Admin Queue events, etc.
5995 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00005996 * - Queues being used for RSS.
5997 * We don't need as many as max_rss_size vectors.
5998 * use rss_size instead in the calculation since that
5999 * is governed by number of cpus in the system.
6000 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006001 * - The number of VMDq pairs
6002 * Once we count this up, try the request.
6003 *
6004 * If we can't get what we want, we'll simplify to nearly nothing
6005 * and try again. If that still fails, we punt.
6006 */
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006007 pf->num_lan_msix = pf->num_lan_qps - (pf->rss_size_max - pf->rss_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006008 pf->num_vmdq_msix = pf->num_vmdq_qps;
6009 v_budget = 1 + pf->num_lan_msix;
6010 v_budget += (pf->num_vmdq_vsis * pf->num_vmdq_msix);
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006011 if (pf->flags & I40E_FLAG_FD_SB_ENABLED)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006012 v_budget++;
6013
6014 /* Scale down if necessary, and the rings will share vectors */
6015 v_budget = min_t(int, v_budget, hw->func_caps.num_msix_vectors);
6016
6017 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
6018 GFP_KERNEL);
6019 if (!pf->msix_entries)
6020 return -ENOMEM;
6021
6022 for (i = 0; i < v_budget; i++)
6023 pf->msix_entries[i].entry = i;
6024 vec = i40e_reserve_msix_vectors(pf, v_budget);
6025 if (vec < I40E_MIN_MSIX) {
6026 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
6027 kfree(pf->msix_entries);
6028 pf->msix_entries = NULL;
6029 return -ENODEV;
6030
6031 } else if (vec == I40E_MIN_MSIX) {
6032 /* Adjust for minimal MSIX use */
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006033 dev_info(&pf->pdev->dev, "Features disabled, not enough MSI-X vectors\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006034 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
6035 pf->num_vmdq_vsis = 0;
6036 pf->num_vmdq_qps = 0;
6037 pf->num_vmdq_msix = 0;
6038 pf->num_lan_qps = 1;
6039 pf->num_lan_msix = 1;
6040
6041 } else if (vec != v_budget) {
6042 /* Scale vector usage down */
6043 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
6044 vec--; /* reserve the misc vector */
6045
6046 /* partition out the remaining vectors */
6047 switch (vec) {
6048 case 2:
6049 pf->num_vmdq_vsis = 1;
6050 pf->num_lan_msix = 1;
6051 break;
6052 case 3:
6053 pf->num_vmdq_vsis = 1;
6054 pf->num_lan_msix = 2;
6055 break;
6056 default:
6057 pf->num_lan_msix = min_t(int, (vec / 2),
6058 pf->num_lan_qps);
6059 pf->num_vmdq_vsis = min_t(int, (vec - pf->num_lan_msix),
6060 I40E_DEFAULT_NUM_VMDQ_VSI);
6061 break;
6062 }
6063 }
6064
6065 return err;
6066}
6067
6068/**
Alexander Duyck493fb302013-09-28 07:01:44 +00006069 * i40e_alloc_q_vector - Allocate memory for a single interrupt vector
6070 * @vsi: the VSI being configured
6071 * @v_idx: index of the vector in the vsi struct
6072 *
6073 * We allocate one q_vector. If allocation fails we return -ENOMEM.
6074 **/
6075static int i40e_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
6076{
6077 struct i40e_q_vector *q_vector;
6078
6079 /* allocate q_vector */
6080 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
6081 if (!q_vector)
6082 return -ENOMEM;
6083
6084 q_vector->vsi = vsi;
6085 q_vector->v_idx = v_idx;
6086 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
6087 if (vsi->netdev)
6088 netif_napi_add(vsi->netdev, &q_vector->napi,
6089 i40e_napi_poll, vsi->work_limit);
6090
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00006091 q_vector->rx.latency_range = I40E_LOW_LATENCY;
6092 q_vector->tx.latency_range = I40E_LOW_LATENCY;
6093
Alexander Duyck493fb302013-09-28 07:01:44 +00006094 /* tie q_vector and vsi together */
6095 vsi->q_vectors[v_idx] = q_vector;
6096
6097 return 0;
6098}
6099
6100/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006101 * i40e_alloc_q_vectors - Allocate memory for interrupt vectors
6102 * @vsi: the VSI being configured
6103 *
6104 * We allocate one q_vector per queue interrupt. If allocation fails we
6105 * return -ENOMEM.
6106 **/
6107static int i40e_alloc_q_vectors(struct i40e_vsi *vsi)
6108{
6109 struct i40e_pf *pf = vsi->back;
6110 int v_idx, num_q_vectors;
Alexander Duyck493fb302013-09-28 07:01:44 +00006111 int err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006112
6113 /* if not MSIX, give the one vector only to the LAN VSI */
6114 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6115 num_q_vectors = vsi->num_q_vectors;
6116 else if (vsi == pf->vsi[pf->lan_vsi])
6117 num_q_vectors = 1;
6118 else
6119 return -EINVAL;
6120
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006121 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00006122 err = i40e_alloc_q_vector(vsi, v_idx);
6123 if (err)
6124 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006125 }
6126
6127 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00006128
6129err_out:
6130 while (v_idx--)
6131 i40e_free_q_vector(vsi, v_idx);
6132
6133 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006134}
6135
6136/**
6137 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
6138 * @pf: board private structure to initialize
6139 **/
6140static void i40e_init_interrupt_scheme(struct i40e_pf *pf)
6141{
6142 int err = 0;
6143
6144 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
6145 err = i40e_init_msix(pf);
6146 if (err) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006147 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
6148 I40E_FLAG_RSS_ENABLED |
6149 I40E_FLAG_DCB_ENABLED |
6150 I40E_FLAG_SRIOV_ENABLED |
6151 I40E_FLAG_FD_SB_ENABLED |
6152 I40E_FLAG_FD_ATR_ENABLED |
6153 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006154
6155 /* rework the queue expectations without MSIX */
6156 i40e_determine_queue_usage(pf);
6157 }
6158 }
6159
6160 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
6161 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006162 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006163 err = pci_enable_msi(pf->pdev);
6164 if (err) {
Shannon Nelson958a3e32013-09-28 07:13:28 +00006165 dev_info(&pf->pdev->dev, "MSI init failed - %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006166 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
6167 }
6168 }
6169
Shannon Nelson958a3e32013-09-28 07:13:28 +00006170 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006171 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00006172
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006173 /* track first vector for misc interrupts */
6174 err = i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT-1);
6175}
6176
6177/**
6178 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
6179 * @pf: board private structure
6180 *
6181 * This sets up the handler for MSIX 0, which is used to manage the
6182 * non-queue interrupts, e.g. AdminQ and errors. This is not used
6183 * when in MSI or Legacy interrupt mode.
6184 **/
6185static int i40e_setup_misc_vector(struct i40e_pf *pf)
6186{
6187 struct i40e_hw *hw = &pf->hw;
6188 int err = 0;
6189
6190 /* Only request the irq if this is the first time through, and
6191 * not when we're rebuilding after a Reset
6192 */
6193 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
6194 err = request_irq(pf->msix_entries[0].vector,
6195 i40e_intr, 0, pf->misc_int_name, pf);
6196 if (err) {
6197 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006198 "request_irq for %s failed: %d\n",
6199 pf->misc_int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006200 return -EFAULT;
6201 }
6202 }
6203
6204 i40e_enable_misc_int_causes(hw);
6205
6206 /* associate no queues to the misc vector */
6207 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
6208 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
6209
6210 i40e_flush(hw);
6211
6212 i40e_irq_dynamic_enable_icr0(pf);
6213
6214 return err;
6215}
6216
6217/**
6218 * i40e_config_rss - Prepare for RSS if used
6219 * @pf: board private structure
6220 **/
6221static int i40e_config_rss(struct i40e_pf *pf)
6222{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006223 /* Set of random keys generated using kernel random number generator */
6224 static const u32 seed[I40E_PFQF_HKEY_MAX_INDEX + 1] = {0x41b01687,
6225 0x183cfd8c, 0xce880440, 0x580cbc3c, 0x35897377,
6226 0x328b25e1, 0x4fa98922, 0xb7d90c14, 0xd5bad70d,
6227 0xcd15a2c1, 0xe8580225, 0x4a1e9d11, 0xfe5731be};
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00006228 struct i40e_hw *hw = &pf->hw;
6229 u32 lut = 0;
6230 int i, j;
6231 u64 hena;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006232
6233 /* Fill out hash function seed */
6234 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
6235 wr32(hw, I40E_PFQF_HKEY(i), seed[i]);
6236
6237 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
6238 hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
6239 ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
Mitch Williams12dc4fe2013-11-28 06:39:32 +00006240 hena |= I40E_DEFAULT_RSS_HENA;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006241 wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
6242 wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
6243
6244 /* Populate the LUT with max no. of queues in round robin fashion */
6245 for (i = 0, j = 0; i < pf->hw.func_caps.rss_table_size; i++, j++) {
6246
6247 /* The assumption is that lan qp count will be the highest
6248 * qp count for any PF VSI that needs RSS.
6249 * If multiple VSIs need RSS support, all the qp counts
6250 * for those VSIs should be a power of 2 for RSS to work.
6251 * If LAN VSI is the only consumer for RSS then this requirement
6252 * is not necessary.
6253 */
6254 if (j == pf->rss_size)
6255 j = 0;
6256 /* lut = 4-byte sliding window of 4 lut entries */
6257 lut = (lut << 8) | (j &
6258 ((0x1 << pf->hw.func_caps.rss_table_entry_width) - 1));
6259 /* On i = 3, we have 4 entries in lut; write to the register */
6260 if ((i & 3) == 3)
6261 wr32(hw, I40E_PFQF_HLUT(i >> 2), lut);
6262 }
6263 i40e_flush(hw);
6264
6265 return 0;
6266}
6267
6268/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006269 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
6270 * @pf: board private structure
6271 * @queue_count: the requested queue count for rss.
6272 *
6273 * returns 0 if rss is not enabled, if enabled returns the final rss queue
6274 * count which may be different from the requested queue count.
6275 **/
6276int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
6277{
6278 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
6279 return 0;
6280
6281 queue_count = min_t(int, queue_count, pf->rss_size_max);
6282 queue_count = rounddown_pow_of_two(queue_count);
6283
6284 if (queue_count != pf->rss_size) {
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006285 i40e_prep_for_reset(pf);
6286
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006287 pf->rss_size = queue_count;
6288
6289 i40e_reset_and_rebuild(pf, true);
6290 i40e_config_rss(pf);
6291 }
6292 dev_info(&pf->pdev->dev, "RSS count: %d\n", pf->rss_size);
6293 return pf->rss_size;
6294}
6295
6296/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006297 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
6298 * @pf: board private structure to initialize
6299 *
6300 * i40e_sw_init initializes the Adapter private data structure.
6301 * Fields are initialized based on PCI device information and
6302 * OS network device settings (MTU size).
6303 **/
6304static int i40e_sw_init(struct i40e_pf *pf)
6305{
6306 int err = 0;
6307 int size;
6308
6309 pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
6310 (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
Shannon Nelson27599972013-11-16 10:00:43 +00006311 pf->hw.debug_mask = pf->msg_enable | I40E_DEBUG_DIAG;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006312 if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
6313 if (I40E_DEBUG_USER & debug)
6314 pf->hw.debug_mask = debug;
6315 pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
6316 I40E_DEFAULT_MSG_ENABLE);
6317 }
6318
6319 /* Set default capability flags */
6320 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
6321 I40E_FLAG_MSI_ENABLED |
6322 I40E_FLAG_MSIX_ENABLED |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006323 I40E_FLAG_RX_1BUF_ENABLED;
6324
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006325 /* Depending on PF configurations, it is possible that the RSS
6326 * maximum might end up larger than the available queues
6327 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006328 pf->rss_size_max = 0x1 << pf->hw.func_caps.rss_table_entry_width;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006329 pf->rss_size_max = min_t(int, pf->rss_size_max,
6330 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006331 if (pf->hw.func_caps.rss) {
6332 pf->flags |= I40E_FLAG_RSS_ENABLED;
Jesse Brandeburgbf051a32013-11-26 10:49:17 +00006333 pf->rss_size = min_t(int, pf->rss_size_max, num_online_cpus());
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006334 pf->rss_size = rounddown_pow_of_two(pf->rss_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006335 } else {
6336 pf->rss_size = 1;
6337 }
6338
Catherine Sullivan2050bc62013-12-18 13:46:03 +00006339 /* MFP mode enabled */
6340 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.mfp_mode_1) {
6341 pf->flags |= I40E_FLAG_MFP_ENABLED;
6342 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
6343 }
6344
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006345 /* FW/NVM is not yet fixed in this regard */
6346 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
6347 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
6348 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
6349 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006350 if (!(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006351 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006352 } else {
6353 dev_info(&pf->pdev->dev,
6354 "Flow Director Side Band mode Disabled in MFP mode\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006355 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006356 pf->fdir_pf_filter_count =
6357 pf->hw.func_caps.fd_filters_guaranteed;
6358 pf->hw.fdir_shared_filter_count =
6359 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006360 }
6361
6362 if (pf->hw.func_caps.vmdq) {
6363 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
6364 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
6365 pf->num_vmdq_qps = I40E_DEFAULT_QUEUES_PER_VMDQ;
6366 }
6367
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006368#ifdef CONFIG_PCI_IOV
6369 if (pf->hw.func_caps.num_vfs) {
6370 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
6371 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
6372 pf->num_req_vfs = min_t(int,
6373 pf->hw.func_caps.num_vfs,
6374 I40E_MAX_VF_COUNT);
6375 }
6376#endif /* CONFIG_PCI_IOV */
6377 pf->eeprom_version = 0xDEAD;
6378 pf->lan_veb = I40E_NO_VEB;
6379 pf->lan_vsi = I40E_NO_VSI;
6380
6381 /* set up queue assignment tracking */
6382 size = sizeof(struct i40e_lump_tracking)
6383 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
6384 pf->qp_pile = kzalloc(size, GFP_KERNEL);
6385 if (!pf->qp_pile) {
6386 err = -ENOMEM;
6387 goto sw_init_done;
6388 }
6389 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
6390 pf->qp_pile->search_hint = 0;
6391
6392 /* set up vector assignment tracking */
6393 size = sizeof(struct i40e_lump_tracking)
6394 + (sizeof(u16) * pf->hw.func_caps.num_msix_vectors);
6395 pf->irq_pile = kzalloc(size, GFP_KERNEL);
6396 if (!pf->irq_pile) {
6397 kfree(pf->qp_pile);
6398 err = -ENOMEM;
6399 goto sw_init_done;
6400 }
6401 pf->irq_pile->num_entries = pf->hw.func_caps.num_msix_vectors;
6402 pf->irq_pile->search_hint = 0;
6403
6404 mutex_init(&pf->switch_mutex);
6405
6406sw_init_done:
6407 return err;
6408}
6409
6410/**
6411 * i40e_set_features - set the netdev feature flags
6412 * @netdev: ptr to the netdev being adjusted
6413 * @features: the feature set that the stack is suggesting
6414 **/
6415static int i40e_set_features(struct net_device *netdev,
6416 netdev_features_t features)
6417{
6418 struct i40e_netdev_priv *np = netdev_priv(netdev);
6419 struct i40e_vsi *vsi = np->vsi;
6420
6421 if (features & NETIF_F_HW_VLAN_CTAG_RX)
6422 i40e_vlan_stripping_enable(vsi);
6423 else
6424 i40e_vlan_stripping_disable(vsi);
6425
6426 return 0;
6427}
6428
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006429#ifdef CONFIG_I40E_VXLAN
6430/**
6431 * i40e_get_vxlan_port_idx - Lookup a possibly offloaded for Rx UDP port
6432 * @pf: board private structure
6433 * @port: The UDP port to look up
6434 *
6435 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
6436 **/
6437static u8 i40e_get_vxlan_port_idx(struct i40e_pf *pf, __be16 port)
6438{
6439 u8 i;
6440
6441 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
6442 if (pf->vxlan_ports[i] == port)
6443 return i;
6444 }
6445
6446 return i;
6447}
6448
6449/**
6450 * i40e_add_vxlan_port - Get notifications about VXLAN ports that come up
6451 * @netdev: This physical port's netdev
6452 * @sa_family: Socket Family that VXLAN is notifying us about
6453 * @port: New UDP port number that VXLAN started listening to
6454 **/
6455static void i40e_add_vxlan_port(struct net_device *netdev,
6456 sa_family_t sa_family, __be16 port)
6457{
6458 struct i40e_netdev_priv *np = netdev_priv(netdev);
6459 struct i40e_vsi *vsi = np->vsi;
6460 struct i40e_pf *pf = vsi->back;
6461 u8 next_idx;
6462 u8 idx;
6463
6464 if (sa_family == AF_INET6)
6465 return;
6466
6467 idx = i40e_get_vxlan_port_idx(pf, port);
6468
6469 /* Check if port already exists */
6470 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6471 netdev_info(netdev, "Port %d already offloaded\n", ntohs(port));
6472 return;
6473 }
6474
6475 /* Now check if there is space to add the new port */
6476 next_idx = i40e_get_vxlan_port_idx(pf, 0);
6477
6478 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6479 netdev_info(netdev, "Maximum number of UDP ports reached, not adding port %d\n",
6480 ntohs(port));
6481 return;
6482 }
6483
6484 /* New port: add it and mark its index in the bitmap */
6485 pf->vxlan_ports[next_idx] = port;
6486 pf->pending_vxlan_bitmap |= (1 << next_idx);
6487
6488 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
6489}
6490
6491/**
6492 * i40e_del_vxlan_port - Get notifications about VXLAN ports that go away
6493 * @netdev: This physical port's netdev
6494 * @sa_family: Socket Family that VXLAN is notifying us about
6495 * @port: UDP port number that VXLAN stopped listening to
6496 **/
6497static void i40e_del_vxlan_port(struct net_device *netdev,
6498 sa_family_t sa_family, __be16 port)
6499{
6500 struct i40e_netdev_priv *np = netdev_priv(netdev);
6501 struct i40e_vsi *vsi = np->vsi;
6502 struct i40e_pf *pf = vsi->back;
6503 u8 idx;
6504
6505 if (sa_family == AF_INET6)
6506 return;
6507
6508 idx = i40e_get_vxlan_port_idx(pf, port);
6509
6510 /* Check if port already exists */
6511 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6512 /* if port exists, set it to 0 (mark for deletion)
6513 * and make it pending
6514 */
6515 pf->vxlan_ports[idx] = 0;
6516
6517 pf->pending_vxlan_bitmap |= (1 << idx);
6518
6519 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
6520 } else {
6521 netdev_warn(netdev, "Port %d was not found, not deleting\n",
6522 ntohs(port));
6523 }
6524}
6525
6526#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006527static const struct net_device_ops i40e_netdev_ops = {
6528 .ndo_open = i40e_open,
6529 .ndo_stop = i40e_close,
6530 .ndo_start_xmit = i40e_lan_xmit_frame,
6531 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
6532 .ndo_set_rx_mode = i40e_set_rx_mode,
6533 .ndo_validate_addr = eth_validate_addr,
6534 .ndo_set_mac_address = i40e_set_mac,
6535 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006536 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006537 .ndo_tx_timeout = i40e_tx_timeout,
6538 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
6539 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
6540#ifdef CONFIG_NET_POLL_CONTROLLER
6541 .ndo_poll_controller = i40e_netpoll,
6542#endif
6543 .ndo_setup_tc = i40e_setup_tc,
6544 .ndo_set_features = i40e_set_features,
6545 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
6546 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
6547 .ndo_set_vf_tx_rate = i40e_ndo_set_vf_bw,
6548 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006549#ifdef CONFIG_I40E_VXLAN
6550 .ndo_add_vxlan_port = i40e_add_vxlan_port,
6551 .ndo_del_vxlan_port = i40e_del_vxlan_port,
6552#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006553};
6554
6555/**
6556 * i40e_config_netdev - Setup the netdev flags
6557 * @vsi: the VSI being configured
6558 *
6559 * Returns 0 on success, negative value on failure
6560 **/
6561static int i40e_config_netdev(struct i40e_vsi *vsi)
6562{
Greg Rose1a103702013-11-28 06:42:39 +00006563 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006564 struct i40e_pf *pf = vsi->back;
6565 struct i40e_hw *hw = &pf->hw;
6566 struct i40e_netdev_priv *np;
6567 struct net_device *netdev;
6568 u8 mac_addr[ETH_ALEN];
6569 int etherdev_size;
6570
6571 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006572 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006573 if (!netdev)
6574 return -ENOMEM;
6575
6576 vsi->netdev = netdev;
6577 np = netdev_priv(netdev);
6578 np->vsi = vsi;
6579
6580 netdev->hw_enc_features = NETIF_F_IP_CSUM |
6581 NETIF_F_GSO_UDP_TUNNEL |
6582 NETIF_F_TSO |
6583 NETIF_F_SG;
6584
6585 netdev->features = NETIF_F_SG |
6586 NETIF_F_IP_CSUM |
6587 NETIF_F_SCTP_CSUM |
6588 NETIF_F_HIGHDMA |
6589 NETIF_F_GSO_UDP_TUNNEL |
6590 NETIF_F_HW_VLAN_CTAG_TX |
6591 NETIF_F_HW_VLAN_CTAG_RX |
6592 NETIF_F_HW_VLAN_CTAG_FILTER |
6593 NETIF_F_IPV6_CSUM |
6594 NETIF_F_TSO |
6595 NETIF_F_TSO6 |
6596 NETIF_F_RXCSUM |
6597 NETIF_F_RXHASH |
6598 0;
6599
6600 /* copy netdev features into list of user selectable features */
6601 netdev->hw_features |= netdev->features;
6602
6603 if (vsi->type == I40E_VSI_MAIN) {
6604 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
6605 memcpy(mac_addr, hw->mac.perm_addr, ETH_ALEN);
6606 } else {
6607 /* relate the VSI_VMDQ name to the VSI_MAIN name */
6608 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
6609 pf->vsi[pf->lan_vsi]->netdev->name);
6610 random_ether_addr(mac_addr);
6611 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
6612 }
Greg Rose1a103702013-11-28 06:42:39 +00006613 i40e_add_filter(vsi, brdcast, I40E_VLAN_ANY, false, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006614
6615 memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
6616 memcpy(netdev->perm_addr, mac_addr, ETH_ALEN);
6617 /* vlan gets same features (except vlan offload)
6618 * after any tweaks for specific VSI types
6619 */
6620 netdev->vlan_features = netdev->features & ~(NETIF_F_HW_VLAN_CTAG_TX |
6621 NETIF_F_HW_VLAN_CTAG_RX |
6622 NETIF_F_HW_VLAN_CTAG_FILTER);
6623 netdev->priv_flags |= IFF_UNICAST_FLT;
6624 netdev->priv_flags |= IFF_SUPP_NOFCS;
6625 /* Setup netdev TC information */
6626 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
6627
6628 netdev->netdev_ops = &i40e_netdev_ops;
6629 netdev->watchdog_timeo = 5 * HZ;
6630 i40e_set_ethtool_ops(netdev);
6631
6632 return 0;
6633}
6634
6635/**
6636 * i40e_vsi_delete - Delete a VSI from the switch
6637 * @vsi: the VSI being removed
6638 *
6639 * Returns 0 on success, negative value on failure
6640 **/
6641static void i40e_vsi_delete(struct i40e_vsi *vsi)
6642{
6643 /* remove default VSI is not allowed */
6644 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
6645 return;
6646
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006647 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
6648 return;
6649}
6650
6651/**
6652 * i40e_add_vsi - Add a VSI to the switch
6653 * @vsi: the VSI being configured
6654 *
6655 * This initializes a VSI context depending on the VSI type to be added and
6656 * passes it down to the add_vsi aq command.
6657 **/
6658static int i40e_add_vsi(struct i40e_vsi *vsi)
6659{
6660 int ret = -ENODEV;
6661 struct i40e_mac_filter *f, *ftmp;
6662 struct i40e_pf *pf = vsi->back;
6663 struct i40e_hw *hw = &pf->hw;
6664 struct i40e_vsi_context ctxt;
6665 u8 enabled_tc = 0x1; /* TC0 enabled */
6666 int f_count = 0;
6667
6668 memset(&ctxt, 0, sizeof(ctxt));
6669 switch (vsi->type) {
6670 case I40E_VSI_MAIN:
6671 /* The PF's main VSI is already setup as part of the
6672 * device initialization, so we'll not bother with
6673 * the add_vsi call, but we will retrieve the current
6674 * VSI context.
6675 */
6676 ctxt.seid = pf->main_vsi_seid;
6677 ctxt.pf_num = pf->hw.pf_id;
6678 ctxt.vf_num = 0;
6679 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6680 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6681 if (ret) {
6682 dev_info(&pf->pdev->dev,
6683 "couldn't get pf vsi config, err %d, aq_err %d\n",
6684 ret, pf->hw.aq.asq_last_status);
6685 return -ENOENT;
6686 }
6687 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
6688 vsi->info.valid_sections = 0;
6689
6690 vsi->seid = ctxt.seid;
6691 vsi->id = ctxt.vsi_number;
6692
6693 enabled_tc = i40e_pf_get_tc_map(pf);
6694
6695 /* MFP mode setup queue map and update VSI */
6696 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
6697 memset(&ctxt, 0, sizeof(ctxt));
6698 ctxt.seid = pf->main_vsi_seid;
6699 ctxt.pf_num = pf->hw.pf_id;
6700 ctxt.vf_num = 0;
6701 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
6702 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
6703 if (ret) {
6704 dev_info(&pf->pdev->dev,
6705 "update vsi failed, aq_err=%d\n",
6706 pf->hw.aq.asq_last_status);
6707 ret = -ENOENT;
6708 goto err;
6709 }
6710 /* update the local VSI info queue map */
6711 i40e_vsi_update_queue_map(vsi, &ctxt);
6712 vsi->info.valid_sections = 0;
6713 } else {
6714 /* Default/Main VSI is only enabled for TC0
6715 * reconfigure it to enable all TCs that are
6716 * available on the port in SFP mode.
6717 */
6718 ret = i40e_vsi_config_tc(vsi, enabled_tc);
6719 if (ret) {
6720 dev_info(&pf->pdev->dev,
6721 "failed to configure TCs for main VSI tc_map 0x%08x, err %d, aq_err %d\n",
6722 enabled_tc, ret,
6723 pf->hw.aq.asq_last_status);
6724 ret = -ENOENT;
6725 }
6726 }
6727 break;
6728
6729 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006730 ctxt.pf_num = hw->pf_id;
6731 ctxt.vf_num = 0;
6732 ctxt.uplink_seid = vsi->uplink_seid;
6733 ctxt.connection_type = 0x1; /* regular data port */
6734 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006735 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006736 break;
6737
6738 case I40E_VSI_VMDQ2:
6739 ctxt.pf_num = hw->pf_id;
6740 ctxt.vf_num = 0;
6741 ctxt.uplink_seid = vsi->uplink_seid;
6742 ctxt.connection_type = 0x1; /* regular data port */
6743 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6744
6745 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6746
6747 /* This VSI is connected to VEB so the switch_id
6748 * should be set to zero by default.
6749 */
6750 ctxt.info.switch_id = 0;
6751 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
6752 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6753
6754 /* Setup the VSI tx/rx queue map for TC0 only for now */
6755 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
6756 break;
6757
6758 case I40E_VSI_SRIOV:
6759 ctxt.pf_num = hw->pf_id;
6760 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
6761 ctxt.uplink_seid = vsi->uplink_seid;
6762 ctxt.connection_type = 0x1; /* regular data port */
6763 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
6764
6765 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6766
6767 /* This VSI is connected to VEB so the switch_id
6768 * should be set to zero by default.
6769 */
6770 ctxt.info.switch_id = cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6771
6772 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
6773 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
6774 /* Setup the VSI tx/rx queue map for TC0 only for now */
6775 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
6776 break;
6777
6778 default:
6779 return -ENODEV;
6780 }
6781
6782 if (vsi->type != I40E_VSI_MAIN) {
6783 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6784 if (ret) {
6785 dev_info(&vsi->back->pdev->dev,
6786 "add vsi failed, aq_err=%d\n",
6787 vsi->back->hw.aq.asq_last_status);
6788 ret = -ENOENT;
6789 goto err;
6790 }
6791 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
6792 vsi->info.valid_sections = 0;
6793 vsi->seid = ctxt.seid;
6794 vsi->id = ctxt.vsi_number;
6795 }
6796
6797 /* If macvlan filters already exist, force them to get loaded */
6798 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
6799 f->changed = true;
6800 f_count++;
6801 }
6802 if (f_count) {
6803 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
6804 pf->flags |= I40E_FLAG_FILTER_SYNC;
6805 }
6806
6807 /* Update VSI BW information */
6808 ret = i40e_vsi_get_bw_info(vsi);
6809 if (ret) {
6810 dev_info(&pf->pdev->dev,
6811 "couldn't get vsi bw info, err %d, aq_err %d\n",
6812 ret, pf->hw.aq.asq_last_status);
6813 /* VSI is already added so not tearing that up */
6814 ret = 0;
6815 }
6816
6817err:
6818 return ret;
6819}
6820
6821/**
6822 * i40e_vsi_release - Delete a VSI and free its resources
6823 * @vsi: the VSI being removed
6824 *
6825 * Returns 0 on success or < 0 on error
6826 **/
6827int i40e_vsi_release(struct i40e_vsi *vsi)
6828{
6829 struct i40e_mac_filter *f, *ftmp;
6830 struct i40e_veb *veb = NULL;
6831 struct i40e_pf *pf;
6832 u16 uplink_seid;
6833 int i, n;
6834
6835 pf = vsi->back;
6836
6837 /* release of a VEB-owner or last VSI is not allowed */
6838 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
6839 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
6840 vsi->seid, vsi->uplink_seid);
6841 return -ENODEV;
6842 }
6843 if (vsi == pf->vsi[pf->lan_vsi] &&
6844 !test_bit(__I40E_DOWN, &pf->state)) {
6845 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
6846 return -ENODEV;
6847 }
6848
6849 uplink_seid = vsi->uplink_seid;
6850 if (vsi->type != I40E_VSI_SRIOV) {
6851 if (vsi->netdev_registered) {
6852 vsi->netdev_registered = false;
6853 if (vsi->netdev) {
6854 /* results in a call to i40e_close() */
6855 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006856 }
6857 } else {
6858 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
6859 i40e_down(vsi);
6860 i40e_vsi_free_irq(vsi);
6861 i40e_vsi_free_tx_resources(vsi);
6862 i40e_vsi_free_rx_resources(vsi);
6863 }
6864 i40e_vsi_disable_irq(vsi);
6865 }
6866
6867 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
6868 i40e_del_filter(vsi, f->macaddr, f->vlan,
6869 f->is_vf, f->is_netdev);
6870 i40e_sync_vsi_filters(vsi);
6871
6872 i40e_vsi_delete(vsi);
6873 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00006874 if (vsi->netdev) {
6875 free_netdev(vsi->netdev);
6876 vsi->netdev = NULL;
6877 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006878 i40e_vsi_clear_rings(vsi);
6879 i40e_vsi_clear(vsi);
6880
6881 /* If this was the last thing on the VEB, except for the
6882 * controlling VSI, remove the VEB, which puts the controlling
6883 * VSI onto the next level down in the switch.
6884 *
6885 * Well, okay, there's one more exception here: don't remove
6886 * the orphan VEBs yet. We'll wait for an explicit remove request
6887 * from up the network stack.
6888 */
6889 for (n = 0, i = 0; i < pf->hw.func_caps.num_vsis; i++) {
6890 if (pf->vsi[i] &&
6891 pf->vsi[i]->uplink_seid == uplink_seid &&
6892 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
6893 n++; /* count the VSIs */
6894 }
6895 }
6896 for (i = 0; i < I40E_MAX_VEB; i++) {
6897 if (!pf->veb[i])
6898 continue;
6899 if (pf->veb[i]->uplink_seid == uplink_seid)
6900 n++; /* count the VEBs */
6901 if (pf->veb[i]->seid == uplink_seid)
6902 veb = pf->veb[i];
6903 }
6904 if (n == 0 && veb && veb->uplink_seid != 0)
6905 i40e_veb_release(veb);
6906
6907 return 0;
6908}
6909
6910/**
6911 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
6912 * @vsi: ptr to the VSI
6913 *
6914 * This should only be called after i40e_vsi_mem_alloc() which allocates the
6915 * corresponding SW VSI structure and initializes num_queue_pairs for the
6916 * newly allocated VSI.
6917 *
6918 * Returns 0 on success or negative on failure
6919 **/
6920static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
6921{
6922 int ret = -ENOENT;
6923 struct i40e_pf *pf = vsi->back;
6924
Alexander Duyck493fb302013-09-28 07:01:44 +00006925 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006926 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
6927 vsi->seid);
6928 return -EEXIST;
6929 }
6930
6931 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00006932 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006933 vsi->seid, vsi->base_vector);
6934 return -EEXIST;
6935 }
6936
6937 ret = i40e_alloc_q_vectors(vsi);
6938 if (ret) {
6939 dev_info(&pf->pdev->dev,
6940 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
6941 vsi->num_q_vectors, vsi->seid, ret);
6942 vsi->num_q_vectors = 0;
6943 goto vector_setup_out;
6944 }
6945
Shannon Nelson958a3e32013-09-28 07:13:28 +00006946 if (vsi->num_q_vectors)
6947 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
6948 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006949 if (vsi->base_vector < 0) {
6950 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00006951 "failed to get queue tracking for VSI %d, err=%d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006952 vsi->seid, vsi->base_vector);
6953 i40e_vsi_free_q_vectors(vsi);
6954 ret = -ENOENT;
6955 goto vector_setup_out;
6956 }
6957
6958vector_setup_out:
6959 return ret;
6960}
6961
6962/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006963 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
6964 * @vsi: pointer to the vsi.
6965 *
6966 * This re-allocates a vsi's queue resources.
6967 *
6968 * Returns pointer to the successfully allocated and configured VSI sw struct
6969 * on success, otherwise returns NULL on failure.
6970 **/
6971static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
6972{
6973 struct i40e_pf *pf = vsi->back;
6974 u8 enabled_tc;
6975 int ret;
6976
6977 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
6978 i40e_vsi_clear_rings(vsi);
6979
6980 i40e_vsi_free_arrays(vsi, false);
6981 i40e_set_num_rings_in_vsi(vsi);
6982 ret = i40e_vsi_alloc_arrays(vsi, false);
6983 if (ret)
6984 goto err_vsi;
6985
6986 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
6987 if (ret < 0) {
6988 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
6989 vsi->seid, ret);
6990 goto err_vsi;
6991 }
6992 vsi->base_queue = ret;
6993
6994 /* Update the FW view of the VSI. Force a reset of TC and queue
6995 * layout configurations.
6996 */
6997 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
6998 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
6999 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
7000 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
7001
7002 /* assign it some queues */
7003 ret = i40e_alloc_rings(vsi);
7004 if (ret)
7005 goto err_rings;
7006
7007 /* map all of the rings to the q_vectors */
7008 i40e_vsi_map_rings_to_vectors(vsi);
7009 return vsi;
7010
7011err_rings:
7012 i40e_vsi_free_q_vectors(vsi);
7013 if (vsi->netdev_registered) {
7014 vsi->netdev_registered = false;
7015 unregister_netdev(vsi->netdev);
7016 free_netdev(vsi->netdev);
7017 vsi->netdev = NULL;
7018 }
7019 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
7020err_vsi:
7021 i40e_vsi_clear(vsi);
7022 return NULL;
7023}
7024
7025/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007026 * i40e_vsi_setup - Set up a VSI by a given type
7027 * @pf: board private structure
7028 * @type: VSI type
7029 * @uplink_seid: the switch element to link to
7030 * @param1: usage depends upon VSI type. For VF types, indicates VF id
7031 *
7032 * This allocates the sw VSI structure and its queue resources, then add a VSI
7033 * to the identified VEB.
7034 *
7035 * Returns pointer to the successfully allocated and configure VSI sw struct on
7036 * success, otherwise returns NULL on failure.
7037 **/
7038struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
7039 u16 uplink_seid, u32 param1)
7040{
7041 struct i40e_vsi *vsi = NULL;
7042 struct i40e_veb *veb = NULL;
7043 int ret, i;
7044 int v_idx;
7045
7046 /* The requested uplink_seid must be either
7047 * - the PF's port seid
7048 * no VEB is needed because this is the PF
7049 * or this is a Flow Director special case VSI
7050 * - seid of an existing VEB
7051 * - seid of a VSI that owns an existing VEB
7052 * - seid of a VSI that doesn't own a VEB
7053 * a new VEB is created and the VSI becomes the owner
7054 * - seid of the PF VSI, which is what creates the first VEB
7055 * this is a special case of the previous
7056 *
7057 * Find which uplink_seid we were given and create a new VEB if needed
7058 */
7059 for (i = 0; i < I40E_MAX_VEB; i++) {
7060 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
7061 veb = pf->veb[i];
7062 break;
7063 }
7064 }
7065
7066 if (!veb && uplink_seid != pf->mac_seid) {
7067
7068 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
7069 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
7070 vsi = pf->vsi[i];
7071 break;
7072 }
7073 }
7074 if (!vsi) {
7075 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
7076 uplink_seid);
7077 return NULL;
7078 }
7079
7080 if (vsi->uplink_seid == pf->mac_seid)
7081 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
7082 vsi->tc_config.enabled_tc);
7083 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
7084 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
7085 vsi->tc_config.enabled_tc);
7086
7087 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
7088 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
7089 veb = pf->veb[i];
7090 }
7091 if (!veb) {
7092 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
7093 return NULL;
7094 }
7095
7096 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
7097 uplink_seid = veb->seid;
7098 }
7099
7100 /* get vsi sw struct */
7101 v_idx = i40e_vsi_mem_alloc(pf, type);
7102 if (v_idx < 0)
7103 goto err_alloc;
7104 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007105 if (!vsi)
7106 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007107 vsi->type = type;
7108 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
7109
7110 if (type == I40E_VSI_MAIN)
7111 pf->lan_vsi = v_idx;
7112 else if (type == I40E_VSI_SRIOV)
7113 vsi->vf_id = param1;
7114 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007115 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
7116 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007117 if (ret < 0) {
7118 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
7119 vsi->seid, ret);
7120 goto err_vsi;
7121 }
7122 vsi->base_queue = ret;
7123
7124 /* get a VSI from the hardware */
7125 vsi->uplink_seid = uplink_seid;
7126 ret = i40e_add_vsi(vsi);
7127 if (ret)
7128 goto err_vsi;
7129
7130 switch (vsi->type) {
7131 /* setup the netdev if needed */
7132 case I40E_VSI_MAIN:
7133 case I40E_VSI_VMDQ2:
7134 ret = i40e_config_netdev(vsi);
7135 if (ret)
7136 goto err_netdev;
7137 ret = register_netdev(vsi->netdev);
7138 if (ret)
7139 goto err_netdev;
7140 vsi->netdev_registered = true;
7141 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007142#ifdef CONFIG_I40E_DCB
7143 /* Setup DCB netlink interface */
7144 i40e_dcbnl_setup(vsi);
7145#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007146 /* fall through */
7147
7148 case I40E_VSI_FDIR:
7149 /* set up vectors and rings if needed */
7150 ret = i40e_vsi_setup_vectors(vsi);
7151 if (ret)
7152 goto err_msix;
7153
7154 ret = i40e_alloc_rings(vsi);
7155 if (ret)
7156 goto err_rings;
7157
7158 /* map all of the rings to the q_vectors */
7159 i40e_vsi_map_rings_to_vectors(vsi);
7160
7161 i40e_vsi_reset_stats(vsi);
7162 break;
7163
7164 default:
7165 /* no netdev or rings for the other VSI types */
7166 break;
7167 }
7168
7169 return vsi;
7170
7171err_rings:
7172 i40e_vsi_free_q_vectors(vsi);
7173err_msix:
7174 if (vsi->netdev_registered) {
7175 vsi->netdev_registered = false;
7176 unregister_netdev(vsi->netdev);
7177 free_netdev(vsi->netdev);
7178 vsi->netdev = NULL;
7179 }
7180err_netdev:
7181 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
7182err_vsi:
7183 i40e_vsi_clear(vsi);
7184err_alloc:
7185 return NULL;
7186}
7187
7188/**
7189 * i40e_veb_get_bw_info - Query VEB BW information
7190 * @veb: the veb to query
7191 *
7192 * Query the Tx scheduler BW configuration data for given VEB
7193 **/
7194static int i40e_veb_get_bw_info(struct i40e_veb *veb)
7195{
7196 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
7197 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
7198 struct i40e_pf *pf = veb->pf;
7199 struct i40e_hw *hw = &pf->hw;
7200 u32 tc_bw_max;
7201 int ret = 0;
7202 int i;
7203
7204 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
7205 &bw_data, NULL);
7206 if (ret) {
7207 dev_info(&pf->pdev->dev,
7208 "query veb bw config failed, aq_err=%d\n",
7209 hw->aq.asq_last_status);
7210 goto out;
7211 }
7212
7213 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
7214 &ets_data, NULL);
7215 if (ret) {
7216 dev_info(&pf->pdev->dev,
7217 "query veb bw ets config failed, aq_err=%d\n",
7218 hw->aq.asq_last_status);
7219 goto out;
7220 }
7221
7222 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
7223 veb->bw_max_quanta = ets_data.tc_bw_max;
7224 veb->is_abs_credits = bw_data.absolute_credits_enable;
7225 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
7226 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
7227 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7228 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
7229 veb->bw_tc_limit_credits[i] =
7230 le16_to_cpu(bw_data.tc_bw_limits[i]);
7231 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
7232 }
7233
7234out:
7235 return ret;
7236}
7237
7238/**
7239 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
7240 * @pf: board private structure
7241 *
7242 * On error: returns error code (negative)
7243 * On success: returns vsi index in PF (positive)
7244 **/
7245static int i40e_veb_mem_alloc(struct i40e_pf *pf)
7246{
7247 int ret = -ENOENT;
7248 struct i40e_veb *veb;
7249 int i;
7250
7251 /* Need to protect the allocation of switch elements at the PF level */
7252 mutex_lock(&pf->switch_mutex);
7253
7254 /* VEB list may be fragmented if VEB creation/destruction has
7255 * been happening. We can afford to do a quick scan to look
7256 * for any free slots in the list.
7257 *
7258 * find next empty veb slot, looping back around if necessary
7259 */
7260 i = 0;
7261 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
7262 i++;
7263 if (i >= I40E_MAX_VEB) {
7264 ret = -ENOMEM;
7265 goto err_alloc_veb; /* out of VEB slots! */
7266 }
7267
7268 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
7269 if (!veb) {
7270 ret = -ENOMEM;
7271 goto err_alloc_veb;
7272 }
7273 veb->pf = pf;
7274 veb->idx = i;
7275 veb->enabled_tc = 1;
7276
7277 pf->veb[i] = veb;
7278 ret = i;
7279err_alloc_veb:
7280 mutex_unlock(&pf->switch_mutex);
7281 return ret;
7282}
7283
7284/**
7285 * i40e_switch_branch_release - Delete a branch of the switch tree
7286 * @branch: where to start deleting
7287 *
7288 * This uses recursion to find the tips of the branch to be
7289 * removed, deleting until we get back to and can delete this VEB.
7290 **/
7291static void i40e_switch_branch_release(struct i40e_veb *branch)
7292{
7293 struct i40e_pf *pf = branch->pf;
7294 u16 branch_seid = branch->seid;
7295 u16 veb_idx = branch->idx;
7296 int i;
7297
7298 /* release any VEBs on this VEB - RECURSION */
7299 for (i = 0; i < I40E_MAX_VEB; i++) {
7300 if (!pf->veb[i])
7301 continue;
7302 if (pf->veb[i]->uplink_seid == branch->seid)
7303 i40e_switch_branch_release(pf->veb[i]);
7304 }
7305
7306 /* Release the VSIs on this VEB, but not the owner VSI.
7307 *
7308 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
7309 * the VEB itself, so don't use (*branch) after this loop.
7310 */
7311 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
7312 if (!pf->vsi[i])
7313 continue;
7314 if (pf->vsi[i]->uplink_seid == branch_seid &&
7315 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
7316 i40e_vsi_release(pf->vsi[i]);
7317 }
7318 }
7319
7320 /* There's one corner case where the VEB might not have been
7321 * removed, so double check it here and remove it if needed.
7322 * This case happens if the veb was created from the debugfs
7323 * commands and no VSIs were added to it.
7324 */
7325 if (pf->veb[veb_idx])
7326 i40e_veb_release(pf->veb[veb_idx]);
7327}
7328
7329/**
7330 * i40e_veb_clear - remove veb struct
7331 * @veb: the veb to remove
7332 **/
7333static void i40e_veb_clear(struct i40e_veb *veb)
7334{
7335 if (!veb)
7336 return;
7337
7338 if (veb->pf) {
7339 struct i40e_pf *pf = veb->pf;
7340
7341 mutex_lock(&pf->switch_mutex);
7342 if (pf->veb[veb->idx] == veb)
7343 pf->veb[veb->idx] = NULL;
7344 mutex_unlock(&pf->switch_mutex);
7345 }
7346
7347 kfree(veb);
7348}
7349
7350/**
7351 * i40e_veb_release - Delete a VEB and free its resources
7352 * @veb: the VEB being removed
7353 **/
7354void i40e_veb_release(struct i40e_veb *veb)
7355{
7356 struct i40e_vsi *vsi = NULL;
7357 struct i40e_pf *pf;
7358 int i, n = 0;
7359
7360 pf = veb->pf;
7361
7362 /* find the remaining VSI and check for extras */
7363 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
7364 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
7365 n++;
7366 vsi = pf->vsi[i];
7367 }
7368 }
7369 if (n != 1) {
7370 dev_info(&pf->pdev->dev,
7371 "can't remove VEB %d with %d VSIs left\n",
7372 veb->seid, n);
7373 return;
7374 }
7375
7376 /* move the remaining VSI to uplink veb */
7377 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
7378 if (veb->uplink_seid) {
7379 vsi->uplink_seid = veb->uplink_seid;
7380 if (veb->uplink_seid == pf->mac_seid)
7381 vsi->veb_idx = I40E_NO_VEB;
7382 else
7383 vsi->veb_idx = veb->veb_idx;
7384 } else {
7385 /* floating VEB */
7386 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
7387 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
7388 }
7389
7390 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
7391 i40e_veb_clear(veb);
7392
7393 return;
7394}
7395
7396/**
7397 * i40e_add_veb - create the VEB in the switch
7398 * @veb: the VEB to be instantiated
7399 * @vsi: the controlling VSI
7400 **/
7401static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
7402{
Greg Rose56747262013-11-28 06:39:37 +00007403 bool is_default = false;
Kevin Scotte1c51b952013-11-20 10:02:51 +00007404 bool is_cloud = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007405 int ret;
7406
7407 /* get a VEB from the hardware */
7408 ret = i40e_aq_add_veb(&veb->pf->hw, veb->uplink_seid, vsi->seid,
Kevin Scotte1c51b952013-11-20 10:02:51 +00007409 veb->enabled_tc, is_default,
7410 is_cloud, &veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007411 if (ret) {
7412 dev_info(&veb->pf->pdev->dev,
7413 "couldn't add VEB, err %d, aq_err %d\n",
7414 ret, veb->pf->hw.aq.asq_last_status);
7415 return -EPERM;
7416 }
7417
7418 /* get statistics counter */
7419 ret = i40e_aq_get_veb_parameters(&veb->pf->hw, veb->seid, NULL, NULL,
7420 &veb->stats_idx, NULL, NULL, NULL);
7421 if (ret) {
7422 dev_info(&veb->pf->pdev->dev,
7423 "couldn't get VEB statistics idx, err %d, aq_err %d\n",
7424 ret, veb->pf->hw.aq.asq_last_status);
7425 return -EPERM;
7426 }
7427 ret = i40e_veb_get_bw_info(veb);
7428 if (ret) {
7429 dev_info(&veb->pf->pdev->dev,
7430 "couldn't get VEB bw info, err %d, aq_err %d\n",
7431 ret, veb->pf->hw.aq.asq_last_status);
7432 i40e_aq_delete_element(&veb->pf->hw, veb->seid, NULL);
7433 return -ENOENT;
7434 }
7435
7436 vsi->uplink_seid = veb->seid;
7437 vsi->veb_idx = veb->idx;
7438 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
7439
7440 return 0;
7441}
7442
7443/**
7444 * i40e_veb_setup - Set up a VEB
7445 * @pf: board private structure
7446 * @flags: VEB setup flags
7447 * @uplink_seid: the switch element to link to
7448 * @vsi_seid: the initial VSI seid
7449 * @enabled_tc: Enabled TC bit-map
7450 *
7451 * This allocates the sw VEB structure and links it into the switch
7452 * It is possible and legal for this to be a duplicate of an already
7453 * existing VEB. It is also possible for both uplink and vsi seids
7454 * to be zero, in order to create a floating VEB.
7455 *
7456 * Returns pointer to the successfully allocated VEB sw struct on
7457 * success, otherwise returns NULL on failure.
7458 **/
7459struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
7460 u16 uplink_seid, u16 vsi_seid,
7461 u8 enabled_tc)
7462{
7463 struct i40e_veb *veb, *uplink_veb = NULL;
7464 int vsi_idx, veb_idx;
7465 int ret;
7466
7467 /* if one seid is 0, the other must be 0 to create a floating relay */
7468 if ((uplink_seid == 0 || vsi_seid == 0) &&
7469 (uplink_seid + vsi_seid != 0)) {
7470 dev_info(&pf->pdev->dev,
7471 "one, not both seid's are 0: uplink=%d vsi=%d\n",
7472 uplink_seid, vsi_seid);
7473 return NULL;
7474 }
7475
7476 /* make sure there is such a vsi and uplink */
7477 for (vsi_idx = 0; vsi_idx < pf->hw.func_caps.num_vsis; vsi_idx++)
7478 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
7479 break;
7480 if (vsi_idx >= pf->hw.func_caps.num_vsis && vsi_seid != 0) {
7481 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
7482 vsi_seid);
7483 return NULL;
7484 }
7485
7486 if (uplink_seid && uplink_seid != pf->mac_seid) {
7487 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
7488 if (pf->veb[veb_idx] &&
7489 pf->veb[veb_idx]->seid == uplink_seid) {
7490 uplink_veb = pf->veb[veb_idx];
7491 break;
7492 }
7493 }
7494 if (!uplink_veb) {
7495 dev_info(&pf->pdev->dev,
7496 "uplink seid %d not found\n", uplink_seid);
7497 return NULL;
7498 }
7499 }
7500
7501 /* get veb sw struct */
7502 veb_idx = i40e_veb_mem_alloc(pf);
7503 if (veb_idx < 0)
7504 goto err_alloc;
7505 veb = pf->veb[veb_idx];
7506 veb->flags = flags;
7507 veb->uplink_seid = uplink_seid;
7508 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
7509 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
7510
7511 /* create the VEB in the switch */
7512 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
7513 if (ret)
7514 goto err_veb;
7515
7516 return veb;
7517
7518err_veb:
7519 i40e_veb_clear(veb);
7520err_alloc:
7521 return NULL;
7522}
7523
7524/**
7525 * i40e_setup_pf_switch_element - set pf vars based on switch type
7526 * @pf: board private structure
7527 * @ele: element we are building info from
7528 * @num_reported: total number of elements
7529 * @printconfig: should we print the contents
7530 *
7531 * helper function to assist in extracting a few useful SEID values.
7532 **/
7533static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
7534 struct i40e_aqc_switch_config_element_resp *ele,
7535 u16 num_reported, bool printconfig)
7536{
7537 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
7538 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
7539 u8 element_type = ele->element_type;
7540 u16 seid = le16_to_cpu(ele->seid);
7541
7542 if (printconfig)
7543 dev_info(&pf->pdev->dev,
7544 "type=%d seid=%d uplink=%d downlink=%d\n",
7545 element_type, seid, uplink_seid, downlink_seid);
7546
7547 switch (element_type) {
7548 case I40E_SWITCH_ELEMENT_TYPE_MAC:
7549 pf->mac_seid = seid;
7550 break;
7551 case I40E_SWITCH_ELEMENT_TYPE_VEB:
7552 /* Main VEB? */
7553 if (uplink_seid != pf->mac_seid)
7554 break;
7555 if (pf->lan_veb == I40E_NO_VEB) {
7556 int v;
7557
7558 /* find existing or else empty VEB */
7559 for (v = 0; v < I40E_MAX_VEB; v++) {
7560 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
7561 pf->lan_veb = v;
7562 break;
7563 }
7564 }
7565 if (pf->lan_veb == I40E_NO_VEB) {
7566 v = i40e_veb_mem_alloc(pf);
7567 if (v < 0)
7568 break;
7569 pf->lan_veb = v;
7570 }
7571 }
7572
7573 pf->veb[pf->lan_veb]->seid = seid;
7574 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
7575 pf->veb[pf->lan_veb]->pf = pf;
7576 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
7577 break;
7578 case I40E_SWITCH_ELEMENT_TYPE_VSI:
7579 if (num_reported != 1)
7580 break;
7581 /* This is immediately after a reset so we can assume this is
7582 * the PF's VSI
7583 */
7584 pf->mac_seid = uplink_seid;
7585 pf->pf_seid = downlink_seid;
7586 pf->main_vsi_seid = seid;
7587 if (printconfig)
7588 dev_info(&pf->pdev->dev,
7589 "pf_seid=%d main_vsi_seid=%d\n",
7590 pf->pf_seid, pf->main_vsi_seid);
7591 break;
7592 case I40E_SWITCH_ELEMENT_TYPE_PF:
7593 case I40E_SWITCH_ELEMENT_TYPE_VF:
7594 case I40E_SWITCH_ELEMENT_TYPE_EMP:
7595 case I40E_SWITCH_ELEMENT_TYPE_BMC:
7596 case I40E_SWITCH_ELEMENT_TYPE_PE:
7597 case I40E_SWITCH_ELEMENT_TYPE_PA:
7598 /* ignore these for now */
7599 break;
7600 default:
7601 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
7602 element_type, seid);
7603 break;
7604 }
7605}
7606
7607/**
7608 * i40e_fetch_switch_configuration - Get switch config from firmware
7609 * @pf: board private structure
7610 * @printconfig: should we print the contents
7611 *
7612 * Get the current switch configuration from the device and
7613 * extract a few useful SEID values.
7614 **/
7615int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
7616{
7617 struct i40e_aqc_get_switch_config_resp *sw_config;
7618 u16 next_seid = 0;
7619 int ret = 0;
7620 u8 *aq_buf;
7621 int i;
7622
7623 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
7624 if (!aq_buf)
7625 return -ENOMEM;
7626
7627 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
7628 do {
7629 u16 num_reported, num_total;
7630
7631 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
7632 I40E_AQ_LARGE_BUF,
7633 &next_seid, NULL);
7634 if (ret) {
7635 dev_info(&pf->pdev->dev,
7636 "get switch config failed %d aq_err=%x\n",
7637 ret, pf->hw.aq.asq_last_status);
7638 kfree(aq_buf);
7639 return -ENOENT;
7640 }
7641
7642 num_reported = le16_to_cpu(sw_config->header.num_reported);
7643 num_total = le16_to_cpu(sw_config->header.num_total);
7644
7645 if (printconfig)
7646 dev_info(&pf->pdev->dev,
7647 "header: %d reported %d total\n",
7648 num_reported, num_total);
7649
7650 if (num_reported) {
7651 int sz = sizeof(*sw_config) * num_reported;
7652
7653 kfree(pf->sw_config);
7654 pf->sw_config = kzalloc(sz, GFP_KERNEL);
7655 if (pf->sw_config)
7656 memcpy(pf->sw_config, sw_config, sz);
7657 }
7658
7659 for (i = 0; i < num_reported; i++) {
7660 struct i40e_aqc_switch_config_element_resp *ele =
7661 &sw_config->element[i];
7662
7663 i40e_setup_pf_switch_element(pf, ele, num_reported,
7664 printconfig);
7665 }
7666 } while (next_seid != 0);
7667
7668 kfree(aq_buf);
7669 return ret;
7670}
7671
7672/**
7673 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
7674 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007675 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007676 *
7677 * Returns 0 on success, negative value on failure
7678 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007679static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007680{
Jesse Brandeburg895106a2013-11-26 10:49:16 +00007681 u32 rxfc = 0, txfc = 0, rxfc_reg;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007682 int ret;
7683
7684 /* find out what's out there already */
7685 ret = i40e_fetch_switch_configuration(pf, false);
7686 if (ret) {
7687 dev_info(&pf->pdev->dev,
7688 "couldn't fetch switch config, err %d, aq_err %d\n",
7689 ret, pf->hw.aq.asq_last_status);
7690 return ret;
7691 }
7692 i40e_pf_reset_stats(pf);
7693
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007694 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007695 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007696 struct i40e_vsi *vsi = NULL;
7697 u16 uplink_seid;
7698
7699 /* Set up the PF VSI associated with the PF's main VSI
7700 * that is already in the HW switch
7701 */
7702 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
7703 uplink_seid = pf->veb[pf->lan_veb]->seid;
7704 else
7705 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007706 if (pf->lan_vsi == I40E_NO_VSI)
7707 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
7708 else if (reinit)
7709 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007710 if (!vsi) {
7711 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
7712 i40e_fdir_teardown(pf);
7713 return -EAGAIN;
7714 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007715 } else {
7716 /* force a reset of TC and queue layout configurations */
7717 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
7718 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
7719 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
7720 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
7721 }
7722 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
7723
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007724 i40e_fdir_sb_setup(pf);
7725
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007726 /* Setup static PF queue filter control settings */
7727 ret = i40e_setup_pf_filter_control(pf);
7728 if (ret) {
7729 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
7730 ret);
7731 /* Failure here should not stop continuing other steps */
7732 }
7733
7734 /* enable RSS in the HW, even for only one queue, as the stack can use
7735 * the hash
7736 */
7737 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
7738 i40e_config_rss(pf);
7739
7740 /* fill in link information and enable LSE reporting */
7741 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
7742 i40e_link_event(pf);
7743
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007744 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007745 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
7746 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007747 /* requested_mode is set in probe or by ethtool */
7748 if (!pf->fc_autoneg_status)
7749 goto no_autoneg;
7750
7751 if ((pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX) &&
7752 (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007753 pf->hw.fc.current_mode = I40E_FC_FULL;
7754 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX)
7755 pf->hw.fc.current_mode = I40E_FC_TX_PAUSE;
7756 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX)
7757 pf->hw.fc.current_mode = I40E_FC_RX_PAUSE;
7758 else
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007759 pf->hw.fc.current_mode = I40E_FC_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007760
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007761 /* sync the flow control settings with the auto-neg values */
7762 switch (pf->hw.fc.current_mode) {
7763 case I40E_FC_FULL:
7764 txfc = 1;
7765 rxfc = 1;
7766 break;
7767 case I40E_FC_TX_PAUSE:
7768 txfc = 1;
7769 rxfc = 0;
7770 break;
7771 case I40E_FC_RX_PAUSE:
7772 txfc = 0;
7773 rxfc = 1;
7774 break;
7775 case I40E_FC_NONE:
7776 case I40E_FC_DEFAULT:
7777 txfc = 0;
7778 rxfc = 0;
7779 break;
7780 case I40E_FC_PFC:
7781 /* TBD */
7782 break;
7783 /* no default case, we have to handle all possibilities here */
7784 }
7785
7786 wr32(&pf->hw, I40E_PRTDCB_FCCFG, txfc << I40E_PRTDCB_FCCFG_TFCE_SHIFT);
7787
7788 rxfc_reg = rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
7789 ~I40E_PRTDCB_MFLCN_RFCE_MASK;
7790 rxfc_reg |= (rxfc << I40E_PRTDCB_MFLCN_RFCE_SHIFT);
7791
7792 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rxfc_reg);
7793
7794 goto fc_complete;
7795
7796no_autoneg:
7797 /* disable L2 flow control, user can turn it on if they wish */
7798 wr32(&pf->hw, I40E_PRTDCB_FCCFG, 0);
7799 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
7800 ~I40E_PRTDCB_MFLCN_RFCE_MASK);
7801
7802fc_complete:
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00007803 i40e_ptp_init(pf);
7804
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007805 return ret;
7806}
7807
7808/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007809 * i40e_determine_queue_usage - Work out queue distribution
7810 * @pf: board private structure
7811 **/
7812static void i40e_determine_queue_usage(struct i40e_pf *pf)
7813{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007814 int queues_left;
7815
7816 pf->num_lan_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007817
7818 /* Find the max queues to be put into basic use. We'll always be
7819 * using TC0, whether or not DCB is running, and TC0 will get the
7820 * big RSS set.
7821 */
7822 queues_left = pf->hw.func_caps.num_tx_qp;
7823
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007824 if ((queues_left == 1) ||
7825 !(pf->flags & I40E_FLAG_MSIX_ENABLED) ||
7826 !(pf->flags & (I40E_FLAG_RSS_ENABLED | I40E_FLAG_FD_SB_ENABLED |
7827 I40E_FLAG_DCB_ENABLED))) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007828 /* one qp for PF, no queues for anything else */
7829 queues_left = 0;
7830 pf->rss_size = pf->num_lan_qps = 1;
7831
7832 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007833 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
7834 I40E_FLAG_FD_SB_ENABLED |
7835 I40E_FLAG_FD_ATR_ENABLED |
7836 I40E_FLAG_DCB_ENABLED |
7837 I40E_FLAG_SRIOV_ENABLED |
7838 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007839 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007840 /* Not enough queues for all TCs */
7841 if ((pf->flags & I40E_FLAG_DCB_ENABLED) &&
7842 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
7843 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7844 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
7845 }
7846 pf->num_lan_qps = pf->rss_size_max;
7847 queues_left -= pf->num_lan_qps;
7848 }
7849
7850 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7851 if (queues_left > 1) {
7852 queues_left -= 1; /* save 1 queue for FD */
7853 } else {
7854 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7855 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
7856 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007857 }
7858
7859 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
7860 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007861 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
7862 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007863 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
7864 }
7865
7866 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7867 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
7868 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
7869 (queues_left / pf->num_vmdq_qps));
7870 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
7871 }
7872
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007873 pf->queues_left = queues_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007874 return;
7875}
7876
7877/**
7878 * i40e_setup_pf_filter_control - Setup PF static filter control
7879 * @pf: PF to be setup
7880 *
7881 * i40e_setup_pf_filter_control sets up a pf's initial filter control
7882 * settings. If PE/FCoE are enabled then it will also set the per PF
7883 * based filter sizes required for them. It also enables Flow director,
7884 * ethertype and macvlan type filter settings for the pf.
7885 *
7886 * Returns 0 on success, negative on failure
7887 **/
7888static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
7889{
7890 struct i40e_filter_control_settings *settings = &pf->filter_settings;
7891
7892 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
7893
7894 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007895 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007896 settings->enable_fdir = true;
7897
7898 /* Ethtype and MACVLAN filters enabled for PF */
7899 settings->enable_ethtype = true;
7900 settings->enable_macvlan = true;
7901
7902 if (i40e_set_filter_control(&pf->hw, settings))
7903 return -ENOENT;
7904
7905 return 0;
7906}
7907
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00007908#define INFO_STRING_LEN 255
7909static void i40e_print_features(struct i40e_pf *pf)
7910{
7911 struct i40e_hw *hw = &pf->hw;
7912 char *buf, *string;
7913
7914 string = kzalloc(INFO_STRING_LEN, GFP_KERNEL);
7915 if (!string) {
7916 dev_err(&pf->pdev->dev, "Features string allocation failed\n");
7917 return;
7918 }
7919
7920 buf = string;
7921
7922 buf += sprintf(string, "Features: PF-id[%d] ", hw->pf_id);
7923#ifdef CONFIG_PCI_IOV
7924 buf += sprintf(buf, "VFs: %d ", pf->num_req_vfs);
7925#endif
7926 buf += sprintf(buf, "VSIs: %d QP: %d ", pf->hw.func_caps.num_vsis,
7927 pf->vsi[pf->lan_vsi]->num_queue_pairs);
7928
7929 if (pf->flags & I40E_FLAG_RSS_ENABLED)
7930 buf += sprintf(buf, "RSS ");
7931 buf += sprintf(buf, "FDir ");
7932 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
7933 buf += sprintf(buf, "ATR ");
7934 if (pf->flags & I40E_FLAG_FD_SB_ENABLED)
7935 buf += sprintf(buf, "NTUPLE ");
7936 if (pf->flags & I40E_FLAG_DCB_ENABLED)
7937 buf += sprintf(buf, "DCB ");
7938 if (pf->flags & I40E_FLAG_PTP)
7939 buf += sprintf(buf, "PTP ");
7940
7941 BUG_ON(buf > (string + INFO_STRING_LEN));
7942 dev_info(&pf->pdev->dev, "%s\n", string);
7943 kfree(string);
7944}
7945
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007946/**
7947 * i40e_probe - Device initialization routine
7948 * @pdev: PCI device information struct
7949 * @ent: entry in i40e_pci_tbl
7950 *
7951 * i40e_probe initializes a pf identified by a pci_dev structure.
7952 * The OS initialization, configuring of the pf private structure,
7953 * and a hardware reset occur.
7954 *
7955 * Returns 0 on success, negative on failure
7956 **/
7957static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
7958{
7959 struct i40e_driver_version dv;
7960 struct i40e_pf *pf;
7961 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00007962 static u16 pfs_found;
Catherine Sullivand4dfb812013-11-28 06:39:21 +00007963 u16 link_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007964 int err = 0;
7965 u32 len;
7966
7967 err = pci_enable_device_mem(pdev);
7968 if (err)
7969 return err;
7970
7971 /* set up for high or low dma */
7972 if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64))) {
7973 /* coherent mask for the same size will always succeed if
7974 * dma_set_mask does
7975 */
7976 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
7977 } else if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
7978 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
7979 } else {
7980 dev_err(&pdev->dev, "DMA configuration failed: %d\n", err);
7981 err = -EIO;
7982 goto err_dma;
7983 }
7984
7985 /* set up pci connections */
7986 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
7987 IORESOURCE_MEM), i40e_driver_name);
7988 if (err) {
7989 dev_info(&pdev->dev,
7990 "pci_request_selected_regions failed %d\n", err);
7991 goto err_pci_reg;
7992 }
7993
7994 pci_enable_pcie_error_reporting(pdev);
7995 pci_set_master(pdev);
7996
7997 /* Now that we have a PCI connection, we need to do the
7998 * low level device setup. This is primarily setting up
7999 * the Admin Queue structures and then querying for the
8000 * device's current profile information.
8001 */
8002 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
8003 if (!pf) {
8004 err = -ENOMEM;
8005 goto err_pf_alloc;
8006 }
8007 pf->next_vsi = 0;
8008 pf->pdev = pdev;
8009 set_bit(__I40E_DOWN, &pf->state);
8010
8011 hw = &pf->hw;
8012 hw->back = pf;
8013 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
8014 pci_resource_len(pdev, 0));
8015 if (!hw->hw_addr) {
8016 err = -EIO;
8017 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
8018 (unsigned int)pci_resource_start(pdev, 0),
8019 (unsigned int)pci_resource_len(pdev, 0), err);
8020 goto err_ioremap;
8021 }
8022 hw->vendor_id = pdev->vendor;
8023 hw->device_id = pdev->device;
8024 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
8025 hw->subsystem_vendor_id = pdev->subsystem_vendor;
8026 hw->subsystem_device_id = pdev->subsystem_device;
8027 hw->bus.device = PCI_SLOT(pdev->devfn);
8028 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008029 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008030
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008031 /* do a special CORER for clearing PXE mode once at init */
8032 if (hw->revision_id == 0 &&
8033 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
8034 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
8035 i40e_flush(hw);
8036 msleep(200);
8037 pf->corer_count++;
8038
8039 i40e_clear_pxe_mode(hw);
8040 }
8041
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008042 /* Reset here to make sure all is clean and to define PF 'n' */
8043 err = i40e_pf_reset(hw);
8044 if (err) {
8045 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
8046 goto err_pf_reset;
8047 }
8048 pf->pfr_count++;
8049
8050 hw->aq.num_arq_entries = I40E_AQ_LEN;
8051 hw->aq.num_asq_entries = I40E_AQ_LEN;
8052 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
8053 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
8054 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
8055 snprintf(pf->misc_int_name, sizeof(pf->misc_int_name) - 1,
8056 "%s-pf%d:misc",
8057 dev_driver_string(&pf->pdev->dev), pf->hw.pf_id);
8058
8059 err = i40e_init_shared_code(hw);
8060 if (err) {
8061 dev_info(&pdev->dev, "init_shared_code failed: %d\n", err);
8062 goto err_pf_reset;
8063 }
8064
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008065 /* set up a default setting for link flow control */
8066 pf->hw.fc.requested_mode = I40E_FC_NONE;
8067
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008068 err = i40e_init_adminq(hw);
8069 dev_info(&pdev->dev, "%s\n", i40e_fw_version_str(hw));
8070 if (err) {
8071 dev_info(&pdev->dev,
8072 "init_adminq failed: %d expecting API %02x.%02x\n",
8073 err,
8074 I40E_FW_API_VERSION_MAJOR, I40E_FW_API_VERSION_MINOR);
8075 goto err_pf_reset;
8076 }
8077
Shannon Nelson6ff4ef82013-12-21 05:44:49 +00008078 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008079 err = i40e_get_capabilities(pf);
8080 if (err)
8081 goto err_adminq_setup;
8082
8083 err = i40e_sw_init(pf);
8084 if (err) {
8085 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
8086 goto err_sw_init;
8087 }
8088
8089 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
8090 hw->func_caps.num_rx_qp,
8091 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
8092 if (err) {
8093 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
8094 goto err_init_lan_hmc;
8095 }
8096
8097 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
8098 if (err) {
8099 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
8100 err = -ENOENT;
8101 goto err_configure_lan_hmc;
8102 }
8103
8104 i40e_get_mac_addr(hw, hw->mac.addr);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +00008105 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008106 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
8107 err = -EIO;
8108 goto err_mac_addr;
8109 }
8110 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
8111 memcpy(hw->mac.perm_addr, hw->mac.addr, ETH_ALEN);
8112
8113 pci_set_drvdata(pdev, pf);
8114 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008115#ifdef CONFIG_I40E_DCB
8116 err = i40e_init_pf_dcb(pf);
8117 if (err) {
8118 dev_info(&pdev->dev, "init_pf_dcb failed: %d\n", err);
8119 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8120 goto err_init_dcb;
8121 }
8122#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008123
8124 /* set up periodic task facility */
8125 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
8126 pf->service_timer_period = HZ;
8127
8128 INIT_WORK(&pf->service_task, i40e_service_task);
8129 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
8130 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
8131 pf->link_check_timeout = jiffies;
8132
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008133 /* WoL defaults to disabled */
8134 pf->wol_en = false;
8135 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
8136
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008137 /* set up the main switch operations */
8138 i40e_determine_queue_usage(pf);
8139 i40e_init_interrupt_scheme(pf);
8140
8141 /* Set up the *vsi struct based on the number of VSIs in the HW,
8142 * and set up our local tracking of the MAIN PF vsi.
8143 */
8144 len = sizeof(struct i40e_vsi *) * pf->hw.func_caps.num_vsis;
8145 pf->vsi = kzalloc(len, GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +00008146 if (!pf->vsi) {
8147 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008148 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +00008149 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008150
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008151 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008152 if (err) {
8153 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
8154 goto err_vsis;
8155 }
8156
8157 /* The main driver is (mostly) up and happy. We need to set this state
8158 * before setting up the misc vector or we get a race and the vector
8159 * ends up disabled forever.
8160 */
8161 clear_bit(__I40E_DOWN, &pf->state);
8162
8163 /* In case of MSIX we are going to setup the misc vector right here
8164 * to handle admin queue events etc. In case of legacy and MSI
8165 * the misc functionality and queue processing is combined in
8166 * the same vector and that gets setup at open.
8167 */
8168 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
8169 err = i40e_setup_misc_vector(pf);
8170 if (err) {
8171 dev_info(&pdev->dev,
8172 "setup of misc vector failed: %d\n", err);
8173 goto err_vsis;
8174 }
8175 }
8176
8177 /* prep for VF support */
8178 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
8179 (pf->flags & I40E_FLAG_MSIX_ENABLED)) {
8180 u32 val;
8181
8182 /* disable link interrupts for VFs */
8183 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
8184 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
8185 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
8186 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -08008187
8188 if (pci_num_vf(pdev)) {
8189 dev_info(&pdev->dev,
8190 "Active VFs found, allocating resources.\n");
8191 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
8192 if (err)
8193 dev_info(&pdev->dev,
8194 "Error %d allocating resources for existing VFs\n",
8195 err);
8196 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008197 }
8198
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008199 pfs_found++;
8200
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008201 i40e_dbg_pf_init(pf);
8202
8203 /* tell the firmware that we're starting */
8204 dv.major_version = DRV_VERSION_MAJOR;
8205 dv.minor_version = DRV_VERSION_MINOR;
8206 dv.build_version = DRV_VERSION_BUILD;
8207 dv.subbuild_version = 0;
8208 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
8209
8210 /* since everything's happy, start the service_task timer */
8211 mod_timer(&pf->service_timer,
8212 round_jiffies(jiffies + pf->service_timer_period));
8213
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008214 /* Get the negotiated link width and speed from PCI config space */
8215 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA, &link_status);
8216
8217 i40e_set_pci_config_data(hw, link_status);
8218
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00008219 dev_info(&pdev->dev, "PCI-Express: %s %s\n",
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008220 (hw->bus.speed == i40e_bus_speed_8000 ? "Speed 8.0GT/s" :
8221 hw->bus.speed == i40e_bus_speed_5000 ? "Speed 5.0GT/s" :
8222 hw->bus.speed == i40e_bus_speed_2500 ? "Speed 2.5GT/s" :
8223 "Unknown"),
8224 (hw->bus.width == i40e_bus_width_pcie_x8 ? "Width x8" :
8225 hw->bus.width == i40e_bus_width_pcie_x4 ? "Width x4" :
8226 hw->bus.width == i40e_bus_width_pcie_x2 ? "Width x2" :
8227 hw->bus.width == i40e_bus_width_pcie_x1 ? "Width x1" :
8228 "Unknown"));
8229
8230 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
8231 hw->bus.speed < i40e_bus_speed_8000) {
8232 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
8233 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
8234 }
8235
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008236 /* print a string summarizing features */
8237 i40e_print_features(pf);
8238
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008239 return 0;
8240
8241 /* Unwind what we've done if something failed in the setup */
8242err_vsis:
8243 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008244 i40e_clear_interrupt_scheme(pf);
8245 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +00008246err_switch_setup:
8247 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008248 del_timer_sync(&pf->service_timer);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008249#ifdef CONFIG_I40E_DCB
8250err_init_dcb:
8251#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008252err_mac_addr:
8253err_configure_lan_hmc:
8254 (void)i40e_shutdown_lan_hmc(hw);
8255err_init_lan_hmc:
8256 kfree(pf->qp_pile);
8257 kfree(pf->irq_pile);
8258err_sw_init:
8259err_adminq_setup:
8260 (void)i40e_shutdown_adminq(hw);
8261err_pf_reset:
8262 iounmap(hw->hw_addr);
8263err_ioremap:
8264 kfree(pf);
8265err_pf_alloc:
8266 pci_disable_pcie_error_reporting(pdev);
8267 pci_release_selected_regions(pdev,
8268 pci_select_bars(pdev, IORESOURCE_MEM));
8269err_pci_reg:
8270err_dma:
8271 pci_disable_device(pdev);
8272 return err;
8273}
8274
8275/**
8276 * i40e_remove - Device removal routine
8277 * @pdev: PCI device information struct
8278 *
8279 * i40e_remove is called by the PCI subsystem to alert the driver
8280 * that is should release a PCI device. This could be caused by a
8281 * Hot-Plug event, or because the driver is going to be removed from
8282 * memory.
8283 **/
8284static void i40e_remove(struct pci_dev *pdev)
8285{
8286 struct i40e_pf *pf = pci_get_drvdata(pdev);
8287 i40e_status ret_code;
8288 u32 reg;
8289 int i;
8290
8291 i40e_dbg_pf_exit(pf);
8292
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008293 i40e_ptp_stop(pf);
8294
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008295 /* no more scheduling of any task */
8296 set_bit(__I40E_DOWN, &pf->state);
8297 del_timer_sync(&pf->service_timer);
8298 cancel_work_sync(&pf->service_task);
8299
Mitch Williamseb2d80b2014-02-13 03:48:48 -08008300 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
8301 i40e_free_vfs(pf);
8302 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
8303 }
8304
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008305 i40e_fdir_teardown(pf);
8306
8307 /* If there is a switch structure or any orphans, remove them.
8308 * This will leave only the PF's VSI remaining.
8309 */
8310 for (i = 0; i < I40E_MAX_VEB; i++) {
8311 if (!pf->veb[i])
8312 continue;
8313
8314 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
8315 pf->veb[i]->uplink_seid == 0)
8316 i40e_switch_branch_release(pf->veb[i]);
8317 }
8318
8319 /* Now we can shutdown the PF's VSI, just before we kill
8320 * adminq and hmc.
8321 */
8322 if (pf->vsi[pf->lan_vsi])
8323 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
8324
8325 i40e_stop_misc_vector(pf);
8326 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
8327 synchronize_irq(pf->msix_entries[0].vector);
8328 free_irq(pf->msix_entries[0].vector, pf);
8329 }
8330
8331 /* shutdown and destroy the HMC */
8332 ret_code = i40e_shutdown_lan_hmc(&pf->hw);
8333 if (ret_code)
8334 dev_warn(&pdev->dev,
8335 "Failed to destroy the HMC resources: %d\n", ret_code);
8336
8337 /* shutdown the adminq */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008338 ret_code = i40e_shutdown_adminq(&pf->hw);
8339 if (ret_code)
8340 dev_warn(&pdev->dev,
8341 "Failed to destroy the Admin Queue resources: %d\n",
8342 ret_code);
8343
8344 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
8345 i40e_clear_interrupt_scheme(pf);
8346 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
8347 if (pf->vsi[i]) {
8348 i40e_vsi_clear_rings(pf->vsi[i]);
8349 i40e_vsi_clear(pf->vsi[i]);
8350 pf->vsi[i] = NULL;
8351 }
8352 }
8353
8354 for (i = 0; i < I40E_MAX_VEB; i++) {
8355 kfree(pf->veb[i]);
8356 pf->veb[i] = NULL;
8357 }
8358
8359 kfree(pf->qp_pile);
8360 kfree(pf->irq_pile);
8361 kfree(pf->sw_config);
8362 kfree(pf->vsi);
8363
8364 /* force a PF reset to clean anything leftover */
8365 reg = rd32(&pf->hw, I40E_PFGEN_CTRL);
8366 wr32(&pf->hw, I40E_PFGEN_CTRL, (reg | I40E_PFGEN_CTRL_PFSWR_MASK));
8367 i40e_flush(&pf->hw);
8368
8369 iounmap(pf->hw.hw_addr);
8370 kfree(pf);
8371 pci_release_selected_regions(pdev,
8372 pci_select_bars(pdev, IORESOURCE_MEM));
8373
8374 pci_disable_pcie_error_reporting(pdev);
8375 pci_disable_device(pdev);
8376}
8377
8378/**
8379 * i40e_pci_error_detected - warning that something funky happened in PCI land
8380 * @pdev: PCI device information struct
8381 *
8382 * Called to warn that something happened and the error handling steps
8383 * are in progress. Allows the driver to quiesce things, be ready for
8384 * remediation.
8385 **/
8386static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
8387 enum pci_channel_state error)
8388{
8389 struct i40e_pf *pf = pci_get_drvdata(pdev);
8390
8391 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
8392
8393 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008394 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
8395 rtnl_lock();
8396 i40e_prep_for_reset(pf);
8397 rtnl_unlock();
8398 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008399
8400 /* Request a slot reset */
8401 return PCI_ERS_RESULT_NEED_RESET;
8402}
8403
8404/**
8405 * i40e_pci_error_slot_reset - a PCI slot reset just happened
8406 * @pdev: PCI device information struct
8407 *
8408 * Called to find if the driver can work with the device now that
8409 * the pci slot has been reset. If a basic connection seems good
8410 * (registers are readable and have sane content) then return a
8411 * happy little PCI_ERS_RESULT_xxx.
8412 **/
8413static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
8414{
8415 struct i40e_pf *pf = pci_get_drvdata(pdev);
8416 pci_ers_result_t result;
8417 int err;
8418 u32 reg;
8419
8420 dev_info(&pdev->dev, "%s\n", __func__);
8421 if (pci_enable_device_mem(pdev)) {
8422 dev_info(&pdev->dev,
8423 "Cannot re-enable PCI device after reset.\n");
8424 result = PCI_ERS_RESULT_DISCONNECT;
8425 } else {
8426 pci_set_master(pdev);
8427 pci_restore_state(pdev);
8428 pci_save_state(pdev);
8429 pci_wake_from_d3(pdev, false);
8430
8431 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8432 if (reg == 0)
8433 result = PCI_ERS_RESULT_RECOVERED;
8434 else
8435 result = PCI_ERS_RESULT_DISCONNECT;
8436 }
8437
8438 err = pci_cleanup_aer_uncorrect_error_status(pdev);
8439 if (err) {
8440 dev_info(&pdev->dev,
8441 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
8442 err);
8443 /* non-fatal, continue */
8444 }
8445
8446 return result;
8447}
8448
8449/**
8450 * i40e_pci_error_resume - restart operations after PCI error recovery
8451 * @pdev: PCI device information struct
8452 *
8453 * Called to allow the driver to bring things back up after PCI error
8454 * and/or reset recovery has finished.
8455 **/
8456static void i40e_pci_error_resume(struct pci_dev *pdev)
8457{
8458 struct i40e_pf *pf = pci_get_drvdata(pdev);
8459
8460 dev_info(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008461 if (test_bit(__I40E_SUSPENDED, &pf->state))
8462 return;
8463
8464 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008465 i40e_handle_reset_warning(pf);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008466 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008467}
8468
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008469/**
8470 * i40e_shutdown - PCI callback for shutting down
8471 * @pdev: PCI device information struct
8472 **/
8473static void i40e_shutdown(struct pci_dev *pdev)
8474{
8475 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008476 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008477
8478 set_bit(__I40E_SUSPENDED, &pf->state);
8479 set_bit(__I40E_DOWN, &pf->state);
8480 rtnl_lock();
8481 i40e_prep_for_reset(pf);
8482 rtnl_unlock();
8483
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008484 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
8485 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
8486
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008487 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008488 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008489 pci_set_power_state(pdev, PCI_D3hot);
8490 }
8491}
8492
8493#ifdef CONFIG_PM
8494/**
8495 * i40e_suspend - PCI callback for moving to D3
8496 * @pdev: PCI device information struct
8497 **/
8498static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
8499{
8500 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008501 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008502
8503 set_bit(__I40E_SUSPENDED, &pf->state);
8504 set_bit(__I40E_DOWN, &pf->state);
8505 rtnl_lock();
8506 i40e_prep_for_reset(pf);
8507 rtnl_unlock();
8508
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008509 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
8510 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
8511
8512 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008513 pci_set_power_state(pdev, PCI_D3hot);
8514
8515 return 0;
8516}
8517
8518/**
8519 * i40e_resume - PCI callback for waking up from D3
8520 * @pdev: PCI device information struct
8521 **/
8522static int i40e_resume(struct pci_dev *pdev)
8523{
8524 struct i40e_pf *pf = pci_get_drvdata(pdev);
8525 u32 err;
8526
8527 pci_set_power_state(pdev, PCI_D0);
8528 pci_restore_state(pdev);
8529 /* pci_restore_state() clears dev->state_saves, so
8530 * call pci_save_state() again to restore it.
8531 */
8532 pci_save_state(pdev);
8533
8534 err = pci_enable_device_mem(pdev);
8535 if (err) {
8536 dev_err(&pdev->dev,
8537 "%s: Cannot enable PCI device from suspend\n",
8538 __func__);
8539 return err;
8540 }
8541 pci_set_master(pdev);
8542
8543 /* no wakeup events while running */
8544 pci_wake_from_d3(pdev, false);
8545
8546 /* handling the reset will rebuild the device state */
8547 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
8548 clear_bit(__I40E_DOWN, &pf->state);
8549 rtnl_lock();
8550 i40e_reset_and_rebuild(pf, false);
8551 rtnl_unlock();
8552 }
8553
8554 return 0;
8555}
8556
8557#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008558static const struct pci_error_handlers i40e_err_handler = {
8559 .error_detected = i40e_pci_error_detected,
8560 .slot_reset = i40e_pci_error_slot_reset,
8561 .resume = i40e_pci_error_resume,
8562};
8563
8564static struct pci_driver i40e_driver = {
8565 .name = i40e_driver_name,
8566 .id_table = i40e_pci_tbl,
8567 .probe = i40e_probe,
8568 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008569#ifdef CONFIG_PM
8570 .suspend = i40e_suspend,
8571 .resume = i40e_resume,
8572#endif
8573 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008574 .err_handler = &i40e_err_handler,
8575 .sriov_configure = i40e_pci_sriov_configure,
8576};
8577
8578/**
8579 * i40e_init_module - Driver registration routine
8580 *
8581 * i40e_init_module is the first routine called when the driver is
8582 * loaded. All it does is register with the PCI subsystem.
8583 **/
8584static int __init i40e_init_module(void)
8585{
8586 pr_info("%s: %s - version %s\n", i40e_driver_name,
8587 i40e_driver_string, i40e_driver_version_str);
8588 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
8589 i40e_dbg_init();
8590 return pci_register_driver(&i40e_driver);
8591}
8592module_init(i40e_init_module);
8593
8594/**
8595 * i40e_exit_module - Driver exit cleanup routine
8596 *
8597 * i40e_exit_module is called just before the driver is removed
8598 * from memory.
8599 **/
8600static void __exit i40e_exit_module(void)
8601{
8602 pci_unregister_driver(&i40e_driver);
8603 i40e_dbg_exit();
8604}
8605module_exit(i40e_exit_module);