blob: 669715bb340079987abffe12929851d1f0128c47 [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);
2459 err = i40e_vsi_configure_tx(vsi);
2460 if (!err)
2461 err = i40e_vsi_configure_rx(vsi);
2462
2463 return err;
2464}
2465
2466/**
2467 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
2468 * @vsi: the VSI being configured
2469 **/
2470static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
2471{
2472 struct i40e_pf *pf = vsi->back;
2473 struct i40e_q_vector *q_vector;
2474 struct i40e_hw *hw = &pf->hw;
2475 u16 vector;
2476 int i, q;
2477 u32 val;
2478 u32 qp;
2479
2480 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
2481 * and PFINT_LNKLSTn registers, e.g.:
2482 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
2483 */
2484 qp = vsi->base_queue;
2485 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00002486 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
2487 q_vector = vsi->q_vectors[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002488 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2489 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2490 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
2491 q_vector->rx.itr);
2492 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2493 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2494 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
2495 q_vector->tx.itr);
2496
2497 /* Linked list for the queuepairs assigned to this vector */
2498 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
2499 for (q = 0; q < q_vector->num_ringpairs; q++) {
2500 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2501 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2502 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
2503 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
2504 (I40E_QUEUE_TYPE_TX
2505 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
2506
2507 wr32(hw, I40E_QINT_RQCTL(qp), val);
2508
2509 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2510 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2511 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
2512 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
2513 (I40E_QUEUE_TYPE_RX
2514 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2515
2516 /* Terminate the linked list */
2517 if (q == (q_vector->num_ringpairs - 1))
2518 val |= (I40E_QUEUE_END_OF_LIST
2519 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2520
2521 wr32(hw, I40E_QINT_TQCTL(qp), val);
2522 qp++;
2523 }
2524 }
2525
2526 i40e_flush(hw);
2527}
2528
2529/**
2530 * i40e_enable_misc_int_causes - enable the non-queue interrupts
2531 * @hw: ptr to the hardware info
2532 **/
2533static void i40e_enable_misc_int_causes(struct i40e_hw *hw)
2534{
2535 u32 val;
2536
2537 /* clear things first */
2538 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
2539 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
2540
2541 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
2542 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
2543 I40E_PFINT_ICR0_ENA_GRST_MASK |
2544 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
2545 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002546 I40E_PFINT_ICR0_ENA_TIMESYNC_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002547 I40E_PFINT_ICR0_ENA_STORM_DETECT_MASK |
2548 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
2549 I40E_PFINT_ICR0_ENA_VFLR_MASK |
2550 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2551
2552 wr32(hw, I40E_PFINT_ICR0_ENA, val);
2553
2554 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00002555 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
2556 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002557
2558 /* OTHER_ITR_IDX = 0 */
2559 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
2560}
2561
2562/**
2563 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
2564 * @vsi: the VSI being configured
2565 **/
2566static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
2567{
Alexander Duyck493fb302013-09-28 07:01:44 +00002568 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002569 struct i40e_pf *pf = vsi->back;
2570 struct i40e_hw *hw = &pf->hw;
2571 u32 val;
2572
2573 /* set the ITR configuration */
2574 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2575 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2576 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
2577 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2578 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2579 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
2580
2581 i40e_enable_misc_int_causes(hw);
2582
2583 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
2584 wr32(hw, I40E_PFINT_LNKLST0, 0);
2585
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00002586 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002587 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2588 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2589 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2590
2591 wr32(hw, I40E_QINT_RQCTL(0), val);
2592
2593 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2594 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2595 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2596
2597 wr32(hw, I40E_QINT_TQCTL(0), val);
2598 i40e_flush(hw);
2599}
2600
2601/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00002602 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
2603 * @pf: board private structure
2604 **/
2605void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
2606{
2607 struct i40e_hw *hw = &pf->hw;
2608
2609 wr32(hw, I40E_PFINT_DYN_CTL0,
2610 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2611 i40e_flush(hw);
2612}
2613
2614/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002615 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
2616 * @pf: board private structure
2617 **/
Shannon Nelson116a57d2013-09-28 07:13:59 +00002618void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002619{
2620 struct i40e_hw *hw = &pf->hw;
2621 u32 val;
2622
2623 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
2624 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
2625 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
2626
2627 wr32(hw, I40E_PFINT_DYN_CTL0, val);
2628 i40e_flush(hw);
2629}
2630
2631/**
2632 * i40e_irq_dynamic_enable - Enable default interrupt generation settings
2633 * @vsi: pointer to a vsi
2634 * @vector: enable a particular Hw Interrupt vector
2635 **/
2636void i40e_irq_dynamic_enable(struct i40e_vsi *vsi, int vector)
2637{
2638 struct i40e_pf *pf = vsi->back;
2639 struct i40e_hw *hw = &pf->hw;
2640 u32 val;
2641
2642 val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
2643 I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
2644 (I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2645 wr32(hw, I40E_PFINT_DYN_CTLN(vector - 1), val);
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00002646 /* skip the flush */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002647}
2648
2649/**
2650 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
2651 * @irq: interrupt number
2652 * @data: pointer to a q_vector
2653 **/
2654static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
2655{
2656 struct i40e_q_vector *q_vector = data;
2657
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002658 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002659 return IRQ_HANDLED;
2660
2661 napi_schedule(&q_vector->napi);
2662
2663 return IRQ_HANDLED;
2664}
2665
2666/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002667 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
2668 * @vsi: the VSI being configured
2669 * @basename: name for the vector
2670 *
2671 * Allocates MSI-X vectors and requests interrupts from the kernel.
2672 **/
2673static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
2674{
2675 int q_vectors = vsi->num_q_vectors;
2676 struct i40e_pf *pf = vsi->back;
2677 int base = vsi->base_vector;
2678 int rx_int_idx = 0;
2679 int tx_int_idx = 0;
2680 int vector, err;
2681
2682 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00002683 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002684
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002685 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002686 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2687 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
2688 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002689 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002690 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2691 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00002692 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002693 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2694 "%s-%s-%d", basename, "tx", tx_int_idx++);
2695 } else {
2696 /* skip this unused q_vector */
2697 continue;
2698 }
2699 err = request_irq(pf->msix_entries[base + vector].vector,
2700 vsi->irq_handler,
2701 0,
2702 q_vector->name,
2703 q_vector);
2704 if (err) {
2705 dev_info(&pf->pdev->dev,
2706 "%s: request_irq failed, error: %d\n",
2707 __func__, err);
2708 goto free_queue_irqs;
2709 }
2710 /* assign the mask for this irq */
2711 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2712 &q_vector->affinity_mask);
2713 }
2714
2715 return 0;
2716
2717free_queue_irqs:
2718 while (vector) {
2719 vector--;
2720 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2721 NULL);
2722 free_irq(pf->msix_entries[base + vector].vector,
2723 &(vsi->q_vectors[vector]));
2724 }
2725 return err;
2726}
2727
2728/**
2729 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
2730 * @vsi: the VSI being un-configured
2731 **/
2732static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
2733{
2734 struct i40e_pf *pf = vsi->back;
2735 struct i40e_hw *hw = &pf->hw;
2736 int base = vsi->base_vector;
2737 int i;
2738
2739 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002740 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
2741 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002742 }
2743
2744 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2745 for (i = vsi->base_vector;
2746 i < (vsi->num_q_vectors + vsi->base_vector); i++)
2747 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
2748
2749 i40e_flush(hw);
2750 for (i = 0; i < vsi->num_q_vectors; i++)
2751 synchronize_irq(pf->msix_entries[i + base].vector);
2752 } else {
2753 /* Legacy and MSI mode - this stops all interrupt handling */
2754 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
2755 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
2756 i40e_flush(hw);
2757 synchronize_irq(pf->pdev->irq);
2758 }
2759}
2760
2761/**
2762 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
2763 * @vsi: the VSI being configured
2764 **/
2765static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
2766{
2767 struct i40e_pf *pf = vsi->back;
2768 int i;
2769
2770 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2771 for (i = vsi->base_vector;
2772 i < (vsi->num_q_vectors + vsi->base_vector); i++)
2773 i40e_irq_dynamic_enable(vsi, i);
2774 } else {
2775 i40e_irq_dynamic_enable_icr0(pf);
2776 }
2777
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00002778 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002779 return 0;
2780}
2781
2782/**
2783 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
2784 * @pf: board private structure
2785 **/
2786static void i40e_stop_misc_vector(struct i40e_pf *pf)
2787{
2788 /* Disable ICR 0 */
2789 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
2790 i40e_flush(&pf->hw);
2791}
2792
2793/**
2794 * i40e_intr - MSI/Legacy and non-queue interrupt handler
2795 * @irq: interrupt number
2796 * @data: pointer to a q_vector
2797 *
2798 * This is the handler used for all MSI/Legacy interrupts, and deals
2799 * with both queue and non-queue interrupts. This is also used in
2800 * MSIX mode to handle the non-queue interrupts.
2801 **/
2802static irqreturn_t i40e_intr(int irq, void *data)
2803{
2804 struct i40e_pf *pf = (struct i40e_pf *)data;
2805 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002806 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002807 u32 icr0, icr0_remaining;
2808 u32 val, ena_mask;
2809
2810 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002811 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002812
Shannon Nelson116a57d2013-09-28 07:13:59 +00002813 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
2814 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002815 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002816
Shannon Nelsoncd92e722013-11-16 10:00:44 +00002817 /* if interrupt but no bits showing, must be SWINT */
2818 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
2819 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
2820 pf->sw_int_count++;
2821
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002822 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
2823 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
2824
2825 /* temporarily disable queue cause for NAPI processing */
2826 u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
2827 qval &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
2828 wr32(hw, I40E_QINT_RQCTL(0), qval);
2829
2830 qval = rd32(hw, I40E_QINT_TQCTL(0));
2831 qval &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
2832 wr32(hw, I40E_QINT_TQCTL(0), qval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002833
2834 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck493fb302013-09-28 07:01:44 +00002835 napi_schedule(&pf->vsi[pf->lan_vsi]->q_vectors[0]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002836 }
2837
2838 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
2839 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2840 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
2841 }
2842
2843 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
2844 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
2845 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
2846 }
2847
2848 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
2849 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
2850 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
2851 }
2852
2853 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
2854 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
2855 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
2856 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
2857 val = rd32(hw, I40E_GLGEN_RSTAT);
2858 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
2859 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelsond52cf0a2013-11-16 10:00:39 +00002860 if (val == I40E_RESET_CORER)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002861 pf->corer_count++;
Shannon Nelsond52cf0a2013-11-16 10:00:39 +00002862 else if (val == I40E_RESET_GLOBR)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002863 pf->globr_count++;
Shannon Nelsond52cf0a2013-11-16 10:00:39 +00002864 else if (val == I40E_RESET_EMPR)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002865 pf->empr_count++;
2866 }
2867
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002868 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
2869 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
2870 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
2871 }
2872
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002873 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
2874 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
2875
2876 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
2877 ena_mask &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
2878 i40e_ptp_tx_hwtstamp(pf);
2879 prttsyn_stat &= ~I40E_PRTTSYN_STAT_0_TXTIME_MASK;
2880 }
2881
2882 wr32(hw, I40E_PRTTSYN_STAT_0, prttsyn_stat);
2883 }
2884
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002885 /* If a critical error is pending we have no choice but to reset the
2886 * device.
2887 * Report and mask out any remaining unexpected interrupts.
2888 */
2889 icr0_remaining = icr0 & ena_mask;
2890 if (icr0_remaining) {
2891 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
2892 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002893 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002894 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
2895 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK) ||
2896 (icr0_remaining & I40E_PFINT_ICR0_MAL_DETECT_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00002897 dev_info(&pf->pdev->dev, "device will be reset\n");
2898 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
2899 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002900 }
2901 ena_mask &= ~icr0_remaining;
2902 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002903 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002904
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002905enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002906 /* re-enable interrupt causes */
2907 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002908 if (!test_bit(__I40E_DOWN, &pf->state)) {
2909 i40e_service_event_schedule(pf);
2910 i40e_irq_dynamic_enable_icr0(pf);
2911 }
2912
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00002913 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002914}
2915
2916/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08002917 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
2918 * @tx_ring: tx ring to clean
2919 * @budget: how many cleans we're allowed
2920 *
2921 * Returns true if there's any budget left (e.g. the clean is finished)
2922 **/
2923static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
2924{
2925 struct i40e_vsi *vsi = tx_ring->vsi;
2926 u16 i = tx_ring->next_to_clean;
2927 struct i40e_tx_buffer *tx_buf;
2928 struct i40e_tx_desc *tx_desc;
2929
2930 tx_buf = &tx_ring->tx_bi[i];
2931 tx_desc = I40E_TX_DESC(tx_ring, i);
2932 i -= tx_ring->count;
2933
2934 do {
2935 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
2936
2937 /* if next_to_watch is not set then there is no work pending */
2938 if (!eop_desc)
2939 break;
2940
2941 /* prevent any other reads prior to eop_desc */
2942 read_barrier_depends();
2943
2944 /* if the descriptor isn't done, no work yet to do */
2945 if (!(eop_desc->cmd_type_offset_bsz &
2946 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
2947 break;
2948
2949 /* clear next_to_watch to prevent false hangs */
2950 tx_buf->next_to_watch = NULL;
2951
2952 /* unmap skb header data */
2953 dma_unmap_single(tx_ring->dev,
2954 dma_unmap_addr(tx_buf, dma),
2955 dma_unmap_len(tx_buf, len),
2956 DMA_TO_DEVICE);
2957
2958 dma_unmap_len_set(tx_buf, len, 0);
2959
2960
2961 /* move to the next desc and buffer to clean */
2962 tx_buf++;
2963 tx_desc++;
2964 i++;
2965 if (unlikely(!i)) {
2966 i -= tx_ring->count;
2967 tx_buf = tx_ring->tx_bi;
2968 tx_desc = I40E_TX_DESC(tx_ring, 0);
2969 }
2970
2971 /* update budget accounting */
2972 budget--;
2973 } while (likely(budget));
2974
2975 i += tx_ring->count;
2976 tx_ring->next_to_clean = i;
2977
2978 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
2979 i40e_irq_dynamic_enable(vsi,
2980 tx_ring->q_vector->v_idx + vsi->base_vector);
2981 }
2982 return budget > 0;
2983}
2984
2985/**
2986 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
2987 * @irq: interrupt number
2988 * @data: pointer to a q_vector
2989 **/
2990static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
2991{
2992 struct i40e_q_vector *q_vector = data;
2993 struct i40e_vsi *vsi;
2994
2995 if (!q_vector->tx.ring)
2996 return IRQ_HANDLED;
2997
2998 vsi = q_vector->tx.ring->vsi;
2999 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3000
3001 return IRQ_HANDLED;
3002}
3003
3004/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003005 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003006 * @vsi: the VSI being configured
3007 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003008 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003009 **/
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003010static void map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003011{
Alexander Duyck493fb302013-09-28 07:01:44 +00003012 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00003013 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3014 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003015
3016 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003017 tx_ring->next = q_vector->tx.ring;
3018 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003019 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003020
3021 rx_ring->q_vector = q_vector;
3022 rx_ring->next = q_vector->rx.ring;
3023 q_vector->rx.ring = rx_ring;
3024 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003025}
3026
3027/**
3028 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3029 * @vsi: the VSI being configured
3030 *
3031 * This function maps descriptor rings to the queue-specific vectors
3032 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3033 * one vector per queue pair, but on a constrained vector budget, we
3034 * group the queue pairs as "efficiently" as possible.
3035 **/
3036static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3037{
3038 int qp_remaining = vsi->num_queue_pairs;
3039 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003040 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003041 int v_start = 0;
3042 int qp_idx = 0;
3043
3044 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3045 * group them so there are multiple queues per vector.
3046 */
3047 for (; v_start < q_vectors && qp_remaining; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003048 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3049
3050 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3051
3052 q_vector->num_ringpairs = num_ringpairs;
3053
3054 q_vector->rx.count = 0;
3055 q_vector->tx.count = 0;
3056 q_vector->rx.ring = NULL;
3057 q_vector->tx.ring = NULL;
3058
3059 while (num_ringpairs--) {
3060 map_vector_to_qp(vsi, v_start, qp_idx);
3061 qp_idx++;
3062 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003063 }
3064 }
3065}
3066
3067/**
3068 * i40e_vsi_request_irq - Request IRQ from the OS
3069 * @vsi: the VSI being configured
3070 * @basename: name for the vector
3071 **/
3072static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3073{
3074 struct i40e_pf *pf = vsi->back;
3075 int err;
3076
3077 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3078 err = i40e_vsi_request_irq_msix(vsi, basename);
3079 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3080 err = request_irq(pf->pdev->irq, i40e_intr, 0,
3081 pf->misc_int_name, pf);
3082 else
3083 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
3084 pf->misc_int_name, pf);
3085
3086 if (err)
3087 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3088
3089 return err;
3090}
3091
3092#ifdef CONFIG_NET_POLL_CONTROLLER
3093/**
3094 * i40e_netpoll - A Polling 'interrupt'handler
3095 * @netdev: network interface device structure
3096 *
3097 * This is used by netconsole to send skbs without having to re-enable
3098 * interrupts. It's not called while the normal interrupt routine is executing.
3099 **/
3100static void i40e_netpoll(struct net_device *netdev)
3101{
3102 struct i40e_netdev_priv *np = netdev_priv(netdev);
3103 struct i40e_vsi *vsi = np->vsi;
3104 struct i40e_pf *pf = vsi->back;
3105 int i;
3106
3107 /* if interface is down do nothing */
3108 if (test_bit(__I40E_DOWN, &vsi->state))
3109 return;
3110
3111 pf->flags |= I40E_FLAG_IN_NETPOLL;
3112 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3113 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003114 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003115 } else {
3116 i40e_intr(pf->pdev->irq, netdev);
3117 }
3118 pf->flags &= ~I40E_FLAG_IN_NETPOLL;
3119}
3120#endif
3121
3122/**
3123 * i40e_vsi_control_tx - Start or stop a VSI's rings
3124 * @vsi: the VSI being configured
3125 * @enable: start or stop the rings
3126 **/
3127static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3128{
3129 struct i40e_pf *pf = vsi->back;
3130 struct i40e_hw *hw = &pf->hw;
3131 int i, j, pf_q;
3132 u32 tx_reg;
3133
3134 pf_q = vsi->base_queue;
3135 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003136 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003137 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003138 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3139 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3140 break;
3141 usleep_range(1000, 2000);
3142 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003143 /* Skip if the queue is already in the requested state */
3144 if (enable && (tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3145 continue;
3146 if (!enable && !(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3147 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003148
3149 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003150 if (enable) {
3151 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003152 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003153 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003154 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003155 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003156
3157 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
3158
3159 /* wait for the change to finish */
3160 for (j = 0; j < 10; j++) {
3161 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
3162 if (enable) {
3163 if ((tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3164 break;
3165 } else {
3166 if (!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3167 break;
3168 }
3169
3170 udelay(10);
3171 }
3172 if (j >= 10) {
3173 dev_info(&pf->pdev->dev, "Tx ring %d %sable timeout\n",
3174 pf_q, (enable ? "en" : "dis"));
3175 return -ETIMEDOUT;
3176 }
3177 }
3178
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003179 if (hw->revision_id == 0)
3180 mdelay(50);
3181
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003182 return 0;
3183}
3184
3185/**
3186 * i40e_vsi_control_rx - Start or stop a VSI's rings
3187 * @vsi: the VSI being configured
3188 * @enable: start or stop the rings
3189 **/
3190static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3191{
3192 struct i40e_pf *pf = vsi->back;
3193 struct i40e_hw *hw = &pf->hw;
3194 int i, j, pf_q;
3195 u32 rx_reg;
3196
3197 pf_q = vsi->base_queue;
3198 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003199 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003200 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003201 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
3202 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
3203 break;
3204 usleep_range(1000, 2000);
3205 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003206
3207 if (enable) {
3208 /* is STAT set ? */
3209 if ((rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3210 continue;
3211 } else {
3212 /* is !STAT set ? */
3213 if (!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3214 continue;
3215 }
3216
3217 /* turn on/off the queue */
3218 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08003219 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003220 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08003221 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003222 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
3223
3224 /* wait for the change to finish */
3225 for (j = 0; j < 10; j++) {
3226 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
3227
3228 if (enable) {
3229 if ((rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3230 break;
3231 } else {
3232 if (!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3233 break;
3234 }
3235
3236 udelay(10);
3237 }
3238 if (j >= 10) {
3239 dev_info(&pf->pdev->dev, "Rx ring %d %sable timeout\n",
3240 pf_q, (enable ? "en" : "dis"));
3241 return -ETIMEDOUT;
3242 }
3243 }
3244
3245 return 0;
3246}
3247
3248/**
3249 * i40e_vsi_control_rings - Start or stop a VSI's rings
3250 * @vsi: the VSI being configured
3251 * @enable: start or stop the rings
3252 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00003253int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003254{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003255 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003256
3257 /* do rx first for enable and last for disable */
3258 if (request) {
3259 ret = i40e_vsi_control_rx(vsi, request);
3260 if (ret)
3261 return ret;
3262 ret = i40e_vsi_control_tx(vsi, request);
3263 } else {
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003264 /* Ignore return value, we need to shutdown whatever we can */
3265 i40e_vsi_control_tx(vsi, request);
3266 i40e_vsi_control_rx(vsi, request);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003267 }
3268
3269 return ret;
3270}
3271
3272/**
3273 * i40e_vsi_free_irq - Free the irq association with the OS
3274 * @vsi: the VSI being configured
3275 **/
3276static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
3277{
3278 struct i40e_pf *pf = vsi->back;
3279 struct i40e_hw *hw = &pf->hw;
3280 int base = vsi->base_vector;
3281 u32 val, qp;
3282 int i;
3283
3284 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3285 if (!vsi->q_vectors)
3286 return;
3287
3288 for (i = 0; i < vsi->num_q_vectors; i++) {
3289 u16 vector = i + base;
3290
3291 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00003292 if (!vsi->q_vectors[i] ||
3293 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003294 continue;
3295
3296 /* clear the affinity_mask in the IRQ descriptor */
3297 irq_set_affinity_hint(pf->msix_entries[vector].vector,
3298 NULL);
3299 free_irq(pf->msix_entries[vector].vector,
Alexander Duyck493fb302013-09-28 07:01:44 +00003300 vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003301
3302 /* Tear down the interrupt queue link list
3303 *
3304 * We know that they come in pairs and always
3305 * the Rx first, then the Tx. To clear the
3306 * link list, stick the EOL value into the
3307 * next_q field of the registers.
3308 */
3309 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
3310 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3311 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3312 val |= I40E_QUEUE_END_OF_LIST
3313 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3314 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
3315
3316 while (qp != I40E_QUEUE_END_OF_LIST) {
3317 u32 next;
3318
3319 val = rd32(hw, I40E_QINT_RQCTL(qp));
3320
3321 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3322 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3323 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3324 I40E_QINT_RQCTL_INTEVENT_MASK);
3325
3326 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3327 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3328
3329 wr32(hw, I40E_QINT_RQCTL(qp), val);
3330
3331 val = rd32(hw, I40E_QINT_TQCTL(qp));
3332
3333 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
3334 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
3335
3336 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3337 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3338 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3339 I40E_QINT_TQCTL_INTEVENT_MASK);
3340
3341 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3342 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3343
3344 wr32(hw, I40E_QINT_TQCTL(qp), val);
3345 qp = next;
3346 }
3347 }
3348 } else {
3349 free_irq(pf->pdev->irq, pf);
3350
3351 val = rd32(hw, I40E_PFINT_LNKLST0);
3352 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3353 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3354 val |= I40E_QUEUE_END_OF_LIST
3355 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
3356 wr32(hw, I40E_PFINT_LNKLST0, val);
3357
3358 val = rd32(hw, I40E_QINT_RQCTL(qp));
3359 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3360 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3361 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3362 I40E_QINT_RQCTL_INTEVENT_MASK);
3363
3364 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3365 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3366
3367 wr32(hw, I40E_QINT_RQCTL(qp), val);
3368
3369 val = rd32(hw, I40E_QINT_TQCTL(qp));
3370
3371 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3372 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3373 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3374 I40E_QINT_TQCTL_INTEVENT_MASK);
3375
3376 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3377 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3378
3379 wr32(hw, I40E_QINT_TQCTL(qp), val);
3380 }
3381}
3382
3383/**
Alexander Duyck493fb302013-09-28 07:01:44 +00003384 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
3385 * @vsi: the VSI being configured
3386 * @v_idx: Index of vector to be freed
3387 *
3388 * This function frees the memory allocated to the q_vector. In addition if
3389 * NAPI is enabled it will delete any references to the NAPI struct prior
3390 * to freeing the q_vector.
3391 **/
3392static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
3393{
3394 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003395 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00003396
3397 if (!q_vector)
3398 return;
3399
3400 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003401 i40e_for_each_ring(ring, q_vector->tx)
3402 ring->q_vector = NULL;
3403
3404 i40e_for_each_ring(ring, q_vector->rx)
3405 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00003406
3407 /* only VSI w/ an associated netdev is set up w/ NAPI */
3408 if (vsi->netdev)
3409 netif_napi_del(&q_vector->napi);
3410
3411 vsi->q_vectors[v_idx] = NULL;
3412
3413 kfree_rcu(q_vector, rcu);
3414}
3415
3416/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003417 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
3418 * @vsi: the VSI being un-configured
3419 *
3420 * This frees the memory allocated to the q_vectors and
3421 * deletes references to the NAPI struct.
3422 **/
3423static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
3424{
3425 int v_idx;
3426
Alexander Duyck493fb302013-09-28 07:01:44 +00003427 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
3428 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003429}
3430
3431/**
3432 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
3433 * @pf: board private structure
3434 **/
3435static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
3436{
3437 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
3438 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3439 pci_disable_msix(pf->pdev);
3440 kfree(pf->msix_entries);
3441 pf->msix_entries = NULL;
3442 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
3443 pci_disable_msi(pf->pdev);
3444 }
3445 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
3446}
3447
3448/**
3449 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
3450 * @pf: board private structure
3451 *
3452 * We go through and clear interrupt specific resources and reset the structure
3453 * to pre-load conditions
3454 **/
3455static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
3456{
3457 int i;
3458
3459 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
3460 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
3461 if (pf->vsi[i])
3462 i40e_vsi_free_q_vectors(pf->vsi[i]);
3463 i40e_reset_interrupt_capability(pf);
3464}
3465
3466/**
3467 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
3468 * @vsi: the VSI being configured
3469 **/
3470static void i40e_napi_enable_all(struct i40e_vsi *vsi)
3471{
3472 int q_idx;
3473
3474 if (!vsi->netdev)
3475 return;
3476
3477 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003478 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003479}
3480
3481/**
3482 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
3483 * @vsi: the VSI being configured
3484 **/
3485static void i40e_napi_disable_all(struct i40e_vsi *vsi)
3486{
3487 int q_idx;
3488
3489 if (!vsi->netdev)
3490 return;
3491
3492 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003493 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003494}
3495
3496/**
3497 * i40e_quiesce_vsi - Pause a given VSI
3498 * @vsi: the VSI being paused
3499 **/
3500static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
3501{
3502 if (test_bit(__I40E_DOWN, &vsi->state))
3503 return;
3504
3505 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
3506 if (vsi->netdev && netif_running(vsi->netdev)) {
3507 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
3508 } else {
3509 set_bit(__I40E_DOWN, &vsi->state);
3510 i40e_down(vsi);
3511 }
3512}
3513
3514/**
3515 * i40e_unquiesce_vsi - Resume a given VSI
3516 * @vsi: the VSI being resumed
3517 **/
3518static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
3519{
3520 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
3521 return;
3522
3523 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
3524 if (vsi->netdev && netif_running(vsi->netdev))
3525 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
3526 else
3527 i40e_up(vsi); /* this clears the DOWN bit */
3528}
3529
3530/**
3531 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
3532 * @pf: the PF
3533 **/
3534static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
3535{
3536 int v;
3537
3538 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
3539 if (pf->vsi[v])
3540 i40e_quiesce_vsi(pf->vsi[v]);
3541 }
3542}
3543
3544/**
3545 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
3546 * @pf: the PF
3547 **/
3548static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
3549{
3550 int v;
3551
3552 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
3553 if (pf->vsi[v])
3554 i40e_unquiesce_vsi(pf->vsi[v]);
3555 }
3556}
3557
3558/**
3559 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
3560 * @dcbcfg: the corresponding DCBx configuration structure
3561 *
3562 * Return the number of TCs from given DCBx configuration
3563 **/
3564static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
3565{
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003566 u8 num_tc = 0;
3567 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003568
3569 /* Scan the ETS Config Priority Table to find
3570 * traffic class enabled for a given priority
3571 * and use the traffic class index to get the
3572 * number of traffic classes enabled
3573 */
3574 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3575 if (dcbcfg->etscfg.prioritytable[i] > num_tc)
3576 num_tc = dcbcfg->etscfg.prioritytable[i];
3577 }
3578
3579 /* Traffic class index starts from zero so
3580 * increment to return the actual count
3581 */
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003582 return num_tc + 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003583}
3584
3585/**
3586 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
3587 * @dcbcfg: the corresponding DCBx configuration structure
3588 *
3589 * Query the current DCB configuration and return the number of
3590 * traffic classes enabled from the given DCBX config
3591 **/
3592static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
3593{
3594 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
3595 u8 enabled_tc = 1;
3596 u8 i;
3597
3598 for (i = 0; i < num_tc; i++)
3599 enabled_tc |= 1 << i;
3600
3601 return enabled_tc;
3602}
3603
3604/**
3605 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
3606 * @pf: PF being queried
3607 *
3608 * Return number of traffic classes enabled for the given PF
3609 **/
3610static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
3611{
3612 struct i40e_hw *hw = &pf->hw;
3613 u8 i, enabled_tc;
3614 u8 num_tc = 0;
3615 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3616
3617 /* If DCB is not enabled then always in single TC */
3618 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3619 return 1;
3620
3621 /* MFP mode return count of enabled TCs for this PF */
3622 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
3623 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3624 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3625 if (enabled_tc & (1 << i))
3626 num_tc++;
3627 }
3628 return num_tc;
3629 }
3630
3631 /* SFP mode will be enabled for all TCs on port */
3632 return i40e_dcb_get_num_tc(dcbcfg);
3633}
3634
3635/**
3636 * i40e_pf_get_default_tc - Get bitmap for first enabled TC
3637 * @pf: PF being queried
3638 *
3639 * Return a bitmap for first enabled traffic class for this PF.
3640 **/
3641static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
3642{
3643 u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3644 u8 i = 0;
3645
3646 if (!enabled_tc)
3647 return 0x1; /* TC0 */
3648
3649 /* Find the first enabled TC */
3650 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3651 if (enabled_tc & (1 << i))
3652 break;
3653 }
3654
3655 return 1 << i;
3656}
3657
3658/**
3659 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
3660 * @pf: PF being queried
3661 *
3662 * Return a bitmap for enabled traffic classes for this PF.
3663 **/
3664static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
3665{
3666 /* If DCB is not enabled for this PF then just return default TC */
3667 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3668 return i40e_pf_get_default_tc(pf);
3669
3670 /* MFP mode will have enabled TCs set by FW */
3671 if (pf->flags & I40E_FLAG_MFP_ENABLED)
3672 return pf->hw.func_caps.enabled_tcmap;
3673
3674 /* SFP mode we want PF to be enabled for all TCs */
3675 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
3676}
3677
3678/**
3679 * i40e_vsi_get_bw_info - Query VSI BW Information
3680 * @vsi: the VSI being queried
3681 *
3682 * Returns 0 on success, negative value on failure
3683 **/
3684static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
3685{
3686 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
3687 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
3688 struct i40e_pf *pf = vsi->back;
3689 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003690 i40e_status aq_ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003691 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003692 int i;
3693
3694 /* Get the VSI level BW configuration */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003695 aq_ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
3696 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003697 dev_info(&pf->pdev->dev,
3698 "couldn't get pf vsi bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003699 aq_ret, pf->hw.aq.asq_last_status);
3700 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003701 }
3702
3703 /* Get the VSI level BW configuration per TC */
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003704 aq_ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
Jesse Brandeburg6838b532014-01-14 00:49:52 -08003705 NULL);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003706 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003707 dev_info(&pf->pdev->dev,
3708 "couldn't get pf vsi ets bw config, err %d, aq_err %d\n",
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003709 aq_ret, pf->hw.aq.asq_last_status);
3710 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003711 }
3712
3713 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
3714 dev_info(&pf->pdev->dev,
3715 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
3716 bw_config.tc_valid_bits,
3717 bw_ets_config.tc_valid_bits);
3718 /* Still continuing */
3719 }
3720
3721 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
3722 vsi->bw_max_quanta = bw_config.max_bw;
3723 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
3724 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
3725 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3726 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
3727 vsi->bw_ets_limit_credits[i] =
3728 le16_to_cpu(bw_ets_config.credits[i]);
3729 /* 3 bits out of 4 for each TC */
3730 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
3731 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00003732
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003733 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003734}
3735
3736/**
3737 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
3738 * @vsi: the VSI being configured
3739 * @enabled_tc: TC bitmap
3740 * @bw_credits: BW shared credits per TC
3741 *
3742 * Returns 0 on success, negative value on failure
3743 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003744static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003745 u8 *bw_share)
3746{
3747 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003748 i40e_status aq_ret;
3749 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003750
3751 bw_data.tc_valid_bits = enabled_tc;
3752 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3753 bw_data.tc_bw_credits[i] = bw_share[i];
3754
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003755 aq_ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
3756 NULL);
3757 if (aq_ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003758 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00003759 "AQ command Config VSI BW allocation per TC failed = %d\n",
3760 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003761 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003762 }
3763
3764 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3765 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
3766
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00003767 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003768}
3769
3770/**
3771 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
3772 * @vsi: the VSI being configured
3773 * @enabled_tc: TC map to be enabled
3774 *
3775 **/
3776static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3777{
3778 struct net_device *netdev = vsi->netdev;
3779 struct i40e_pf *pf = vsi->back;
3780 struct i40e_hw *hw = &pf->hw;
3781 u8 netdev_tc = 0;
3782 int i;
3783 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3784
3785 if (!netdev)
3786 return;
3787
3788 if (!enabled_tc) {
3789 netdev_reset_tc(netdev);
3790 return;
3791 }
3792
3793 /* Set up actual enabled TCs on the VSI */
3794 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
3795 return;
3796
3797 /* set per TC queues for the VSI */
3798 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3799 /* Only set TC queues for enabled tcs
3800 *
3801 * e.g. For a VSI that has TC0 and TC3 enabled the
3802 * enabled_tc bitmap would be 0x00001001; the driver
3803 * will set the numtc for netdev as 2 that will be
3804 * referenced by the netdev layer as TC 0 and 1.
3805 */
3806 if (vsi->tc_config.enabled_tc & (1 << i))
3807 netdev_set_tc_queue(netdev,
3808 vsi->tc_config.tc_info[i].netdev_tc,
3809 vsi->tc_config.tc_info[i].qcount,
3810 vsi->tc_config.tc_info[i].qoffset);
3811 }
3812
3813 /* Assign UP2TC map for the VSI */
3814 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3815 /* Get the actual TC# for the UP */
3816 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
3817 /* Get the mapped netdev TC# for the UP */
3818 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
3819 netdev_set_prio_tc_map(netdev, i, netdev_tc);
3820 }
3821}
3822
3823/**
3824 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
3825 * @vsi: the VSI being configured
3826 * @ctxt: the ctxt buffer returned from AQ VSI update param command
3827 **/
3828static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
3829 struct i40e_vsi_context *ctxt)
3830{
3831 /* copy just the sections touched not the entire info
3832 * since not all sections are valid as returned by
3833 * update vsi params
3834 */
3835 vsi->info.mapping_flags = ctxt->info.mapping_flags;
3836 memcpy(&vsi->info.queue_mapping,
3837 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
3838 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
3839 sizeof(vsi->info.tc_mapping));
3840}
3841
3842/**
3843 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
3844 * @vsi: VSI to be configured
3845 * @enabled_tc: TC bitmap
3846 *
3847 * This configures a particular VSI for TCs that are mapped to the
3848 * given TC bitmap. It uses default bandwidth share for TCs across
3849 * VSIs to configure TC for a particular VSI.
3850 *
3851 * NOTE:
3852 * It is expected that the VSI queues have been quisced before calling
3853 * this function.
3854 **/
3855static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3856{
3857 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
3858 struct i40e_vsi_context ctxt;
3859 int ret = 0;
3860 int i;
3861
3862 /* Check if enabled_tc is same as existing or new TCs */
3863 if (vsi->tc_config.enabled_tc == enabled_tc)
3864 return ret;
3865
3866 /* Enable ETS TCs with equal BW Share for now across all VSIs */
3867 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3868 if (enabled_tc & (1 << i))
3869 bw_share[i] = 1;
3870 }
3871
3872 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
3873 if (ret) {
3874 dev_info(&vsi->back->pdev->dev,
3875 "Failed configuring TC map %d for VSI %d\n",
3876 enabled_tc, vsi->seid);
3877 goto out;
3878 }
3879
3880 /* Update Queue Pairs Mapping for currently enabled UPs */
3881 ctxt.seid = vsi->seid;
3882 ctxt.pf_num = vsi->back->hw.pf_id;
3883 ctxt.vf_num = 0;
3884 ctxt.uplink_seid = vsi->uplink_seid;
3885 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
3886 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
3887
3888 /* Update the VSI after updating the VSI queue-mapping information */
3889 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3890 if (ret) {
3891 dev_info(&vsi->back->pdev->dev,
3892 "update vsi failed, aq_err=%d\n",
3893 vsi->back->hw.aq.asq_last_status);
3894 goto out;
3895 }
3896 /* update the local VSI info with updated queue map */
3897 i40e_vsi_update_queue_map(vsi, &ctxt);
3898 vsi->info.valid_sections = 0;
3899
3900 /* Update current VSI BW information */
3901 ret = i40e_vsi_get_bw_info(vsi);
3902 if (ret) {
3903 dev_info(&vsi->back->pdev->dev,
3904 "Failed updating vsi bw info, aq_err=%d\n",
3905 vsi->back->hw.aq.asq_last_status);
3906 goto out;
3907 }
3908
3909 /* Update the netdev TC setup */
3910 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
3911out:
3912 return ret;
3913}
3914
3915/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08003916 * i40e_veb_config_tc - Configure TCs for given VEB
3917 * @veb: given VEB
3918 * @enabled_tc: TC bitmap
3919 *
3920 * Configures given TC bitmap for VEB (switching) element
3921 **/
3922int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
3923{
3924 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
3925 struct i40e_pf *pf = veb->pf;
3926 int ret = 0;
3927 int i;
3928
3929 /* No TCs or already enabled TCs just return */
3930 if (!enabled_tc || veb->enabled_tc == enabled_tc)
3931 return ret;
3932
3933 bw_data.tc_valid_bits = enabled_tc;
3934 /* bw_data.absolute_credits is not set (relative) */
3935
3936 /* Enable ETS TCs with equal BW Share for now */
3937 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3938 if (enabled_tc & (1 << i))
3939 bw_data.tc_bw_share_credits[i] = 1;
3940 }
3941
3942 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
3943 &bw_data, NULL);
3944 if (ret) {
3945 dev_info(&pf->pdev->dev,
3946 "veb bw config failed, aq_err=%d\n",
3947 pf->hw.aq.asq_last_status);
3948 goto out;
3949 }
3950
3951 /* Update the BW information */
3952 ret = i40e_veb_get_bw_info(veb);
3953 if (ret) {
3954 dev_info(&pf->pdev->dev,
3955 "Failed getting veb bw config, aq_err=%d\n",
3956 pf->hw.aq.asq_last_status);
3957 }
3958
3959out:
3960 return ret;
3961}
3962
3963#ifdef CONFIG_I40E_DCB
3964/**
3965 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
3966 * @pf: PF struct
3967 *
3968 * Reconfigure VEB/VSIs on a given PF; it is assumed that
3969 * the caller would've quiesce all the VSIs before calling
3970 * this function
3971 **/
3972static void i40e_dcb_reconfigure(struct i40e_pf *pf)
3973{
3974 u8 tc_map = 0;
3975 int ret;
3976 u8 v;
3977
3978 /* Enable the TCs available on PF to all VEBs */
3979 tc_map = i40e_pf_get_tc_map(pf);
3980 for (v = 0; v < I40E_MAX_VEB; v++) {
3981 if (!pf->veb[v])
3982 continue;
3983 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
3984 if (ret) {
3985 dev_info(&pf->pdev->dev,
3986 "Failed configuring TC for VEB seid=%d\n",
3987 pf->veb[v]->seid);
3988 /* Will try to configure as many components */
3989 }
3990 }
3991
3992 /* Update each VSI */
3993 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
3994 if (!pf->vsi[v])
3995 continue;
3996
3997 /* - Enable all TCs for the LAN VSI
3998 * - For all others keep them at TC0 for now
3999 */
4000 if (v == pf->lan_vsi)
4001 tc_map = i40e_pf_get_tc_map(pf);
4002 else
4003 tc_map = i40e_pf_get_default_tc(pf);
4004
4005 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
4006 if (ret) {
4007 dev_info(&pf->pdev->dev,
4008 "Failed configuring TC for VSI seid=%d\n",
4009 pf->vsi[v]->seid);
4010 /* Will try to configure as many components */
4011 } else {
4012 if (pf->vsi[v]->netdev)
4013 i40e_dcbnl_set_all(pf->vsi[v]);
4014 }
4015 }
4016}
4017
4018/**
4019 * i40e_init_pf_dcb - Initialize DCB configuration
4020 * @pf: PF being configured
4021 *
4022 * Query the current DCB configuration and cache it
4023 * in the hardware structure
4024 **/
4025static int i40e_init_pf_dcb(struct i40e_pf *pf)
4026{
4027 struct i40e_hw *hw = &pf->hw;
4028 int err = 0;
4029
4030 if (pf->hw.func_caps.npar_enable)
4031 goto out;
4032
4033 /* Get the initial DCB configuration */
4034 err = i40e_init_dcb(hw);
4035 if (!err) {
4036 /* Device/Function is not DCBX capable */
4037 if ((!hw->func_caps.dcb) ||
4038 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
4039 dev_info(&pf->pdev->dev,
4040 "DCBX offload is not supported or is disabled for this PF.\n");
4041
4042 if (pf->flags & I40E_FLAG_MFP_ENABLED)
4043 goto out;
4044
4045 } else {
4046 /* When status is not DISABLED then DCBX in FW */
4047 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
4048 DCB_CAP_DCBX_VER_IEEE;
4049 pf->flags |= I40E_FLAG_DCB_ENABLED;
4050 }
4051 }
4052
4053out:
4054 return err;
4055}
4056#endif /* CONFIG_I40E_DCB */
4057
4058/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004059 * i40e_up_complete - Finish the last steps of bringing up a connection
4060 * @vsi: the VSI being configured
4061 **/
4062static int i40e_up_complete(struct i40e_vsi *vsi)
4063{
4064 struct i40e_pf *pf = vsi->back;
4065 int err;
4066
4067 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4068 i40e_vsi_configure_msix(vsi);
4069 else
4070 i40e_configure_msi_and_legacy(vsi);
4071
4072 /* start rings */
4073 err = i40e_vsi_control_rings(vsi, true);
4074 if (err)
4075 return err;
4076
4077 clear_bit(__I40E_DOWN, &vsi->state);
4078 i40e_napi_enable_all(vsi);
4079 i40e_vsi_enable_irq(vsi);
4080
4081 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
4082 (vsi->netdev)) {
Anjali Singhai6d779b42013-09-28 06:00:02 +00004083 netdev_info(vsi->netdev, "NIC Link is Up\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004084 netif_tx_start_all_queues(vsi->netdev);
4085 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00004086 } else if (vsi->netdev) {
4087 netdev_info(vsi->netdev, "NIC Link is Down\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004088 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00004089
4090 /* replay FDIR SB filters */
4091 if (vsi->type == I40E_VSI_FDIR)
4092 i40e_fdir_filter_restore(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004093 i40e_service_event_schedule(pf);
4094
4095 return 0;
4096}
4097
4098/**
4099 * i40e_vsi_reinit_locked - Reset the VSI
4100 * @vsi: the VSI being configured
4101 *
4102 * Rebuild the ring structs after some configuration
4103 * has changed, e.g. MTU size.
4104 **/
4105static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
4106{
4107 struct i40e_pf *pf = vsi->back;
4108
4109 WARN_ON(in_interrupt());
4110 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
4111 usleep_range(1000, 2000);
4112 i40e_down(vsi);
4113
4114 /* Give a VF some time to respond to the reset. The
4115 * two second wait is based upon the watchdog cycle in
4116 * the VF driver.
4117 */
4118 if (vsi->type == I40E_VSI_SRIOV)
4119 msleep(2000);
4120 i40e_up(vsi);
4121 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
4122}
4123
4124/**
4125 * i40e_up - Bring the connection back up after being down
4126 * @vsi: the VSI being configured
4127 **/
4128int i40e_up(struct i40e_vsi *vsi)
4129{
4130 int err;
4131
4132 err = i40e_vsi_configure(vsi);
4133 if (!err)
4134 err = i40e_up_complete(vsi);
4135
4136 return err;
4137}
4138
4139/**
4140 * i40e_down - Shutdown the connection processing
4141 * @vsi: the VSI being stopped
4142 **/
4143void i40e_down(struct i40e_vsi *vsi)
4144{
4145 int i;
4146
4147 /* It is assumed that the caller of this function
4148 * sets the vsi->state __I40E_DOWN bit.
4149 */
4150 if (vsi->netdev) {
4151 netif_carrier_off(vsi->netdev);
4152 netif_tx_disable(vsi->netdev);
4153 }
4154 i40e_vsi_disable_irq(vsi);
4155 i40e_vsi_control_rings(vsi, false);
4156 i40e_napi_disable_all(vsi);
4157
4158 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004159 i40e_clean_tx_ring(vsi->tx_rings[i]);
4160 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004161 }
4162}
4163
4164/**
4165 * i40e_setup_tc - configure multiple traffic classes
4166 * @netdev: net device to configure
4167 * @tc: number of traffic classes to enable
4168 **/
4169static int i40e_setup_tc(struct net_device *netdev, u8 tc)
4170{
4171 struct i40e_netdev_priv *np = netdev_priv(netdev);
4172 struct i40e_vsi *vsi = np->vsi;
4173 struct i40e_pf *pf = vsi->back;
4174 u8 enabled_tc = 0;
4175 int ret = -EINVAL;
4176 int i;
4177
4178 /* Check if DCB enabled to continue */
4179 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
4180 netdev_info(netdev, "DCB is not enabled for adapter\n");
4181 goto exit;
4182 }
4183
4184 /* Check if MFP enabled */
4185 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
4186 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
4187 goto exit;
4188 }
4189
4190 /* Check whether tc count is within enabled limit */
4191 if (tc > i40e_pf_get_num_tc(pf)) {
4192 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
4193 goto exit;
4194 }
4195
4196 /* Generate TC map for number of tc requested */
4197 for (i = 0; i < tc; i++)
4198 enabled_tc |= (1 << i);
4199
4200 /* Requesting same TC configuration as already enabled */
4201 if (enabled_tc == vsi->tc_config.enabled_tc)
4202 return 0;
4203
4204 /* Quiesce VSI queues */
4205 i40e_quiesce_vsi(vsi);
4206
4207 /* Configure VSI for enabled TCs */
4208 ret = i40e_vsi_config_tc(vsi, enabled_tc);
4209 if (ret) {
4210 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
4211 vsi->seid);
4212 goto exit;
4213 }
4214
4215 /* Unquiesce VSI */
4216 i40e_unquiesce_vsi(vsi);
4217
4218exit:
4219 return ret;
4220}
4221
4222/**
4223 * i40e_open - Called when a network interface is made active
4224 * @netdev: network interface device structure
4225 *
4226 * The open entry point is called when a network interface is made
4227 * active by the system (IFF_UP). At this point all resources needed
4228 * for transmit and receive operations are allocated, the interrupt
4229 * handler is registered with the OS, the netdev watchdog subtask is
4230 * enabled, and the stack is notified that the interface is ready.
4231 *
4232 * Returns 0 on success, negative value on failure
4233 **/
4234static int i40e_open(struct net_device *netdev)
4235{
4236 struct i40e_netdev_priv *np = netdev_priv(netdev);
4237 struct i40e_vsi *vsi = np->vsi;
4238 struct i40e_pf *pf = vsi->back;
4239 char int_name[IFNAMSIZ];
4240 int err;
4241
4242 /* disallow open during test */
4243 if (test_bit(__I40E_TESTING, &pf->state))
4244 return -EBUSY;
4245
4246 netif_carrier_off(netdev);
4247
4248 /* allocate descriptors */
4249 err = i40e_vsi_setup_tx_resources(vsi);
4250 if (err)
4251 goto err_setup_tx;
4252 err = i40e_vsi_setup_rx_resources(vsi);
4253 if (err)
4254 goto err_setup_rx;
4255
4256 err = i40e_vsi_configure(vsi);
4257 if (err)
4258 goto err_setup_rx;
4259
4260 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
4261 dev_driver_string(&pf->pdev->dev), netdev->name);
4262 err = i40e_vsi_request_irq(vsi, int_name);
4263 if (err)
4264 goto err_setup_rx;
4265
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004266 /* Notify the stack of the actual queue counts. */
Neerav Parikhd7397642013-11-28 06:39:37 +00004267 err = netif_set_real_num_tx_queues(netdev, vsi->num_queue_pairs);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004268 if (err)
4269 goto err_set_queues;
4270
Neerav Parikhd7397642013-11-28 06:39:37 +00004271 err = netif_set_real_num_rx_queues(netdev, vsi->num_queue_pairs);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004272 if (err)
4273 goto err_set_queues;
4274
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004275 err = i40e_up_complete(vsi);
4276 if (err)
4277 goto err_up_complete;
4278
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00004279#ifdef CONFIG_I40E_VXLAN
4280 vxlan_get_rx_port(netdev);
4281#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004282
4283 return 0;
4284
4285err_up_complete:
4286 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00004287err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004288 i40e_vsi_free_irq(vsi);
4289err_setup_rx:
4290 i40e_vsi_free_rx_resources(vsi);
4291err_setup_tx:
4292 i40e_vsi_free_tx_resources(vsi);
4293 if (vsi == pf->vsi[pf->lan_vsi])
4294 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
4295
4296 return err;
4297}
4298
4299/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00004300 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
4301 * @pf: Pointer to pf
4302 *
4303 * This function destroys the hlist where all the Flow Director
4304 * filters were saved.
4305 **/
4306static void i40e_fdir_filter_exit(struct i40e_pf *pf)
4307{
4308 struct i40e_fdir_filter *filter;
4309 struct hlist_node *node2;
4310
4311 hlist_for_each_entry_safe(filter, node2,
4312 &pf->fdir_filter_list, fdir_node) {
4313 hlist_del(&filter->fdir_node);
4314 kfree(filter);
4315 }
4316 pf->fdir_pf_active_filters = 0;
4317}
4318
4319/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004320 * i40e_close - Disables a network interface
4321 * @netdev: network interface device structure
4322 *
4323 * The close entry point is called when an interface is de-activated
4324 * by the OS. The hardware is still under the driver's control, but
4325 * this netdev interface is disabled.
4326 *
4327 * Returns 0, this is not allowed to fail
4328 **/
4329static int i40e_close(struct net_device *netdev)
4330{
4331 struct i40e_netdev_priv *np = netdev_priv(netdev);
4332 struct i40e_vsi *vsi = np->vsi;
4333
4334 if (test_and_set_bit(__I40E_DOWN, &vsi->state))
4335 return 0;
4336
4337 i40e_down(vsi);
4338 i40e_vsi_free_irq(vsi);
4339
4340 i40e_vsi_free_tx_resources(vsi);
4341 i40e_vsi_free_rx_resources(vsi);
4342
4343 return 0;
4344}
4345
4346/**
4347 * i40e_do_reset - Start a PF or Core Reset sequence
4348 * @pf: board private structure
4349 * @reset_flags: which reset is requested
4350 *
4351 * The essential difference in resets is that the PF Reset
4352 * doesn't clear the packet buffers, doesn't reset the PE
4353 * firmware, and doesn't bother the other PFs on the chip.
4354 **/
4355void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
4356{
4357 u32 val;
4358
4359 WARN_ON(in_interrupt());
4360
4361 /* do the biggest reset indicated */
4362 if (reset_flags & (1 << __I40E_GLOBAL_RESET_REQUESTED)) {
4363
4364 /* Request a Global Reset
4365 *
4366 * This will start the chip's countdown to the actual full
4367 * chip reset event, and a warning interrupt to be sent
4368 * to all PFs, including the requestor. Our handler
4369 * for the warning interrupt will deal with the shutdown
4370 * and recovery of the switch setup.
4371 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004372 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004373 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4374 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
4375 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4376
4377 } else if (reset_flags & (1 << __I40E_CORE_RESET_REQUESTED)) {
4378
4379 /* Request a Core Reset
4380 *
4381 * Same as Global Reset, except does *not* include the MAC/PHY
4382 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004383 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004384 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4385 val |= I40E_GLGEN_RTRIG_CORER_MASK;
4386 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4387 i40e_flush(&pf->hw);
4388
Shannon Nelson7823fe32013-11-16 10:00:45 +00004389 } else if (reset_flags & (1 << __I40E_EMP_RESET_REQUESTED)) {
4390
4391 /* Request a Firmware Reset
4392 *
4393 * Same as Global reset, plus restarting the
4394 * embedded firmware engine.
4395 */
4396 /* enable EMP Reset */
4397 val = rd32(&pf->hw, I40E_GLGEN_RSTENA_EMP);
4398 val |= I40E_GLGEN_RSTENA_EMP_EMP_RST_ENA_MASK;
4399 wr32(&pf->hw, I40E_GLGEN_RSTENA_EMP, val);
4400
4401 /* force the reset */
4402 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4403 val |= I40E_GLGEN_RTRIG_EMPFWR_MASK;
4404 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4405 i40e_flush(&pf->hw);
4406
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004407 } else if (reset_flags & (1 << __I40E_PF_RESET_REQUESTED)) {
4408
4409 /* Request a PF Reset
4410 *
4411 * Resets only the PF-specific registers
4412 *
4413 * This goes directly to the tear-down and rebuild of
4414 * the switch, since we need to do all the recovery as
4415 * for the Core Reset.
4416 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004417 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004418 i40e_handle_reset_warning(pf);
4419
4420 } else if (reset_flags & (1 << __I40E_REINIT_REQUESTED)) {
4421 int v;
4422
4423 /* Find the VSI(s) that requested a re-init */
4424 dev_info(&pf->pdev->dev,
4425 "VSI reinit requested\n");
4426 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4427 struct i40e_vsi *vsi = pf->vsi[v];
4428 if (vsi != NULL &&
4429 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
4430 i40e_vsi_reinit_locked(pf->vsi[v]);
4431 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
4432 }
4433 }
4434
4435 /* no further action needed, so return now */
4436 return;
4437 } else {
4438 dev_info(&pf->pdev->dev,
4439 "bad reset request 0x%08x\n", reset_flags);
4440 return;
4441 }
4442}
4443
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004444#ifdef CONFIG_I40E_DCB
4445/**
4446 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
4447 * @pf: board private structure
4448 * @old_cfg: current DCB config
4449 * @new_cfg: new DCB config
4450 **/
4451bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
4452 struct i40e_dcbx_config *old_cfg,
4453 struct i40e_dcbx_config *new_cfg)
4454{
4455 bool need_reconfig = false;
4456
4457 /* Check if ETS configuration has changed */
4458 if (memcmp(&new_cfg->etscfg,
4459 &old_cfg->etscfg,
4460 sizeof(new_cfg->etscfg))) {
4461 /* If Priority Table has changed reconfig is needed */
4462 if (memcmp(&new_cfg->etscfg.prioritytable,
4463 &old_cfg->etscfg.prioritytable,
4464 sizeof(new_cfg->etscfg.prioritytable))) {
4465 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004466 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004467 }
4468
4469 if (memcmp(&new_cfg->etscfg.tcbwtable,
4470 &old_cfg->etscfg.tcbwtable,
4471 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004472 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004473
4474 if (memcmp(&new_cfg->etscfg.tsatable,
4475 &old_cfg->etscfg.tsatable,
4476 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004477 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004478 }
4479
4480 /* Check if PFC configuration has changed */
4481 if (memcmp(&new_cfg->pfc,
4482 &old_cfg->pfc,
4483 sizeof(new_cfg->pfc))) {
4484 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004485 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004486 }
4487
4488 /* Check if APP Table has changed */
4489 if (memcmp(&new_cfg->app,
4490 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05004491 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004492 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004493 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05004494 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004495
4496 return need_reconfig;
4497}
4498
4499/**
4500 * i40e_handle_lldp_event - Handle LLDP Change MIB event
4501 * @pf: board private structure
4502 * @e: event info posted on ARQ
4503 **/
4504static int i40e_handle_lldp_event(struct i40e_pf *pf,
4505 struct i40e_arq_event_info *e)
4506{
4507 struct i40e_aqc_lldp_get_mib *mib =
4508 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
4509 struct i40e_hw *hw = &pf->hw;
4510 struct i40e_dcbx_config *dcbx_cfg = &hw->local_dcbx_config;
4511 struct i40e_dcbx_config tmp_dcbx_cfg;
4512 bool need_reconfig = false;
4513 int ret = 0;
4514 u8 type;
4515
4516 /* Ignore if event is not for Nearest Bridge */
4517 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
4518 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
4519 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
4520 return ret;
4521
4522 /* Check MIB Type and return if event for Remote MIB update */
4523 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
4524 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
4525 /* Update the remote cached instance and return */
4526 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
4527 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
4528 &hw->remote_dcbx_config);
4529 goto exit;
4530 }
4531
4532 /* Convert/store the DCBX data from LLDPDU temporarily */
4533 memset(&tmp_dcbx_cfg, 0, sizeof(tmp_dcbx_cfg));
4534 ret = i40e_lldp_to_dcb_config(e->msg_buf, &tmp_dcbx_cfg);
4535 if (ret) {
4536 /* Error in LLDPDU parsing return */
4537 dev_info(&pf->pdev->dev, "Failed parsing LLDPDU from event buffer\n");
4538 goto exit;
4539 }
4540
4541 /* No change detected in DCBX configs */
4542 if (!memcmp(&tmp_dcbx_cfg, dcbx_cfg, sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004543 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004544 goto exit;
4545 }
4546
4547 need_reconfig = i40e_dcb_need_reconfig(pf, dcbx_cfg, &tmp_dcbx_cfg);
4548
4549 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg);
4550
4551 /* Overwrite the new configuration */
4552 *dcbx_cfg = tmp_dcbx_cfg;
4553
4554 if (!need_reconfig)
4555 goto exit;
4556
4557 /* Reconfiguration needed quiesce all VSIs */
4558 i40e_pf_quiesce_all_vsi(pf);
4559
4560 /* Changes in configuration update VEB/VSI */
4561 i40e_dcb_reconfigure(pf);
4562
4563 i40e_pf_unquiesce_all_vsi(pf);
4564exit:
4565 return ret;
4566}
4567#endif /* CONFIG_I40E_DCB */
4568
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004569/**
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004570 * i40e_do_reset_safe - Protected reset path for userland calls.
4571 * @pf: board private structure
4572 * @reset_flags: which reset is requested
4573 *
4574 **/
4575void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
4576{
4577 rtnl_lock();
4578 i40e_do_reset(pf, reset_flags);
4579 rtnl_unlock();
4580}
4581
4582/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004583 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
4584 * @pf: board private structure
4585 * @e: event info posted on ARQ
4586 *
4587 * Handler for LAN Queue Overflow Event generated by the firmware for PF
4588 * and VF queues
4589 **/
4590static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
4591 struct i40e_arq_event_info *e)
4592{
4593 struct i40e_aqc_lan_overflow *data =
4594 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
4595 u32 queue = le32_to_cpu(data->prtdcb_rupto);
4596 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
4597 struct i40e_hw *hw = &pf->hw;
4598 struct i40e_vf *vf;
4599 u16 vf_id;
4600
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004601 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
4602 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004603
4604 /* Queue belongs to VF, find the VF and issue VF reset */
4605 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
4606 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
4607 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
4608 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
4609 vf_id -= hw->func_caps.vf_base_id;
4610 vf = &pf->vf[vf_id];
4611 i40e_vc_notify_vf_reset(vf);
4612 /* Allow VF to process pending reset notification */
4613 msleep(20);
4614 i40e_reset_vf(vf, false);
4615 }
4616}
4617
4618/**
4619 * i40e_service_event_complete - Finish up the service event
4620 * @pf: board private structure
4621 **/
4622static void i40e_service_event_complete(struct i40e_pf *pf)
4623{
4624 BUG_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
4625
4626 /* flush memory to make sure state is correct before next watchog */
4627 smp_mb__before_clear_bit();
4628 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
4629}
4630
4631/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004632 * i40e_get_current_fd_count - Get the count of FD filters programmed in the HW
4633 * @pf: board private structure
4634 **/
4635int i40e_get_current_fd_count(struct i40e_pf *pf)
4636{
4637 int val, fcnt_prog;
4638 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
4639 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
4640 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
4641 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
4642 return fcnt_prog;
4643}
4644
4645/**
4646 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
4647 * @pf: board private structure
4648 **/
4649void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
4650{
4651 u32 fcnt_prog, fcnt_avail;
4652
4653 /* Check if, FD SB or ATR was auto disabled and if there is enough room
4654 * to re-enable
4655 */
4656 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4657 (pf->flags & I40E_FLAG_FD_SB_ENABLED))
4658 return;
4659 fcnt_prog = i40e_get_current_fd_count(pf);
4660 fcnt_avail = pf->hw.fdir_shared_filter_count +
4661 pf->fdir_pf_filter_count;
4662 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) {
4663 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
4664 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
4665 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
4666 dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
4667 }
4668 }
4669 /* Wait for some more space to be available to turn on ATR */
4670 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
4671 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4672 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
4673 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
4674 dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table now\n");
4675 }
4676 }
4677}
4678
4679/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004680 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
4681 * @pf: board private structure
4682 **/
4683static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
4684{
4685 if (!(pf->flags & I40E_FLAG_FDIR_REQUIRES_REINIT))
4686 return;
4687
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004688 /* if interface is down do nothing */
4689 if (test_bit(__I40E_DOWN, &pf->state))
4690 return;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00004691 i40e_fdir_check_and_reenable(pf);
4692
4693 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
4694 (pf->flags & I40E_FLAG_FD_SB_ENABLED))
4695 pf->flags &= ~I40E_FLAG_FDIR_REQUIRES_REINIT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004696}
4697
4698/**
4699 * i40e_vsi_link_event - notify VSI of a link event
4700 * @vsi: vsi to be notified
4701 * @link_up: link up or down
4702 **/
4703static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
4704{
4705 if (!vsi)
4706 return;
4707
4708 switch (vsi->type) {
4709 case I40E_VSI_MAIN:
4710 if (!vsi->netdev || !vsi->netdev_registered)
4711 break;
4712
4713 if (link_up) {
4714 netif_carrier_on(vsi->netdev);
4715 netif_tx_wake_all_queues(vsi->netdev);
4716 } else {
4717 netif_carrier_off(vsi->netdev);
4718 netif_tx_stop_all_queues(vsi->netdev);
4719 }
4720 break;
4721
4722 case I40E_VSI_SRIOV:
4723 break;
4724
4725 case I40E_VSI_VMDQ2:
4726 case I40E_VSI_CTRL:
4727 case I40E_VSI_MIRROR:
4728 default:
4729 /* there is no notification for other VSIs */
4730 break;
4731 }
4732}
4733
4734/**
4735 * i40e_veb_link_event - notify elements on the veb of a link event
4736 * @veb: veb to be notified
4737 * @link_up: link up or down
4738 **/
4739static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
4740{
4741 struct i40e_pf *pf;
4742 int i;
4743
4744 if (!veb || !veb->pf)
4745 return;
4746 pf = veb->pf;
4747
4748 /* depth first... */
4749 for (i = 0; i < I40E_MAX_VEB; i++)
4750 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
4751 i40e_veb_link_event(pf->veb[i], link_up);
4752
4753 /* ... now the local VSIs */
4754 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
4755 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
4756 i40e_vsi_link_event(pf->vsi[i], link_up);
4757}
4758
4759/**
4760 * i40e_link_event - Update netif_carrier status
4761 * @pf: board private structure
4762 **/
4763static void i40e_link_event(struct i40e_pf *pf)
4764{
4765 bool new_link, old_link;
4766
4767 new_link = (pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP);
4768 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
4769
4770 if (new_link == old_link)
4771 return;
4772
Anjali Singhai6d779b42013-09-28 06:00:02 +00004773 if (!test_bit(__I40E_DOWN, &pf->vsi[pf->lan_vsi]->state))
4774 netdev_info(pf->vsi[pf->lan_vsi]->netdev,
4775 "NIC Link is %s\n", (new_link ? "Up" : "Down"));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004776
4777 /* Notify the base of the switch tree connected to
4778 * the link. Floating VEBs are not notified.
4779 */
4780 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
4781 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
4782 else
4783 i40e_vsi_link_event(pf->vsi[pf->lan_vsi], new_link);
4784
4785 if (pf->vf)
4786 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00004787
4788 if (pf->flags & I40E_FLAG_PTP)
4789 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004790}
4791
4792/**
4793 * i40e_check_hang_subtask - Check for hung queues and dropped interrupts
4794 * @pf: board private structure
4795 *
4796 * Set the per-queue flags to request a check for stuck queues in the irq
4797 * clean functions, then force interrupts to be sure the irq clean is called.
4798 **/
4799static void i40e_check_hang_subtask(struct i40e_pf *pf)
4800{
4801 int i, v;
4802
4803 /* If we're down or resetting, just bail */
4804 if (test_bit(__I40E_CONFIG_BUSY, &pf->state))
4805 return;
4806
4807 /* for each VSI/netdev
4808 * for each Tx queue
4809 * set the check flag
4810 * for each q_vector
4811 * force an interrupt
4812 */
4813 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4814 struct i40e_vsi *vsi = pf->vsi[v];
4815 int armed = 0;
4816
4817 if (!pf->vsi[v] ||
4818 test_bit(__I40E_DOWN, &vsi->state) ||
4819 (vsi->netdev && !netif_carrier_ok(vsi->netdev)))
4820 continue;
4821
4822 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004823 set_check_for_tx_hang(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004824 if (test_bit(__I40E_HANG_CHECK_ARMED,
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00004825 &vsi->tx_rings[i]->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004826 armed++;
4827 }
4828
4829 if (armed) {
4830 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
4831 wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0,
4832 (I40E_PFINT_DYN_CTL0_INTENA_MASK |
4833 I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK));
4834 } else {
4835 u16 vec = vsi->base_vector - 1;
4836 u32 val = (I40E_PFINT_DYN_CTLN_INTENA_MASK |
4837 I40E_PFINT_DYN_CTLN_SWINT_TRIG_MASK);
4838 for (i = 0; i < vsi->num_q_vectors; i++, vec++)
4839 wr32(&vsi->back->hw,
4840 I40E_PFINT_DYN_CTLN(vec), val);
4841 }
4842 i40e_flush(&vsi->back->hw);
4843 }
4844 }
4845}
4846
4847/**
4848 * i40e_watchdog_subtask - Check and bring link up
4849 * @pf: board private structure
4850 **/
4851static void i40e_watchdog_subtask(struct i40e_pf *pf)
4852{
4853 int i;
4854
4855 /* if interface is down do nothing */
4856 if (test_bit(__I40E_DOWN, &pf->state) ||
4857 test_bit(__I40E_CONFIG_BUSY, &pf->state))
4858 return;
4859
4860 /* Update the stats for active netdevs so the network stack
4861 * can look at updated numbers whenever it cares to
4862 */
4863 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
4864 if (pf->vsi[i] && pf->vsi[i]->netdev)
4865 i40e_update_stats(pf->vsi[i]);
4866
4867 /* Update the stats for the active switching components */
4868 for (i = 0; i < I40E_MAX_VEB; i++)
4869 if (pf->veb[i])
4870 i40e_update_veb_stats(pf->veb[i]);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00004871
4872 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004873}
4874
4875/**
4876 * i40e_reset_subtask - Set up for resetting the device and driver
4877 * @pf: board private structure
4878 **/
4879static void i40e_reset_subtask(struct i40e_pf *pf)
4880{
4881 u32 reset_flags = 0;
4882
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004883 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004884 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
4885 reset_flags |= (1 << __I40E_REINIT_REQUESTED);
4886 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
4887 }
4888 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
4889 reset_flags |= (1 << __I40E_PF_RESET_REQUESTED);
4890 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
4891 }
4892 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
4893 reset_flags |= (1 << __I40E_CORE_RESET_REQUESTED);
4894 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
4895 }
4896 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
4897 reset_flags |= (1 << __I40E_GLOBAL_RESET_REQUESTED);
4898 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
4899 }
4900
4901 /* If there's a recovery already waiting, it takes
4902 * precedence before starting a new reset sequence.
4903 */
4904 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
4905 i40e_handle_reset_warning(pf);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004906 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004907 }
4908
4909 /* If we're already down or resetting, just bail */
4910 if (reset_flags &&
4911 !test_bit(__I40E_DOWN, &pf->state) &&
4912 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
4913 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00004914
4915unlock:
4916 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004917}
4918
4919/**
4920 * i40e_handle_link_event - Handle link event
4921 * @pf: board private structure
4922 * @e: event info posted on ARQ
4923 **/
4924static void i40e_handle_link_event(struct i40e_pf *pf,
4925 struct i40e_arq_event_info *e)
4926{
4927 struct i40e_hw *hw = &pf->hw;
4928 struct i40e_aqc_get_link_status *status =
4929 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
4930 struct i40e_link_status *hw_link_info = &hw->phy.link_info;
4931
4932 /* save off old link status information */
4933 memcpy(&pf->hw.phy.link_info_old, hw_link_info,
4934 sizeof(pf->hw.phy.link_info_old));
4935
4936 /* update link status */
4937 hw_link_info->phy_type = (enum i40e_aq_phy_type)status->phy_type;
4938 hw_link_info->link_speed = (enum i40e_aq_link_speed)status->link_speed;
4939 hw_link_info->link_info = status->link_info;
4940 hw_link_info->an_info = status->an_info;
4941 hw_link_info->ext_info = status->ext_info;
4942 hw_link_info->lse_enable =
4943 le16_to_cpu(status->command_flags) &
4944 I40E_AQ_LSE_ENABLE;
4945
4946 /* process the event */
4947 i40e_link_event(pf);
4948
4949 /* Do a new status request to re-enable LSE reporting
4950 * and load new status information into the hw struct,
4951 * then see if the status changed while processing the
4952 * initial event.
4953 */
4954 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
4955 i40e_link_event(pf);
4956}
4957
4958/**
4959 * i40e_clean_adminq_subtask - Clean the AdminQ rings
4960 * @pf: board private structure
4961 **/
4962static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
4963{
4964 struct i40e_arq_event_info event;
4965 struct i40e_hw *hw = &pf->hw;
4966 u16 pending, i = 0;
4967 i40e_status ret;
4968 u16 opcode;
4969 u32 val;
4970
4971 if (!test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state))
4972 return;
4973
Mitch Williams3197ce22013-11-28 06:39:39 +00004974 event.msg_size = I40E_MAX_AQ_BUF_SIZE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004975 event.msg_buf = kzalloc(event.msg_size, GFP_KERNEL);
4976 if (!event.msg_buf)
4977 return;
4978
4979 do {
Mitch Williams2f019122013-11-28 06:39:33 +00004980 event.msg_size = I40E_MAX_AQ_BUF_SIZE; /* reinit each time */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004981 ret = i40e_clean_arq_element(hw, &event, &pending);
4982 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
4983 dev_info(&pf->pdev->dev, "No ARQ event found\n");
4984 break;
4985 } else if (ret) {
4986 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
4987 break;
4988 }
4989
4990 opcode = le16_to_cpu(event.desc.opcode);
4991 switch (opcode) {
4992
4993 case i40e_aqc_opc_get_link_status:
4994 i40e_handle_link_event(pf, &event);
4995 break;
4996 case i40e_aqc_opc_send_msg_to_pf:
4997 ret = i40e_vc_process_vf_msg(pf,
4998 le16_to_cpu(event.desc.retval),
4999 le32_to_cpu(event.desc.cookie_high),
5000 le32_to_cpu(event.desc.cookie_low),
5001 event.msg_buf,
5002 event.msg_size);
5003 break;
5004 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005005 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005006#ifdef CONFIG_I40E_DCB
5007 rtnl_lock();
5008 ret = i40e_handle_lldp_event(pf, &event);
5009 rtnl_unlock();
5010#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005011 break;
5012 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005013 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005014 i40e_handle_lan_overflow_event(pf, &event);
5015 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00005016 case i40e_aqc_opc_send_msg_to_peer:
5017 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
5018 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005019 default:
5020 dev_info(&pf->pdev->dev,
Shannon Nelson0467bc92013-12-18 13:45:58 +00005021 "ARQ Error: Unknown event 0x%04x received\n",
5022 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005023 break;
5024 }
5025 } while (pending && (i++ < pf->adminq_work_limit));
5026
5027 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
5028 /* re-enable Admin queue interrupt cause */
5029 val = rd32(hw, I40E_PFINT_ICR0_ENA);
5030 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
5031 wr32(hw, I40E_PFINT_ICR0_ENA, val);
5032 i40e_flush(hw);
5033
5034 kfree(event.msg_buf);
5035}
5036
5037/**
5038 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
5039 * @veb: pointer to the VEB instance
5040 *
5041 * This is a recursive function that first builds the attached VSIs then
5042 * recurses in to build the next layer of VEB. We track the connections
5043 * through our own index numbers because the seid's from the HW could
5044 * change across the reset.
5045 **/
5046static int i40e_reconstitute_veb(struct i40e_veb *veb)
5047{
5048 struct i40e_vsi *ctl_vsi = NULL;
5049 struct i40e_pf *pf = veb->pf;
5050 int v, veb_idx;
5051 int ret;
5052
5053 /* build VSI that owns this VEB, temporarily attached to base VEB */
5054 for (v = 0; v < pf->hw.func_caps.num_vsis && !ctl_vsi; v++) {
5055 if (pf->vsi[v] &&
5056 pf->vsi[v]->veb_idx == veb->idx &&
5057 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
5058 ctl_vsi = pf->vsi[v];
5059 break;
5060 }
5061 }
5062 if (!ctl_vsi) {
5063 dev_info(&pf->pdev->dev,
5064 "missing owner VSI for veb_idx %d\n", veb->idx);
5065 ret = -ENOENT;
5066 goto end_reconstitute;
5067 }
5068 if (ctl_vsi != pf->vsi[pf->lan_vsi])
5069 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
5070 ret = i40e_add_vsi(ctl_vsi);
5071 if (ret) {
5072 dev_info(&pf->pdev->dev,
5073 "rebuild of owner VSI failed: %d\n", ret);
5074 goto end_reconstitute;
5075 }
5076 i40e_vsi_reset_stats(ctl_vsi);
5077
5078 /* create the VEB in the switch and move the VSI onto the VEB */
5079 ret = i40e_add_veb(veb, ctl_vsi);
5080 if (ret)
5081 goto end_reconstitute;
5082
5083 /* create the remaining VSIs attached to this VEB */
5084 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5085 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
5086 continue;
5087
5088 if (pf->vsi[v]->veb_idx == veb->idx) {
5089 struct i40e_vsi *vsi = pf->vsi[v];
5090 vsi->uplink_seid = veb->seid;
5091 ret = i40e_add_vsi(vsi);
5092 if (ret) {
5093 dev_info(&pf->pdev->dev,
5094 "rebuild of vsi_idx %d failed: %d\n",
5095 v, ret);
5096 goto end_reconstitute;
5097 }
5098 i40e_vsi_reset_stats(vsi);
5099 }
5100 }
5101
5102 /* create any VEBs attached to this VEB - RECURSION */
5103 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
5104 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
5105 pf->veb[veb_idx]->uplink_seid = veb->seid;
5106 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
5107 if (ret)
5108 break;
5109 }
5110 }
5111
5112end_reconstitute:
5113 return ret;
5114}
5115
5116/**
5117 * i40e_get_capabilities - get info about the HW
5118 * @pf: the PF struct
5119 **/
5120static int i40e_get_capabilities(struct i40e_pf *pf)
5121{
5122 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
5123 u16 data_size;
5124 int buf_len;
5125 int err;
5126
5127 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
5128 do {
5129 cap_buf = kzalloc(buf_len, GFP_KERNEL);
5130 if (!cap_buf)
5131 return -ENOMEM;
5132
5133 /* this loads the data into the hw struct for us */
5134 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
5135 &data_size,
5136 i40e_aqc_opc_list_func_capabilities,
5137 NULL);
5138 /* data loaded, buffer no longer needed */
5139 kfree(cap_buf);
5140
5141 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
5142 /* retry with a larger buffer */
5143 buf_len = data_size;
5144 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
5145 dev_info(&pf->pdev->dev,
5146 "capability discovery failed: aq=%d\n",
5147 pf->hw.aq.asq_last_status);
5148 return -ENODEV;
5149 }
5150 } while (err);
5151
Jesse Brandeburgd0b10242014-01-17 15:36:32 -08005152 /* increment MSI-X count because current FW skips one */
5153 pf->hw.func_caps.num_msix_vectors++;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00005154
Anjali Singhai Jainac71b7b2014-02-06 05:51:08 +00005155 if (((pf->hw.aq.fw_maj_ver == 2) && (pf->hw.aq.fw_min_ver < 22)) ||
5156 (pf->hw.aq.fw_maj_ver < 2)) {
5157 pf->hw.func_caps.num_msix_vectors++;
5158 pf->hw.func_caps.num_msix_vectors_vf++;
5159 }
5160
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005161 if (pf->hw.debug_mask & I40E_DEBUG_USER)
5162 dev_info(&pf->pdev->dev,
5163 "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",
5164 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
5165 pf->hw.func_caps.num_msix_vectors,
5166 pf->hw.func_caps.num_msix_vectors_vf,
5167 pf->hw.func_caps.fd_filters_guaranteed,
5168 pf->hw.func_caps.fd_filters_best_effort,
5169 pf->hw.func_caps.num_tx_qp,
5170 pf->hw.func_caps.num_vsis);
5171
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00005172#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
5173 + pf->hw.func_caps.num_vfs)
5174 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
5175 dev_info(&pf->pdev->dev,
5176 "got num_vsis %d, setting num_vsis to %d\n",
5177 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
5178 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
5179 }
5180
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005181 return 0;
5182}
5183
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005184static int i40e_vsi_clear(struct i40e_vsi *vsi);
5185
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005186/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005187 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005188 * @pf: board private structure
5189 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005190static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005191{
5192 struct i40e_vsi *vsi;
5193 bool new_vsi = false;
5194 int err, i;
5195
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005196 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005197 return;
5198
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005199 /* find existing VSI and see if it needs configuring */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005200 vsi = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005201 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
5202 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005203 vsi = pf->vsi[i];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005204 break;
5205 }
5206 }
5207
5208 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005209 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005210 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
5211 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005212 if (!vsi) {
5213 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005214 goto err_vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005215 }
5216 new_vsi = true;
5217 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005218 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005219
5220 err = i40e_vsi_setup_tx_resources(vsi);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005221 if (err)
5222 goto err_setup_tx;
5223 err = i40e_vsi_setup_rx_resources(vsi);
5224 if (err)
5225 goto err_setup_rx;
5226
5227 if (new_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005228 char int_name[IFNAMSIZ + 9];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005229 err = i40e_vsi_configure(vsi);
5230 if (err)
5231 goto err_setup_rx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005232 snprintf(int_name, sizeof(int_name) - 1, "%s-fdir",
5233 dev_driver_string(&pf->pdev->dev));
5234 err = i40e_vsi_request_irq(vsi, int_name);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005235 if (err)
5236 goto err_setup_rx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005237 err = i40e_up_complete(vsi);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005238 if (err)
5239 goto err_up_complete;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005240 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005241 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005242
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08005243 return;
5244
5245err_up_complete:
5246 i40e_down(vsi);
5247 i40e_vsi_free_irq(vsi);
5248err_setup_rx:
5249 i40e_vsi_free_rx_resources(vsi);
5250err_setup_tx:
5251 i40e_vsi_free_tx_resources(vsi);
5252err_vsi:
5253 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
5254 i40e_vsi_clear(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005255}
5256
5257/**
5258 * i40e_fdir_teardown - release the Flow Director resources
5259 * @pf: board private structure
5260 **/
5261static void i40e_fdir_teardown(struct i40e_pf *pf)
5262{
5263 int i;
5264
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005265 i40e_fdir_filter_exit(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005266 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
5267 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
5268 i40e_vsi_release(pf->vsi[i]);
5269 break;
5270 }
5271 }
5272}
5273
5274/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005275 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005276 * @pf: board private structure
5277 *
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005278 * Close up the VFs and other things in prep for pf Reset.
5279 **/
5280static int i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005281{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005282 struct i40e_hw *hw = &pf->hw;
5283 i40e_status ret;
5284 u32 v;
5285
5286 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
5287 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005288 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005289
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005290 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005291
Anjali Singhai Jain37f0be62013-11-28 06:39:46 +00005292 if (i40e_check_asq_alive(hw))
5293 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005294
5295 /* quiesce the VSIs and their queues that are not already DOWN */
5296 i40e_pf_quiesce_all_vsi(pf);
5297
5298 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5299 if (pf->vsi[v])
5300 pf->vsi[v]->seid = 0;
5301 }
5302
5303 i40e_shutdown_adminq(&pf->hw);
5304
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005305 /* call shutdown HMC */
5306 ret = i40e_shutdown_lan_hmc(hw);
5307 if (ret) {
5308 dev_info(&pf->pdev->dev, "shutdown_lan_hmc failed: %d\n", ret);
5309 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
5310 }
5311 return ret;
5312}
5313
5314/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00005315 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005316 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005317 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005318 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005319static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005320{
5321 struct i40e_driver_version dv;
5322 struct i40e_hw *hw = &pf->hw;
5323 i40e_status ret;
5324 u32 v;
5325
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005326 /* Now we wait for GRST to settle out.
5327 * We don't have to delete the VEBs or VSIs from the hw switch
5328 * because the reset will make them disappear.
5329 */
5330 ret = i40e_pf_reset(hw);
5331 if (ret)
5332 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
5333 pf->pfr_count++;
5334
5335 if (test_bit(__I40E_DOWN, &pf->state))
5336 goto end_core_reset;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005337 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005338
5339 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
5340 ret = i40e_init_adminq(&pf->hw);
5341 if (ret) {
5342 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, %d\n", ret);
5343 goto end_core_reset;
5344 }
5345
5346 ret = i40e_get_capabilities(pf);
5347 if (ret) {
5348 dev_info(&pf->pdev->dev, "i40e_get_capabilities failed, %d\n",
5349 ret);
5350 goto end_core_reset;
5351 }
5352
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005353 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
5354 hw->func_caps.num_rx_qp,
5355 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
5356 if (ret) {
5357 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
5358 goto end_core_reset;
5359 }
5360 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
5361 if (ret) {
5362 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
5363 goto end_core_reset;
5364 }
5365
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005366#ifdef CONFIG_I40E_DCB
5367 ret = i40e_init_pf_dcb(pf);
5368 if (ret) {
5369 dev_info(&pf->pdev->dev, "init_pf_dcb failed: %d\n", ret);
5370 goto end_core_reset;
5371 }
5372#endif /* CONFIG_I40E_DCB */
5373
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005374 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005375 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005376 if (ret)
5377 goto end_core_reset;
5378
5379 /* Rebuild the VSIs and VEBs that existed before reset.
5380 * They are still in our local switch element arrays, so only
5381 * need to rebuild the switch model in the HW.
5382 *
5383 * If there were VEBs but the reconstitution failed, we'll try
5384 * try to recover minimal use by getting the basic PF VSI working.
5385 */
5386 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005387 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005388 /* find the one VEB connected to the MAC, and find orphans */
5389 for (v = 0; v < I40E_MAX_VEB; v++) {
5390 if (!pf->veb[v])
5391 continue;
5392
5393 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
5394 pf->veb[v]->uplink_seid == 0) {
5395 ret = i40e_reconstitute_veb(pf->veb[v]);
5396
5397 if (!ret)
5398 continue;
5399
5400 /* If Main VEB failed, we're in deep doodoo,
5401 * so give up rebuilding the switch and set up
5402 * for minimal rebuild of PF VSI.
5403 * If orphan failed, we'll report the error
5404 * but try to keep going.
5405 */
5406 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
5407 dev_info(&pf->pdev->dev,
5408 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
5409 ret);
5410 pf->vsi[pf->lan_vsi]->uplink_seid
5411 = pf->mac_seid;
5412 break;
5413 } else if (pf->veb[v]->uplink_seid == 0) {
5414 dev_info(&pf->pdev->dev,
5415 "rebuild of orphan VEB failed: %d\n",
5416 ret);
5417 }
5418 }
5419 }
5420 }
5421
5422 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
5423 dev_info(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
5424 /* no VEB, so rebuild only the Main VSI */
5425 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
5426 if (ret) {
5427 dev_info(&pf->pdev->dev,
5428 "rebuild of Main VSI failed: %d\n", ret);
5429 goto end_core_reset;
5430 }
5431 }
5432
5433 /* reinit the misc interrupt */
5434 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5435 ret = i40e_setup_misc_vector(pf);
5436
5437 /* restart the VSIs that were rebuilt and running before the reset */
5438 i40e_pf_unquiesce_all_vsi(pf);
5439
Mitch Williams69f64b22014-02-13 03:48:46 -08005440 if (pf->num_alloc_vfs) {
5441 for (v = 0; v < pf->num_alloc_vfs; v++)
5442 i40e_reset_vf(&pf->vf[v], true);
5443 }
5444
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005445 /* tell the firmware that we're starting */
5446 dv.major_version = DRV_VERSION_MAJOR;
5447 dv.minor_version = DRV_VERSION_MINOR;
5448 dv.build_version = DRV_VERSION_BUILD;
5449 dv.subbuild_version = 0;
5450 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
5451
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005452 dev_info(&pf->pdev->dev, "reset complete\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005453
5454end_core_reset:
5455 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
5456}
5457
5458/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005459 * i40e_handle_reset_warning - prep for the pf to reset, reset and rebuild
5460 * @pf: board private structure
5461 *
5462 * Close up the VFs and other things in prep for a Core Reset,
5463 * then get ready to rebuild the world.
5464 **/
5465static void i40e_handle_reset_warning(struct i40e_pf *pf)
5466{
5467 i40e_status ret;
5468
5469 ret = i40e_prep_for_reset(pf);
5470 if (!ret)
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005471 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005472}
5473
5474/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005475 * i40e_handle_mdd_event
5476 * @pf: pointer to the pf structure
5477 *
5478 * Called from the MDD irq handler to identify possibly malicious vfs
5479 **/
5480static void i40e_handle_mdd_event(struct i40e_pf *pf)
5481{
5482 struct i40e_hw *hw = &pf->hw;
5483 bool mdd_detected = false;
5484 struct i40e_vf *vf;
5485 u32 reg;
5486 int i;
5487
5488 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
5489 return;
5490
5491 /* find what triggered the MDD event */
5492 reg = rd32(hw, I40E_GL_MDET_TX);
5493 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
5494 u8 func = (reg & I40E_GL_MDET_TX_FUNCTION_MASK)
5495 >> I40E_GL_MDET_TX_FUNCTION_SHIFT;
5496 u8 event = (reg & I40E_GL_MDET_TX_EVENT_SHIFT)
5497 >> I40E_GL_MDET_TX_EVENT_SHIFT;
5498 u8 queue = (reg & I40E_GL_MDET_TX_QUEUE_MASK)
5499 >> I40E_GL_MDET_TX_QUEUE_SHIFT;
5500 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00005501 "Malicious Driver Detection event 0x%02x on TX queue %d of function 0x%02x\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005502 event, queue, func);
5503 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
5504 mdd_detected = true;
5505 }
5506 reg = rd32(hw, I40E_GL_MDET_RX);
5507 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
5508 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK)
5509 >> I40E_GL_MDET_RX_FUNCTION_SHIFT;
5510 u8 event = (reg & I40E_GL_MDET_RX_EVENT_SHIFT)
5511 >> I40E_GL_MDET_RX_EVENT_SHIFT;
5512 u8 queue = (reg & I40E_GL_MDET_RX_QUEUE_MASK)
5513 >> I40E_GL_MDET_RX_QUEUE_SHIFT;
5514 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00005515 "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005516 event, queue, func);
5517 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
5518 mdd_detected = true;
5519 }
5520
5521 /* see if one of the VFs needs its hand slapped */
5522 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
5523 vf = &(pf->vf[i]);
5524 reg = rd32(hw, I40E_VP_MDET_TX(i));
5525 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
5526 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
5527 vf->num_mdd_events++;
5528 dev_info(&pf->pdev->dev, "MDD TX event on VF %d\n", i);
5529 }
5530
5531 reg = rd32(hw, I40E_VP_MDET_RX(i));
5532 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
5533 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
5534 vf->num_mdd_events++;
5535 dev_info(&pf->pdev->dev, "MDD RX event on VF %d\n", i);
5536 }
5537
5538 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
5539 dev_info(&pf->pdev->dev,
5540 "Too many MDD events on VF %d, disabled\n", i);
5541 dev_info(&pf->pdev->dev,
5542 "Use PF Control I/F to re-enable the VF\n");
5543 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
5544 }
5545 }
5546
5547 /* re-enable mdd interrupt cause */
5548 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
5549 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
5550 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
5551 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
5552 i40e_flush(hw);
5553}
5554
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005555#ifdef CONFIG_I40E_VXLAN
5556/**
5557 * i40e_sync_vxlan_filters_subtask - Sync the VSI filter list with HW
5558 * @pf: board private structure
5559 **/
5560static void i40e_sync_vxlan_filters_subtask(struct i40e_pf *pf)
5561{
5562 const int vxlan_hdr_qwords = 4;
5563 struct i40e_hw *hw = &pf->hw;
5564 i40e_status ret;
5565 u8 filter_index;
5566 __be16 port;
5567 int i;
5568
5569 if (!(pf->flags & I40E_FLAG_VXLAN_FILTER_SYNC))
5570 return;
5571
5572 pf->flags &= ~I40E_FLAG_VXLAN_FILTER_SYNC;
5573
5574 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
5575 if (pf->pending_vxlan_bitmap & (1 << i)) {
5576 pf->pending_vxlan_bitmap &= ~(1 << i);
5577 port = pf->vxlan_ports[i];
5578 ret = port ?
5579 i40e_aq_add_udp_tunnel(hw, ntohs(port),
5580 vxlan_hdr_qwords,
5581 I40E_AQC_TUNNEL_TYPE_VXLAN,
5582 &filter_index, NULL)
5583 : i40e_aq_del_udp_tunnel(hw, i, NULL);
5584
5585 if (ret) {
5586 dev_info(&pf->pdev->dev, "Failed to execute AQ command for %s port %d with index %d\n",
5587 port ? "adding" : "deleting",
5588 ntohs(port), port ? i : i);
5589
5590 pf->vxlan_ports[i] = 0;
5591 } else {
5592 dev_info(&pf->pdev->dev, "%s port %d with AQ command with index %d\n",
5593 port ? "Added" : "Deleted",
5594 ntohs(port), port ? i : filter_index);
5595 }
5596 }
5597 }
5598}
5599
5600#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005601/**
5602 * i40e_service_task - Run the driver's async subtasks
5603 * @work: pointer to work_struct containing our data
5604 **/
5605static void i40e_service_task(struct work_struct *work)
5606{
5607 struct i40e_pf *pf = container_of(work,
5608 struct i40e_pf,
5609 service_task);
5610 unsigned long start_time = jiffies;
5611
5612 i40e_reset_subtask(pf);
5613 i40e_handle_mdd_event(pf);
5614 i40e_vc_process_vflr_event(pf);
5615 i40e_watchdog_subtask(pf);
5616 i40e_fdir_reinit_subtask(pf);
5617 i40e_check_hang_subtask(pf);
5618 i40e_sync_filters_subtask(pf);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00005619#ifdef CONFIG_I40E_VXLAN
5620 i40e_sync_vxlan_filters_subtask(pf);
5621#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005622 i40e_clean_adminq_subtask(pf);
5623
5624 i40e_service_event_complete(pf);
5625
5626 /* If the tasks have taken longer than one timer cycle or there
5627 * is more work to be done, reschedule the service task now
5628 * rather than wait for the timer to tick again.
5629 */
5630 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
5631 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
5632 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
5633 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
5634 i40e_service_event_schedule(pf);
5635}
5636
5637/**
5638 * i40e_service_timer - timer callback
5639 * @data: pointer to PF struct
5640 **/
5641static void i40e_service_timer(unsigned long data)
5642{
5643 struct i40e_pf *pf = (struct i40e_pf *)data;
5644
5645 mod_timer(&pf->service_timer,
5646 round_jiffies(jiffies + pf->service_timer_period));
5647 i40e_service_event_schedule(pf);
5648}
5649
5650/**
5651 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
5652 * @vsi: the VSI being configured
5653 **/
5654static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
5655{
5656 struct i40e_pf *pf = vsi->back;
5657
5658 switch (vsi->type) {
5659 case I40E_VSI_MAIN:
5660 vsi->alloc_queue_pairs = pf->num_lan_qps;
5661 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5662 I40E_REQ_DESCRIPTOR_MULTIPLE);
5663 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5664 vsi->num_q_vectors = pf->num_lan_msix;
5665 else
5666 vsi->num_q_vectors = 1;
5667
5668 break;
5669
5670 case I40E_VSI_FDIR:
5671 vsi->alloc_queue_pairs = 1;
5672 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
5673 I40E_REQ_DESCRIPTOR_MULTIPLE);
5674 vsi->num_q_vectors = 1;
5675 break;
5676
5677 case I40E_VSI_VMDQ2:
5678 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
5679 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5680 I40E_REQ_DESCRIPTOR_MULTIPLE);
5681 vsi->num_q_vectors = pf->num_vmdq_msix;
5682 break;
5683
5684 case I40E_VSI_SRIOV:
5685 vsi->alloc_queue_pairs = pf->num_vf_qps;
5686 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5687 I40E_REQ_DESCRIPTOR_MULTIPLE);
5688 break;
5689
5690 default:
5691 WARN_ON(1);
5692 return -ENODATA;
5693 }
5694
5695 return 0;
5696}
5697
5698/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005699 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
5700 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005701 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005702 *
5703 * On error: returns error code (negative)
5704 * On success: returns 0
5705 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005706static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005707{
5708 int size;
5709 int ret = 0;
5710
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00005711 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005712 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
5713 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
5714 if (!vsi->tx_rings)
5715 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005716 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
5717
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005718 if (alloc_qvectors) {
5719 /* allocate memory for q_vector pointers */
5720 size = sizeof(struct i40e_q_vectors *) * vsi->num_q_vectors;
5721 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
5722 if (!vsi->q_vectors) {
5723 ret = -ENOMEM;
5724 goto err_vectors;
5725 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005726 }
5727 return ret;
5728
5729err_vectors:
5730 kfree(vsi->tx_rings);
5731 return ret;
5732}
5733
5734/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005735 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
5736 * @pf: board private structure
5737 * @type: type of VSI
5738 *
5739 * On error: returns error code (negative)
5740 * On success: returns vsi index in PF (positive)
5741 **/
5742static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
5743{
5744 int ret = -ENODEV;
5745 struct i40e_vsi *vsi;
5746 int vsi_idx;
5747 int i;
5748
5749 /* Need to protect the allocation of the VSIs at the PF level */
5750 mutex_lock(&pf->switch_mutex);
5751
5752 /* VSI list may be fragmented if VSI creation/destruction has
5753 * been happening. We can afford to do a quick scan to look
5754 * for any free VSIs in the list.
5755 *
5756 * find next empty vsi slot, looping back around if necessary
5757 */
5758 i = pf->next_vsi;
5759 while (i < pf->hw.func_caps.num_vsis && pf->vsi[i])
5760 i++;
5761 if (i >= pf->hw.func_caps.num_vsis) {
5762 i = 0;
5763 while (i < pf->next_vsi && pf->vsi[i])
5764 i++;
5765 }
5766
5767 if (i < pf->hw.func_caps.num_vsis && !pf->vsi[i]) {
5768 vsi_idx = i; /* Found one! */
5769 } else {
5770 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00005771 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005772 }
5773 pf->next_vsi = ++i;
5774
5775 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
5776 if (!vsi) {
5777 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00005778 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005779 }
5780 vsi->type = type;
5781 vsi->back = pf;
5782 set_bit(__I40E_DOWN, &vsi->state);
5783 vsi->flags = 0;
5784 vsi->idx = vsi_idx;
5785 vsi->rx_itr_setting = pf->rx_itr_default;
5786 vsi->tx_itr_setting = pf->tx_itr_default;
5787 vsi->netdev_registered = false;
5788 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
5789 INIT_LIST_HEAD(&vsi->mac_filter_list);
5790
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005791 ret = i40e_set_num_rings_in_vsi(vsi);
5792 if (ret)
5793 goto err_rings;
5794
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005795 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005796 if (ret)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005797 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00005798
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005799 /* Setup default MSIX irq handler for VSI */
5800 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
5801
5802 pf->vsi[vsi_idx] = vsi;
5803 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00005804 goto unlock_pf;
5805
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005806err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00005807 pf->next_vsi = i - 1;
5808 kfree(vsi);
5809unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005810 mutex_unlock(&pf->switch_mutex);
5811 return ret;
5812}
5813
5814/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005815 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
5816 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005817 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005818 *
5819 * On error: returns error code (negative)
5820 * On success: returns 0
5821 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005822static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005823{
5824 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005825 if (free_qvectors) {
5826 kfree(vsi->q_vectors);
5827 vsi->q_vectors = NULL;
5828 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00005829 kfree(vsi->tx_rings);
5830 vsi->tx_rings = NULL;
5831 vsi->rx_rings = NULL;
5832}
5833
5834/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005835 * i40e_vsi_clear - Deallocate the VSI provided
5836 * @vsi: the VSI being un-configured
5837 **/
5838static int i40e_vsi_clear(struct i40e_vsi *vsi)
5839{
5840 struct i40e_pf *pf;
5841
5842 if (!vsi)
5843 return 0;
5844
5845 if (!vsi->back)
5846 goto free_vsi;
5847 pf = vsi->back;
5848
5849 mutex_lock(&pf->switch_mutex);
5850 if (!pf->vsi[vsi->idx]) {
5851 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
5852 vsi->idx, vsi->idx, vsi, vsi->type);
5853 goto unlock_vsi;
5854 }
5855
5856 if (pf->vsi[vsi->idx] != vsi) {
5857 dev_err(&pf->pdev->dev,
5858 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
5859 pf->vsi[vsi->idx]->idx,
5860 pf->vsi[vsi->idx],
5861 pf->vsi[vsi->idx]->type,
5862 vsi->idx, vsi, vsi->type);
5863 goto unlock_vsi;
5864 }
5865
5866 /* updates the pf for this cleared vsi */
5867 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
5868 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
5869
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00005870 i40e_vsi_free_arrays(vsi, true);
Alexander Duyck493fb302013-09-28 07:01:44 +00005871
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005872 pf->vsi[vsi->idx] = NULL;
5873 if (vsi->idx < pf->next_vsi)
5874 pf->next_vsi = vsi->idx;
5875
5876unlock_vsi:
5877 mutex_unlock(&pf->switch_mutex);
5878free_vsi:
5879 kfree(vsi);
5880
5881 return 0;
5882}
5883
5884/**
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005885 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
5886 * @vsi: the VSI being cleaned
5887 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00005888static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005889{
5890 int i;
5891
Greg Rose8e9dca52013-12-18 13:45:53 +00005892 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00005893 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00005894 kfree_rcu(vsi->tx_rings[i], rcu);
5895 vsi->tx_rings[i] = NULL;
5896 vsi->rx_rings[i] = NULL;
5897 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00005898 }
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005899}
5900
5901/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005902 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
5903 * @vsi: the VSI being configured
5904 **/
5905static int i40e_alloc_rings(struct i40e_vsi *vsi)
5906{
5907 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005908 int i;
5909
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005910 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00005911 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005912 struct i40e_ring *tx_ring;
5913 struct i40e_ring *rx_ring;
5914
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00005915 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005916 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
5917 if (!tx_ring)
5918 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005919
5920 tx_ring->queue_index = i;
5921 tx_ring->reg_idx = vsi->base_queue + i;
5922 tx_ring->ring_active = false;
5923 tx_ring->vsi = vsi;
5924 tx_ring->netdev = vsi->netdev;
5925 tx_ring->dev = &pf->pdev->dev;
5926 tx_ring->count = vsi->num_desc;
5927 tx_ring->size = 0;
5928 tx_ring->dcb_tc = 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005929 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005930
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005931 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005932 rx_ring->queue_index = i;
5933 rx_ring->reg_idx = vsi->base_queue + i;
5934 rx_ring->ring_active = false;
5935 rx_ring->vsi = vsi;
5936 rx_ring->netdev = vsi->netdev;
5937 rx_ring->dev = &pf->pdev->dev;
5938 rx_ring->count = vsi->num_desc;
5939 rx_ring->size = 0;
5940 rx_ring->dcb_tc = 0;
5941 if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
5942 set_ring_16byte_desc_enabled(rx_ring);
5943 else
5944 clear_ring_16byte_desc_enabled(rx_ring);
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005945 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005946 }
5947
5948 return 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005949
5950err_out:
5951 i40e_vsi_clear_rings(vsi);
5952 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005953}
5954
5955/**
5956 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
5957 * @pf: board private structure
5958 * @vectors: the number of MSI-X vectors to request
5959 *
5960 * Returns the number of vectors reserved, or error
5961 **/
5962static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
5963{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01005964 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
5965 I40E_MIN_MSIX, vectors);
5966 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005967 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01005968 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005969 vectors = 0;
5970 }
5971
Alexander Gordeev7b37f372014-02-18 11:11:42 +01005972 pf->num_msix_entries = vectors;
5973
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005974 return vectors;
5975}
5976
5977/**
5978 * i40e_init_msix - Setup the MSIX capability
5979 * @pf: board private structure
5980 *
5981 * Work with the OS to set up the MSIX vectors needed.
5982 *
5983 * Returns 0 on success, negative on failure
5984 **/
5985static int i40e_init_msix(struct i40e_pf *pf)
5986{
5987 i40e_status err = 0;
5988 struct i40e_hw *hw = &pf->hw;
5989 int v_budget, i;
5990 int vec;
5991
5992 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
5993 return -ENODEV;
5994
5995 /* The number of vectors we'll request will be comprised of:
5996 * - Add 1 for "other" cause for Admin Queue events, etc.
5997 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00005998 * - Queues being used for RSS.
5999 * We don't need as many as max_rss_size vectors.
6000 * use rss_size instead in the calculation since that
6001 * is governed by number of cpus in the system.
6002 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006003 * - The number of VMDq pairs
6004 * Once we count this up, try the request.
6005 *
6006 * If we can't get what we want, we'll simplify to nearly nothing
6007 * and try again. If that still fails, we punt.
6008 */
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006009 pf->num_lan_msix = pf->num_lan_qps - (pf->rss_size_max - pf->rss_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006010 pf->num_vmdq_msix = pf->num_vmdq_qps;
6011 v_budget = 1 + pf->num_lan_msix;
6012 v_budget += (pf->num_vmdq_vsis * pf->num_vmdq_msix);
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006013 if (pf->flags & I40E_FLAG_FD_SB_ENABLED)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006014 v_budget++;
6015
6016 /* Scale down if necessary, and the rings will share vectors */
6017 v_budget = min_t(int, v_budget, hw->func_caps.num_msix_vectors);
6018
6019 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
6020 GFP_KERNEL);
6021 if (!pf->msix_entries)
6022 return -ENOMEM;
6023
6024 for (i = 0; i < v_budget; i++)
6025 pf->msix_entries[i].entry = i;
6026 vec = i40e_reserve_msix_vectors(pf, v_budget);
6027 if (vec < I40E_MIN_MSIX) {
6028 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
6029 kfree(pf->msix_entries);
6030 pf->msix_entries = NULL;
6031 return -ENODEV;
6032
6033 } else if (vec == I40E_MIN_MSIX) {
6034 /* Adjust for minimal MSIX use */
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006035 dev_info(&pf->pdev->dev, "Features disabled, not enough MSI-X vectors\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006036 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
6037 pf->num_vmdq_vsis = 0;
6038 pf->num_vmdq_qps = 0;
6039 pf->num_vmdq_msix = 0;
6040 pf->num_lan_qps = 1;
6041 pf->num_lan_msix = 1;
6042
6043 } else if (vec != v_budget) {
6044 /* Scale vector usage down */
6045 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
6046 vec--; /* reserve the misc vector */
6047
6048 /* partition out the remaining vectors */
6049 switch (vec) {
6050 case 2:
6051 pf->num_vmdq_vsis = 1;
6052 pf->num_lan_msix = 1;
6053 break;
6054 case 3:
6055 pf->num_vmdq_vsis = 1;
6056 pf->num_lan_msix = 2;
6057 break;
6058 default:
6059 pf->num_lan_msix = min_t(int, (vec / 2),
6060 pf->num_lan_qps);
6061 pf->num_vmdq_vsis = min_t(int, (vec - pf->num_lan_msix),
6062 I40E_DEFAULT_NUM_VMDQ_VSI);
6063 break;
6064 }
6065 }
6066
6067 return err;
6068}
6069
6070/**
Alexander Duyck493fb302013-09-28 07:01:44 +00006071 * i40e_alloc_q_vector - Allocate memory for a single interrupt vector
6072 * @vsi: the VSI being configured
6073 * @v_idx: index of the vector in the vsi struct
6074 *
6075 * We allocate one q_vector. If allocation fails we return -ENOMEM.
6076 **/
6077static int i40e_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
6078{
6079 struct i40e_q_vector *q_vector;
6080
6081 /* allocate q_vector */
6082 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
6083 if (!q_vector)
6084 return -ENOMEM;
6085
6086 q_vector->vsi = vsi;
6087 q_vector->v_idx = v_idx;
6088 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
6089 if (vsi->netdev)
6090 netif_napi_add(vsi->netdev, &q_vector->napi,
6091 i40e_napi_poll, vsi->work_limit);
6092
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00006093 q_vector->rx.latency_range = I40E_LOW_LATENCY;
6094 q_vector->tx.latency_range = I40E_LOW_LATENCY;
6095
Alexander Duyck493fb302013-09-28 07:01:44 +00006096 /* tie q_vector and vsi together */
6097 vsi->q_vectors[v_idx] = q_vector;
6098
6099 return 0;
6100}
6101
6102/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006103 * i40e_alloc_q_vectors - Allocate memory for interrupt vectors
6104 * @vsi: the VSI being configured
6105 *
6106 * We allocate one q_vector per queue interrupt. If allocation fails we
6107 * return -ENOMEM.
6108 **/
6109static int i40e_alloc_q_vectors(struct i40e_vsi *vsi)
6110{
6111 struct i40e_pf *pf = vsi->back;
6112 int v_idx, num_q_vectors;
Alexander Duyck493fb302013-09-28 07:01:44 +00006113 int err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006114
6115 /* if not MSIX, give the one vector only to the LAN VSI */
6116 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6117 num_q_vectors = vsi->num_q_vectors;
6118 else if (vsi == pf->vsi[pf->lan_vsi])
6119 num_q_vectors = 1;
6120 else
6121 return -EINVAL;
6122
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006123 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00006124 err = i40e_alloc_q_vector(vsi, v_idx);
6125 if (err)
6126 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006127 }
6128
6129 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00006130
6131err_out:
6132 while (v_idx--)
6133 i40e_free_q_vector(vsi, v_idx);
6134
6135 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006136}
6137
6138/**
6139 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
6140 * @pf: board private structure to initialize
6141 **/
6142static void i40e_init_interrupt_scheme(struct i40e_pf *pf)
6143{
6144 int err = 0;
6145
6146 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
6147 err = i40e_init_msix(pf);
6148 if (err) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006149 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
6150 I40E_FLAG_RSS_ENABLED |
6151 I40E_FLAG_DCB_ENABLED |
6152 I40E_FLAG_SRIOV_ENABLED |
6153 I40E_FLAG_FD_SB_ENABLED |
6154 I40E_FLAG_FD_ATR_ENABLED |
6155 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006156
6157 /* rework the queue expectations without MSIX */
6158 i40e_determine_queue_usage(pf);
6159 }
6160 }
6161
6162 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
6163 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006164 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006165 err = pci_enable_msi(pf->pdev);
6166 if (err) {
Shannon Nelson958a3e32013-09-28 07:13:28 +00006167 dev_info(&pf->pdev->dev, "MSI init failed - %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006168 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
6169 }
6170 }
6171
Shannon Nelson958a3e32013-09-28 07:13:28 +00006172 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006173 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00006174
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006175 /* track first vector for misc interrupts */
6176 err = i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT-1);
6177}
6178
6179/**
6180 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
6181 * @pf: board private structure
6182 *
6183 * This sets up the handler for MSIX 0, which is used to manage the
6184 * non-queue interrupts, e.g. AdminQ and errors. This is not used
6185 * when in MSI or Legacy interrupt mode.
6186 **/
6187static int i40e_setup_misc_vector(struct i40e_pf *pf)
6188{
6189 struct i40e_hw *hw = &pf->hw;
6190 int err = 0;
6191
6192 /* Only request the irq if this is the first time through, and
6193 * not when we're rebuilding after a Reset
6194 */
6195 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
6196 err = request_irq(pf->msix_entries[0].vector,
6197 i40e_intr, 0, pf->misc_int_name, pf);
6198 if (err) {
6199 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08006200 "request_irq for %s failed: %d\n",
6201 pf->misc_int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006202 return -EFAULT;
6203 }
6204 }
6205
6206 i40e_enable_misc_int_causes(hw);
6207
6208 /* associate no queues to the misc vector */
6209 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
6210 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
6211
6212 i40e_flush(hw);
6213
6214 i40e_irq_dynamic_enable_icr0(pf);
6215
6216 return err;
6217}
6218
6219/**
6220 * i40e_config_rss - Prepare for RSS if used
6221 * @pf: board private structure
6222 **/
6223static int i40e_config_rss(struct i40e_pf *pf)
6224{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006225 /* Set of random keys generated using kernel random number generator */
6226 static const u32 seed[I40E_PFQF_HKEY_MAX_INDEX + 1] = {0x41b01687,
6227 0x183cfd8c, 0xce880440, 0x580cbc3c, 0x35897377,
6228 0x328b25e1, 0x4fa98922, 0xb7d90c14, 0xd5bad70d,
6229 0xcd15a2c1, 0xe8580225, 0x4a1e9d11, 0xfe5731be};
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00006230 struct i40e_hw *hw = &pf->hw;
6231 u32 lut = 0;
6232 int i, j;
6233 u64 hena;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006234
6235 /* Fill out hash function seed */
6236 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
6237 wr32(hw, I40E_PFQF_HKEY(i), seed[i]);
6238
6239 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
6240 hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
6241 ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
Mitch Williams12dc4fe2013-11-28 06:39:32 +00006242 hena |= I40E_DEFAULT_RSS_HENA;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006243 wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
6244 wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
6245
6246 /* Populate the LUT with max no. of queues in round robin fashion */
6247 for (i = 0, j = 0; i < pf->hw.func_caps.rss_table_size; i++, j++) {
6248
6249 /* The assumption is that lan qp count will be the highest
6250 * qp count for any PF VSI that needs RSS.
6251 * If multiple VSIs need RSS support, all the qp counts
6252 * for those VSIs should be a power of 2 for RSS to work.
6253 * If LAN VSI is the only consumer for RSS then this requirement
6254 * is not necessary.
6255 */
6256 if (j == pf->rss_size)
6257 j = 0;
6258 /* lut = 4-byte sliding window of 4 lut entries */
6259 lut = (lut << 8) | (j &
6260 ((0x1 << pf->hw.func_caps.rss_table_entry_width) - 1));
6261 /* On i = 3, we have 4 entries in lut; write to the register */
6262 if ((i & 3) == 3)
6263 wr32(hw, I40E_PFQF_HLUT(i >> 2), lut);
6264 }
6265 i40e_flush(hw);
6266
6267 return 0;
6268}
6269
6270/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006271 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
6272 * @pf: board private structure
6273 * @queue_count: the requested queue count for rss.
6274 *
6275 * returns 0 if rss is not enabled, if enabled returns the final rss queue
6276 * count which may be different from the requested queue count.
6277 **/
6278int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
6279{
6280 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
6281 return 0;
6282
6283 queue_count = min_t(int, queue_count, pf->rss_size_max);
6284 queue_count = rounddown_pow_of_two(queue_count);
6285
6286 if (queue_count != pf->rss_size) {
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006287 i40e_prep_for_reset(pf);
6288
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006289 pf->rss_size = queue_count;
6290
6291 i40e_reset_and_rebuild(pf, true);
6292 i40e_config_rss(pf);
6293 }
6294 dev_info(&pf->pdev->dev, "RSS count: %d\n", pf->rss_size);
6295 return pf->rss_size;
6296}
6297
6298/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006299 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
6300 * @pf: board private structure to initialize
6301 *
6302 * i40e_sw_init initializes the Adapter private data structure.
6303 * Fields are initialized based on PCI device information and
6304 * OS network device settings (MTU size).
6305 **/
6306static int i40e_sw_init(struct i40e_pf *pf)
6307{
6308 int err = 0;
6309 int size;
6310
6311 pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
6312 (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
Shannon Nelson27599972013-11-16 10:00:43 +00006313 pf->hw.debug_mask = pf->msg_enable | I40E_DEBUG_DIAG;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006314 if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
6315 if (I40E_DEBUG_USER & debug)
6316 pf->hw.debug_mask = debug;
6317 pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
6318 I40E_DEFAULT_MSG_ENABLE);
6319 }
6320
6321 /* Set default capability flags */
6322 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
6323 I40E_FLAG_MSI_ENABLED |
6324 I40E_FLAG_MSIX_ENABLED |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006325 I40E_FLAG_RX_1BUF_ENABLED;
6326
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006327 /* Depending on PF configurations, it is possible that the RSS
6328 * maximum might end up larger than the available queues
6329 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006330 pf->rss_size_max = 0x1 << pf->hw.func_caps.rss_table_entry_width;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006331 pf->rss_size_max = min_t(int, pf->rss_size_max,
6332 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006333 if (pf->hw.func_caps.rss) {
6334 pf->flags |= I40E_FLAG_RSS_ENABLED;
Jesse Brandeburgbf051a32013-11-26 10:49:17 +00006335 pf->rss_size = min_t(int, pf->rss_size_max, num_online_cpus());
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006336 pf->rss_size = rounddown_pow_of_two(pf->rss_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006337 } else {
6338 pf->rss_size = 1;
6339 }
6340
Catherine Sullivan2050bc62013-12-18 13:46:03 +00006341 /* MFP mode enabled */
6342 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.mfp_mode_1) {
6343 pf->flags |= I40E_FLAG_MFP_ENABLED;
6344 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
6345 }
6346
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006347 /* FW/NVM is not yet fixed in this regard */
6348 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
6349 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
6350 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
6351 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006352 if (!(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08006353 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006354 } else {
6355 dev_info(&pf->pdev->dev,
6356 "Flow Director Side Band mode Disabled in MFP mode\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006357 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006358 pf->fdir_pf_filter_count =
6359 pf->hw.func_caps.fd_filters_guaranteed;
6360 pf->hw.fdir_shared_filter_count =
6361 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006362 }
6363
6364 if (pf->hw.func_caps.vmdq) {
6365 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
6366 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
6367 pf->num_vmdq_qps = I40E_DEFAULT_QUEUES_PER_VMDQ;
6368 }
6369
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006370#ifdef CONFIG_PCI_IOV
6371 if (pf->hw.func_caps.num_vfs) {
6372 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
6373 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
6374 pf->num_req_vfs = min_t(int,
6375 pf->hw.func_caps.num_vfs,
6376 I40E_MAX_VF_COUNT);
6377 }
6378#endif /* CONFIG_PCI_IOV */
6379 pf->eeprom_version = 0xDEAD;
6380 pf->lan_veb = I40E_NO_VEB;
6381 pf->lan_vsi = I40E_NO_VSI;
6382
6383 /* set up queue assignment tracking */
6384 size = sizeof(struct i40e_lump_tracking)
6385 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
6386 pf->qp_pile = kzalloc(size, GFP_KERNEL);
6387 if (!pf->qp_pile) {
6388 err = -ENOMEM;
6389 goto sw_init_done;
6390 }
6391 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
6392 pf->qp_pile->search_hint = 0;
6393
6394 /* set up vector assignment tracking */
6395 size = sizeof(struct i40e_lump_tracking)
6396 + (sizeof(u16) * pf->hw.func_caps.num_msix_vectors);
6397 pf->irq_pile = kzalloc(size, GFP_KERNEL);
6398 if (!pf->irq_pile) {
6399 kfree(pf->qp_pile);
6400 err = -ENOMEM;
6401 goto sw_init_done;
6402 }
6403 pf->irq_pile->num_entries = pf->hw.func_caps.num_msix_vectors;
6404 pf->irq_pile->search_hint = 0;
6405
6406 mutex_init(&pf->switch_mutex);
6407
6408sw_init_done:
6409 return err;
6410}
6411
6412/**
6413 * i40e_set_features - set the netdev feature flags
6414 * @netdev: ptr to the netdev being adjusted
6415 * @features: the feature set that the stack is suggesting
6416 **/
6417static int i40e_set_features(struct net_device *netdev,
6418 netdev_features_t features)
6419{
6420 struct i40e_netdev_priv *np = netdev_priv(netdev);
6421 struct i40e_vsi *vsi = np->vsi;
6422
6423 if (features & NETIF_F_HW_VLAN_CTAG_RX)
6424 i40e_vlan_stripping_enable(vsi);
6425 else
6426 i40e_vlan_stripping_disable(vsi);
6427
6428 return 0;
6429}
6430
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006431#ifdef CONFIG_I40E_VXLAN
6432/**
6433 * i40e_get_vxlan_port_idx - Lookup a possibly offloaded for Rx UDP port
6434 * @pf: board private structure
6435 * @port: The UDP port to look up
6436 *
6437 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
6438 **/
6439static u8 i40e_get_vxlan_port_idx(struct i40e_pf *pf, __be16 port)
6440{
6441 u8 i;
6442
6443 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
6444 if (pf->vxlan_ports[i] == port)
6445 return i;
6446 }
6447
6448 return i;
6449}
6450
6451/**
6452 * i40e_add_vxlan_port - Get notifications about VXLAN ports that come up
6453 * @netdev: This physical port's netdev
6454 * @sa_family: Socket Family that VXLAN is notifying us about
6455 * @port: New UDP port number that VXLAN started listening to
6456 **/
6457static void i40e_add_vxlan_port(struct net_device *netdev,
6458 sa_family_t sa_family, __be16 port)
6459{
6460 struct i40e_netdev_priv *np = netdev_priv(netdev);
6461 struct i40e_vsi *vsi = np->vsi;
6462 struct i40e_pf *pf = vsi->back;
6463 u8 next_idx;
6464 u8 idx;
6465
6466 if (sa_family == AF_INET6)
6467 return;
6468
6469 idx = i40e_get_vxlan_port_idx(pf, port);
6470
6471 /* Check if port already exists */
6472 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6473 netdev_info(netdev, "Port %d already offloaded\n", ntohs(port));
6474 return;
6475 }
6476
6477 /* Now check if there is space to add the new port */
6478 next_idx = i40e_get_vxlan_port_idx(pf, 0);
6479
6480 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6481 netdev_info(netdev, "Maximum number of UDP ports reached, not adding port %d\n",
6482 ntohs(port));
6483 return;
6484 }
6485
6486 /* New port: add it and mark its index in the bitmap */
6487 pf->vxlan_ports[next_idx] = port;
6488 pf->pending_vxlan_bitmap |= (1 << next_idx);
6489
6490 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
6491}
6492
6493/**
6494 * i40e_del_vxlan_port - Get notifications about VXLAN ports that go away
6495 * @netdev: This physical port's netdev
6496 * @sa_family: Socket Family that VXLAN is notifying us about
6497 * @port: UDP port number that VXLAN stopped listening to
6498 **/
6499static void i40e_del_vxlan_port(struct net_device *netdev,
6500 sa_family_t sa_family, __be16 port)
6501{
6502 struct i40e_netdev_priv *np = netdev_priv(netdev);
6503 struct i40e_vsi *vsi = np->vsi;
6504 struct i40e_pf *pf = vsi->back;
6505 u8 idx;
6506
6507 if (sa_family == AF_INET6)
6508 return;
6509
6510 idx = i40e_get_vxlan_port_idx(pf, port);
6511
6512 /* Check if port already exists */
6513 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6514 /* if port exists, set it to 0 (mark for deletion)
6515 * and make it pending
6516 */
6517 pf->vxlan_ports[idx] = 0;
6518
6519 pf->pending_vxlan_bitmap |= (1 << idx);
6520
6521 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
6522 } else {
6523 netdev_warn(netdev, "Port %d was not found, not deleting\n",
6524 ntohs(port));
6525 }
6526}
6527
6528#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006529static const struct net_device_ops i40e_netdev_ops = {
6530 .ndo_open = i40e_open,
6531 .ndo_stop = i40e_close,
6532 .ndo_start_xmit = i40e_lan_xmit_frame,
6533 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
6534 .ndo_set_rx_mode = i40e_set_rx_mode,
6535 .ndo_validate_addr = eth_validate_addr,
6536 .ndo_set_mac_address = i40e_set_mac,
6537 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006538 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006539 .ndo_tx_timeout = i40e_tx_timeout,
6540 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
6541 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
6542#ifdef CONFIG_NET_POLL_CONTROLLER
6543 .ndo_poll_controller = i40e_netpoll,
6544#endif
6545 .ndo_setup_tc = i40e_setup_tc,
6546 .ndo_set_features = i40e_set_features,
6547 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
6548 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
6549 .ndo_set_vf_tx_rate = i40e_ndo_set_vf_bw,
6550 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00006551#ifdef CONFIG_I40E_VXLAN
6552 .ndo_add_vxlan_port = i40e_add_vxlan_port,
6553 .ndo_del_vxlan_port = i40e_del_vxlan_port,
6554#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006555};
6556
6557/**
6558 * i40e_config_netdev - Setup the netdev flags
6559 * @vsi: the VSI being configured
6560 *
6561 * Returns 0 on success, negative value on failure
6562 **/
6563static int i40e_config_netdev(struct i40e_vsi *vsi)
6564{
Greg Rose1a103702013-11-28 06:42:39 +00006565 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006566 struct i40e_pf *pf = vsi->back;
6567 struct i40e_hw *hw = &pf->hw;
6568 struct i40e_netdev_priv *np;
6569 struct net_device *netdev;
6570 u8 mac_addr[ETH_ALEN];
6571 int etherdev_size;
6572
6573 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00006574 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006575 if (!netdev)
6576 return -ENOMEM;
6577
6578 vsi->netdev = netdev;
6579 np = netdev_priv(netdev);
6580 np->vsi = vsi;
6581
6582 netdev->hw_enc_features = NETIF_F_IP_CSUM |
6583 NETIF_F_GSO_UDP_TUNNEL |
6584 NETIF_F_TSO |
6585 NETIF_F_SG;
6586
6587 netdev->features = NETIF_F_SG |
6588 NETIF_F_IP_CSUM |
6589 NETIF_F_SCTP_CSUM |
6590 NETIF_F_HIGHDMA |
6591 NETIF_F_GSO_UDP_TUNNEL |
6592 NETIF_F_HW_VLAN_CTAG_TX |
6593 NETIF_F_HW_VLAN_CTAG_RX |
6594 NETIF_F_HW_VLAN_CTAG_FILTER |
6595 NETIF_F_IPV6_CSUM |
6596 NETIF_F_TSO |
6597 NETIF_F_TSO6 |
6598 NETIF_F_RXCSUM |
6599 NETIF_F_RXHASH |
6600 0;
6601
6602 /* copy netdev features into list of user selectable features */
6603 netdev->hw_features |= netdev->features;
6604
6605 if (vsi->type == I40E_VSI_MAIN) {
6606 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
6607 memcpy(mac_addr, hw->mac.perm_addr, ETH_ALEN);
6608 } else {
6609 /* relate the VSI_VMDQ name to the VSI_MAIN name */
6610 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
6611 pf->vsi[pf->lan_vsi]->netdev->name);
6612 random_ether_addr(mac_addr);
6613 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
6614 }
Greg Rose1a103702013-11-28 06:42:39 +00006615 i40e_add_filter(vsi, brdcast, I40E_VLAN_ANY, false, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006616
6617 memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
6618 memcpy(netdev->perm_addr, mac_addr, ETH_ALEN);
6619 /* vlan gets same features (except vlan offload)
6620 * after any tweaks for specific VSI types
6621 */
6622 netdev->vlan_features = netdev->features & ~(NETIF_F_HW_VLAN_CTAG_TX |
6623 NETIF_F_HW_VLAN_CTAG_RX |
6624 NETIF_F_HW_VLAN_CTAG_FILTER);
6625 netdev->priv_flags |= IFF_UNICAST_FLT;
6626 netdev->priv_flags |= IFF_SUPP_NOFCS;
6627 /* Setup netdev TC information */
6628 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
6629
6630 netdev->netdev_ops = &i40e_netdev_ops;
6631 netdev->watchdog_timeo = 5 * HZ;
6632 i40e_set_ethtool_ops(netdev);
6633
6634 return 0;
6635}
6636
6637/**
6638 * i40e_vsi_delete - Delete a VSI from the switch
6639 * @vsi: the VSI being removed
6640 *
6641 * Returns 0 on success, negative value on failure
6642 **/
6643static void i40e_vsi_delete(struct i40e_vsi *vsi)
6644{
6645 /* remove default VSI is not allowed */
6646 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
6647 return;
6648
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006649 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
6650 return;
6651}
6652
6653/**
6654 * i40e_add_vsi - Add a VSI to the switch
6655 * @vsi: the VSI being configured
6656 *
6657 * This initializes a VSI context depending on the VSI type to be added and
6658 * passes it down to the add_vsi aq command.
6659 **/
6660static int i40e_add_vsi(struct i40e_vsi *vsi)
6661{
6662 int ret = -ENODEV;
6663 struct i40e_mac_filter *f, *ftmp;
6664 struct i40e_pf *pf = vsi->back;
6665 struct i40e_hw *hw = &pf->hw;
6666 struct i40e_vsi_context ctxt;
6667 u8 enabled_tc = 0x1; /* TC0 enabled */
6668 int f_count = 0;
6669
6670 memset(&ctxt, 0, sizeof(ctxt));
6671 switch (vsi->type) {
6672 case I40E_VSI_MAIN:
6673 /* The PF's main VSI is already setup as part of the
6674 * device initialization, so we'll not bother with
6675 * the add_vsi call, but we will retrieve the current
6676 * VSI context.
6677 */
6678 ctxt.seid = pf->main_vsi_seid;
6679 ctxt.pf_num = pf->hw.pf_id;
6680 ctxt.vf_num = 0;
6681 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6682 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6683 if (ret) {
6684 dev_info(&pf->pdev->dev,
6685 "couldn't get pf vsi config, err %d, aq_err %d\n",
6686 ret, pf->hw.aq.asq_last_status);
6687 return -ENOENT;
6688 }
6689 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
6690 vsi->info.valid_sections = 0;
6691
6692 vsi->seid = ctxt.seid;
6693 vsi->id = ctxt.vsi_number;
6694
6695 enabled_tc = i40e_pf_get_tc_map(pf);
6696
6697 /* MFP mode setup queue map and update VSI */
6698 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
6699 memset(&ctxt, 0, sizeof(ctxt));
6700 ctxt.seid = pf->main_vsi_seid;
6701 ctxt.pf_num = pf->hw.pf_id;
6702 ctxt.vf_num = 0;
6703 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
6704 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
6705 if (ret) {
6706 dev_info(&pf->pdev->dev,
6707 "update vsi failed, aq_err=%d\n",
6708 pf->hw.aq.asq_last_status);
6709 ret = -ENOENT;
6710 goto err;
6711 }
6712 /* update the local VSI info queue map */
6713 i40e_vsi_update_queue_map(vsi, &ctxt);
6714 vsi->info.valid_sections = 0;
6715 } else {
6716 /* Default/Main VSI is only enabled for TC0
6717 * reconfigure it to enable all TCs that are
6718 * available on the port in SFP mode.
6719 */
6720 ret = i40e_vsi_config_tc(vsi, enabled_tc);
6721 if (ret) {
6722 dev_info(&pf->pdev->dev,
6723 "failed to configure TCs for main VSI tc_map 0x%08x, err %d, aq_err %d\n",
6724 enabled_tc, ret,
6725 pf->hw.aq.asq_last_status);
6726 ret = -ENOENT;
6727 }
6728 }
6729 break;
6730
6731 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006732 ctxt.pf_num = hw->pf_id;
6733 ctxt.vf_num = 0;
6734 ctxt.uplink_seid = vsi->uplink_seid;
6735 ctxt.connection_type = 0x1; /* regular data port */
6736 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006737 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006738 break;
6739
6740 case I40E_VSI_VMDQ2:
6741 ctxt.pf_num = hw->pf_id;
6742 ctxt.vf_num = 0;
6743 ctxt.uplink_seid = vsi->uplink_seid;
6744 ctxt.connection_type = 0x1; /* regular data port */
6745 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6746
6747 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6748
6749 /* This VSI is connected to VEB so the switch_id
6750 * should be set to zero by default.
6751 */
6752 ctxt.info.switch_id = 0;
6753 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
6754 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6755
6756 /* Setup the VSI tx/rx queue map for TC0 only for now */
6757 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
6758 break;
6759
6760 case I40E_VSI_SRIOV:
6761 ctxt.pf_num = hw->pf_id;
6762 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
6763 ctxt.uplink_seid = vsi->uplink_seid;
6764 ctxt.connection_type = 0x1; /* regular data port */
6765 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
6766
6767 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6768
6769 /* This VSI is connected to VEB so the switch_id
6770 * should be set to zero by default.
6771 */
6772 ctxt.info.switch_id = cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6773
6774 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
6775 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
6776 /* Setup the VSI tx/rx queue map for TC0 only for now */
6777 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
6778 break;
6779
6780 default:
6781 return -ENODEV;
6782 }
6783
6784 if (vsi->type != I40E_VSI_MAIN) {
6785 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6786 if (ret) {
6787 dev_info(&vsi->back->pdev->dev,
6788 "add vsi failed, aq_err=%d\n",
6789 vsi->back->hw.aq.asq_last_status);
6790 ret = -ENOENT;
6791 goto err;
6792 }
6793 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
6794 vsi->info.valid_sections = 0;
6795 vsi->seid = ctxt.seid;
6796 vsi->id = ctxt.vsi_number;
6797 }
6798
6799 /* If macvlan filters already exist, force them to get loaded */
6800 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
6801 f->changed = true;
6802 f_count++;
6803 }
6804 if (f_count) {
6805 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
6806 pf->flags |= I40E_FLAG_FILTER_SYNC;
6807 }
6808
6809 /* Update VSI BW information */
6810 ret = i40e_vsi_get_bw_info(vsi);
6811 if (ret) {
6812 dev_info(&pf->pdev->dev,
6813 "couldn't get vsi bw info, err %d, aq_err %d\n",
6814 ret, pf->hw.aq.asq_last_status);
6815 /* VSI is already added so not tearing that up */
6816 ret = 0;
6817 }
6818
6819err:
6820 return ret;
6821}
6822
6823/**
6824 * i40e_vsi_release - Delete a VSI and free its resources
6825 * @vsi: the VSI being removed
6826 *
6827 * Returns 0 on success or < 0 on error
6828 **/
6829int i40e_vsi_release(struct i40e_vsi *vsi)
6830{
6831 struct i40e_mac_filter *f, *ftmp;
6832 struct i40e_veb *veb = NULL;
6833 struct i40e_pf *pf;
6834 u16 uplink_seid;
6835 int i, n;
6836
6837 pf = vsi->back;
6838
6839 /* release of a VEB-owner or last VSI is not allowed */
6840 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
6841 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
6842 vsi->seid, vsi->uplink_seid);
6843 return -ENODEV;
6844 }
6845 if (vsi == pf->vsi[pf->lan_vsi] &&
6846 !test_bit(__I40E_DOWN, &pf->state)) {
6847 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
6848 return -ENODEV;
6849 }
6850
6851 uplink_seid = vsi->uplink_seid;
6852 if (vsi->type != I40E_VSI_SRIOV) {
6853 if (vsi->netdev_registered) {
6854 vsi->netdev_registered = false;
6855 if (vsi->netdev) {
6856 /* results in a call to i40e_close() */
6857 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006858 }
6859 } else {
6860 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
6861 i40e_down(vsi);
6862 i40e_vsi_free_irq(vsi);
6863 i40e_vsi_free_tx_resources(vsi);
6864 i40e_vsi_free_rx_resources(vsi);
6865 }
6866 i40e_vsi_disable_irq(vsi);
6867 }
6868
6869 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
6870 i40e_del_filter(vsi, f->macaddr, f->vlan,
6871 f->is_vf, f->is_netdev);
6872 i40e_sync_vsi_filters(vsi);
6873
6874 i40e_vsi_delete(vsi);
6875 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00006876 if (vsi->netdev) {
6877 free_netdev(vsi->netdev);
6878 vsi->netdev = NULL;
6879 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006880 i40e_vsi_clear_rings(vsi);
6881 i40e_vsi_clear(vsi);
6882
6883 /* If this was the last thing on the VEB, except for the
6884 * controlling VSI, remove the VEB, which puts the controlling
6885 * VSI onto the next level down in the switch.
6886 *
6887 * Well, okay, there's one more exception here: don't remove
6888 * the orphan VEBs yet. We'll wait for an explicit remove request
6889 * from up the network stack.
6890 */
6891 for (n = 0, i = 0; i < pf->hw.func_caps.num_vsis; i++) {
6892 if (pf->vsi[i] &&
6893 pf->vsi[i]->uplink_seid == uplink_seid &&
6894 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
6895 n++; /* count the VSIs */
6896 }
6897 }
6898 for (i = 0; i < I40E_MAX_VEB; i++) {
6899 if (!pf->veb[i])
6900 continue;
6901 if (pf->veb[i]->uplink_seid == uplink_seid)
6902 n++; /* count the VEBs */
6903 if (pf->veb[i]->seid == uplink_seid)
6904 veb = pf->veb[i];
6905 }
6906 if (n == 0 && veb && veb->uplink_seid != 0)
6907 i40e_veb_release(veb);
6908
6909 return 0;
6910}
6911
6912/**
6913 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
6914 * @vsi: ptr to the VSI
6915 *
6916 * This should only be called after i40e_vsi_mem_alloc() which allocates the
6917 * corresponding SW VSI structure and initializes num_queue_pairs for the
6918 * newly allocated VSI.
6919 *
6920 * Returns 0 on success or negative on failure
6921 **/
6922static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
6923{
6924 int ret = -ENOENT;
6925 struct i40e_pf *pf = vsi->back;
6926
Alexander Duyck493fb302013-09-28 07:01:44 +00006927 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006928 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
6929 vsi->seid);
6930 return -EEXIST;
6931 }
6932
6933 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00006934 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006935 vsi->seid, vsi->base_vector);
6936 return -EEXIST;
6937 }
6938
6939 ret = i40e_alloc_q_vectors(vsi);
6940 if (ret) {
6941 dev_info(&pf->pdev->dev,
6942 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
6943 vsi->num_q_vectors, vsi->seid, ret);
6944 vsi->num_q_vectors = 0;
6945 goto vector_setup_out;
6946 }
6947
Shannon Nelson958a3e32013-09-28 07:13:28 +00006948 if (vsi->num_q_vectors)
6949 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
6950 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006951 if (vsi->base_vector < 0) {
6952 dev_info(&pf->pdev->dev,
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00006953 "failed to get queue tracking for VSI %d, err=%d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006954 vsi->seid, vsi->base_vector);
6955 i40e_vsi_free_q_vectors(vsi);
6956 ret = -ENOENT;
6957 goto vector_setup_out;
6958 }
6959
6960vector_setup_out:
6961 return ret;
6962}
6963
6964/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006965 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
6966 * @vsi: pointer to the vsi.
6967 *
6968 * This re-allocates a vsi's queue resources.
6969 *
6970 * Returns pointer to the successfully allocated and configured VSI sw struct
6971 * on success, otherwise returns NULL on failure.
6972 **/
6973static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
6974{
6975 struct i40e_pf *pf = vsi->back;
6976 u8 enabled_tc;
6977 int ret;
6978
6979 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
6980 i40e_vsi_clear_rings(vsi);
6981
6982 i40e_vsi_free_arrays(vsi, false);
6983 i40e_set_num_rings_in_vsi(vsi);
6984 ret = i40e_vsi_alloc_arrays(vsi, false);
6985 if (ret)
6986 goto err_vsi;
6987
6988 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
6989 if (ret < 0) {
6990 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
6991 vsi->seid, ret);
6992 goto err_vsi;
6993 }
6994 vsi->base_queue = ret;
6995
6996 /* Update the FW view of the VSI. Force a reset of TC and queue
6997 * layout configurations.
6998 */
6999 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
7000 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
7001 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
7002 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
7003
7004 /* assign it some queues */
7005 ret = i40e_alloc_rings(vsi);
7006 if (ret)
7007 goto err_rings;
7008
7009 /* map all of the rings to the q_vectors */
7010 i40e_vsi_map_rings_to_vectors(vsi);
7011 return vsi;
7012
7013err_rings:
7014 i40e_vsi_free_q_vectors(vsi);
7015 if (vsi->netdev_registered) {
7016 vsi->netdev_registered = false;
7017 unregister_netdev(vsi->netdev);
7018 free_netdev(vsi->netdev);
7019 vsi->netdev = NULL;
7020 }
7021 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
7022err_vsi:
7023 i40e_vsi_clear(vsi);
7024 return NULL;
7025}
7026
7027/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007028 * i40e_vsi_setup - Set up a VSI by a given type
7029 * @pf: board private structure
7030 * @type: VSI type
7031 * @uplink_seid: the switch element to link to
7032 * @param1: usage depends upon VSI type. For VF types, indicates VF id
7033 *
7034 * This allocates the sw VSI structure and its queue resources, then add a VSI
7035 * to the identified VEB.
7036 *
7037 * Returns pointer to the successfully allocated and configure VSI sw struct on
7038 * success, otherwise returns NULL on failure.
7039 **/
7040struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
7041 u16 uplink_seid, u32 param1)
7042{
7043 struct i40e_vsi *vsi = NULL;
7044 struct i40e_veb *veb = NULL;
7045 int ret, i;
7046 int v_idx;
7047
7048 /* The requested uplink_seid must be either
7049 * - the PF's port seid
7050 * no VEB is needed because this is the PF
7051 * or this is a Flow Director special case VSI
7052 * - seid of an existing VEB
7053 * - seid of a VSI that owns an existing VEB
7054 * - seid of a VSI that doesn't own a VEB
7055 * a new VEB is created and the VSI becomes the owner
7056 * - seid of the PF VSI, which is what creates the first VEB
7057 * this is a special case of the previous
7058 *
7059 * Find which uplink_seid we were given and create a new VEB if needed
7060 */
7061 for (i = 0; i < I40E_MAX_VEB; i++) {
7062 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
7063 veb = pf->veb[i];
7064 break;
7065 }
7066 }
7067
7068 if (!veb && uplink_seid != pf->mac_seid) {
7069
7070 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
7071 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
7072 vsi = pf->vsi[i];
7073 break;
7074 }
7075 }
7076 if (!vsi) {
7077 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
7078 uplink_seid);
7079 return NULL;
7080 }
7081
7082 if (vsi->uplink_seid == pf->mac_seid)
7083 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
7084 vsi->tc_config.enabled_tc);
7085 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
7086 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
7087 vsi->tc_config.enabled_tc);
7088
7089 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
7090 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
7091 veb = pf->veb[i];
7092 }
7093 if (!veb) {
7094 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
7095 return NULL;
7096 }
7097
7098 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
7099 uplink_seid = veb->seid;
7100 }
7101
7102 /* get vsi sw struct */
7103 v_idx = i40e_vsi_mem_alloc(pf, type);
7104 if (v_idx < 0)
7105 goto err_alloc;
7106 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007107 if (!vsi)
7108 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007109 vsi->type = type;
7110 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
7111
7112 if (type == I40E_VSI_MAIN)
7113 pf->lan_vsi = v_idx;
7114 else if (type == I40E_VSI_SRIOV)
7115 vsi->vf_id = param1;
7116 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007117 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
7118 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007119 if (ret < 0) {
7120 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
7121 vsi->seid, ret);
7122 goto err_vsi;
7123 }
7124 vsi->base_queue = ret;
7125
7126 /* get a VSI from the hardware */
7127 vsi->uplink_seid = uplink_seid;
7128 ret = i40e_add_vsi(vsi);
7129 if (ret)
7130 goto err_vsi;
7131
7132 switch (vsi->type) {
7133 /* setup the netdev if needed */
7134 case I40E_VSI_MAIN:
7135 case I40E_VSI_VMDQ2:
7136 ret = i40e_config_netdev(vsi);
7137 if (ret)
7138 goto err_netdev;
7139 ret = register_netdev(vsi->netdev);
7140 if (ret)
7141 goto err_netdev;
7142 vsi->netdev_registered = true;
7143 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007144#ifdef CONFIG_I40E_DCB
7145 /* Setup DCB netlink interface */
7146 i40e_dcbnl_setup(vsi);
7147#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007148 /* fall through */
7149
7150 case I40E_VSI_FDIR:
7151 /* set up vectors and rings if needed */
7152 ret = i40e_vsi_setup_vectors(vsi);
7153 if (ret)
7154 goto err_msix;
7155
7156 ret = i40e_alloc_rings(vsi);
7157 if (ret)
7158 goto err_rings;
7159
7160 /* map all of the rings to the q_vectors */
7161 i40e_vsi_map_rings_to_vectors(vsi);
7162
7163 i40e_vsi_reset_stats(vsi);
7164 break;
7165
7166 default:
7167 /* no netdev or rings for the other VSI types */
7168 break;
7169 }
7170
7171 return vsi;
7172
7173err_rings:
7174 i40e_vsi_free_q_vectors(vsi);
7175err_msix:
7176 if (vsi->netdev_registered) {
7177 vsi->netdev_registered = false;
7178 unregister_netdev(vsi->netdev);
7179 free_netdev(vsi->netdev);
7180 vsi->netdev = NULL;
7181 }
7182err_netdev:
7183 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
7184err_vsi:
7185 i40e_vsi_clear(vsi);
7186err_alloc:
7187 return NULL;
7188}
7189
7190/**
7191 * i40e_veb_get_bw_info - Query VEB BW information
7192 * @veb: the veb to query
7193 *
7194 * Query the Tx scheduler BW configuration data for given VEB
7195 **/
7196static int i40e_veb_get_bw_info(struct i40e_veb *veb)
7197{
7198 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
7199 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
7200 struct i40e_pf *pf = veb->pf;
7201 struct i40e_hw *hw = &pf->hw;
7202 u32 tc_bw_max;
7203 int ret = 0;
7204 int i;
7205
7206 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
7207 &bw_data, NULL);
7208 if (ret) {
7209 dev_info(&pf->pdev->dev,
7210 "query veb bw config failed, aq_err=%d\n",
7211 hw->aq.asq_last_status);
7212 goto out;
7213 }
7214
7215 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
7216 &ets_data, NULL);
7217 if (ret) {
7218 dev_info(&pf->pdev->dev,
7219 "query veb bw ets config failed, aq_err=%d\n",
7220 hw->aq.asq_last_status);
7221 goto out;
7222 }
7223
7224 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
7225 veb->bw_max_quanta = ets_data.tc_bw_max;
7226 veb->is_abs_credits = bw_data.absolute_credits_enable;
7227 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
7228 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
7229 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7230 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
7231 veb->bw_tc_limit_credits[i] =
7232 le16_to_cpu(bw_data.tc_bw_limits[i]);
7233 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
7234 }
7235
7236out:
7237 return ret;
7238}
7239
7240/**
7241 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
7242 * @pf: board private structure
7243 *
7244 * On error: returns error code (negative)
7245 * On success: returns vsi index in PF (positive)
7246 **/
7247static int i40e_veb_mem_alloc(struct i40e_pf *pf)
7248{
7249 int ret = -ENOENT;
7250 struct i40e_veb *veb;
7251 int i;
7252
7253 /* Need to protect the allocation of switch elements at the PF level */
7254 mutex_lock(&pf->switch_mutex);
7255
7256 /* VEB list may be fragmented if VEB creation/destruction has
7257 * been happening. We can afford to do a quick scan to look
7258 * for any free slots in the list.
7259 *
7260 * find next empty veb slot, looping back around if necessary
7261 */
7262 i = 0;
7263 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
7264 i++;
7265 if (i >= I40E_MAX_VEB) {
7266 ret = -ENOMEM;
7267 goto err_alloc_veb; /* out of VEB slots! */
7268 }
7269
7270 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
7271 if (!veb) {
7272 ret = -ENOMEM;
7273 goto err_alloc_veb;
7274 }
7275 veb->pf = pf;
7276 veb->idx = i;
7277 veb->enabled_tc = 1;
7278
7279 pf->veb[i] = veb;
7280 ret = i;
7281err_alloc_veb:
7282 mutex_unlock(&pf->switch_mutex);
7283 return ret;
7284}
7285
7286/**
7287 * i40e_switch_branch_release - Delete a branch of the switch tree
7288 * @branch: where to start deleting
7289 *
7290 * This uses recursion to find the tips of the branch to be
7291 * removed, deleting until we get back to and can delete this VEB.
7292 **/
7293static void i40e_switch_branch_release(struct i40e_veb *branch)
7294{
7295 struct i40e_pf *pf = branch->pf;
7296 u16 branch_seid = branch->seid;
7297 u16 veb_idx = branch->idx;
7298 int i;
7299
7300 /* release any VEBs on this VEB - RECURSION */
7301 for (i = 0; i < I40E_MAX_VEB; i++) {
7302 if (!pf->veb[i])
7303 continue;
7304 if (pf->veb[i]->uplink_seid == branch->seid)
7305 i40e_switch_branch_release(pf->veb[i]);
7306 }
7307
7308 /* Release the VSIs on this VEB, but not the owner VSI.
7309 *
7310 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
7311 * the VEB itself, so don't use (*branch) after this loop.
7312 */
7313 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
7314 if (!pf->vsi[i])
7315 continue;
7316 if (pf->vsi[i]->uplink_seid == branch_seid &&
7317 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
7318 i40e_vsi_release(pf->vsi[i]);
7319 }
7320 }
7321
7322 /* There's one corner case where the VEB might not have been
7323 * removed, so double check it here and remove it if needed.
7324 * This case happens if the veb was created from the debugfs
7325 * commands and no VSIs were added to it.
7326 */
7327 if (pf->veb[veb_idx])
7328 i40e_veb_release(pf->veb[veb_idx]);
7329}
7330
7331/**
7332 * i40e_veb_clear - remove veb struct
7333 * @veb: the veb to remove
7334 **/
7335static void i40e_veb_clear(struct i40e_veb *veb)
7336{
7337 if (!veb)
7338 return;
7339
7340 if (veb->pf) {
7341 struct i40e_pf *pf = veb->pf;
7342
7343 mutex_lock(&pf->switch_mutex);
7344 if (pf->veb[veb->idx] == veb)
7345 pf->veb[veb->idx] = NULL;
7346 mutex_unlock(&pf->switch_mutex);
7347 }
7348
7349 kfree(veb);
7350}
7351
7352/**
7353 * i40e_veb_release - Delete a VEB and free its resources
7354 * @veb: the VEB being removed
7355 **/
7356void i40e_veb_release(struct i40e_veb *veb)
7357{
7358 struct i40e_vsi *vsi = NULL;
7359 struct i40e_pf *pf;
7360 int i, n = 0;
7361
7362 pf = veb->pf;
7363
7364 /* find the remaining VSI and check for extras */
7365 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
7366 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
7367 n++;
7368 vsi = pf->vsi[i];
7369 }
7370 }
7371 if (n != 1) {
7372 dev_info(&pf->pdev->dev,
7373 "can't remove VEB %d with %d VSIs left\n",
7374 veb->seid, n);
7375 return;
7376 }
7377
7378 /* move the remaining VSI to uplink veb */
7379 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
7380 if (veb->uplink_seid) {
7381 vsi->uplink_seid = veb->uplink_seid;
7382 if (veb->uplink_seid == pf->mac_seid)
7383 vsi->veb_idx = I40E_NO_VEB;
7384 else
7385 vsi->veb_idx = veb->veb_idx;
7386 } else {
7387 /* floating VEB */
7388 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
7389 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
7390 }
7391
7392 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
7393 i40e_veb_clear(veb);
7394
7395 return;
7396}
7397
7398/**
7399 * i40e_add_veb - create the VEB in the switch
7400 * @veb: the VEB to be instantiated
7401 * @vsi: the controlling VSI
7402 **/
7403static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
7404{
Greg Rose56747262013-11-28 06:39:37 +00007405 bool is_default = false;
Kevin Scotte1c51b952013-11-20 10:02:51 +00007406 bool is_cloud = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007407 int ret;
7408
7409 /* get a VEB from the hardware */
7410 ret = i40e_aq_add_veb(&veb->pf->hw, veb->uplink_seid, vsi->seid,
Kevin Scotte1c51b952013-11-20 10:02:51 +00007411 veb->enabled_tc, is_default,
7412 is_cloud, &veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007413 if (ret) {
7414 dev_info(&veb->pf->pdev->dev,
7415 "couldn't add VEB, err %d, aq_err %d\n",
7416 ret, veb->pf->hw.aq.asq_last_status);
7417 return -EPERM;
7418 }
7419
7420 /* get statistics counter */
7421 ret = i40e_aq_get_veb_parameters(&veb->pf->hw, veb->seid, NULL, NULL,
7422 &veb->stats_idx, NULL, NULL, NULL);
7423 if (ret) {
7424 dev_info(&veb->pf->pdev->dev,
7425 "couldn't get VEB statistics idx, err %d, aq_err %d\n",
7426 ret, veb->pf->hw.aq.asq_last_status);
7427 return -EPERM;
7428 }
7429 ret = i40e_veb_get_bw_info(veb);
7430 if (ret) {
7431 dev_info(&veb->pf->pdev->dev,
7432 "couldn't get VEB bw info, err %d, aq_err %d\n",
7433 ret, veb->pf->hw.aq.asq_last_status);
7434 i40e_aq_delete_element(&veb->pf->hw, veb->seid, NULL);
7435 return -ENOENT;
7436 }
7437
7438 vsi->uplink_seid = veb->seid;
7439 vsi->veb_idx = veb->idx;
7440 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
7441
7442 return 0;
7443}
7444
7445/**
7446 * i40e_veb_setup - Set up a VEB
7447 * @pf: board private structure
7448 * @flags: VEB setup flags
7449 * @uplink_seid: the switch element to link to
7450 * @vsi_seid: the initial VSI seid
7451 * @enabled_tc: Enabled TC bit-map
7452 *
7453 * This allocates the sw VEB structure and links it into the switch
7454 * It is possible and legal for this to be a duplicate of an already
7455 * existing VEB. It is also possible for both uplink and vsi seids
7456 * to be zero, in order to create a floating VEB.
7457 *
7458 * Returns pointer to the successfully allocated VEB sw struct on
7459 * success, otherwise returns NULL on failure.
7460 **/
7461struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
7462 u16 uplink_seid, u16 vsi_seid,
7463 u8 enabled_tc)
7464{
7465 struct i40e_veb *veb, *uplink_veb = NULL;
7466 int vsi_idx, veb_idx;
7467 int ret;
7468
7469 /* if one seid is 0, the other must be 0 to create a floating relay */
7470 if ((uplink_seid == 0 || vsi_seid == 0) &&
7471 (uplink_seid + vsi_seid != 0)) {
7472 dev_info(&pf->pdev->dev,
7473 "one, not both seid's are 0: uplink=%d vsi=%d\n",
7474 uplink_seid, vsi_seid);
7475 return NULL;
7476 }
7477
7478 /* make sure there is such a vsi and uplink */
7479 for (vsi_idx = 0; vsi_idx < pf->hw.func_caps.num_vsis; vsi_idx++)
7480 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
7481 break;
7482 if (vsi_idx >= pf->hw.func_caps.num_vsis && vsi_seid != 0) {
7483 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
7484 vsi_seid);
7485 return NULL;
7486 }
7487
7488 if (uplink_seid && uplink_seid != pf->mac_seid) {
7489 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
7490 if (pf->veb[veb_idx] &&
7491 pf->veb[veb_idx]->seid == uplink_seid) {
7492 uplink_veb = pf->veb[veb_idx];
7493 break;
7494 }
7495 }
7496 if (!uplink_veb) {
7497 dev_info(&pf->pdev->dev,
7498 "uplink seid %d not found\n", uplink_seid);
7499 return NULL;
7500 }
7501 }
7502
7503 /* get veb sw struct */
7504 veb_idx = i40e_veb_mem_alloc(pf);
7505 if (veb_idx < 0)
7506 goto err_alloc;
7507 veb = pf->veb[veb_idx];
7508 veb->flags = flags;
7509 veb->uplink_seid = uplink_seid;
7510 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
7511 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
7512
7513 /* create the VEB in the switch */
7514 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
7515 if (ret)
7516 goto err_veb;
7517
7518 return veb;
7519
7520err_veb:
7521 i40e_veb_clear(veb);
7522err_alloc:
7523 return NULL;
7524}
7525
7526/**
7527 * i40e_setup_pf_switch_element - set pf vars based on switch type
7528 * @pf: board private structure
7529 * @ele: element we are building info from
7530 * @num_reported: total number of elements
7531 * @printconfig: should we print the contents
7532 *
7533 * helper function to assist in extracting a few useful SEID values.
7534 **/
7535static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
7536 struct i40e_aqc_switch_config_element_resp *ele,
7537 u16 num_reported, bool printconfig)
7538{
7539 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
7540 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
7541 u8 element_type = ele->element_type;
7542 u16 seid = le16_to_cpu(ele->seid);
7543
7544 if (printconfig)
7545 dev_info(&pf->pdev->dev,
7546 "type=%d seid=%d uplink=%d downlink=%d\n",
7547 element_type, seid, uplink_seid, downlink_seid);
7548
7549 switch (element_type) {
7550 case I40E_SWITCH_ELEMENT_TYPE_MAC:
7551 pf->mac_seid = seid;
7552 break;
7553 case I40E_SWITCH_ELEMENT_TYPE_VEB:
7554 /* Main VEB? */
7555 if (uplink_seid != pf->mac_seid)
7556 break;
7557 if (pf->lan_veb == I40E_NO_VEB) {
7558 int v;
7559
7560 /* find existing or else empty VEB */
7561 for (v = 0; v < I40E_MAX_VEB; v++) {
7562 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
7563 pf->lan_veb = v;
7564 break;
7565 }
7566 }
7567 if (pf->lan_veb == I40E_NO_VEB) {
7568 v = i40e_veb_mem_alloc(pf);
7569 if (v < 0)
7570 break;
7571 pf->lan_veb = v;
7572 }
7573 }
7574
7575 pf->veb[pf->lan_veb]->seid = seid;
7576 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
7577 pf->veb[pf->lan_veb]->pf = pf;
7578 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
7579 break;
7580 case I40E_SWITCH_ELEMENT_TYPE_VSI:
7581 if (num_reported != 1)
7582 break;
7583 /* This is immediately after a reset so we can assume this is
7584 * the PF's VSI
7585 */
7586 pf->mac_seid = uplink_seid;
7587 pf->pf_seid = downlink_seid;
7588 pf->main_vsi_seid = seid;
7589 if (printconfig)
7590 dev_info(&pf->pdev->dev,
7591 "pf_seid=%d main_vsi_seid=%d\n",
7592 pf->pf_seid, pf->main_vsi_seid);
7593 break;
7594 case I40E_SWITCH_ELEMENT_TYPE_PF:
7595 case I40E_SWITCH_ELEMENT_TYPE_VF:
7596 case I40E_SWITCH_ELEMENT_TYPE_EMP:
7597 case I40E_SWITCH_ELEMENT_TYPE_BMC:
7598 case I40E_SWITCH_ELEMENT_TYPE_PE:
7599 case I40E_SWITCH_ELEMENT_TYPE_PA:
7600 /* ignore these for now */
7601 break;
7602 default:
7603 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
7604 element_type, seid);
7605 break;
7606 }
7607}
7608
7609/**
7610 * i40e_fetch_switch_configuration - Get switch config from firmware
7611 * @pf: board private structure
7612 * @printconfig: should we print the contents
7613 *
7614 * Get the current switch configuration from the device and
7615 * extract a few useful SEID values.
7616 **/
7617int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
7618{
7619 struct i40e_aqc_get_switch_config_resp *sw_config;
7620 u16 next_seid = 0;
7621 int ret = 0;
7622 u8 *aq_buf;
7623 int i;
7624
7625 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
7626 if (!aq_buf)
7627 return -ENOMEM;
7628
7629 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
7630 do {
7631 u16 num_reported, num_total;
7632
7633 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
7634 I40E_AQ_LARGE_BUF,
7635 &next_seid, NULL);
7636 if (ret) {
7637 dev_info(&pf->pdev->dev,
7638 "get switch config failed %d aq_err=%x\n",
7639 ret, pf->hw.aq.asq_last_status);
7640 kfree(aq_buf);
7641 return -ENOENT;
7642 }
7643
7644 num_reported = le16_to_cpu(sw_config->header.num_reported);
7645 num_total = le16_to_cpu(sw_config->header.num_total);
7646
7647 if (printconfig)
7648 dev_info(&pf->pdev->dev,
7649 "header: %d reported %d total\n",
7650 num_reported, num_total);
7651
7652 if (num_reported) {
7653 int sz = sizeof(*sw_config) * num_reported;
7654
7655 kfree(pf->sw_config);
7656 pf->sw_config = kzalloc(sz, GFP_KERNEL);
7657 if (pf->sw_config)
7658 memcpy(pf->sw_config, sw_config, sz);
7659 }
7660
7661 for (i = 0; i < num_reported; i++) {
7662 struct i40e_aqc_switch_config_element_resp *ele =
7663 &sw_config->element[i];
7664
7665 i40e_setup_pf_switch_element(pf, ele, num_reported,
7666 printconfig);
7667 }
7668 } while (next_seid != 0);
7669
7670 kfree(aq_buf);
7671 return ret;
7672}
7673
7674/**
7675 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
7676 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007677 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007678 *
7679 * Returns 0 on success, negative value on failure
7680 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007681static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007682{
Jesse Brandeburg895106a2013-11-26 10:49:16 +00007683 u32 rxfc = 0, txfc = 0, rxfc_reg;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007684 int ret;
7685
7686 /* find out what's out there already */
7687 ret = i40e_fetch_switch_configuration(pf, false);
7688 if (ret) {
7689 dev_info(&pf->pdev->dev,
7690 "couldn't fetch switch config, err %d, aq_err %d\n",
7691 ret, pf->hw.aq.asq_last_status);
7692 return ret;
7693 }
7694 i40e_pf_reset_stats(pf);
7695
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007696 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007697 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007698 struct i40e_vsi *vsi = NULL;
7699 u16 uplink_seid;
7700
7701 /* Set up the PF VSI associated with the PF's main VSI
7702 * that is already in the HW switch
7703 */
7704 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
7705 uplink_seid = pf->veb[pf->lan_veb]->seid;
7706 else
7707 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007708 if (pf->lan_vsi == I40E_NO_VSI)
7709 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
7710 else if (reinit)
7711 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007712 if (!vsi) {
7713 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
7714 i40e_fdir_teardown(pf);
7715 return -EAGAIN;
7716 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007717 } else {
7718 /* force a reset of TC and queue layout configurations */
7719 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
7720 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
7721 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
7722 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
7723 }
7724 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
7725
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007726 i40e_fdir_sb_setup(pf);
7727
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007728 /* Setup static PF queue filter control settings */
7729 ret = i40e_setup_pf_filter_control(pf);
7730 if (ret) {
7731 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
7732 ret);
7733 /* Failure here should not stop continuing other steps */
7734 }
7735
7736 /* enable RSS in the HW, even for only one queue, as the stack can use
7737 * the hash
7738 */
7739 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
7740 i40e_config_rss(pf);
7741
7742 /* fill in link information and enable LSE reporting */
7743 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
7744 i40e_link_event(pf);
7745
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007746 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007747 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
7748 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007749 /* requested_mode is set in probe or by ethtool */
7750 if (!pf->fc_autoneg_status)
7751 goto no_autoneg;
7752
7753 if ((pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX) &&
7754 (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007755 pf->hw.fc.current_mode = I40E_FC_FULL;
7756 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX)
7757 pf->hw.fc.current_mode = I40E_FC_TX_PAUSE;
7758 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX)
7759 pf->hw.fc.current_mode = I40E_FC_RX_PAUSE;
7760 else
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007761 pf->hw.fc.current_mode = I40E_FC_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007762
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00007763 /* sync the flow control settings with the auto-neg values */
7764 switch (pf->hw.fc.current_mode) {
7765 case I40E_FC_FULL:
7766 txfc = 1;
7767 rxfc = 1;
7768 break;
7769 case I40E_FC_TX_PAUSE:
7770 txfc = 1;
7771 rxfc = 0;
7772 break;
7773 case I40E_FC_RX_PAUSE:
7774 txfc = 0;
7775 rxfc = 1;
7776 break;
7777 case I40E_FC_NONE:
7778 case I40E_FC_DEFAULT:
7779 txfc = 0;
7780 rxfc = 0;
7781 break;
7782 case I40E_FC_PFC:
7783 /* TBD */
7784 break;
7785 /* no default case, we have to handle all possibilities here */
7786 }
7787
7788 wr32(&pf->hw, I40E_PRTDCB_FCCFG, txfc << I40E_PRTDCB_FCCFG_TFCE_SHIFT);
7789
7790 rxfc_reg = rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
7791 ~I40E_PRTDCB_MFLCN_RFCE_MASK;
7792 rxfc_reg |= (rxfc << I40E_PRTDCB_MFLCN_RFCE_SHIFT);
7793
7794 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rxfc_reg);
7795
7796 goto fc_complete;
7797
7798no_autoneg:
7799 /* disable L2 flow control, user can turn it on if they wish */
7800 wr32(&pf->hw, I40E_PRTDCB_FCCFG, 0);
7801 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
7802 ~I40E_PRTDCB_MFLCN_RFCE_MASK);
7803
7804fc_complete:
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00007805 i40e_ptp_init(pf);
7806
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007807 return ret;
7808}
7809
7810/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007811 * i40e_determine_queue_usage - Work out queue distribution
7812 * @pf: board private structure
7813 **/
7814static void i40e_determine_queue_usage(struct i40e_pf *pf)
7815{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007816 int queues_left;
7817
7818 pf->num_lan_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007819
7820 /* Find the max queues to be put into basic use. We'll always be
7821 * using TC0, whether or not DCB is running, and TC0 will get the
7822 * big RSS set.
7823 */
7824 queues_left = pf->hw.func_caps.num_tx_qp;
7825
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007826 if ((queues_left == 1) ||
7827 !(pf->flags & I40E_FLAG_MSIX_ENABLED) ||
7828 !(pf->flags & (I40E_FLAG_RSS_ENABLED | I40E_FLAG_FD_SB_ENABLED |
7829 I40E_FLAG_DCB_ENABLED))) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007830 /* one qp for PF, no queues for anything else */
7831 queues_left = 0;
7832 pf->rss_size = pf->num_lan_qps = 1;
7833
7834 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007835 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
7836 I40E_FLAG_FD_SB_ENABLED |
7837 I40E_FLAG_FD_ATR_ENABLED |
7838 I40E_FLAG_DCB_ENABLED |
7839 I40E_FLAG_SRIOV_ENABLED |
7840 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007841 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007842 /* Not enough queues for all TCs */
7843 if ((pf->flags & I40E_FLAG_DCB_ENABLED) &&
7844 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
7845 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7846 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
7847 }
7848 pf->num_lan_qps = pf->rss_size_max;
7849 queues_left -= pf->num_lan_qps;
7850 }
7851
7852 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7853 if (queues_left > 1) {
7854 queues_left -= 1; /* save 1 queue for FD */
7855 } else {
7856 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7857 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
7858 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007859 }
7860
7861 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
7862 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08007863 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
7864 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007865 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
7866 }
7867
7868 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7869 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
7870 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
7871 (queues_left / pf->num_vmdq_qps));
7872 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
7873 }
7874
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007875 pf->queues_left = queues_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007876 return;
7877}
7878
7879/**
7880 * i40e_setup_pf_filter_control - Setup PF static filter control
7881 * @pf: PF to be setup
7882 *
7883 * i40e_setup_pf_filter_control sets up a pf's initial filter control
7884 * settings. If PE/FCoE are enabled then it will also set the per PF
7885 * based filter sizes required for them. It also enables Flow director,
7886 * ethertype and macvlan type filter settings for the pf.
7887 *
7888 * Returns 0 on success, negative on failure
7889 **/
7890static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
7891{
7892 struct i40e_filter_control_settings *settings = &pf->filter_settings;
7893
7894 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
7895
7896 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007897 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007898 settings->enable_fdir = true;
7899
7900 /* Ethtype and MACVLAN filters enabled for PF */
7901 settings->enable_ethtype = true;
7902 settings->enable_macvlan = true;
7903
7904 if (i40e_set_filter_control(&pf->hw, settings))
7905 return -ENOENT;
7906
7907 return 0;
7908}
7909
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00007910#define INFO_STRING_LEN 255
7911static void i40e_print_features(struct i40e_pf *pf)
7912{
7913 struct i40e_hw *hw = &pf->hw;
7914 char *buf, *string;
7915
7916 string = kzalloc(INFO_STRING_LEN, GFP_KERNEL);
7917 if (!string) {
7918 dev_err(&pf->pdev->dev, "Features string allocation failed\n");
7919 return;
7920 }
7921
7922 buf = string;
7923
7924 buf += sprintf(string, "Features: PF-id[%d] ", hw->pf_id);
7925#ifdef CONFIG_PCI_IOV
7926 buf += sprintf(buf, "VFs: %d ", pf->num_req_vfs);
7927#endif
7928 buf += sprintf(buf, "VSIs: %d QP: %d ", pf->hw.func_caps.num_vsis,
7929 pf->vsi[pf->lan_vsi]->num_queue_pairs);
7930
7931 if (pf->flags & I40E_FLAG_RSS_ENABLED)
7932 buf += sprintf(buf, "RSS ");
7933 buf += sprintf(buf, "FDir ");
7934 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
7935 buf += sprintf(buf, "ATR ");
7936 if (pf->flags & I40E_FLAG_FD_SB_ENABLED)
7937 buf += sprintf(buf, "NTUPLE ");
7938 if (pf->flags & I40E_FLAG_DCB_ENABLED)
7939 buf += sprintf(buf, "DCB ");
7940 if (pf->flags & I40E_FLAG_PTP)
7941 buf += sprintf(buf, "PTP ");
7942
7943 BUG_ON(buf > (string + INFO_STRING_LEN));
7944 dev_info(&pf->pdev->dev, "%s\n", string);
7945 kfree(string);
7946}
7947
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007948/**
7949 * i40e_probe - Device initialization routine
7950 * @pdev: PCI device information struct
7951 * @ent: entry in i40e_pci_tbl
7952 *
7953 * i40e_probe initializes a pf identified by a pci_dev structure.
7954 * The OS initialization, configuring of the pf private structure,
7955 * and a hardware reset occur.
7956 *
7957 * Returns 0 on success, negative on failure
7958 **/
7959static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
7960{
7961 struct i40e_driver_version dv;
7962 struct i40e_pf *pf;
7963 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00007964 static u16 pfs_found;
Catherine Sullivand4dfb812013-11-28 06:39:21 +00007965 u16 link_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007966 int err = 0;
7967 u32 len;
7968
7969 err = pci_enable_device_mem(pdev);
7970 if (err)
7971 return err;
7972
7973 /* set up for high or low dma */
7974 if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64))) {
7975 /* coherent mask for the same size will always succeed if
7976 * dma_set_mask does
7977 */
7978 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
7979 } else if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
7980 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
7981 } else {
7982 dev_err(&pdev->dev, "DMA configuration failed: %d\n", err);
7983 err = -EIO;
7984 goto err_dma;
7985 }
7986
7987 /* set up pci connections */
7988 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
7989 IORESOURCE_MEM), i40e_driver_name);
7990 if (err) {
7991 dev_info(&pdev->dev,
7992 "pci_request_selected_regions failed %d\n", err);
7993 goto err_pci_reg;
7994 }
7995
7996 pci_enable_pcie_error_reporting(pdev);
7997 pci_set_master(pdev);
7998
7999 /* Now that we have a PCI connection, we need to do the
8000 * low level device setup. This is primarily setting up
8001 * the Admin Queue structures and then querying for the
8002 * device's current profile information.
8003 */
8004 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
8005 if (!pf) {
8006 err = -ENOMEM;
8007 goto err_pf_alloc;
8008 }
8009 pf->next_vsi = 0;
8010 pf->pdev = pdev;
8011 set_bit(__I40E_DOWN, &pf->state);
8012
8013 hw = &pf->hw;
8014 hw->back = pf;
8015 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
8016 pci_resource_len(pdev, 0));
8017 if (!hw->hw_addr) {
8018 err = -EIO;
8019 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
8020 (unsigned int)pci_resource_start(pdev, 0),
8021 (unsigned int)pci_resource_len(pdev, 0), err);
8022 goto err_ioremap;
8023 }
8024 hw->vendor_id = pdev->vendor;
8025 hw->device_id = pdev->device;
8026 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
8027 hw->subsystem_vendor_id = pdev->subsystem_vendor;
8028 hw->subsystem_device_id = pdev->subsystem_device;
8029 hw->bus.device = PCI_SLOT(pdev->devfn);
8030 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008031 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008032
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008033 /* do a special CORER for clearing PXE mode once at init */
8034 if (hw->revision_id == 0 &&
8035 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
8036 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
8037 i40e_flush(hw);
8038 msleep(200);
8039 pf->corer_count++;
8040
8041 i40e_clear_pxe_mode(hw);
8042 }
8043
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008044 /* Reset here to make sure all is clean and to define PF 'n' */
8045 err = i40e_pf_reset(hw);
8046 if (err) {
8047 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
8048 goto err_pf_reset;
8049 }
8050 pf->pfr_count++;
8051
8052 hw->aq.num_arq_entries = I40E_AQ_LEN;
8053 hw->aq.num_asq_entries = I40E_AQ_LEN;
8054 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
8055 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
8056 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
8057 snprintf(pf->misc_int_name, sizeof(pf->misc_int_name) - 1,
8058 "%s-pf%d:misc",
8059 dev_driver_string(&pf->pdev->dev), pf->hw.pf_id);
8060
8061 err = i40e_init_shared_code(hw);
8062 if (err) {
8063 dev_info(&pdev->dev, "init_shared_code failed: %d\n", err);
8064 goto err_pf_reset;
8065 }
8066
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +00008067 /* set up a default setting for link flow control */
8068 pf->hw.fc.requested_mode = I40E_FC_NONE;
8069
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008070 err = i40e_init_adminq(hw);
8071 dev_info(&pdev->dev, "%s\n", i40e_fw_version_str(hw));
8072 if (err) {
8073 dev_info(&pdev->dev,
8074 "init_adminq failed: %d expecting API %02x.%02x\n",
8075 err,
8076 I40E_FW_API_VERSION_MAJOR, I40E_FW_API_VERSION_MINOR);
8077 goto err_pf_reset;
8078 }
8079
Shannon Nelson6ff4ef82013-12-21 05:44:49 +00008080 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008081 err = i40e_get_capabilities(pf);
8082 if (err)
8083 goto err_adminq_setup;
8084
8085 err = i40e_sw_init(pf);
8086 if (err) {
8087 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
8088 goto err_sw_init;
8089 }
8090
8091 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
8092 hw->func_caps.num_rx_qp,
8093 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
8094 if (err) {
8095 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
8096 goto err_init_lan_hmc;
8097 }
8098
8099 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
8100 if (err) {
8101 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
8102 err = -ENOENT;
8103 goto err_configure_lan_hmc;
8104 }
8105
8106 i40e_get_mac_addr(hw, hw->mac.addr);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +00008107 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008108 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
8109 err = -EIO;
8110 goto err_mac_addr;
8111 }
8112 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
8113 memcpy(hw->mac.perm_addr, hw->mac.addr, ETH_ALEN);
8114
8115 pci_set_drvdata(pdev, pf);
8116 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008117#ifdef CONFIG_I40E_DCB
8118 err = i40e_init_pf_dcb(pf);
8119 if (err) {
8120 dev_info(&pdev->dev, "init_pf_dcb failed: %d\n", err);
8121 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8122 goto err_init_dcb;
8123 }
8124#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008125
8126 /* set up periodic task facility */
8127 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
8128 pf->service_timer_period = HZ;
8129
8130 INIT_WORK(&pf->service_task, i40e_service_task);
8131 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
8132 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
8133 pf->link_check_timeout = jiffies;
8134
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008135 /* WoL defaults to disabled */
8136 pf->wol_en = false;
8137 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
8138
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008139 /* set up the main switch operations */
8140 i40e_determine_queue_usage(pf);
8141 i40e_init_interrupt_scheme(pf);
8142
8143 /* Set up the *vsi struct based on the number of VSIs in the HW,
8144 * and set up our local tracking of the MAIN PF vsi.
8145 */
8146 len = sizeof(struct i40e_vsi *) * pf->hw.func_caps.num_vsis;
8147 pf->vsi = kzalloc(len, GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +00008148 if (!pf->vsi) {
8149 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008150 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +00008151 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008152
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00008153 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008154 if (err) {
8155 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
8156 goto err_vsis;
8157 }
8158
8159 /* The main driver is (mostly) up and happy. We need to set this state
8160 * before setting up the misc vector or we get a race and the vector
8161 * ends up disabled forever.
8162 */
8163 clear_bit(__I40E_DOWN, &pf->state);
8164
8165 /* In case of MSIX we are going to setup the misc vector right here
8166 * to handle admin queue events etc. In case of legacy and MSI
8167 * the misc functionality and queue processing is combined in
8168 * the same vector and that gets setup at open.
8169 */
8170 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
8171 err = i40e_setup_misc_vector(pf);
8172 if (err) {
8173 dev_info(&pdev->dev,
8174 "setup of misc vector failed: %d\n", err);
8175 goto err_vsis;
8176 }
8177 }
8178
8179 /* prep for VF support */
8180 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
8181 (pf->flags & I40E_FLAG_MSIX_ENABLED)) {
8182 u32 val;
8183
8184 /* disable link interrupts for VFs */
8185 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
8186 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
8187 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
8188 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -08008189
8190 if (pci_num_vf(pdev)) {
8191 dev_info(&pdev->dev,
8192 "Active VFs found, allocating resources.\n");
8193 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
8194 if (err)
8195 dev_info(&pdev->dev,
8196 "Error %d allocating resources for existing VFs\n",
8197 err);
8198 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008199 }
8200
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +00008201 pfs_found++;
8202
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008203 i40e_dbg_pf_init(pf);
8204
8205 /* tell the firmware that we're starting */
8206 dv.major_version = DRV_VERSION_MAJOR;
8207 dv.minor_version = DRV_VERSION_MINOR;
8208 dv.build_version = DRV_VERSION_BUILD;
8209 dv.subbuild_version = 0;
8210 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
8211
8212 /* since everything's happy, start the service_task timer */
8213 mod_timer(&pf->service_timer,
8214 round_jiffies(jiffies + pf->service_timer_period));
8215
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008216 /* Get the negotiated link width and speed from PCI config space */
8217 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA, &link_status);
8218
8219 i40e_set_pci_config_data(hw, link_status);
8220
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00008221 dev_info(&pdev->dev, "PCI-Express: %s %s\n",
Catherine Sullivand4dfb812013-11-28 06:39:21 +00008222 (hw->bus.speed == i40e_bus_speed_8000 ? "Speed 8.0GT/s" :
8223 hw->bus.speed == i40e_bus_speed_5000 ? "Speed 5.0GT/s" :
8224 hw->bus.speed == i40e_bus_speed_2500 ? "Speed 2.5GT/s" :
8225 "Unknown"),
8226 (hw->bus.width == i40e_bus_width_pcie_x8 ? "Width x8" :
8227 hw->bus.width == i40e_bus_width_pcie_x4 ? "Width x4" :
8228 hw->bus.width == i40e_bus_width_pcie_x2 ? "Width x2" :
8229 hw->bus.width == i40e_bus_width_pcie_x1 ? "Width x1" :
8230 "Unknown"));
8231
8232 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
8233 hw->bus.speed < i40e_bus_speed_8000) {
8234 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
8235 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
8236 }
8237
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +00008238 /* print a string summarizing features */
8239 i40e_print_features(pf);
8240
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008241 return 0;
8242
8243 /* Unwind what we've done if something failed in the setup */
8244err_vsis:
8245 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008246 i40e_clear_interrupt_scheme(pf);
8247 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +00008248err_switch_setup:
8249 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008250 del_timer_sync(&pf->service_timer);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008251#ifdef CONFIG_I40E_DCB
8252err_init_dcb:
8253#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008254err_mac_addr:
8255err_configure_lan_hmc:
8256 (void)i40e_shutdown_lan_hmc(hw);
8257err_init_lan_hmc:
8258 kfree(pf->qp_pile);
8259 kfree(pf->irq_pile);
8260err_sw_init:
8261err_adminq_setup:
8262 (void)i40e_shutdown_adminq(hw);
8263err_pf_reset:
8264 iounmap(hw->hw_addr);
8265err_ioremap:
8266 kfree(pf);
8267err_pf_alloc:
8268 pci_disable_pcie_error_reporting(pdev);
8269 pci_release_selected_regions(pdev,
8270 pci_select_bars(pdev, IORESOURCE_MEM));
8271err_pci_reg:
8272err_dma:
8273 pci_disable_device(pdev);
8274 return err;
8275}
8276
8277/**
8278 * i40e_remove - Device removal routine
8279 * @pdev: PCI device information struct
8280 *
8281 * i40e_remove is called by the PCI subsystem to alert the driver
8282 * that is should release a PCI device. This could be caused by a
8283 * Hot-Plug event, or because the driver is going to be removed from
8284 * memory.
8285 **/
8286static void i40e_remove(struct pci_dev *pdev)
8287{
8288 struct i40e_pf *pf = pci_get_drvdata(pdev);
8289 i40e_status ret_code;
8290 u32 reg;
8291 int i;
8292
8293 i40e_dbg_pf_exit(pf);
8294
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008295 i40e_ptp_stop(pf);
8296
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008297 /* no more scheduling of any task */
8298 set_bit(__I40E_DOWN, &pf->state);
8299 del_timer_sync(&pf->service_timer);
8300 cancel_work_sync(&pf->service_task);
8301
Mitch Williamseb2d80b2014-02-13 03:48:48 -08008302 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
8303 i40e_free_vfs(pf);
8304 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
8305 }
8306
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008307 i40e_fdir_teardown(pf);
8308
8309 /* If there is a switch structure or any orphans, remove them.
8310 * This will leave only the PF's VSI remaining.
8311 */
8312 for (i = 0; i < I40E_MAX_VEB; i++) {
8313 if (!pf->veb[i])
8314 continue;
8315
8316 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
8317 pf->veb[i]->uplink_seid == 0)
8318 i40e_switch_branch_release(pf->veb[i]);
8319 }
8320
8321 /* Now we can shutdown the PF's VSI, just before we kill
8322 * adminq and hmc.
8323 */
8324 if (pf->vsi[pf->lan_vsi])
8325 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
8326
8327 i40e_stop_misc_vector(pf);
8328 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
8329 synchronize_irq(pf->msix_entries[0].vector);
8330 free_irq(pf->msix_entries[0].vector, pf);
8331 }
8332
8333 /* shutdown and destroy the HMC */
8334 ret_code = i40e_shutdown_lan_hmc(&pf->hw);
8335 if (ret_code)
8336 dev_warn(&pdev->dev,
8337 "Failed to destroy the HMC resources: %d\n", ret_code);
8338
8339 /* shutdown the adminq */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008340 ret_code = i40e_shutdown_adminq(&pf->hw);
8341 if (ret_code)
8342 dev_warn(&pdev->dev,
8343 "Failed to destroy the Admin Queue resources: %d\n",
8344 ret_code);
8345
8346 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
8347 i40e_clear_interrupt_scheme(pf);
8348 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
8349 if (pf->vsi[i]) {
8350 i40e_vsi_clear_rings(pf->vsi[i]);
8351 i40e_vsi_clear(pf->vsi[i]);
8352 pf->vsi[i] = NULL;
8353 }
8354 }
8355
8356 for (i = 0; i < I40E_MAX_VEB; i++) {
8357 kfree(pf->veb[i]);
8358 pf->veb[i] = NULL;
8359 }
8360
8361 kfree(pf->qp_pile);
8362 kfree(pf->irq_pile);
8363 kfree(pf->sw_config);
8364 kfree(pf->vsi);
8365
8366 /* force a PF reset to clean anything leftover */
8367 reg = rd32(&pf->hw, I40E_PFGEN_CTRL);
8368 wr32(&pf->hw, I40E_PFGEN_CTRL, (reg | I40E_PFGEN_CTRL_PFSWR_MASK));
8369 i40e_flush(&pf->hw);
8370
8371 iounmap(pf->hw.hw_addr);
8372 kfree(pf);
8373 pci_release_selected_regions(pdev,
8374 pci_select_bars(pdev, IORESOURCE_MEM));
8375
8376 pci_disable_pcie_error_reporting(pdev);
8377 pci_disable_device(pdev);
8378}
8379
8380/**
8381 * i40e_pci_error_detected - warning that something funky happened in PCI land
8382 * @pdev: PCI device information struct
8383 *
8384 * Called to warn that something happened and the error handling steps
8385 * are in progress. Allows the driver to quiesce things, be ready for
8386 * remediation.
8387 **/
8388static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
8389 enum pci_channel_state error)
8390{
8391 struct i40e_pf *pf = pci_get_drvdata(pdev);
8392
8393 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
8394
8395 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008396 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
8397 rtnl_lock();
8398 i40e_prep_for_reset(pf);
8399 rtnl_unlock();
8400 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008401
8402 /* Request a slot reset */
8403 return PCI_ERS_RESULT_NEED_RESET;
8404}
8405
8406/**
8407 * i40e_pci_error_slot_reset - a PCI slot reset just happened
8408 * @pdev: PCI device information struct
8409 *
8410 * Called to find if the driver can work with the device now that
8411 * the pci slot has been reset. If a basic connection seems good
8412 * (registers are readable and have sane content) then return a
8413 * happy little PCI_ERS_RESULT_xxx.
8414 **/
8415static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
8416{
8417 struct i40e_pf *pf = pci_get_drvdata(pdev);
8418 pci_ers_result_t result;
8419 int err;
8420 u32 reg;
8421
8422 dev_info(&pdev->dev, "%s\n", __func__);
8423 if (pci_enable_device_mem(pdev)) {
8424 dev_info(&pdev->dev,
8425 "Cannot re-enable PCI device after reset.\n");
8426 result = PCI_ERS_RESULT_DISCONNECT;
8427 } else {
8428 pci_set_master(pdev);
8429 pci_restore_state(pdev);
8430 pci_save_state(pdev);
8431 pci_wake_from_d3(pdev, false);
8432
8433 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8434 if (reg == 0)
8435 result = PCI_ERS_RESULT_RECOVERED;
8436 else
8437 result = PCI_ERS_RESULT_DISCONNECT;
8438 }
8439
8440 err = pci_cleanup_aer_uncorrect_error_status(pdev);
8441 if (err) {
8442 dev_info(&pdev->dev,
8443 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
8444 err);
8445 /* non-fatal, continue */
8446 }
8447
8448 return result;
8449}
8450
8451/**
8452 * i40e_pci_error_resume - restart operations after PCI error recovery
8453 * @pdev: PCI device information struct
8454 *
8455 * Called to allow the driver to bring things back up after PCI error
8456 * and/or reset recovery has finished.
8457 **/
8458static void i40e_pci_error_resume(struct pci_dev *pdev)
8459{
8460 struct i40e_pf *pf = pci_get_drvdata(pdev);
8461
8462 dev_info(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008463 if (test_bit(__I40E_SUSPENDED, &pf->state))
8464 return;
8465
8466 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008467 i40e_handle_reset_warning(pf);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008468 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008469}
8470
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008471/**
8472 * i40e_shutdown - PCI callback for shutting down
8473 * @pdev: PCI device information struct
8474 **/
8475static void i40e_shutdown(struct pci_dev *pdev)
8476{
8477 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008478 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008479
8480 set_bit(__I40E_SUSPENDED, &pf->state);
8481 set_bit(__I40E_DOWN, &pf->state);
8482 rtnl_lock();
8483 i40e_prep_for_reset(pf);
8484 rtnl_unlock();
8485
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008486 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
8487 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
8488
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008489 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008490 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008491 pci_set_power_state(pdev, PCI_D3hot);
8492 }
8493}
8494
8495#ifdef CONFIG_PM
8496/**
8497 * i40e_suspend - PCI callback for moving to D3
8498 * @pdev: PCI device information struct
8499 **/
8500static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
8501{
8502 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008503 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008504
8505 set_bit(__I40E_SUSPENDED, &pf->state);
8506 set_bit(__I40E_DOWN, &pf->state);
8507 rtnl_lock();
8508 i40e_prep_for_reset(pf);
8509 rtnl_unlock();
8510
Shannon Nelson8e2773a2013-11-28 06:39:22 +00008511 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
8512 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
8513
8514 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008515 pci_set_power_state(pdev, PCI_D3hot);
8516
8517 return 0;
8518}
8519
8520/**
8521 * i40e_resume - PCI callback for waking up from D3
8522 * @pdev: PCI device information struct
8523 **/
8524static int i40e_resume(struct pci_dev *pdev)
8525{
8526 struct i40e_pf *pf = pci_get_drvdata(pdev);
8527 u32 err;
8528
8529 pci_set_power_state(pdev, PCI_D0);
8530 pci_restore_state(pdev);
8531 /* pci_restore_state() clears dev->state_saves, so
8532 * call pci_save_state() again to restore it.
8533 */
8534 pci_save_state(pdev);
8535
8536 err = pci_enable_device_mem(pdev);
8537 if (err) {
8538 dev_err(&pdev->dev,
8539 "%s: Cannot enable PCI device from suspend\n",
8540 __func__);
8541 return err;
8542 }
8543 pci_set_master(pdev);
8544
8545 /* no wakeup events while running */
8546 pci_wake_from_d3(pdev, false);
8547
8548 /* handling the reset will rebuild the device state */
8549 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
8550 clear_bit(__I40E_DOWN, &pf->state);
8551 rtnl_lock();
8552 i40e_reset_and_rebuild(pf, false);
8553 rtnl_unlock();
8554 }
8555
8556 return 0;
8557}
8558
8559#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008560static const struct pci_error_handlers i40e_err_handler = {
8561 .error_detected = i40e_pci_error_detected,
8562 .slot_reset = i40e_pci_error_slot_reset,
8563 .resume = i40e_pci_error_resume,
8564};
8565
8566static struct pci_driver i40e_driver = {
8567 .name = i40e_driver_name,
8568 .id_table = i40e_pci_tbl,
8569 .probe = i40e_probe,
8570 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +00008571#ifdef CONFIG_PM
8572 .suspend = i40e_suspend,
8573 .resume = i40e_resume,
8574#endif
8575 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008576 .err_handler = &i40e_err_handler,
8577 .sriov_configure = i40e_pci_sriov_configure,
8578};
8579
8580/**
8581 * i40e_init_module - Driver registration routine
8582 *
8583 * i40e_init_module is the first routine called when the driver is
8584 * loaded. All it does is register with the PCI subsystem.
8585 **/
8586static int __init i40e_init_module(void)
8587{
8588 pr_info("%s: %s - version %s\n", i40e_driver_name,
8589 i40e_driver_string, i40e_driver_version_str);
8590 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
8591 i40e_dbg_init();
8592 return pci_register_driver(&i40e_driver);
8593}
8594module_init(i40e_init_module);
8595
8596/**
8597 * i40e_exit_module - Driver exit cleanup routine
8598 *
8599 * i40e_exit_module is called just before the driver is removed
8600 * from memory.
8601 **/
8602static void __exit i40e_exit_module(void)
8603{
8604 pci_unregister_driver(&i40e_driver);
8605 i40e_dbg_exit();
8606}
8607module_exit(i40e_exit_module);